RUBY 模拟rtsp消息

require 'rtsp/client'
require 'log_switch'
require 'socket'

RTSP::Client.log?                # => false
RTSP::Client.log = true
client = RTSP::Client.new "rtsp://10.0.10.200:8557/H264"

client.server_uri                   # => #<URI::Generic:0x00000100ba4db0 URL:rtsp://64.202.98.91:554/sa.sdp>
client.session_state                # => :init
client.cseq                         # => 1
client.connection.do_capture        # => true
client.connection.interleave        # => false
client.connection.timeout           # => 30
client.capturer.ip_addressing_type  # => :unicast
client.capturer.rtp_port            # => 6970
client.capturer.capture_file        # => #<File:/var/folders/tg/j9jxvvfs4qn9cg4vztzyy2gc0000gp/T/rtp_capture.raw-59901-1l8dgv2>
client.capturer.transport_protocol  # => :UDP

response = client.options
response.class             # => RTSP::Response
response.code              # => 200
response.message           # => "OK"
client.cseq                # => 2

response = client.describe
response.body.class             # => SDP::Description
response.content_type           # => "application/sdp"
#response.server                 # => "DSS/5.5 (Build/489.7; Platform/Linux; Release/Darwin; )"
client.aggregate_control_track  # => "rtsp://64.202.98.91:554/sa.sdp/"
client.media_control_tracks     # => ["rtsp://64.202.98.91:554/sa.sdp/trackID=1"]
client.cseq                     # => 3

response = client.setup(client.media_control_tracks.first)
response.session[:session_id]   # => 7098486223178290313
client.session[:session_id]     # => 7098486223178290313
client.cseq                     # => 4
client.session_state            # => :ready

response = client.play(client.aggregate_control_track)
response.range            # => "npt=now="
response.rtp_info         # => "url=rtsp://64.202.98.91:554/sa.sdp/trackID=1"
client.session_state      # => :playing

# Wait while the video streams
 sleep 5
#
client.pause(client.aggregate_control_track)
client.session_state    # => :ready
#
# # Wait while the video is paused
sleep 2
#
client.teardown(client.aggregate_control_track)
client.session[:session_id]   # => 0
client.session_state          # => :init

执行后,如下:

