-
Notifications
You must be signed in to change notification settings - Fork 1
/
f1.py
574 lines (509 loc) · 18.1 KB
/
f1.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
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
from flask import Flask, render_template, Response, request
# import os
# import random
import cv2
import socket as S
from socket import socket, AF_INET, SOCK_STREAM
from webcamVideoStream import WebcamVideoStream
import pyaudio
from threading import Thread
import numpy as np
import zlib
import struct
# HOST = input("Enter Server IP\n")
HOST = '172.16.84.70'
PORT_AUDIO = 10000
PORT1 = 4000
PORT2 = 5000
PORT3 = 6000
PORT4 = 7000
PORT_UNIV = 8000
BufferSize = 4096
CHUNK=1024
lnF = 200*200*3
FORMAT=pyaudio.paInt16
CHANNELS=2
RATE=44100
ports = {'10000':True,'8000':True,'4000':False,'5000':False,'6000':False,'7000':False}
USERS = {}
imageStream = np.array([])
quit1=False
startaudio = 1
startvideo = 1
f1,f2,f3,f4,f5,f6 = 0,0,0,0,0,0
filter_list = {'f1':False,'f2':False,'f3':False,'f4':False,'f5':False,'f6':False}
app = Flask(__name__)
@app.route('/')
def home():
#os.system("python3 script.py")
ip_addr = get_ip_address()
return render_template('home.html',ip_addr=ip_addr)
@app.route('/hosted')
def hosting():
global HOST
ip_addr = get_ip_address()
HOST = ip_addr
ServerThread = Thread(target = ServerMedia , args= ()) #####################CHAnge ServerMEdia File##########################
ServerThread.start()
return render_template('index.html')
@app.route('/connectfeed',methods=['POST'])
def connectfeed():
global HOST
ip_addr = request.form['inputval']
HOST = ip_addr
return render_template("index.html")
def RecieveFrame(clientVideoSocket):
IP = get_ip_address()
global quit1
global imageStream
while True:
lengthbuf = recvallVideo(clientVideoSocket, 4)
# print('Lengthbuf - ',lengthbuf)
length, = struct.unpack('!I', lengthbuf)
# print('Length - ',length)
databytes = recvallVideo(clientVideoSocket, length)
databytes1 = databytes
# print('Status - ',databytes[:6])
STATUS = databytes[:6].decode()
if STATUS == "ACTIVE" or STATUS == "INTIVE":
lenip, = struct.unpack('!I',databytes[6:10])
ipUser = databytes[10:10+int(lenip)]
databytes = databytes[(len(STATUS)+4+len(ipUser)):]
img = zlib.decompress(databytes)
if len(databytes1) == length:
# print("Recieving Media..")
# print("Image Frame Size:- {}".format(len(img)))
img = np.array(list(img))
img = np.array(img, dtype = np.uint8).reshape(200, 200, 3)
img = cv2.resize(img,(640,480))
if ipUser not in USERS:
USERS[ipUser] = img
else:
if STATUS == "ACTIVE":
USERS[ipUser] = img
elif STATUS == "INTIVE":
del USERS[ipUser]
else:
print("Data CORRUPTED")
else:
print('Status Error!:- ',STATUS)
continue #Hope It Works
@app.route('/video_feed')
def video_feed():
clientVideoSocketUniv = socket(family=AF_INET, type=SOCK_STREAM)
clientVideoSocketUniv.connect((HOST, PORT_UNIV))
wvs = WebcamVideoStream(0).start()
clientAudioSocket = socket(family=AF_INET, type=SOCK_STREAM)
clientAudioSocket.connect((HOST, PORT_AUDIO))
PORTNUMBER = clientVideoSocketUniv.recv(4).decode()
clientVideoSocket1 = socket(family=AF_INET, type=SOCK_STREAM)
clientVideoSocket1.connect((HOST, int(PORTNUMBER)))
ports[PORTNUMBER] = True
SendFrameThread = Thread(target=SendFrame , args=(clientVideoSocket1 , wvs)).start()
for portnos in sorted(ports.keys()):
if ports[portnos] == False:
clientVideoSocket2 = socket(family=AF_INET, type=SOCK_STREAM)
clientVideoSocket2.connect((HOST, int(portnos)))
ports[portnos] = True
RecieveFrameThread1 = Thread(target=RecieveFrame , args = (clientVideoSocket2,)).start()
print(portnos,' - Connected !')
break
for portnos in sorted(ports.keys()):
if ports[portnos] == False:
clientVideoSocket3 = socket(family=AF_INET, type=SOCK_STREAM)
clientVideoSocket3.connect((HOST, int(portnos)))
ports[portnos] = True
RecieveFrameThread2 = Thread(target=RecieveFrame , args = (clientVideoSocket3,)).start()
print(portnos,' - Connected !')
break
for portnos in sorted(ports.keys()):
if ports[portnos] == False:
clientVideoSocket4 = socket(family=AF_INET, type=SOCK_STREAM)
clientVideoSocket4.connect((HOST, int(portnos)))
ports[portnos] = True
RecieveFrameThread3 = Thread(target=RecieveFrame , args = (clientVideoSocket4,)).start()
print(portnos,' - Connected !')
break
audio=pyaudio.PyAudio()
stream=audio.open(format=FORMAT,channels=CHANNELS, rate=RATE, input=True, output = True,frames_per_buffer=CHUNK)
SendAudioThread = Thread(target=SendAudio , args=(clientAudioSocket , stream ,))
RecieveAudioThread = Thread(target=RecieveAudio , args=(clientAudioSocket , stream ,))
# DisplayThread = Thread(target=display) ############# TRY ######################
# RecieveAudioThread.start()
# DisplayThread.start()
# SendAudioThread.start()
# SendAudioThread.join()
return Response(gen(wvs),
mimetype='multipart/x-mixed-replace; boundary=frame')
def gen(wvs):
global quit1
global USERS
while quit1!=True:
US = USERS.copy()
if len(US) == 1:
for user in US:
background = cv2.resize(US[user],(640,480))
overlay = wvs.read()
overlay = cv2.resize(overlay, (200, 150))
s_img = overlay
finalImage = cv2.resize(background,(1080,600))
x_offset=880
y_offset=450
finalImage[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1]] = s_img
ret, frame = cv2.imencode('.jpg', finalImage)
finalImage = frame.tobytes()
elif len(US) == 2:
frames = []
for ip in US:
frames.append(USERS[ip])
l_img1 = cv2.resize(frames[0], (640, 480))
l_img2 = cv2.resize(frames[1], (640, 480))
overlay = wvs.read()
overlay = cv2.resize(overlay, (200, 150))
ret, frame = cv2.imencode('.jpg', overlay)
overlay = frame.tobytes()
s_img = overlay
l_img = np.hstack((l_img1, l_img2))
finalImage = cv2.resize(l_img, (1080, 600))
x_offset = 880
y_offset = 450
finalImage[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1]] = s_img
ret, frame = cv2.imencode('.jpg', finalImage)
finalImage = frame.tobytes()
elif len(US) == 3:
frames = []
for ip in US:
frames.append(USERS[ip])
l_img1 = cv2.resize(frames[0], (640, 480))
l_img2 = cv2.resize(frames[1], (640, 480))
l_img3 = cv2.resize(frames[2], (640, 480))
overlay = wvs.read()
overlay = cv2.resize(overlay, (640, 480))
ret, frame = cv2.imencode('.jpg', overlay)
overlay = frame.tobytes()
s_img = overlay
l_img4 = np.hstack((l_img1, l_img2))
l_img5 = np.hstack((l_img3, s_img))
finalImage = np.vstack((l_img4, l_img5))
finalImage = cv2.resize(finalImage, (1080, 600))
ret, frame = cv2.imencode('.jpg', finalImage)
finalImage = frame.tobytes()
elif len(US) == 0:
finalImage = wvs.read()
finalImage = cv2.resize(finalImage, (1080, 600))
ret, frame = cv2.imencode('.jpg', finalImage)
finalImage = frame.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + finalImage + b'\r\n\r\n')
def SendAudio(clientAudioSocket , stream):
while quit1!=True:
if startaudio == 1:
data = stream.read(CHUNK)
# dataChunk = array('h', data)
# vol = max(dataChunk)
# if(vol > 500):
# # print("Recording Sound...")
# else:
# # print("Silence..")
# pass
clientAudioSocket.sendall(data) #Same
def RecieveAudio(clientAudioSocket , stream):
while quit1!=True:
data = recvallAudio(clientAudioSocket, BufferSize)
stream.write(data) #Same
def recvallAudio(clientAudioSocket,size):
databytes = b''
while len(databytes) != size:
to_read = size - len(databytes)
if to_read > (4 * CHUNK):
databytes += clientAudioSocket.recv(4 * CHUNK)
else:
databytes += clientAudioSocket.recv(to_read)
return databytes #Same
@app.route('/audio')
def listen():
global startaudio
if startaudio == 0:
startaudio = 1
else:
startaudio = 0
print("Audio success")
return "jbsdj"
def SendFrame(clientVideoSocket1 , wvs):
IP = get_ip_address()
global quit1
while True:
if startvideo == 1:
frame = wvs.read()
frame = cv2.resize(frame, (200,200))
jpg_as_text = zlib.compress(frame, 9)
lenip = struct.pack('!I',len(IP))
if quit1 == False:
databytes = b"ACTIVE" + lenip + IP.encode() + jpg_as_text
else:
databytes = b"INTIVE" + lenip + IP.encode() + jpg_as_text
print('Connection Terminated Mofo !!!')
length = struct.pack('!I', len(databytes))
bytesToBeSend = b''
clientVideoSocket1.sendall(length)
while len(databytes) > 0:
if (1000 * CHUNK) <= len(databytes):
bytesToBeSend = databytes[:(1000 * CHUNK)]
databytes = databytes[(1000 * CHUNK):]
clientVideoSocket1.sendall(bytesToBeSend)
else:
bytesToBeSend = databytes
clientVideoSocket1.sendall(bytesToBeSend)
databytes = b''
elif quit1 == True:
break #VErified
def recvallVideo(clientVideoSocket, size):
databytes = b''
while len(databytes) != size:
to_read = size - len(databytes)
if to_read > (5000 * CHUNK):
databytes += clientVideoSocket.recv(5000 * CHUNK)
else:
databytes += clientVideoSocket.recv(to_read)
return databytes
def get_ip_address():
s = S.socket(S.AF_INET, S.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
return ip
@app.route('/video')
def video():
global startvideo
if startvideo == 1:
startvideo = 0
else:
startvideo = 1
print ("Sucess Video")
return "jskjdhsjk"
@app.route('/quit')
def quit112():
global quit1
quit1 = True
print ("quit1 Sucess" , quit1)
return render_template("home.html")
@app.route('/f1')
def filter_f1():
global f1
if f1==0:
f1 = 1
#func
else:
f1 = 0
return "Success"
@app.route('/f2')
def filter_f2():
global f2
if f2==0:
f2 = 1
#func
else:
f2 = 0
return "Success"
@app.route('/f3')
def filter_f3():
global f3
if f3==0:
f3 = 1
#func
else:
f3 = 0
return "Success"
@app.route('/f4')
def filter_f4():
global f4
if f4==0:
f4 = 1
#func
else:
f4 = 0
return "Success"
@app.route('/f5')
def filter_f5():
global f5
if f5==0:
f5 = 1
#func
else:
f5 = 0
return "Success"
@app.route('/f6')
def filter_f6():
global f6
if f6==0:
f6 = 1
#func
else:
f6 = 0
return "Success"
def ServerMedia():
# HOST = input("Enter Host IP\n")
HOST = '172.16.84.70'
PORT_AUDIO = 10000
PORT1 = 4000
PORT2 = 5000
PORT3 = 6000
PORT4 = 7000
PORT_UNIV = 8000
lnF = 640*480*3
CHUNK = 1024
BufferSize = 4096
quitUsers = {}
addressesAudio = {}
addresses = {}
USERS = {'4000':[],'5000':[],'6000':[],'7000':[]}
ports = {'10000':True,'8000':True,'4000':False,'5000':False,'6000':False,'7000':False}
def accept(port, server1,server2,server3,server4):
client1,addr1 = server1.accept()
client2,addr2 = server2.accept()
client3,addr3 = server3.accept()
client4,addr4 = server4.accept()
i = 0
if port == '4000':
PORTS = ['4000', '5000', '6000', '7000']
print ("1st User detected")
elif port == '5000':
PORTS = ['5000', '4000', '6000', '7000']
print ("2nd User detected")
elif port == '6000':
PORTS = ['6000', '4000', '5000', '7000']
print ("3rd User detected")
elif port == '7000':
PORTS = ['7000', '4000', '5000', '6000']
print ("4th User detected")
clients = [client1, client2, client3, client4]
for PORT in PORTS:
if PORT != port:
USERS[PORT].append(clients[i])
i += 1
return client1
def ConnectionsUniv():
while True:
client, addr = serverUniv.accept()
addresses[client] = addr
quitUsers[addr[0]] = False
print("{} is connected!!".format(addr))
for port in ports:
if ports[port] == False:
client.sendall(port.encode())
ports[port] = True
if port == '4000':
client1 = accept(port, server1,server2,server3,server4)
Thread(target=ClientConnectionVideo, args=(port, client, client1, )).start()
if port == '5000':
client1 = accept(port, server2,server1,server3,server4)
Thread(target=ClientConnectionVideo, args=(port, client, client1, )).start()
if port == '6000':
client1 = accept(port, server3,server1,server2,server4)
Thread(target=ClientConnectionVideo, args=(port, client, client1, )).start()
if port == '7000':
client1 = accept(port, server4,server1,server2,server3)
Thread(target=ClientConnectionVideo, args=(port, client, client1, )).start()
break
def ConnectionsSound():
while True:
clientAudio, addr = serverAudio.accept()
print("{} is connected!!".format(addr))
addressesAudio[clientAudio] = addr[0]
Thread(target=ClientConnectionSound, args=(clientAudio, )).start()
def ClientConnectionVideo(port, client, client1):
while True:
if len(addresses)>1:
databytes = b''
lengthbuf = recvall(port, client1, 4)
databytes += lengthbuf
length, = struct.unpack('!I', lengthbuf)
STATUS = recvall(port, client1, 6)
databytes += STATUS
STATUS = STATUS.decode()
databytes += recvall(port, client1, length-6)
broadcastVideo(port, databytes)
if STATUS == "INTIVE":
del addresses[client]
quitUsers[addresses[client][0]] = True
ports[port] = False
break
def ClientConnectionSound(clientAudio):
while True:
if quitUsers[addressesAudio[clientAudio]] == False:
data = clientAudio.recv(BufferSize)
broadcastSound(clientAudio, data)
else:
quitUsers[addressesAudio[clientAudio]] = False
del addressesAudio[clientAudio]
break
def recvall(port, client1, BufferSize):
databytes = b''
i = 0
while i != BufferSize:
to_read = BufferSize - i
if to_read > (1000 * CHUNK):
databytes += client1.recv(1000 * CHUNK)
i = len(databytes)
else:
databytes += client1.recv(to_read)
i = len(databytes)
return databytes
def broadcastVideoFrame(client, data_to_be_sent):
client.sendall(data_to_be_sent)
def broadcastVideo(port, data_to_be_sent):
threads = []
for client in USERS[port]:
frameThread = Thread(target=broadcastVideoFrame, args=(client, data_to_be_sent, ))
threads.append(frameThread)
frameThread.start()
for thread in threads:
thread.join()
def broadcastSound(clientSocket, data_to_be_sent):
for clientAudio in addressesAudio:
if clientAudio != clientSocket:
clientAudio.sendall(data_to_be_sent)
serverAudio = socket(family=AF_INET, type=SOCK_STREAM)
server1 = socket(family=AF_INET, type=SOCK_STREAM)
server2 = socket(family=AF_INET, type=SOCK_STREAM)
server3 = socket(family=AF_INET, type=SOCK_STREAM)
server4 = socket(family=AF_INET, type=SOCK_STREAM)
serverUniv = socket(family=AF_INET, type=SOCK_STREAM)
try:
serverAudio.bind((HOST, PORT_AUDIO))
except OSError:
print("Server Audio is Busy")
try:
serverUniv.bind((HOST, PORT_UNIV))
except OSError:
print("Server Univ Busy")
try:
server1.bind((HOST, PORT1))
except OSError:
print("Server1 Busy")
try:
server2.bind((HOST, PORT2))
except OSError:
print("Server2 Busy")
try:
server3.bind((HOST, PORT3))
except OSError:
print("Server3 Busy")
try:
server4.bind((HOST, PORT4))
except OSError:
print("Server4 Busy")
serverAudio.listen(4)
AcceptThreadAudio = Thread(target=ConnectionsSound)
AcceptThreadAudio.start()
serverUniv.listen(4)
server1.listen(4)
server2.listen(4)
server3.listen(4)
server4.listen(4)
print("Waiting for connection..")
AcceptThreadUniv = Thread(target=ConnectionsUniv)
AcceptThreadUniv.start()
AcceptThreadUniv.join()
serverUniv.close()
if __name__ == "__main__":
app.run(debug=True)