Class: RTSP::Message

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rtsp/message.rb

Overview

This class is responsible for building a single RTSP message that can be used by both clients and servers.

Only message types defined in RFC 2326 are implemented, however if you need to add a new message type (perhaps for some custom server implementation?), you can simply add to the supported list by:

RTSP::Message.message_types << :barrel_roll

You can then build it like a standard message:

message = RTSP::Message.barrel_roll("192.168.1.10").with_headers({
cseq: 123, content_type: "video/x-m4v" })

Constant Summary collapse

RTSP_ACCEPT_TYPE =
"application/sdp"
RTSP_DEFAULT_NPT =
"0.000-"
RTSP_DEFAULT_SEQUENCE_NUMBER =
1
USER_AGENT =
"RubyRTSP/#{RTSP::VERSION} (Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})"

Constants included from Global

Global::DEFAULT_RTSP_PORT, Global::DEFAULT_VERSION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Global

#log, #log_level, #logger, #raise_errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#build_resource_uri_from

Methods included from Global

#log?, #raise_errors?, #reset_config!, #rtsp_version

Constructor Details

#initialize(method_type, request_uri) ⇒ Message

Returns a new instance of Message.

Parameters:

  • :method_type (Symbol)

    The RTSP method to build and send.

  • request_uri (String)

    The URL to communicate to.



80
81
82
83
84
85
86
# File 'lib/rtsp/message.rb', line 80

def initialize(method_type, request_uri)
  @method_type = method_type
  @request_uri = build_resource_uri_from request_uri
  @headers     = default_headers
  @body        = ""
  @version     = DEFAULT_VERSION
end

Class Attribute Details

.message_typesArray<Symbol>

Lists the method/message types this class can create.

Returns:

  • (Array<Symbol>)


47
48
49
# File 'lib/rtsp/message.rb', line 47

def message_types
  @message_types
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



75
76
77
# File 'lib/rtsp/message.rb', line 75

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



74
75
76
# File 'lib/rtsp/message.rb', line 74

def headers
  @headers
end

#method_typeObject (readonly)

Returns the value of attribute method_type.



72
73
74
# File 'lib/rtsp/message.rb', line 72

def method_type
  @method_type
end

#request_uriObject (readonly)

Returns the value of attribute request_uri.



73
74
75
# File 'lib/rtsp/message.rb', line 73

def request_uri
  @request_uri
end

#rtsp_version=(value) ⇒ Object (writeonly)

Sets the attribute rtsp_version

Parameters:

  • value

    the value to set the attribute rtsp_version to.



76
77
78
# File 'lib/rtsp/message.rb', line 76

def rtsp_version=(value)
  @rtsp_version = value
end

Class Method Details

.method_missing(method, *args) ⇒ RTSP::Message

Creates a new message based on the given method type and URI.

Parameters:

  • method (Symbol)
  • args (Array)

Returns:



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

def method_missing(method, *args)
  request_uri = args.first

  if @message_types.include? method
    self.new(method, request_uri)
  else
    super
  end
end

.respond_to?(method) ⇒ Boolean

Make sure the class responds to our message types.

Parameters:

  • method (Symbol)

Returns:

  • (Boolean)


52
53
54
# File 'lib/rtsp/message.rb', line 52

def respond_to?(method)
  @message_types.include?(method) || super
end

Instance Method Details

#add_body(new_body) ⇒ Object



135
136
137
138
# File 'lib/rtsp/message.rb', line 135

def add_body new_body
  add_headers({ content_length: new_body.length })
  @body = new_body
end

#add_headers(new_headers) ⇒ Object



119
120
121
# File 'lib/rtsp/message.rb', line 119

def add_headers(new_headers)
  @headers.merge! new_headers
end

#header(type, value) ⇒ Object

Adds the header and its value to the list of headers for the message.

Parameters:

  • type (Symbol)

    The header type.

  • []

    value The value to set the header field to.



92
93
94
95
96
97
98
# File 'lib/rtsp/message.rb', line 92

def header(type, value)
  if type.is_a? Symbol
    headers[type] = value
  else
    raise RTSP::Error, "Header type must be a Symbol (i.e. :cseq)."
  end
end

#to_sString

Returns The message as a String.

Returns:

  • (String)

    The message as a String.



148
149
150
# File 'lib/rtsp/message.rb', line 148

def to_s
  message.to_s
end

#with_body(new_body) ⇒ Object

Use when creating a new Message to add body you want.

Examples:

Simple header

RTSP::Message.options("192.168.1.10").with_body("The body!")

Parameters:

  • new_headers (Hash)

    The headers to add to the Request. The Hash key will be capitalized; if



129
130
131
132
133
# File 'lib/rtsp/message.rb', line 129

def with_body(new_body)
  add_body new_body

  self
end

#with_headers(new_headers) ⇒ RTSP::Message

Use to message-chain with one of the method types; used when creating a new Message to add headers you want.

Examples:

Simple header

RTSP::Message.options("192.168.1.10").with_headers({ cseq: @cseq })

Multi-word header

RTSP::Message.options("192.168.1.10").with_headers({ user_agent:
'My RTSP Client 1.0' })   # => "OPTIONS 192.168.1.10 RTSP 1.0\r\n
                          #     CSeq: 1\r\n
                          #     User-Agent: My RTSP Client 1.0\r\n"

Parameters:

  • new_headers (Hash)

    The headers to add to the Request. The Hash key of each will be converted from snake_case to Rtsp-Style.

Returns:



113
114
115
116
117
# File 'lib/rtsp/message.rb', line 113

def with_headers(new_headers)
  add_headers new_headers

  self
end