[root@localhost script]# ruby rtsp.rb
[2017-03-31 15:17:49 +0800] Sending OPTIONS to rtsp://10.0.10.200:8557/H264
[2017-03-31 15:17:49 +0800] OPTIONS rtsp://10.0.10.200:8557/H264 RTSP/1.0
[2017-03-31 15:17:49 +0800] CSeq: 1
[2017-03-31 15:17:49 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Received response:
[2017-03-31 15:17:49 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:49 +0800] CSeq: 1
[2017-03-31 15:17:49 +0800] Date: Fri, Mar 31 2017 07:18:17 GMT
[2017-03-31 15:17:49 +0800] Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, GET_PARAMETER, SET_PARAMETER
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Sending DESCRIBE to rtsp://10.0.10.200:8557/H264
[2017-03-31 15:17:49 +0800] DESCRIBE rtsp://10.0.10.200:8557/H264 RTSP/1.0
[2017-03-31 15:17:49 +0800] CSeq: 2
[2017-03-31 15:17:49 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:49 +0800] Accept: application/sdp
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Received response:
[2017-03-31 15:17:49 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:49 +0800] CSeq: 2
[2017-03-31 15:17:49 +0800] Date: Fri, Mar 31 2017 07:18:17 GMT
[2017-03-31 15:17:49 +0800] Content-Base: rtsp://10.0.10.200:8557/H264/
[2017-03-31 15:17:49 +0800] Content-Type: application/sdp
[2017-03-31 15:17:49 +0800] Content-Length: 615
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] v=0
[2017-03-31 15:17:49 +0800] o=- 1489133093616171 1 IN IP4 10.0.10.200
[2017-03-31 15:17:49 +0800] s=RTSP/RTP stream from IPNC
[2017-03-31 15:17:49 +0800] i=H264
[2017-03-31 15:17:49 +0800] t=0 0
[2017-03-31 15:17:49 +0800] a=tool:LIVE555 Streaming Media v2014.09.11
[2017-03-31 15:17:49 +0800] a=type:broadcast
[2017-03-31 15:17:49 +0800] a=control:*
[2017-03-31 15:17:49 +0800] a=range:npt=0-
[2017-03-31 15:17:49 +0800] a=x-qt-text-nam:RTSP/RTP stream from IPNC
[2017-03-31 15:17:49 +0800] a=x-qt-text-inf:H264
[2017-03-31 15:17:49 +0800] m=video 0 RTP/AVP 96
[2017-03-31 15:17:49 +0800] c=IN IP4 0.0.0.0
[2017-03-31 15:17:49 +0800] b=AS:12000
[2017-03-31 15:17:49 +0800] a=rtpmap:96 H264/90000
[2017-03-31 15:17:49 +0800] a=fmtp:96 packetization-mode=1;profile-level-id=640032;sprop-parameter-sets=J2QAMq2EBUViuKxUcQgKisVxWKjiECSFITk8nyfk/k/J8nm5s00IEkKQnJ5Pk/J/J+T5PNzZphcqAeAIn5ZsgAAB9AAA6mBwAAAPQkAAAPQkBe91hAAAAAE=,KP4Brg==
[2017-03-31 15:17:49 +0800] a=control:track1
[2017-03-31 15:17:49 +0800] m=audio 0 RTP/AVP 0
[2017-03-31 15:17:49 +0800] c=IN IP4 0.0.0.0
[2017-03-31 15:17:49 +0800] b=AS:64
[2017-03-31 15:17:49 +0800] a=control:track2
[2017-03-31 15:17:49 +0800] Sending SETUP to rtsp://10.0.10.200:8557/H264/track1
[2017-03-31 15:17:49 +0800] SETUP rtsp://10.0.10.200:8557/H264/track1 RTSP/1.0
[2017-03-31 15:17:49 +0800] CSeq: 3
[2017-03-31 15:17:49 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:49 +0800] Transport: RTP/AVP;unicast;client_port=6970-6971
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Received response:
[2017-03-31 15:17:49 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:49 +0800] CSeq: 3
[2017-03-31 15:17:49 +0800] Date: Fri, Mar 31 2017 07:18:17 GMT
[2017-03-31 15:17:49 +0800] Transport: RTP/AVP;unicast;destination=172.17.3.54;source=10.0.10.200;client_port=6970-6971;server_port=6970-6971
[2017-03-31 15:17:49 +0800] Session: 2A4A2DAF;timeout=65
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Sending PLAY to rtsp://10.0.10.200:8557/H264/
[2017-03-31 15:17:49 +0800] PLAY rtsp://10.0.10.200:8557/H264/ RTSP/1.0
[2017-03-31 15:17:49 +0800] CSeq: 4
[2017-03-31 15:17:49 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:49 +0800] Session: 2A4A2DAF
[2017-03-31 15:17:49 +0800] Range: npt=0.000-
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Received response:
[2017-03-31 15:17:49 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:49 +0800] CSeq: 4
[2017-03-31 15:17:49 +0800] Date: Fri, Mar 31 2017 07:18:17 GMT
[2017-03-31 15:17:49 +0800] Range: npt=0.000-
[2017-03-31 15:17:49 +0800] Session: 2A4A2DAF
[2017-03-31 15:17:49 +0800] RTP-Info: url=rtsp://10.0.10.200:8557/H264/track1;seq=5877;rtptime=3937889106,url=rtsp://10.0.10.200:8557/H264/track2;seq=0;rtptime=0
[2017-03-31 15:17:49 +0800]
[2017-03-31 15:17:49 +0800] Capturing RTP data on port 6970
[2017-03-31 15:17:54 +0800] Sending PAUSE to rtsp://10.0.10.200:8557/H264/
[2017-03-31 15:17:54 +0800] PAUSE rtsp://10.0.10.200:8557/H264/ RTSP/1.0
[2017-03-31 15:17:54 +0800] CSeq: 5
[2017-03-31 15:17:54 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:54 +0800] Session: 2A4A2DAF
[2017-03-31 15:17:54 +0800]
[2017-03-31 15:17:54 +0800] Received response:
[2017-03-31 15:17:54 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:54 +0800] CSeq: 5
[2017-03-31 15:17:54 +0800] Date: Fri, Mar 31 2017 07:18:22 GMT
[2017-03-31 15:17:54 +0800] Session: 2A4A2DAF
[2017-03-31 15:17:54 +0800]
[2017-03-31 15:17:56 +0800] Sending TEARDOWN to rtsp://10.0.10.200:8557/H264/
[2017-03-31 15:17:56 +0800] TEARDOWN rtsp://10.0.10.200:8557/H264/ RTSP/1.0
[2017-03-31 15:17:56 +0800] CSeq: 6
[2017-03-31 15:17:56 +0800] User-Agent: RubyRTSP/0.4.5 (Ruby 2.0.0-p648)
[2017-03-31 15:17:56 +0800] Session: 2A4A2DAF
[2017-03-31 15:17:56 +0800]
[2017-03-31 15:17:57 +0800] Received response:
[2017-03-31 15:17:57 +0800] RTSP/1.0 200 OK
[2017-03-31 15:17:57 +0800] CSeq: 6
[2017-03-31 15:17:57 +0800] Date: Fri, Mar 31 2017 07:18:25 GMT

参考文档:https://github.com/turboladen/rtsp

原文地址:https://www.cnblogs.com/fithon/p/6651616.html