6 import util.misc as misc_utils
9 _moduleLogger = logging.getLogger(__name__)
12 class Stream(gobject.GObject):
20 gobject.SIGNAL_RUN_LAST,
22 (gobject.TYPE_STRING, ),
25 gobject.SIGNAL_RUN_LAST,
27 (gobject.TYPE_STRING, ),
30 gobject.SIGNAL_RUN_LAST,
32 (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT),
36 _SERVICE_NAME = "com.nokia.osso_media_server"
37 _OBJECT_PATH = "/com/nokia/osso_media_server"
38 _AUDIO_INTERFACE_NAME = "com.nokia.osso_media_server.music"
41 gobject.GObject.__init__(self)
43 self._state = self.STATE_STOP
44 self._nextState = self.STATE_STOP
49 session_bus = dbus.SessionBus()
51 # Get the osso-media-player proxy object
52 oms_object = session_bus.get_object(
56 follow_name_owner_changes=True,
58 # Use the audio interface
59 oms_audio_interface = dbus.Interface(
61 self._AUDIO_INTERFACE_NAME,
63 self._audioProxy = oms_audio_interface
65 self._audioProxy.connect_to_signal("state_changed", self._on_state_changed)
66 self._audioProxy.connect_to_signal("end_of_stream", self._on_end_of_stream)
78 "audio_codec_not_supported",
80 for error in error_signals:
81 self._audioProxy.connect_to_signal(error, self._on_error)
85 return self.state == self.STATE_PLAY
89 return 0 < len(self._uri)
95 def set_file(self, uri):
97 self._invalidate_cache()
98 if self.state != self.STATE_STOP:
102 self._audioProxy.set_media_location(self._uri)
105 if self._nextState == self.STATE_PLAY:
106 _moduleLogger.info("Already play")
108 _moduleLogger.info("Play")
109 self._audioProxy.play()
110 self._nextState = self.STATE_PLAY
111 #self.emit("state-change", self.STATE_PLAY)
114 if self._nextState == self.STATE_PAUSE:
115 _moduleLogger.info("Already pause")
117 _moduleLogger.info("Pause")
118 self._audioProxy.pause()
119 self._nextState = self.STATE_PAUSE
120 #self.emit("state-change", self.STATE_PLAY)
123 if self._nextState == self.STATE_STOP:
124 _moduleLogger.info("Already stop")
126 self._audioProxy.stop()
127 _moduleLogger.info("Stopped")
128 self._nextState = self.STATE_STOP
129 #self.emit("state-change", self.STATE_STOP)
133 pos_info = self._audioProxy.get_position()
134 if isinstance(pos_info, tuple):
135 self._elapsed, self._duration = pos_info
140 pos_info = self._audioProxy.get_position()
141 if isinstance(pos_info, tuple):
142 self._elapsed, self._duration = pos_info
143 return self._duration
145 def seek_time(self, ns):
146 _moduleLogger.debug("Seeking to: %s", ns)
147 self._audioProxy.seek( dbus.Int32(1), dbus.Int32(ns) )
149 def _invalidate_cache(self):
153 @misc_utils.log_exception(_moduleLogger)
154 def _on_error(self, *args):
155 err, debug = "", repr(args)
156 _moduleLogger.error("Error: %s, (%s)" % (err, debug))
157 self.emit("error", err, debug)
159 @misc_utils.log_exception(_moduleLogger)
160 def _on_end_of_stream(self, *args):
161 self._state = self.STATE_STOP
162 self._nextState = self.STATE_STOP
163 self.emit("eof", self._uri)
165 @misc_utils.log_exception(_moduleLogger)
166 def _on_state_changed(self, state):
167 _moduleLogger.info("State: %s", state)
169 "playing": self.STATE_PLAY,
170 "paused": self.STATE_PAUSE,
171 "stopped": self.STATE_STOP,
173 if self._state == self.STATE_STOP and self._nextState == self.STATE_PLAY and state == self.STATE_STOP:
174 # They seem to want to advertise stop right as the stream is starting, breaking the owner of this
177 self._nextState = state
178 self.emit("state-change", state)
181 gobject.type_register(Stream)