-
Notifications
You must be signed in to change notification settings - Fork 3
/
madeo-uplayer
executable file
·620 lines (521 loc) · 21.4 KB
/
madeo-uplayer
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#!/usr/bin/env python
# Madeo MUMS Player
# Copyright 2011 Brendan Le Foll - [email protected]
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with madeo-uplayer; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
import sys
import os
import traceback
os.environ["GST_DEBUG_DUMP_DOT_DIR"] = "/tmp"
os.putenv('GST_DEBUG_DUMP_DIR_DIR', '/tmp')
import gobject
gobject.threads_init()
import logging
import gst
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import logging
UPLAYER_BUS_NAME = 'uk.co.madeo.uplayer'
UPLAYER_OBJ_PATH = '/uk/co/madeo/uplayer'
LOG_FILENAME = '/tmp/uplayer.log'
LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG, fomat=LOG_FORMAT)
class TSPipeline(gst.Pipeline):
RECORDING_TEMPLATE = "/tmp/ts.XXXXX"
AUDIO_BIN = 'queue name=aq ! autoaudiosink'
VIDEO_BIN = 'queue name=vq ! autovideosink'
def __init__(self):
gst.Pipeline.__init__(self, 'TSPipeline')
self._src = None
self.has_video = False
self.has_audio = False
self._enable_buffering = False
self._buffering_size = 512 * 1024
self._underrun_signal = None
self._running_signal = None
def on_new_pad(self, element, pad):
padname = pad.get_name()
caps = pad.get_caps()
logging.debug('padname %s %s' % (padname, caps))
name = caps[0].get_name()
bin = None
queue = None
if 'video' in name and not self.has_video:
bin = gst.parse_bin_from_description(self.VIDEO_BIN, False)
if bin:
queue = bin.get_by_name("vq")
self.has_video = True
elif 'audio' in name and not self.has_audio:
bin = gst.parse_bin_from_description(self.AUDIO_BIN, False)
if bin:
queue = bin.get_by_name("aq")
self.has_audio = True
if bin and queue:
logging.debug('adding decoders to pipeline')
targetpad = queue.get_pad('sink')
ghostpad = gst.GhostPad('sink', targetpad)
bin.add_pad(ghostpad)
self.add(bin)
bin.set_state(gst.STATE_READY)
pad.link(ghostpad)
bin.sync_state_with_parent()
# ensure to preroll the sinks
self.lost_state_full(True)
gst.DEBUG_BIN_TO_DOT_FILE (self, \
gst.DEBUG_GRAPH_SHOW_ALL, 'uplayer_tspipe.png')
def on_underrun (self, queue):
logging.debug("underrun detected")
if self._running_signal is None:
self._queue.set_property ("min-threshold-bytes", self._buffering_size)
self._running_signal = self._queue.connect('running', self.on_running)
self.post_message (gst.message_new_buffering (self, 0))
def on_running (self, queue):
logging.debug("pipeline prerolled")
self._queue.set_property ("min-threshold-bytes", 0)
self._queue.disconnect (self._running_signal)
self._running_signal = None
self.post_message (gst.message_new_buffering (self, 100))
def build_pipeline (self, uri, ismd):
if self._src is None:
logging.debug('Opening %s' % uri)
if ismd:
logging.debug('using ismd + ts mode')
self._src = gst.parse_bin_from_description('%s ! ' \
'flumpegshifter name=timeshift recording-template=/tmp/ts.XXXXXX ! ' \
'flutsdemux name=dec ! autoaudiosink dec. ! decodebin2 ! autovideosink name=autovidsink ' % uri, False)
logging.debug('ismd ts pipeline parsed')
self.add(self._src)
else:
self._src = gst.parse_bin_from_description('%s ! ' \
'flumpegshifter name=timeshift recording-template=/tmp/ts.XXXXXX ! ' \
'decodebin2 name=dec ! autoaudiosink dec. ! autovideosink name=autovidsink ' % uri, False)
self.add(self._src)
def destroy_pipeline(self):
if self._src is not None:
logging.debug("removing elements from TS pipeline")
if self._underrun_signal is not None:
self._queue.disconnect (self._underrun_signal)
if self._running_signal is not None:
self._queue.disconnect(self._running_signal)
#gst.element_unlink_many(self._src)
self._vlinked = False
self._alinked = False
self.remove (self._src)
del self._src
self._src = None
def do_cleanup(self):
current = self.get_state(0)[1]
if current != gst.STATE_NULL:
logging.debug('do_cleanup')
self.set_state(gst.STATE_READY)
current = self.get_state(0)[1]
logging.debug('changed state to %s' % current)
self.set_state(gst.STATE_NULL)
current = self.get_state(0)[1]
self.destroy_pipeline()
logging.debug('changed state to %s' % current)
class GstPlayer:
STOPPED = 0
PLAYING = 1
PAUSED = 2
BUFFERING = 3
PREROLLING = 4
def __init__(self):
self.status = self.STOPPED
self.target_status = self.STOPPED
self._rate = 1.0
self._volume = 1.0
self.c_video = 0
self.c_audio = 0
self.c_text = 0
self.n_video = 0
self.n_audio = 0
self.n_text = 0
self._videosink = None
self.uses_ismd = True
#we assume that we need to find ismd_audio_sink in order to use ismd
reg = gst.registry_get_default()
ismdaudiosink = reg.find_feature ("ismd_audio_sink", gst.TYPE_ELEMENT_FACTORY)
if ismdaudiosink is None:
logging.debug('disabling ismd mode')
self.uses_ismd = False
self.ts_mode = False
self.uri = ""
self.player = None
self.has_video = None
self.has_audio = None
def setup_player(self):
logging.debug('starting player setup')
if self.ts_mode:
self.player = TSPipeline()
self.player.build_pipeline(self.uri, self.uses_ismd)
else:
self.player = gst.element_factory_make('playbin2', None)
self.player.set_property('uri', self.uri)
bus = self.player.get_bus()
#connect stuff to the bus
bus.connect('message::eos', self.on_message_eos)
bus.connect('message::error', self.on_message_error)
#bus.connect('message::state-changed', self.on_message_state_changed)
bus.connect('message::async-done', self.on_message_async_done)
bus.add_signal_watch()
def on_message_async_done(self, bus, message):
logging.debug('async done')
if not self.ts_mode:
self.n_video = self.player.get_property('n-video')
self.n_audio = self.player.get_property('n-audio')
self.n_text = self.player.get_property('n-text')
logging.debug ("%d video %d audio %d text" % (self.n_video, self.n_audio, self.n_text))
if self.n_video > 0:
self.player.set_property("current-video", self.c_video)
if self.n_audio > 0:
self.player.set_property("current-audio", self.c_audio)
if self.n_text > 0:
self.player.set_property("current-text", self.c_text)
sink = self.player.get_property('video-sink')
if sink is not None:
self._videosink = sink
factory = sink.get_factory()
if "ismd_vidrend_bin" in factory.get_name():
logging.debug("using SMD video sink %s" % sink)
self.uses_ismd = True
elif "autovideosink" in factory.get_name():
logging.debug("using Autovideosink")
self._videosink.set_property('message-forward', True)
for child in sink.sinks():
factory = child.get_factory()
if "ismd_vidrend_bin" in factory.get_name():
logging.debug("SMD video sink %s" % child)
self._videosink = child
elif "xvimagesink" in factory.get_name():
logging.debug("xvimagesink sink %s" % child)
self._videosink = child
sink = self.player.get_property('audio-sink')
if sink is not None:
factory = sink.get_factory()
if "ismd_audio_sink" in factory.get_name():
logging.debug("Using SMD audio sink %s" % sink)
elif "autoaudiosink" in factory.get_name():
logging.debug("Using Autoaudiosink")
for child in sink.sinks():
factory = child.get_factory()
if "ismd_audio_sink" in factory.get_name():
logging.debug("SMD audio sink %s" % child)
send_duration (self.get_duration())
# gst.DEBUG_BIN_TO_DOT_FILE (self.player, \
# gst.DEBUG_GRAPH_SHOW_ALL, 'uplayer_tspipe.png')
def cleanup(self):
logging.debug('cleaning up player')
if self.player and self.ts_mode:
self.player.do_cleanup()
elif self.player:
current = self.player.get_state(0)[1]
if current != gst.STATE_NULL:
logging.debug ('do_cleanup')
self.player.set_state(gst.STATE_READY)
current = self.player.get_state(1000000000)[1]
logging.debug ('changed state to %s' % current)
self.player.set_state(gst.STATE_NULL)
current = self.player.get_state(1000000000)[1]
logging.debug ('changed state to %s' % current)
#scrap all elements we may have
self.player = None
#reset vars
self.has_video = None
self.has_audio = None
def on_message_eos(self, bus, message):
send_eos()
self.playing = False
self.stop()
def set_ts(self, ts):
if ts:
logging.debug ('TS mode on')
self.ts_mode = True
return True
else:
logging.debug ('TS mode off')
self.ts_mode = False
return False
def set_uri(self, uri):
if not gst.uri_is_valid(uri):
logging.error ("Invalid URI: %s" % uri)
return False
# if valid we set the new uri which is set in setup
self.uri = uri
# At every set_uri we clean the pipeline
self.cleanup()
self.setup_player()
# signal that we have a new uri
logging.debug ("New URI: %s" % uri)
send_new_uri(uri)
return True
def on_message_error(self, bus, message):
err, msg = message.parse_error()
code = message.structure['gerror'].code
if self.status != self.STOPPED:
send_error_message (msg)
self.stop()
self.status = self.STOPPED
self.target_status = self.status
logging.debug ("Gstreamer %s:%s" % (err, msg))
def on_message_state_changed(self, bus, message):
logging.debug('pipe state changing %s', message)
if message.src != self.player:
return
old_state, new_state, pending = message.parse_state_changed()
logging.debug ("old %s current %s pending %s status %s target status %s" % \
(old_state, new_state, pending, self.status, self.target_status))
if new_state == gst.STATE_PLAYING:
if self.status != self.PLAYING:
self.status = self.PLAYING
self.playing = True
def pause(self):
logging.debug('Pausing pipeline')
self.player.set_state(gst.STATE_PAUSED)
self.playing = False
def play(self):
current = self.player.get_state(0)[1]
if current != gst.STATE_PLAYING :
self.player.set_state(gst.STATE_PLAYING)
#self.set_volume (self._volume)
logging.debug('Playback starting')
def stop(self):
self.cleanup()
self.player.set_state(gst.STATE_STOPPED)
def get_state(self, timeout=1):
return self.player.get_state(timeout=timeout)
def is_playing(self):
current = self.player.get_state(0)[1]
if current != gst.STATE_PLAYING:
return False
else:
return True
def get_position(self):
try:
position, format = self.player.query_position(gst.FORMAT_TIME)
except:
position = gst.CLOCK_TIME_NONE
position = position * (1e-09)
return position
def seek(self, location):
logging.debug ("seek to %d", location)
# convert seconds to nanoseconds
location = location * (1e09)
if self._rate < 0:
res = self.player.seek (self._rate, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_KEY_UNIT,
gst.SEEK_TYPE_SET, 0,
gst.SEEK_TYPE_SET, location)
else:
res = self.player.seek (self._rate, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_KEY_UNIT,
gst.SEEK_TYPE_SET, location,
gst.SEEK_TYPE_SET, -1)
if not res:
logging.debug ("seek failed");
def set_rate(self, rate):
self.pause()
logging.debug ("rate set to %s" % rate);
self._rate = rate
try:
position, format = self.player.query_position(gst.FORMAT_TIME)
except:
position = gst.CLOCK_TIME_NONE
if self._rate < 0:
res = self.player.seek(self._rate, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, 0,
gst.SEEK_TYPE_SET, position)
else:
res = self.player.seek(self._rate, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, position,
gst.SEEK_TYPE_SET, -1)
self.play()
def next_video_stream(self):
if self.n_video > 0:
current = (self.c_video + 1) % self.n_video
if current != self.c_video:
self.player.set_property("current-video", current)
self.c_video = current
def next_audio_stream(self):
if self.n_audio > 0:
current = (self.c_audio + 1) % self.n_audio
if current != self.c_audio:
self.player.set_property("current-audio", current)
self.c_audio = current
def next_text_stream(self):
if self.n_text > 0:
current = (self.c_text + 1) % self.n_text
if current != self.c_text:
self.player.set_property("current-text", current)
self.c_text = current
def set_gdl_plane(self, plane):
if plane > 3 and plane < 8 and self.uses_ismd:
self._videosink.set_property('gdl-plane', plane)
return True
return False
def set_window_id(self, window_id):
#check we are using xvimagesink?
try:
if self.ts_mode:
sink = self.player.get_by_name("autovidsink")
logging.debug("using Autovideosink")
for child in sink.sinks():
factory = child.get_factory()
if "xvimagesink" in factory.get_name():
logging.debug("xvimagesink sink %s" % child)
child.set_xwindow_id(window_id)
else:
logging.debug ("_videosink is %s" % self._videosink)
self._videosink.set_xwindow_id(window_id)
logging.debug ("setting xwindowid to %s succeeded" % window_id)
return True
except:
logging.debug ("setting xwindowid to %s failed : %s" % window_id)
return False
def set_volume(self, volume):
try:
self._volume = volume
self.player.set_property("volume", volume)
logging.debug("volume set to %f" % self._volume)
return True
except:
logging.debug ("setting volume to %s failed" % volume)
return False
def set_user_agent(self, user_agent):
source = self.player.get_property('source')
try:
source.set_property("user-agent", user_agent)
except:
logging.debug ("failed to set user-agent to %s" % user_agent)
pass
def set_rectangle_size(self, x, y, w, h):
# check we are using ismd sink
if self.uses_ismd:
logging.debug ("rectangle size changed to %s %d %d %d %d" % (self._videosink, x, y, w, h))
self._videosink.set_property ("rectangle", "%d,%d,%d,%d" % (x, y, w, h))
return True
return False
def get_duration(self):
# if we are in a live stream
if self.ts_mode:
return -1
# attempt to get a duration
try:
duration = self.player.query_duration(gst.FORMAT_TIME, None)[0]
return duration * (1e-09)
except:
return -1
class uplayerDBUSService(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName(UPLAYER_BUS_NAME, bus=dbus.SystemBus())
dbus.service.Object.__init__(self, bus_name, UPLAYER_OBJ_PATH)
self.player = GstPlayer()
@dbus.service.signal(UPLAYER_BUS_NAME)
def emitEOSSignal(self):
logging.debug ("EOS signal sent")
return
@dbus.service.signal(UPLAYER_BUS_NAME, signature='s')
def emitNewURI(self, uri):
logging.debug ("New URI signal sent")
return
@dbus.service.signal(UPLAYER_BUS_NAME, signature='i')
def emitDuration(self, duration):
logging.debug ("Duration signal sent (%d)", duration)
return
@dbus.service.signal(UPLAYER_BUS_NAME)
def emitPlaying(self):
logging.debug ("Playing signal sent")
return
@dbus.service.signal(UPLAYER_BUS_NAME)
def emitError(self, msg):
logging.debug ("Error signal sent")
return
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='s', out_signature='b')
def set_uri(self, uri):
return self.player.set_uri(uri);
@dbus.service.method(UPLAYER_BUS_NAME)
def play(self):
self.player.play();
@dbus.service.method(UPLAYER_BUS_NAME)
def pause(self):
self.player.pause();
@dbus.service.method(UPLAYER_BUS_NAME)
def stop(self):
self.player.stop();
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='', out_signature='i')
def get_position(self):
return self.player.get_position()
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='i', out_signature='')
def seek(self, position):
self.player.seek(position)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='i', out_signature='')
def set_rate(self, rate):
self.player.set_rate(rate)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='s', out_signature='b')
def set_user_agent(self, user_agent):
return self.player.set_user_agent(user_agent)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='iiii', out_signature='')
def set_rectangle_size(self, x, y, w, h):
self.player.set_rectangle_size(x,y,w,h)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='i', out_signature='b')
def set_gdl_plane(self, gdl_plane):
return self.player.set_gdl_plane (gdl_plane)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='', out_signature='i')
def get_duration(self):
return self.player.get_duration ()
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='i', out_signature='b')
def set_xwindow_id(self, window_id):
return self.player.set_window_id (window_id)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='d', out_signature='b')
def set_volume(self, volume):
return self.player.set_volume(volume)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='i', out_signature='b')
def set_jump(self, length):
return self.player.set_jump(length)
@dbus.service.method(UPLAYER_BUS_NAME,
in_signature='b', out_signature='b')
def set_ts_mode(self, ts):
return self.player.set_ts(ts)
DBusGMainLoop(set_as_default=True)
uplayerService = uplayerDBUSService()
player = GstPlayer()
def send_eos ():
uplayerService.emitEOSSignal()
def send_new_uri (uri):
uplayerService.emitNewURI(uri)
def send_duration (duration):
uplayerService.emitDuration(duration)
def send_playing ():
uplayerService.emitPlaying()
def send_error_message (msg):
uplayerService.emitError(msg)
logging.debug ("Starting uplayer...")
loop = gobject.MainLoop()
loop.run()