try_play.py 1.65 KB
import shlex
import subprocess
import re
import time
import os
import threading

#config

keywords = [r'rtsp client working']
shell_cmd = './rtsp_decode_encode_test_app --url0=rtsp://127.0.0.1:8555/rtsp://127.0.0.1:18554/0 --serverport=18654'

starttm = time.time()
has_keyword = False
cmd = shlex.split(shell_cmd)
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f = open('./current.log', 'w')

def tryplay():
    global p
    global has_keyword
    while p.poll() is None:
        line = p.stdout.readline()
        f.write(line)
        line = line.strip()
        if line:
            for keyword in keywords:
                m = re.search(keyword, line)
                if(m != None):
                    has_keyword = True
                    print('play ok')
                    return True
    return True

if __name__ == '__main__':
    t = threading.Thread(target=tryplay)
    t.start()
    exitcode = -2
    while exitcode == -2 :
        print('has_keyword:{}'.format(has_keyword))
        if has_keyword == True:
            print('kill -9 {}'.format(p.pid))
            os.system('kill -9 {}'.format(p.pid))
            exitcode = 0;
            break
        now = time.time()
        if(now- starttm > 10):
            if(has_keyword == False):
                print('play failed')
                print('kill -9 {}'.format(p.pid))
                os.system('kill -9 {}'.format(p.pid))
                exitcode = -1
                break
        else:
            print('time.sleep(1)')
            time.sleep(1)

    print('breaked')
    t.join(timeout = 10)
    print('joined')
    f.close()
    print('fclosed')
    exit(exitcode)