forked from sugarlabs/fractionbounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aplay.py
72 lines (58 loc) · 2.23 KB
/
aplay.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
# Copyright (C) 2009, Aleksey Lim
# Copyright (C) 2018, James Cameron <quozl@laptop.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
from Queue import Queue
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
Gst.init(None)
class Aplay:
def __init__(self):
pipeline = Gst.ElementFactory.make('playbin', 'playbin')
pipeline.set_property(
"video-sink",
Gst.ElementFactory.make('fakesink', 'fakesink'))
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::eos', self._on_message_eos)
bus.connect('message::error', self._on_message_error)
self._pipeline = pipeline
self._queue = Queue()
def _dequeue(self):
if self._queue.empty():
return
name = self._queue.get()
self._pipeline.props.uri = 'file://' + name
self._pipeline.set_state(Gst.State.PLAYING)
def _on_message_eos(self, bus, message):
if self._pipeline:
self._pipeline.set_state(Gst.State.NULL)
self._dequeue()
def _on_message_error(self, bus, message):
err, debug = message.parse_error()
logging.error('%s %s', err, debug)
self._pipeline.set_state(Gst.State.NULL)
self._dequeue()
def play(self, name):
self._queue.put(name)
if self._pipeline:
if self._pipeline.get_state(Gst.CLOCK_TIME_NONE)[1] == Gst.State.NULL:
self._dequeue()
def close(self):
self._pipeline.set_state(Gst.State.NULL)
self._pipeline = None
aplay = Aplay()