Video Streaming using Socket Programming and OpenCV

Video Streaming using Socket Programming and OpenCV

What is Socket?

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 software structure within a network mode.
  • Serves as an endpoint to send and receive data.
  • Its properties are defined by network API.
  • Exists only during the process of application.
  • Externally identified by its socket address.

What are Socket Addresses?

  • A combination of protocol type, IP address and Port number for data communication.
  • A remote process establishes a socket in its protocol stack.
  • The remote process then uses a networking API to connect to the application.
  • It presents its own socket address for use.

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:

  • Socket Creation: clientsocket= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • Socket Connect: clientsocket.connect((host_ip, port))
  • Socket Send: clientsocket.sendall() image.png

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) 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.


image.png I would like to thanks Mr Vimal Daga sir for providing us with such knowledge to complete this task.

Thank You.