-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyzm_player.py
executable file
·936 lines (845 loc) · 31.8 KB
/
pyzm_player.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
#!/usr/bin/env python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
# for mainloop in listeners
import gobject
gobject.threads_init()
# python gstreamer bindings
import gst
# callbacks are organized in dict
import collections
# input parsing
import getopt
# __main__ keep alive is based on polling to
# check is listener threads are alive, sleep
# between polls
from time import sleep
# to exit __main__
import sys
# enables verification of url
from urllib2 import urlopen
# enables verification of local files
import os
# used for:
# - gst,zmq listeners,
# - bg metadata lookup
import threading
# metadata reading
import tagget
# server-client communication
import zmq
# server-client msg builder, defs, etc
import shared
class GstListener(threading.Thread):
"""
Thread to listen on gst bus and catch events
relevant to the player.
Takes a callback function as Class argument on _init__,
this callback will be executed when any message arrives
on the gst bus. Messages are not processes by this class.
"""
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
threading.Thread.__init__(self, group=group, target=target,
name=name, verbose=verbose)
self.args = args
self.kargs = kwargs
# don't know how to get out of self.loop.run(), so just die
# when everybody else dies
self.daemon = True
return
def run(self):
self.gstBus = self.args[0]
self.cb = self.args[1]
self.gstBus.add_signal_watch()
self.gstBus.connect('message', self.cb)
self.loop = gobject.MainLoop()
gst.info('Running gst listener thread')
self.loop.run()
class GstPlayer():
"""
Gst player class
Must not be used on its own, runs within an instance
of PlayerControl(), which takes care of running the
listening threads requiered to respond to input/events.
Constructor takes a callback function as an argument. This function
will be called on gst.MESSAGE_EOS.
"""
def __init__(self, cb_eos=None):
self.playing = False
self.player = gst.element_factory_make("playbin", "player")
self.bus = self.player.get_bus()
self.cb_eos = cb_eos if not None else self.stop()
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.stop()
elif t == gst.MESSAGE_EOS:
gst.info("File playback completed")
ok,moved = self.cb_eos()
if ok:
gst.debug('eos callback completed, moved queue %d places' % moved)
else:
gst.warning('eos callback failed!')
# if(self.queued):
# self.play()
def set_location(self, location):
print "Loading %s" % location
self.player.set_property('uri', location)
def query_position(self):
"Returns a (position, duration) tuple"
try:
position, format = self.player.query_position(gst.FORMAT_TIME)
except:
position = gst.CLOCK_TIME_NONE
try:
duration, format = self.player.query_duration(gst.FORMAT_TIME)
except:
duration = gst.CLOCK_TIME_NONE
return (position, duration)
# def seek(self, location):
# """
# @param location: time to seek to, in nanoseconds
# """
# gst.debug("seeking to %r" % location)
# event = gst.event_new_seek(1.0, gst.FORMAT_TIME,
# gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
# gst.SEEK_TYPE_SET, location,
# gst.SEEK_TYPE_NONE, 0)
# res = self.player.send_event(event)
# if res:
# gst.info("setting new stream time to 0")
# self.player.set_new_stream_time(0L)
# else:
# gst.error("seek to %r failed" % location)
def pause(self):
gst.info("pausing player")
self.player.set_state(gst.STATE_PAUSED)
self.playing = False
def play(self):
gst.info("playing player. src:%s" % self.get_current)
self.player.set_state(gst.STATE_PLAYING)
self.playing = True
def stop(self):
self.player.set_state(gst.STATE_NULL)
gst.info("stopped player")
self.playing = False
def get_state(self, timeout=1):
return self.player.get_state(timeout=timeout)
def is_playing(self):
return self.playing
def get_current(self):
ans = []
try:
ans = self.player.get_property('uri')
except Exception as e:
err_msg = 'Failed to get current. Exception:%s' % e.__str__()
gst.error(err_msg)
return ans
class Listener(threading.Thread):
"""
Listen for commands on src (zmq or stdin).
Class constructor takes 3 arguments:
- cb : Callback function that responds to input.
- src : Indicates if input will come from 'zmq' or 'stdin'
- port: Determines which port the 'zmq' server should bind to.
"""
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
threading.Thread.__init__(self, group=group, target=target,
name=name, verbose=verbose)
self.args = args
self.kargs = kwargs
self.stop_request = threading.Event()
return
def run(self):
# arg list: cb,src,port=555
assert(len(self.args) == 3)
self.threadID = 1
self.cb = self.args[0]
self.src = self.args[1]
self.port = self.args[2]
self.context = None
self.server = None
self.name = 'Listener'
gst.debug('%s listener thread starting....' % self.src)
if self.src == 'zmq':
# zmq_init
self.context = zmq.Context()
self.server = self.context.socket(zmq.REP)
self.server.bind('tcp://*:%d' % self.port)
print 'zmq listener running on port %d' % self.port
# run loop
while not self.stop_request.is_set():
gst.info('Waiting for %s input...' % self.src)
if(self.src == 'zmq'):
msg = self.server.recv()
else:
msg = raw_input('Command:')
gst.info('Rx: %s' % msg)
ans = self.cb(msg)
gst.debug('Callback exe completed')
if(self.src == 'zmq'):
self.server.send(ans)
gst.debug('Answer sent to client: %s' % ans)
# zmq_deinit
if self.context != None:
gst.debug('Cleaning up zmq')
if self.server != None:
self.server.close()
self.context.term()
def __del__(self):
gst.debug('Listener thread finished')
def join(self, timeout=None):
self.stop_request.set()
class PlayerControl():
"""
PlayerControl plays mp3 files.
Receives commands over zmq or stdin.
Constructor takes the following arguments:
- src : Indicates if server should listen on
either 'stdin' or 'zmq'.
- port : Port to which the 'zmq' server should
bind to.
- verify_on_load: If True, then before adding a file to the
queue the server will check if the file exists.
NOTE: In order to be able to add directories to the play
queue, the server must be run with verify_on_load=True
Usage example:
pl = PlayerControl('zmq',5556)
pl.start()
# now run a client and send commands, loop
# until client sends 'quit'.
while pl.is_alive():
sleep(0.5)
"""
handlers = None
queue = None
queue_pos= None
# zmq variables
port = None
context = None
server = None
src = None
def __init__(self, src, port=5555, verify_on_load=False):
self.port = port
self.src = src
self.verify_on_load = verify_on_load
self.player = GstPlayer(self.queue_next)
self.gst_listener = GstListener(args=(self.player.bus, self.player.on_message),kwargs=[])
self.listener = Listener(args=(self.handle_cmd, self.src, self.port),kwargs=[])
self.queue = list()
self.queue_pos = -1
# init callbacks
self.handlers = collections.defaultdict(set)
self.register('play',self.play)
self.register('stop',self.stop)
self.register('status',self.status)
self.register('queue_add',self.queue_add)
self.register('queue_del',self.queue_del)
self.register('queue_get',self.queue_get)
self.register('queue_clear',self.queue_clear)
self.register('queue_next',self.queue_next)
self.register('queue_prev',self.queue_prev)
self.register('quit',self.quit)
self.register('help',self.help)
def __enter__(self):
return self
def __del__(self):
""" Cleanup zmq stuff if any exists """
gst.debug("Terminating server...")
def register(self, event, callback):
self.handlers[event].add(callback)
def fire(self, event, **kwargs):
for handler in self.handlers.get(event, []):
return handler(**kwargs)
def queue_add(self, location):
"""
Adds location to the play queue.
NOTES:
- location must be a valid URI
- If self.verify_on_load==True, then location
can be a directory. All .mp3 files from the
directory will be loaded.
Return values:
- If successful: [200]
- else: [err_code,[err_msg]].
"""
ans = [200]
is_dir = False
is_ascii = True
# ignore non-ascii stuff
try:
unicode(location)
except UnicodeDecodeError as e:
is_ascii = False
gst.warning('Ignoring %s, not ascii' % location)
ans = [406]
ans.append(e.__str__())
if not is_ascii or not gst.uri_is_valid(location):
if is_ascii:
gst.error("Error: Invalid URI: %s\nExpected uri"
"like file:///home/foo/bar.mp3\nIgnoring...\n" % location)
ans = [403]
else:
# if(self.player.status()):
# self.queued = True
gst.debug('URI is valid: %s' % location)
err_msg = []
code = 0
msg = ''
if self.verify_on_load:
gst.debug('Attempting to verify %s' % location)
if location[0:4] == 'file':
path = location[7:]
try:
with open(path) as f: pass
except IOError as e:
if e.errno == 21:
is_dir = True
loaded = []
try:
# is directory, load content
os.chdir(path)
for f in os.listdir('.'):
if f.endswith('.mp3'):
full_path = location+'/'+f
res = self.queue_add(full_path)
if res[0] == 200:
loaded.append(full_path)
ans.append(loaded)
except IOError as e:
err_msg = 'Error parsing directory, will'\
'stop loading, exception:%s' % e.__str__()
gst.error(err_msg)
ans = [404]
ans.append(err_msg)
else:
err_msg = 'Will not load, exception when opening'\
'%s: %s' % (path,e.__str__())
gst.error(err_msg)
ans = [404]
ans.append(err_msg)
elif location[0:4] == 'http':
try:
urlopen_ans = urlopen(location)
code = urlopen_ans.code
msg = urlopen_ans.msg
if code >= 400:
err_msg = 'urlopen failed with %d: %s' % (code,msg)
except:
err_msg = 'urlopen() failed'
if err_msg:
ans = [400]
ans.append(err_msg)
if not err_msg:
gst.debug('Verification succeeded!')
if err_msg:
gst.warning(err_msg)
elif not is_dir:
gst.debug('Setting location to %s' % location)
try:
if location in self.queue:
gst.info('Duplicate entry %s' % location)
self.queue.append({'uri':location,'tags':{}})
try:
gst.debug('will prepare tag getter: get_tags(%s)' % location)
tgt = threading.Thread(target=tagget.get_tags,
args=[self.queue[-1]['tags'],
location])
gst.debug('will start tag getter')
tgt.start()
except Exception as e:
err_msg = 'Problem near tag get thread. Exception:%s' % e.__str__()
gst.warning(err_msg)
# call to print current queue
self.queue_get()
if self.queue_pos == -1:
gst.debug('New queue, will next()')
ans_n = self.queue_next()
if ans_n[0] == 200:
gst.debug('New queue play success!')
else:
ans = [400]
ans.append('Failed to set initial queue position')
except Exception as e:
print e
ans = [400]
gst.error('Problem near queue.append()')
return ans
def queue_item_by_uri(self,uri):
try:
elem = (item for item in self.queue if item['uri'] == uri).next()
except StopIteration as e:
err_msg = 'Exception:%s' % e.__str__()
return False
return elem
def queue_del(self,uri=None,pos=-1):
"""Remove an item from the queue. Item can be selected
either by URI or by pos (position) in the queue.
Removing the currently playing element will not stop playback.
"""
ans = [200]
ind = -1
if uri:
try:
elem = self.queue_item_by_uri(uri)
self.queue.index(elem)
self.queue.remove(uri)
ans.append('Removed element %s' % uri)
except (ValueError, StopIteration) as e:
err_msg = 'Exception:%s' % e.__str__()
gst.warning(err_msg)
ans = [400]
ans.append(err_msg)
elif pos >= 0:
try:
ind = pos
del self.queue[pos]
ans.append('Removed element at queue[%d]' % pos)
except IndexError as e:
err_msg = 'Failed to remove at %d. Exception:%s' % (pos,e.__str__())
gst.warning(err_msg)
ans = [400]
ans.append(err_msg)
try:
if ans[0] == 200:
if ind < self.queue_pos:
# update queue_pos to new queue size
self.queue_pos -= 1
elif ind == self.queue_pos:
# check if currently selected item was deleted
if not ind and self.queue:
# special case, first element was removed and
# more are left, set queue_pos to head
self.queue_pos = 0
if self.queue_pos >= 0:
uri_new = self.queue[self.queue_pos]['uri']
get.debug('setting uri to %s' % uri_new)
self.player.set_location(uri_new)
else:
# queue is empty now
self.player.set_location(None)
except Exception as e:
print e
err_msg = 'Problem near update queue_pos. Exception:%s' % e.__str__()
gst.error(err_msg)
ans = [400]
ans.append(err_msg)
if ans[0] == 200 and len(ans) > 1:
# call to get debug output
self.queue_get()
return ans
def queue_get(self):
"""
Returns full queue info: uri and tag info.
"""
gst.debug('Will get complete queue info')
return self.queue_info(False)
def queue_info(self,only_uri=False):
"""
Gets the current queue.
Return values:
- If successful: [200,data]
if only_uri:
data=['file:///etc.mp3','http://another.mp3']
else:
data is a list, each entry e is a dict:
e = {'uri':'file:///etc.mp3','tags':{'title':'song name',...}}
- else: [err_code,[err_message]]
"""
ans = [200]
try:
if not only_uri:
gst.debug('Appending full queue info to ans')
ans.append(self.queue)
else:
uris = []
for elem in self.queue:
uris.append(elem['uri'])
gst.debug('Current queue:\n\t%s' % '\n\t'.join(uris))
ans.append(uris)
except Exception as e:
err_msg = 'Problem in queue_get. Exception:%s' % e.__str__()
gst.debug(err_msg)
ans = [400]
ans.append(err_msg)
return ans
def queue_clear(self):
"""
Clears the current queue.
Return values:
- If successful: [200]
- else: [err_code,[err_message]]
"""
ans = [200]
try:
self.queue_pos = -1
self.queue[:] = []
gst.debug('queue cleared')
self.queue_get()
except Exception as e:
err_msg = 'Problem near queue_clear. Exception:%s' % e.__str__()
gst.debug(err_msg)
ans = [400]
ans.append(err_msg)
return ans
def queue_next(self,step=1):
"""
Advances 1 position in queue.
case player state:
- playing: the new track will start playing
- paused : the current track will be preserved. new track will be played
only if user inputs 'next' or ['stop','play'].
- stopped: the new track will not be played until client sends 'play'
Return values:
- If successful [200,positions_moved]
- else: [error_code]
"""
ans = [400]
try:
gst.debug('Will next_prev(%d)' % step)
ok,moved = self.next_prev(step)
gst.debug('next_prev(%d) returned (%f,%d)' % (step,ok,moved))
if ok:
ans = [200]
ans.append(moved)
except Exception as e:
print e
gst.error('Problem near queue_next()')
return ans
def queue_prev(self,step=-1):
"""
Just like 'queue_next()' but backwards'
"""
ans = [400]
if step >= 0:
return ans
try:
ok,moved = self.next_prev(step)
if ok:
ans = [200]
ans.append(moved)
except Exception as e:
print e
gst.error('Problem near queue_prev()')
return ans
def next_prev(self, step):
"""
Move queue step positions (next,back)
Returns a tuple (ok,queue_moved), where
the value queue_moved indicates number of postions
shifted by queue_move(), and is valid only if ok==True
"""
ans = False
queue_moved = 0
if(step == 0):
# come on...
return ans,queue_moved
elif not self.queue:
# queue is empty
gst.debug('queue is empty')
ans = True
return ans,queue_moved
try:
queue_pos_old = self.queue_pos
queue_pos_new = self.queue_pos + step
queue_finished = False
# Verify index is within limits, stop if out of boundries
# This is the typical behaviour of mp3 playing software
# such as Rhythmbox.
if queue_pos_new < 0 or queue_pos_new >= len(self.queue):
gst.debug('queue playback completed, will stop and return to first element')
# queue completed, stop player and return
queue_pos_new = 0
queue_finished = True
queue_moved = abs(queue_pos_new - queue_pos_old)
uri_new = self.queue[queue_pos_new]['uri']
gst.debug('Will set new location to %s' % uri_new)
self.player.set_location(uri_new)
# update queue position
self.queue_pos = queue_pos_new
gst.debug('New queue_pos:%d/%d' % (self.queue_pos, len(self.queue)))
try:
if(self.is_playing()):
gst.debug('Will stop+play to advance in queue')
self.stop()
if queue_finished:
gst.debug('Omiting play(), queue finished')
else:
self.play()
# Mark success
ans = True
except Exception as e:
print e
gst.error('Failed to advance in queue')
except Exception as e:
print e
gst.error('Problem near next()')
return ans,queue_moved
def play(self):
"""
case player status:
- If stopped, starts playback (will run through queue until end)
- If playing/paused, toggles play/pause.
Return values:
- If successful [200]
- else: [err_code,err_message]
"""
ans = [200]
try:
if self.is_playing():
gst.debug('Will pause player')
self.player.pause()
else:
gst.debug('Will play player')
self.player.play()
except Exception as e:
err_msg = 'Problem near play. Exception:%s' % e.__str__()
ans = [400]
ans.append(err_msg)
return ans
def stop(self):
"""
Stops playback.
Wrapper for player.stop()
"""
ans = [200]
try:
gst.debug('Will stop player')
self.player.stop()
except:
ans = [400]
return ans
def is_playing(self):
"""
Wrapper for player.is_playing()
"""
playing = False
try:
playing = self.player.is_playing()
if self.src == 'stdin':
print "Player state: Playing" if playing else "Player state: NOT Playing"
gst.debug('status:%r' % playing)
except Exception as e:
print e
gst.error('Problem near is_playing()')
return playing
def status(self):
"""
Get player playing/!playing
If metadata is available, then the answer will include it.
Return value:
- If successful: [200,True/False,metadata]
- else: [err_code]
"""
ans = [200]
try:
gst.debug(self.queue.__str__())
playing = self.is_playing()
current = self.player.get_current()
tags = self.queue_item_by_uri(current)
if not tags:
# Failed to find tags or search is still
# in progress
tags = {}
ans.append([playing,current,tags])
except Exception as e:
print e
gst.error('Problem near status()')
ans = [400]
return ans
def help(self):
"""
Gets a help message.
Return values:
- If successful [200,help_string]
- else: [error_code]
"""
ans = [200]
try:
help_msg = self.help_msg()
ans.append(help_msg)
except:
ans = [400]
return ans
def help_msg(self):
"""
Returns help message string.
"""
menu = "\n-- Help menu --\n\nValid commands:\n\t"
try:
funcs = '\n\t'.join(self.handlers.keys())
except:
gst.error('Failed to build handler list')
funcs = []
return "%s%s" % (menu,funcs)
def is_registered(self, cmd):
"""
Returns True if the command 'cmd' is registered.
"""
return cmd in self.handlers
def handle_cmd(self, msg):
"""
Receives a msg and executes it if it corresponds to a registered cmd
Return values:
- If successful: [200,data_returned_by_cmd]
- else: [err_code,err_msg]
"""
if self.src == 'zmq':
cmd,cmd_code,args = shared.json_server_dec(msg)
gst.debug('dec: cmd_name=%s,'\
'cmd_code=%d,args=%s' % (cmd,cmd_code,args.__str__()))
else:
words = msg.split()
cmd = words[0]
cmd_code = words[-1]
args = []
cmd_code_dic= 0
ans = [200] # default to OK
if not self.is_registered(cmd) or (self.src == 'stdin' and len(words) < 2):
gst.error("Error: Invalid command: %s, Ignoring...\n" % cmd)
help_msg = self.help_msg()
if(self.src == 'stdin'):
print help_msg
ans = [401]
ans.append(help_msg)
else:
# first arg is command name, last is cmd if
if self.src == 'stdin':
# if src is zmq, then json_server_dec() took
# care of fetching arguments for command
args = words[1:-1]
try:
# convert from string to int
cmd_code = int(cmd_code)
except ValueError as e:
gst.warning('int(cmd_code) failed!Exception:%s' % e.__str__())
try:
# get command id from dict, compare with rx id (must match)
gst.debug('Matching command id with dict values...')
cmd_code_dic = shared.cmd_name_id[cmd]
try:
# check matching
if cmd_code_dic != cmd_code:
gst.error('Command code received %d does not match dict %d'
% (cmd_code,cmd_code_dic))
else:
gst.debug('Command id matched!')
try:
if(args):
gst.debug('Executing cmd=%s with args=%s' % (cmd,args))
args = args[0]
if cmd == 'queue_add':
ans = list(self.queue_add(args))
elif cmd == 'queue_del':
param = -1
try:
param = int(args)
except ValueError as e:
gst.debug('Param is not int. Exception:%s' % e.__str__())
if not param == -1:
ans = list(self.queue_del(pos=param))
else:
ans = list(self.queue_del(uri=args))
#TODO implement generic multi arg command and support for multi load (queue)
# ans = self.fire(cmd,args)
else:
gst.debug('Executing cmd=%s without args' % cmd)
ans = list(self.fire(cmd))
except ValueError as ve:
print 'Exception:',ve
except Exception, e:
print 'Exception:',e
gst.error('Problem near cmd execution: %s' % msg)
ans = [402]
except Exception as e:
print 'Exception:',e
gst.error('Problem near command id matching: (rx,dict)=(%d,%d)' % (cmd_code,cmd_code_dic))
except Exception as e:
print 'Exception:',e
gst.error('Problem near: find Id for cmd: %s' % cmd)
# Take cmd result and prepare answer to send to client
gst.debug('ans(len=%d)=%s' % (len(ans),ans))
assert(len(ans) == 1 or len(ans) == 2) # (code, data)
data = []
if len(ans) == 2:
data = ans[1]
return shared.json_server_enc(cmd_code, ans[0], data)
def start(self):
"""Starts listener threads for both gst and src (stdin,zmq)"""
# start gst_listener thread
self.gst_listener.start()
# start input listener thread
self.listener.start()
def is_alive(self):
"""Returns true if both gst and src listener threads are alive"""
return self.gst_listener.is_alive() and self.listener.is_alive()
def quit(self):
"""
Trigger join() on listener thread to terminate server.
When threads die, the server will detect it and quit.
"""
ans = [200]
try:
gst.debug('Triggering join() on %s listener thread...' % self.src)
self.listener.join()
# don't wait, quit() is a callback within listener thread, it will
# never finish if we wait here.
except:
ans = [400]
return ans
def main(argv):
def usage():
help_str = "Valid arguments:\n"\
"\t-f mediaFileUri\t//\t--file=uriOfMediaFile\n"\
"\t-p portNumber\t//\t--port=portNumber\n"\
"\t-l [zmq,stdin]\t//\t--listen=[zmq,stdin]\n"\
"\t-i\t\t//\t--info\n"\
"\t-v\t\t//\t--verify\n"
sys.stderr.write("%s" % help_str)
sys.exit(1)
# default values
port = 5555
file_name = None
listen = 'zmq'
verify = False
# parse command line arguments
# note: Cannot use -h/--help with gst:
# - https://bugzilla.gnome.org/show_bug.cgi?id=625211
# - https://bugzilla.gnome.org/show_bug.cgi?id=549879
arg_list = argv[1:]
try:
opts, args = getopt.getopt(arg_list, "ip:f:l:v", ["i", "port=", "file=", "listen=", "verify"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-i","--info"):
usage()
sys.exit()
elif opt in ("-p", "--port"):
port = int(arg)
elif opt in ("-f", "--file"):
file_name = arg
elif opt in ("-l", "--listen"):
listen = arg
elif opt in ("-v", "--verify"):
verify = True
pl = PlayerControl(listen,port,verify)
if(file_name != None):
pl.load(file_name)
print 'Starting PlayerControl...'
pl.start()
print 'Waiting...'
while pl.is_alive():
sleep(0.5)
if(not pl.listener.is_alive()):
gst.debug('%s listener thread died, terminating...' % pl.src)
if(not pl.gst_listener.is_alive()):
gst.debug('gst_listener thread died, terminating...')
gst.debug('Gracefully finishing...')
if __name__ == '__main__':
sys.exit(main(sys.argv))