6bbd6e611f75473b5b9f8a3466e85bffc2f5a6fb
[experiments/gstreamer.git] / gst-trick-mode-looping-2.py
1 #!/usr/bin/env python
2
3 # Get a test sample with:
4 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
5
6 import sys
7
8 import gobject
9 gobject.threads_init()
10
11 import gst
12
13
14 class Player:
15     def __init__(self, filename, rate):
16         self._filename = filename
17         self._rate = rate
18
19         self._player = gst.element_factory_make("playbin2", "player")
20         self._player.set_property("uri", filename)
21         self._player.connect("about-to-finish", self.on_about_to_finish)
22
23         bus = self._player.get_bus()
24         bus.add_signal_watch()
25         bus.connect('message::state-changed', self.on_state_changed)
26
27     def run(self):
28         self._player.set_state(gst.STATE_PLAYING)
29         loop = gobject.MainLoop()
30         loop.run()
31
32     def on_about_to_finish(self, player):
33         sys.stderr.write(".")
34         player.set_property("uri", self._filename)
35
36         # XXX Ater the first iteration of the loop the playback rate is
37         # reset to normal. Do I need to re-set the rate here?
38
39         # If I uncomment the following line, the player hangs:
40         #self.set_rate(self._date)
41
42         # And it hangs also if I set the seek_event manually using position=0:
43         #seek_event = gst.event_new_seek(self._rate,
44         #        gst.FORMAT_TIME,
45         #        gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
46         #        gst.SEEK_TYPE_SET, 0,
47         #        gst.SEEK_TYPE_NONE, 0)
48         #self._player.send_event(seek_event)
49
50     def on_state_changed(self, bus, msg):
51         if msg.src != self._player:
52             return
53
54         print 'on_state_changed'
55         old_state, new_state, pending = msg.parse_state_changed()
56         print "%s -> %s" % (old_state, new_state)
57         if new_state == gst.STATE_PAUSED:
58             self.set_rate(self._rate)
59
60     def set_rate(self, rate):
61         self._rate = rate
62         try:
63             position, format = self._player.query_position(gst.FORMAT_TIME)
64         except:
65             position = 0
66
67         # Create the seek event
68         if rate > 0:
69             seek_event = gst.event_new_seek(rate,
70                     gst.FORMAT_TIME,
71                     gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
72                     gst.SEEK_TYPE_SET, position,
73                     gst.SEEK_TYPE_NONE, 0)
74         else:
75             seek_event = gst.event_new_seek(rate,
76                     gst.FORMAT_TIME,
77                     gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
78                     gst.SEEK_TYPE_SET, 0,
79                     gst.SEEK_TYPE_SET, position)
80
81         if seek_event:
82             self._player.send_event(seek_event)
83             gst.info("rate set to %s" % rate)
84         else:
85             gst.warining("change rate failed")
86
87
88 def main(args):
89     def usage():
90         sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
91
92     if len(args) != 2:
93         usage()
94         sys.exit(1)
95
96     if not gst.uri_is_valid(args[1]):
97         sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
98         sys.exit(1)
99
100     player = Player(args[1], 3.0)
101     player.run()
102
103 if __name__ == '__main__':
104     sys.exit(main(sys.argv))