lib/rtsp/client.rb in rtsp-0.4.3 vs lib/rtsp/client.rb in rtsp-0.4.4

- old
+ new

@@ -1,9 +1,10 @@ require 'socket' require 'tempfile' require 'timeout' require 'rtp/receiver' +require "base64" require_relative 'transport_parser' require_relative 'error' require_relative 'global' require_relative 'helpers' @@ -58,11 +59,11 @@ MAX_BYTES_TO_RECEIVE = 3000 # @return [URI] The URI that points to the RTSP server's resource. attr_reader :server_uri - + # @return [Fixnum] Also known as the "sequence" number, this starts at 1 and # increments after every request to the server. It is reset after # calling #teardown. attr_reader :cseq @@ -119,10 +120,11 @@ @connection.timeout ||= 30 @connection.socket ||= TCPSocket.new(@server_uri.host, @server_uri.port) @connection.do_capture ||= true @connection.interleave ||= false + @max_authorization_tries = 3 @cseq = 1 reset_state end # The URL for the RTSP server to talk to can change if multiple servers are @@ -132,10 +134,24 @@ # @param [String] new_url The new server URL to use to communicate over. def server_url=(new_url) @server_uri = build_resource_uri_from new_url end + # The user to be used in Basic Authentication + # + # @param [String] user + def server_user=(user) + @server_uri.user = user + end + + # The password to be used in Basic Authentication + # + # @param [String] password + def server_password=(password) + @server_uri.password = password + end + # Sends the message over the socket. # # @param [RTSP::Message] message # @return [RTSP::Response] # @raise [RTSP::Error] If the timeout value is reached and the server hasn't @@ -251,12 +267,14 @@ @session_state = :ready end @session = response.session parser = RTSP::TransportParser.new + #pass up raw transport for debug and/or logging + yield response.transport if block_given? @transport = parser.parse(response.transport) - + unless @transport[:transport_protocol].nil? @capturer.transport_protocol = @transport[:transport_protocol] end @capturer.rtp_port = @transport[:client_port][:rtp].to_i @@ -381,13 +399,14 @@ # @raise [RTSP::Error] All 4xx & 5xx response codes & their messages. def request message response = send_message message #compare_sequence_number response.cseq @cseq += 1 - if response.code.to_s =~ /2../ yield response if block_given? + elsif response.code == 401 + send_authorization(message) elsif response.code.to_s =~ /(4|5)../ if (defined? response.connection) && response.connection == 'Close' reset_state end @@ -439,10 +458,22 @@ tracks end private - + + # Resends a message with an Authorization header when possible + # @param [RTSP:Message] message The message that must be repeated with Authorization + def send_authorization(message) + if @server_uri.user and @server_uri.password + credentials = Base64.strict_encode64("#{@server_uri.user}:#{@server_uri.password}") + headers = { :authorization => "Basic #{credentials}" } + new_message = RTSP::Message.send(message.method_type.to_sym,@server_uri.to_s).with_headers(headers) + new_message.add_headers message.headers + request(new_message) + end + end + # Sets state related variables back to their starting values; # +@session_state+ is set to +:init+; +@session+ is set to 0. def reset_state @session_state = :init @session = {}