-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.py
134 lines (109 loc) · 4.45 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Collabora Ltd
# Copyright (C) 2012 Luis de Bethencourt <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
"""GStreamer engine class"""
from gi.repository import Gst
from gi.repository import GObject
class Engine (GObject.GObject):
'''GStreamer engine class. Encapsulates all the core gstreamer work in
simple function per feature for the HMI'''
__gsignals__ = {
'about_to_finish': (GObject.SIGNAL_RUN_FIRST, None,
()),
'error': (GObject.SIGNAL_RUN_FIRST, None,
())
}
def __init__ (self):
GObject.GObject.__init__(self)
Gst.init (None)
print "Running with GStreamer", str(Gst.version()[0]) + "." + \
str(Gst.version()[1]) + "." + str(Gst.version()[2])
self.IS_GST010 = Gst.version()[0] == 0
if self.IS_GST010:
PLAYBIN_ELEMENT = "playbin2"
print "Only GStreamer 1.0 is supported for this demo."
exit(1)
else:
PLAYBIN_ELEMENT = "playbin"
self.player = Gst.ElementFactory.make(PLAYBIN_ELEMENT, "Player")
self.is_playing = False
self._seeking = False
self._current_position = self._target_position = 0
self.bus = self.player.get_bus()
self.bus.add_signal_watch()
self.bus.connect("message", self._onBusMessage)
self.player.connect("about-to-finish", self._about_to_finish)
def play (self, uri):
if self.is_playing == False:
self.player.set_state (Gst.State.NULL)
self.player.props.uri = uri
self.player.set_state (Gst.State.PLAYING)
self.is_playing = True
def pause (self):
self.player.set_state (Gst.State.PAUSED)
def stop (self):
self.player.set_state (Gst.State.READY)
self.is_playing = False
def seek (self, target_position):
self.player.seek_simple (Gst.Format.TIME, \
Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, \
target_position)
def _seek (self):
if not self._seeking and self._current_position != self._target_position:
self._seeking = True
print "Seek to", self._target_position
self.seek(self._target_position)
self._current_position = self._target_position
def query_duration (self):
return self.player.query_duration (Gst.Format.TIME)[1]
def query_position (self):
return self.player.query_position (Gst.Format.TIME)[1]
"""
GStreamer callbacks
"""
def _onBusMessage(self, bus, message):
if message is None:
# This doesn't make any sense, but it happens all the time.
return
elif message.type is Gst.MessageType.TAG:
# TODO: do something with ID3 tags or not?
pass
elif message.type is Gst.MessageType.ASYNC_DONE:
print "Async done, now try seeking"
self._seeking = False
self._seek()
elif message.type is Gst.MessageType.ERROR:
print "Got message of type ", message.type
print "Got message of src ", message.src
print "Got message of error ", message.parse_error()
self.emit ("error")
def _about_to_finish (self, playbin):
print "engine: about to finish"
self.emit ("about_to_finish");
if __name__ == "__main__":
import os, optparse
usage = """engine.py -i [file]"""
parser = optparse.OptionParser(usage=usage)
parser.add_option("-i", "--input", action="store", type="string", \
dest="input", help="Input video file", default="")
(options, args) = parser.parse_args()
print "Playing: %r" % options.input
engine = Engine()
engine.play (options.input)
GObject.MainLoop().run()