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:.



161
162
163
# File 'lib/rtp/receiver.rb', line 161

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.



128
129
130
# File 'lib/rtp/receiver.rb', line 128

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.



149
150
151
152
# File 'lib/rtp/receiver.rb', line 149

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.



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

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:

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

Yields:

  • (RTP::Packet)

    Each parsed packet that comes in over the wire.

Returns:

  • (Boolean)

    true if started successfully.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rtp/receiver.rb', line 95

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rtp/receiver.rb', line 113

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.



155
156
157
# File 'lib/rtp/receiver.rb', line 155

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.



133
134
135
# File 'lib/rtp/receiver.rb', line 133

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