forked from Blueion76/eufyp2pstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheufyp2pstream.py
403 lines (363 loc) · 14.6 KB
/
eufyp2pstream.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from websocket import EufySecurityWebSocket
import aiohttp
import asyncio
import json
import socket
import select
import threading
import time
import sys
import signal
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
from queue import Queue
RECV_CHUNK_SIZE = 4096
video_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
audio_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
backchannel_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
EVENT_CONFIGURATION: dict = {
"livestream video data": {
"name": "video_data",
"value": "buffer",
"type": "event",
},
"livestream audio data": {
"name": "audio_data",
"value": "buffer",
"type": "event",
},
}
START_P2P_LIVESTREAM_MESSAGE = {
"messageId": "start_livestream",
"command": "device.start_livestream",
"serialNumber": None,
}
STOP_P2P_LIVESTREAM_MESSAGE = {
"messageId": "stop_livestream",
"command": "device.stop_livestream",
"serialNumber": None,
}
START_TALKBACK = {
"messageId": "start_talkback",
"command": "device.start_talkback",
"serialNumber": None,
}
SEND_TALKBACK_AUDIO_DATA = {
"messageId": "talkback_audio_data",
"command": "device.talkback_audio_data",
"serialNumber": None,
"buffer": None
}
STOP_TALKBACK = {
"messageId": "stop_talkback",
"command": "device.stop_talkback",
"serialNumber": None,
}
SET_API_SCHEMA = {
"messageId": "set_api_schema",
"command": "set_api_schema",
"schemaVersion": 13,
}
P2P_LIVESTREAMING_STATUS = "p2pLiveStreamingStatus"
START_LISTENING_MESSAGE = {"messageId": "start_listening", "command": "start_listening"}
TALKBACK_RESULT_MESSAGE = {"messageId": "talkback_audio_data", "errorCode": "device_talkback_not_running"}
DRIVER_CONNECT_MESSAGE = {"messageId": "driver_connect", "command": "driver.connect"}
run_event = threading.Event()
def exit_handler(signum, frame):
print(f'Signal handler called with signal {signum}')
run_event.set()
# Install signal handler
signal.signal(signal.SIGINT, exit_handler)
class ClientAcceptThread(threading.Thread):
def __init__(self,socket,run_event,name,ws,serialno):
threading.Thread.__init__(self)
self.socket = socket
self.queues = []
self.run_event = run_event
self.name = name
self.ws = ws
self.serialno = serialno
self.my_threads = []
def update_threads(self):
my_threads_before = len(self.my_threads)
for thread in self.my_threads:
if not thread.is_alive():
self.queues.remove(thread.queue)
self.my_threads = [t for t in self.my_threads if t.is_alive()]
if self.ws and my_threads_before > 0 and len(self.my_threads) == 0:
if self.name == "BackChannel":
print("All clients died (BackChannel): ", self.name)
sys.stdout.flush()
else:
print("All clients died. Stopping Stream: ", self.name)
sys.stdout.flush()
msg = STOP_P2P_LIVESTREAM_MESSAGE.copy()
msg["serialNumber"] = self.serialno
asyncio.run(self.ws.send_message(json.dumps(msg)))
def run(self):
print("Accepting connection for ", self.name)
msg = STOP_TALKBACK.copy()
msg["serialNumber"] = self.serialno
asyncio.run(self.ws.send_message(json.dumps(msg)))
while not self.run_event.is_set():
self.update_threads()
sys.stdout.flush()
try:
client_sock, client_addr = self.socket.accept()
print ("New connection added: ", client_addr, " for ", self.name)
sys.stdout.flush()
if self.name == "BackChannel":
client_sock.setblocking(True)
print("Starting BackChannel")
thread = ClientRecvThread(client_sock, run_event, self.name, self.ws, self.serialno)
thread.start()
else:
client_sock.setblocking(False)
thread = ClientSendThread(client_sock, run_event, self.name, self.ws, self.serialno)
self.queues.append(thread.queue)
if self.ws:
msg = START_P2P_LIVESTREAM_MESSAGE.copy()
msg["serialNumber"] = self.serialno
asyncio.run(self.ws.send_message(json.dumps(msg)))
self.my_threads.append(thread)
thread.start()
except socket.timeout:
pass
class ClientSendThread(threading.Thread):
def __init__(self,client_sock,run_event,name,ws,serialno):
threading.Thread.__init__(self)
self.client_sock = client_sock
self.queue = Queue(100)
self.run_event = run_event
self.name = name
self.ws = ws
self.serialno = serialno
def run(self):
print ("Thread running: ", self.name)
sys.stdout.flush()
try:
while not self.run_event.is_set():
ready_to_read, ready_to_write, in_error = \
select.select([], [self.client_sock], [self.client_sock], 2)
if len(in_error):
print("Exception in socket", self.name)
sys.stdout.flush()
break
if not len(ready_to_write):
print("Socket not ready to write ", self.name)
sys.stdout.flush()
break
if not self.queue.empty():
self.client_sock.sendall(
bytearray(self.queue.get(True)["data"])
)
except socket.error as e:
print("Connection lost", self.name, e)
pass
except socket.timeout:
print("Timeout on socket for ", self.name)
pass
try:
self.client_sock.shutdown(socket.SHUT_RDWR)
except OSError:
print ("Error shutdown socket: ", self.name)
self.client_sock.close()
print ("Thread stopping: ", self.name)
sys.stdout.flush()
class ClientRecvThread(threading.Thread):
def __init__(self,client_sock,run_event,name,ws,serialno):
threading.Thread.__init__(self)
self.client_sock = client_sock
self.run_event = run_event
self.name = name
self.ws = ws
self.serialno = serialno
def run(self):
msg = START_TALKBACK.copy()
msg["serialNumber"] = self.serialno
asyncio.run(self.ws.send_message(json.dumps(msg)))
try:
curr_packet = bytearray()
no_data = 0
while not self.run_event.is_set():
try:
ready_to_read, ready_to_write, in_error = \
select.select([self.client_sock,], [], [self.client_sock], 2)
if len(in_error):
print("Exception in socket", self.name)
sys.stdout.flush()
break
if len(ready_to_read):
data = self.client_sock.recv(RECV_CHUNK_SIZE)
curr_packet += bytearray(data)
if len(data) > 0: # and len(data) <= RECV_CHUNK_SIZE:
msg = SEND_TALKBACK_AUDIO_DATA.copy()
msg["serialNumber"] = self.serialno
msg["buffer"] = list(bytes(curr_packet))
asyncio.run(self.ws.send_message(json.dumps(msg)))
curr_packet = bytearray()
no_data = 0
else:
no_data += 1
else:
no_data += 1
if (no_data >= 15):
print("15x in a row no data in socket ", self.name)
sys.stdout.flush()
break
except BlockingIOError:
# Resource temporarily unavailable (errno EWOULDBLOCK)
pass
except socket.error as e:
print("Connection lost", self.name, e)
pass
except socket.timeout:
print("Timeout on socket for ", self.name)
pass
except select.error:
print("Select error on socket ", self.name)
pass
sys.stdout.flush()
try:
self.client_sock.shutdown(socket.SHUT_RDWR)
except OSError:
print ("Error shutdown socket: ", self.name)
sys.stdout.flush()
self.client_sock.close()
msg = STOP_TALKBACK.copy()
msg["serialNumber"] = self.serialno
asyncio.run(self.ws.send_message(json.dumps(msg)))
class Connector:
def __init__(
self,
run_event,
):
video_sock.bind(("0.0.0.0", 63336))
video_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
video_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
video_sock.settimeout(1) # timeout for listening
video_sock.listen()
audio_sock.bind(("0.0.0.0", 63337))
audio_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
audio_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
audio_sock.settimeout(1) # timeout for listening
audio_sock.listen()
backchannel_sock.bind(("0.0.0.0", 63338))
backchannel_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
backchannel_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
backchannel_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
backchannel_sock.settimeout(1) # timeout for listening
backchannel_sock.listen()
self.ws = None
self.run_event = run_event
self.serialno = ""
def stop(self):
try:
self.video_sock.shutdown(socket.SHUT_RDWR)
except OSError:
print ("Error shutdown socket")
self.video_sock.close()
try:
self.audio_sock.shutdown(socket.SHUT_RDWR)
except OSError:
print ("Error shutdown socket")
self.audio_sock.close()
try:
self.backchannel_sock.shutdown(socket.SHUT_RDWR)
except OSError:
print ("Error shutdown socket")
self.backchannel_sock.close()
def setWs(self, ws : EufySecurityWebSocket):
self.ws = ws
async def on_open(self):
print(f" on_open - executed")
async def on_close(self):
print(f" on_close - executed")
self.run_event.set()
self.ws = None
stop()
os._exit(-1)
async def on_error(self, message):
print(f" on_error - executed - {message}")
async def on_message(self, message):
payload = message.json()
message_type: str = payload["type"]
if message_type == "result":
message_id = payload["messageId"]
if message_id != SEND_TALKBACK_AUDIO_DATA["messageId"]:
# Avoid spamming of TALKBACK_AUDIO_DATA logs
print(f"on_message result: {payload}")
sys.stdout.flush()
if message_id == START_LISTENING_MESSAGE["messageId"]:
message_result = payload[message_type]
states = message_result["state"]
for state in states["devices"]:
self.serialno = state["serialNumber"]
self.video_thread = ClientAcceptThread(video_sock, run_event, "Video", self.ws, self.serialno)
self.audio_thread = ClientAcceptThread(audio_sock, run_event, "Audio", self.ws, self.serialno)
self.backchannel_thread = ClientAcceptThread(backchannel_sock, run_event, "BackChannel", self.ws, self.serialno)
self.audio_thread.start()
self.video_thread.start()
self.backchannel_thread.start()
if message_id == TALKBACK_RESULT_MESSAGE["messageId"] and "errorCode" in payload:
error_code = payload["errorCode"]
if error_code == "device_talkback_not_running":
msg = START_TALKBACK.copy()
msg["serialNumber"] = self.serialno
await self.ws.send_message(json.dumps(msg))
if message_type == "event":
message = payload[message_type]
event_type = message["event"]
sys.stdout.flush()
if message["event"] == "livestream audio data":
#print(f"on_audio - {payload}")
event_value = message[EVENT_CONFIGURATION[event_type]["value"]]
event_data_type = EVENT_CONFIGURATION[event_type]["type"]
if event_data_type == "event":
for queue in self.audio_thread.queues:
if queue.full():
print("Audio queue full.")
queue.get(False)
queue.put(event_value)
if message["event"] == "livestream video data":
#print(f"on_video - {payload}")
event_value = message[EVENT_CONFIGURATION[event_type]["value"]]
event_data_type = EVENT_CONFIGURATION[event_type]["type"]
if event_data_type == "event":
for queue in self.video_thread.queues:
if queue.full():
print("Video queue full.")
queue.get(False)
queue.put(event_value)
if message["event"] == "livestream error":
print("Livestream Error!")
if self.ws and len(self.video_thread.queues) > 0:
msg = START_P2P_LIVESTREAM_MESSAGE.copy()
msg["serialNumber"] = self.serialno
await self.ws.send_message(json.dumps(msg))
# Websocket connector
c = Connector(run_event)
async def init_websocket():
ws: EufySecurityWebSocket = EufySecurityWebSocket(
"172.18.0.2",
sys.argv[1],
aiohttp.ClientSession(),
c.on_open,
c.on_message,
c.on_close,
c.on_error,
)
c.setWs(ws)
try:
await ws.connect()
await ws.send_message(json.dumps(START_LISTENING_MESSAGE))
await ws.send_message(json.dumps(SET_API_SCHEMA))
await ws.send_message(json.dumps(DRIVER_CONNECT_MESSAGE))
while not run_event.is_set():
await asyncio.sleep(1000)
except Exception as ex:
print(ex)
print("init_websocket failed. Exiting.")
os._exit(-1)
loop = asyncio.get_event_loop()
loop.run_until_complete(init_websocket())