-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
144 lines (130 loc) · 3.91 KB
/
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
import vlc
import socket
import os
import sys
import urllib
import time
MAX_VOL = 130
DEF_PORT = 9988
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = DEF_PORT
v=vlc.Instance()
p=v.media_player_new()
s=socket.socket()
s.bind(('0.0.0.0',port))
s.listen(1)
c=s.accept()
cur_track = '-'
target_vol = 50
while True:
try:
cmd=c[0].recv(1024)
if cmd == '':
raise Exception
except:
print 'connection terminated, waiting for new master'
c=s.accept()
cmd=c[0].recv(1024)
print 'got new master'
try:
print 'received command:',cmd
cmd = cmd.split('\n')[-1].strip().split(' ')
#
# Command is in parts:
#
# "op track time vol"
#
# Where any individual part may be simply "-" character, which
# means unspecified
#
op = cmd[0]
track = cmd[1]
t = cmd[2]
vol = cmd[3]
if vol != '-':
try:
target_vol = int(vol)
p.audio_set_volume(target_vol)
except:
pass
if track != '-':
path = urllib.unquote(track)
#
# If the path is absolute or a url, or really not like our
# expected mounted path format, just try to play that
#
if '/' not in path:
path = '/mnt/'+path[0]+'/'+path[1]+'/'+path
print 'preparing to play track %s' %path
if path[0:7] == 'http://' or os.path.exists(path):
print 'playing track'
m=v.media_new(path)
p.set_media(m)
p.play()
cur_track = track
#
# After we start play, ensure we set vol to desired
# level (in cases where the player could not while in
# another state)
#
p.audio_set_volume(target_vol)
else:
print 'track not valid...'
cur_track = '-'
if op == 'pause':
p.pause()
if op == 'stop':
p.stop()
if op != 'hb' and t != '-':
#
# When given time that is not specified as heartbeat,
# simply seek the player to that time. This could be seek
# command or just adding player to group whose in middle
# of track
#
try:
t = int(t)
p.set_time(t)
except:
pass
#
# Get the state of player. This will be sent in response of
# same format as command where op is replaced by state.
#
# Do this before heartbeat adjustment since it will query this
# (we may get heartbeats out of turn)
#
_state = p.get_state()
state = 'stopped'
if _state == vlc.State.Playing:
state = 'playing'
elif _state == vlc.State.Buffering:
state = 'playing'
elif _state == vlc.State.Opening:
state = 'opening'
elif _state == vlc.State.Paused:
state = 'paused'
if op == 'hb' and t != '-':
#
# Do the synchronization jump and/or rate adjustment
#
(t,r) = t.split('@')
t_delta = abs(p.get_time() - int(t))
r_delta = time.time() - float(r)
print 'got time offset %d with ref clock offset %0.5f' %(t_delta,r_delta)
if t_delta > 70 and state == 'playing':
# p.set_time(t)
pass
continue
#
# Get other status info,
t = p.get_time()
vol = p.audio_get_volume()
res = '%s %s %d %d' %(state,cur_track,t,vol)
print 'responding with %s' %res
c[0].send(res+'\n')
except:
print 'caught exception',sys.exc_info()
c[0].send('- - - -\n')