~ubuntu-branches/ubuntu/trusty/pitivi/trusty

« back to all changes in this revision

Viewing changes to pitivi/playground.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-02-04 14:42:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060204144230-9ihvyas6lhgn81k1
Tags: upstream-0.9.9.2
ImportĀ upstreamĀ versionĀ 0.9.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# PiTiVi , Non-linear video editor
 
3
#
 
4
#       playground.py
 
5
#
 
6
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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.
 
22
 
 
23
import gobject
 
24
import gst
 
25
from bin import SmartBin, SmartDefaultBin, SmartFileBin#, SmartTempUriBin
 
26
 
 
27
class PlayGround(gobject.GObject):
 
28
    """
 
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).
 
33
 
 
34
    Only SmartBin can be added to the PlayGround
 
35
 
 
36
    Signals:
 
37
      current-changed : There's a new bin playing
 
38
      current-state : The state of the current bin has changed
 
39
    """
 
40
 
 
41
    __gsignals__ = {
 
42
        "current-changed" : ( gobject.SIGNAL_RUN_LAST,
 
43
                              gobject.TYPE_NONE,
 
44
                              (gobject.TYPE_PYOBJECT, )),
 
45
        "current-state" : ( gobject.SIGNAL_RUN_LAST,
 
46
                            gobject.TYPE_NONE,
 
47
                            (gobject.TYPE_PYOBJECT, )),
 
48
        "bin-added" : ( gobject.SIGNAL_RUN_LAST,
 
49
                       gobject.TYPE_NONE,
 
50
                       ( gobject.TYPE_PYOBJECT, )),
 
51
        "bin-removed" : ( gobject.SIGNAL_RUN_LAST,
 
52
                          gobject.TYPE_NONE,
 
53
                          ( gobject.TYPE_PYOBJECT, ))
 
54
        }
 
55
 
 
56
    def __init__(self):
 
57
        gst.info("Starting up playground")
 
58
        gobject.GObject.__init__(self)
 
59
        # List of used pipelines
 
60
        self.pipelines = []
 
61
        
 
62
        self.vsinkthread = None
 
63
        self.asinkthread = None
 
64
        
 
65
        # Defaut pipeline if no other pipeline is playing
 
66
        self.default = SmartDefaultBin()
 
67
 
 
68
        # Current playing pipeline
 
69
        self.current = None
 
70
        self.currentstart = 0
 
71
        self.currentlength = 0
 
72
        self.currentpos = 0
 
73
        self.tempsmartbin = None
 
74
        self.cur_state_signal = None
 
75
        self.cur_eos_signal = None
 
76
        
 
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)
 
81
 
 
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):
 
86
            return
 
87
 
 
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)
 
93
 
 
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:
 
98
            return
 
99
 
 
100
        bus = pipeline.get_bus()
 
101
        bus.remove_signal_watch()
 
102
 
 
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)
 
108
 
 
109
    def switch_to_pipeline(self, pipeline):
 
110
        """
 
111
        switch to the given pipeline for play output
 
112
        """
 
113
        pipeline.debug("BEGINNING")
 
114
        if self.current == pipeline:
 
115
            return
 
116
        if not pipeline in self.pipelines and not pipeline == self.default:
 
117
            return
 
118
        if self.current:
 
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
 
131
 
 
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)
 
141
 
 
142
        pipeline.debug("END")
 
143
 
 
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)
 
148
 
 
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)
 
158
 
 
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)
 
168
 
 
169
    def _play_temporary_bin(self, tempbin):
 
170
        """ temporarely play a smartbin """
 
171
        gst.debug("BEGINNING tempbin : %s" % tempbin)
 
172
        self.pause()
 
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
 
178
        self.play()
 
179
        gst.debug("END tempbin : %s" % tempbin)
 
180
 
 
181
    def play_temporary_uri(self, uri):
 
182
        """ plays a uri """
 
183
        gst.debug("uri : %s" % uri)
 
184
        tempbin = SmartTempUriBin(uri)
 
185
        self._play_temporary_bin(tempbin)
 
186
        pass
 
187
 
 
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)
 
193
            return
 
194
        tempbin = SmartFileBin(factory)
 
195
        self._play_temporary_bin(tempbin)
 
196
 
 
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))
 
200
        if not self.current:
 
201
            return
 
202
        target = self.current
 
203
 
 
204
        # actual seeking
 
205
        res = target.seek(1.0, format, gst.SEEK_FLAG_FLUSH,
 
206
                          gst.SEEK_TYPE_SET, value,
 
207
                          gst.SEEK_TYPE_NONE, -1)
 
208
        if not res:
 
209
            gst.warning ("Seeking in current failed !");
 
210
 
 
211
        # bring back current to previous state
 
212
 
 
213
    #
 
214
    # Bus handler
 
215
    #
 
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)
 
226
 
 
227
 
 
228
    #
 
229
    # playing proxy functions
 
230
    #
 
231
 
 
232
    def play(self):
 
233
        """ play the current pipeline """
 
234
        gst.debug("play")
 
235
        if not self.current or not self.asinkthread or not self.vsinkthread:
 
236
            gst.warning("returning ???")
 
237
            return
 
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))
 
244
 
 
245
    def pause(self):
 
246
        """ pause the current pipeline """
 
247
        gst.debug("pause")
 
248
        if not self.current or self.current == self.default:
 
249
            return
 
250
        self.state = gst.STATE_PAUSED
 
251
        self.current.set_state(self.state)
 
252
 
 
253
    def fast_forward(self):
 
254
        """ fast forward the current pipeline """
 
255
        pass
 
256
 
 
257
    def rewind(self):
 
258
        """ play the current pipeline backwards """
 
259
        pass
 
260
 
 
261
    def forward_one(self):
 
262
        """ forward the current pipeline by one video frame """
 
263
        pass
 
264
 
 
265
    def backward_one(self):
 
266
        """ rewind the current pipeline by one video frame """
 
267
        pass
 
268
 
 
269
    def seek(self, time):
 
270
        """ seek in the current pipeline """
 
271
        pass
 
272