-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathaudio_server.py
289 lines (240 loc) · 7.96 KB
/
audio_server.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
# encoding: utf-8
# Copyright 2014 Xinyu, He <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# alsa settings refer to:
# https://gist.github.com/legendmohe/83ba17c1e9b9c46480d2
import threading
import subprocess
import os
import signal
from Queue import Queue
from time import sleep
import argparse
import tornado.ioloop
import tornado.web
import alsaaudio
from util.log import *
SOUNDCARD_NAME = u'ALSA'
class RETURNCODE:
SUCCESS = '1'
ERROR = '2'
FAIL = '3'
EMPTY = '4'
NO_RES = '5'
class StopHandler(tornado.web.RequestHandler):
def get(self):
url = self.get_argument("url", None)
if url is None or url == "":
INFO("url is empty")
self.write(str(RETURNCODE.EMPTY))
return
if stop_audio(url):
self.write(RETURNCODE.FAIL)
else:
self.write(RETURNCODE.SUCCESS)
class ClearQeueuHandler(tornado.web.RequestHandler):
def get(self):
clear_audio_queue()
self.write(RETURNCODE.SUCCESS)
class VolumeHandler(tornado.web.RequestHandler):
def initialize(self):
cards = alsaaudio.cards()
INFO(cards)
card_idx = cards.index(SOUNDCARD_NAME)
self._m = alsaaudio.Mixer(control='PCM', cardindex=card_idx)
INFO("use card %d." % card_idx)
def get(self):
DEBUG(u"正在获取音量值")
try:
volumes = self._m.getvolume()
INFO("get volumes:%s" % volumes)
volume = str(int(volumes[0]))
DEBUG(u"当前音量值为:%s" % volume)
self.write(volume)
except Exception, e:
ERROR(e)
self.write("")
def post(self):
v_str = self.get_argument("v", default=None, strip=False)
if v_str is None:
self.write("-1")
WARN(u"请输入音量值")
return
try:
DEBUG("set volume:%s" % v_str)
volume = int(v_str)
except ValueError:
try:
volume = float(v_str)
self.write("-2")
ERROR(u"音量值必须为整数")
return
except ValueError:
volume = -1
if volume == -1:
self.write("-3")
ERROR(u"音量值无效:%s" % msg)
return
self._m.setvolume(volume)
INFO(u"设置音量值为:%s" % str(volume))
self.write(RETURNCODE.SUCCESS)
class PlayHandler(tornado.web.RequestHandler):
def get(self):
url = self.get_argument("url", None)
if url is None or url == "":
INFO("url is empty")
self.write(str(RETURNCODE.EMPTY))
return
is_inqueue = self.get_argument("inqueue", None)
channel = self.get_argument("channel", "default")
loop = self.get_argument("loop", None)
if is_inqueue is None:
if loop is None:
play_audio(url, channel)
else:
play_audio(url, channel, int(loop))
else:
if loop is None:
play_audio_inqueue(url, channel)
else:
play_audio_inqueue(url, channel, int(loop))
self.write(str(RETURNCODE.SUCCESS))
# ============== functions ============
def worker(play_url, channel, loop):
global mp_context, mixer_normal, mixer_notice
cmd = [
'sudo',
'mplayer',
'-ao', 'alsa:device=%s' % channel,
play_url,
'-loop', str(loop)]
INFO("play cmd:%s" % cmd)
nor_vol = mixer_normal.getvolume()[0]
if channel == 'notice':
mixer_normal.setvolume(int(nor_vol*0.8))
with open(os.devnull, 'w') as tempf:
player = subprocess.Popen(cmd, stdout=tempf, stderr=tempf)
mp_context[play_url] = player
print "player create: " + str(player.pid)
player.communicate()
if play_url in mp_context:
del mp_context[play_url]
print "play finished:%s" % (play_url,)
mixer_normal.setvolume(nor_vol)
def play_audio(url, channel='default', loop=1):
global mp_context
if url in mp_context:
mp = mp_context[url]
if not mp is None: # for thread-safe
mp.terminate()
t = threading.Thread(target=worker, args=(url, channel, loop))
t.setDaemon(True)
t.start()
return True
def play_audio_inqueue(url, channel='default', loop=1):
global mp_queue
mp_queue.put((url, channel, loop))
INFO("%s was added to queue." % (url,))
def stop_audio(url):
global mp_context
if not url in mp_context:
WARN("%s is not playing" % (url, ))
return False
else:
mp = mp_context[url]
if not mp is None: # for thread-safe
mp.terminate()
if url in mp_context:
del mp_context[url]
return True
def clear_audio_queue():
global mp_context
global mp_queue
with mp_queue.mutex:
mp_queue.queue.clear()
if "queue" in mp_context:
mp_context["queue"].terminate()
del mp_context["queue"]
def queue_worker():
global mp_context, mixer_normal, mixer_notice
global mp_queue
while True:
url, channel, loop = mp_queue.get()
print "get from queue:%s \n channel:%s" % (str(url), channel)
cmd = [
'sudo',
'mplayer',
'-ao', 'alsa:device=%s' % channel,
url,
'-loop', str(loop)]
# print cmd
INFO("queue play cmd:%s" % cmd)
nor_vol = mixer_normal.getvolume()[0]
if channel == 'notice':
# INFO("set nor_vol to 20")
mixer_normal.setvolume(int(nor_vol*0.8))
with open(os.devnull, 'w') as tempf:
player = subprocess.Popen(cmd, stdout=tempf, stderr=tempf)
mp_context["queue"] = player
player.communicate()
print url + u" stopped."
if "queue" in mp_context:
del mp_context["queue"]
mixer_normal.setvolume(nor_vol)
mp_queue.task_done()
sleep(1)
def init_queue_player():
t = threading.Thread(target=queue_worker)
t.setDaemon(True)
t.start()
mp_context = {}
mp_queue = Queue()
mixer_normal = alsaaudio.Mixer(control='chan_norl_amp')
mixer_notice = alsaaudio.Mixer(control='chan_noti_amp')
# http://stackoverflow.com/questions/17101502/how-to-stop-the-tornado-web-server-with-ctrlc
is_closing = False
def signal_handler(signum, frame):
global is_closing
is_closing = True
def try_exit():
global is_closing, mp_context
if is_closing:
# clean up here
tornado.ioloop.IOLoop.instance().stop()
logging.info('exit success')
for url in mp_context:
mp = mp_context[url]
mp.terminate()
application = tornado.web.Application([
(r"/play", PlayHandler),
(r"/clear", ClearQeueuHandler),
(r"/stop", StopHandler),
(r"/volume", VolumeHandler),
])
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='audio_server.py -p port')
parser.add_argument('-p',
action="store",
dest="port",
default="8001",
)
port = parser.parse_args().port
INFO("bind to %s " % (port))
signal.signal(signal.SIGINT, signal_handler)
application.listen(port)
init_queue_player()
tornado.ioloop.PeriodicCallback(try_exit, 100).start()
tornado.ioloop.IOLoop.instance().start()