Streaming voice between two endpoint - python

everyone, i'm using a python code to Stream voice between to endpoints.
the code doesn't give me an error i just stuck at some point i don't now why
what im trying to achieve is i want to speak from one endpoint and hear my voice at the other end point
i don't want to use TCP i want to use UDP so i can avoid the latency
the receiver cod
import socket
import pyaudio
import select
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 4096
audio = pyaudio.PyAudio()
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.connect(("172.20.10.6", 5555))
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=CHUNK)
#while True :
#print("Enter a UDP M\n")
#MESSAGE = input("")
#sock.sendto(MESSAGE.encode('utf-8'), (UDP_IP, UDP_PORT))
try:
while True:
data = sock.recvfrom(CHUNK)
stream.write(data)
except KeyboardInterrupt:
pass
print('Shutting down')
sock.close()
stream.close()
audio.terminate()
the sender
import pyaudio
import socket
import select
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 4096
audio = pyaudio.PyAudio()
UDP_IP = "0.0.0.0"
UDP_PORT = 5555
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM , socket.IPPROTO_UDP) # UDP
sock.bind((UDP_IP, UDP_PORT))
def callback(in_data, frame_count, time_info, status):
for s in read_list[1:]:
print (s)
s.send(in_data)
return (None, pyaudio.paContinue)
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, stream_callback=callback)
# stream.start_stream()
read_list = [sock]
print ("recording...")
try:
while True:
print ("1")
readable, writable, errored = select.select(read_list, [], [])
print ("2")
for s in readable:
if s is sock:
(clientsocket, address) = sock.accept()
read_list.append(clientsocket)
print ("Connection from", address)
else:
data = sock.recvfrom(1024)
if not data:
read_list.remove(s)
except KeyboardInterrupt:
pass
print ("finished recording")
serversocket.close()
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
as you can see the code stuck at where i print the number 2 it doesn't print it
the below code is to a UDP connection that only Send a String
import socket
UDP_IP = "172.20.10.6"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print ("UDP target IP:", UDP_IP)
print ("UDP target port:", UDP_PORT)
print ("message:", MESSAGE)
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
while True :
print("Enter a UDP M\n")
MESSAGE = input("")
sock.sendto(MESSAGE.encode('utf-8'), (UDP_IP, UDP_PORT))
import socket
UDP_IP = "172.20.10.6"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print ("received message:", data)

Related

Voice call app crashes after joining Voice Call

I built a voice call app and for some reason when I join the vc it just crashes the app. No errors, nothing. It appears that is it not connecting to the server even tho the connection details are correct but I think there is something else going on. Pls Help!
Here is the Server Code:
def on_server_stop():
log(consoleLog="Stopping Vc Server")
sys.exit()
host = '0.0.0.0'
port = 5000
server = socket.socket()
server.bind((host, port))
server.listen()
log(consoleLog='Voice call server started and online!')
clientsInVC = []
def handleVC(fromConnection):
log(consoleLog="User conected to VC server.")
while True:
try:
data = fromConnection.recv(4096)
for client in clientsInVC:
if client != fromConnection:
client.send(data)
except:
client.close()
clientsInVC.remove(conn)
while True:
conn, addr = server.accept()
clientsInVC.append(conn)
thread = threading.Thread(target=handleVC, args=(conn, ))
thread.start()
Here is the Client Code:
import pyaudio
import socket
import threading
def JOINVC():
print("started vc")
vcclient = socket.socket()
host = "127.0.0.1"
port = 5000
vcclient.connect((host, port))
p = pyaudio.PyAudio()
Format = pyaudio.paInt16
Chunks = 1024 * 4
Channels = 1
Rate = 44100
inputStream = p.open(format=Format, channels=Channels, rate=Rate, input=True, frames_per_buffer= Chunks)
outputStream = p.open(format=Format, channels=Channels, rate=Rate, output=True, frames_per_buffer= Chunks)
def sendData():
while True:
try:
data = inputStream
vcclient.send(data)
except:
break
inputStream.close()
def recive():
while True:
try:
data = inputStream.read(Chunks)
outputStream.write(data)
except:
break
outputStream.close()
sendThread = threading.Thread(target = sendData)
sendThread.start()
reciveThread = threading.Thread(target= recive)
reciveThread.start()

Read Brodcast (ff02::1) msg python socket

