-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.py
153 lines (100 loc) · 4.45 KB
/
buffer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import tempfile
import gobject
import gst
from common import STATE_BUFFERING, STATE_NULL, STATE_PAUSED, STATE_PLAYING
class BufferException(BaseException):
pass
class Buffer(gobject.GObject):
# TODO: One buffer for each video -- one buffer file for each video
__gtype_name__ = 'Buffer'
__gsignals__ = {
'ready': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'update': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_FLOAT,))
}
def __init__(self):
gobject.GObject.__init__(self)
self.state = STATE_NULL
self.ready = False
self.eos = False
self.update_timeout = None
self.pipeline = gst.parse_launch('souphttpsrc name=src ! filesink name=sink sync=false')
self.src = self.pipeline.get_by_name('src')
self.sink = self.pipeline.get_by_name('sink')
self.foo = self.pipeline.get_by_name('foo')
self.bus = self.pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect("message", self.bus_message_cb)
self.test_pipeline = gst.parse_launch('filesrc name=test_src ! decodebin2 name=decoder ! fakesink')
self.test_src = self.test_pipeline.get_by_name('test_src')
self.test_decoder = self.test_pipeline.get_by_name('decoder')
self.test_decoder.connect('autoplug-continue', self.autoplug_continue_cb)
self.test_bus = self.test_pipeline.get_bus()
self.test_bus.add_signal_watch()
self.test_bus.connect("message", self.test_bus_message_cb)
def emit_ready(self):
if self.pipeline.query_position(gst.FORMAT_BYTES, None)[0] <= 200000:
return True
self.emit('ready')
self.ready = True
self.test_pipeline.set_state(gst.STATE_NULL)
self.test_pipeline = gst.parse_launch('filesrc name=test_src ! decodebin2 name=decoder ! fakesink')
self.test_src = self.test_pipeline.get_by_name('test_src')
self.test_decoder = self.test_pipeline.get_by_name('decoder')
self.test_decoder.connect('autoplug-continue', self.autoplug_continue_cb)
self.test_bus = self.test_pipeline.get_bus()
self.test_bus.add_signal_watch()
self.test_bus.connect("message", self.test_bus_message_cb)
return False
def autoplug_continue_cb(self, bin, pad, caps):
gobject.timeout_add(100, lambda *args: self.emit_ready())
def bus_message_cb(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.eos = True
def test_bus_message_cb(self, bus, message):
t = message.type
if t == gst.MESSAGE_ERROR:
self.test_pipeline.set_state(gst.STATE_NULL)
def update(self):
if self.eos:
self.emit('update', 100)
return
try:
duration = self.pipeline.query_duration(gst.FORMAT_BYTES, None)[0]
position = max(0, self.pipeline.query_position(gst.FORMAT_BYTES, None)[0] - 500000)
if position == 0:
self.emit('update', -1)
else:
self.emit('update', max(0, (float(position) / float(duration) * 100)))
if not self.ready:
self.test_pipeline.set_state(gst.STATE_PLAYING)
except gst.QueryError:
self.emit('update', -1)
return True
def load(self, uri):
self.ready = False
self.src.set_property('location', uri)
tmp = tempfile.mktemp(dir='/tmp')
self.sink.set_property('location', tmp)
self.test_src.set_property('location', tmp)
return tmp
def flush(self):
self.ready = False
self.eos = False
self.set_state(STATE_NULL)
self.emit('update', 0)
def set_state(self, state):
if state not in [STATE_NULL, STATE_PLAYING]:
raise BufferException, "'state' must be either 'STATE_NULL' or 'STATE_PLAYING', not '{0}'!".format(state)
if state == STATE_NULL:
self.pipeline.set_state(gst.STATE_NULL)
if self.update_timeout:
gobject.source_remove(self.update_timeout)
self.update_timeout = None
self.state = STATE_NULL
elif state == STATE_PLAYING:
self.pipeline.set_state(gst.STATE_PLAYING)
if self.update_timeout:
gobject.source_remove(self.update_timeout)
self.update_timeout = gobject.timeout_add(100, self.update)
self.state = STATE_PLAYING