try_play.py
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)