Skip to content

Commit

Permalink
Integrated dumpChunkStream.js in the unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nalply committed Oct 28, 2011
1 parent b7d69a2 commit 5e7aa69
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 44 deletions.
21 changes: 14 additions & 7 deletions examples/dumpChunkStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ var net = require('net');
var mtrude = require('mtrude');
var ChunkStream = mtrude.rtmp.ChunkStream;
var asSocket = mtrude.asSocket;
var dumpTools = require('./dumpTools');

function main() {
var optimist = require('optimist')
.usage('Usage: $0 [--debug] [--debugchain] [in [out]]')
.usage('Usage: $0 [--debug] [--debugchain] [--nocolor] [in [out]]')
.boolean('debug')
.boolean('debugchain')
.boolean('nocolor')
.boolean('help')
.alias('h', 'help')
.describe('debug', 'Set ChunkStream.DBG = true')
.describe('debugchain', 'Set BufferChain.DBG = true')
.describe('nocolor', 'Don\'t use colors')
;
var argv = optimist.argv;

Expand All @@ -23,6 +26,8 @@ function main() {
return;
}

if (argv.nocolor) dumpTools.dontColor();

console.log(''
+ 'NOTE! ChunkStream is not complete without MessageStream and might get\n'
+ 'stuck because of missing coordination with peer. Dump format:\n'
Expand Down Expand Up @@ -57,11 +62,10 @@ function main() {
}


var DumpTools = require('./DumpTools');
var dump8 = DumpTools.dump8;
var hex2 = DumpTools.hex2;
var hex6 = DumpTools.hex6;
var ascii8 = DumpTools.ascii8;
var dump8 = dumpTools.dump8;
var hex2 = dumpTools.hex2;
var hex6 = dumpTools.hex6;
var ascii8 = dumpTools.ascii8;

var dumpChunkStream = function(chunkStream) {
chunkStream.on('error', function(errorMessage) {
Expand All @@ -78,6 +82,9 @@ var dumpChunkStream = function(chunkStream) {
chunkStream.on('warn', function(message) {
console.log('WARN : %s', ('ChunkStream: ' + message).red);
});
chunkStream.on('info', function(message) {
console.log('INFO : %s', ('ChunkStream: ' + message).magenta);
});
chunkStream.on('chunk', function(chunk) {
console.log(
'CHUNK: %s %s %s %s %s:%s %s:%s,%s',
Expand All @@ -88,7 +95,7 @@ var dumpChunkStream = function(chunkStream) {
chunk.rest.toString().blue);
if (chunk.typeid == 1) {
var chunkSize = chunk.data.readUInt32LE(0);
chunkStream.warn('Setting chunk size to ' + chunkSize);
chunkStream.info('Setting chunk size to ' + chunkSize);
chunkStream.chunkSize = chunkSize;
}
});
Expand Down
16 changes: 10 additions & 6 deletions examples/dumpMessageStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ var rtmp = mtrude.rtmp;
var ChunkStream = rtmp.ChunkStream;
var MessageStream = rtmp.MessageStream;
var asSocket = mtrude.asSocket;
var dumpTools = require('./dumpTools');

function main() {
var optimist = require('optimist')
.usage('Usage: $0 [--debug] [--chunks] [in [out]]')
.usage('Usage: $0 [--debug] [--chunks] [--nocolor] [in [out]]')
.boolean('debug')
.boolean('chunks')
.boolean('nocolor')
.boolean('help')
.alias('h', 'help')
.describe('debug', 'Set MessageStream.DBG = true')
.describe('chunks', 'Also dump chunks')
.describe('nocolor', 'Don\'t use colors')
;
var argv = optimist.argv;

Expand All @@ -25,6 +28,8 @@ function main() {
return;
}

if (argv.nocolor) dumpTools.dontColor();

console.log('Dump format:\n'
+ 'MSG : %s %s %s %s %s:%s %s\n'
+ 'PING : %s (%s) %s %s%s%s:%s\n',
Expand Down Expand Up @@ -61,11 +66,10 @@ function main() {
dumpMessageStream(messageStream);
}

var DumpTools = require('./DumpTools');
var dump8 = DumpTools.dump8;
var hex2 = DumpTools.hex2;
var hex6 = DumpTools.hex6;
var ascii8 = DumpTools.ascii8;
var dump8 = dumpTools.dump8;
var hex2 = dumpTools.hex2;
var hex6 = dumpTools.hex6;
var ascii8 = dumpTools.ascii8;

function dumpMessageStream(messageStream) {
messageStream.on('error', function(errorMessage) {
Expand Down
6 changes: 6 additions & 0 deletions examples/dumpTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ var ascii8 = exports.ascii8 = function(data) {
return b(0) + b(1) + b(2) + b(3) + ' ' + b(4) + b(5) + b(6) + b(7);
}

var dontColor = exports.dontColor = function() {
var colors = 'cyan yellow blue grey white magenta red green'.split(' ');
for (var i in colors)
Object.defineProperty(String.prototype, colors[i],
{ get: function() { return this } });
}
10 changes: 9 additions & 1 deletion lib/rtmp/ChunkStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function dbg() { if (ChunkStream.DBG) console.log.apply(console, arguments) }
* Emitted on networking or protocol errors
* 'warn': function(message) {}
* Emitted on potential problems
* 'info': function(message) {}
* Emitted if something special has been found but which poses no problem
* 'end' function(graceful) {}
* Emitted on disconnect from other end (graceful or not)
* 'handshake' function(deltaTime) {}
Expand Down Expand Up @@ -125,6 +127,10 @@ p.warn = function(warnText) {
this.emit('warn', warnText);
}

p.info = function(warnText) {
this.emit('info', warnText);
}

p.close = function() {
var it = this;

Expand Down Expand Up @@ -373,7 +379,7 @@ s.chunkType2 = function() { // RTMCSP 6.1.2.4 Chunk Type 2; additional chunk
chunks.timestamp = this.buffers.consumeUInt24BE();
resetLengthIfNewMessage(chunks);

this.warn('chunkType2 timestamp 0x' + chunks.timestamp.toString(16));
this.info('chunkType2 timestamp 0x' + chunks.timestamp.toString(16));

if (chunks.timestamp === 0xffffff)
return this.error("Extended Timestamp not supported");
Expand Down Expand Up @@ -404,6 +410,8 @@ s.chunkData = function() { // RTMCSP 6.1 Chunk Data
var data = this.buffers.consumeBuffers(length);
var chunk = newChunk(chunks, chunks.msid, chunks.timestamp, chunks.length,
chunks.typeid, data, rest);
dbg('emit : chunk timestamp=%s typeid=%s %s:%s length=',
chunk.timestamp, chunk.typeid, chunk.csid, chunk.msid, chunk.data.length);
this.emit('chunk', chunk);

this.state = 'chunk';
Expand Down
87 changes: 58 additions & 29 deletions test/TestChunkStream.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
"use strict";

var assert = require('assert');
var fs = require('fs');
var mtrude = require('mtrude');
var ChunkStream = mtrude.rtmp.ChunkStream;
var spawn = require('child_process').spawn;

var mockSocket = require('./fixtures').mockSocket;

function fix(assert) {
for (var key in assert) assert.ok[key] = assert[key];
return assert.ok;
}

function incoming(filename, chunkCount, exit) {
function incoming(filename, chunkCount, exit, assert) {
var handshake = false;
var end = false;
var length = 0;
var chunkIndex = 0;

var cs = new ChunkStream(mockSocket(filename));
cs.on('error', function(err) {
assert(false, filename + ': error emitted: ' + err);
cs.on('error', function(errorMessage) {
assert.ok(false, filename + ': error emitted: ' + errorMessage);
});
cs.on('warn', function(warnMessage) {
assert.ok(false, filename + ': warn emitted: ' + warnMessage);
});
cs.on('warn', function(message) { console.log('WARN', message); });
cs.on('handshake', function() { handshake = true; });
cs.on('end', function(graceful) {
assert(graceful, filename + ': not a graceful end');
assert.ok(graceful, filename + ': not a graceful end');
assert.equal(chunkIndex, chunkCount,
filename + ': ' + chunkIndex + ' == ' + chunkCount);
end = true;
});
cs.on('chunk', function(chunk) {
assert(handshake, filename +
assert.ok(handshake, filename +
': handshake must be emitted before any chunks');
chunkIndex++;
});
exit(function() {
assert(handshake, filename + ': handshake has not been emitted');
assert(end, filename + ': end has not been emitted');
assert.ok(handshake, filename + ': handshake has not been emitted');
assert.ok(end, filename + ': end has not been emitted');
});
return cs;
}

function dumpChunkStream(filename, assert) {
var iFile = 'test/data/' + filename + '.raw';
var tFile = 'test/data/' + filename + '-dumpChunkStream.txt';
var js = 'examples/dumpChunkStream.js';
var child = spawn('node', [js, '--nocolor', iFile, '/dev/null']);
var text = '';

child.stdout.on('data', function(data) { text += data });
child.on('exit', function(code) {
assert.equal(code, 0);
fs.readFile(tFile, function(err, testData) {
assert.equal(err, null, err + '');
assert.equal(text, testData, js + ' ' + iFile);
});
});
}


module.exports = {
'test incoming1': function(exit, assert) {
assert = fix(assert);

var cs = incoming('incoming1', 3, exit);
var cs = incoming('incoming1', 3, exit, assert);
var chunkIndex = 0;

cs.on('chunk', function(chunk) {
Expand All @@ -61,9 +74,7 @@ module.exports = {
},

'test incoming2': function(exit, assert) {
assert = fix(assert);

var cs = incoming('incoming2', 7, exit);
var cs = incoming('incoming2', 7, exit, assert);
var chunkIndex = 0;

cs.on('chunk', function(chunk) {
Expand All @@ -78,20 +89,38 @@ module.exports = {
},

'test incoming3': function(exit, assert) {
assert = fix(assert);
var cs = incoming('incoming3', 8, exit);
var cs = incoming('incoming3', 8, exit, assert);
},

'test outgoing2': function(exit, assert) {
assert = fix(assert);
var cs = incoming('outgoing2', 7, exit);
var cs = incoming('outgoing2', 7, exit, assert);
},

// Implement ChunkStream.chunkType2
/*
'test outgoing3': function(exit, assert) {
assert = fix(assert);
var cs = incoming('outgoing3', 7, exit);
var cs = incoming('outgoing3', 25, exit, assert);
var chunkIndex = 0;

cs.on('chunk', function(chunk) {
if (chunk.typeid == 1) cs.chunkSize = chunk.data.readUInt32LE(0);
assert.equal(chunk.csid, [2, 2, 2, 3, 3, 3, 3, 2, 2, 20, 20, 20, 20, 20,
20, 20, 2, 21, 21, 20, 20, 2, 20, 20, 2][chunkIndex]);
chunkIndex++;
});
},

'dump incoming1': function(exit, assert) {
dumpChunkStream('outgoing3', assert);
},

'dump incoming2': function(exit, assert) {
dumpChunkStream('outgoing3', assert);
},

'dump outgoing2': function(exit, assert) {
dumpChunkStream('outgoing2', assert);
},

'dump outgoing3': function(exit, assert) {
dumpChunkStream('outgoing3', assert);
},
*/
}
11 changes: 11 additions & 0 deletions test/data/incoming1-dumpChunkStream.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NOTE! ChunkStream is not complete without MessageStream and might get
stuck because of missing coordination with peer. Dump format:
CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest

FILE : Reading test/data/incoming1.raw
FILE : Writing out
HANDS: Handshake completed successfully
CHUNK: 02000763 6f6e6e65 ···c onne 000000 14 000003:000000 235:128,107
CHUNK: 00000b61 7564696f ···a udio 000000 14 000003:000000 235:107,0
CHUNK: 002625a0 ·&%· ···· ca90ea 05 000002:000000 4:4,0
END : ChunkStream gracefully ended
File renamed without changes.
15 changes: 15 additions & 0 deletions test/data/incoming2-dumpChunkStream.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
NOTE! ChunkStream is not complete without MessageStream and might get
stuck because of missing coordination with peer. Dump format:
CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest

FILE : Reading test/data/incoming2.raw
FILE : Writing out
HANDS: Handshake completed successfully
CHUNK: 02000763 6f6e6e65 ···c onne 000001 14 000003:000000 235:128,107
CHUNK: 00000b61 7564696f ···a udio 000001 14 000003:000000 235:107,0
CHUNK: 002625a0 ·&%· ···· 603010 05 000002:000000 4:4,0
CHUNK: 0002000c 63726561 ···· crea 003442 11 000003:000000 26:26,0
CHUNK: 00030000 00000000 ···· ···· 000000 04 000002:000000 10:10,0
CHUNK: 00030000 00010000 ···· ···· 000000 04 000002:000000 10:10,0
CHUNK: 00020004 706c6179 ···· play 005ef4 11 000008:000000 43:43,0
END : ChunkStream gracefully ended
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions test/data/outgoing2-dumpChunkStream.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
NOTE! ChunkStream is not complete without MessageStream and might get
stuck because of missing coordination with peer. Dump format:
CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest

FILE : Reading test/data/outgoing2.raw
FILE : Writing /dev/null
HANDS: Handshake completed successfully
CHUNK: 002625a0 ·&%· ···· 000000 05 000002:000000 4:4,0
CHUNK: 002625a0 02 ·&%· ···· 000000 06 000002:000000 5:5,0
CHUNK: 00000000 0000 ···· ···· 000000 04 000002:000000 6:6,0
CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 189:128,61
CHUNK: 65736372 69707469 escr ipti 000000 14 000003:000000 189:61,0
CHUNK: 0200086f 6e425744 ···o nBWD 000000 14 000003:000000 30:30,0
CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 29:29,0
END : ChunkStream gracefully ended
File renamed without changes.
34 changes: 34 additions & 0 deletions test/data/outgoing3-dumpChunkStream.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
NOTE! ChunkStream is not complete without MessageStream and might get
stuck because of missing coordination with peer. Dump format:
CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest

FILE : Reading test/data/outgoing3.raw
FILE : Writing /dev/null
HANDS: Handshake completed successfully
CHUNK: 002625a0 ·&%· ···· 000000 05 000002:000000 4:4,0
CHUNK: 002625a0 02 ·&%· ···· 000000 06 000002:000000 5:5,0
CHUNK: 00000000 0000 ···· ···· 000000 04 000002:000000 6:6,0
CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 189:128,61
CHUNK: 65736372 69707469 escr ipti 000000 14 000003:000000 189:61,0
CHUNK: 0200086f 6e425744 ···o nBWD 000000 14 000003:000000 30:30,0
CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 29:29,0
CHUNK: 00040000 0001 ···· ···· 000000 04 000002:000000 6:6,0
CHUNK: 00000000 0001 ···· ···· 000000 04 000002:000000 6:6,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 134:128,6
CHUNK: 37383400 0009 784· ···· 000000 14 000014:000000 134:6,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 134:128,6
CHUNK: 37383400 0009 784· ···· 000000 14 000014:000000 134:6,0
CHUNK: 0200117c 52746d70 ···| Rtmp 000000 12 000014:000000 24:24,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 12 000014:000000 44:44,0
CHUNK: 02000a6f 6e4d6574 ···o nMet 000000 12 000014:000000 105:105,0
CHUNK: 00400000 ·@·· ···· 000000 01 000002:000000 4:4,0
INFO : ChunkStream: Setting chunk size to 16384
CHUNK: 12000084 030299c7 ···· ···· 000000 09 000015:000000 1376:1376,0
CHUNK: 32000084 074298dc 2··· ·B·· 00004e 09 000015:000000 901:901,0
CHUNK: 02000c6f 6e506c61 ···o nPla 000000 12 000014:000000 102:102,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 143:143,0
CHUNK: 00010000 0001 ···· ···· 000000 04 000002:000000 6:6,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 136:136,0
CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 143:143,0
CHUNK: 00010000 0001 ···· ···· 000000 04 000002:000000 6:6,0
END : ChunkStream gracefully ended
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.mockSocket = function(name, assert) {
var incoming = dir + name + '.raw';
var outgoing = dir + 'out-' + new Date().getTime().toString(36) + '.raw';
return asSocket(incoming, outgoing
, function() { console.log('mockSocket(): reading ' + incoming); }
, function() { /* console.log('mockSocket(): reading ' + incoming); */ }
, function() { fs.unlink(outgoing); }
);
}
Expand Down

0 comments on commit 5e7aa69

Please sign in to comment.