Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions Client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__author__ = 'Tibbers'
from Tkinter import *
import tkMessageBox
from tkinter import *
import tkinter.messagebox as tkMessageBox
from PIL import Image, ImageTk
import socket, threading, sys, traceback, os

Expand Down Expand Up @@ -80,7 +80,7 @@ def exitClient(self):
self.master.destroy() # Close the gui window
os.remove(CACHE_FILE_NAME + str(self.sessionId) + CACHE_FILE_EXT) # Delete the cache image from video
rate = float(self.counter/self.frameNbr)
print '-'*60 + "\nRTP Packet Loss Rate :" + str(rate) +"\n" + '-'*60
print ('-'*60 + "\nRTP Packet Loss Rate :" + str(rate) +"\n" + '-'*60)
sys.exit(0)

def pauseMovie(self):
Expand All @@ -92,7 +92,7 @@ def playMovie(self):
"""Play button handler."""
if self.state == self.READY:
# Create a new thread to listen for RTP packets
print "Playing Movie"
print ("Playing Movie")
threading.Thread(target=self.listenRtp).start()
self.playEvent = threading.Event()
self.playEvent.clear()
Expand All @@ -106,27 +106,27 @@ def listenRtp(self):
if data:
rtpPacket = RtpPacket()
rtpPacket.decode(data)
print "||Received Rtp Packet #" + str(rtpPacket.seqNum()) + "|| "
print ("||Received Rtp Packet #" + str(rtpPacket.seqNum()) + "|| ")

try:
if self.frameNbr + 1 != rtpPacket.seqNum():
self.counter += 1
print '!'*60 + "\nPACKET LOSS\n" + '!'*60
print ('!'*60 + "\nPACKET LOSS\n" + '!'*60)
currFrameNbr = rtpPacket.seqNum()
#version = rtpPacket.version()
except:
print "seqNum() error"
print '-'*60
print ("seqNum() error")
print ('-'*60)
traceback.print_exc(file=sys.stdout)
print '-'*60
print ('-'*60)

if currFrameNbr > self.frameNbr: # Discard the late packet
self.frameNbr = currFrameNbr
self.updateMovie(self.writeFrame(rtpPacket.getPayload()))

except:
# Stop listening upon requesting PAUSE or TEARDOWN
print "Didn`t receive data!"
print ("Didn`t receive data!")
if self.playEvent.isSet():
break

Expand All @@ -145,12 +145,12 @@ def writeFrame(self, data):
try:
file = open(cachename, "wb")
except:
print "file open error"
print ("file open error")

try:
file.write(data)
except:
print "file write error"
print ("file write error")

file.close()

Expand All @@ -161,10 +161,10 @@ def updateMovie(self, imageFile):
try:
photo = ImageTk.PhotoImage(Image.open(imageFile)) #stuck here !!!!!!
except:
print "photo error"
print '-'*60
print ("photo error")
print ('-'*60)
traceback.print_exc(file=sys.stdout)
print '-'*60
print ('-'*60)

self.label.configure(image = photo, height=288)
self.label.image = photo
Expand Down Expand Up @@ -194,7 +194,7 @@ def sendRtspRequest(self, requestCode):
# request = ...
request = "SETUP " + str(self.fileName) + "\n" + str(self.rtspSeq) + "\n" + " RTSP/1.0 RTP/UDP " + str(self.rtpPort)

self.rtspSocket.send(request)
self.rtspSocket.send(request.encode('utf-8'))
# Keep track of the sent request.
# self.requestSent = ...
self.requestSent = self.SETUP
Expand All @@ -208,8 +208,8 @@ def sendRtspRequest(self, requestCode):
# request = ...
request = "PLAY " + "\n" + str(self.rtspSeq)

self.rtspSocket.send(request)
print '-'*60 + "\nPLAY request sent to Server...\n" + '-'*60
self.rtspSocket.send(request.encode('utf-8'))
print ('-'*60 + "\nPLAY request sent to Server...\n" + '-'*60)
# Keep track of the sent request.
# self.requestSent = ...
self.requestSent = self.PLAY
Expand All @@ -222,8 +222,8 @@ def sendRtspRequest(self, requestCode):
# Write the RTSP request to be sent.
# request = ...
request = "PAUSE " + "\n" + str(self.rtspSeq)
self.rtspSocket.send(request)
print '-'*60 + "\nPAUSE request sent to Server...\n" + '-'*60
self.rtspSocket.send(request.encode('utf-8'))
print ('-'*60 + "\nPAUSE request sent to Server...\n" + '-'*60)
# Keep track of the sent request.
# self.requestSent = ...
self.requestSent = self.PAUSE
Expand All @@ -239,8 +239,8 @@ def sendRtspRequest(self, requestCode):
# Write the RTSP request to be sent.
# request = ...
request = "TEARDOWN " + "\n" + str(self.rtspSeq)
self.rtspSocket.send(request)
print '-'*60 + "\nTEARDOWN request sent to Server...\n" + '-'*60
self.rtspSocket.send(request.encode('utf-8'))
print ('-'*60 + "\nTEARDOWN request sent to Server...\n" + '-'*60)
# Keep track of the sent request.
# self.requestSent = ...
self.requestSent = self.TEARDOWN
Expand Down Expand Up @@ -268,10 +268,10 @@ def recvRtspReply(self):
break

