2
# PiTiVi , Non-linear video editor
6
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
8
# This program is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU Lesser General Public
10
# License as published by the Free Software Foundation; either
11
# version 2.1 of the License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
# Lesser General Public License for more details.
18
# You should have received a copy of the GNU Lesser General Public
19
# License along with this program; if not, write to the
20
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
# Boston, MA 02111-1307, USA.
25
from bin import SmartBin, SmartDefaultBin, SmartFileBin#, SmartTempUriBin
27
class PlayGround(gobject.GObject):
29
Holds all the applications pipelines in a GstThread.
30
They all share the same (audio,video) sink threads.
31
Only one pipeline uses those sinks at any given time, but other pipelines
32
can be in a PLAYED state (because they can be encoding).
34
Only SmartBin can be added to the PlayGround
37
current-changed : There's a new bin playing
38
current-state : The state of the current bin has changed
42
"current-changed" : ( gobject.SIGNAL_RUN_LAST,
44
(gobject.TYPE_PYOBJECT, )),
45
"current-state" : ( gobject.SIGNAL_RUN_LAST,
47
(gobject.TYPE_PYOBJECT, )),
48
"bin-added" : ( gobject.SIGNAL_RUN_LAST,
50
( gobject.TYPE_PYOBJECT, )),
51
"bin-removed" : ( gobject.SIGNAL_RUN_LAST,
53
( gobject.TYPE_PYOBJECT, ))
57
gst.info("Starting up playground")
58
gobject.GObject.__init__(self)
59
# List of used pipelines
62
self.vsinkthread = None
63
self.asinkthread = None
65
# Defaut pipeline if no other pipeline is playing
66
self.default = SmartDefaultBin()
68
# Current playing pipeline
71
self.currentlength = 0
73
self.tempsmartbin = None
74
self.cur_state_signal = None
75
self.cur_eos_signal = None
77
self.switch_to_default()
78
self.state = gst.STATE_READY
79
self.current.set_state(self.state)
80
#self.playthread.set_state(self.state)
82
def add_pipeline(self, pipeline):
83
""" add a pipeline to the playground """
84
gst.debug("pipeline : %s" % pipeline)
85
if not isinstance(pipeline, SmartBin):
88
self.pipelines.append(pipeline)
89
bus = pipeline.get_bus()
90
bus.add_signal_watch()
91
bus.connect("message", self._bus_message_cb, pipeline)
92
self.emit("bin-added", pipeline)
94
def remove_pipeline(self, pipeline):
95
""" removes a pipeline from the playground """
96
gst.debug("pipeline : %s" % pipeline)
97
if not pipeline in self.pipelines:
100
bus = pipeline.get_bus()
101
bus.remove_signal_watch()
103
pipeline.set_state(gst.STATE_READY)
104
if self.current == pipeline:
105
self.switch_to_default()
106
self.pipelines.remove(pipeline)
107
self.emit("bin-removed", pipeline)
109
def switch_to_pipeline(self, pipeline):
111
switch to the given pipeline for play output
113
pipeline.debug("BEGINNING")
114
if self.current == pipeline:
116
if not pipeline in self.pipelines and not pipeline == self.default:
119
self.current.info("setting to READY")
120
self.current.set_state(gst.STATE_READY)
121
self.current.remove_audio_sink_thread()
122
self.current.remove_video_sink_thread()
123
if self.cur_state_signal:
124
self.current.disconnect(self.cur_state_signal)
125
if self.cur_eos_signal:
126
self.current.disconnect(self.cur_eos_signal)
127
#self.playthread.remove(self.current)
128
# remove the tempsmartbin if it's the current
129
if self.current == self.tempsmartbin:
130
self.tempsmartbin = None
132
self.current = pipeline
133
self.current.set_state(gst.STATE_READY)
134
if self.current.has_video and self.vsinkthread:
135
#self.vsinkthread.set_state(gst.STATE_READY)
136
self.current.set_video_sink_thread(self.vsinkthread)
137
if self.current.has_audio and self.asinkthread:
138
#self.asinkthread.set_state(gst.STATE_READY)
139
self.current.set_audio_sink_thread(self.asinkthread)
140
self.emit("current-changed", self.current)
142
pipeline.debug("END")
144
def switch_to_default(self):
145
""" switch to the default pipeline """
146
gst.debug("switching to default")
147
self.switch_to_pipeline(self.default)
149
def set_video_sink_thread(self, vsinkthread):
150
""" sets the video sink thread """
151
gst.debug("video sink thread : %s" % vsinkthread)
152
if self.vsinkthread and self.current.has_video:
153
self.current.set_state(gst.STATE_READY)
154
self.current.remove_video_sink_thread()
155
self.vsinkthread = vsinkthread
156
if self.current and self.current.has_video:
157
self.current.set_video_sink_thread(self.vsinkthread)
159
def set_audio_sink_thread(self, asinkthread):
160
""" sets the audio sink thread """
161
gst.debug("set audio sink thread : %s" % asinkthread)
162
if self.asinkthread and self.current.asinkthread:
163
self.current.set_state(gst.STATE_READY)
164
self.current.remove_audio_sink_thread()
165
self.asinkthread = asinkthread
166
if self.current and self.current.has_audio:
167
self.current.set_audio_sink_thread(self.asinkthread)
169
def _play_temporary_bin(self, tempbin):
170
""" temporarely play a smartbin """
171
gst.debug("BEGINNING tempbin : %s" % tempbin)
173
self.add_pipeline(tempbin)
174
res = self.switch_to_pipeline(tempbin)
175
if self.tempsmartbin:
176
self.remove_pipeline(self.tempsmartbin)
177
self.tempsmartbin = tempbin
179
gst.debug("END tempbin : %s" % tempbin)
181
def play_temporary_uri(self, uri):
183
gst.debug("uri : %s" % uri)
184
tempbin = SmartTempUriBin(uri)
185
self._play_temporary_bin(tempbin)
188
def play_temporary_filesourcefactory(self, factory):
189
""" temporarely play a FileSourceFactory """
190
gst.debug("factory : %s" % factory)
191
if isinstance(self.current, SmartFileBin) and self.current.factory == factory:
192
gst.info("Already playing factory : %s" % factory)
194
tempbin = SmartFileBin(factory)
195
self._play_temporary_bin(tempbin)
197
def seek_in_current(self, value, format=gst.FORMAT_TIME):
198
""" seek to the given position in the current playing bin """
199
gst.debug("value : %s , format : %s" % (value, format))
202
target = self.current
205
res = target.seek(1.0, format, gst.SEEK_FLAG_FLUSH,
206
gst.SEEK_TYPE_SET, value,
207
gst.SEEK_TYPE_NONE, -1)
209
gst.warning ("Seeking in current failed !");
211
# bring back current to previous state
216
def _bus_message_cb(self, bus, message, pipeline):
217
""" handler for messages from the pipelines' buses """
218
gst.info("%s [%s]" % (message.type, message.src))
219
if message.src == self.current:
220
if message.type == gst.MESSAGE_STATE_CHANGED:
221
oldstate, newstate, pending = message.parse_state_changed()
222
self.current.info("old:%s, new:%s, pending:%s" %
223
(oldstate, newstate, pending))
224
if pending == gst.STATE_VOID_PENDING:
225
self.emit("current-state", newstate)
229
# playing proxy functions
233
""" play the current pipeline """
235
if not self.current or not self.asinkthread or not self.vsinkthread:
236
gst.warning("returning ???")
238
gst.debug("setting to play")
239
self.state = gst.STATE_PLAYING
240
gst.info("%s" % self.current.set_state(self.state))
241
## gst.debug("set_state() done, getting state now")
242
## value = self.current.get_state(None)
243
## gst.debug("got_state : %s" % str(value))
246
""" pause the current pipeline """
248
if not self.current or self.current == self.default:
250
self.state = gst.STATE_PAUSED
251
self.current.set_state(self.state)
253
def fast_forward(self):
254
""" fast forward the current pipeline """
258
""" play the current pipeline backwards """
261
def forward_one(self):
262
""" forward the current pipeline by one video frame """
265
def backward_one(self):
266
""" rewind the current pipeline by one video frame """
269
def seek(self, time):
270
""" seek in the current pipeline """