/
rtsp.rb
219 lines (191 loc) · 5.22 KB
/
rtsp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
111
112
113
114
115
116
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'net/protocol'
require 'net/http'
module Net
class RTSP < Protocol
class << self
def default_port
5000
end
end
attr_accessor :debug_output
attr_reader :port
attr_reader :address
def initialize(address, id, instance, port = RTSP.default_port)
@address = address
@port = port
@started = false
@debug_output = nil
@cseq = 0
@open_timeout = nil
@socket = nil
@client_id = id
@client_instance = instance
end
def request(req)
unless started?
start {
return request(req)
}
end
req['Client-Instance'] = @client_instance
begin_transport(req)
res = transport_request(req)
end_transport(req, res)
res
end
def start
raise IOError, 'RTSP session already open' if @started
if block_given?
begin
do_start
return yield(self)
ensure
do_finish
end
end
do_start
self
end
def started?; @started; end
def connect
s = timeout(@open_timeout) { TCPSocket.open(address(), port())}
@socket = BufferedIO.new(s)
end
private
def transport_request(req)
req.exec(@socket, @client_id, @cseq += 1)
RTSPResponse.read_new(@socket)
end
def begin_transport(req)
end
def end_transport(req, res)
@socket.close if res['connection'] == 'close'
end
def do_start
connect
@started = true
end
def do_finish
end
class RTSPResponse < HTTPResponse
class << self
def read_status_line(sock)
str = sock.readline
m = /^RTSP\/(\d+\.\d+)\s(\d+)\s(.*)$/.match(str)
m.captures
end
end
end
class RTSPGenericRequest < HTTPGenericRequest
def initialize(method)
@method = method
@body = nil
initialize_http_header nil
self['User-Agent'] = 'iTunes/4.6 (Macintosh; U; PPC Mac OS X 10.3)'
end
def exec(sock, client_id, cseq)
write_header(sock, client_id, cseq)
write_body(sock)
end
private
def write_header(sock, client_id, cseq, url = nil)
self['Content-Length'] = @body.length.to_s if @body
url ||= sprintf("rtsp://%s/%s", sock.io.addr.last, client_id)
buf = "#{@method} #{url} RTSP/1.0\r\n" +
"CSeq: #{cseq}\r\n"
each_capitalized do |k,v|
buf << "#{k}: #{v}\r\n"
end
buf << "\r\n"
#$stdout.write buf
sock.write buf
end
def write_body(sock)
#$stdout.write(@body) if @body
sock.write(@body) if @body
end
end
class GetParameter < RTSPGenericRequest
def initialize(session_id, parameters)
super('GET_PARAMETER')
self['Content-Type'] = 'text/parameters'
self['Session'] = session_id
@body = parameters.join("\r\n") + "\r\n"
end
end
class Options < RTSPGenericRequest
def initialize
super('OPTIONS')
end
private
def write_header(sock, client_id, cseq, url = nil)
super(sock, client_id, cseq, '*')
end
end
class Teardown < RTSPGenericRequest
def initialize
super('TEARDOWN')
end
end
class Flush < RTSPGenericRequest
def initialize(session_id, seq)
super('Flush')
self['Session'] = session_id
self['RTP-Info'] = "seq=0;rtptime=0"
end
end
class SetParameter < RTSPGenericRequest
def initialize(session_id, opts = {})
super('SET_PARAMETER')
self['Content-Type'] = 'text/parameters'
buf = ''
opts.each do |k,v|
buf << "#{k}: #{sprintf('%0.6f', v)}\r\n"
end
@body = buf if buf.length > 0
end
end
class Record < RTSPGenericRequest
def initialize(session_id)
super('RECORD')
self['Range'] = 'npt=0-'
self['RTP-Info'] = "seq=0;rtptime=0"
self['Session'] = session_id
end
end
class Setup < RTSPGenericRequest
def initialize
super('SETUP')
self['Transport'] = 'RTP/AVP/TCP;unicast;interleaved=0-1;mode=record'
end
end
class Announce < RTSPGenericRequest
def initialize(sac, key, iv)
super('ANNOUNCE')
@key = key
@iv = iv
self['Content-Type'] = 'application/sdp'
self['Apple-Challenge'] = sac.gsub(/[=\s]/, '')
end
private
def write_header(sock, client_id, cseq)
@body = sprintf(
"v=0\r\n" +
"o=iTunes %s 0 IN IP4 %s\r\n" +
"s=iTunes\r\n" +
"c=IN IP4 %s\r\n" +
"t=0 0\r\n" +
"m=audio 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 AppleLossless\r\n" +
"a=fmtp:96 4096 0 16 40 10 14 2 255 0 0 44100\r\n" +
"a=rsaaeskey:%s\r\n" +
"a=aesiv:%s\r\n",
client_id.gsub(/[=\s]/, ''),
sock.io.addr.last, sock.io.peeraddr.last,
@key.gsub(/[=\s]/, ""),
@iv.gsub(/[=\s]/, "" ))
super(sock, client_id, cseq)
end
end
end
end