def parseRtspReply(self, data):
print "Parsing Received Rtsp data..."
print ("Parsing Received Rtsp data...")

"""Parse the RTSP reply from the server."""
lines = data.split('\n')
lines = data.decode('utf-8').split('\n')
seqNum = int(lines[1].split(' ')[1])

# Process only if the server reply's sequence number is the same as the request's
Expand All @@ -289,17 +289,17 @@ def parseRtspReply(self, data):
# TO COMPLETE
#-------------
# Update RTSP state.
print "Updating RTSP state..."
print ("Updating RTSP state...")
# self.state = ...
self.state = self.READY
# Open RTP port.
#self.openRtpPort()
print "Setting Up RtpPort for Video Stream"
print ("Setting Up RtpPort for Video Stream")
self.openRtpPort()

elif self.requestSent == self.PLAY:
self.state = self.PLAYING
print '-'*60 + "\nClient is PLAYING...\n" + '-'*60
print ('-'*60 + "\nClient is PLAYING...\n" + '-'*60)
elif self.requestSent == self.PAUSE:
self.state = self.READY

Expand Down Expand Up @@ -334,7 +334,7 @@ def openRtpPort(self):
#self.rtpSocket.connect(self.serverAddr,self.rtpPort)
self.rtpSocket.bind((self.serverAddr,self.rtpPort)) # WATCH OUT THE ADDRESS FORMAT!!!!! rtpPort# should be bigger than 1024
#self.rtpSocket.listen(5)
print "Bind RtpPort Success"
print ("Bind RtpPort Success")

except:
tkMessageBox.showwarning('Connection Failed', 'Connection to rtpServer failed...')
Expand All @@ -347,7 +347,7 @@ def handler(self):
self.exitClient()
else: # When the user presses cancel, resume playing.
#self.playMovie()
print "Playing Movie"
print ("Playing Movie")
threading.Thread(target=self.listenRtp).start()
#self.playEvent = threading.Event()
#self.playEvent.clear()
Expand Down
4 changes: 2 additions & 2 deletions ClientLauncher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__author__ = 'Tibbers'
import sys
from Tkinter import Tk
from tkinter import Tk
from Client import Client

if __name__ == "__main__":
Expand All @@ -10,7 +10,7 @@
rtpPort = sys.argv[3]
fileName = sys.argv[4]
except:
print "[Usage: ClientLauncher.py Server_name Server_port RTP_port Video_file]\n"
print ("[Usage: ClientLauncher.py Server_name Server_port RTP_port Video_file]\n")

root = Tk()

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ How to:
Example:

Open a terminal:
python Server.py 1025
python3 Server.py 1025

Open another terminal:
python ClientLauncher.py 127.0.0.1 1025 5008 video.mjpeg
python3 ClientLauncher.py 127.0.0.1 1025 5008 video.mjpeg





Start the server with the command line

python Server.py server_port
python3 Server.py server_port

Where server_port is the port your server listens to for incoming RTSP connections
# 1025
Expand All @@ -26,7 +26,7 @@ Open a new terminal

Start the client with the command line

python ClientLauncher.py server_host server_port RTP_port video_file
python3 ClientLauncher.py server_host server_port RTP_port video_file