i have this code to read a msg from a client.
import socket
# UDP_IP = 'fd53:7cb8:383:2::10'
UDP_IP = '::'
UDP_PORT = 13400
# create socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# bind the socket
server_add = (UDP_IP, UDP_PORT)
print('starting up on {} on port {}'.format(*server_add))
sock.bind(server_add)
while True:
print('waiting msg')
data, addr = sock.recvfrom(1024)
print("received message:", data)
print("addr is", addr)
sock.close()
break
the client IP is : 'fd53:7cb8:383:2::c9'
the Server IP is : 'fd53:7cb8:383:2::10'
i can saw the msg on wireshark
No.
Time
Delta
Source
Destination
Protocol
Length
70
6.516768
3.631930
fd53:7cb8:383:2::c9
ff02::1
DoIP
103
but the sock.recvfrom(1024) is not getting anything

my code is not throwing any packets in the console window

I have a correct code. Everything seems okay but it is not capturing any data packets:
UDP_PORT = 0
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM ) # UDP
sock.bind(('', 0))
print("waiting on port:", UDP_PORT)
while True:
data, addr = sock.recvfrom(65565) # buffer size is 1024 bytes
print ("received message:", data)

Python Networking Timestamp (SO_TIMESTAMP, SO_TIMESTAMPNS, SO_TIMESTAMPING)

Linux Kernel offers a few ways to get timestamps for received (SO_TIMESTAMP, SO_TIMESTAMPNS, SO_TIMESTAMPING) or sent (SO_TIMESTAMPING) packets.
Kernel Doc: https://www.kernel.org/doc/Documentation/networking/timestamping.txt
Is there a way I can use that with Python? I don't see any SO_TIMESTAMP constant inside the Python sources. Tried 3.6.2 and GitHub master branch.
Right now, I can only use SIOCGSTAMP that gives me the timestamp of the last received packet and nothing seems available for sent packet timestamp.
Finally, I have been able to get the SO_TIMESTAMPNS value like this:
SO_TIMESTAMPNS = 35
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1)
raw_data, ancdata, flags, address = s.recvmsg(65535, 1024)
ancdata[0][2] is the hardware timestamp as a timespec(ulong, ulong).
Does work on Linux but not on Mac OS X. Not tested on Windows.
complete code, send and receive using python3
import struct
import time
import select
import socket
import sys
if(len(sys.argv)!=2):
print("usage: ",sys.argv[0]," <send|receive>")
sys.exit()
print(sys.argv[1]);
if(sys.argv[1]=='send'):
MESSAGE = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.connect(('localhost', 10000))
s.send(MESSAGE)
time.sleep(0.0001)
#time.sleep(5)
s.send(MESSAGE)
s.close()
else:
SO_TIMESTAMPNS = 35
#s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1)
server.bind(('localhost', 10000))
server.listen(5)
inputs = [ server ]
message_queues = {}
outputs = []
while inputs:
print('\nwaiting for the next event')
readable, writable, exceptional = select.select(inputs, outputs, inputs)
for s in readable:
if s is server:
connection, client_address = s.accept()
print('new connection from', client_address)
connection.setblocking(0)
inputs.append(connection)
else:
raw_data, ancdata, flags, address = s.recvmsg(65535, 1024)
print('received ', raw_data, '-',ancdata,'-',flags,'-',address)
if(len(ancdata)>0):
#print(len(ancdata),len(ancdata[0]),ancdata[0][0],ancdata[0][1],ancdata[0][2])
#print('ancdata[0][2]:',type(ancdata[0][2])," - ",ancdata[0][2], " - ",len(ancdata[0][2]));
for i in ancdata:
print('ancdata: (cmsg_level, cmsg_type, cmsg_data)=(',i[0],",",i[1],", (",len(i[2]),") ",i[2],")");
if(i[0]!=socket.SOL_SOCKET or i[1]!=SO_TIMESTAMPNS):
continue
tmp=(struct.unpack("iiii",i[2]))
timestamp = tmp[0] + tmp[2]*1e-10
print("SCM_TIMESTAMPNS,", tmp, ", timestamp=",timestamp)
if(not raw_data):
print('closing after reading no data')
# Stop listening for input on the connection
if s in outputs:
outputs.remove(s)
inputs.remove(s)
s.close()

Socket programing - constantly send and recive data (python)

I'm programing a very simple screen sharing program.
I have the code for sending and reciving the screen picture. I want the server to be able to recive data at any time, and I want the Client to send the data every 3 seconds. this is the code:
Server:
import socket
import zlib
def conv(code):
with open('Image2.pgm', 'wb') as f:
f.write(code)
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 100000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
str = zlib.decompress(data)
conv(str)
print "All OK"
conn.send(data) # echo
conn.close()
Client:
import socket
import pyscreenshot as ImageGrab
import zlib
def PrtSc():
ImageGrab.grab_to_file("Image.pgm")
f = open("Image.pgm", "rb")
Image = f.read()
Image = zlib.compress(Image)
return Image
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 100000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(PrtSc())
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
Thanks :)

Categories