1 # Implements playback controls
2 # Gstreamer stuff mostly snibbed from Panucci
4 # This file is part of Panucci.
5 # Copyright (c) 2008-2009 The Panucci Audiobook and Podcast Player Project
7 # Panucci is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Panucci is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
29 from settings import settings
30 from postoffice import postoffice
32 log = logging.getLogger(__name__)
34 class _Player(object):
35 """Defines the internal player interface"""
38 def play_url(self, filetype, uri):
42 def play_pause_toggle(self):
43 self.pause() if self.playing() else self.play()
50 def set_eos_callback(self, cb):
53 class GStreamer(_Player):
55 STATES = { gst.STATE_NULL : 'stopped',
56 gst.STATE_PAUSED : 'paused',
57 gst.STATE_PLAYING : 'playing' }
60 _Player.__init__(self)
61 self.time_format = gst.Format(gst.FORMAT_TIME)
64 self.filesrc_property = None
65 self.volume_control = None
66 self.volume_multiplier = 1.
67 self.volume_property = None
68 self.eos_callback = lambda: self.stop()
69 postoffice.connect('settings-changed', self, self.on_settings_changed)
71 def on_settings_changed(self, key, value):
73 self._set_volume_level(value)
74 #postoffice.disconnect(self)
77 def play_url(self, filetype, uri):
78 if None in (filetype, uri):
83 if self.player is None:
86 self._maemo_setup_playbin2_player(uri)
87 log.debug('Using playbin2 (maemo)')
88 elif util.platform == 'maemo':
89 self._maemo_setup_playbin_player()
90 log.debug('Using playbin (maemo)')
92 self._setup_playbin_player()
93 log.debug( 'Using playbin (non-maemo)' )
95 bus = self.player.get_bus()
96 bus.add_signal_watch()
97 bus.connect('message', self._on_message)
98 self._set_volume_level(settings.volume)
100 self._set_uri_to_be_played(uri)
107 state = self.player.get_state()[1]
108 return self.STATES.get(state, 'none')
111 def get_position_duration(self):
113 pos_int = self.player.query_position(self.time_format, None)[0]
114 dur_int = self.player.query_duration(self.time_format, None)[0]
116 log.exception('Error getting position')
117 pos_int = dur_int = 0
118 return pos_int, dur_int
121 return self.get_state() == 'playing'
126 self.player.set_state(gst.STATE_PLAYING)
130 self.player.set_state(gst.STATE_PAUSED)
132 def stop(self, reset = True):
134 self.player.set_state(gst.STATE_NULL)
138 def _maemo_setup_playbin2_player(self, url):
139 self.player = gst.parse_launch("playbin2 uri=%s" % (url,))
140 self.filesrc = self.player
141 self.filesrc_property = 'uri'
142 self.volume_control = self.player
143 self.volume_multiplier = 1.
144 self.volume_property = 'volume'
146 def _maemo_setup_playbin_player( self):
147 self.player = gst.element_factory_make('playbin2', 'player')
148 self.filesrc = self.player
149 self.filesrc_property = 'uri'
150 self.volume_control = self.player
151 self.volume_multiplier = 1.
152 self.volume_property = 'volume'
155 def _setup_playbin_player( self ):
156 """ This is for situations where we have a normal (read: non-maemo)
157 version of gstreamer like on a regular linux distro. """
158 self.player = gst.element_factory_make('playbin2', 'player')
159 self.filesrc = self.player
160 self.filesrc_property = 'uri'
161 self.volume_control = self.player
162 self.volume_multiplier = 1.
163 self.volume_property = 'volume'
165 def _on_decoder_pad_added(self, decoder, src_pad, sink_pad):
166 # link the decoder's new "src_pad" to "sink_pad"
167 src_pad.link( sink_pad )
169 def _get_volume_level(self):
170 if self.volume_control is not None:
171 vol = self.volume_control.get_property( self.volume_property )
172 return vol / float(self.volume_multiplier)
174 def _set_volume_level(self, value):
175 assert 0 <= value <= 1
177 if self.volume_control is not None:
178 vol = value * float(self.volume_multiplier)
179 log.debug("Setting volume to %s", vol)
180 self.volume_control.set_property( self.volume_property, vol )
182 def _set_uri_to_be_played(self, uri):
183 # Sets the right property depending on the platform of self.filesrc
184 if self.player is not None:
185 self.filesrc.set_property(self.filesrc_property, uri)
187 def _on_message(self, bus, message):
190 if t == gst.MESSAGE_EOS:
191 log.info("End of stream")
193 elif t == gst.MESSAGE_STATE_CHANGED:
194 if (message.src == self.player and
195 message.structure['new-state'] == gst.STATE_PLAYING):
196 log.info("State changed to playing")
197 elif t == gst.MESSAGE_ERROR:
198 err, debug = message.parse_error()
199 log.critical( 'Error: %s %s', err, debug )
202 def set_eos_callback(self, cb):
203 self.eos_callback = cb
205 if util.platform == 'maemo':
206 class OssoPlayer(_Player):
208 A player which uses osso-media-player for playback (Maemo-specific)
211 SERVICE_NAME = "com.nokia.osso_media_server"
212 OBJECT_PATH = "/com/nokia/osso_media_server"
213 AUDIO_INTERFACE_NAME = "com.nokia.osso_media_server.music"
216 self._on_eos = lambda: self.stop()
218 self._audio = self._init_dbus()
221 def play_url(self, filetype, uri):
222 self._audio.play_media(uri)
225 return self._state == 'playing'
227 def play_pause_toggle(self):
228 self.pause() if self.playing() else self.play()
240 def set_eos_callback(self, cb):
244 def _init_dbus(self):
245 session_bus = dbus.SessionBus()
246 oms_object = session_bus.get_object(self.SERVICE_NAME,
249 follow_name_owner_changes = True)
250 return dbus.Interface(oms_object, self.AUDIO_INTERFACE_NAME)
252 def _init_signals(self):
254 "no_media_selected": "No media selected",
255 "file_not_found": "File not found",
256 "type_not_found": "Type not found",
257 "unsupported_type": "Unsupported type",
258 "gstreamer": "GStreamer Error",
260 "device_unavailable": "Device Unavailable",
261 "corrupted_file": "Corrupted File",
262 "out_of_memory": "Out of Memory",
263 "audio_codec_not_supported": "Audio codec not supported"
266 # Connect status signals
267 self._audio.connect_to_signal( "state_changed",
268 self._on_state_changed )
269 self._audio.connect_to_signal( "end_of_stream",
270 lambda x: self._call_eos() )
272 # Connect error signals
273 for error, msg in error_signals.iteritems():
274 self._audio.connect_to_signal(error, lambda *x: self._error(msg))
276 def _error(self, msg):
282 def _on_state_changed(self, state):
283 states = ("playing", "paused", "stopped")
284 self.__state = state if state in states else 'none'
286 # PlayerBackend = OssoPlayer
288 PlayerBackend = GStreamer
290 class Playlist(object):
291 def __init__(self, items = []):
295 assert(isinstance(item, jamaendo.Track))
300 if isinstance(item, list):
302 assert(isinstance(i, jamaendo.Track))
303 self.items.extend(item)
305 self.items.append(item)
309 self._current = self._current + 1
310 return self.items[self._current]
315 self._current = self._current - 1
316 return self.items[self._current]
320 return self._current < (len(self.items)-1)
323 return self._current > 0
326 if self._current >= 0:
327 return self.items[self._current]
330 def current_index(self):
334 return len(self.items)
337 return "Playlist(%s)"%(", ".join([str(item.ID) for item in self.items]))
339 class Player(object):
341 self.backend = PlayerBackend()
342 self.backend.set_eos_callback(self._on_eos)
343 self.playlist = Playlist()
345 def get_position_duration(self):
346 return self.backend.get_position_duration()
348 def play(self, playlist = None):
350 self.playlist = playlist
351 elif self.playlist is None:
352 self.playlist = Playlist()
353 if self.playlist.size():
354 if self.playlist.current():
355 entry = self.playlist.current()
356 self.backend.play_url('mp3', entry.mp3_url())
357 log.debug("playing %s", entry)
358 elif self.playlist.has_next():
359 entry = self.playlist.next()
360 self.backend.play_url('mp3', entry.mp3_url())
361 log.debug("playing %s", entry)
370 return self.backend.playing()
373 if self.playlist.has_next():
374 self.backend.stop(reset=False)
375 entry = self.playlist.next()
376 self.backend.play_url('mp3', entry.mp3_url())
377 log.debug("playing %s", entry)
382 if self.playlist.has_prev():
383 self.backend.stop(reset=False)
384 entry = self.playlist.prev()
385 self.backend.play_url('mp3', entry.mp3_url())
386 log.debug("playing %s", entry)
392 the_player = Player() # the player instance