Where
# server_host : the name of the machine where server is running (here "127.0.0.1")
Expand Down
8 changes: 4 additions & 4 deletions RtpPacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def encode(self, version, padding, extension, cc, seqnum, marker, pt, ssrc, payl
"""Encode the RTP packet with header fields and payload."""

timestamp = int(time())
print "timestamp: " + str(timestamp)
print ("timestamp: " + str(timestamp))
self.header = bytearray(HEADER_SIZE)
#--------------
# TO COMPLETE
Expand All @@ -45,8 +45,8 @@ def encode(self, version, padding, extension, cc, seqnum, marker, pt, ssrc, payl
self.header[1] = marker << 7
self.header[1] = self.header[1] | pt

self.header[2] = seqnum >> 8
self.header[3] = seqnum
self.header[2] = seqnum >> 8 & 0xFF # sequence number: 16 bits
self.header[3] = seqnum & 0xFF

self.header[4] = (timestamp >> 24) & 0xFF
self.header[5] = (timestamp >> 16) & 0xFF
Expand Down Expand Up @@ -98,4 +98,4 @@ def getPayload(self):

def getPacket(self):
"""Return RTP packet."""
return self.header + self.payload
return self.header + self.payload
4 changes: 2 additions & 2 deletions Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def main(self):
try:
SERVER_PORT = int(sys.argv[1])
except:
print "[Usage: Server.py Server_port]\n"
print("[Usage: Server.py Server_port]\n")
rtspSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
rtspSocket.bind(('', SERVER_PORT))
print "RTSP Listing incoming request..."
print("RTSP Listing incoming request...")
rtspSocket.listen(5)

# Receive client info (address,port) through RTSP/TCP session
Expand Down
36 changes: 17 additions & 19 deletions ServerWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ def recvRtspRequest(self):
while True:
data = connSocket.recv(256) ###
if data:
print '-'*60 + "\nData received:\n" + '-'*60
print ('-'*60 + "\nData received:\n" + '-'*60)
self.processRtspRequest(data)

def processRtspRequest(self, data):
"""Process RTSP request sent from the client."""
# Get the request type
request = data.split('\n')
request = data.decode('utf-8').split('\n')
line1 = request[0].split(' ')
requestType = line1[0]
# Get the media file name
Expand All @@ -55,7 +53,7 @@ def processRtspRequest(self, data):
if requestType == self.SETUP:
if self.state == self.INIT:
# Update state
print "SETUP Request received\n"
print ("SETUP Request received\n")

try:

Expand All @@ -70,37 +68,37 @@ def processRtspRequest(self, data):

# Send RTSP reply
self.replyRtsp(self.OK_200, seq[0]) #seq[0] the sequenceNum received from Client.py
print "sequenceNum is " + seq[0]
print ("sequenceNum is " + seq[0])
# Get the RTP/UDP port from the last line
self.clientInfo['rtpPort'] = request[2].split(' ')[3]
print '-'*60 + "\nrtpPort is :" + self.clientInfo['rtpPort'] + "\n" + '-'*60
print "filename is " + filename
print ('-'*60 + "\nrtpPort is :" + self.clientInfo['rtpPort'] + "\n" + '-'*60)
print ("filename is " + filename)

# Process PLAY request
elif requestType == self.PLAY:
if self.state == self.READY:
print '-'*60 + "\nPLAY Request Received\n" + '-'*60
print ('-'*60 + "\nPLAY Request Received\n" + '-'*60)
self.state = self.PLAYING

# Create a new socket for RTP/UDP
self.clientInfo["rtpSocket"] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

self.replyRtsp(self.OK_200, seq[0])
print '-'*60 + "\nSequence Number ("+ seq[0] + ")\nReplied to client\n" + '-'*60
print ('-'*60 + "\nSequence Number ("+ seq[0] + ")\nReplied to client\n" + '-'*60)

# Create a new thread and start sending RTP packets
self.clientInfo['event'] = threading.Event()
self.clientInfo['worker']= threading.Thread(target=self.sendRtp)
self.clientInfo['worker'].start()
# Process RESUME request
elif self.state == self.PAUSE:
print '-'*60 + "\nRESUME Request Received\n" + '-'*60
print('-'*60 + "\nRESUME Request Received\n" + '-'*60)
self.state = self.PLAYING

# Process PAUSE request
elif requestType == self.PAUSE:
if self.state == self.PLAYING:
print '-'*60 + "\nPAUSE Request Received\n" + '-'*60
print('-'*60 + "\nPAUSE Request Received\n" + '-'*60)
self.state = self.READY

self.clientInfo['event'].set()
Expand All @@ -109,7 +107,7 @@ def processRtspRequest(self, data):

# Process TEARDOWN request
elif requestType == self.TEARDOWN:
print '-'*60 + "\nTEARDOWN Request Received\n" + '-'*60
print ('-'*60 + "\nTEARDOWN Request Received\n" + '-'*60)

self.clientInfo['event'].set()

Expand Down Expand Up @@ -155,10 +153,10 @@ def sendRtp(self):
counter += 1
time.sleep(jit)
except:
print "Connection Error"
print '-'*60
print ("Connection Error")
print ('-'*60)
traceback.print_exc(file=sys.stdout)
print '-'*60
print ('-'*60)

def makeRtp(self, payload, frameNbr):
"""RTP-packetize the video data."""
Expand All @@ -183,10 +181,10 @@ def replyRtsp(self, code, seq):
#print "200 OK"
reply = 'RTSP/1.0 200 OK\nCSeq: ' + seq + '\nSession: ' + str(self.clientInfo['session'])
connSocket = self.clientInfo['rtspSocket'][0]
connSocket.send(reply)
connSocket.send(reply.encode('utf-8'))

# Error messages
elif code == self.FILE_NOT_FOUND_404:
print "404 NOT FOUND"
print ("404 NOT FOUND")
elif code == self.CON_ERR_500:
print "500 CONNECTION ERROR"
print ("500 CONNECTION ERROR")
Loading