@@ -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