/
engine_normal.py
267 lines (227 loc) · 8.77 KB
/
engine_normal.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
# Copyright (C) 2008-2009 Adam Olsen
#
# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# The developers of the Exaile media player hereby grant permission
# for non-GPL compatible GStreamer and Exaile plugins to be used and
# distributed together with GStreamer and Exaile. This permission is
# above and beyond the permissions granted by the GPL license by which
# Exaile is covered. If you modify this code, you may extend this
# exception to your version of the code, but you are not obligated to
# do so. If you do not wish to do so, delete this exception statement
# from your version.
import logging
import time
import urllib
import urlparse
import gst, gobject
from xl.nls import gettext as _
from xl import common, event
from xl.player import pipe, _base
logger = logging.getLogger(__name__)
class NormalPlayer(_base.ExailePlayer):
def __init__(self):
_base.ExailePlayer.__init__(self,
pre_elems=[pipe.ProviderBin("stream_element")])
self._current = None
self.playbin = None
self.bus = None
self.fakevideo = gst.element_factory_make("fakesink")
self.fakevideo.set_property("sync", True)
self.setup_pipe()
def setup_pipe(self):
self.setup_playbin()
self.setup_bus()
self.setup_gst_elements()
def setup_playbin(self):
"""
setup the playbin to use for playback
"""
self.playbin = gst.element_factory_make("playbin", "player")
def setup_bus(self):
"""
setup the gstreamer message bus and callacks
"""
self.bus = self.playbin.get_bus()
self.bus.add_signal_watch()
self.bus.enable_sync_message_emission()
self.bus.connect('message', self.on_message)
def setup_gst_elements(self):
"""
sets up additional gst elements
"""
self.playbin.set_property("audio-sink", self.mainbin)
self.playbin.set_property("video-sink", self.fakevideo)
def eof_func(self, *args):
"""
called at the end of a stream
"""
self._queue.next()
def on_message(self, bus, message, reading_tag = False):
"""
Called when a message is received from gstreamer
"""
if message.type == gst.MESSAGE_TAG and self.tag_func:
self.tag_func(message.parse_tag())
if not self.current.get_tag_raw('__length'):
try:
duration = float(self.playbin.query_duration(
gst.FORMAT_TIME, None)[0])/1000000000
if duration > 0:
self.current.set_tag_raw('__length', duration)
except gst.QueryError:
logger.error("Couldn't query duration")
elif message.type == gst.MESSAGE_EOS and not self.is_paused():
self.eof_func()
elif message.type == gst.MESSAGE_ERROR:
logger.error("%s %s" %(message, dir(message)) )
a = message.parse_error()[0]
gobject.idle_add(self._on_playback_error, a.message)
# TODO: merge this into stop() and make it engine-agnostic somehow
curr = self.current
self._current = None
self.playbin.set_state(gst.STATE_NULL)
self.setup_pipe()
event.log_event("playback_track_end", self, curr)
event.log_event("playback_player_end", self, curr)
elif message.type == gst.MESSAGE_BUFFERING:
percent = message.parse_buffering()
if not percent < 100:
logger.info('Buffering complete')
if percent % 5 == 0:
event.log_event('playback_buffering', self, percent)
return True
def _get_current(self):
return self._current
def _get_gst_state(self):
"""
Returns the raw GStreamer state
"""
return self.playbin.get_state(timeout=50*gst.MSECOND)[1]
def get_position(self):
"""
Gets the current playback position of the playing track
"""
if self.is_paused(): return self._last_position
try:
self._last_position = \
self.playbin.query_position(gst.FORMAT_TIME)[0]
except gst.QueryError:
self._last_position = 0
return self._last_position
def update_playtime(self):
"""
updates the total playtime for the currently playing track
"""
if self.current and self._playtime_stamp:
last = self.current.get_tag_raw('__playtime')
if type(last) == str:
try:
last = int(last)
except:
last = 0
elif type(last) != int:
last = 0
self.current.set_tag_raw('__playtime', last + int(time.time() - \
self._playtime_stamp))
self._playtime_stamp = None
def reset_playtime_stamp(self):
self._playtime_stamp = int(time.time())
def __notify_source(self, *args):
# this is for handling multiple CD devices properly
source = self.playbin.get_property('source')
device = self.current.get_loc_for_io().split("#")[-1]
source.set_property('device', device)
self.playbin.disconnect(self.notify_id)
def play(self, track):
"""
plays the specified track, overriding any currently playing track
if the track cannot be played, playback stops completely
"""
if track is None:
self.stop()
return False
else:
self.stop(fire=False)
playing = self.is_playing()
self._current = track
uri = track.get_loc_for_io()
logger.info("Playing %s" % uri)
self.reset_playtime_stamp()
self.playbin.set_property("uri", uri)
if urlparse.urlsplit(uri)[0] == "cdda":
self.notify_id = self.playbin.connect('notify::source',
self.__notify_source)
self.playbin.set_state(gst.STATE_PLAYING)
if not playing:
event.log_event('playback_player_start', self, track)
event.log_event('playback_track_start', self, track)
return True
def stop(self, fire=True):
"""
stop playback
"""
if self.is_playing() or self.is_paused():
self.update_playtime()
current = self.current
self.playbin.set_state(gst.STATE_NULL)
self._current = None
event.log_event('playback_track_end', self, current)
if fire:
event.log_event('playback_player_end', self, current)
return True
return False
def pause(self):
"""
pause playback. DOES NOT TOGGLE
"""
if self.is_playing():
self.update_playtime()
self.playbin.set_state(gst.STATE_PAUSED)
self.reset_playtime_stamp()
event.log_event('playback_player_pause', self, self.current)
return True
return False
def unpause(self):
"""
unpause playback
"""
if self.is_paused():
self.reset_playtime_stamp()
# gstreamer does not buffer paused network streams, so if the user
# is unpausing a stream, just restart playback
if not (self.current.is_local() or
self.current.get_tag_raw('__length')):
self.playbin.set_state(gst.STATE_READY)
self.playbin.set_state(gst.STATE_PLAYING)
event.log_event('playback_player_resume', self, self.current)
return True
return False
def seek(self, value):
"""
seek to the given position in the current stream
"""
value = int(gst.SECOND * value)
event.log_event('seek', self, value)
seekevent = gst.event_new_seek(1.0, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, value, gst.SEEK_TYPE_NONE, 0)
res = self.playbin.send_event(seekevent)
if res:
self.playbin.set_new_stream_time(0L)
else:
logger.debug("Couldn't send seek event")
self.last_seek_pos = value