-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
266 lines (225 loc) · 7.48 KB
/
Copy pathClient.py
File metadata and controls
266 lines (225 loc) · 7.48 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Brandon Sayers
# Rocco Haro
from tkinter import *
from tkinter import messagebox
from PIL import Image
from PIL import ImageTk
import sys
import socket
import threading
import os
import traceback
import pickle
import time
import imageio
import numpy as np
class Client:
# Initialize our client
# root: GUI object
# serverAddress: IP address for server
# serverPort: Port number for server
# rtpPort: Port number for RTSP
def __init__(self, root, serverAddress, serverPort, rtpPort):
self.window = root
self.buildWindow()
self.lastcommand = ''
self.currentstate = 'INITIAL'
self.rtspSeqNo = 0
self.rtpSeqNo = 0
self.droppedFrames = 0
self.sessionID = 0
self.serverAddress = serverAddress
self.serverPort = serverPort
self.rtpPort = rtpPort
self.establishRTSPConnect()
# 33 minutes
# Populate our window with actionable buttons and our "video".
# self.setup: Changes state to READY.
# self.play: Changes state to PLAYING. Begin receiving stream.
# self.pause: Changes state to READY. Stop receiving stream.
# self.teardown: Terminates the client.
def buildWindow(self):
self.window.protocol("WM_DELETE_WINDOW", self.manualExit)
self.setup = Button(self.window, width = 12, padx = 2, pady = 2)
self.setup["text"] = "Setup"
self.setup["command"] = self.setupPlayer
self.setup.grid(row = 1, column = 0, padx = 2, pady = 2)
self.play = Button(self.window, width = 12, padx = 2, pady = 2)
self.play["text"] = "Play"
self.play["command"] = self.playPlayer
self.play.grid(row = 1, column = 1, padx = 2, pady = 2)
self.pause = Button(self.window, width = 12, padx = 2, pady = 2)
self.pause["text"] = "Pause"
self.pause["command"] = self.pausePlayer
self.pause.grid(row = 1, column = 2, padx = 2, pady = 2)
self.teardown = Button(self.window, width = 12, padx = 2, pady = 2)
self.teardown["text"] = "Teardown"
self.teardown["command"] = self.teardownPlayer
self.teardown.grid(row = 1, column = 3, padx = 2, pady = 2)
self.picture = Label(self.window, height = 30)
self.picture.grid(row = 0, column=0, columnspan=4, padx=10, pady=10)
# 4 minutes
# Handler for when the window is abruptly exited.
def manualExit(self):
if messagebox.askokcancel("Exit Livestream" ,"Are you sure you wish to exit?"):
self.teardownPlayer()
# 4 minutes
# Setup button handler.
def setupPlayer(self):
if self.currentstate == 'INITIAL':
self.sendRTSP('SETUP')
# 13 minutes
# Play button handler.
# Starts receive video data thread.
def playPlayer(self):
if self.currentstate == 'READY':
threading.Thread(target = self.receiveRTP).start()
self.videoThread = threading.Event()
self.videoThread.clear()
self.sendRTSP('PLAY')
# 3 minutes
# Pause button handler.
def pausePlayer(self):
if self.currentstate == 'PLAYING':
self.sendRTSP('PAUSE')
# 15 minutes
# Teardown button/Exit handler.
def teardownPlayer(self):
self.sendRTSP('TEARDOWN')
self.window.destroy()
sys.exit(0)
# 6 minutes
# Connect rtspSocket to server.
def establishRTSPConnect(self):
self.rtspSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.rtspSocket.connect((self.serverAddress, self.serverPort))
except:
messagebox.showwarning('Connection Unsuccessful', 'Connection to %s failed.' %self.serverAddress)
# 44 minutes
# Process button click and send RTSP packet with the command and sequence number.
def sendRTSP(self, command):
# Change states from INITIAL to READY.
# Start receiving data from RTSP socket.
if self.currentstate == 'INITIAL' and command == 'SETUP':
print("Sending Setup Request to Server.........")
threading.Thread(target = self.receiveRTSP).start()
self.rtspSeqNo += 1
packet = ['SETUP', self.rtspSeqNo, self.rtpPort]
self.rtspSocket.send(self.pickleIt(packet))
self.lastcommand = 'SETUP'
# Change states from READY to PLAYING.
elif self.currentstate == 'READY' and command == 'PLAY':
print("Sending Play Request to Server.........")
self.rtspSeqNo += 1
packet = ['PLAY', self.rtspSeqNo]
self.rtspSocket.send(self.pickleIt(packet))
self.lastcommand = 'PLAY'
# Change states from PLAYING to READY.
elif self.currentstate == 'PLAYING' and command == 'PAUSE':
print("Sending Pause Request to Server.........")
self.rtspSeqNo += 1
packet = ['PAUSE', self.rtspSeqNo]
self.rtspSocket.send(self.pickleIt(packet))
self.lastcommand = 'PAUSE'
# If not in INITIAL state, change state to TEARDOWN.
elif self.currentstate != 'INITIAL' and command == 'TEARDOWN':
print("Sending Teardown Request to Server.........")
self.rtspSeqNo += 1
packet = ['TEARDOWN', self.rtspSeqNo]
self.rtspSocket.send(self.pickleIt(packet))
self.lastcommand = 'TEARDOWN'
# 22 minutes
def receiveRTSP(self):
while True:
rawdata = self.rtspSocket.recv(1024)
if rawdata:
self.processRTSP(rawdata)
if self.lastcommand == 'TEARDOWN':
self.rtspSocket.shutdown(socket.SHUT_RDWR)
self.rtspSocket.close()
break
# 51 minutes
def processRTSP(self, rawdata):
data = self.unpickleIt(rawdata)
print("Here is our data:")
print(data)
code = int(data[0])
rtspSeqNo = data[1]
sessionID = data[2]
if rtspSeqNo == self.rtspSeqNo:
if self.sessionID == 0:
self.sessionID = sessionID
if sessionID == self.sessionID and code == 200:
if self.lastcommand == 'SETUP':
print("RTP connection setup...\n")
self.establishRTPConnect()
self.currentstate = 'READY'
elif self.lastcommand == 'PLAY':
print("Loading video...\n")
self.currentstate = 'PLAYING'
# RTP-Receive thread stops and new thread will be created when client selects Play.
elif self.lastcommand == 'PAUSE':
print("Video was paused...\n")
self.currentstate = 'READY'
self.videoThread.set()
elif self.lastcommand == 'TEARDOWN':
print("Disconnecting stream...\n")
self.currentstate = 'TEARDOWN'
# 31 minutes
# Create a new RTP Socket.
# Bind the socket to server's RTP port.
def establishRTPConnect(self):
try:
self.rtpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.rtpSocket.bind(('',self.rtpPort))
self.rtpSocket.settimeout(3)
except:
messagebox.showwarning('Connection Unsuccessful', 'Connection to %s failed.' %self.serverAddress)
# 1 hour 12 minutes
def receiveRTP(self):
count = 0
while True:
try:
rawdata, address = self.rtpSocket.recvfrom(25000)
if rawdata:
self.processRTP(rawdata, count)
count+=1
except:
if self.currentstate == "PLAYING":
print("Loading...")
else:
print("Not Receiving Data...")
if self.videoThread.isSet():
break
elif self.lastcommand == 'TEARDOWN':
self.rtpSocket.close()
break
# 3 hours 15 minutes
def processRTP(self, rawdata, pos):
data = self.unpickleIt(rawdata)
self.rtpSeqNo += 1
payloadtype = data[0]
rtpSeqNo = data[1]
timestamp = data[2]
videodata = data[3]
print("Received RTP Data seq:" + str(rtpSeqNo))
filePath = 'received/temp'+str(pos)+'.jpg'
videofile = open(filePath, 'wb')
videofile.write(videodata)
videofile.close()
videoreader = Image.open(filePath)
if self.rtpSeqNo != rtpSeqNo:
self.droppedFrames += (rtpSeqNo - self.rtpSeqNo)
self.rtpSeqNo = rtpSeqNo
print("!"*30 + "PACKET LOSS" + "!"*30)
self.Player(videoreader)
# 41 minutes
def Player(self, videoreader):
frame = ImageTk.PhotoImage(videoreader)
self.picture.configure(image = frame, height=400)
self.picture.image = frame
def pickleIt(self, packet):
return pickle.dumps(packet)
def unpickleIt(self, packet):
return pickle.loads(packet)