Debian Codegraph @

xl/player/gst/dynamic_sink.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 # Copyright (C) 2013-2015 Dustin Spicuzza
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #
18 #
19 # The developers of the Exaile media player hereby grant permission
20 # for non-GPL compatible GStreamer and Exaile plugins to be used and
21 # distributed together with GStreamer and Exaile. This permission is
22 # above and beyond the permissions granted by the GPL license by which
23 # Exaile is covered. If you modify this code, you may extend this
24 # exception to your version of the code, but you are not obligated to
25 # do so. If you do not wish to do so, delete this exception statement
26 # from your version.
27
28
29 import threading
30
31 from gi.repository import Gst
32 from gi.repository import GLib
33
34 import logging
35
36 logger = logging.getLogger(__name__)
37
38
39 class DynamicAudioSink(Gst.Bin):
40 """
41 An audio sink that can dynamically switch its output
42
43 TODO: When switching outputs rapidly, sometimes it tends to seek
44 ahead quite a bit. Not sure why.
45 """
46
47 def __init__(self, name):
48 Gst.Bin.__init__(self, name=name)
49
50 self.audio_sink = None
51 self.__audio_sink_lock = threading.Lock()
52
53 # Create an identity object so we don't need to deal with linking
54 # the audio sink with anything external to this bin
55 self.identity = Gst.ElementFactory.make('identity', None)
56 self.identity.props.signal_handoffs = False
57 self.add(self.identity)
58
59 # Create a ghost sink pad so this bin appears to be an audio sink
60 sinkpad = self.identity.get_static_pad("sink")
61 self.add_pad(Gst.GhostPad.new('sink', sinkpad))
62
63 def reconfigure(self, audio_sink):
64 # don't try to switch more than one source at a time
65 release_lock = True
66 self.__audio_sink_lock.acquire()
67
68 try:
69 # If this is the first time we added a sink, just add it to
70 # the pipeline and we're done.
71
72 if not self.audio_sink:
73 self._add_audiosink(audio_sink, None)
74 return
75
76 old_audio_sink = self.audio_sink
77
78 # Ok, time to replace the old sink. If it's not in a playing state,
79 # then this isn't so bad.
80
81 # if we don't use the timeout, when we set it to READY, it may be performing
82 # an async wait for PAUSE, so we use the timeout here.
83
84 state = old_audio_sink.get_state(timeout=50 * Gst.MSECOND)[1]
85
86 if state != Gst.State.PLAYING:
87 buffer_position = None
88
89 if state != Gst.State.NULL:
90 try:
91 buffer_position = old_audio_sink.query_position(Gst.Format.TIME)
92 except Exception:
93 pass
94
95 self.remove(old_audio_sink)
96 old_audio_sink.set_state(Gst.State.NULL)
97
98 # Then add the new sink
99 self._add_audiosink(audio_sink, buffer_position)
100
101 return
102
103 #
104 # Otherwise, disconnecting the old device is a bit complex. Code is
105 # derived from algorithm/code described at the following link:
106 #
107 # https://gstreamer.freedesktop.org/documentation/application-development/advanced/pipeline-manipulation.html
108 #
109
110 # Start off by blocking the src pad of the prior element
111 spad = old_audio_sink.get_static_pad('sink').get_peer()
112 spad.add_probe(
113 Gst.PadProbeType.BLOCK_DOWNSTREAM, self._pad_blocked_cb, audio_sink
114 )
115
116 # Don't release the lock until pad block is done
117 release_lock = False
118
119 finally:
120 if release_lock:
121 self.__audio_sink_lock.release()
122
123 def _pad_blocked_cb(self, pad, info, new_audio_sink):
124 pad.remove_probe(info.id)
125
126 old_audio_sink = self.audio_sink
127 buffer_position = old_audio_sink.query_position(Gst.Format.TIME)
128
129 # No data is flowing at this point. Unlink the element, add the new one
130 self.remove(old_audio_sink)
131
132 def _flush_old_sink():
133 old_audio_sink.set_state(Gst.State.NULL)
134
135 GLib.timeout_add(2000, _flush_old_sink)
136
137 # Add the new element
138 self._add_audiosink(new_audio_sink, buffer_position)
139
140 self.__audio_sink_lock.release()
141
142 # And drop the probe, which will cause data to flow again
143 return Gst.PadProbeReturn.DROP
144
145 def _add_audiosink(self, audio_sink, buffer_position):
146 '''Sets up the new audiosink and syncs it'''
147
148 self.add(audio_sink)
149 audio_sink.sync_state_with_parent()
150 self.identity.link(audio_sink)
151
152 if buffer_position is not None:
153 # buffer position is the output from get_position. If set, we
154 # seek to that position.
155
156 # TODO: this actually seems to skip ahead a tiny bit. why?
157
158 # Note! this is super important in paused mode too, because when
159 # we switch the sinks around the new sink never goes into
160 # the paused state because there's no buffer. This forces
161 # a resync of the buffer, so things still work.
162
163 seek_event = Gst.Event.new_seek(
164 1.0,
165 Gst.Format.TIME,
166 Gst.SeekFlags.FLUSH,
167 Gst.SeekType.SET,
168 buffer_position[1],
169 Gst.SeekType.NONE,
170 0,
171 )
172
173 self.send_event(seek_event)
174
175 self.audio_sink = audio_sink
176
177
178 # vim: et sts=4 sw=4