In order to stream video data, we first created a program that can send and receive simple number data!
At first, I made a streaming server with TCP, but it seemed that it would not be possible to communicate with multiple clients at the same time with TCP, so I decided to make it with UDP, which is a stateless protocol!
connection.ini
[server]
ip = 127.0.0.1
port = 1935
[packet]
# [bytes]
header_size = 4
# [pixels]
image_width = 256
image_height = 256
server.py
import socket
import configparser
import logging
logging.basicConfig(level=logging.DEBUG)
config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')
# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))
# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))
if __name__ == '__main__':
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind((SERVER_IP, SERVER_PORT))
logging.info(" Binding port on: " + SERVER_IP + ":" + str(SERVER_PORT))
while True:
data, addr = s.recvfrom(IMAGE_SIZE)
print(addr, int.from_bytes(data, 'big'))
stream.py
import socket
import configparser
import logging
import time
logging.basicConfig(level=logging.DEBUG)
config = configparser.ConfigParser()
config.read('./connection.ini', 'UTF-8')
# Connection Settings
SERVER_IP = config.get('server', 'ip')
SERVER_PORT = int(config.get('server', 'port'))
# Image Settings
IMAGE_WIDTH = int(config.get('packet', 'image_width'))
IMAGE_HEIGHT = int(config.get('packet', 'image_height'))
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT // 2
logging.info(" IMAGE SIZE: " + str(IMAGE_SIZE))
if __name__ == '__main__':
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
i = 0
while True:
s.sendto(i.to_bytes(IMAGE_SIZE, 'big'), (SERVER_IP, SERVER_PORT))
logging.info(" Sent: " + str(i))
time.sleep(1)
i = i + 1
It reads the address and port from the config file and continues to send the ever-incremented ʻi value from stream.pyto'server.py'. 'socket.SOCK_DGRAM' indicates that UDP communication is performed.recvfrom ()` returns the received data and the tuple of the data source, which is used to display the information in the terminal.
Try running it with one server.py and two stream.py.
INFO:root: IMAGE SIZE: 32768
INFO:root: Binding port on: 127.0.0.1:1935
('127.0.0.1', 55382) 0
('127.0.0.1', 55382) 1
('127.0.0.1', 55382) 2
('127.0.0.1', 55382) 3
('127.0.0.1', 55382) 4
('127.0.0.1', 55382) 5
('127.0.0.1', 55382) 6
('127.0.0.1', 55382) 7
('127.0.0.1', 55382) 8
('127.0.0.1', 59979) 0
('127.0.0.1', 55382) 9
('127.0.0.1', 59979) 1
('127.0.0.1', 55382) 10
('127.0.0.1', 59979) 2
('127.0.0.1', 55382) 11
('127.0.0.1', 59979) 3
('127.0.0.1', 55382) 12
('127.0.0.1', 59979) 4
('127.0.0.1', 55382) 13
('127.0.0.1', 59979) 5
('127.0.0.1', 55382) 14
('127.0.0.1', 59979) 6
('127.0.0.1', 55382) 15
('127.0.0.1', 59979) 7
('127.0.0.1', 55382) 16
('127.0.0.1', 59979) 8
('127.0.0.1', 55382) 17
('127.0.0.1', 59979) 9
('127.0.0.1', 55382) 18
('127.0.0.1', 59979) 10
('127.0.0.1', 55382) 19
('127.0.0.1', 59979) 11
Data is being sent from each of the two addresses!
Recommended Posts