Skip to content

Commit

Permalink
Merge pull request #36 from sinricpro/2.8.1
Browse files Browse the repository at this point in the history
getCameraStreamUrl, getWebRTCAnswer added
  • Loading branch information
kakopappa authored Jul 7, 2023
2 parents 8d40e5c + 69302ca commit e3a6bef
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 71 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = {
"no-underscore-dangle": "off",
quotes: "off",
"linebreak-style": 0,
printWidth: 120,
},
};
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 120
}
64 changes: 64 additions & 0 deletions examples/camera/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const {
SinricPro, startSinricPro, raiseEvent, eventNames,
} = require('sinricpro');

const fetch = require('node-fetch');

const APPKEY = "";
const APPSECRET = "";
const cameraId = "";
const deviceIds = [cameraId];

async function mediamtx(offer) {
/*
Get the answer from mediamtx `http://<hostname>:8889/<name>/whep`. eg: http://pi3:8889/cam/whep
*/
const url = `http://pi3:8889/cam/whep`;
const response = await fetch(url, {
headers: {
"content-type": "application/sdp",
},
body: offer,
method: "POST",
});

// eslint-disable-next-line no-return-await
return await response.text();
}

const getWebRTCAnswer = async (deviceid, base64Offer) => {
// Alexa Eco needs camera stream answer
const offer = Buffer.from(base64Offer, 'base64').toString();
try {
const answer = await mediamtx(offer);
const answerInBase64 = Buffer.from(answer).toString('base64');
return { success: true, answer: answerInBase64 };
} catch (error) {
console.error(error);
return { success: false };
}
};

const getCameraStreamUrl = async (deviceid, cameraStreamProtocol) => {
// Google Home: RTSP protocol not supported. Requires a Chromecast TV or Google Nest Hub
// Alexa: RTSP url must be interleaved TCP on port 443 (for both RTP and RTSP) over TLS 1.2 port 443

if(cameraStreamProtocol === 'hls') {
return { success: true, url: 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8' };
}
else if(cameraStreamProtocol === 'rtsp') {
return { success: true, url: 'rtsp://rtspurl:443' };
} else {
return { success: false};
}
};

const setPowerState = async (deviceid, data) => {
console.log(deviceid, data);
return true;
};

const sinricpro = new SinricPro(APPKEY, deviceIds, APPSECRET, false);
const callbacks = { setPowerState, getWebRTCAnswer, getCameraStreamUrl };

startSinricPro(sinricpro, callbacks);
42 changes: 0 additions & 42 deletions examples/webrtc/webrtc.js

This file was deleted.

22 changes: 14 additions & 8 deletions lib/cbhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { Bands, BandReset } = require('./contorllers/speaker_controller');
const { AdjustBands } = require('./contorllers/speaker_controller');
const { ToggleState } = require('./contorllers/toggle_state_controller');
const { Mode } = require('./contorllers/mode_controller');
const { WebRTC } = require('./contorllers/webrtc_controller');
const { CameraStreamController } = require('./contorllers/camera_stream_controller');
const { UndefinedCallbackError, InvalidResponseError } = require('./errors/errors');

const jsonResponse = (client, defaultPayload, payloadValue, instanceId) => {
Expand Down Expand Up @@ -153,16 +153,22 @@ const handlePayload = (client, payload, signature, callbacks) => verifySignature
return ToggleState(payload, callbacks)
.then(([value, instanceId]) => jsonResponse(client, payload, value, instanceId))
.catch(handleError);
} else if (action === 'webrtcOffer') {
return WebRTC(payload, callbacks)
.then(([value, answer]) => jsonResponse(client, payload, { answer: answer } ))
.catch((err) => {
if (process.env.SR_DEBUG) console.log(err.message);
});
} else if (action === 'getWebRTCAnswer') {
return CameraStreamController(payload, callbacks)
.then((answer) => jsonResponse(client, payload, { answer }))
.catch(handleError);
} else if (action === 'getCameraStreamUrl') {
return CameraStreamController(payload, callbacks)
.then((url) => jsonResponse(client, payload, { url }))
.catch(handleError);
} else if (action === 'getWebRTCOffer') {
return CameraStreamController(payload, callbacks)
.then((offer) => jsonResponse(client, payload, { offer }))
.catch(handleError);
}
return {};
})
.catch((err) => {
console.error(err.message);
console.error(err);
});
module.exports = handlePayload;
39 changes: 39 additions & 0 deletions lib/contorllers/camera_stream_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2019 Sinric. All rights reserved.
* Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
*
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
*/
const { UndefinedCallbackError, InvalidResponseError } = require("../errors/errors");

const CameraStreamController = async (payload, callbacks) => {
if (payload.action === "getWebRTCAnswer" && callbacks.getWebRTCAnswer) {
const data = await callbacks.getWebRTCAnswer(payload.deviceId, payload.value.offer);

if (data.answer) {
return data.answer;
}
throw new InvalidResponseError(" error");
}
if (payload.action === "getWebRTCOffer" && callbacks.getWebRTCOffer) {
const data = await callbacks.getWebRTCOffer(payload.deviceId);

if (data.offer) {
return data.offer;
}
throw new InvalidResponseError(" error");
}
else if (payload.action === "getCameraStreamUrl" && callbacks.getCameraStreamUrl) {
const data = await callbacks.getCameraStreamUrl(payload.deviceId, payload.value.protocol);
if (data.url) {
return data.url;
}
throw new InvalidResponseError("getCameraStreamUrl error");
} else {
throw new UndefinedCallbackError("getCameraStreamUrl callback is undefined.");
}
};

module.exports = {
CameraStreamController,
};
21 changes: 0 additions & 21 deletions lib/contorllers/webrtc_controller.js

This file was deleted.

0 comments on commit e3a6bef

Please sign in to comment.