Class: RTSP::Presentation

Inherits:
Object
  • Object
show all
Includes:
LogSwitch::Mixin
Defined in:
lib/rtsp/presentation.rb

Overview

Per the RFC definition:

A set of one or more streams presented to the client as a complete media
feed, using a presentation description [...].  In most cases in the RTSP
context, this implies aggregate control of those streams, but does not
have to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) {|_self| ... } ⇒ Presentation

Returns a new instance of Presentation.

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
34
# File 'lib/rtsp/presentation.rb', line 28

def initialize(base_url)
  @base_url = base_url
  @streams = []
  @description = default_description

  yield self if block_given?
end

Instance Attribute Details

#descriptionObject (readonly)

The SDP description that describes all of the streams. Per the RFC definition:

A presentation description contains information about one or more media
streams within a presentation, such as the set of encodings, network
addresses and information about the content.


24
25
26
# File 'lib/rtsp/presentation.rb', line 24

def description
  @description
end

#streamsObject (readonly)

The list of stream objects that a server controls.



17
18
19
# File 'lib/rtsp/presentation.rb', line 17

def streams
  @streams
end

#urlObject

Returns the value of attribute url.



26
27
28
# File 'lib/rtsp/presentation.rb', line 26

def url
  @url
end

Instance Method Details

#add_stream {|| ... } ⇒ Object

Yield Parameters:

  • (RTSP::Stream)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rtsp/presentation.rb', line 37

def add_stream(&block)
  stream = RTSP::Stream.new
  yield stream

  log "Adding stream: #{stream}"

  @description.media_sections << {
    media: stream.media_type,
    port: stream.client_rtp_port,
    format: stream.format,
    protocol: stream.protocol,
    attributes: stream.attributes
  }

  @streams << stream
end

#default_descriptionSDP::Description

Returns:

  • (SDP::Description)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rtsp/presentation.rb', line 90

def default_description
  sdp = SDP::Description.new
  sdp.username = Etc.getlogin
  sdp.id = Time.now.to_ntp
  sdp.version = sdp.id
  sdp.network_type = "IN"
  sdp.address_type = "IP4"

  sdp.unicast_address = UDPSocket.open do |s|
    s.connect('64.233.187.99', 1); s.addr.last
  end

  sdp.name = "Ruby RTSP Stream"
  sdp.information = "This is a Ruby RTSP stream"
  sdp.connection_network_type = "IN"
  sdp.connection_address_type = "IP4"
  sdp.connection_address = sdp.unicast_address
  sdp.start_time = 0
  sdp.stop_time = 0

  sdp.attributes << { tool: "RubyRTSP #{RTSP::VERSION}" }
  sdp.attributes << { control: "*" }
  # User must still define media section.

  sdp
end

#media_urlsArray

Returns The list of URLs that make up all of the media streams for this presentation.

Returns:

  • (Array)

    The list of URLs that make up all of the media streams for this presentation.



67
68
69
70
71
# File 'lib/rtsp/presentation.rb', line 67

def media_urls
  @streams.map do |stream|
    "#{@base_url}#{stream.uri}"
  end
end

#setup_rtp_sender(type) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rtsp/presentation.rb', line 117

def setup_rtp_sender(type)
  case type
  when :socat
    @rtp_sender = RTP::Sender.instance
    @rtp_sender.stream_module = RTP::Senders::Socat

    yield @rtp_sender if block_given?


  when :indirection
    # Get remote server's URL
    server_url = yield()

    # Get description from the remote RTSP server
    require_relative 'client'
    client = RTSP::Client.new(server_url)
    response = client.describe
    @description = response.body

    # Get the control URL for the main remote presentation and set that for
    # this URL
    control = @description.attributes.find { |a| a[:attribute] == "control" }
    self.url = control[:value]

    # Get the control URL for the first media section so we can get the
    # Stream object that was setup in configuration.
    media_control =
      @description.media_sections.first[:attributes].find { |a| a[:attribute] == "control" }
    redirected_stream = stream_at(media_control[:value])

    # Create a socket to redirect the RTP data that we'll start receiving.
    redirected_socket = UDPSocket.new
    redirected_socket.bind('0.0.0.0', redirected_stream.client_rtp_port)
    client.capturer.rtp_file = redirected_socket

    # Setup
    media_track = client.media_control_tracks.first
    aggregate_track = client.aggregate_control_track
    client.setup media_track
    client.play aggregate_track
  end
end

#stream_at(uri) ⇒ RTSP::Stream

Returns The stream that’s identified by the URI.

Parameters:

  • uri (String)

Returns:

  • (RTSP::Stream)

    The stream that’s identified by the URI.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rtsp/presentation.rb', line 75

def stream_at(uri)
  puts "looking for stream at #{uri}"

  streams.find do |stream|
    log "Checking stream match on #{stream.uri}"

    if URI(uri).absolute?
      @base_url + stream.uri == uri
    else
      stream.uri == uri
    end
  end
end