Skip to content

Commit 3aae2fa

Browse files
committed
Moving globabl variables related to stream into class
1 parent 1cbd8e4 commit 3aae2fa

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

rtsp.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,19 @@
3434
#x-notice in ANNOUNCE, BOS-Begin of Stream, EOS-End of Stream
3535
X_NOTICE_EOS, X_NOTICE_BOS, X_NOTICE_CLOSE = 2101, 2102, 2103
3636

37-
#### Variables ####
38-
CUR_RANGE = 'npt=end-'
39-
CUR_SCALE = 1
40-
TRANSPORT_TYPE_LIST = []
41-
NAT_IP_PORT = ''
42-
ENABLE_ARQ = False
43-
ENABLE_FEC = False
44-
PING = False
45-
HEARTBEAT_INTERVAL = 10 # 10s
46-
CLIENT_PORT_RANGE = '10014-10015'
47-
4837
class RTSPError(Exception): pass
4938
class RTSPURLError(RTSPError): pass
5039
class RTSPNetError(RTSPError): pass
5140

5241
class RTSPClient(threading.Thread):
42+
TRANSPORT_TYPE_LIST = []
43+
NAT_IP_PORT = ''
44+
ENABLE_ARQ = False
45+
ENABLE_FEC = False
46+
HEARTBEAT_INTERVAL = 10 # 10s
47+
CLIENT_PORT_RANGE = '10014-10015'
48+
5349
def __init__(self, url, dest_ip='', callback=None):
54-
global CUR_RANGE
5550
threading.Thread.__init__(self)
5651
self._auth = None
5752
self._callback = callback or (lambda x: x)
@@ -66,13 +61,15 @@ def __init__(self, url, dest_ip='', callback=None):
6661
self._parsed_url.path
6762
self._session_id = ''
6863
self._sock = None
64+
self.cur_range = 'npt=end-'
65+
self.cur_scale = 1
6966
self.location = ''
7067
self.playing = False
7168
self.response = None
7269
self.response_buf = []
7370
self.running = True
7471
if '.sdp' not in self._parsed_url.path.lower():
75-
CUR_RANGE = 'npt=0.00000-' # On demand starts from the beginning
72+
self.cur_range = 'npt=0.00000-' # On demand starts from the beginning
7673
self._connect_server()
7774
self._update_dest_ip()
7875
self.closed = False
@@ -259,20 +256,19 @@ def _process_response(self, msg):
259256
self.do_setup(track_id_str)
260257
elif self._cseq_map[rsp_cseq] == 'SETUP':
261258
self._session_id = headers['session']
262-
self.do_play(CUR_RANGE, CUR_SCALE)
259+
self.do_play(self.cur_range, self.cur_scale)
263260
self.send_heart_beat_msg()
264261
elif self._cseq_map[rsp_cseq] == 'PLAY':
265262
self.playing = True
266263

267264
def _process_announce(self, msg):
268265
'''Processes the ANNOUNCE notification message'''
269-
global CUR_RANGE, CUR_SCALE
270266
self._callback(msg)
271267
headers = self._parse_header_params(msg.splitlines()[1:])
272268
x_notice_val = int(headers['x-notice'])
273269
if x_notice_val in (X_NOTICE_EOS, X_NOTICE_BOS):
274-
CUR_SCALE = 1
275-
self.do_play(CUR_RANGE, CUR_SCALE)
270+
self.cur_scale = 1
271+
self.do_play(self.cur_range, self.cur_scale)
276272
elif x_notice_val == X_NOTICE_CLOSE:
277273
self.do_teardown()
278274

@@ -328,26 +324,28 @@ def _get_transport_type(self):
328324
transport_str = ''
329325
ip_type = 'unicast' #TODO: if IPAddress(DEST_IP).is_unicast()
330326
# else 'multicast'
331-
for t in TRANSPORT_TYPE_LIST:
327+
for t in self.TRANSPORT_TYPE_LIST:
332328
if t not in TRANSPORT_TYPE_MAP:
333329
raise RTSPError('Error param: %s' % t)
334330
if t.endswith('tcp'):
335-
transport_str += TRANSPORT_TYPE_MAP[t]%ip_type
331+
transport_str +=TRANSPORT_TYPE_MAP[t]%ip_type
336332
else:
337-
transport_str += TRANSPORT_TYPE_MAP[t]%(ip_type,
338-
self._dest_ip,
339-
CLIENT_PORT_RANGE)
333+
transport_str +=TRANSPORT_TYPE_MAP[t]%(ip_type,
334+
self._dest_ip,
335+
self.CLIENT_PORT_RANGE)
340336
return transport_str
341337

