Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('Roundtrip a frame', async t => {
let frm = beamcoder.frame({
pts: 1234567890,
width: 1920,
height: 1080,
format: 'yuv422p',
metadata: { wibble: 'wobble' },
side_data: { replaygain : Buffer.from('wombles') }
}).alloc();
let redis = new Redis();
await redis.del(`beam:frame:${frm.pts}`);
// for ( let x = 0 ; x < 100 ; x++ ) {
let start = process.hrtime();
let frmr = frameToRedis(frm.toJSON());
console.log('frameToRedis', process.hrtime(start));
start = process.hrtime();
test('GET a frame', async t => {
try {
t.ok(await flushdb(), 'database flushed OK.');
t.comment('### Retrieve a frame');
t.deepEqual(await redisio.storeMedia('test_url', testUtil.frm, 3),
['OK-crt', 'OK-crt', 'OK-crt', 'OK-crt'], 'test frame stored OK.');
let response = await request(server).get('/beams/test_url/stream_3/42')
.expect(200);
t.ok(response.ok, 'response claims OK.');
t.equal(response.type, 'application/json', 'response is JSON.');
t.ok(Array.isArray(response.body), 'result is an array.');
let frm = beamcoder.frame(response.body[0]);
t.ok(frm, 'roundtrip frame is truthy.');
t.deepEqual(frm.toJSON(), frm.toJSON(),
'retrieved frame as expected.');
t.deepEqual(frm.buf_sizes, [ 2073600, 1036800, 1036800 ],
'has expected buf_sizes parameter.');
t.comment('### Frame not found');
response = await request(server).get('/beams/test_url/stream_3/41')
.expect(404);
t.notOk(response.ok, 'response is not OK.');
t.equal(response.type, 'application/json', 'response is JSON.');
t.deepEqual(response.body, { statusCode: 404,
error: 'Not Found',
message: `Media with name 'test_url:stream_3:41' was not found: Unable to find requested media elements.` }, // eslint-disable-line
'error message structure as expected.');
test('GET a frame directly', async t => {
try {
t.ok(await flushdb(), 'database flushed OK.');
t.comment('### Retrieve a frame directly');
t.deepEqual(await redisio.storeMedia('test_url', testUtil.frm, 3),
['OK-crt', 'OK-crt', 'OK-crt', 'OK-crt'], 'test frame stored OK.');
let response = await request(server).get('/beams/test_url/stream_3/frame_42')
.expect(200);
t.ok(response.ok, 'response claims OK.');
t.equal(response.type, 'application/json', 'response is JSON.');
t.notOk(Array.isArray(response.body), 'result is an array.');
let frm = beamcoder.frame(response.body);
t.ok(frm, 'roundtrip frame is truthy.');
t.deepEqual(frm.toJSON(), frm.toJSON(),
'retrieved frame as expected.');
t.deepEqual(frm.buf_sizes, [ 2073600, 1036800, 1036800 ],
'has expected buf_sizes parameter.');
t.comment('### Frame not found');
response = await request(server).get('/beams/test_url/stream_3/frame_41')
.expect(404);
t.notOk(response.ok, 'response is not OK.');
t.equal(response.type, 'application/json', 'response is JSON.');
t.deepEqual(response.body, { statusCode: 404,
error: 'Not Found',
message: `Frame with name 'test_url:stream_3:frame_41' was not found: Frame in stream 'stream_3' with timestamp '41' is not found.` }, // eslint-disable-line
'error message structure as expected.');
} catch (err) {
test('Retrieve media', async t => {
t.ok(await beforeTest(), 'test database flushed OK.');
let pkt = beamcoder.packet({
pts: 42,
dts: 43,
data: Buffer.alloc(16383),
stream_index: 3,
flags: { KEY: true, TRUSTED: true},
side_data: { replaygain: Buffer.from('Zen time?') },
duration: 44,
pos: 45
});
let frm = beamcoder.frame({
pts: 43,
width: 1920,
height: 1080,
format: 'yuv422p'
}).alloc();
t.deepEqual(await redisio.storeMedia('test_url', pkt), [ 'OK-crt', 'OK-crt' ],
'redis reports store of packet and data OK.');
t.deepEqual(await redisio.storeMedia('test_url', frm, 3),
[ 'OK-crt', 'OK-crt', 'OK-crt', 'OK-crt' ],
'redis reports store of frame and data OK.');
t.comment('### searching from 42 upwards');
let mix = await redisio.retrieveMedia('test_url', 3, 42);
t.ok(Array.isArray(mix), 'result is an array ...');
t.equal(mix.length, 2, '... of length two ...');
t.deepEqual(mix.map(x => x.type), [ 'Packet', 'Frame' ],
let fmt = testUtil.fmt;
let response = await request(server)
.post('/beams')
.send(fmt.toJSON())
.expect(201);
t.ok(response.ok, 'response reports OK.');
t.comment('Put in a frame for the format with "stream_0"');
let frm = testUtil.frm;
response = await request(server)
.put('/beams/test_url/stream_0/frame_42')
.send(frm.toJSON())
.expect(201);
t.ok(response.ok, 'response is truthy.');
t.equal(response.type, 'application/json', 'response is JSON.');
let rfrm = beamcoder.frame(response.body);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'returned frame as expected.');
rfrm = await redisio.retrieveFrame('test_url', 0, 42);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'stored frame as expected.');
t.comment('Put in a frame for the format with "0"');
frm = testUtil.frm;
frm.pkt_duration = 143;
response = await request(server)
.put('/beams/test_url/0/frame_42')
.send(frm.toJSON())
.expect(200);
t.ok(response.ok, 'response is truthy.');
t.equal(response.type, 'application/json', 'response is JSON.');
rfrm = beamcoder.frame(response.body);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'returned frame as expected.');
rfrm = await redisio.retrieveFrame('test_url', 0, 42);
t.equal(response.type, 'application/json', 'response is JSON.');
rfrm = beamcoder.frame(response.body);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'returned frame as expected.');
rfrm = await redisio.retrieveFrame('test_url', 0, 42);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'stored frame as expected.');
t.comment('Put in a frame for the format with "video"');
frm = testUtil.frm;
frm.pkt_duration = 144;
response = await request(server)
.put('/beams/test_url/video/frame_42')
.send(frm.toJSON())
.expect(200);
t.ok(response.ok, 'response is truthy.');
t.equal(response.type, 'application/json', 'response is JSON.');
rfrm = beamcoder.frame(response.body);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'returned frame as expected.');
rfrm = await redisio.retrieveFrame('test_url', 0, 42);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'stored frame as expected.');
t.comment('### Put a frame that does not identify');
frm = testUtil.frm;
frm.pkt_duration = 145;
response = await request(server)
.put('/beams/test_url/stream_0/42')
.send(frm.toJSON())
.expect(200);
t.ok(response.ok, 'response is truthy.');
t.equal(response.type, 'application/json', 'response is JSON.');
rfrm = beamcoder.frame(response.body);
t.deepEqual(rfrm.toJSON(), frm.toJSON(), 'returned frame as expected.');
rfrm = await redisio.retrieveFrame('test_url', 0, 42);
get frm() {
return beamcoder.frame({
width: 1920,
height: 1080,
format: 'yuv420p',
pts: 42,
interlaced_frame: true,
color_range: 'tv',
color_primaries: 'bt709',
color_trc: 'bt709',
colorspace: 'bt709',
chroma_location: 'left',
pkt_duration: 1
}).alloc();
}
};
async function retrieveFrame(fmtOrKey, stream_index = 0, pts = 0) {
let redisZero = await redisPool.use();
let frameb = fmtOrKey.indexOf('frame_') < 0 ?
await getWithStreamAlias(redisZero, fmtOrKey, stream_index, `frame_${pts}`) :
await redisZero.hgetallBuffer(fmtOrKey);
if ((!frameb) || (Object.keys(frameb).length === 0)) {
redisPool.recycle(redisZero);
throw new Error(`Frame in stream '${stream_index}' with timestamp '${pts}' is not found.`);
}
let frm = beamcoder.frame(mappings.frameFromRedis(frameb));
let bufSizes = frm.buf_sizes;
if (frm.buf_sizes.length > 0) {
let redis = [ redisZero ];
for ( let x = 1 ; x < frm.buf_sizes.length ; x++ ) {
redis.push(await redisPool.use());
}
let key = `${makeStreamBase(fmtOrKey, stream_index)}:frame_${pts}`;
let getData = [];
for ( let x = 0 ; x < redis.length ; x++ ) {
getData.push(redis[x].getBuffer(`${key}:data_${x}`));
}
let data = await Promise.all(getData);
redis.forEach(x => redisPool.recycle(x));
if (data.every(x => x !== null)) {
frm.data = data;
frm.buf_sizes = data.map(x => x.length);
async function retrieveFrameMetadata(fmtOrKey, stream_index = 0, pts = 0) {
let redis = await redisPool.use();
let frameb = fmtOrKey.indexOf('frame') < 0 ?
await getWithStreamAlias(redis, fmtOrKey, stream_index, `frame_${pts}`) :
await redis.hgetallBuffer(fmtOrKey);
redisPool.recycle(redis);
if ((!frameb) || (Object.keys(frameb).length === 0)) {
throw new Error(`Frame in stream '${stream_index}' with timestamp '${pts}' is not found.`);
}
let frm = beamcoder.frame(mappings.frameFromRedis(frameb));
frm.stream_index = stream_index;
return frm;
}