Class: SippyCup::Media::RTPHeader

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/sippy_cup/media/rtp_header.rb

Constant Summary collapse

VERSION =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RTPHeader

Returns a new instance of RTPHeader.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sippy_cup/media/rtp_header.rb', line 11

def initialize(args = {})
  # TODO: Support Extension Header
  super(
    (args[:version] ? args[:version] : VERSION),
    (args[:padding] ? args[:padding] : 0),
    (args[:extension] ? args[:extension] : 0),
    (args[:marker] ? args[:marker] : 0),
    (args[:payload_id] ? args[:payload_id] : 0),
    Int16.new(args[:sequence_num] ? args[:sequence_num] : 0),
    Int32.new(args[:timestamp] ? args[:timestamp] : 0),
    Int32.new(args[:ssrc_id] ? args[:ssrc_id] : 0),
    (args[:csrc_ids] ? Array(args[:csrc_ids]) : []),
  )
end

Instance Attribute Details

#csrc_idsObject

Returns the value of attribute csrc_ids

Returns:

  • (Object)

    the current value of csrc_ids



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def csrc_ids
  @csrc_ids
end

#extensionObject

Returns the value of attribute extension

Returns:

  • (Object)

    the current value of extension



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def extension
  @extension
end

#markerObject

Returns the value of attribute marker

Returns:

  • (Object)

    the current value of marker



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def marker
  @marker
end

#paddingObject

Returns the value of attribute padding

Returns:

  • (Object)

    the current value of padding



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def padding
  @padding
end

#payload_idObject

Returns the value of attribute payload_id

Returns:

  • (Object)

    the current value of payload_id



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def payload_id
  @payload_id
end

#sequence_numObject

Returns the value of attribute sequence_num

Returns:

  • (Object)

    the current value of sequence_num



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def sequence_num
  @sequence_num
end

#ssrc_idObject

Returns the value of attribute ssrc_id

Returns:

  • (Object)

    the current value of ssrc_id



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def ssrc_id
  @ssrc_id
end

#timestampObject

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def timestamp
  @timestamp
end

#versionObject

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



6
7
8
# File 'lib/sippy_cup/media/rtp_header.rb', line 6

def version
  @version
end

Instance Method Details

#csrc_countObject



43
44
45
# File 'lib/sippy_cup/media/rtp_header.rb', line 43

def csrc_count
  csrc_ids.count
end

#csrc_ids_readableObject



47
48
49
# File 'lib/sippy_cup/media/rtp_header.rb', line 47

def csrc_ids_readable
  csrc_ids.to_s
end

#read(str) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sippy_cup/media/rtp_header.rb', line 26

def read(str)
  self[:version] = str[0].ord >> 6
  self[:padding] = (str[0].ord >> 5) & 1
  self[:extension] = (str[0].ord >> 4) & 1
  num_csrcs = str[0].ord & 0xf
  self[:marker] = str[1] >> 7
  self[:payload_id] = str[1] & 0x7f
  self[:sequence_num].read str[2,2]
  self[:timestamp].read str[4,4]
  self[:ssrc_id].read str[8,4]
  i = 8
  num_csrcs.times do
    self[:csrc_ids] << Int32.new(str[i += 4, 4])
  end
  self[:body] = str[i, str.length - i]
end

#to_sObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sippy_cup/media/rtp_header.rb', line 51

def to_s
  bytes = [
    (version << 6) + (padding << 5) + (extension << 4) + (csrc_count),
    (marker << 7) + (payload_id),
    sequence_num,
    timestamp,
    ssrc_id
  ].pack 'CCnNN'

  csrc_ids.each do |csrc_id|
    bytes << [csrc_id].pack('N')
  end

  bytes
end