Implementing an audio seekbar
[watersofshiloah] / src / player.py
index c4a06de..6e61429 100644 (file)
@@ -2,6 +2,11 @@ import logging
 
 import gobject
 
+import util.misc as misc_utils
+import stream
+import stream_index
+import call_monitor
+
 
 _moduleLogger = logging.getLogger(__name__)
 
@@ -19,20 +24,33 @@ class Player(gobject.GObject):
                        gobject.TYPE_NONE,
                        (gobject.TYPE_PYOBJECT, ),
                ),
+               'error' : (
+                       gobject.SIGNAL_RUN_LAST,
+                       gobject.TYPE_NONE,
+                       (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT),
+               ),
        }
 
+       STATE_PLAY = stream.GSTStream.STATE_PLAY
+       STATE_PAUSE = stream.GSTStream.STATE_PAUSE
+       STATE_STOP = stream.GSTStream.STATE_STOP
+
        def __init__(self, index):
                gobject.GObject.__init__(self)
                self._index = index
                self._node = None
-               self._state = "play"
+               self._nextSearch = None
+
+               self._calls = call_monitor.CallMonitor()
+               self._calls.connect("call_start", self._on_call_start)
+
+               self._stream = stream.GSTStream()
+               self._stream.connect("state-change", self._on_stream_state)
+               self._stream.connect("eof", self._on_stream_eof)
+               self._stream.connect("error", self._on_stream_error)
 
        def set_piece_by_node(self, node):
-               assert node.is_leaf() or node is None
-               if self._node is node:
-                       return
-               self._node = node
-               self.emit("title_change", self._state)
+               self._set_piece_by_node(node)
 
        @property
        def node(self):
@@ -58,35 +76,90 @@ class Player(gobject.GObject):
 
        @property
        def state(self):
-               return self._state
+               return self._stream.state
 
        def play(self):
-               if self._state == "play":
-                       return
-               self._state = "play"
-               self.emit("state_change", self._state)
                _moduleLogger.info("play")
+               self._stream.play()
+
+               self._calls.start()
 
        def pause(self):
-               if self._state == "pause":
-                       return
-               self._state = "pause"
-               self.emit("state_change", self._state)
                _moduleLogger.info("pause")
+               self._stream.pause()
 
        def stop(self):
-               if self._state == "stop":
-                       return
-               self._state = "stop"
-               self.set_piece_by_node(None)
-               self.emit("state_change", self._state)
                _moduleLogger.info("stop")
+               self._stream.stop()
+               self.set_piece_by_node(None)
+
+               self._calls.stop()
 
        def back(self):
                _moduleLogger.info("back")
+               assert self._nextSearch is None
+               self._nextSearch = stream_index.AsyncWalker(stream_index.get_previous)
+               self._nextSearch.start(self.node, self._on_next_node, self._on_node_search_error)
 
        def next(self):
                _moduleLogger.info("next")
+               assert self._nextSearch is None
+               self._nextSearch = stream_index.AsyncWalker(stream_index.get_next)
+               self._nextSearch.start(self.node, self._on_next_node, self._on_node_search_error)
+
+       def seek(self, percent):
+               target = percent * self._stream.duration
+               self._stream.seek_time(target)
+
+       @property
+       def percent_elapsed(self):
+               percent = float(self._stream.elapsed) / float(self._stream.duration)
+               return percent
+
+       def _set_piece_by_node(self, node):
+               assert node is None or node.is_leaf(), node
+               if self._node is node:
+                       _moduleLogger.info("Already set to %r" % node)
+                       return
+               self._node = node
+               if self._node is not None:
+                       self._stream.set_file(self._node.uri)
+               _moduleLogger.info("New node %r" % self._node)
+               self.emit("title_change", self._node)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_next_node(self, node):
+               self._nextSearch = None
+
+               restart = self.state == self.STATE_PLAY
+               self._set_piece_by_node(node)
+               if restart:
+                       self.play()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_node_search_error(self, e):
+               self._nextSearch = None
+               self.emit("error", e, "")
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_stream_state(self, s, state):
+               _moduleLogger.info("State change %r" % state)
+               self.emit("state_change", state)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_stream_eof(self, s, uri):
+               _moduleLogger.info("EOF %s" % uri)
+               self.next()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_stream_error(self, s, error, debug):
+               _moduleLogger.info("Error %s %s" % (error, debug))
+               self.emit("error", error, debug)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_call_start(self, monitor):
+               _moduleLogger.info("Call in progress, pausing")
+               self.pause()
 
 
 gobject.type_register(Player)