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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
#!/usr/bin/env python
"""
A module consisting of a zeromq worker receiving video stream
via router from clients. Able to send control messages to clients.
Needs libzmq and pyzmq with 'drafts' support.
"""
import os
import sys
import struct
import datetime
from threading import Thread, Event
import time
import signal
import logging
from queue import Queue
from collections import defaultdict
import tempfile
import json
import zmq
import cv2
import numpy
from helpers import (
CustomLoggingFormatter,
bytes_to_str,
TIME_FORMAT_STRING,
)
__author__ = "Franoosh Corporation"
# Various constants: # TODO: put them in a config
HOST = "127.0.0.1"
ZMQPORT = "9979"
# WSPORT = "8000"
WSPORT = "8008"
ZMQ_BACKEND_ADDR = f"tcp://{HOST}:{ZMQPORT}"
WEB_BACKEND_ADDR = f"tcp://{HOST}:{WSPORT}"
# LOGLEVEL = logging.DEBUG
LOGLEVEL = logging.INFO
# File paths:
LOGDIR = 'logs'
if not os.path.exists(LOGDIR):
try:
os.makedirs(LOGDIR)
except Exception as exc:
print(f"Could not create log directory: {exc}")
sys.exit()
BASENAME = f"{os.path.splitext(os.path.basename(__file__))[0]}"
LOGFILE = os.path.join(LOGDIR, f"{BASENAME}.log")
CWD = os.getcwd()
TMP_DIR = os.path.join(CWD, "tmp")
CLIENTS_JSON_FILE = os.path.join(CWD, 'clients.json')
# This should come from the config file:
VIDEO_DIR = os.path.join(CWD, "videos")
# Other:
CLIENTS_DICT = {}
stop_event = Event()
log_formatter = CustomLoggingFormatter()
handler = logging.FileHandler(LOGFILE, encoding='utf-8', mode='a')
handler.setFormatter(log_formatter)
logging.root.addHandler(handler)
logging.root.setLevel(LOGLEVEL)
logger = logging.getLogger(__name__)
logging.basicConfig(
filename=LOGFILE,
datefmt='%Y-%m-%d %I:%M:%S',
level=LOGLEVEL,
)
if not os.path.exists(TMP_DIR):
try:
os.makedirs(TMP_DIR)
except Exception as exc:
logger.error("Could not create temporary directory: %r", exc)
sys.exit()
class MonitorTask(Thread):
"""
Monitor the connection status of the zmq socket.
Parameters
----------
socket : zmq.Socket
The zmq socket to monitor.
Methods
-------
run()
Monitor connection on the initial socket.
stop()
Stop thread
"""
def __init__(self, socket) -> None:
"""
Docstring for __init__
Parameters
----------
socket : zmq.Socket
The zmq socket to monitor.
"""
super().__init__(daemon=True)
self.socket = socket
self.running = True
def run(self) -> None:
"""
Monitor connection on the initial socket.
This is heartbeat monitoring
Parameters
----------
None
Returns
-------
None
"""
monitor = self.socket.get_monitor_socket()
monitor.setsockopt(zmq.RCVTIMEO, 5000)
logger.debug("Monitor socket started.")
while self.running:
try:
event, _ = monitor.recv_multipart()
# Resolve the received event type:
event_type = int.from_bytes(event[:2], "little")
if event_type in (zmq.EVENT_CLOSED, zmq.EVENT_DISCONNECTED):
logger.warning("Monitor socket: closed | disconnected")
stop_event.set()
elif event_type == zmq.EVENT_CONNECT_DELAYED:
logger.debug("Monitor socket: event connect delayed")
elif event_type == zmq.EVENT_CONNECT_RETRIED:
logger.debug("Monitor socket: event connect retried")
elif event_type in (zmq.EVENT_CONNECTED, zmq.EVENT_HANDSHAKE_SUCCEEDED):
logger.debug("Monitor socket: client connected to router, handshake OK.")
stop_event.clear()
else:
logger.warning("Monitor socket: other event: %r", event_type)
except zmq.Again:
logger.debug("Timeout on monitoring socket.")
except Exception as exc: # W: Catching too general exception Exception
logger.error("Other exception on monitoring socket: %r", exc)
# monitor.close()
def stop(self) -> None:
"""
Stop thread
Parameters
----------
None
Returns
-------
None
"""
logger.info("Stopping monitor thread ...")
self.running = False
class ServerWorker(Thread):
"""
ServerWorker class for zmq video messaging.
Parameters
----------
identity : bytes
Worker identity
context : zmq.Context, optional
ZMQ context (default is None, which creates a new context)
Methods
-------
start_client(client_id, camera_id, filename)
Start a video thread for new client_id and camera_id.
stop_client(client_id, camera_id)
Stop video thread for a client_id and camera_id.
run()
Main loop of the worker.
"""
def __init__(self, identity, context=None) -> None:
"""
Docstring for __init__
Parameters
----------
identity : bytes
Worker identity
context : zmq.Context, optional
ZMQ context (default is None, which creates a new context)
"""
super().__init__(daemon=True)
self.context = context or zmq.Context.instance()
self.socket = self.context.socket(zmq.DEALER)
self.id = self.socket.identity = identity
self.monitor = MonitorTask(self.socket)
self.video_threads = defaultdict(lambda: defaultdict(dict))
self.poller = zmq.Poller()
self.web_sock = self.context.socket(zmq.DEALER)
self.connected = False
self.running = True
def start_client(self, client_id, camera_id, filename) -> None:
"""
Start a video thread for new client_id and camera_id.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
filename : str
Filename to write video to
Returns
-------
None
"""
# New client or new camera for existing client:
if client_id not in self.video_threads or not self.video_threads[client_id].get(camera_id):
q = Queue()
logger.debug("Starting new video thread for client %r, camera %r", client_id, camera_id)
video_worker = VideoWorker(client_id, camera_id, filename, q)
video_worker.start()
self.video_threads[client_id][camera_id] = video_worker
def stop_client(self, client_id, camera_id) -> None:
"""
Stop video thread for a client_id and camera_id.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
Returns
-------
None
"""
if client_id in self.video_threads and camera_id in self.video_threads[client_id]:
logger.debug("Stopping video thread for client %r, camera %r", client_id, camera_id)
self.video_threads[client_id][camera_id].stop() # Stop the thread
del self.video_threads[client_id][camera_id]
logger.info("Stopped video thread for client %r, camera %r", client_id, camera_id)
def handle_metadata(
self,
client_id: bytes,
camera_id: bytes,
directive: bytes,
timestamp: bytes
) -> None:
"""
Handle metadata messages.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
directive : bytes
Action to perform (e.g., 'start', 'stop')
timestamp : bytes
Timestamp of the action
Returns
-------
None
"""
logger.info(
"Received metadata directive: %r for client: %r, camera: %r, timestamp: %r",
directive, client_id, camera_id, timestamp
)
update_clients(client_id, camera_id)
if directive in (b'start', b'stop'):
camera_id_string = camera_id.decode('utf-8')
timestamp_string = datetime.datetime.fromtimestamp(
struct.unpack('d', timestamp)[0]
).strftime(TIME_FORMAT_STRING)
filename = f"{camera_id_string}-{timestamp_string}.mp4"
logger.debug("Directive: %r, Timestamp: %r, Filename: %r", directive, timestamp, filename)
if directive == b"start":
self.start_client(client_id, camera_id, filename)
elif directive == b"stop":
self.stop_client(client_id, camera_id)
else:
logger.error("Unknown directive: %r", directive)
# elif directive == b'rename':
# old_name = new_name = timestamp = None
# try:
# old_name, new_name, timestamp = data.split(b":")
# logger.info("Renamed video thread from %r to %r.", old_name, new_name)
# except ValueError:
# logger.error("Invalid rename data format: %r", data)
# if old_name and new_name and timestamp:
# # I think it's better to stop the old thread and start a new one,
# # rather than reuse the old one as it's less mucking about.
# self.stop_client(old_name, camera_id)
# new_name_string = new_name.decode('utf-8')
# camera_id_string = camera_id.decode('utf-8')
# timestamp_string = timestamp.decode('utf-8')
# self.start_client(
# new_name,
# camera_id,
# f"{new_name_string}_{camera_id_string}-{timestamp_string}.mp4"
# )
# to_remove= b':'.join([client_id, camera_id])
# try:
# self.web_sock.send_multipart([to_remove, b'', b'']) # Notify webserver of rename
# except Exception as exc:
# logger.error("Sending rename notification to websocket failed: %r", exc)
# elif directive in (b'new_camera', b'removed_camera'):
# logger.info("Received camera metadata update: %r for client %r, camera %r",
# directive,
# client_id,
# camera_id,
# )
# # Just forward the message to the webserver:
# try:
# self.web_sock.send_multipart([client_id, camera_id, directive, data], flags=zmq.NOBLOCK)
# except Exception as exc:
# logger.error("Sending camera metadata update to websocket failed: %r", exc)
else:
logger.error("Unknown directive: %r", directive)
return False
def handle_video_message(self, msg: list) -> None:
logger.debug("Received video message with data only.")
client_id, camera_id, *content = msg
if client_id in self.video_threads and camera_id in self.video_threads[client_id]:
self.video_threads[client_id][camera_id].queue.put(content[0])
# Send only [client_id, camera_id, jpeg_bytes] to the webserver:
# zmq subsciber can subscribe to a topic defined by the first
# part of the multipart message, so in order to allow for a
# per camera subscription, we need to join client_id and camera_id
topic = b':'.join([client_id, camera_id])
try:
self.web_sock.send_multipart([topic, content[0]], flags=zmq.NOBLOCK)
except Exception as exc:
logger.error("Sending message to websocket failed: %r", exc)
else:
logger.error("No video thread found for client %r, camera %r", client_id, camera_id)
logger.debug("Available video threads keys: %r", self.video_threads.keys())
logger.debug("Available video threads values: %r", self.video_threads.values())
def run(self) -> None:
"""
Main loop of the worker.
Full of wonders.
Parameters
----------
None
Returns
-------
None
"""
logger.debug("ServerWorker %r starting ...", self.id)
self.socket.connect(ZMQ_BACKEND_ADDR)
try:
self.web_sock.bind(WEB_BACKEND_ADDR)
except Exception as exc:
logger.error("Connection to zmq websocket failed: %r", exc)
self.poller.register(self.socket, zmq.POLLIN)
self.poller.register(self.web_sock, zmq.POLLIN)
self.monitor.start()
while self.running:
logger.debug("ServerWorker %r waiting for a message ...", self.id)
if not self.connected or stop_event.is_set():
self.socket.send_multipart([b"READY"]) # Router needs worker identity, hence this
time.sleep(0.1) # Wait a bit before trying to connect again
sockets = dict(self.poller.poll(10))
if self.socket in sockets:
self.connected = True
msg = self.socket.recv_multipart()
logger.debug("ServerWorker %r received message of length: %d.", self.id, len(msg))
filename = None
# At the moment we don't expect any other message than a
# start/stop message (of length 4) and a video message:
if len(msg) == 4: # This is a message with start/stop/rename directive and no video data.
self.handle_metadata(*msg)
elif len(msg) == 3: # This is a video message with data
self.handle_video_message(msg)
else:
logger.warning("Received a message of unexpected length from client. Message length: %d", len(msg))
else:
logger.debug("No message received, polling again ...")
time.sleep(0.1)
if self.web_sock in sockets:
frontend_msg = self.web_sock.recv_multipart()
logger.info("Received message from frontend: %r", frontend_msg)
self.socket.send_multipart(frontend_msg)
logger.info("Forwarded message to backend: %r", frontend_msg)
self.monitor.stop()
self.monitor.join()
for camera_thread in self.video_threads.values():
for thread in camera_thread.values():
thread.queue.put(None) # Sentinel to unblock queue.get()
thread.stop()
thread.join()
def send_control_message(self, client_id, camera_id, message) -> None:
"""
Send control message to a specific client and camera.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
message : bytes
Control message to send
Returns
-------
None
"""
if client_id in self.video_threads and camera_id in self.video_threads[client_id]:
# self.socket.send_multipart([client_id.encode("utf-8"), camera_id.encode("utf-8"), message])
self.socket.send_multipart([client_id, camera_id, message])
logger.info("Sent control message to client %r, camera %r: %r", client_id, camera_id, message)
else:
logger.error("No video thread found for client %r, camera %r", client_id, camera_id)
def stop(self) -> None:
"""
Stop the worker thread.
Parameters
----------
None
Returns
-------
None
"""
logger.info("ServerWorker %r exiting ...", self.id)
self.running = False
class VideoWorker(Thread):
"""
Class for video threads writing video stream to files.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
filename : str
Filename to write video to
queue : Queue
Queue to receive video frames
Methods
-------
run()
Main loop of the video worker thread.
stop()
Stop thread
"""
def __init__(self, client_id, camera_id, filename, queue) -> None:
"""
Docstring for __init__
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
filename : str
Filename to write video to
queue : Queue
Queue to receive video frames
"""
super().__init__(daemon=True)
self.context = zmq.Context()
self.context.setsockopt(zmq.LINGER, 0)
self.client_id = client_id
self.camera_id = camera_id
self.filename = filename
self.queue = queue
self.live = True
def stop(self) -> None:
"""
Docstring for stop
Parameters
----------
None
Returns
-------
None
"""
logger.info("VideoWorker %r exiting ...", self.camera_id)
self.live = False
def run(self) -> None:
"""
Main loop of the video worker thread.
Parameters
----------
None
Returns
-------
None
"""
# Create a VIDEO_DIR/client_id/camera_id/ directory if it doesn't exist
dirname = os.path.join(
VIDEO_DIR,
self.client_id.decode('utf-8'),
self.camera_id.decode('utf-8'),
)
filename = os.path.join(
dirname,
self.filename
)
try:
os.makedirs(dirname, exist_ok=True)
except PermissionError as exc:
logger.error("Could not create directory '%s': %r", dirname, exc)
return
if os.path.exists(filename):
logger.warning("File '%s' already exists, overwriting ...", filename)
fourcc = cv2.VideoWriter_fourcc(*'avc1')
out = cv2.VideoWriter(filename, fourcc, 30.0, (640, 480)) # Assuming 640x480 resolution
logger.info("This is the first run, binding websocket ...")
while self.live:
logger.debug("VideoWorker writing to file: %s", filename)
frame_bytes = self.queue.get()
if frame_bytes is None:
logger.debug("Received None, stopping video worker for camera: '%r'", self.camera_id)
break
frame = cv2.imdecode(numpy.frombuffer(frame_bytes, dtype=numpy.uint8), cv2.IMREAD_COLOR)
logger.debug("Processing ('writing to a file') frame for camera: '%r'", self.camera_id)
# Write frame to file:
out.write(frame)
# Release video writer
out.release()
logger.info("VideoWorker finished writing to file: %s", self.filename)
def signal_handler(sig, frame) -> None:
"""
Docstring for signal_handler
sig : Signal
Signal number
frame : Frame
Current stack frame
"""
worker.stop()
def update_clients(client_id, camera_id) -> None:
"""
Update client and camera dictionary and write to a shared file.
Parameters
----------
client_id : bytes
Client ID
camera_id : bytes
Camera ID
Returns
-------
None
"""
global CLIENTS_DICT
if client_id not in CLIENTS_DICT:
logger.debug("Client_id not in CLIENTS_DICT, adding an empty list for it.")
CLIENTS_DICT[client_id] = []
if camera_id not in CLIENTS_DICT[client_id]:
logger.debug("Camera_id not in CLIENTS_DICT[%s] list, adding", client_id)
CLIENTS_DICT[client_id].append(camera_id)
# Atomic write using tempfile. Works only when both files on the same filesystem
with tempfile.NamedTemporaryFile('w', dir=TMP_DIR, delete=False) as tf:
logger.debug("Dumping to file CLIENTS_DICT: %r", CLIENTS_DICT)
json.dump(bytes_to_str(CLIENTS_DICT), tf)
tempname = tf.name
os.replace(tempname, CLIENTS_JSON_FILE)
if __name__ == "__main__":
logger.info("Starting up ...")
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
worker = ServerWorker(b"worker-task")
worker.start()
worker.join()
try:
os.remove(CLIENTS_JSON_FILE)
except FileNotFoundError:
pass
|