Video Streaming Program using Socket Programming and OpenCV -Python3
What is Socket programming?
Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection.
Sockets-:
Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.
To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. What is Network Socket? A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture. Sockets are created only during the lifetime of a process of an application running in the node.
Implementation of Socket. In standard Internet Protocols like TCP and UDP: Socket Address is a combination of = (IP address, Port Number), Much like Telephone and extension.
Several types of Internet Sockets:-
Datagram Sockets:-
- Connectionless Sockets User Datagram Protocol (UDP).
- Each packet sent or received is individually addressed.
- Order and reliability are not guaranteed.
Stream Sockets:-
- Connection-Oriented Sockets Transmission Control Protocol (TCP).
- Packets are sequenced with a unique flow of error-free data.
- Order and reliability are guaranteed.
Raw Sockets:-
- Allow direct sending and receiving of IP packets.
- Without any protocol-specific transport layer formatting
- When transmitting packets automatic addition of a header is optional
- Mostly used in security-related application
- Raw sockets are typically available in network equipment
Video Streaming Application:-
Python Server Module:
1.Socket Creation: clientsocket= socket.socket(socket.AF_INET, socket.SOCK_STREAM) 2.Socket Connect: clientsocket.connect((host_ip, port)) 3.Socket Send: clientsocket.sendall()
Python Client Module:
- Socket Creation: s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- Socket Bind: s.bind((host_ip, port))
- Socket Accept: conn, addr = s.accept()
- Socket Listen: s.listen(10)
- Socket Receive: data += conn.recv(4096)
Video Data Transmission:-
At server side:- -With Open CV get video frames of the webcam. -With “pickle” serialize frame to byte data. -Pack each frame data using the struct module.
- Send data to client and display frame.
At client Side:-
- Receive packets and append them to data.
- Unpack the data using the struct module.
- Load the frame using the pickle.
- Display the frame at the client-side.
Output:-