in reply to Binary conversion, RTP header

BTW, anyone trying to extract information from RTP packets should note that nearly all of your code is going to great lengths to get 'raw' packets and then going to great lengths to strip off the nested headers to get to $user_data. Yes, I realize that you have reasons for doing it this way, but I wanted to note that people who just want what you asked for can just grab RTP packets in much simpler ways, where they come with the IP and UDP headers already stripped off. So forget all of the above code and just read the RTP packet into $user_data. (:

If you google for rtp rfc, then you'll quickly find this:

5.1 RTP Fixed Header Fields The RTP header has the following format: |0 | 1 | 2 | 3 | |0 1 2 3 4 5 6 7|8 9 0 1 2 3 4 5|6 7 8 9 0 1 2 3|4 5 6 7 8 9 0 1| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |V=2|P|X| CC |M| PT | sequence number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | synchronization source (SSRC) identifier | +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | contributing source (CSRC) identifiers | | .... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Then you read the pack documentation and then the unpack docs and extracting these fields is quite simple:

my( $bits, $type, $seq, $time )= unpack "C C n N", $user_data;

- tye        

Replies are listed 'Best First'.
Re^2: Binary conversion, RTP header (unpack)
by adamtistler (Initiate) on Dec 22, 2005 at 04:20 UTC
    You have no idea how much this has helped me. Thank you so much for spreading your wisdom to a perl newbie. One more thing, the $header variable in this case is a reference to a hash?? I believe that it contians references to packet recv time, len, etc. How can I view that information??
    sub process_rtp_packets { my ($user_data, $header, $packet) = @_; for my $key ( keys %header ) { my $value = $header{$key}; print "$key => $value\n"; } my $eth_obj = NetPacket::Ethernet->decode($packet); my $ether_data = NetPacket::Ethernet::strip($packet); my $ip_obj = NetPacket::IP->decode($ether_data); my $ip_data = NetPacket::IP::strip($ether_data); my $udp_obj = NetPacket::UDP->decode($ip_data); my $RTP_data = $udp_obj->{data}; my( $bits, $type, $seq, $time )= unpack "C C n N", $RTP_data; print "$bits\n"; print "$type\n"; print "$seq\n"; print "$time\n"; }

      Add 'use strict;' and change %header to %$header and change $header{...} to $header->{...}. You might find References quick reference helpful. For more in-depth information on references, start with "perldoc perlreftut"

      - tye