-
Notifications
You must be signed in to change notification settings - Fork 1
/
vidstream.py
480 lines (434 loc) · 14.5 KB
/
vidstream.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
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
import cv2
import pyautogui
import numpy as np
import socket
import pickle
import struct
import threading
class StreamingServer:
"""
Class for the streaming server.
Attributes
----------
Private:
__host : str
host address of the listening server
__port : int
port on which the server is listening
__slots : int
amount of maximum avaialable slots (not ready yet)
__used_slots : int
amount of used slots (not ready yet)
__quit_key : chr
key that has to be pressed to close connection
__running : bool
inicates if the server is already running or not
__block : Lock
a basic lock used for the synchronization of threads
__server_socket : socket
the main server socket
Methods
-------
Private:
__init_socket : method that binds the server socket to the host and port
__server_listening: method that listens for new connections
__client_connection : main method for processing the client streams
Public:
start_server : starts the server in a new thread
stop_server : stops the server and closes all connections
"""
# TODO: Implement slots functionality
def __init__(self, host, port, slots=8, quit_key='q'):
"""
Creates a new instance of StreamingServer
Parameters
----------
host : str
host address of the listening server
port : int
port on which the server is listening
slots : int
amount of avaialable slots (not ready yet) (default = 8)
quit_key : chr
key that has to be pressed to close connection (default = 'q')
"""
self.__host = host
self.__port = port
self.__slots = slots
self.__used_slots = 0
self.__running = False
self.__quit_key = quit_key
self.__block = threading.Lock()
self.__server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__init_socket()
def __init_socket(self):
"""
Binds the server socket to the given host and port
"""
self.__server_socket.bind((self.__host, self.__port))
def start_server(self):
"""
Starts the server if it is not running already.
"""
if self.__running:
print("Server is already running")
else:
self.__running = True
server_thread = threading.Thread(target=self.__server_listening)
server_thread.start()
def __server_listening(self):
"""
Listens for new connections.
"""
self.__server_socket.listen()
while self.__running:
self.__block.acquire()
connection, address = self.__server_socket.accept()
if self.__used_slots >= self.__slots:
print("Connection refused! No free slots!")
connection.close()
self.__block.release()
continue
else:
self.__used_slots += 1
self.__block.release()
thread = threading.Thread(target=self.__client_connection, args=(connection, address,))
thread.start()
def stop_server(self):
"""
Stops the server and closes all connections
"""
if self.__running:
self.__running = False
closing_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
closing_connection.connect((self.__host, self.__port))
closing_connection.close()
self.__block.acquire()
self.__server_socket.close()
self.__block.release()
else:
print("Server not running!")
def __client_connection(self, connection, address):
"""
Handles the individual client connections and processes their stream data.
"""
payload_size = struct.calcsize('>L')
data = b""
while self.__running:
break_loop = False
while len(data) < payload_size:
received = connection.recv(4096)
if received == b'':
connection.close()
self.__used_slots -= 1
break_loop = True
break
data += received
if break_loop:
break
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(">L", packed_msg_size)[0]
while len(data) < msg_size:
data += connection.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data, fix_imports=True, encoding="bytes")
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
cv2.imshow(str(address), frame)
if cv2.waitKey(1) == ord(self.__quit_key):
connection.close()
self.__used_slots -= 1
break
class StreamingClient:
"""
Abstract class for the generic streaming client.
Attributes
----------
Private:
__host : str
host address to connect to
__port : int
port to connect to
__running : bool
inicates if the client is already streaming or not
__encoding_parameters : list
a list of encoding parameters for OpenCV
__client_socket : socket
the main client socket
Methods
-------
Private:
__client_streaming : main method for streaming the client data
Protected:
_configure : sets basic configurations (overridden by child classes)
_get_frame : returns the frame to be sent to the server (overridden by child classes)
_cleanup : cleans up all the resources and closes everything
Public:
start_stream : starts the client stream in a new thread
"""
def __init__(self, host, port):
"""
Creates a new instance of StreamingClient.
Parameters
----------
host : str
host address to connect to
port : int
port to connect to
"""
self.__host = host
self.__port = port
self._configure()
self.__running = False
self.__client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def _configure(self):
"""
Basic configuration function.
"""
self.__encoding_parameters = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
def _get_frame(self):
"""
Basic function for getting the next frame.
Returns
-------
frame : the next frame to be processed (default = None)
"""
return None
def _cleanup(self):
"""
Cleans up resources and closes everything.
"""
cv2.destroyAllWindows()
def __client_streaming(self):
"""
Main method for streaming the client data.
"""
self.__client_socket.connect((self.__host, self.__port))
while self.__running:
frame = self._get_frame()
result, frame = cv2.imencode('.jpg', frame, self.__encoding_parameters)
data = pickle.dumps(frame, 0)
size = len(data)
try:
self.__client_socket.sendall(struct.pack('>L', size) + data)
except ConnectionResetError:
self.__running = False
except ConnectionAbortedError:
self.__running = False
except BrokenPipeError:
self.__running = False
self._cleanup()
def start_stream(self):
"""
Starts client stream if it is not already running.
"""
if self.__running:
print("Client is already streaming!")
else:
self.__running = True
client_thread = threading.Thread(target=self.__client_streaming)
client_thread.start()
def stop_stream(self):
"""
Stops client stream if running
"""
if self.__running:
self.__running = False
else:
print("Client not streaming!")
class CameraClient(StreamingClient):
"""
Class for the camera streaming client.
Attributes
----------
Private:
__host : str
host address to connect to
__port : int
port to connect to
__running : bool
inicates if the client is already streaming or not
__encoding_parameters : list
a list of encoding parameters for OpenCV
__client_socket : socket
the main client socket
__camera : VideoCapture
the camera object
__x_res : int
the x resolution
__y_res : int
the y resolution
Methods
-------
Protected:
_configure : sets basic configurations
_get_frame : returns the camera frame to be sent to the server
_cleanup : cleans up all the resources and closes everything
Public:
start_stream : starts the camera stream in a new thread
"""
def __init__(self, host, port, x_res=1024, y_res=576):
"""
Creates a new instance of CameraClient.
Parameters
----------
host : str
host address to connect to
port : int
port to connect to
x_res : int
the x resolution
y_res : int
the y resolution
"""
self.__x_res = x_res
self.__y_res = y_res
self.__camera = cv2.VideoCapture(0)
super(CameraClient, self).__init__(host, port)
def _configure(self):
"""
Sets the camera resultion and the encoding parameters.
"""
self.__camera.set(3, self.__x_res)
self.__camera.set(4, self.__y_res)
super(CameraClient, self)._configure()
def _get_frame(self):
"""
Gets the next camera frame.
Returns
-------
frame : the next camera frame to be processed
"""
ret, frame = self.__camera.read()
return frame
def _cleanup(self):
"""
Cleans up resources and closes everything.
"""
self.__camera.release()
cv2.destroyAllWindows()
class VideoClient(StreamingClient):
"""
Class for the video streaming client.
Attributes
----------
Private:
__host : str
host address to connect to
__port : int
port to connect to
__running : bool
inicates if the client is already streaming or not
__encoding_parameters : list
a list of encoding parameters for OpenCV
__client_socket : socket
the main client socket
__video : VideoCapture
the video object
__loop : bool
boolean that decides whether the video shall loop or not
Methods
-------
Protected:
_configure : sets basic configurations
_get_frame : returns the video frame to be sent to the server
_cleanup : cleans up all the resources and closes everything
Public:
start_stream : starts the video stream in a new thread
"""
def __init__(self, host, port, video, loop=True):
"""
Creates a new instance of VideoClient.
Parameters
----------
host : str
host address to connect to
port : int
port to connect to
video : str
path to the video
loop : bool
indicates whether the video shall loop or not
"""
self.__video = cv2.VideoCapture(video)
self.__loop = loop
super(VideoClient, self).__init__(host, port)
def _configure(self):
"""
Set video resolution and encoding parameters.
"""
self.__video.set(3, 1024)
self.__video.set(4, 576)
super(VideoClient, self)._configure()
def _get_frame(self):
"""
Gets the next video frame.
Returns
-------
frame : the next video frame to be processed
"""
ret, frame = self.__video.read()
return frame
def _cleanup(self):
"""
Cleans up resources and closes everything.
"""
self.__video.release()
cv2.destroyAllWindows()
class ScreenShareClient(StreamingClient):
"""
Class for the screen share streaming client.
Attributes
----------
Private:
__host : str
host address to connect to
__port : int
port to connect to
__running : bool
inicates if the client is already streaming or not
__encoding_parameters : list
a list of encoding parameters for OpenCV
__client_socket : socket
the main client socket
__x_res : int
the x resolution
__y_res : int
the y resolution
Methods
-------
Protected:
_get_frame : returns the screenshot frame to be sent to the server
Public:
start_stream : starts the screen sharing stream in a new thread
"""
def __init__(self, host, port, x_res=1024, y_res=576):
"""
Creates a new instance of ScreenShareClient.
Parameters
----------
host : str
host address to connect to
port : int
port to connect to
x_res : int
the x resolution
y_res : int
the y resolution
"""
self.__x_res = x_res
self.__y_res = y_res
super(ScreenShareClient, self).__init__(host, port)
def _get_frame(self):
"""
Gets the next screenshot.
Returns
-------
frame : the next screenshot frame to be processed
"""
screen = pyautogui.screenshot()
frame = np.array(screen)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (self.__x_res, self.__y_res), interpolation=cv2.INTER_AREA)
return frame