Integrating and testing of playback
[watersofshiloah] / src / player.py
1 import logging
2
3 import gobject
4
5 import util.misc as misc_utils
6 import stream
7
8
9 _moduleLogger = logging.getLogger(__name__)
10
11
12 class Player(gobject.GObject):
13
14         __gsignals__ = {
15                 'state_change' : (
16                         gobject.SIGNAL_RUN_LAST,
17                         gobject.TYPE_NONE,
18                         (gobject.TYPE_PYOBJECT, ),
19                 ),
20                 'title_change' : (
21                         gobject.SIGNAL_RUN_LAST,
22                         gobject.TYPE_NONE,
23                         (gobject.TYPE_PYOBJECT, ),
24                 ),
25                 'error' : (
26                         gobject.SIGNAL_RUN_LAST,
27                         gobject.TYPE_NONE,
28                         (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT),
29                 ),
30         }
31
32         STATE_PLAY = stream.GSTStream.STATE_PLAY
33         STATE_PAUSE = stream.GSTStream.STATE_PAUSE
34         STATE_STOP = stream.GSTStream.STATE_STOP
35
36         def __init__(self, index):
37                 gobject.GObject.__init__(self)
38                 self._index = index
39                 self._node = None
40                 self._stream = stream.GSTStream()
41                 self._stream.connect("state-change", self._on_stream_state)
42                 self._stream.connect("eof", self._on_stream_eof)
43                 self._stream.connect("error", self._on_stream_error)
44
45         def set_piece_by_node(self, node):
46                 assert node is None or node.is_leaf(), node
47                 if self._node is node:
48                         return
49                 self._node = node
50                 if self._node is not None:
51                         self._stream.set_file(self._node.uri)
52                 _moduleLogger.info("New node %r" % self._node)
53                 self.emit("title_change", self._node)
54
55         @property
56         def node(self):
57                 return self._node
58
59         @property
60         def title(self):
61                 if self._node is None:
62                         return ""
63                 return self._node.title
64
65         @property
66         def subtitle(self):
67                 if self._node is None:
68                         return ""
69                 return self._node.subtitle
70
71         @property
72         def can_navigate(self):
73                 if self._node is None:
74                         return False
75                 return self.node.can_navigate
76
77         @property
78         def state(self):
79                 return self._stream.state
80
81         def play(self):
82                 _moduleLogger.info("play")
83                 self._stream.play()
84
85         def pause(self):
86                 _moduleLogger.info("pause")
87                 self._stream.pause()
88
89         def stop(self):
90                 _moduleLogger.info("stop")
91                 self._stream.stop()
92                 self.set_piece_by_node(None)
93
94         def back(self):
95                 _moduleLogger.info("back")
96
97         def next(self):
98                 _moduleLogger.info("next")
99
100         @misc_utils.log_exception(_moduleLogger)
101         def _on_stream_state(self, s, state):
102                 _moduleLogger.info("State change %r" % state)
103                 self.emit("state_change", state)
104
105         @misc_utils.log_exception(_moduleLogger)
106         def _on_stream_eof(self, s, uri):
107                 _moduleLogger.info("EOF %s" % uri)
108                 self.next()
109
110         @misc_utils.log_exception(_moduleLogger)
111         def _on_stream_error(self, s, error, debug):
112                 _moduleLogger.info("Error %s %s" % (error, debug))
113                 self.emit("error", error, debug)
114
115
116 gobject.type_register(Player)