sdp

<img src=“https://secure.travis-ci.org/turboladen/sdp.png?branch=master” alt=“Build Status” />

Description

SDP is used by a number of protocols for describing multimedia sessions; protocols include SIP (Session Initiation Protocol), SAP (Session Announcement Protocol), and RTSP (Real Time Streaming Protocol).

This gem has two purposes:

  1. To use as a client to parse SDP descriptions into useful objects

  2. To allow for creating an SDP description from scratch

Since SDP descriptions contain a lot of useful data pieces, but due to the design of SDP descriptions, not only is it tough to know what’s what when you visually look at that description, but it’s tough to extract out the piece of data that you need to work with (ex. session ID). The SDP.parse method takes a description and parses it in to a Hash-like SDP::Description object, which has accessor methods (that mimic the RFC 4566 document) for easily getting and setting values.

Similarly, since it’s difficult to remember all the ins and outs of building an SDP description string that meets the requirements of RFC 4566, the SDP::Description class has a #to_s method that takes the fields that you’ve populated (from using the accessors) and turns those in to a string that’s suitable to use for an SDP description.

Features

  • Parse an SDP description from String to Ruby object

  • Create new SDP description

Examples

Creating an SDP description

require 'sdp/description'

sdp = SDP::Description.new
sdp.inspect       # => {:session_section=>{
                  #       :time_zones=>[], :attributes=>[], :protocol_version=>0
                  #      },
                  #     :media_sections=>[]
                  #    } 
sdp.to_s          # => "v=0\no=     \ns=\nc=  \nt= \n\n" 
sdp.username = "elvis"
sdp.media_sections << { :media => "video", :port => 9000, :format => 0, :protocol => "RTP/AVP", :attributes => [{ :attribute => "recvonly" }] }
sdp.media_sections << { :media => "audio", :port => 9100, :format => 33, :protocol => "RTP/AVP", :attributes => [{ :attribute => "rtpmap", :value => "99 h263-1998/90000" }] }

# Fields are stored in a Hash, where the session information goes in :session_section,
# and each media section goes in an Array at :media_sections:
sdp.inspect     # => {
                #     :session_section=>{
                #       :time_zones=>[], :attributes=>[], :protocol_version=>0,
                #       :username=>"elvis"
                #     },
                #     :media_sections=>[
                #       {
                #         :media=>"video", :port=>9000, :format=>0,
                #         :protocol=>"RTP/AVP", :attributes=>[{:attribute=>"recvonly"}]
                #       }, {
                #         :media=>"audio", :port=>9100, :format=>33,
                #         :protocol=>"RTP/AVP", :attributes=>[{:attribute=>"rtpmap", :value=>"99 h263-1998/90000"}]
                #       }
                #     ]
                #   }
sdp.id = 1
sdp.version = 123
sdp.network_type = :IN
sdp.address_type = :IP4
sdp.name = " "
sdp.start_time = 1
sdp.stop_time = 10
sdp.valid?      # => false (Require fields haven't yet been given)
sdp.errors      # => ["unicast_address", "connection_network_type", "connection_address_type", "connection_address"]
sdp.unicast_address = "127.0.0.1"
sdp.connection_network_type = :IN
sdp.connection_address_type = :IP4
sdp.connection_address = "127.0.0.1"
sdp.valid?      # => true
sdp.to_s        # => "v=0\r\no=elvis 1 123 IN IP4 127.0.0.1\r\ns= \r\nc=IN IP4 127.0.0.1\r\nt=1 10\r\nm=video 9000 RTP/AVP 0\r\na=recvonly\r\nm=audio 9100 RTP/AVP 33\r\na=rtpmap:99 h263-1998/90000\r\n"

Parsing an SDP description

sdp_string = <<-EOF
v=0
o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
s=SDP Seminar
i=A Seminar on the session description protocol
u=http://www.example.com/seminars/sdp.pdf
[email protected] (Jane Doe)
p=+1 617 555-6011
c=IN IP4 224.2.17.12/127
b=CT:1000
t=2873397496 2873404696
r=604800 3600 0 90000
z=2882844526 -1h
k=clear:password
a=recvonly
a=type:test
m=audio 49170 RTP/AVP 0
m=video 51372 RTP/AVP 99
a=rtpmap:99 h263-1998/90000
EOF

session = SDP.parse sdp_string

session.class               # => SDP::Description
session.protocol_version    # => "0"
session.media_sections      # => [{:media=>"audio", :port=>"49170", :protocol=>"RTP/AVP", :format=>"0", :attributes=>""}, {:media=>"video", :port=>"51372", :protocol=>"RTP/AVP", :format=>"99", :attributes=>[{:attribute=>"rtpmap", :value=>"99 h263-1998/90000"}]}] 
session.username            # => "jdoe"
session.id                  # => "2890844526"
session.version             # => "2890842807"
session.network_type        # => "IN"
session.address_type        # => "IP4"
session.unicast_address     # => "10.47.16.5"
session.name                # => "SDP Seminar"
session.information         # => "A Seminar on the session description protocol"
session.uri                 # => "http://www.example.com/seminars/sdp.pdf"
session.email_address       # => "[email protected] (Jane Doe)"
session.connection_address  # => "224.2.17.12/127"
session.start_time          # => 2873397496
session.stop_time           # => 2873404696
session.attributes          # => [{:attribute=>"recvonly"}, {:attribute=>"type", :value=>"test"}] 

# Put it back to a string...
session.to_s        # => "v=0\r\n
                    #     o=elvis 2890844526 2890842807 IN IP4 10.47.16.5\r\n
                    #     s=SDP Seminar\r\n
                    #     i=A Seminar on the session description protocol\r\n
                    #     u=http://www.example.com/seminars/sdp.pdf\r\n
                    #     [email protected] (Jane Doe)\r\n
                    #     c=IN IP4 224.2.17.12/127\r\n
                    #     t=2873397496 2873404696\r\n
                    #     a=recvonly\r\n
                    #     m=audio 49170 RTP/AVP 0\r\n
                    #     m=video\r\n"
                    #     a=rtpmap:99 h263-1998/90000\r\n"

Requirements

  • Rubies (tested, at least):

    • 1.8.7

    • 1.9.2

    • 1.9.3

    • JRuby (1.8 and 1.9 modes)

    • Rubinius (1.8 and 1.9 modes)

  • Gems:

    • parslet, >= 1.1.0

Install

$ gem install sdp

Copyright © 2011-2012 Steve Loveless

See LICENSE.rdoc for details.