forked from magnetikonline/docker-acestream-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playstream.py
executable file
·191 lines (153 loc) · 4.27 KB
/
playstream.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
import argparse
import hashlib
import json
import re
import signal
import subprocess
import sys
import time
import urllib2
DEFAULT_SERVER_HOSTNAME = '127.0.0.1'
DEFAULT_SERVER_PORT = 6878
SERVER_POLL_TIME = 2
SERVER_STATUS_STREAM_ACTIVE = 'dl'
def exit_error(message):
sys.stderr.write('Error: {0}\n'.format(message))
sys.exit(1)
class WatchSigint(object):
_sent = None
def __init__(self):
if (WatchSigint._sent is None):
# install handler
WatchSigint._sent = False
signal.signal(signal.SIGINT,self._handler)
def _handler(self,signal,frame):
# Ctrl-C (SIGINT) sent to process
WatchSigint._sent = True
def sent(self):
return WatchSigint._sent
def read_arguments():
# create parser
parser = argparse.ArgumentParser(
description =
'Instructs server to commence a given program ID. '
'Will optionally execute a local media player once playback has started.'
)
parser.add_argument(
'--ace-stream-pid',
help = 'program ID to stream',
metavar = 'HASH',
required = True
)
parser.add_argument(
'--player',
help = 'media player to execute once stream active',
)
parser.add_argument(
'--progress',
action = 'store_true',
help = 'continue to output stream statistics (connected peers/transfer rates) every {0} seconds'.format(SERVER_POLL_TIME)
)
parser.add_argument(
'--server',
default = DEFAULT_SERVER_HOSTNAME,
help = 'server hostname, defaults to %(default)s',
metavar = 'HOSTNAME'
)
parser.add_argument(
'--port',
default = DEFAULT_SERVER_PORT,
help = 'server HTTP API port, defaults to %(default)s',
)
arg_list = parser.parse_args()
if (not re.search(r'^[a-f0-9]{40}$',arg_list.ace_stream_pid)):
exit_error('invalid stream program ID of [{0}] given'.format(arg_list.ace_stream_pid))
# return arguments
return (
arg_list.ace_stream_pid,
arg_list.player,
arg_list.progress,
arg_list.server,
arg_list.port
)
def api_request(uri):
response = urllib2.urlopen(uri)
return json.loads(response.read()).get('response',{})
def start_stream(server_hostname,server_port,stream_pid):
# build stream UID from PID
stream_uid = hashlib.sha1(stream_pid).hexdigest()
# call API to commence stream
response = api_request('http://{0}:{1}/ace/getstream?format=json&sid={2}&id={3}'.format(
server_hostname,
server_port,
stream_uid,
stream_pid
))
# return statistics API endpoint and HTTP video stream URLs
return (
response['stat_url'],
response['playback_url']
)
def stream_stats_message(response):
return 'Peers: {0} // Down: {1}KB // Up: {2}KB'.format(
response.get('peers',0),
response.get('speed_down',0),
response.get('speed_up',0)
)
def await_playback(watch_sigint,statistics_url):
while (True):
response = api_request(statistics_url)
if (response.get('status') == SERVER_STATUS_STREAM_ACTIVE):
# stream is ready
print('Ready!\n')
return True
if (watch_sigint.sent()):
# user sent SIGINT, exit now
print('\nExit!')
return False
# pause and check again
print('Waiting... [{0}]'.format(stream_stats_message(response)))
time.sleep(SERVER_POLL_TIME)
def execute_media_player(media_player_bin,playback_url):
subprocess.Popen(
media_player_bin.split() + [playback_url],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
def stream_progress(watch_sigint,statistics_url):
print('')
while (True):
print('Streaming... [{0}]'.format(
stream_stats_message(api_request(statistics_url))
))
if (watch_sigint.sent()):
# user sent SIGINT, exit now
print('\nExit!')
return
time.sleep(SERVER_POLL_TIME)
def main():
# read CLI arguments
(
stream_pid,
media_player_bin,
progress_follow,
server_hostname,
server_port
) = read_arguments()
# create Ctrl-C watcher
watch_sigint = WatchSigint()
print('Connecting to program ID [{0}]'.format(stream_pid))
statistics_url,playback_url = start_stream(server_hostname,server_port,stream_pid)
print('Awaiting successful connection to stream')
if (not await_playback(watch_sigint,statistics_url)):
# exit early
return
print('Playback available at [{0}]'.format(playback_url))
if (media_player_bin is not None):
print('Starting media player...')
execute_media_player(media_player_bin,playback_url)
if (progress_follow):
stream_progress(watch_sigint,statistics_url)
if (__name__ == '__main__'):
main()