-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstreamer_renderer.py
1086 lines (921 loc) · 44.3 KB
/
gstreamer_renderer.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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# Copyright 2006,2007,2008,2009 Frank Scholz <[email protected]>
from sets import Set
from twisted.internet import reactor, defer
from twisted.internet.task import LoopingCall
from twisted.python import failure
from coherence.upnp.core.soap_service import errorCode
from coherence.upnp.core import DIDLLite
import string
import os, platform
from StringIO import StringIO
import tokenize
import pygst
pygst.require('0.10')
import gst
import coherence.extern.louie as louie
from coherence.extern.simple_plugin import Plugin
from coherence import log
class Player(log.Loggable):
logCategory = 'gstreamer_player'
max_playbin_volume = 1.
def __init__(self, default_mimetype='audio/mpeg', audio_sink_name=None,
video_sink_name=None, audio_sink_options=None,
video_sink_options=None):
self.audio_sink_name = audio_sink_name or "autoaudiosink"
self.video_sink_name = video_sink_name or "autovideosink"
self.audio_sink_options = audio_sink_options or {}
self.video_sink_options = video_sink_options or {}
self.player = None
self.source = None
self.sink = None
self.bus = None
self.views = []
self.playing = False
self.duration = None
self.mimetype = default_mimetype
self.create_pipeline(self.mimetype)
def add_view(self,view):
self.views.append(view)
def remove_view(self,view):
self.views.remove(view)
def update(self,message=None):
for v in self.views:
v(message=message)
def _is_not_playbin2_friendly(self):
uname = platform.uname()[1]
result = False
if uname.startswith('Nokia'):
try:
device = uname.split("-")[1]
except:
device = "unknown"
result = device != "N900"
return result
def create_pipeline(self, mimetype):
self.debug("creating pipeline")
if self._is_not_playbin2_friendly():
self.bus = None
self.player = None
self.source = None
self.sink = None
if mimetype == 'application/ogg':
self.player = gst.parse_launch('gnomevfssrc name=source ! oggdemux ! ivorbisdec ! audioconvert ! dsppcmsink name=sink')
self.player.set_name('oggplayer')
self.set_volume = self.set_volume_dsp_pcm_sink
self.get_volume = self.get_volume_dsp_pcm_sink
elif mimetype == 'application/flac':
self.player = gst.parse_launch('gnomevfssrc name=source ! flacdemux ! flacdec ! audioconvert ! dsppcmsink name=sink')
self.player.set_name('flacplayer')
self.set_volume = self.set_volume_dsp_pcm_sink
self.get_volume = self.get_volume_dsp_pcm_sink
else:
self.player = gst.parse_launch('gnomevfssrc name=source ! id3lib ! dspmp3sink name=sink')
self.player.set_name('mp3player')
self.set_volume = self.set_volume_dsp_mp3_sink
self.get_volume = self.get_volume_dsp_mp3_sink
self.source = self.player.get_by_name('source')
self.sink = self.player.get_by_name('sink')
self.player_uri = 'location'
self.mute = self.mute_hack
self.unmute = self.unmute_hack
self.get_mute = self.get_mute_hack
else:
self.player = gst.element_factory_make('playbin2', 'player')
self.player_uri = 'uri'
self.source = self.sink = self.player
self.set_volume = self.set_volume_playbin
self.get_volume = self.get_volume_playbin
self.mute = self.mute_playbin
self.unmute = self.unmute_playbin
self.get_mute = self.get_mute_playbin
audio_sink = gst.element_factory_make(self.audio_sink_name)
self._set_props(audio_sink, self.audio_sink_options)
self.player.set_property("audio-sink", audio_sink)
video_sink = gst.element_factory_make(self.video_sink_name)
self._set_props(video_sink, self.video_sink_options)
self.player.set_property("video-sink", video_sink)
self.bus = self.player.get_bus()
self.player_clean = True
self.bus.connect('message', self.on_message)
self.bus.add_signal_watch()
self.update_LC = LoopingCall(self.update)
def _set_props(self, element, props):
for option, value in props.iteritems():
value = self._py_value(value)
element.set_property(option, value)
def _py_value(self, s):
value = None
g = tokenize.generate_tokens(StringIO(s).readline)
for toknum, tokval, _, _, _ in g:
if toknum == tokenize.NUMBER:
if '.' in tokval:
value = float(tokval)
else:
value = int(tokval)
elif toknum == tokenize.NAME:
value = tokval
if value is not None:
break
return value
def get_volume_playbin(self):
""" playbin volume is a double from 0.0 - 10.0
"""
volume = self.sink.get_property('volume')
return int((volume*100) / self.max_playbin_volume)
def set_volume_playbin(self, volume):
volume = int(volume)
if volume < 0:
volume=0
if volume > 100:
volume=100
volume = (volume * self.max_playbin_volume) / 100.
self.sink.set_property('volume', volume)
def get_volume_dsp_mp3_sink(self):
""" dspmp3sink volume is a n in from 0 to 65535
"""
volume = self.sink.get_property('volume')
return int(volume*100/65535)
def set_volume_dsp_mp3_sink(self, volume):
volume = int(volume)
if volume < 0:
volume=0
if volume > 100:
volume=100
self.sink.set_property('volume', volume*65535/100)
def get_volume_dsp_pcm_sink(self):
""" dspmp3sink volume is a n in from 0 to 65535
"""
volume = self.sink.get_property('volume')
return int(volume*100/65535)
def set_volume_dsp_pcm_sink(self, volume):
volume = int(volume)
if volume < 0:
volume=0
if volume > 100:
volume=100
self.sink.set_property('volume', volume*65535/100)
def mute_playbin(self):
self.player.set_property('mute', True)
def unmute_playbin(self):
self.player.set_property('mute', False)
def get_mute_playbin(self):
return self.player.get_property('mute')
def mute_hack(self):
if hasattr(self,'stored_volume'):
self.stored_volume = self.sink.get_property('volume')
self.sink.set_property('volume', 0)
else:
self.sink.set_property('mute', True)
def unmute_hack(self):
if hasattr(self,'stored_volume'):
self.sink.set_property('volume', self.stored_volume)
else:
self.sink.set_property('mute', False)
def get_mute_hack(self):
if hasattr(self,'stored_volume'):
muted = self.sink.get_property('volume') == 0
else:
try:
muted = self.sink.get_property('mute')
except TypeError:
if not hasattr(self,'stored_volume'):
self.stored_volume = self.sink.get_property('volume')
muted = self.stored_volume == 0
except:
muted = False
self.warning("can't get mute state")
return muted
def get_state(self):
return self.player.get_state()
def get_uri(self):
""" playbin2 has an empty uri property after a
pipeline stops, as the uri is nowdays the next
track to play, not the current one
"""
if self.player.get_name() != 'player':
return self.source.get_property(self.player_uri)
else:
try:
return self.current_uri
except:
return None
def set_uri(self,uri):
self.source.set_property(self.player_uri, uri.encode('utf-8'))
if self.player.get_name() == 'player':
self.current_uri = uri.encode('utf-8')
def on_message(self, bus, message):
#print "on_message", message
#print "from", message.src.get_name()
t = message.type
#print t
if t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
self.warning("Gstreamer error: %s,%r" % (err.message, debug))
if self.playing == True:
self.seek('-0')
#self.player.set_state(gst.STATE_READY)
elif t == gst.MESSAGE_TAG:
for key in message.parse_tag().keys():
self.tags[key] = message.structure[key]
#print self.tags
elif t == gst.MESSAGE_STATE_CHANGED:
if message.src == self.player:
old, new, pending = message.parse_state_changed()
#print "player (%s) state_change:" %(message.src.get_path_string()), old, new, pending
if new == gst.STATE_PLAYING:
self.playing = True
self.update_LC.start( 1, False)
self.update()
elif old == gst.STATE_PLAYING:
self.playing = False
try:
self.update_LC.stop()
except:
pass
self.update()
#elif new == gst.STATE_READY:
# self.update()
elif t == gst.MESSAGE_EOS:
self.debug("reached file end")
self.seek('-0')
self.update(message=gst.MESSAGE_EOS)
def query_position( self):
#print "query_position"
try:
position, format = self.player.query_position(gst.FORMAT_TIME)
except:
#print "CLOCK_TIME_NONE", gst.CLOCK_TIME_NONE
position = gst.CLOCK_TIME_NONE
position = 0
#print position
if self.duration == None:
try:
self.duration, format = self.player.query_duration(gst.FORMAT_TIME)
except:
self.duration = gst.CLOCK_TIME_NONE
self.duration = 0
#import traceback
#print traceback.print_exc()
#print self.duration
r = {}
if self.duration == 0:
self.duration = None
self.debug("duration unknown")
return r
r[u'raw'] = {u'position':unicode(str(position)), u'remaining':unicode(str(self.duration - position)), u'duration':unicode(str(self.duration))}
position_human = u'%d:%02d' % (divmod( position/1000000000, 60))
duration_human = u'%d:%02d' % (divmod( self.duration/1000000000, 60))
remaining_human = u'%d:%02d' % (divmod( (self.duration-position)/1000000000, 60))
r[u'human'] = {u'position':position_human, u'remaining':remaining_human, u'duration':duration_human}
r[u'percent'] = {u'position':position*100/self.duration, u'remaining':100-(position*100/self.duration)}
self.debug(r)
return r
def load( self, uri, mimetype):
self.debug("load --> %r %r" % (uri, mimetype))
_,state,_ = self.player.get_state()
if( state == gst.STATE_PLAYING or state == gst.STATE_PAUSED):
self.stop()
#print "player -->", self.player.get_name()
if self.player.get_name() != 'player':
self.create_pipeline(mimetype)
self.player.set_state(gst.STATE_READY)
self.set_uri(uri)
self.player_clean = True
self.duration = None
self.mimetype = mimetype
self.tags = {}
#self.player.set_state(gst.STATE_PAUSED)
#self.update()
self.debug("load <--")
self.play()
def play( self):
uri = self.get_uri()
mimetype = self.mimetype
self.debug("play --> %r %r" % (uri, mimetype))
if self.player.get_name() != 'player':
if self.player_clean == False:
#print "rebuild pipeline"
self.player.set_state(gst.STATE_NULL)
self.create_pipeline(mimetype)
self.set_uri(uri)
self.player.set_state(gst.STATE_READY)
else:
self.player_clean = True
self.player.set_state(gst.STATE_PLAYING)
self.debug("play <--")
def pause(self):
self.debug("pause --> %r" % self.get_uri())
self.player.set_state(gst.STATE_PAUSED)
self.debug("pause <--")
def stop(self):
self.debug("stop --> %r" % self.get_uri())
self.seek('-0')
self.player.set_state(gst.STATE_READY)
self.update(message=gst.MESSAGE_EOS)
self.debug("stop <-- %r " % self.get_uri())
def seek(self, location):
"""
@param location: simple number = time to seek to, in seconds
+nL = relative seek forward n seconds
-nL = relative seek backwards n seconds
"""
_,state,_ = self.player.get_state()
if state != gst.STATE_PAUSED:
self.player.set_state(gst.STATE_PAUSED)
l = long(location)*1000000000
p = self.query_position()
#print p['raw']['position'], l
if location[0] == '+':
l = long(p[u'raw'][u'position']) + (long(location[1:])*1000000000)
l = min( l, long(p[u'raw'][u'duration']))
elif location[0] == '-':
if location == '-0':
l = 0L
else:
l = long(p[u'raw'][u'position']) - (long(location[1:])*1000000000)
l = max( l, 0L)
self.debug("seeking to %r" % l)
"""
self.player.seek( 1.0, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
gst.SEEK_TYPE_SET, l,
gst.SEEK_TYPE_NONE, 0)
"""
event = gst.event_new_seek(1.0, gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_KEY_UNIT,
gst.SEEK_TYPE_SET, l,
gst.SEEK_TYPE_NONE, 0)
res = self.player.send_event(event)
if res:
pass
#print "setting new stream time to 0"
#self.player.set_new_stream_time(0L)
elif location != '-0':
print "seek to %r failed" % location
if location == '-0':
content_type, _ = self.mimetype.split("/")
try:
self.update_LC.stop()
except:
pass
if self.player.get_name() != 'player':
self.player.set_state(gst.STATE_NULL)
self.player_clean = False
elif content_type != "image":
self.player.set_state(gst.STATE_READY)
self.update()
else:
self.player.set_state(state)
if state == gst.STATE_PAUSED:
self.update()
class GStreamerPlayer(log.Loggable,Plugin):
""" a backend with a GStreamer based audio player
needs gnomevfssrc from gst-plugins-base
unfortunately gnomevfs has way too much dependencies
# not working -> http://bugzilla.gnome.org/show_bug.cgi?id=384140
# needs the neonhttpsrc plugin from gst-plugins-bad
# tested with CVS version
# and with this patch applied
# --> http://bugzilla.gnome.org/show_bug.cgi?id=375264
# not working
and id3demux from gst-plugins-good CVS too
"""
logCategory = 'gstreamer_player'
implements = ['MediaRenderer']
vendor_value_defaults = {'RenderingControl': {'A_ARG_TYPE_Channel':'Master'},
'AVTransport': {'A_ARG_TYPE_SeekMode':('ABS_TIME','REL_TIME','TRACK_NR')}}
vendor_range_defaults = {'RenderingControl': {'Volume': {'maximum':100}}}
def __init__(self, device, **kwargs):
if(device.coherence.config.get('use_dbus','no') != 'yes' and
device.coherence.config.get('glib','no') != 'yes'):
raise Exception('this media renderer needs use_dbus enabled in the configuration')
self.name = kwargs.get('name','GStreamer Audio Player')
audio_sink_name = kwargs.get("audio_sink_name")
audio_sink_options = kwargs.get("audio_sink_options")
video_sink_name = kwargs.get("video_sink_name")
video_sink_options = kwargs.get("video_sink_options")
self.player = Player(audio_sink_name=audio_sink_name,
video_sink_name=video_sink_name,
audio_sink_options=audio_sink_options,
video_sink_options=video_sink_options)
self.player.add_view(self.update)
self.metadata = None
self.duration = None
self.view = []
self.tags = {}
self.server = device
self.playcontainer = None
self.dlna_caps = ['playcontainer-0-1']
louie.send('Coherence.UPnP.Backend.init_completed', None, backend=self)
def __repr__(self):
return str(self.__class__).split('.')[-1]
def update(self, message=None):
_, current,_ = self.player.get_state()
self.debug("update current %r", current)
connection_manager = self.server.connection_manager_server
av_transport = self.server.av_transport_server
conn_id = connection_manager.lookup_avt_id(self.current_connection_id)
if current == gst.STATE_PLAYING:
state = 'playing'
av_transport.set_variable(conn_id, 'TransportState', 'PLAYING')
elif current == gst.STATE_PAUSED:
state = 'paused'
av_transport.set_variable(conn_id, 'TransportState',
'PAUSED_PLAYBACK')
elif self.playcontainer != None and message == gst.MESSAGE_EOS and \
self.playcontainer[0]+1 < len(self.playcontainer[2]):
state = 'transitioning'
av_transport.set_variable(conn_id, 'TransportState', 'TRANSITIONING')
next_track = ()
item = self.playcontainer[2][self.playcontainer[0]+1]
infos = connection_manager.get_variable('SinkProtocolInfo')
local_protocol_infos = infos.value.split(',')
res = item.res.get_matching(local_protocol_infos,
protocol_type='internal')
if len(res) == 0:
res = item.res.get_matching(local_protocol_infos)
if len(res) > 0:
res = res[0]
infos = res.protocolInfo.split(':')
remote_protocol, remote_network, remote_content_format, _ = infos
didl = DIDLLite.DIDLElement()
didl.addItem(item)
next_track = (res.data, didl.toString(), remote_content_format)
self.playcontainer[0] = self.playcontainer[0]+1
if len(next_track) == 3:
av_transport.set_variable(conn_id, 'CurrentTrack',
self.playcontainer[0]+1)
self.load(next_track[0], next_track[1], next_track[2])
self.play()
else:
state = 'idle'
av_transport.set_variable(conn_id, 'TransportState', 'STOPPED')
elif message == gst.MESSAGE_EOS and \
len(av_transport.get_variable('NextAVTransportURI').value) > 0:
state = 'transitioning'
av_transport.set_variable(conn_id, 'TransportState', 'TRANSITIONING')
CurrentURI = av_transport.get_variable('NextAVTransportURI').value
metadata = av_transport.get_variable('NextAVTransportURIMetaData')
CurrentURIMetaData = metadata.value
av_transport.set_variable(conn_id, 'NextAVTransportURI', '')
av_transport.set_variable(conn_id, 'NextAVTransportURIMetaData', '')
r = self.upnp_SetAVTransportURI(self, InstanceID=0,
CurrentURI=CurrentURI,
CurrentURIMetaData=CurrentURIMetaData)
if r == {}:
self.play()
else:
state = 'idle'
av_transport.set_variable(conn_id, 'TransportState', 'STOPPED')
else:
state = 'idle'
av_transport.set_variable(conn_id, 'TransportState', 'STOPPED')
self.info("update %r" % state)
self._update_transport_position(state)
def _update_transport_position(self, state):
connection_manager = self.server.connection_manager_server
av_transport = self.server.av_transport_server
conn_id = connection_manager.lookup_avt_id(self.current_connection_id)
position = self.player.query_position()
#print position
for view in self.view:
view.status(self.status(position))
if position.has_key(u'raw'):
if self.duration == None and 'duration' in position[u'raw']:
self.duration = int(position[u'raw'][u'duration'])
if self.metadata != None and len(self.metadata)>0:
# FIXME: duration breaks client parsing MetaData?
elt = DIDLLite.DIDLElement.fromString(self.metadata)
for item in elt:
for res in item.findall('res'):
formatted_duration = self._format_time(self.duration)
res.attrib['duration'] = formatted_duration
self.metadata = elt.toString()
#print self.metadata
if self.server != None:
av_transport.set_variable(conn_id,
'AVTransportURIMetaData',
self.metadata)
av_transport.set_variable(conn_id,
'CurrentTrackMetaData',
self.metadata)
self.info("%s %d/%d/%d - %d%%/%d%% - %s/%s/%s", state,
string.atol(position[u'raw'][u'position'])/1000000000,
string.atol(position[u'raw'][u'remaining'])/1000000000,
string.atol(position[u'raw'][u'duration'])/1000000000,
position[u'percent'][u'position'],
position[u'percent'][u'remaining'],
position[u'human'][u'position'],
position[u'human'][u'remaining'],
position[u'human'][u'duration'])
duration = string.atol(position[u'raw'][u'duration'])
formatted = self._format_time(duration)
av_transport.set_variable(conn_id, 'CurrentTrackDuration', formatted)
av_transport.set_variable(conn_id, 'CurrentMediaDuration', formatted)
position = string.atol(position[u'raw'][u'position'])
formatted = self._format_time(position)
av_transport.set_variable(conn_id, 'RelativeTimePosition', formatted)
av_transport.set_variable(conn_id, 'AbsoluteTimePosition', formatted)
def _format_time(self, time):
fmt = '%d:%02d:%02d'
try:
m, s = divmod(time / 1000000000, 60)
h, m = divmod(m, 60)
except:
h = m = s = 0
fmt = '%02d:%02d:%02d'
formatted = fmt % (h, m, s)
return formatted
def load( self, uri,metadata, mimetype=None):
self.info("loading: %r %r " % (uri, mimetype))
_,state,_ = self.player.get_state()
connection_id = self.server.connection_manager_server.lookup_avt_id(self.current_connection_id)
self.stop(silent=True) # the check whether a stop is really needed is done inside stop
if mimetype is None:
_,ext = os.path.splitext(uri)
if ext == '.ogg':
mimetype = 'application/ogg'
elif ext == '.flac':
mimetype = 'application/flac'
else:
mimetype = 'audio/mpeg'
self.player.load( uri, mimetype)
self.metadata = metadata
self.mimetype = mimetype
self.tags = {}
if self.playcontainer == None:
self.server.av_transport_server.set_variable(connection_id, 'AVTransportURI',uri)
self.server.av_transport_server.set_variable(connection_id, 'AVTransportURIMetaData',metadata)
self.server.av_transport_server.set_variable(connection_id, 'NumberOfTracks',1)
self.server.av_transport_server.set_variable(connection_id, 'CurrentTrack',1)
else:
self.server.av_transport_server.set_variable(connection_id, 'AVTransportURI',self.playcontainer[1])
self.server.av_transport_server.set_variable(connection_id, 'NumberOfTracks',len(self.playcontainer[2]))
self.server.av_transport_server.set_variable(connection_id, 'CurrentTrack',self.playcontainer[0]+1)
self.server.av_transport_server.set_variable(connection_id, 'CurrentTrackURI',uri)
self.server.av_transport_server.set_variable(connection_id, 'CurrentTrackMetaData',metadata)
#self.server.av_transport_server.set_variable(connection_id, 'TransportState', 'TRANSITIONING')
#self.server.av_transport_server.set_variable(connection_id, 'CurrentTransportActions','PLAY,STOP,PAUSE,SEEK,NEXT,PREVIOUS')
if uri.startswith('http://'):
transport_actions = Set(['PLAY,STOP,PAUSE'])
else:
transport_actions = Set(['PLAY,STOP,PAUSE,SEEK'])
if len(self.server.av_transport_server.get_variable('NextAVTransportURI').value) > 0:
transport_actions.add('NEXT')
if self.playcontainer != None:
if len(self.playcontainer[2]) - (self.playcontainer[0]+1) > 0:
transport_actions.add('NEXT')
if self.playcontainer[0] > 0:
transport_actions.add('PREVIOUS')
self.server.av_transport_server.set_variable(connection_id, 'CurrentTransportActions',transport_actions)
if state == gst.STATE_PLAYING:
self.info("was playing...")
self.play()
self.update()
def status( self, position):
uri = self.player.get_uri()
if uri == None:
return {u'state':u'idle',u'uri':u''}
else:
r = {u'uri':unicode(uri),
u'position':position}
if self.tags != {}:
try:
r[u'artist'] = unicode(self.tags['artist'])
except:
pass
try:
r[u'title'] = unicode(self.tags['title'])
except:
pass
try:
r[u'album'] = unicode(self.tags['album'])
except:
pass
if self.player.get_state()[1] == gst.STATE_PLAYING:
r[u'state'] = u'playing'
elif self.player.get_state()[1] == gst.STATE_PAUSED:
r[u'state'] = u'paused'
else:
r[u'state'] = u'idle'
return r
def start( self, uri):
self.load( uri)
self.play()
def stop(self,silent=False):
self.info('Stopping: %r' % self.player.get_uri())
if self.player.get_uri() == None:
return
if self.player.get_state()[1] in [gst.STATE_PLAYING,gst.STATE_PAUSED]:
self.player.stop()
if silent is True:
self.server.av_transport_server.set_variable(self.server.connection_manager_server.lookup_avt_id(self.current_connection_id), 'TransportState', 'STOPPED')
def play( self):
self.info("Playing: %r" % self.player.get_uri())
if self.player.get_uri() == None:
return
self.player.play()
self.server.av_transport_server.set_variable(self.server.connection_manager_server.lookup_avt_id(self.current_connection_id), 'TransportState', 'PLAYING')
def pause( self):
self.info('Pausing: %r' % self.player.get_uri())
self.player.pause()
self.server.av_transport_server.set_variable(self.server.connection_manager_server.lookup_avt_id(self.current_connection_id), 'TransportState', 'PAUSED_PLAYBACK')
def seek(self, location, old_state):
self.player.seek(location)
if old_state != None:
self.server.av_transport_server.set_variable(0, 'TransportState', old_state)
def mute(self):
self.player.mute()
rcs_id = self.server.connection_manager_server.lookup_rcs_id(self.current_connection_id)
self.server.rendering_control_server.set_variable(rcs_id, 'Mute', 'True')
def unmute(self):
self.player.unmute()
rcs_id = self.server.connection_manager_server.lookup_rcs_id(self.current_connection_id)
self.server.rendering_control_server.set_variable(rcs_id, 'Mute', 'False')
def get_mute(self):
return self.player.get_mute()
def get_volume(self):
return self.player.get_volume()
def set_volume(self, volume):
self.player.set_volume(volume)
rcs_id = self.server.connection_manager_server.lookup_rcs_id(self.current_connection_id)
self.server.rendering_control_server.set_variable(rcs_id, 'Volume', volume)
def playcontainer_browse(self,uri):
"""
dlna-playcontainer://uuid%3Afe814e3e-5214-4c24-847b-383fb599ff01?sid=urn%3Aupnp-org%3AserviceId%3AContentDirectory&cid=1441&fid=1444&fii=0&sc=&md=0
"""
from urllib import unquote
from cgi import parse_qs
from coherence.extern.et import ET
from coherence.upnp.core.utils import parse_xml
def handle_reply(r,uri,action,kw):
try:
next_track = ()
elt = DIDLLite.DIDLElement.fromString(r['Result'])
item = elt.getItems()[0]
local_protocol_infos=self.server.connection_manager_server.get_variable('SinkProtocolInfo').value.split(',')
res = item.res.get_matching(local_protocol_infos, protocol_type='internal')
if len(res) == 0:
res = item.res.get_matching(local_protocol_infos)
if len(res) > 0:
res = res[0]
remote_protocol,remote_network,remote_content_format,_ = res.protocolInfo.split(':')
didl = DIDLLite.DIDLElement()
didl.addItem(item)
next_track = (res.data,didl.toString(),remote_content_format)
""" a list with these elements:
the current track index
- will change during playback of the container items
the initial complete playcontainer-uri
a list of all the items in the playcontainer
the action methods to do the Browse call on the device
the kwargs for the Browse call
- kwargs['StartingIndex'] will be modified during further Browse requests
"""
self.playcontainer = [int(kw['StartingIndex']),uri,elt.getItems()[:],action,kw]
def browse_more(starting_index,number_returned,total_matches):
self.info("browse_more", starting_index,number_returned,total_matches)
try:
def handle_error(r):
pass
def handle_reply(r,starting_index):
elt = DIDLLite.DIDLElement.fromString(r['Result'])
self.playcontainer[2] += elt.getItems()[:]
browse_more(starting_index,int(r['NumberReturned']),int(r['TotalMatches']))
if((number_returned != 5 or
number_returned < (total_matches-starting_index)) and
(total_matches-number_returned) != starting_index):
self.info("seems we have been returned only a part of the result")
self.info("requested %d, starting at %d" % (5,starting_index))
self.info("got %d out of %d" % (number_returned, total_matches))
self.info("requesting more starting now at %d" % (starting_index+number_returned))
self.playcontainer[4]['StartingIndex'] = str(starting_index+number_returned)
d = self.playcontainer[3].call(**self.playcontainer[4])
d.addCallback(handle_reply,starting_index+number_returned)
d.addErrback(handle_error)
except:
import traceback
traceback.print_exc()
browse_more(int(kw['StartingIndex']),int(r['NumberReturned']),int(r['TotalMatches']))
if len(next_track) == 3:
return next_track
except:
import traceback
traceback.print_exc()
return failure.Failure(errorCode(714))
def handle_error(r):
return failure.Failure(errorCode(714))
try:
udn,args = uri[21:].split('?')
udn = unquote(udn)
args = parse_qs(args)
type = args['sid'][0].split(':')[-1]
try:
sc = args['sc'][0]
except:
sc = ''
device = self.server.coherence.get_device_with_id(udn)
service = device.get_service_by_type(type)
action = service.get_action('Browse')
kw = {'ObjectID':args['cid'][0],
'BrowseFlag':'BrowseDirectChildren',
'StartingIndex':args['fii'][0],
'RequestedCount':str(5),
'Filter':'*',
'SortCriteria':sc}
d = action.call(**kw)
d.addCallback(handle_reply,uri,action,kw)
d.addErrback(handle_error)
return d
except:
return failure.Failure(errorCode(714))
def upnp_init(self):
self.current_connection_id = None
self.server.connection_manager_server.set_variable(0, 'SinkProtocolInfo',
['internal:%s:audio/mpeg:*' % self.server.coherence.hostname,
'http-get:*:audio/mpeg:*',
'internal:%s:audio/mp4:*' % self.server.coherence.hostname,
'http-get:*:audio/mp4:*',
'internal:%s:application/ogg:*' % self.server.coherence.hostname,
'http-get:*:application/ogg:*',
'internal:%s:audio/ogg:*' % self.server.coherence.hostname,
'http-get:*:audio/ogg:*',
'internal:%s:video/ogg:*' % self.server.coherence.hostname,
'http-get:*:video/ogg:*',
'internal:%s:application/flac:*' % self.server.coherence.hostname,
'http-get:*:application/flac:*',
'internal:%s:audio/flac:*' % self.server.coherence.hostname,
'http-get:*:audio/flac:*',
'internal:%s:video/x-msvideo:*' % self.server.coherence.hostname,
'http-get:*:video/x-msvideo:*',
'internal:%s:video/mp4:*' % self.server.coherence.hostname,
'http-get:*:video/mp4:*',
'internal:%s:video/quicktime:*' % self.server.coherence.hostname,
'http-get:*:video/quicktime:*',
'internal:%s:image/gif:*' % self.server.coherence.hostname,
'http-get:*:image/gif:*',
'internal:%s:image/jpeg:*' % self.server.coherence.hostname,
'http-get:*:image/jpeg:*',
'internal:%s:image/png:*' % self.server.coherence.hostname,
'http-get:*:image/png:*',
'http-get:*:*:*'],
default=True)
self.server.av_transport_server.set_variable(0, 'TransportState', 'NO_MEDIA_PRESENT', default=True)
self.server.av_transport_server.set_variable(0, 'TransportStatus', 'OK', default=True)
self.server.av_transport_server.set_variable(0, 'CurrentPlayMode', 'NORMAL', default=True)
self.server.av_transport_server.set_variable(0, 'CurrentTransportActions', '', default=True)
self.server.rendering_control_server.set_variable(0, 'Volume', self.get_volume())
self.server.rendering_control_server.set_variable(0, 'Mute', self.get_mute())
def upnp_Play(self, *args, **kwargs):
InstanceID = int(kwargs['InstanceID'])
Speed = int(kwargs['Speed'])
self.play()
return {}
def upnp_Pause(self, *args, **kwargs):
InstanceID = int(kwargs['InstanceID'])
self.pause()
return {}
def upnp_Stop(self, *args, **kwargs):
InstanceID = int(kwargs['InstanceID'])
self.stop()
return {}
def upnp_Seek(self, *args, **kwargs):
InstanceID = int(kwargs['InstanceID'])
Unit = kwargs['Unit']
Target = kwargs['Target']
if InstanceID != 0:
return failure.Failure(errorCode(718))
if Unit in ['ABS_TIME','REL_TIME']:
old_state = self.server.av_transport_server.get_variable('TransportState').value
self.server.av_transport_server.set_variable(InstanceID, 'TransportState', 'TRANSITIONING')
sign = ''
if Target[0] == '+':
Target = Target[1:]
sign = '+'
if Target[0] == '-':
Target = Target[1:]
sign = '-'
h,m,s = Target.split(':')
seconds = int(h)*3600 + int(m)*60 + int(s)
self.seek(sign+str(seconds), old_state)
if Unit in ['TRACK_NR']:
if self.playcontainer == None:
NextURI = self.server.av_transport_server.get_variable('NextAVTransportURI',InstanceID).value
if NextURI != '':
self.server.av_transport_server.set_variable(InstanceID, 'TransportState', 'TRANSITIONING')
NextURIMetaData = self.server.av_transport_server.get_variable('NextAVTransportURIMetaData').value
self.server.av_transport_server.set_variable(InstanceID, 'NextAVTransportURI', '')
self.server.av_transport_server.set_variable(InstanceID, 'NextAVTransportURIMetaData', '')
r = self.upnp_SetAVTransportURI(self, InstanceID=InstanceID,CurrentURI=NextURI,CurrentURIMetaData=NextURIMetaData)
return r
else:
Target = int(Target)
if 0 < Target <= len(self.playcontainer[2]):
self.server.av_transport_server.set_variable(InstanceID, 'TransportState', 'TRANSITIONING')
next_track = ()
item = self.playcontainer[2][Target-1]
local_protocol_infos=self.server.connection_manager_server.get_variable('SinkProtocolInfo').value.split(',')
res = item.res.get_matching(local_protocol_infos, protocol_type='internal')
if len(res) == 0:
res = item.res.get_matching(local_protocol_infos)
if len(res) > 0:
res = res[0]
remote_protocol,remote_network,remote_content_format,_ = res.protocolInfo.split(':')
didl = DIDLLite.DIDLElement()
didl.addItem(item)
next_track = (res.data,didl.toString(),remote_content_format)
self.playcontainer[0] = Target-1
if len(next_track) == 3:
self.server.av_transport_server.set_variable(self.server.connection_manager_server.lookup_avt_id(self.current_connection_id), 'CurrentTrack',Target)
self.load(next_track[0],next_track[1],next_track[2])
self.play()
return {}
return failure.Failure(errorCode(711))
return {}
def upnp_Next(self,*args,**kwargs):
InstanceID = int(kwargs['InstanceID'])
track_nr = self.server.av_transport_server.get_variable('CurrentTrack')
return self.upnp_Seek(self,InstanceID=InstanceID,Unit='TRACK_NR',Target=str(int(track_nr.value)+1))
def upnp_Previous(self,*args,**kwargs):
InstanceID = int(kwargs['InstanceID'])
track_nr = self.server.av_transport_server.get_variable('CurrentTrack')
return self.upnp_Seek(self,InstanceID=InstanceID,Unit='TRACK_NR',Target=str(int(track_nr.value)-1))
def upnp_SetNextAVTransportURI(self, *args, **kwargs):