Net::SIP::SDP - Parsing and manipulation of SDP data for SIP


  my $sdp = Net::SIP::SDP->new( sdp_string );
  my @media = $sdp->get_media;


Net::SIP::SDP can parse and manipulate SDP data.

It's not a general purpose SDP class (like Net::SDP) but designed to work with SDP data contained in SIP packets and for easy extraction and manipulation (for NAT etc) of media information contained in the SDP.

The class is also designed for easy creation of SDP bodies in the context of the rest of Net::SIP::*.


  # creation based on media data
  my $sdp = Net::SIP::SDP->new(
        { addr => '' },
        { port => 2012, proto => 'RTP/AVP', media => 'audio', fmt => 0 },
        { port => 2014, proto => 'RTP/AVP', media => 'video', fmt => 0 },

  # parse from string
  my $sdp = Net::SIP::SDP->new( sdp_string );

  # extract all media data
  my @media = $sdp->get_media;

  # and replace them with new addr + port (for NAT)
  my @new_media,;
  foreach (@media) {
        my ($port,@socks) = create_rtp_sockets( '', $_->{range} );
        push @new_media, [ '', $port ];
  $sdp->replace_media_listen( @new_media );



Default constructor. Depending on kind of arguments branches into new_from_string or new_from_parts. See there.

new_from_string ( STRING )

Creates object from STRING containing the SDP data. Raises an exception (e.g. die()) if SDP is invalid.

new_from_parts ( \%GLOBAL, @MEDIA )

Creates object from specification. %GLOBAL describes the global keys, usually only a common addr for all media but any of the keys defined in RFC2327 can be used.

@MEDIA is a list of hash references, one hash for each media part. These hashes can contain as keys the one-letter keys specified in RFC2327 and/or special keys for constructing the 'c' and 'm' line:

addr - The address, used in the 'c' line.
port - The port number
range - Range of ports, for RTP/AVP defaults to 2, else 1
media - The media typ, e.g. 'audio','video',...
proto - Transport protocol, ususally 'RTP/AVP' or 'udp'

If the SDP should contain multiple values for the same key in the same media section on can specify the value for the key as a \@list instead of a string (this is often the case for 'a' lines).



Returns string representation for object.


Returns 'application/sdp'


Returns list of all media described in the SDP. If the caller expects an array the result will be a list, otherwise a reference to a list.

Each element of the list is a hash with the following keys:

addr - IP4/IP6 address for media
port - Start port
range - Range for ports
proto - Media proto, usually 'RTP/AVP' or 'udp'
media - Media typ, usually 'audio', 'video' or 'data'
fmt - Format info from media line as \@list, e.g [ 0,10,5 ].
lines - All lines from media description as \@list of [ key,value ].

WARNING! You should never manipulate the values you got from this function, because this might affect the objects internals.

replace_media_listen ( NEW_MEDIA )

Replaces the exisisting media in the object with new media. Useful for NAT.

NEW_MEDIA is ether an array or a reference to an array. Each element in the list consists of the new [ addr,port ] mapping for the matching media entry.

The number of entries in the list should be the same as the number of media entries in the object ( see get_media ). If this is not the case it will die().

name2int ( NAME, INDEX )

Returns the RTP payload id for NAME (e.g. "telephone-event/8000"). INDEX is the index into the list of media information, matching the list returned from get_media. INDEX can also be 'audio','video'.., which will then lookup at the first matching entry in the media list.