Skip to content

Commit f5053bc

Browse files
committed
Seperating out example into its own file
1 parent c6a27bb commit f5053bc

File tree

3 files changed

+172
-157
lines changed

3 files changed

+172
-157
lines changed

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
# python-rtsp-client
1+
python-rtsp-client
2+
==================
3+
24
A basic rtsp client writen in pure python
35

4-
![GitHub issues](https://img.shields.io/github/issues/Yadro-Intra/python-rtsp-client.svg)
5-
![GitHub forks](https://img.shields.io/github/forks/Yadro-Intra/python-rtsp-client.svg)
6-
![GitHub stars](https://img.shields.io/github/stars/Yadro-Intra/python-rtsp-client.svg)
6+
Getting Started
7+
---------------
8+
9+
::
10+
from rtsp import RTSPClient
11+
myrtsp = RTSPClient(url='rtsp://username:password@hostname:port/path',callback=print)
12+
try:
13+
myrtsp.do_describe()
14+
except:
15+
myrtsp.do_teardown()
16+
717

8-
Usage: rtsp.py [options] url
18+
Examples
19+
--------
20+
Usage: setupandplay.py [options] url
921

1022
While running, you can control play by inputting "forward","backward","begin","live","pause"
1123
or "play" a with "range" and "scale" parameter, such as "play range:npt=beginning- scale:2"

examples/setupandplay.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
#-----------------------------------------------------------------------
3+
# Input with autocompletion
4+
#-----------------------------------------------------------------------
5+
import readline, sys
6+
sys.path.append('../')
7+
from rtsp import *
8+
from optparse import OptionParser
9+
COMMANDS = (
10+
'backward',
11+
'begin',
12+
'exit',
13+
'forward',
14+
'help',
15+
'live',
16+
'pause',
17+
'play',
18+
'range:',
19+
'scale:',
20+
'teardown',
21+
)
22+
23+
def complete(text, state):
24+
options = [i for i in COMMANDS if i.startswith(text)]
25+
return (state < len(options) and options[state]) or None
26+
27+
def input_cmd():
28+
readline.set_completer_delims(' \t\n')
29+
readline.parse_and_bind("tab: complete")
30+
readline.set_completer(complete)
31+
if(sys.version_info > (3, 0)):
32+
cmd = input(COLOR_STR('Input Command # ', CYAN))
33+
else:
34+
cmd = raw_input(COLOR_STR('Input Command # ', CYAN))
35+
PRINT('') # add one line
36+
return cmd
37+
#-----------------------------------------------------------------------
38+
39+
#--------------------------------------------------------------------------
40+
# Colored Output in Console
41+
#--------------------------------------------------------------------------
42+
DEBUG = False
43+
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA,CYAN,WHITE = list(range(90, 98))
44+
def COLOR_STR(msg, color=WHITE):
45+
return '\033[%dm%s\033[0m'%(color, msg)
46+
47+
def PRINT(msg, color=WHITE, out=sys.stdout):
48+
if DEBUG and out.isatty() :
49+
out.write(COLOR_STR(msg, color) + '\n')
50+
#--------------------------------------------------------------------------
51+
52+
def exec_cmd(rtsp, cmd):
53+
'''Execute the operation according to the command'''
54+
if cmd in ('exit', 'teardown'):
55+
rtsp.do_teardown()
56+
elif cmd == 'pause':
57+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=now-'
58+
rtsp.do_pause()
59+
elif cmd == 'help':
60+
PRINT(play_ctrl_help())
61+
elif cmd == 'forward':
62+
if rtsp.cur_scale < 0: rtsp.cur_scale = 1
63+
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
64+
elif cmd == 'backward':
65+
if rtsp.cur_scale > 0: rtsp.cur_scale = -1
66+
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
67+
elif cmd == 'begin':
68+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=beginning-'
69+
elif cmd == 'live':
70+
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=end-'
71+
elif cmd.startswith('play'):
72+
m = re.search(r'range[:\s]+(?P<range>[^\s]+)', cmd)
73+
if m: rtsp.cur_range = m.group('range')
74+
m = re.search(r'scale[:\s]+(?P<scale>[\d\.]+)', cmd)
75+
if m: rtsp.cur_scale = int(m.group('scale'))
76+
77+
if cmd not in ('pause', 'exit', 'teardown', 'help'):
78+
rtsp.do_play(rtsp.cur_range, rtsp.cur_scale)
79+
80+
def main(url, options):
81+
rtsp = RTSPClient(url, options.dest_ip, callback=PRINT)
82+
83+
if options.transport: rtsp.TRANSPORT_TYPE_LIST = options.transport.split(',')
84+
if options.client_port: rtsp.CLIENT_PORT_RANGE = options.client_port
85+
if options.nat: rtsp.NAT_IP_PORT = options.nat
86+
if options.arq: rtsp.ENABLE_ARQ = options.arq
87+
if options.fec: rtsp.ENABLE_FEC = options.fec
88+
89+
if options.ping:
90+
PRINT('PING START', YELLOW)
91+
rtsp.ping(0.1)
92+
PRINT('PING DONE', YELLOW)
93+
sys.exit(0)
94+
return
95+
96+
try:
97+
rtsp.do_describe()
98+
while rtsp.running:
99+
if rtsp.playing:
100+
cmd = input_cmd()
101+
exec_cmd(rtsp, cmd)
102+
# 302 redirect to re-establish chain
103+
if not rtsp.running and rtsp.location:
104+
rtsp = RTSPClient(rtsp.location)
105+
rtsp.do_describe()
106+
time.sleep(0.5)
107+
except KeyboardInterrupt:
108+
rtsp.do_teardown()
109+
print('\n^C received, Exit.')
110+
111+
def play_ctrl_help():
112+
help = COLOR_STR('In running, you can control play by input "forward"' \
113+
+', "backward", "begin", "live", "pause"\n', MAGENTA)
114+
help += COLOR_STR('or "play" with "range" and "scale" parameter, such ' \
115+
+'as "play range:npt=beginning- scale:2"\n', MAGENTA)
116+
help += COLOR_STR('You can input "exit", "teardown" or ctrl+c to ' \
117+
+'quit\n', MAGENTA)
118+
return help
119+
120+
if __name__ == '__main__':
121+
usage = COLOR_STR('%prog [options] url\n\n', GREEN) + play_ctrl_help()
122+
123+
parser = OptionParser(usage=usage)
124+
parser.add_option('-t', '--transport', dest='transport',
125+
default='rtp_over_udp',
126+
help='Set transport type when issuing SETUP: '
127+
+'ts_over_tcp, ts_over_udp, rtp_over_tcp, '
128+
+'rtp_over_udp[default]')
129+
parser.add_option('-d', '--dest_ip', dest='dest_ip',
130+
help='Set destination ip of udp data transmission, '
131+
+'default uses same ip as this rtsp client')
132+
parser.add_option('-p', '--client_port', dest='client_port',
133+
help='Set client port range when issuing SETUP of udp, '
134+
+'default is "10014-10015"')
135+
parser.add_option('-n', '--nat', dest='nat',
136+
help='Add "x-NAT" when issuing DESCRIBE, arg format '
137+
+'"192.168.1.100:20008"')
138+
parser.add_option('-r', '--arq', dest='arq', action="store_true",
139+
help='Add "x-Retrans:yes" when issuing DESCRIBE')
140+
parser.add_option('-f', '--fec', dest='fec', action="store_true",
141+
help='Add "x-zmssFecCDN:yes" when issuing DESCRIBE')
142+
parser.add_option('-P', '--ping', dest='ping', action="store_true",
143+
help='Just issue OPTIONS and exit.')
144+
145+
(options, args) = parser.parse_args()
146+
if len(args) < 1:
147+
parser.print_help()
148+
sys.exit()
149+
150+
url = args[0]
151+
152+
DEBUG = True
153+
main(url, options)
154+
# EOF #
155+

rtsp.py

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -410,155 +410,3 @@ def ping(self, timeout=0.01):
410410
time.sleep(timeout)
411411
self.close()
412412
return self.response
413-
414-
#-----------------------------------------------------------------------
415-
# Input with autocompletion
416-
#-----------------------------------------------------------------------
417-
import readline, sys
418-
from optparse import OptionParser
419-
COMMANDS = (
420-
'backward',
421-
'begin',
422-
'exit',
423-
'forward',
424-
'help',
425-
'live',
426-
'pause',
427-
'play',
428-
'range:',
429-
'scale:',
430-
'teardown',
431-
)
432-
433-
def complete(text, state):
434-
options = [i for i in COMMANDS if i.startswith(text)]
435-
return (state < len(options) and options[state]) or None
436-
437-
def input_cmd():
438-
readline.set_completer_delims(' \t\n')
439-
readline.parse_and_bind("tab: complete")
440-
readline.set_completer(complete)
441-
if(sys.version_info > (3, 0)):
442-
cmd = input(COLOR_STR('Input Command # ', CYAN))
443-
else:
444-
cmd = raw_input(COLOR_STR('Input Command # ', CYAN))
445-
PRINT('') # add one line
446-
return cmd
447-
#-----------------------------------------------------------------------
448-
449-
#--------------------------------------------------------------------------
450-
# Colored Output in Console
451-
#--------------------------------------------------------------------------
452-
DEBUG = False
453-
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA,CYAN,WHITE = list(range(90, 98))
454-
def COLOR_STR(msg, color=WHITE):
455-
return '\033[%dm%s\033[0m'%(color, msg)
456-
457-
def PRINT(msg, color=WHITE, out=sys.stdout):
458-
if DEBUG and out.isatty() :
459-
out.write(COLOR_STR(msg, color) + '\n')
460-
#--------------------------------------------------------------------------
461-
462-
def exec_cmd(rtsp, cmd):
463-
'''Execute the operation according to the command'''
464-
if cmd in ('exit', 'teardown'):
465-
rtsp.do_teardown()
466-
elif cmd == 'pause':
467-
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=now-'
468-
rtsp.do_pause()
469-
elif cmd == 'help':
470-
PRINT(play_ctrl_help())
471-
elif cmd == 'forward':
472-
if rtsp.cur_scale < 0: rtsp.cur_scale = 1
473-
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
474-
elif cmd == 'backward':
475-
if rtsp.cur_scale > 0: rtsp.cur_scale = -1
476-
rtsp.cur_scale *= 2; rtsp.cur_range = 'npt=now-'
477-
elif cmd == 'begin':
478-
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=beginning-'
479-
elif cmd == 'live':
480-
rtsp.cur_scale = 1; rtsp.cur_range = 'npt=end-'
481-
elif cmd.startswith('play'):
482-
m = re.search(r'range[:\s]+(?P<range>[^\s]+)', cmd)
483-
if m: rtsp.cur_range = m.group('range')
484-
m = re.search(r'scale[:\s]+(?P<scale>[\d\.]+)', cmd)
485-
if m: rtsp.cur_scale = int(m.group('scale'))
486-
487-
if cmd not in ('pause', 'exit', 'teardown', 'help'):
488-
rtsp.do_play(rtsp.cur_range, rtsp.cur_scale)
489-
490-
def main(url, options):
491-
rtsp = RTSPClient(url, options.dest_ip, callback=PRINT)
492-
493-
if options.transport: rtsp.TRANSPORT_TYPE_LIST = options.transport.split(',')
494-
if options.client_port: rtsp.CLIENT_PORT_RANGE = options.client_port
495-
if options.nat: rtsp.NAT_IP_PORT = options.nat
496-
if options.arq: rtsp.ENABLE_ARQ = options.arq
497-
if options.fec: rtsp.ENABLE_FEC = options.fec
498-
499-
if options.ping:
500-
PRINT('PING START', YELLOW)
501-
rtsp.ping(0.1)
502-
PRINT('PING DONE', YELLOW)
503-
sys.exit(0)
504-
return
505-
506-
try:
507-
rtsp.do_describe()
508-
while rtsp.running:
509-
if rtsp.playing:
510-
cmd = input_cmd()
511-
exec_cmd(rtsp, cmd)
512-
# 302 redirect to re-establish chain
513-
if not rtsp.running and rtsp.location:
514-
rtsp = RTSPClient(rtsp.location)
515-
rtsp.do_describe()
516-
time.sleep(0.5)
517-
except KeyboardInterrupt:
518-
rtsp.do_teardown()
519-
print('\n^C received, Exit.')
520-
521-
def play_ctrl_help():
522-
help = COLOR_STR('In running, you can control play by input "forward"' \
523-
+', "backward", "begin", "live", "pause"\n', MAGENTA)
524-
help += COLOR_STR('or "play" with "range" and "scale" parameter, such ' \
525-
+'as "play range:npt=beginning- scale:2"\n', MAGENTA)
526-
help += COLOR_STR('You can input "exit", "teardown" or ctrl+c to ' \
527-
+'quit\n', MAGENTA)
528-
return help
529-
530-
if __name__ == '__main__':
531-
usage = COLOR_STR('%prog [options] url\n\n', GREEN) + play_ctrl_help()
532-
533-
parser = OptionParser(usage=usage)
534-
parser.add_option('-t', '--transport', dest='transport',
535-
default='rtp_over_udp',
536-
help='Set transport type when issuing SETUP: '
537-
+'ts_over_tcp, ts_over_udp, rtp_over_tcp, '
538-
+'rtp_over_udp[default]')
539-
parser.add_option('-d', '--dest_ip', dest='dest_ip',
540-
help='Set destination ip of udp data transmission, '
541-
+'default uses same ip as this rtsp client')
542-
parser.add_option('-p', '--client_port', dest='client_port',
543-
help='Set client port range when issuing SETUP of udp, '
544-
+'default is "10014-10015"')
545-
parser.add_option('-n', '--nat', dest='nat',
546-
help='Add "x-NAT" when issuing DESCRIBE, arg format '
547-
+'"192.168.1.100:20008"')
548-
parser.add_option('-r', '--arq', dest='arq', action="store_true",
549-
help='Add "x-Retrans:yes" when issuing DESCRIBE')
550-
parser.add_option('-f', '--fec', dest='fec', action="store_true",
551-
help='Add "x-zmssFecCDN:yes" when issuing DESCRIBE')
552-
parser.add_option('-P', '--ping', dest='ping', action="store_true",
553-
help='Just issue OPTIONS and exit.')
554-
555-
(options, args) = parser.parse_args()
556-
if len(args) < 1:
557-
parser.print_help()
558-
sys.exit()
559-
560-
url = args[0]
561-
562-
DEBUG = True
563-
main(url, options)
564-
# EOF #

0 commit comments

Comments
 (0)