This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
add mjpeg stream helper class #75
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import _ from 'lodash'; | ||
import request from 'request'; | ||
import log from './logger'; | ||
import http from 'http'; | ||
import B from 'bluebird'; | ||
import Jimp from 'jimp'; | ||
import MJpegConsumer from 'mjpeg-consumer'; | ||
import mJpegServer from 'mjpeg-server'; | ||
import { Writable } from 'stream'; | ||
|
||
const TEST_IMG_JPG = '/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAeAAD/4QOBaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjYtYzE0MCA3OS4xNjA0NTEsIDIwMTcvMDUvMDYtMDE6MDg6MjEgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vI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AECBBEDEgYhMRODo7PTVAUWNhEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8Az8AAdAAAAAAI8+fE8dEuTZtzZR7VMb6OdTE5GJoYirrUp/e8qd9wb3TGe/lJ2551sx8D/9k='; | ||
|
||
// amount of time to wait for the first image in the stream | ||
const MJPEG_SERVER_TIMEOUT_MS = 10000; | ||
|
||
/** Class which stores the last bit of data streamed into it */ | ||
class MJpegStream extends Writable { | ||
|
||
/** | ||
* Create an MJpegStream | ||
* @param {string} mJpegUrl - URL of MJPEG-over-HTTP stream | ||
* @param {function} [errorHandler=noop] - additional function that will be | ||
* called in the case of any errors. | ||
* @param {object} [options={}] - Options to pass to the Writable constructor | ||
*/ | ||
constructor (mJpegUrl, errorHandler = _.noop, options = {}) { | ||
super(options); | ||
this.errorHandler = errorHandler; | ||
this.url = mJpegUrl; | ||
this.clear(); | ||
} | ||
|
||
/** | ||
* Get the base64-encoded version of the JPEG | ||
* @returns {string} | ||
*/ | ||
get lastChunkBase64 () { | ||
return _.isBuffer(this.lastChunk) ? | ||
this.lastChunk.toString('base64') : | ||
null; | ||
} | ||
|
||
/** | ||
* Get the PNG version of the JPEG buffer | ||
* @returns {Buffer} PNG image data | ||
*/ | ||
async lastChunkPNG () { | ||
if (!_.isBuffer(this.lastChunk)) { | ||
return null; | ||
} | ||
|
||
const jpg = await new B((res, rej) => { | ||
new Jimp(this.lastChunk, (err, img) => { | ||
if (err) { | ||
return rej(err); | ||
} | ||
res(img); | ||
}); | ||
}); | ||
|
||
return await B.promisify(jpg.getBuffer, {context: jpg})(Jimp.MIME_PNG); | ||
} | ||
|
||
/** | ||
* Get the base64-encoded version of the PNG | ||
* @returns {string} | ||
*/ | ||
async lastChunkPNGBase64 () { | ||
const png = await this.lastChunkPNG(); | ||
|
||
if (!png) { | ||
return null; | ||
} | ||
|
||
return png.toString('base64'); | ||
} | ||
|
||
/** | ||
* Reset internal state | ||
*/ | ||
clear () { | ||
this.registerStartSuccess = null; | ||
this.registerStartFailure = null; | ||
this.request = null; | ||
this.consumer = null; | ||
this.lastChunk = null; | ||
this.updateCount = 0; | ||
} | ||
|
||
/** | ||
* Start reading the MJpeg stream and storing the last image | ||
*/ | ||
async start (serverTimeout = MJPEG_SERVER_TIMEOUT_MS) { | ||
// ensure we're not started already | ||
this.stop(); | ||
|
||
this.consumer = new MJpegConsumer(); | ||
|
||
// use the deferred pattern so we can wait for the start of the stream | ||
// based on what comes in from an external pipe | ||
const startPromise = new B((res, rej) => { | ||
this.registerStartSuccess = res; | ||
this.registerStartFailure = rej; | ||
}) | ||
// start a timeout so that if the server does not return data, we don't | ||
// block forever. | ||
.timeout(serverTimeout, | ||
`Waited ${serverTimeout}ms but the MJPEG server never sent any images`); | ||
|
||
const onErr = (err) => { | ||
log.error(`Error getting MJpeg screenshot chunk: ${err}`); | ||
this.errorHandler(err); | ||
if (this.registerStartFailure) { | ||
this.registerStartFailure(err); | ||
} | ||
}; | ||
|
||
this.request = request(this.url); | ||
|
||
this.request | ||
.on('error', onErr) // ensure we do something with errors | ||
.pipe(this.consumer) // allow chunking and transforming of jpeg data | ||
.pipe(this); // send the actual jpegs to ourself | ||
|
||
await startPromise; | ||
} | ||
|
||
/** | ||
* Stop reading the MJpeg stream. Ensure we disconnect all the pipes and stop | ||
* the HTTP request itself. Then reset the state. | ||
*/ | ||
stop () { | ||
if (!this.consumer) { | ||
return; | ||
} | ||
|
||
this.consumer.unpipe(); | ||
this.request.end(); | ||
this.clear(); | ||
} | ||
|
||
/** | ||
* Override the Writable write() method in order to save the last image and | ||
* log the number of images we have received | ||
* @override | ||
* @param {Buffer} data - binary data streamed from the MJpeg consumer | ||
*/ | ||
write (data) { | ||
this.lastChunk = data; | ||
this.updateCount++; | ||
|
||
if (this.registerStartSuccess) { | ||
this.registerStartSuccess(); | ||
this.registerStartSuccess = null; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Start an mjpeg server for the purpose of testing, which just sends the same | ||
* image over and over. Caller is responsible for closing the server. | ||
* @param {int} port - port the server should listen on | ||
* @param {int} [intMs] - how often the server should push an image | ||
* @param {int} [times] - how many times the server should push an image before | ||
* it closes the connection | ||
* @returns {http.Server} | ||
*/ | ||
function initMJpegServer (port, intMs = 300, times = 20) { | ||
const server = http.createServer(async function (req, res) { | ||
const mJpegReqHandler = mJpegServer.createReqHandler(req, res); | ||
const jpg = Buffer.from(TEST_IMG_JPG, 'base64'); | ||
|
||
// just send the same jpeg over and over | ||
for (let i = 0; i < times; i++) { | ||
await B.delay(intMs); | ||
mJpegReqHandler._write(jpg, null, _.noop); | ||
} | ||
mJpegReqHandler.close(); | ||
}).listen(port); | ||
|
||
return server; | ||
} | ||
|
||
export { MJpegStream, initMJpegServer, TEST_IMG_JPG }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import _ from 'lodash'; | ||
import { mjpeg } from '..'; | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import B from 'bluebird'; | ||
|
||
const {MJpegStream, initMJpegServer} = mjpeg; | ||
|
||
const should = chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
const MJPEG_SERVER_PORT = 8589; | ||
const MJPEG_SERVER_URL = `http://localhost:${MJPEG_SERVER_PORT}`; | ||
|
||
describe('MJpeg Stream (e2e)', function () { | ||
|
||
let mJpegServer, stream; | ||
|
||
before(function () { | ||
mJpegServer = initMJpegServer(MJPEG_SERVER_PORT); | ||
}); | ||
|
||
after(function () { | ||
mJpegServer.close(); | ||
stream.stop(); // ensure streams are always stopped | ||
}); | ||
|
||
it('should update mjpeg stream based on new data from mjpeg server', async function () { | ||
stream = new MJpegStream(MJPEG_SERVER_URL, _.noop); | ||
should.not.exist(stream.lastChunk); | ||
await stream.start(); | ||
should.exist(stream.lastChunk); | ||
stream.updateCount.should.eql(1); | ||
|
||
await B.delay(1000); // let the stream update a bit | ||
stream.updateCount.should.be.above(1); | ||
|
||
// verify jpeg type and byte length of fixture image | ||
const startBytes = Buffer.from([0xff, 0xd8]); | ||
const endBytes = Buffer.from([0xff, 0xd9]); | ||
const startPos = stream.lastChunk.indexOf(startBytes); | ||
const endPos = stream.lastChunk.indexOf(endBytes); | ||
startPos.should.eql(0); // proves we have a jpeg | ||
endPos.should.eql(1278); // proves we have a jpeg of the right size | ||
|
||
// verify we can get the base64 version too | ||
const b64 = stream.lastChunkBase64; | ||
b64.should.eql(mjpeg.TEST_IMG_JPG); | ||
|
||
// verify we can get the PNG version too | ||
const png = await stream.lastChunkPNGBase64(); | ||
png.should.be.a('string'); | ||
png.indexOf('iVBOR').should.eql(0); | ||
png.length.should.be.above(400); | ||
|
||
|
||
// now stop the stream and wait some more then assert no new data has come in | ||
stream.stop(); | ||
await B.delay(1000); | ||
should.not.exist(stream.lastChunk); | ||
stream.updateCount.should.eql(0); | ||
}); | ||
|
||
it('should error out if the server does not send any images before a timeout', async function () { | ||
stream = new MJpegStream(MJPEG_SERVER_URL, _.noop); | ||
await stream.start(0).should.eventually.be.rejectedWith(/never sent/); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather put this helper method into tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will be used by the drivers though, so it needs to be exported from appium-support