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.

index.png

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:-

  1. Connectionless Sockets User Datagram Protocol (UDP).
  2. Each packet sent or received is individually addressed.
  3. Order and reliability are not guaranteed.

Stream Sockets:-

  1. Connection-Oriented Sockets Transmission Control Protocol (TCP).
  2. Packets are sequenced with a unique flow of error-free data.
  3. Order and reliability are guaranteed.

Raw Sockets:-

  1. Allow direct sending and receiving of IP packets.
  2. Without any protocol-specific transport layer formatting
  3. When transmitting packets automatic addition of a header is optional
  4. Mostly used in security-related application
  5. 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()

image.png

Python Client Module:

  1. Socket Creation: s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  2. Socket Bind: s.bind((host_ip, port))
  3. Socket Accept: conn, addr = s.accept()
  4. Socket Listen: s.listen(10)
  5. Socket Receive: data += conn.recv(4096)

image.png

image.png

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:-

image.png