-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer1.py
More file actions
94 lines (73 loc) · 2.18 KB
/
peer1.py
File metadata and controls
94 lines (73 loc) · 2.18 KB
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
import socket
import sounddevice as sd
import pickle
import threading
import queue
import sys
localCredentials = {
'IP': '', # your ip address
'PORT': 9000
}
remoteCredentials = {
'IP': sys.argv[1],
'PORT': int(sys.argv[2])
}
q_in = queue.Queue()
q_out = queue.Queue()
peer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
peer.bind((localCredentials['IP'], localCredentials['PORT']))
def reliable_recv(sock):
message = b''
while True:
try:
data, addr = sock.recvfrom(2048)
message += data
return pickle.loads(message), addr
except ValueError:
continue
def send_media(sock):
while True:
try:
data = q_in.get(timeout=1)
data = pickle.dumps(data)
except queue.Empty:
sys.exit()
sock.sendto(data, (remoteCredentials['IP'], remoteCredentials['PORT']))
def recv_media(sock):
while True:
data, addr = reliable_recv(sock)
q_out.put(data)
def input_callback(indata, frame, time, status):
if status:
print(status)
q_in.put(indata.copy())
def audio_call_input():
with sd.InputStream(blocksize=200, channels=2, dtype='float32', callback=input_callback):
while True:
sd.sleep(1) # unlimited time call
def output_callback(outdata, frame, time, status):
if status:
print(status)
try:
outdata[:] = q_out.get_nowait()
except queue.Empty:
print('[+] Call has end !')
raise sd.CallbackAbort
def audio_call_output():
with sd.OutputStream(blocksize=200, channels=2, dtype='float32', callback=output_callback):
while True:
sd.sleep(1) # unlimited call
def main():
thread1 = threading.Thread(target=send_media, args=(peer,))
thread2 = threading.Thread(target=audio_call_input)
thread3 = threading.Thread(target=recv_media, args=(peer,))
thread4 = threading.Thread(target=audio_call_output, daemon=True)
thread2.start()
thread1.start()
thread3.start()
while not (q_out.qsize() > 100):
print(q_out.qsize(), end='\r')
print("[+] Call has started !")
thread4.start()
if __name__ == "__main__":
main()