-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
executable file
·190 lines (140 loc) · 5.88 KB
/
control.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# This file is part of pyracast.
#
# pyracast is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyracast is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---
#
# Most of this file is taken from or at least heavily inspired by the
# piracast project, licensed under GPLv3 or later, found at
# https://github.com/codemonkeyricky/piracast
import re
import socket
m1_options_rsp = 'RTSP/1.0 200 OK\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 1\r\n' \
'Public: org.wfa.wfd1.0, GET_PARAMETER, SET_PARAMETER\r\n' \
'\r\n'
m2_req = 'OPTIONS * RTSP/1.0\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 2\r\n' \
'Require: org.wfa.wfd1.0\r\n' \
'\r\n'
m3_body = 'wfd_video_formats: 39 00 02 02 0001FFFF 3FFFFFFF 00000000 00 0000 0000 00 none none\r\n' \
'wfd_audio_codecs: LPCM 00000002 00\r\n' \
'wfd_client_rtp_ports: RTP/AVP/UDP;unicast 50000 0 mode=play\r\n'
m3_rsp = 'RTSP/1.0 200 OK\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 2\r\n' \
'Content-Type: text/parameters\r\n' \
'Content-Length: ' + str(len(m3_body)) + '\r\n' \
'\r\n' + m3_body
m4_rsp = 'RTSP/1.0 200 OK\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 3\r\n' \
'\r\n'
m5_rsp = 'RTSP/1.0 200 OK\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 4\r\n' \
'\r\n'
m6_req = 'SETUP rtsp://{}/wfd1.0/streamid=0 RTSP/1.0\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 4\r\n' \
'Transport: RTP/AVP/UDP;unicast;client_port=50000-50001\r\n' \
'\r\n'
m7_req = 'PLAY rtsp://{}/wfd1.0/streamid=0 RTSP/1.0\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: 5\r\n' \
'Session: {}\r\n' \
'\r\n'
m16_rsp = 'RTSP/1.0 200 OK\r\n' \
'Date: Sun, 11 Aug 2013 04:41:40 +000\r\n' \
'Server: stagefright/1.2 (Linux;Android 4.3)\r\n' \
'CSeq: {}\r\n' \
'\r\n'
def source_connect(dev, ip, port):
# Create a socket object
s = socket.socket()
# Connect to port
s.connect((ip, port))
# Connect via wireless interface
#s.setsockopt(socket.SOL_SOCKET, 25, dev)
# Wait to receive data
data = s.recv(1024)
print 'Received %s' % repr(data)
# Send m1 response
s.send(m1_options_rsp)
# Send m2 request
s.send(m2_req)
# Receive m2 response
m2_rsp = s.recv(1024)
print 'M2 Resp: %s' % repr(m2_rsp)
# Receive: M3 GET_PARAMETER Request
m3_req = s.recv(1024)
print 'M3 Req:\n%s\n' % repr(m3_req)
print 'M3 Resp:\n%s\n' % repr(m3_rsp)
# Send: M3 Response
s.send(m3_rsp)
# Receive: M4 RTSP SET_PARAMETER Request
m4_req = s.recv(1024)
print 'M4 Req:\n%s\n' % repr(m4_req)
print 'M4 Resp:\n%s\n' % repr(m4_rsp)
# Send: M4 response
s.send(m4_rsp)
# Receive: M5 RTSP SET_PARAMETER Request (setup)
m5_req = s.recv(1024)
# Send: M5 Response
s.send(m5_rsp)
# Send: M6 RTSP SETUP Request
s.send(m6_req.format(ip))
# Receive: M6 RTSP SETUP response
m6_rsp = s.recv(1024)
print 'M6 Rsp: %s' % repr(m6_rsp)
# TODO: extract session ID
match = re.search(r'Session: (\d*)', m6_rsp)
# Send: M7 RTSP PLAY Request
s.send(m7_req.format(ip, match.group(1)))
m7_rsp = s.recv(1024)
print 'M7 Rsp: %s' % repr(m7_rsp)
cseq = 5
keepalives_sent = 0
while 1:
req = s.recv(1024)
print 'req: %s' % req
if req.__len__() == 0:
print 'socket closed!'
break
if 'TEARDOWN' in req:
print 'Teardown received!'
break
#if keepalives_sent == 120:
# print 'demo time\'s up!'
# break
s.send(m16_rsp.format(cseq))
print 'sent keepalive!'
cseq += 1
keepalives_sent += 1
s.close()
if __name__ == "__main__":
import sys
source_connect(sys.argv[1], sys.argv[2], int(sys.argv[3]))