Module: RTSP::Common

Included in:
Request, Response
Defined in:
lib/rtsp/common.rb

Overview

Contains common methods belonging to Request and Response classes.

Instance Method Summary collapse

Instance Method Details

#extract_status_line(line) ⇒ Object

Pulls out the RTSP version, request code, and request message (AKA the status line info) into instance variables.

Parameters:

  • line (String)

    The String containing the status line info.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rtsp/common.rb', line 46

def extract_status_line(line)
  /RTSP\/(?<rtsp_version>\d\.\d)/ =~ line
  /(?<url>rtsp:\/\/.*) RTSP/ =~ line
  /rtsp:\/\/.*stream(?<stream_index>\d*)m?\/?.* RTSP/ =~ line
  @url = url
  @stream_index = stream_index.to_i

  if rtsp_version.nil?
    raise RTSP::Error, "Status line corrupted: #{line}"
  end
end

#inspectString

Custom redefine to make sure all the dynamically created instance variables are displayed when this method is called.

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rtsp/common.rb', line 15

def inspect
  me = "#<#{self.class.name}:#{self.__id__} "

  self.instance_variables.each do |variable|
    me << "#{variable}=#{instance_variable_get(variable).inspect}, "
  end

  me.sub!(/, $/, "")
  me << ">"

  me
end

#multicast?Boolean

Checks if the request is for a multicast stream.

Returns:

  • (Boolean)

    true if the request is for a multicast stream.



74
75
76
77
78
# File 'lib/rtsp/common.rb', line 74

def multicast?
  return false if @url.nil?

  @url.end_with? "m"
end

#parse_body(body) ⇒ SDP::Description, String

Reads through each line of the RTSP response body and parses it if needed. Returns a SDP::Description if the Content-Type is ‘application/sdp’, otherwise returns the String that was passed in.

Parameters:

  • body (String)

Returns:

  • (SDP::Description, String)


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rtsp/common.rb', line 118

def parse_body body
  if body =~ /^(\r\n|\n)/
    body.gsub!(/^(\r\n|\n)/, '')
  end

  if @content_type == "application/sdp"
    SDP.parse body
  else
    body
  end
end

#parse_head(head) ⇒ Object

Reads through each header line of the RTSP request, extracts the request code, request message, request version, and creates a snake-case accessor with that value set.

Parameters:

  • head (String)

    The section of headers from the request text.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rtsp/common.rb', line 85

def parse_head head
  lines = head.split "\r\n"

  lines.each_with_index do |line, i|
    if i == 0
      extract_status_line(line)
      next
    end

    if line.include? "Session: "
      value = {}
      line =~ /Session: (\d+)/
      value[:session_id] = $1.to_i

      if line =~ /timeout=(.+)/
        value[:timeout] = $1.to_i
      end

      create_reader("session", value)
    elsif line.include? ": "
      header_and_value = line.strip.split(":", 2)
      header_name = header_and_value.first.downcase.gsub(/-/, "_")
      create_reader(header_name, header_and_value[1].strip)
    end
  end
end

#split_head_and_body_from(raw_request) ⇒ Array<String>

Takes the raw request text and splits it into a 2-element Array, where 0 is the text containing the headers and 1 is the text containing the body.

Parameters:

  • raw_request (String)

Returns:

  • (Array<String>)

    2-element Array containing the head and body of the request. Body will be nil if there wasn’t one in the request.



34
35
36
37
38
39
40
# File 'lib/rtsp/common.rb', line 34

def split_head_and_body_from raw_request
  head_and_body = raw_request.split("\r\n\r\n", 2)
  head = head_and_body.first
  body = head_and_body.last == head ? nil : head_and_body.last

  [head, body]
end

#to_sString

Returns The unparsed request as a String.

Returns:

  • (String)

    The unparsed request as a String.



7
8
9
# File 'lib/rtsp/common.rb', line 7

def to_s
  @raw_body
end

#transport_urlString

Returns the transport URL.

Returns:

  • (String)

    Transport URL associated with the request.



61
62
63
64
65
66
67
68
69
# File 'lib/rtsp/common.rb', line 61

def transport_url
  /client_port=(?<port>.*)-/ =~ transport

  if port.nil?
    RTSP::Request.log("Could not find client port associated with transport", :warn)
  else
    "#{@remote_host}:#{port}"
  end
end