├── README.md ├── client.py ├── LICENSE ├── serverAudio.py ├── clientAudio.py ├── server.py ├── serverVideo.py ├── clientVideo.py ├── serverMedia.py └── clientMedia.py /README.md: -------------------------------------------------------------------------------- 1 | # Chat-Room 2 | - A 1v1 Video Chatting application using Python3 sockets. 3 | 4 | - To Host:- 5 | ``` 6 | python3 serverMedia.py 7 | ``` 8 | 9 | - To Connect:- 10 | ``` 11 | python3 clientMedia.py 12 | ``` 13 | 14 | Please Go through [this link](https://medium.com/@adityakumar_365/video-conferencing-using-sockets-in-python-3-b4a346416bca) for further details. 15 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | from socket import AF_INET, SOCK_STREAM, socket 2 | from threading import Thread 3 | 4 | HOST = input("Enter Host IP: ") 5 | PORT = eval(input("Enter Port No: ")) 6 | BufferSize = 1024 7 | 8 | def Recieve(): 9 | while True: 10 | try: 11 | msg = client.recv(BufferSize).decode("utf-8") 12 | print(msg) 13 | except OSError: 14 | break 15 | 16 | def Send(): 17 | while True: 18 | msg = input() 19 | if msg == "quit": 20 | client.send(msg.encode("utf-8")) 21 | client.close() 22 | break 23 | else: 24 | client.send(msg.encode("utf-8")) 25 | 26 | client = socket(family=AF_INET, type=SOCK_STREAM) 27 | client.connect((HOST, PORT)) 28 | 29 | RecieveThread = Thread(target=Recieve).start() 30 | SendThread = Thread(target=Send).start() 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Aditya Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /serverAudio.py: -------------------------------------------------------------------------------- 1 | from socket import socket, AF_INET, SOCK_STREAM 2 | from threading import Thread 3 | 4 | 5 | HOST = input("Enter Host IP\n") 6 | PORT = 4000 7 | BufferSize = 4096 8 | addresses = {} 9 | 10 | def Connections(): 11 | while True: 12 | try: 13 | client, addr = server.accept() 14 | print("{} is connected!!".format(addr)) 15 | addresses[client] = addr 16 | Thread(target=ClientConnectionSound, args=(client, )).start() 17 | except: 18 | continue 19 | 20 | def ClientConnectionSound(client): 21 | while True: 22 | try: 23 | data = client.recv(BufferSize) 24 | broadcastSound(client, data) 25 | except: 26 | continue 27 | 28 | def broadcastSound(clientSocket, data_to_be_sent): 29 | for client in addresses: 30 | if client != clientSocket: 31 | client.sendall(data_to_be_sent) 32 | 33 | 34 | server = socket(family=AF_INET, type=SOCK_STREAM) 35 | try: 36 | server.bind((HOST, PORT)) 37 | except OSError: 38 | print("Server Busy") 39 | 40 | server.listen(2) 41 | print("Waiting for connection..") 42 | AcceptThread = Thread(target=Connections) 43 | AcceptThread.start() 44 | AcceptThread.join() 45 | -------------------------------------------------------------------------------- /clientAudio.py: -------------------------------------------------------------------------------- 1 | from socket import socket, AF_INET, SOCK_STREAM 2 | from threading import Thread 3 | import pyaudio 4 | from array import array 5 | 6 | 7 | HOST = input("Enter Server IP\n") 8 | PORT = 4000 9 | BufferSize = 4096 10 | 11 | FORMAT=pyaudio.paInt16 12 | CHANNELS=2 13 | RATE=44100 14 | CHUNK=1024 15 | 16 | def SendAudio(): 17 | while True: 18 | data = stream.read(CHUNK) 19 | dataChunk = array('h', data) 20 | vol = max(dataChunk) 21 | if(vol > 500): 22 | print("Recording Sound...") 23 | else: 24 | print("Silence..") 25 | client.sendall(data) 26 | 27 | 28 | def RecieveAudio(): 29 | while True: 30 | data = recvall(BufferSize) 31 | stream.write(data) 32 | 33 | def recvall(size): 34 | databytes = b'' 35 | while len(databytes) != size: 36 | to_read = size - len(databytes) 37 | if to_read > (4 * CHUNK): 38 | databytes += client.recv(4 * CHUNK) 39 | else: 40 | databytes += client.recv(to_read) 41 | return databytes 42 | 43 | client = socket(family=AF_INET, type=SOCK_STREAM) 44 | client.connect((HOST, PORT)) 45 | 46 | audio=pyaudio.PyAudio() 47 | stream=audio.open(format=FORMAT,channels=CHANNELS, rate=RATE, input=True, output = True,frames_per_buffer=CHUNK) 48 | 49 | 50 | RecieveAudioThread = Thread(target=RecieveAudio).start() 51 | SendAudioThread = Thread(target=SendAudio).start() 52 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from socket import AF_INET, SOCK_STREAM, socket 2 | from threading import Thread 3 | 4 | HOST = "127.0.0.1" 5 | PORT = 3000 6 | 7 | addresses = {} 8 | clients = {} 9 | 10 | def Connections(): 11 | while True: 12 | client, addr = server.accept() 13 | print("{} is connected!!".format(addr)) 14 | client.send(("Welcome to Chat Room. Type {quit} to exit. Enter your name: ").encode("utf-8")) 15 | addresses[client] = addr 16 | Thread(target = ClientConnection, args=(client, )).start() 17 | 18 | def ClientConnection(client): 19 | name = client.recv(BufferSize).decode("utf-8") 20 | client.send(("Hello {}".format(name)).encode("utf-8")) 21 | message = ("{} has joined the chat..").format(name) 22 | Broadcast(message.encode("utf-8")) 23 | clients[client] = name 24 | while True: 25 | msg = client.recv(BufferSize).decode("utf-8") 26 | if msg != "quit": 27 | Broadcast(msg.encode("utf-8"), name + ": ") 28 | else: 29 | message = ("{} has left the chat.").format(clients[client]) 30 | Broadcast(message.encode("utf-8")) 31 | client.send(("Will see you soon..").encode("utf-8")) 32 | del clients[client] 33 | break 34 | 35 | def Broadcast(msg, name = ""): 36 | for sockets in clients: 37 | sockets.send(name.encode("utf-8") + msg) 38 | 39 | server = socket(family=AF_INET, type=SOCK_STREAM) 40 | try: 41 | server.bind((HOST, PORT)) 42 | except OSError: 43 | print("Server Busy") 44 | BufferSize = 1024 45 | 46 | server.listen(5) 47 | print("Waiting for Connections... ") 48 | AcceptThread = Thread(target=Connections) 49 | AcceptThread.start() 50 | AcceptThread.join() 51 | server.close() 52 | -------------------------------------------------------------------------------- /serverVideo.py: -------------------------------------------------------------------------------- 1 | from socket import socket, AF_INET, SOCK_STREAM 2 | from threading import Thread 3 | import struct 4 | 5 | HOST = input("Enter Host IP\n") 6 | PORT = 3000 7 | lnF = 640*480*3 8 | CHUNK = 1024 9 | addresses = {} 10 | threads = {} 11 | 12 | def Connections(): 13 | while True: 14 | try: 15 | client, addr = server.accept() 16 | print("{} is connected!!".format(addr)) 17 | addresses[client] = addr 18 | if len(addresses) > 1: 19 | for sockets in addresses: 20 | if sockets not in threads: 21 | threads[sockets] = True 22 | sockets.send(("start").encode()) 23 | Thread(target=ClientConnection, args=(sockets, )).start() 24 | else: 25 | continue 26 | except: 27 | continue 28 | 29 | def ClientConnection(client): 30 | while True: 31 | try: 32 | lengthbuf = recvall(client, 4) 33 | length, = struct.unpack('!I', lengthbuf) 34 | recvall(client, length) 35 | except: 36 | continue 37 | 38 | def broadcast(clientSocket, data_to_be_sent): 39 | for client in addresses: 40 | if client != clientSocket: 41 | client.sendall(data_to_be_sent) 42 | 43 | def recvall(client, BufferSize): 44 | databytes = b'' 45 | i = 0 46 | while i != BufferSize: 47 | to_read = BufferSize - i 48 | if to_read > (1000 * CHUNK): 49 | databytes = client.recv(1000 * CHUNK) 50 | i += len(databytes) 51 | broadcast(client, databytes) 52 | else: 53 | if BufferSize == 4: 54 | databytes += client.recv(to_read) 55 | else: 56 | databytes = client.recv(to_read) 57 | i += len(databytes) 58 | if BufferSize != 4: 59 | broadcast(client, databytes) 60 | print("YES!!!!!!!!!" if i == BufferSize else "NO!!!!!!!!!!!!") 61 | if BufferSize == 4: 62 | broadcast(client, databytes) 63 | return databytes 64 | 65 | server = socket(family=AF_INET, type=SOCK_STREAM) 66 | try: 67 | server.bind((HOST, PORT)) 68 | except OSError: 69 | print("Server Busy") 70 | 71 | server.listen(2) 72 | print("Waiting for connection..") 73 | AcceptThread = Thread(target=Connections) 74 | AcceptThread.start() 75 | AcceptThread.join() 76 | server.close() 77 | -------------------------------------------------------------------------------- /clientVideo.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from socket import socket, AF_INET, SOCK_STREAM 3 | from imutils.video import WebcamVideoStream 4 | from threading import Thread 5 | import numpy as np 6 | import zlib 7 | import struct 8 | 9 | HOST = input("Enter Server IP\n") 10 | PORT = 3000 11 | 12 | CHUNK=1024 13 | lnF = 640*480*3 14 | 15 | def SendFrame(): 16 | while True: 17 | try: 18 | frame = wvs.read() 19 | cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 20 | frame = cv2.resize(frame, (640, 480)) 21 | frame = np.array(frame, dtype = np.uint8).reshape(1, lnF) 22 | jpg_as_text = bytearray(frame) 23 | 24 | databytes = zlib.compress(jpg_as_text, 9) 25 | length = struct.pack('!I', len(databytes)) 26 | bytesToBeSend = b'' 27 | client.sendall(length) 28 | while len(databytes) > 0: 29 | if (1000 * CHUNK) <= len(databytes): 30 | bytesToBeSend = databytes[:(1000 * CHUNK)] 31 | databytes = databytes[(1000 * CHUNK):] 32 | client.sendall(bytesToBeSend) 33 | else: 34 | bytesToBeSend = databytes 35 | client.sendall(bytesToBeSend) 36 | databytes = b'' 37 | print("##### Data Sent!! #####") 38 | except: 39 | continue 40 | 41 | def RecieveMedia(): 42 | while True: 43 | try: 44 | lengthbuf = recvall(4) 45 | length, = struct.unpack('!I', lengthbuf) 46 | databytes = recvall(length) 47 | img = zlib.decompress(databytes) 48 | if len(databytes) == length: 49 | print("Recieving Media..") 50 | print("Image Frame Size:- {}".format(len(img))) 51 | img = np.array(list(img)) 52 | img = np.array(img, dtype = np.uint8).reshape(480, 640, 3) 53 | cv2.imshow("Stream", img) 54 | if cv2.waitKey(1) == 27: 55 | cv2.destroyAllWindows() 56 | else: 57 | print("Data CORRUPTED") 58 | except: 59 | continue 60 | 61 | def recvall(size): 62 | databytes = b'' 63 | while len(databytes) != size: 64 | to_read = size - len(databytes) 65 | if to_read > (1000 * CHUNK): 66 | databytes += client.recv(1000 * CHUNK) 67 | else: 68 | databytes += client.recv(to_read) 69 | return databytes 70 | 71 | client = socket(family=AF_INET, type=SOCK_STREAM) 72 | client.connect((HOST, PORT)) 73 | wvs = WebcamVideoStream(0).start() 74 | 75 | initiation = client.recv(5).decode() 76 | 77 | if initiation == "start": 78 | RecieveFrameThread = Thread(target=RecieveMedia).start() 79 | SendFrameThread = Thread(target=SendFrame).start() 80 | -------------------------------------------------------------------------------- /serverMedia.py: -------------------------------------------------------------------------------- 1 | from socket import socket, AF_INET, SOCK_STREAM 2 | from threading import Thread 3 | import struct 4 | 5 | HOST = input("Enter Host IP\n") 6 | PORT_VIDEO = 3000 7 | PORT_AUDIO = 4000 8 | lnF = 640*480*3 9 | CHUNK = 1024 10 | BufferSize = 4096 11 | addressesAudio = {} 12 | addresses = {} 13 | threads = {} 14 | 15 | def ConnectionsVideo(): 16 | while True: 17 | try: 18 | clientVideo, addr = serverVideo.accept() 19 | print("{} is connected!!".format(addr)) 20 | addresses[clientVideo] = addr 21 | if len(addresses) > 1: 22 | for sockets in addresses: 23 | if sockets not in threads: 24 | threads[sockets] = True 25 | sockets.send(("start").encode()) 26 | Thread(target=ClientConnectionVideo, args=(sockets, )).start() 27 | else: 28 | continue 29 | except: 30 | continue 31 | 32 | def ConnectionsSound(): 33 | while True: 34 | try: 35 | clientAudio, addr = serverAudio.accept() 36 | print("{} is connected!!".format(addr)) 37 | addressesAudio[clientAudio] = addr 38 | Thread(target=ClientConnectionSound, args=(clientAudio, )).start() 39 | except: 40 | continue 41 | 42 | def ClientConnectionVideo(clientVideo): 43 | while True: 44 | try: 45 | lengthbuf = recvall(clientVideo, 4) 46 | length, = struct.unpack('!I', lengthbuf) 47 | recvall(clientVideo, length) 48 | except: 49 | continue 50 | 51 | def ClientConnectionSound(clientAudio): 52 | while True: 53 | try: 54 | data = clientAudio.recv(BufferSize) 55 | broadcastSound(clientAudio, data) 56 | except: 57 | continue 58 | 59 | def recvall(clientVideo, BufferSize): 60 | databytes = b'' 61 | i = 0 62 | while i != BufferSize: 63 | to_read = BufferSize - i 64 | if to_read > (1000 * CHUNK): 65 | databytes = clientVideo.recv(1000 * CHUNK) 66 | i += len(databytes) 67 | broadcastVideo(clientVideo, databytes) 68 | else: 69 | if BufferSize == 4: 70 | databytes += clientVideo.recv(to_read) 71 | else: 72 | databytes = clientVideo.recv(to_read) 73 | i += len(databytes) 74 | if BufferSize != 4: 75 | broadcastVideo(clientVideo, databytes) 76 | print("YES!!!!!!!!!" if i == BufferSize else "NO!!!!!!!!!!!!") 77 | if BufferSize == 4: 78 | broadcastVideo(clientVideo, databytes) 79 | return databytes 80 | 81 | def broadcastVideo(clientSocket, data_to_be_sent): 82 | for clientVideo in addresses: 83 | if clientVideo != clientSocket: 84 | clientVideo.sendall(data_to_be_sent) 85 | 86 | def broadcastSound(clientSocket, data_to_be_sent): 87 | for clientAudio in addressesAudio: 88 | if clientAudio != clientSocket: 89 | clientAudio.sendall(data_to_be_sent) 90 | 91 | serverVideo = socket(family=AF_INET, type=SOCK_STREAM) 92 | try: 93 | serverVideo.bind((HOST, PORT_VIDEO)) 94 | except OSError: 95 | print("Server Busy") 96 | 97 | serverAudio = socket(family=AF_INET, type=SOCK_STREAM) 98 | try: 99 | serverAudio.bind((HOST, PORT_AUDIO)) 100 | except OSError: 101 | print("Server Busy") 102 | 103 | serverAudio.listen(2) 104 | print("Waiting for connection..") 105 | AcceptThreadAudio = Thread(target=ConnectionsSound) 106 | AcceptThreadAudio.start() 107 | 108 | 109 | serverVideo.listen(2) 110 | print("Waiting for connection..") 111 | AcceptThreadVideo = Thread(target=ConnectionsVideo) 112 | AcceptThreadVideo.start() 113 | AcceptThreadVideo.join() 114 | serverVideo.close() 115 | -------------------------------------------------------------------------------- /clientMedia.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from socket import socket, AF_INET, SOCK_STREAM 3 | from imutils.video import WebcamVideoStream 4 | import pyaudio 5 | from array import array 6 | from threading import Thread 7 | import numpy as np 8 | import zlib 9 | import struct 10 | 11 | HOST = input("Enter Server IP\n") 12 | PORT_VIDEO = 3000 13 | PORT_AUDIO = 4000 14 | 15 | BufferSize = 4096 16 | CHUNK=1024 17 | lnF = 640*480*3 18 | FORMAT=pyaudio.paInt16 19 | CHANNELS=2 20 | RATE=44100 21 | 22 | def SendAudio(): 23 | while True: 24 | data = stream.read(CHUNK) 25 | dataChunk = array('h', data) 26 | vol = max(dataChunk) 27 | if(vol > 500): 28 | print("Recording Sound...") 29 | else: 30 | print("Silence..") 31 | clientAudioSocket.sendall(data) 32 | 33 | def RecieveAudio(): 34 | while True: 35 | data = recvallAudio(BufferSize) 36 | stream.write(data) 37 | 38 | def recvallAudio(size): 39 | databytes = b'' 40 | while len(databytes) != size: 41 | to_read = size - len(databytes) 42 | if to_read > (4 * CHUNK): 43 | databytes += clientAudioSocket.recv(4 * CHUNK) 44 | else: 45 | databytes += clientAudioSocket.recv(to_read) 46 | return databytes 47 | 48 | def SendFrame(): 49 | while True: 50 | try: 51 | frame = wvs.read() 52 | cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 53 | frame = cv2.resize(frame, (640, 480)) 54 | frame = np.array(frame, dtype = np.uint8).reshape(1, lnF) 55 | jpg_as_text = bytearray(frame) 56 | 57 | databytes = zlib.compress(jpg_as_text, 9) 58 | length = struct.pack('!I', len(databytes)) 59 | bytesToBeSend = b'' 60 | clientVideoSocket.sendall(length) 61 | while len(databytes) > 0: 62 | if (5000 * CHUNK) <= len(databytes): 63 | bytesToBeSend = databytes[:(5000 * CHUNK)] 64 | databytes = databytes[(5000 * CHUNK):] 65 | clientVideoSocket.sendall(bytesToBeSend) 66 | else: 67 | bytesToBeSend = databytes 68 | clientVideoSocket.sendall(bytesToBeSend) 69 | databytes = b'' 70 | print("##### Data Sent!! #####") 71 | except: 72 | continue 73 | 74 | 75 | def RecieveFrame(): 76 | while True: 77 | try: 78 | lengthbuf = recvallVideo(4) 79 | length, = struct.unpack('!I', lengthbuf) 80 | databytes = recvallVideo(length) 81 | img = zlib.decompress(databytes) 82 | if len(databytes) == length: 83 | print("Recieving Media..") 84 | print("Image Frame Size:- {}".format(len(img))) 85 | img = np.array(list(img)) 86 | img = np.array(img, dtype = np.uint8).reshape(480, 640, 3) 87 | cv2.imshow("Stream", img) 88 | if cv2.waitKey(1) == 27: 89 | cv2.destroyAllWindows() 90 | else: 91 | print("Data CORRUPTED") 92 | except: 93 | continue 94 | 95 | 96 | def recvallVideo(size): 97 | databytes = b'' 98 | while len(databytes) != size: 99 | to_read = size - len(databytes) 100 | if to_read > (5000 * CHUNK): 101 | databytes += clientVideoSocket.recv(5000 * CHUNK) 102 | else: 103 | databytes += clientVideoSocket.recv(to_read) 104 | return databytes 105 | 106 | 107 | 108 | clientVideoSocket = socket(family=AF_INET, type=SOCK_STREAM) 109 | clientVideoSocket.connect((HOST, PORT_VIDEO)) 110 | wvs = WebcamVideoStream(0).start() 111 | 112 | clientAudioSocket = socket(family=AF_INET, type=SOCK_STREAM) 113 | clientAudioSocket.connect((HOST, PORT_AUDIO)) 114 | 115 | audio=pyaudio.PyAudio() 116 | stream=audio.open(format=FORMAT,channels=CHANNELS, rate=RATE, input=True, output = True,frames_per_buffer=CHUNK) 117 | 118 | initiation = clientVideoSocket.recv(5).decode() 119 | 120 | if initiation == "start": 121 | SendFrameThread = Thread(target=SendFrame).start() 122 | SendAudioThread = Thread(target=SendAudio).start() 123 | RecieveFrameThread = Thread(target=RecieveFrame).start() 124 | RecieveAudioThread = Thread(target=RecieveAudio).start() 125 | --------------------------------------------------------------------------------