#!/usr/bin/python # Copyright (c) 2012, Matthew Daley # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from this # software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import StringIO import sys import bidibid.rtmp_packet import bidibid.rtmp_mplxr import bidibid.rtmp_session SPLITTER = "-" * 78 def print_stats(mplxr): print SPLITTER print "Multiplexer: %d channels (%d active), %d B read (%d B pending), %d packets read" % \ (mplxr.get_num_channels(True, False), mplxr.get_num_channels(), mplxr.bytes_in, mplxr.get_bytes_pending(), mplxr.packets_in) for channel_id, in_state in mplxr._in_states.items(): import bidibid.util print "-- Channel %d --" % channel_id print "Payload: (len %d B (0x%X B))" % ((len(in_state['payload']),) * 2) print "<%s>" % bidibid.util.hexdump(in_state['payload']) print "State: %r" % in_state['channel'].__dict__ print "Stream: offset %d B (0x%X B)" % ((sys.stdin.tell(),) * 2) class EventHandler(object): _PACKET_HANDLERS = { bidibid.rtmp_packet.TYPE_BYTES_READ: lambda packet, payload: bidibid.rtmp_bytes_read.read_bytes_read(payload), bidibid.rtmp_packet.TYPE_PING: lambda packet, payload: bidibid.rtmp_ping.read_ping(payload), bidibid.rtmp_packet.TYPE_TO_SERVER_BANDWIDTH: lambda packet, payload: bidibid.rtmp_bandwidth.read_server_bandwidth(payload), bidibid.rtmp_packet.TYPE_TO_CLIENT_BANDWIDTH: lambda packet, payload: bidibid.rtmp_bandwidth.read_client_bandwidth(payload), bidibid.rtmp_packet.TYPE_NOTIFY: lambda packet, payload: bidibid.rtmp_invoke.read_notify(payload, packet.peer), bidibid.rtmp_packet.TYPE_SHARED_OBJECT: lambda packet, payload: bidibid.rtmp_so_msg.read_so_message(payload, False),#, packet.peer), bidibid.rtmp_packet.TYPE_INVOKE: lambda packet, payload: bidibid.rtmp_invoke.read_invoke(payload, packet.peer) } def on_channel_chunk_size_change(self, mplxr, channel_id, chunk_size): print SPLITTER print "Channel %d: changed chunk size to %d B (0x%X B)" % (channel_id, chunk_size, chunk_size) print_stats(mplxr) def on_packet_read(self, mplxr, packet, channel_id, jitter): print SPLITTER print "Channel %d: packet %d" % (channel_id, mplxr.packets_in) print packet if packet.type in self._PACKET_HANDLERS: payload = StringIO.StringIO(packet.payload) print self._PACKET_HANDLERS[packet.type](packet, payload) print_stats(mplxr) def on_chunk_read(self, mplxr): print SPLITTER print_stats(mplxr) if len(sys.argv) >= 2: if sys.argv[1] == "posthandshake": offset = 1 + 2 * bidibid.rtmp_session.RTMPSession._HANDSHAKE_CHUNK_SIZE else: is_hex_offset = sys.argv[1][:2] in ["0x", "0X"] offset = int(sys.argv[1][2 if is_hex_offset else 0:], 16 if is_hex_offset else 10) print >>sys.stderr, "Starting from offset %d B (0x%X B)" % ((offset,) * 2) sys.stdin.read(offset) print >>sys.stderr, "Reading stream.." mplxr = bidibid.rtmp_mplxr.RTMPMultiplexer(EventHandler()) print_stats(mplxr) mplxr.read(sys.stdin) print SPLITTER