-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial implementation of AMF3 and Flex messages
- Loading branch information
Showing
5 changed files
with
275 additions
and
53 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
out | ||
tmp | ||
|
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 |
---|---|---|
@@ -1,37 +1,109 @@ | ||
"use strict"; | ||
|
||
require('colors'); | ||
var net = require('net'); | ||
var util = require('util'); | ||
var net = require('net'); | ||
var mtrude = require('mtrude'); | ||
var ChunkStream = mtrude.rtmp.ChunkStream; | ||
var MessageStream = mtrude.rtmp.MessageStream; | ||
var AMF = mtrude.rtmp.AMF; | ||
var rtmp = mtrude.rtmp; | ||
var ChunkStream = rtmp.ChunkStream; | ||
var MessageStream = rtmp.MessageStream; | ||
var AMF = rtmp.AMF; | ||
var asSocket = mtrude.asSocket; | ||
var dumpTools = require('./dumpTools'); | ||
|
||
function main() { | ||
var optimist = require('optimist') | ||
.usage('Usage: $0 [--debug] [--messages] [--nocolor] [--ignore] [in [out]]') | ||
.boolean('debug') | ||
.boolean('nocolor') | ||
.boolean('messages') | ||
.boolean('ignore') | ||
.boolean('help') | ||
.alias('h', 'help') | ||
.describe('debug', 'Set MessageStream.DBG = true') | ||
.describe('messages', 'Also dump messages') | ||
.describe('nocolor', 'Don\'t use colors') | ||
.describe('ignore', 'Ignore errors') | ||
; | ||
var argv = optimist.argv; | ||
|
||
if (argv.help || argv._.length > 2) { | ||
var messageStream = new MessageStream(chunkStream); | ||
if (argv.messages) require('./dumpMessageStream')(messageStream); | ||
dumpAmf(messageStream); | ||
optimist.showHelp(); | ||
return; | ||
} | ||
|
||
if (argv.nocolor) dumpTools.dontColor(); | ||
|
||
if (argv.debug) MessageStream.DBG = true; | ||
|
||
if (argv._.length == 0) { | ||
var server = net.createServer(); | ||
server.on('connection', function(socket) { | ||
console.log('CONN : ' + 'Connection from %s'.grey, socket.remoteAddress); | ||
var chunkStream = new ChunkStream(); | ||
}); | ||
server.listen(1935); | ||
console.log('LISTN: ' + 'Listening on port 1935'.grey); | ||
} | ||
else { | ||
var iFile = argv._[0]; | ||
var time36 = new Date().getTime().toString(36) | ||
var oFile = argv._[1] || 'out-' + time36 + '.raw'; | ||
var chunkStream = new ChunkStream(asSocket(iFile, oFile | ||
, function() { console.log('FILE : ' + 'Reading %s'.grey, iFile); } | ||
, function() { console.log('FILE : ' + 'Writing %s'.grey, oFile); } | ||
)); | ||
} | ||
|
||
var chain = new mtrude.BufferChain(); | ||
var messageStream = new MessageStream(chunkStream); | ||
if (argv.messages) require('./dumpMessageStream')(messageStream); | ||
dumpAmf(messageStream, argv.ignore); | ||
} | ||
|
||
function dumpZ(buffer, messageStream) { | ||
return util.inspect(AMF.deserializeZ(buffer, messageStream)); | ||
} | ||
|
||
var server = net.createServer(); | ||
server.on('connection', function(socket) { | ||
console.log('Connection from %s', socket.remoteAddress); | ||
var messageStream = new MessageStream(new ChunkStream(socket)); | ||
function dump3(buffer, messageStream) { | ||
return util.inspect(AMF.deserialize3(buffer, messageStream)); | ||
} | ||
|
||
function dumpAmf(messageStream, ignoreErrors) { | ||
messageStream.on('error', function(msg) { | ||
console.log('ERROR %s'.red, msg); | ||
server.close(); | ||
console.log('ERROR: %s', msg.red); | ||
if (!ignoreErrors) messageStream.close(); | ||
}); | ||
messageStream.on('warn', function(msg) { | ||
console.log('WARN %s'.magenta, msg); | ||
console.log('WARN : %s', msg.magenta); | ||
if (!ignoreErrors) messageStream.close(); | ||
}); | ||
messageStream.on('end', function(graceful) { | ||
console.log('END :', 'Ended', (graceful ? '' : 'not ') + 'gracefully'); | ||
var msg = 'dumpAmf MessageStream ' + (graceful ? '' : 'not ') | ||
+ 'gracefully ended'; | ||
console.log('END :', graceful ? msg.green : msg.red); | ||
}); | ||
messageStream.on('message', function(message) { | ||
console.log( | ||
'MESSAGE: %s:%s typeid=%s data=%s'.blue, message.csid, | ||
message.msid, message.typeid, message.data.length); | ||
if (message.typeid == 20) { | ||
console.log('INVOKE'.green, util.inspect(AMF.deserialize(message.data))); | ||
switch (message.typeid) { | ||
case rtmp.types.INVOKE: | ||
console.log('INVOK: %s', dumpZ(message.data, messageStream).magenta); | ||
break; | ||
case rtmp.types.SO: | ||
console.log('SHOBJ: %s', dumpZ(message.data, messageStream).blue); | ||
break; | ||
case rtmp.types.NOTIFY: | ||
console.log('NOTIF: %s', dumpZ(message.data, messageStream).blue); | ||
break; | ||
case rtmp.types.FLEX: | ||
console.log('FLEX : %s', dump3(message.data, messageStream).cyan); | ||
break; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
server.listen(1935); | ||
console.log(); | ||
console.log('Listening on port 1935'); | ||
if (require.main === module) main(); | ||
exports.dumpAmf = dumpAmf; | ||
|
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
Oops, something went wrong.