-
Notifications
You must be signed in to change notification settings - Fork 0
/
client1.py
122 lines (94 loc) · 2.88 KB
/
client1.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
import cv2, imutils, socket
import numpy as np
import time, os
import base64
import threading, wave, pyaudio,pickle,struct
BUFF_SIZE = 65536
BREAK = False
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print(host_ip)
port = 9688
message = b'Hello'
client_socket.sendto(message,(host_ip,port))
def video_stream():
cv2.namedWindow('RECEIVING VIDEO')
cv2.moveWindow('RECEIVING VIDEO', 10,360)
fps,st,frames_to_count,cnt = (0,0,20,0)
while True:
packet,_ = client_socket.recvfrom(BUFF_SIZE)
data = base64.b64decode(packet,' /')
npdata = np.fromstring(data,dtype=np.uint8)
frame = cv2.imdecode(npdata,1)
frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
cv2.imshow("RECEIVING VIDEO",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
client_socket.close()
os._exit(1)
break
if key == ord('r') :
print("rewind")
s.send("rewind".encode())
if key == ord('f') :
print("forward")
s.send("forward".encode())
if key == ord('p') :
print("play")
s.send("play".encode())
try:
fps = round(frames_to_count/(time.time()-st))
st=time.time()
cnt=0
except Exception:
pass
cnt+=1
client_socket.close()
cv2.destroyAllWindows()
def audio_stream():
print("audio_stream function called")
p = pyaudio.PyAudio()
CHUNK = 1024
stream = p.open(format=p.get_format_from_width(2),
channels=2,
rate=44100,
output=True,
frames_per_buffer=CHUNK)
print("lets see")
client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket_address = (host_ip,port-5)
print('server listening at',socket_address)
client_socket.connect(socket_address)
print("CLIENT CONNECTED TO",socket_address)
data = b""
payload_size = struct.calcsize("Q")
while True:
try:
while len(data) < payload_size:
packet = client_socket.recv(4*1024) # 4K
if not packet: break
data+=packet
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("Q",packed_msg_size)[0]
while len(data) < msg_size:
data += client_socket.recv(4*1024)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data)
stream.write(frame)
except:
break
client_socket.close()
print('Audio closed',BREAK)
os._exit(1)
def msgconnect():
s.connect((host_name,9595))
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=3) as executor:
executor.submit(audio_stream)
executor.submit(video_stream)
executor.submit(msgconnect)