Different codecs for different VSP

Support zone
AndrewZ
Posts: 19
Joined: Thu Nov 13, 2008 10:03 pm

Re: Different codecs for different VSP

Post by AndrewZ » Fri Nov 19, 2010 5:18 pm

MikeTelis wrote: Thus, we need to use the names from original SDP (or am I missing something?).
Agree - we need to use all the names found in the original SDP and try to keep those names correct on the SIP UA side.

MikeTelis
Posts: 1582
Joined: Wed Jul 30, 2008 6:48 am

Re: Different codecs for different VSP

Post by MikeTelis » Fri Nov 19, 2010 5:43 pm

So, I've simplified the code by removing all a=* manipulations, and I added codecs() method returning a comma-separated list of codecs.

Code: Select all

class SDP
  CRLF = "\r\n"
  def initialize sdp, &block
    @lines = sdp.split(CRLF); @codecs = {}
    @lines.each_with_index do |ln, i|
      ln =~ /^a=rtpmap:(\d+)\s+(.+)\// and @codecs[$2.upcase] = $1.to_i
      !@m && ln =~ /^(m=.*RTP\/AVP\s+)(.+)$/ and (@mstr = $1; @m = $2.split(/\s+/).map {|x| x.to_i}; @pos = i)
    end
    instance_eval(&block) if block_given?
  end

  def priority(order); requested = codec2pt(order); @m = requested + (@m-requested); self; end
  def delete(clist); @m = @m - codec2pt(clist); self; end
  def supported?(codec); @codecs[codec.upcase]; end
  def to_s; @pos && (@lines[@pos] = @mstr + @m.join(' '); @lines.join(CRLF)); end; alias :to_str :to_s
  def codecs; @codecs.keys.join(', '); end

  private
  # Find PTs for requested codecs, remove codecs not listed in original m=*
  def codec2pt(s); s.gsub(/\s+/,'').split(',').map{|x| supported?(x)}.compact; end
end
I gave it a whirl and it works just as expected. Check out and let me know if you find anything unusual.

Update: Optimized the code.

Post Reply