Class: RTP::Receiver

Inherits:
Object
  • Object
show all
Includes:
LogSwitch::Mixin
Defined in:
lib/rtp/receiver.rb

Overview

Objects of this type receive RTP data over a socket and either save them to a file, or yield the packets to a given block. This is useful with other protocols, like RTSP.

Constant Summary collapse

DEFAULT_CAPFILE_NAME =

Name of the file the data will be captured to unless #rtp_file is set.

"rtp_capture.raw"
MAX_BYTES_TO_RECEIVE =

Maximum number of bytes to receive on the socket.

1500
MULTICAST_TTL =

TTL value that will be used if receiving on a multicast socket.

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Receiver

Returns a new instance of Receiver.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :rtp_port (Fixnum)

    The port on which to capture RTP data. rtcp_port will be set to the next port above this.

  • :transport_protocol (Symbol)

    The type of socket to use for capturing the data. :UDP or :TCP.

  • :ip_address (String)

    The IP address to open the socket on. If this is a multicast address, multicast options will be set.

  • :strip_headers (Boolean)

    If set to true, RTP headers will be stripped from packets before they’re written to the capture file.

  • :capture_file (File)

    The file object to capture the RTP data to.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rtp/receiver.rb', line 58

def initialize(options={})
  @rtp_port           = options[:rtp_port]           || 6970
  @rtcp_port          = @rtp_port + 1
  @transport_protocol = options[:transport_protocol] || :UDP
  @ip_address         = options[:ip_address]         || '0.0.0.0'
  @strip_headers      = options[:strip_headers]      || false
  @capture_file       = options[:capture_file]       ||
    Tempfile.new(DEFAULT_CAPFILE_NAME)

  at_exit do
    unless @capture_file.closed?
      log "Closing and deleting capture capture file..."
      @capture_file.close
      @capture_file.unlink
    end
  end

  @socket = nil
  @listener = nil
  @packet_writer = nil
  @packets = Queue.new
  @packet_timestamps = []
end

Instance Attribute Details

#capture_fileFile (readonly)

Returns The file to capture the RTP data to.

Returns:

  • (File)

    The file to capture the RTP data to.



28
29
30
# File 'lib/rtp/receiver.rb', line 28

def capture_file
  @capture_file
end

#ip_addressString

Returns The IP address to receive RTP data on.

Returns:

  • (String)

    The IP address to receive RTP data on.



45
46
47
# File 'lib/rtp/receiver.rb', line 45

def ip_address
  @ip_address
end

#packet_timestampsArray<Time> (readonly)

Returns The packet receipt timestamps.

Returns:

  • (Array<Time>)

    The packet receipt timestamps.



31
32
33
# File 'lib/rtp/receiver.rb', line 31

def packet_timestamps
  @packet_timestamps
end

#rtcp_portFixnum (readonly)

Returns Added for clarifying the roles of ports; not currently used though.

Returns:

  • (Fixnum)

    Added for clarifying the roles of ports; not currently used though.



38
39
40
# File 'lib/rtp/receiver.rb', line 38

def rtcp_port
  @rtcp_port
end

#rtp_portFixnum

Returns The port on which to capture RTP data.

Returns:

  • (Fixnum)

    The port on which to capture RTP data.



34
35
36
# File 'lib/rtp/receiver.rb', line 34

def rtp_port
  @rtp_port
end

#transport_protocolSymbol

Returns The type of socket to use for capturing the RTP data. :UDP or :TCP.

Returns:

  • (Symbol)

    The type of socket to use for capturing the RTP data. :UDP or :TCP.



42
43
44
# File 'lib/rtp/receiver.rb', line 42

def transport_protocol
  @transport_protocol
end

Instance Method Details

#ip_addressing_typeSymbol

Returns The IP addressing type to use for capturing the data. :multicast or +:unicast:.

Returns:

  • (Symbol)

    The IP addressing type to use for capturing the data. :multicast or +:unicast:.



170
171
172
# File 'lib/rtp/receiver.rb', line 170

def ip_addressing_type
  multicast? ? :multicast : :unicast
end

#listening?Boolean

Returns true if the listener thread is running; false if not.

Returns:

  • (Boolean)

    true if the listener thread is running; false if not.



137
138
139
# File 'lib/rtp/receiver.rb', line 137

def listening?
  !@listener.nil? ? @listener.alive? : false
end

#multicast?Boolean

Returns true if ip_address is a multicast address or not.

Returns:

  • (Boolean)

    true if ip_address is a multicast address or not.



158
159
160
161
# File 'lib/rtp/receiver.rb', line 158

def multicast?
  first_octet = @ip_address.match(/^(\d\d\d?)/).to_s.to_i
  first_octet >= 224 && first_octet <= 239
end

#running?Boolean

Returns true if the Receiver is listening and writing packets.

Returns:

  • (Boolean)

    true if the Receiver is listening and writing packets.



147
148
149
# File 'lib/rtp/receiver.rb', line 147

def running?
  listening? && writing_packets?
end

#start {|RTP::Packet| ... } ⇒ Boolean

Starts the packet writer (buffer) and listener.

If a block is given, this will yield each parsed packet as an RTP::Packet. This lets you inspect packets as they come in:

Examples:

Just the packet

receiver = RTP::Receiver.new
receiver.start do |packet|
  puts packet.sequence_number
end

The packet and its timestamp

receiver = RTP::Receiver.new
receiver.start do |packet, timestamp|
  puts packet.sequence_number
  puts timestamp
end

Yields:

  • (RTP::Packet)

    Each parsed packet that comes in over the wire.

  • (Time)

    The timestamp from the packet as it was received on the socket.

Returns:

  • (Boolean)

    true if started successfully.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rtp/receiver.rb', line 104

def start(&block)
  return false if running?
  log "Starting receiving on port #{@rtp_port}..."

  @packet_writer = start_packet_writer(&block)
  @packet_writer.abort_on_exception = true

  @socket = init_socket(@transport_protocol, @rtp_port, @ip_address)

  @listener = start_listener(@socket)
  @listener.abort_on_exception = true

  running?
end

#stopBoolean

Stops the listener and packet writer threads.

Returns:

  • (Boolean)

    true if stopped successfully.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rtp/receiver.rb', line 122

def stop
  return false if !running?

  log "Stopping #{self.class} on port #{@rtp_port}..."
  stop_listener
  log "listening? #{listening?}"

  stop_packet_writer
  log "writing packets? #{writing_packets?}"
  log "running? #{running?}"

  !running?
end

#unicast?Boolean

Returns true if ip_address is a unicast address or not.

Returns:

  • (Boolean)

    true if ip_address is a unicast address or not.



164
165
166
# File 'lib/rtp/receiver.rb', line 164

def unicast?
  !multicast?
end

#writing_packets?Boolean

Returns true if ready to write packets to file.

Returns:

  • (Boolean)

    true if ready to write packets to file.



142
143
144
# File 'lib/rtp/receiver.rb', line 142

def writing_packets?
  !@packet_writer.nil? ? @packet_writer.alive? : false
end