diff --git a/server/services/rtsp-camera/index.js b/server/services/rtsp-camera/index.js index 3277a1a127..2e87d75aaa 100644 --- a/server/services/rtsp-camera/index.js +++ b/server/services/rtsp-camera/index.js @@ -6,8 +6,7 @@ const RtspCameraHandler = require('./lib'); const RtspCameraController = require('./api/rtspCamera.controller'); module.exports = function RtspCameraService(gladys, serviceId) { - const ffmpeg = require('fluent-ffmpeg'); - const device = new RtspCameraHandler(gladys, ffmpeg, childProcess, serviceId); + const device = new RtspCameraHandler(gladys, childProcess, serviceId); /** * @public * @description This function starts service. diff --git a/server/services/rtsp-camera/lib/getImage.js b/server/services/rtsp-camera/lib/getImage.js index 45419c1665..63932ab8e5 100644 --- a/server/services/rtsp-camera/lib/getImage.js +++ b/server/services/rtsp-camera/lib/getImage.js @@ -38,55 +38,60 @@ async function getImage(device) { this.gladys.config.tempFolder, `camera-${device.id}-${now.getMilliseconds()}-${now.getSeconds()}-${now.getMinutes()}-${now.getHours()}.jpg`, ); - // we create a writestream - const writeStream = fse.createWriteStream(filePath); - const outputOptions = [ - '-vframes 1', - '-qscale:v 15', // Effective range for JPEG is 2-31 with 31 being the worst quality. - ]; + + const args = ['-i', cameraUrlParam.value, '-f', 'image2', '-vframes', '1', '-qscale:v', '15']; + + args.push('-vf'); switch (cameraRotationParam.value) { case DEVICE_ROTATION.DEGREES_90: - outputOptions.push('-vf scale=640:-1,transpose=1'); // Rotate 90 + args.push('scale=640:-1,transpose=1'); // Rotate 90 break; case DEVICE_ROTATION.DEGREES_180: - outputOptions.push('-vf scale=640:-1,transpose=1,transpose=1'); // Rotate 180 + args.push('scale=640:-1,transpose=1,transpose=1'); // Rotate 180 break; case DEVICE_ROTATION.DEGREES_270: - outputOptions.push('-vf scale=640:-1,transpose=2'); // Rotate 270 + args.push('scale=640:-1,transpose=2'); // Rotate 270 break; default: - outputOptions.push('-vf scale=640:-1'); // Rotate 0 + args.push('scale=640:-1'); // Rotate 0 break; } - // Send a camera thumbnail to this stream - // Add a timeout to prevent ffmpeg from running forever - this.ffmpeg(cameraUrlParam.value, { timeout: 10 }) - .format('image2') - .outputOptions(outputOptions) - .output(writeStream) - .on('end', async () => { + // add destination file path + args.push(filePath); + + logger.debug(`Getting camera image on URL ${cameraUrlParam.value}`); + this.childProcess.execFile( + 'ffmpeg', + args, + { + timeout: 10 * 1000, // 10 second max + }, + async (error, stdout, stderr) => { + if (error) { + logger.warn(error); + await fse.remove(filePath); + return reject(error); + } + logger.debug('Camera image saved to disk. Reading disk.'); let image; try { image = await fse.readFile(filePath); } catch (e) { - reject(e); - return; + await fse.remove(filePath); + return reject(e); } + logger.debug('Camera image read from disk, converting to base64'); // convert binary data to base64 encoded string const cameraImageBase = Buffer.from(image).toString('base64'); - const cameraImage = `image/png;base64,${cameraImageBase}`; + const cameraImage = `image/jpg;base64,${cameraImageBase}`; + logger.debug('Camera converted to base64, resolving.'); resolve(cameraImage); await fse.remove(filePath); - }) - .on('error', async (err, stdout, stderr) => { - logger.debug(`Cannot process video: ${err.message}`); - logger.debug(stderr); - reject(err.message); - await fse.remove(filePath); - }) - .run(); + return null; + }, + ); }); } diff --git a/server/services/rtsp-camera/lib/index.js b/server/services/rtsp-camera/lib/index.js index 8c1cedd950..d592a36745 100644 --- a/server/services/rtsp-camera/lib/index.js +++ b/server/services/rtsp-camera/lib/index.js @@ -12,15 +12,13 @@ const { stopStreaming } = require('./stopStreaming'); /** * @description Add ability to connect to RTSP camera. * @param {object} gladys - Gladys instance. - * @param {object} ffmpeg - Ffmpeg library. * @param {object} childProcess - ChildProcess library. * @param {string} serviceId - UUID of the service in DB. * @example - * const rtspCameraHandler = new RtspCameraHandler(gladys, ffmpeg, serviceId); + * const rtspCameraHandler = new RtspCameraHandler(gladys, childProcess, serviceId); */ -const RtspCameraHandler = function RtspCameraHandler(gladys, ffmpeg, childProcess, serviceId) { +const RtspCameraHandler = function RtspCameraHandler(gladys, childProcess, serviceId) { this.gladys = gladys; - this.ffmpeg = ffmpeg; this.childProcess = childProcess; this.serviceId = serviceId; this.checkIfLiveActiveFrequencyInSeconds = 10; diff --git a/server/services/rtsp-camera/package-lock.json b/server/services/rtsp-camera/package-lock.json index 4b1b925ea5..095bf65c32 100644 --- a/server/services/rtsp-camera/package-lock.json +++ b/server/services/rtsp-camera/package-lock.json @@ -20,15 +20,9 @@ "dependencies": { "bluebird": "^3.7.2", "bottleneck": "^2.19.5", - "fluent-ffmpeg": "^2.1.2", "fs-extra": "^8.0.1" } }, - "node_modules/async": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/async/-/async-3.0.0.tgz", - "integrity": "sha512-LNZ6JSpKraIia6VZKKbKxmX6nWIdfsG7WqrOvKpCuDjH7BnGyQRFMTSXEe8to2WF/rqoAKgZvj+L5nnxe0suAg==" - }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -39,18 +33,6 @@ "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", "integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==" }, - "node_modules/fluent-ffmpeg": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.2.tgz", - "integrity": "sha1-yVLeIkD4EuvaCqgAbXd27irPfXQ=", - "dependencies": { - "async": ">=0.2.9", - "which": "^1.1.1" - }, - "engines": { - "node": ">=0.8.0" - } - }, "node_modules/fs-extra": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.0.1.tgz", @@ -69,11 +51,6 @@ "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" }, - "node_modules/isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" - }, "node_modules/jsonfile": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", @@ -89,25 +66,9 @@ "engines": { "node": ">= 4.0.0" } - }, - "node_modules/which": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "dependencies": { - "isexe": "^2.0.0" - }, - "bin": { - "which": "bin/which" - } } }, "dependencies": { - "async": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/async/-/async-3.0.0.tgz", - "integrity": "sha512-LNZ6JSpKraIia6VZKKbKxmX6nWIdfsG7WqrOvKpCuDjH7BnGyQRFMTSXEe8to2WF/rqoAKgZvj+L5nnxe0suAg==" - }, "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -118,15 +79,6 @@ "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", "integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==" }, - "fluent-ffmpeg": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.2.tgz", - "integrity": "sha1-yVLeIkD4EuvaCqgAbXd27irPfXQ=", - "requires": { - "async": ">=0.2.9", - "which": "^1.1.1" - } - }, "fs-extra": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.0.1.tgz", @@ -142,11 +94,6 @@ "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" }, - "isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" - }, "jsonfile": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", @@ -159,14 +106,6 @@ "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" - }, - "which": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", - "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "requires": { - "isexe": "^2.0.0" - } } } } diff --git a/server/services/rtsp-camera/package.json b/server/services/rtsp-camera/package.json index d0f7518997..7d25e38d9f 100644 --- a/server/services/rtsp-camera/package.json +++ b/server/services/rtsp-camera/package.json @@ -15,7 +15,6 @@ "dependencies": { "bluebird": "^3.7.2", "bottleneck": "^2.19.5", - "fluent-ffmpeg": "^2.1.2", "fs-extra": "^8.0.1" } } diff --git a/server/test/services/rtsp-camera/FfmpegMock.test.js b/server/test/services/rtsp-camera/FfmpegMock.test.js deleted file mode 100644 index 30def577e7..0000000000 --- a/server/test/services/rtsp-camera/FfmpegMock.test.js +++ /dev/null @@ -1,33 +0,0 @@ -const { fake } = require('sinon'); -const fse = require('fs-extra'); -const EventEmitter = require('events'); - -const FfmpegMock = (path) => { - let lastStream = null; - let func = new EventEmitter(); - func = Object.assign(func, { - format: fake.returns(func), - outputOptions: fake.returns(func), - output: (stream) => { - stream.write('image'); - stream.close(); - lastStream = stream; - return func; - }, - run: () => { - lastStream.on('finish', () => { - if (path === 'broken') { - func.emit('error', { message: 'broken' }); - } else if (path === 'no-image-written') { - fse.removeSync(lastStream.path); - func.emit('end'); - } else { - func.emit('end'); - } - }); - return func; - }, - }); - return func; -}; -module.exports = FfmpegMock; diff --git a/server/test/services/rtsp-camera/rstp.convertLocalStreamToGateway.test.js b/server/test/services/rtsp-camera/rstp.convertLocalStreamToGateway.test.js index 6ac28e5d9b..d5e518e1af 100644 --- a/server/test/services/rtsp-camera/rstp.convertLocalStreamToGateway.test.js +++ b/server/test/services/rtsp-camera/rstp.convertLocalStreamToGateway.test.js @@ -2,7 +2,6 @@ const { expect, assert } = require('chai'); const fse = require('fs-extra'); const path = require('path'); const { fake, assert: fakeAssert } = require('sinon'); -const FfmpegMock = require('./FfmpegMock.test'); const RtspCameraManager = require('../../../services/rtsp-camera/lib'); const { NotFoundError } = require('../../../utils/coreErrors'); @@ -41,12 +40,7 @@ describe('Camera.convertLocalStreamToGateway', () => { const indexFilePath = path.join(folderPath, 'index.m3u8'); const keyfilePath = path.join(folderPath, 'index.m3u8.key'); const videoFilePath = path.join(folderPath, 'index0.ts'); - const rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + const rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); before(async () => { await fse.ensureDir(folderPath); await fse.writeFile(indexFilePath, 'this is index'); diff --git a/server/test/services/rtsp-camera/rstpCamera.onNewCameraFile.test.js b/server/test/services/rtsp-camera/rstpCamera.onNewCameraFile.test.js index 3ebe5a9147..c419667a69 100644 --- a/server/test/services/rtsp-camera/rstpCamera.onNewCameraFile.test.js +++ b/server/test/services/rtsp-camera/rstpCamera.onNewCameraFile.test.js @@ -2,7 +2,6 @@ const { expect } = require('chai'); const fse = require('fs-extra'); const path = require('path'); const { fake, assert: fakeAssert } = require('sinon'); -const FfmpegMock = require('./FfmpegMock.test'); const RtspCameraManager = require('../../../services/rtsp-camera/lib'); const device = { @@ -51,12 +50,7 @@ describe('Camera.onNewCameraFile', () => { await fse.remove(folderPath); }); beforeEach(() => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); rtspCameraManager.sendCameraFileToGatewayLimited = fake.resolves(null); }); it('should return directly, no live stream', async () => { @@ -111,12 +105,7 @@ describe('Camera.onNewCameraFile', () => { fakeAssert.calledWith(eventEmitter.emit, 'gateway-ready'); }); it('should upload a file that fail, and return null', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); rtspCameraManager.sendCameraFileToGatewayLimited = fake.rejects(null); rtspCameraManager.liveStreams.set('my-camera', { isGladysGateway: true, diff --git a/server/test/services/rtsp-camera/rtspCamera.sendCameraFileToGateway.test.js b/server/test/services/rtsp-camera/rtspCamera.sendCameraFileToGateway.test.js index 2883ea2cc3..c6fa799253 100644 --- a/server/test/services/rtsp-camera/rtspCamera.sendCameraFileToGateway.test.js +++ b/server/test/services/rtsp-camera/rtspCamera.sendCameraFileToGateway.test.js @@ -1,7 +1,6 @@ const fse = require('fs-extra'); const path = require('path'); const { fake, assert: fakeAssert } = require('sinon'); -const FfmpegMock = require('./FfmpegMock.test'); const RtspCameraManager = require('../../../services/rtsp-camera/lib'); const gladys = { @@ -23,12 +22,7 @@ describe('Camera.sendCameraFileToGateway', () => { const indexFilePath = path.join(folderPath, 'index.m3u8'); const keyfilePath = path.join(folderPath, 'index.m3u8.key'); const videoFilePath = path.join(folderPath, 'index0.ts'); - const rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + const rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); before(async () => { await fse.ensureDir(folderPath); await fse.writeFile(indexFilePath, 'this is index'); diff --git a/server/test/services/rtsp-camera/rtspCamera.streaming.test.js b/server/test/services/rtsp-camera/rtspCamera.streaming.test.js index 75602bcec5..0ef4023509 100644 --- a/server/test/services/rtsp-camera/rtspCamera.streaming.test.js +++ b/server/test/services/rtsp-camera/rtspCamera.streaming.test.js @@ -3,7 +3,6 @@ const { expect, assert } = require('chai'); const fse = require('fs-extra'); const path = require('path'); const { fake, assert: fakeAssert } = require('sinon'); -const FfmpegMock = require('./FfmpegMock.test'); const RtspCameraManager = require('../../../services/rtsp-camera/lib'); const { NotFoundError } = require('../../../utils/coreErrors'); const { DEVICE_ROTATION } = require('../../../utils/constants'); @@ -72,12 +71,7 @@ describe('Camera.streaming', () => { await fse.ensureDir(gladys.config.tempFolder); }); beforeEach(() => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); }); afterEach(() => { // remove interval @@ -95,12 +89,7 @@ describe('Camera.streaming', () => { }), }, }; - rtspCameraManager = new RtspCameraManager( - wrongGladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(wrongGladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); const promise = rtspCameraManager.startStreaming('my-camera', false, 1); await assert.isRejected(promise, NotFoundError); wrongGladys.device.getBySelector = fake.resolves({ @@ -158,7 +147,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladysDeviceWithRotation, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -194,7 +182,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladysDeviceWithRotation, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -230,7 +217,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladysDeviceWithRotation, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -262,7 +248,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladysDeviceWithRotation, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -299,12 +284,7 @@ describe('Camera.streaming', () => { await rtspCameraManager.stopStreaming('my-camera'); }); it('should start streaming only once', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); // @ts-ignore rtspCameraManager.startStreaming = fake.resolves({}); @@ -317,12 +297,7 @@ describe('Camera.streaming', () => { fakeAssert.calledOnce(rtspCameraManager.startStreaming); }); it('should start streaming if not started', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); // @ts-ignore rtspCameraManager.startStreaming = fake.rejects(new Error('test')); const promise = rtspCameraManager.startStreamingIfNotStarted('my-camera', false, 1); @@ -330,12 +305,7 @@ describe('Camera.streaming', () => { expect(rtspCameraManager.liveStreamsStarting.size).to.equal(0); }); it('should start streaming locally, then convert local stream to online stream during init', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); // @ts-ignore rtspCameraManager.convertLocalStreamToGateway = fake.resolves(null); const promise = rtspCameraManager.startStreamingIfNotStarted('my-camera', false, 1); @@ -345,12 +315,7 @@ describe('Camera.streaming', () => { fakeAssert.calledOnce(rtspCameraManager.convertLocalStreamToGateway); }); it('should start streaming locally, then convert local stream to online stream after stream started', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); // @ts-ignore rtspCameraManager.convertLocalStreamToGateway = fake.resolves(null); await rtspCameraManager.startStreamingIfNotStarted('my-camera', false, 1); @@ -382,7 +347,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladys, - FfmpegMock, childProcessMockWithCrash, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -418,7 +382,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladys, - FfmpegMock, childProcessMockWithCrash, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -443,7 +406,6 @@ describe('Camera.streaming', () => { }; rtspCameraManager = new RtspCameraManager( gladysWithFailClean, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); @@ -460,12 +422,7 @@ describe('Camera.streaming', () => { await rtspCameraManager.stopStreaming('my-camera'); }); it('should return even if stream does not exist in stopStreaming', async () => { - rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); await rtspCameraManager.stopStreaming('unknown stream'); }); }); diff --git a/server/test/services/rtsp-camera/rtspCamera.test.js b/server/test/services/rtsp-camera/rtspCamera.test.js index 20fc277df3..3ca9eef186 100644 --- a/server/test/services/rtsp-camera/rtspCamera.test.js +++ b/server/test/services/rtsp-camera/rtspCamera.test.js @@ -2,7 +2,6 @@ const { expect } = require('chai'); const fse = require('fs-extra'); const assertChai = require('chai').assert; const { fake, assert } = require('sinon'); -const FfmpegMock = require('./FfmpegMock.test'); const RtspCameraManager = require('../../../services/rtsp-camera/lib'); const RtspCameraService = require('../../../services/rtsp-camera'); @@ -101,6 +100,16 @@ const deviceThatResultInNoImage = { }; const childProcessMock = { + execFile: (prog, args, options, cb) => { + if (args[1] === 'broken') { + cb(new Error('broken url')); + } else if (args[1] === 'no-image-written') { + cb(null, '', ''); + } else { + fse.writeFileSync(args[args.length - 1], 'image'); + cb(null, '', ''); + } + }, spawn: (command, args, options) => { const writeFile = () => { fse.writeFileSync(args[args.length - 1], 'hello'); @@ -120,12 +129,7 @@ const childProcessMock = { }; describe('RtspCameraManager commands', () => { - const rtspCameraManager = new RtspCameraManager( - gladys, - FfmpegMock, - childProcessMock, - 'de051f90-f34a-4fd5-be2e-e502339ec9bc', - ); + const rtspCameraManager = new RtspCameraManager(gladys, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc'); before(async () => { await fse.ensureDir(gladys.config.tempFolder); }); @@ -135,19 +139,19 @@ describe('RtspCameraManager commands', () => { }); it('should getImage', async () => { const image = await rtspCameraManager.getImage(device); - expect(image).to.equal('image/png;base64,aW1hZ2U='); + expect(image).to.equal('image/jpg;base64,aW1hZ2U='); }); it('should getImage 90°', async () => { const image = await rtspCameraManager.getImage(deviceRotation90); - expect(image).to.equal('image/png;base64,aW1hZ2U='); + expect(image).to.equal('image/jpg;base64,aW1hZ2U='); }); it('should getImage 180°', async () => { const image = await rtspCameraManager.getImage(deviceRotation180); - expect(image).to.equal('image/png;base64,aW1hZ2U='); + expect(image).to.equal('image/jpg;base64,aW1hZ2U='); }); it('should getImage 270°', async () => { const image = await rtspCameraManager.getImage(deviceRotation270); - expect(image).to.equal('image/png;base64,aW1hZ2U='); + expect(image).to.equal('image/jpg;base64,aW1hZ2U='); }); it('should return error', async () => { const promise = rtspCameraManager.getImage(brokenDevice); @@ -174,16 +178,15 @@ describe('RtspCameraManager commands', () => { }); it('should poll', async () => { await rtspCameraManager.poll(device); - assert.calledWith(gladys.device.camera.setImage, 'my-camera', 'image/png;base64,aW1hZ2U='); + assert.calledWith(gladys.device.camera.setImage, 'my-camera', 'image/jpg;base64,aW1hZ2U='); }); it('should fail to poll, but not crash', async () => { const rtspCameraManagerBroken = new RtspCameraManager( gladys, - FfmpegMock, childProcessMock, 'de051f90-f34a-4fd5-be2e-e502339ec9bc', ); - rtspCameraManagerBroken.getImage = fake.rejects('NOT_WORKI?NG'); + rtspCameraManagerBroken.getImage = fake.rejects('NOT_WORKING'); await rtspCameraManagerBroken.poll(device); }); it('should stop service', async () => {