342338
def do_describe(self, headers={}):
343339
if self._auth:
344340
headers['Authorization'] = self._auth
345341
headers['Accept'] = 'application/sdp'
346-
if ENABLE_ARQ:
342+
if self.ENABLE_ARQ:
347343
headers['x-Retrans'] = 'yes'
348344
headers['x-Burst'] = 'yes'
349-
if ENABLE_FEC: headers['x-zmssFecCDN'] = 'yes'
350-
if NAT_IP_PORT: headers['x-NAT'] = NAT_IP_PORT
345+
if self.ENABLE_FEC:
346+
headers['x-zmssFecCDN'] = 'yes'
347+
if self.NAT_IP_PORT:
348+
headers['x-NAT'] = self.NAT_IP_PORT
351349
self._sendmsg('DESCRIBE', self._orig_url, headers)
352350

353351
def do_setup(self, track_id_str='', headers={}):
@@ -404,7 +402,7 @@ def send_heart_beat_msg(self):
404402
'''Timed sending GET_PARAMETER message keep alive'''
405403
if not self.running:
406404
self.do_get_parameter()
407-
threading.Timer(HEARTBEAT_INTERVAL,
405+
threading.Timer(self.HEARTBEAT_INTERVAL,
408406
self.send_heart_beat_msg).start()
409407

410408
def ping(self, timeout=0.01):
@@ -464,36 +462,41 @@ def PRINT(msg, color=WHITE, out=sys.stdout):
464462

465463
def exec_cmd(rtsp, cmd):
466464
'''Execute the operation according to the command'''
467-
global CUR_RANGE, CUR_SCALE
468465
if cmd in ('exit', 'teardown'):
469466
rtsp.do_teardown()
470467
elif cmd == 'pause':
471-
CUR_SCALE = 1; CUR_RANGE = 'npt=now-'
468+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=now-'
472469
rtsp.do_pause()
473470
elif cmd == 'help':
474471
PRINT(play_ctrl_help())
475472
elif cmd == 'forward':
476-
if CUR_SCALE < 0: CUR_SCALE = 1
477-
CUR_SCALE *= 2; CUR_RANGE = 'npt=now-'
473+
if rtsp.cur_scale < 0: rtsp.cur_scale = 1
474+
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
478475
elif cmd == 'backward':
479-
if CUR_SCALE > 0: CUR_SCALE = -1
480-
CUR_SCALE *= 2; CUR_RANGE = 'npt=now-'
476+
if rtsp.cur_scale > 0: rtsp.cur_scale = -1
477+
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
481478
elif cmd == 'begin':
482-
CUR_SCALE = 1; CUR_RANGE = 'npt=beginning-'
479+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=beginning-'
483480
elif cmd == 'live':
484-
CUR_SCALE = 1; CUR_RANGE = 'npt=end-'
481+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=end-'
485482
elif cmd.startswith('play'):
486483
m = re.search(r'range[:\s]+(?P<range>[^\s]+)', cmd)
487-
if m: CUR_RANGE = m.group('range')
484+
if m: rtsp.cur_range = m.group('range')
488485
m = re.search(r'scale[:\s]+(?P<scale>[\d\.]+)', cmd)
489-
if m: CUR_SCALE = int(m.group('scale'))
486+
if m: rtsp.cur_scale = int(m.group('scale'))
490487

491488
if cmd not in ('pause', 'exit', 'teardown', 'help'):
492-
rtsp.do_play(CUR_RANGE, CUR_SCALE)
489+
rtsp.do_play(rtsp.cur_range, rtsp.cur_scale)
493490

494491
def main(url, options):
495492
rtsp = RTSPClient(url, options.dest_ip, callback=PRINT)
496493

494+
if options.transport: rtsp.TRANSPORT_TYPE_LIST = options.transport.split(',')
495+
if options.client_port: rtsp.CLIENT_PORT_RANGE = options.client_port
496+
if options.nat: rtsp.NAT_IP_PORT = options.nat
497+
if options.arq: rtsp.ENABLE_ARQ = options.arq
498+
if options.fec: rtsp.ENABLE_FEC = options.fec
499+
497500
if options.ping:
498501
PRINT('PING START', YELLOW)
499502
rtsp.ping()
@@ -554,13 +557,6 @@ def play_ctrl_help():
554557
parser.print_help()
555558
sys.exit()
556559

557-
if options.transport: TRANSPORT_TYPE_LIST = options.transport.split(',')
558-
if options.client_port: CLIENT_PORT_RANGE = options.client_port
559-
if options.nat: NAT_IP_PORT = options.nat
560-
if options.arq: ENABLE_ARQ = options.arq
561-
if options.fec: ENABLE_FEC = options.fec
562-
if options.ping: PING = options.ping
563-
564560
url = args[0]
565561

566562
DEBUG = True

0 commit comments

Comments
 (0)