Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
177 lines (137 loc) · 5.21 KB
/
Copy pathserver.py
File metadata and controls
177 lines (137 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# PROJECT TITLE
# “Internet Radio Multicasting Multimedia over IP”
# GROUP MEMBERS
# Syed Umer Rashid (19K-1257)
# Yasir Hussain (19K-0223)
# Section: 6A
import cv2
import time
import struct
import socket
import numpy as np
from select import select
from videoprops import get_video_properties
# Define IP address and port
multicast_group = '224.1.1.1'
server_address = ('', 10000)
try:
# Create socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
# Set up multicast group
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
except socket.error as e:
print(f"\n Socket Error Occurred: {e} \n")
exit(1)
# pip install -U get-video-properties
try:
# props_1 = get_video_properties('video.mp4')
props_1 = get_video_properties('example.mp4')
width_1 = props_1['width']
height_1 = props_1['height']
Ratio_1 = props_1['display_aspect_ratio']
Frame_1 = props_1['avg_frame_rate']
props_2 = get_video_properties('Big_Bunny.mp4')
width_2 = props_2['width']
height_2 = props_2['height']
Ratio_2 = props_2['display_aspect_ratio']
Frame_2 = props_2['avg_frame_rate']
except Exception as e:
print(f"\n Error Occurred while Getting Video Properties: {e}")
exit(1)
print("\n Server is Successfully Started")
try:
data1, address = sock.recvfrom(10001)
except socket.error as e:
print(f"\n Socket Error : {e} \n")
sock.close()
exit(1)
print(f"\n --> Client Connected : {address} \n")
data1 = data1.decode()
choice = str(data1)
if(data1 != "start"):
print("\n Can't Connect, Bye !!! \n")
exit(1)
else:
# input_value = ("video.mp4" + "," + str(width_1) + "," + str(height_1) + "," + str(Ratio_1) + "," + str(Frame_1) + "," + "sample.mp4" + "," + str(width_2) + "," + str(height_2) + "," + str(Ratio_2) + "," + str(Frame_2))
input_value = ("example.mp4" + "," + str(width_1) + "," + str(height_1) + "," + str(Ratio_1) + "," + str(Frame_1) + "," + "Big_Bunny.mp4" + "," + str(width_2) + "," + str(height_2) + "," + str(Ratio_2) + "," + str(Frame_2))
input_value = input_value.encode()
# Send input to server
try:
sock.sendto(input_value, (multicast_group, 10001))
except socket.error as e:
print("\n Socket Error \n")
exit(1)
# data, address = sock.recvfrom(10001)
data, address = sock.recvfrom(65535) # Use Maximum Buffer Size of 65535
data = data.decode()
choice = str(data)
if(len(choice) > 1):
print("\n Kindly, Restart Server for New Client \n")
exit(1)
print(f"\n initial : User Selected Station - {choice}")
# Start capturing video
if(choice == '1'):
# cap = cv2.VideoCapture('video.mp4')
cap = cv2.VideoCapture('example.mp4')
elif(choice == '2'):
# cap = cv2.VideoCapture('sample.mp4')
cap = cv2.VideoCapture('Big_Bunny.mp4')
else:
print("\n No Station Selected \n")
exit(1)
FPS = 30
inpv = 5
timeout = 0.0001 # Timeout in Seconds
while True:
# To Change Station
# Check if there is data to be received
try:
ready, _, _ = select([sock], [], [], timeout)
except:
print("\n Program Terminated \n")
exit(1)
if ready:
try:
inpv = ''
inpv, address = sock.recvfrom(65535)
data = str(inpv)
if not inpv:
break
if(len(inpv) > 1):
print("\n\n Terminating ... \n")
print("\n --> Try to Restart the Server \n")
exit(1)
inpv = inpv.decode()
print(f"\n updated : User Selected Station - {inpv}")
if inpv == '1':
# cap = cv2.VideoCapture('video.mp4')
cap = cv2.VideoCapture('example.mp4')
elif inpv == '2':
# cap = cv2.VideoCapture('sample.mp4')
cap = cv2.VideoCapture('Big_Bunny.mp4')
except socket.error as e:
print(f"\n Socket Error Occurred: {e} \n")
exit(1)
try:
# Capture frame-by-frame
ret, frame = cap.read()
# Check if video has ended
if not ret:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Convert frame to string
data = cv2.imencode('.jpg', frame)[1].tobytes()
# Send data to multicast group
sock.sendto(data, (multicast_group, 10001))
# Calculate time taken to send frame
send_time = time.time()
# Delay to stream at original speed
time.sleep(1/FPS - (time.time() - send_time))
except KeyboardInterrupt:
# Clean up the socket
sock.close()
break