Class: RTP::Receiver
- Inherits:
-
Object
- Object
- RTP::Receiver
- 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
-
#capture_file ⇒ File
readonly
The file to capture the RTP data to.
-
#ip_address ⇒ String
The IP address to receive RTP data on.
-
#packet_timestamps ⇒ Array<Time>
readonly
The packet receipt timestamps.
-
#rtcp_port ⇒ Fixnum
readonly
Added for clarifying the roles of ports; not currently used though.
-
#rtp_port ⇒ Fixnum
The port on which to capture RTP data.
-
#transport_protocol ⇒ Symbol
The type of socket to use for capturing the RTP data.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Receiver
constructor
A new instance of Receiver.
-
#ip_addressing_type ⇒ Symbol
The IP addressing type to use for capturing the data.
-
#listening? ⇒ Boolean
True if the listener thread is running; false if not.
-
#multicast? ⇒ Boolean
True if
ip_address
is a multicast address or not. -
#running? ⇒ Boolean
True if the Receiver is listening and writing packets.
-
#start {|RTP::Packet| ... } ⇒ Boolean
Starts the packet writer (buffer) and listener.
-
#stop ⇒ Boolean
Stops the listener and packet writer threads.
-
#unicast? ⇒ Boolean
True if
ip_address
is a unicast address or not. -
#writing_packets? ⇒ Boolean
True if ready to write packets to file.
Constructor Details
#initialize(options = {}) ⇒ Receiver
Returns a new instance of Receiver.
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(={}) @rtp_port = [:rtp_port] || 6970 @rtcp_port = @rtp_port + 1 @transport_protocol = [:transport_protocol] || :UDP @ip_address = [:ip_address] || '0.0.0.0' @strip_headers = [:strip_headers] || false @capture_file = [: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_file ⇒ File (readonly)
Returns 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_address ⇒ String
Returns 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_timestamps ⇒ Array<Time> (readonly)
Returns The packet receipt timestamps.
31 32 33 |
# File 'lib/rtp/receiver.rb', line 31 def @packet_timestamps end |
#rtcp_port ⇒ Fixnum (readonly)
Returns 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_port ⇒ Fixnum
Returns 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_protocol ⇒ Symbol
Returns 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_type ⇒ Symbol
Returns 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.
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.
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.
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:
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 |
#stop ⇒ Boolean
Stops the listener and packet writer threads.
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.
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.
142 143 144 |
# File 'lib/rtp/receiver.rb', line 142 def writing_packets? !@packet_writer.nil? ? @packet_writer.alive? : false end |