Today we would be creating a video chat application that will allow the users to see each other in real time. However I would not be integrating the voice transfer today.
So we would be using socket programming in python to send data and opencv to display the results.
Image is nothing but stacked arrays for the computer. So when we transmit image we are actually transferring arrays. This is a very important thing to keep in mind when we would be actually creating the application.
For the application we need two programs. One for the server and one for the client let’s begin with understanding the server side program first.
So firstly we created a socket. We have used SOCK_STREAM which means that we are using the TCP protocol and AF_INET tells that we would be using the ipV4 protocol for our application
I am using the port number 9999 however any unused port can be used.
For any successful connection we need both the IP and the port number so we bind them using the connect function. Now when we need to transfer the video over the network we need to stream this data continuously. This is needed to maintain a constant video. For this we have used the while loop so that data keeps on transmitting.
We are transmitting 1024*4 bytes per image. This is mentioned in the client.recv() function. We however cannot just transfer the data so we first load it using the pickle.
For showing the video we are using the imshow(). We have kept all this process inside the True of while. This is done so that we can keep getting the data . Send it and also keep showing our image using imshow().
Now let’s look at the other code.
For making a connection we need to connect to the server. We specify the ip of the host and the port we are going to connect to.
This is a simple task here we need to send the information to the server.
We use the similar dump function. This is send using the client_socker.sendall(). We are also showing the video that we are going to transmit.
This is similar to the server . We have to put all this code in the while loop so that it does not terminates.
Let’s see this code in action now.
Here when we run both the programs the video is transmitted and we can see both the recieving and transmitted video. Anyone connected within the same network can access the video chat application.