From 0fd40ed0c186c33dd9d157c0347c287834f9bc15 Mon Sep 17 00:00:00 2001 From: Fabrizio Bertone Date: Wed, 12 Apr 2017 21:01:24 +0200 Subject: [PATCH] get/stop video and audio live streams, init camera configuration --- CHANGELOG.md | 15 ++++ index.js | 32 ++++++++- lib/camClient.js | 176 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 218 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 133019e..510d2f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ +# 1.2.0 (2017-04-12) + +### Features + +* **client:** (*camera*) Get snapshot +* **client:** (*camera*) Get video stream +* **client:** (*camera*) Stop video stream +* **client:** (*camera*) Get audio stream +* **client:** (*camera*) Stop audio stream + +### Enhancements + +* **client:** (*camera*) Better handling of pings +* **client:** (*camera*) Acks sending for responses, video and audio packets + # 1.1.0 (2017-03-31) ### Features diff --git a/index.js b/index.js index 9b6f474..0f4548c 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,12 @@ const client = function client (opts = {}, udpSocket) { } // TODO better buffer handling (deal with ordered sequence numbers, packet repetitions and packet losses) + // create a 'buffer' object and put packets as properties using seq as key. + // when a message is complete remove all parts and return complete + + // TODO deal with retransmission of our packets lost during transmission + // -> must check remote acks and pings + // -> must keep a cache of our packets socket.on('message', function (msg, rinfo) { // check if message is from a server or the camera @@ -84,7 +90,7 @@ const client = function client (opts = {}, udpSocket) { camClient.sendPing(socket, {host: rinfo.address, port: rinfo.port}) let now = Date.now() let past = now - currentCamSession.lastTimeReceivedPacket - if (past > 10000) { + if (past > 10000) { // TODO should send close message and reset the connection at this point emitter.emit('lostConnection', {lastReceived: currentCamSession.lastTimeReceivedPacket, timePast: past, message: `not receiving packets since ${past / 1000} seconds`}) } } @@ -222,6 +228,26 @@ const client = function client (opts = {}, udpSocket) { currentCamSession.mySeq++ } + const getAudioStream = function getAudioStream (stream = 1) { + camClient.getAudioStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials, stream) + currentCamSession.mySeq++ + } + + const getVideoStream = function getVideoStream (stream = 10) { + camClient.getVideoStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials, stream) + currentCamSession.mySeq++ + } + + const stopAudioStream = function stopAudioStream () { + camClient.stopAudioStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials) + currentCamSession.mySeq++ + } + + const stopVideoStream = function stopVideoStream () { + camClient.stopVideoStream(socket, currentCamSession.address, currentCamSession.mySeq, camCredentials) + currentCamSession.mySeq++ + } + const sendPing = function sendPing () { camClient.sendPing(socket, currentCamSession.address) } @@ -254,6 +280,10 @@ const client = function client (opts = {}, udpSocket) { sendMultipleGet, sendGet, getSnapshot, + getAudioStream, + getVideoStream, + stopAudioStream, + stopVideoStream, sendPing, on } diff --git a/lib/camClient.js b/lib/camClient.js index 298a12a..6a6e9d3 100644 --- a/lib/camClient.js +++ b/lib/camClient.js @@ -90,16 +90,26 @@ const getSnapshot = function getSnapshot (socket, address, seq, creds) { sendGet(socket, address, seq, url) } -const getAudiostream = function getAudiostream (socket, address, seq, streamid, creds) { +const getAudioStream = function getAudioStream (socket, address, seq, creds, streamid = 1) { let url = `/audiostream.cgi?streamid=${streamid}&loginuse=${creds.user}&loginpas=${creds.pass}&user=${creds.user}&pwd=${creds.pass}&` sendGet(socket, address, seq, url) } -const getVideostream = function getVideostream (socket, address, seq, streamid, creds) { +const getVideoStream = function getVideoStream (socket, address, seq, creds, streamid = 10) { let url = `/livestream.cgi?streamid=${streamid}&loginuse=${creds.user}&loginpas=${creds.pass}&user=${creds.user}&pwd=${creds.pass}&` sendGet(socket, address, seq, url) } +const stopAudioStream = function stopAudioStream (socket, address, seq, creds) { + let url = `/audiostream.cgi?streamid=16&loginuse=${creds.user}&loginpas=${creds.pass}&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopVideoStream = function stopVideoStream (socket, address, seq, creds) { + let url = `/livestream.cgi?streamid=16&loginuse=${creds.user}&loginpas=${creds.pass}&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + const sendGet = function sendGet (socket, address, seq, url) { let urlBuf = Buffer.from(url) let payloadHead = Buffer.from([0xd1, 0x00, Math.floor(seq / 256), seq % 256, 0x01, 0x0a, 0x00, Math.floor((url.length + 4) / 256), (url.length + 4) % 256, 0x00, 0x00, 0x00, 0x47, 0x45, 0x54, 0x20]) @@ -170,6 +180,136 @@ const parseHttp = function parseHttp (msg, cb) { cb({size: size, header: header, payload: payload}) } +const stepDown = function stepDown (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=2&onestep=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stepUp = function stepUp (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=0&onestep=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stepRight = function stepRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=6&onestep=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stepLeft = function stepLeft (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=4&onestep=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveDown = function moveDown (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=2&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveUp = function moveUp (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=0&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveRight = function moveRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=6&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveLeft = function moveLeft (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=4&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopMoveDown = function stopMoveDown (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=3&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopMoveUp = function stopMoveUp (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=1&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopMoveRight = function stopMoveRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=7&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopMoveLeft = function stopMoveLeft (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=5&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveDownRight = function moveDownRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=93&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveUpRight = function moveUpRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=91&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveUpLeft = function moveUpLeft (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=90&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const moveDownLeft = function moveDownLeft (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=92&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const stopMove = function stopMove (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=1&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const resetPosition = function resetPosition (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=25&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const upAndDown = function upAndDown (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=26&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const leftAndRight = function leftAndRight (socket, address, seq, creds) { + let url = `/decoder_control.cgi?command=28&onestep=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const reverseImage = function reverseImage (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=5&value=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const mirrorImage = function mirrorImage (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=5&value=2&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const resetImage = function resetImage (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=5&value=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const reverseAndMirrorImage = function reverseAndMirrorImage (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=5&value=3&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const activateInfrared = function activateInfrared (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=14&value=1&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + +const deactivateInfrared = function deactivateInfrared (socket, address, seq, creds) { + let url = `/camera_control.cgi?param=14&value=0&user=${creds.user}&pwd=${creds.pass}&` + sendGet(socket, address, seq, url) +} + module.exports.openSession = openSession module.exports.sendPing = sendPing module.exports.sendPong = sendPong @@ -180,11 +320,39 @@ module.exports.checkCredentials = checkCredentials module.exports.sendMultipleGet = sendMultipleGet module.exports.getParameters = getParameters module.exports.getSnapshot = getSnapshot -module.exports.getAudiostream = getAudiostream -module.exports.getVideostream = getVideostream +module.exports.getAudioStream = getAudioStream +module.exports.getVideoStream = getVideoStream +module.exports.stopAudioStream = stopAudioStream +module.exports.stopVideoStream = stopVideoStream module.exports.sendAuthenticatedGet = sendAuthenticatedGet module.exports.getCameraParameters = getCameraParameters module.exports.sendHttpAck = sendHttpAck module.exports.sendAudioAck = sendAudioAck module.exports.sendVideoAck = sendVideoAck module.exports.parseHttp = parseHttp +module.exports.stepDown = stepDown +module.exports.stepUp = stepUp +module.exports.stepRight = stepRight +module.exports.stepLeft = stepLeft +module.exports.moveDown = moveDown +module.exports.moveUp = moveUp +module.exports.moveRight = moveRight +module.exports.moveLeft = moveLeft +module.exports.stopMoveDown = stopMoveDown +module.exports.stopMoveUp = stopMoveUp +module.exports.stopMoveRight = stopMoveRight +module.exports.stopMoveLeft = stopMoveLeft +module.exports.moveDownRight = moveDownRight +module.exports.moveUpRight = moveUpRight +module.exports.moveUpLeft = moveUpLeft +module.exports.moveDownLeft = moveDownLeft +module.exports.stopMove = stopMove +module.exports.resetPosition = resetPosition +module.exports.upAndDown = upAndDown +module.exports.leftAndRight = leftAndRight +module.exports.reverseImage = reverseImage +module.exports.mirrorImage = mirrorImage +module.exports.resetImage = resetImage +module.exports.reverseAndMirrorImage = reverseAndMirrorImage +module.exports.activateInfrared = activateInfrared +module.exports.deactivateInfrared = deactivateInfrared