forked from dohliam/stream2chromecast
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cc_media_controller.py
505 lines (319 loc) · 15.4 KB
/
cc_media_controller.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
"""
Provides a control interface to the Chromecast Media Player app
version 0.2.1
"""
# Copyright (C) 2014-2016 Pat Carter
#
# This file is part of Stream2chromecast.
#
# Stream2chromecast is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Stream2chromecast 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Stream2chromecast. If not, see <http://www.gnu.org/licenses/>.
import socket, ssl, select
import json
import sys
import time
import re
import cc_device_finder
import cc_message
MEDIAPLAYER_APPID = "CC1AD845"
class CCMediaController():
def __init__(self, device_name=None):
""" initialise """
self.host = self.get_device(device_name)
self.sock = None
self.request_id = 1
self.source_id = "sender-0"
self.receiver_app_status = None
self.media_status = None
self.volume_status = None
self.current_applications = None
def get_device(self, device_name):
""" get the device ip address """
host = None
dummy = None
is_ip_addr = device_name is not None and re.match( "[0-9]+.[0-9]+.[0-9]+.[0-9]+$", device_name) is not None
if is_ip_addr:
host = device_name
try:
#print "ip_addr:", host.encode('utf-8'), "device name:", cc_device_finder.get_device_name(host).encode('utf-8')
print >> sys.stderr, "ip_addr:", host.encode('utf-8'), "device name:", cc_device_finder.get_device_name(host).encode('utf-8')
except socket.error:
sys.exit("No Chromecast found on ip:" + host)
else:
host, name = cc_device_finder.find_device(name=device_name)
if host is None:
sys.exit("No Chromecast found on the network")
#print "device name:", name
print >> sys.stderr, "device name:", name
return host
def open_socket(self):
""" open a socket if there is not currently one open """
if self.sock is None:
self.sock = socket.socket()
self.sock = ssl.wrap_socket(self.sock)
self.sock.connect((self.host,8009))
def close_socket(self):
""" close the socket if there is one open """
if self.sock is not None:
self.sock.close()
self.sock = None
def send_data(self, namespace, data_dict):
""" send data to the device in binary format"""
data = json.dumps(data_dict)
#print "Sending: ", namespace, data
msg = cc_message.format_message(self.source_id, self.destination_id, namespace, data)
self.sock.write(msg)
def read_message(self):
""" read a complete message from the device """
data = ""
while len(data) < 4:
data += self.sock.recv(4)
msg_length, data = cc_message.extract_length_header(data)
while len(data) < msg_length:
data += self.sock.recv(2048)
message_dict = cc_message.extract_message(data)
message = {}
try:
message = json.loads(message_dict['data'])
except:
pass
#print message_dict['namespace']
#print json.dumps(message, indent=4, separators=(',', ': '))
return message
def get_response(self, request_id):
""" get the response matching the original request id """
resp = {}
count = 0
while len(resp) == 0:
msg = self.read_message()
msg_type = msg.get("type", msg.get("responseType", ""))
if msg_type == "PING":
data = {"type":"PONG"}
namespace = "urn:x-cast:com.google.cast.tp.heartbeat"
self.send_data(namespace, data)
# if 30 ping/pong messages are received without a response to the request_id,
# assume no response is coming
count += 1
if count == 30:
return resp
elif msg_type == "RECEIVER_STATUS":
self.update_receiver_status_data(msg)
elif msg_type == "MEDIA_STATUS":
self.update_media_status_data(msg)
if "requestId" in msg.keys() and msg['requestId'] == request_id:
resp = msg
return resp
def send_msg_with_response(self, namespace, data):
""" send a request to the device and wait for a response matching the request id """
self.request_id += 1
data['requestId'] = self.request_id
self.send_data(namespace, data)
return self.get_response(self.request_id)
def update_receiver_status_data(self, msg):
""" update the status for the Media Player app if it is running """
self.receiver_app_status = None
if msg.has_key('status'):
status = msg['status']
if status.has_key('applications'):
self.current_applications = status['applications']
for application in self.current_applications:
if application.get("appId") == MEDIAPLAYER_APPID:
self.receiver_app_status = application
if status.has_key('volume'):
self.volume_status = status['volume']
def update_media_status_data(self, msg):
""" update the media status if there is any media loaded """
self.media_status = None
status = msg.get("status", [])
if len(status) > 0:
self.media_status = status[0] # status is an array - selecting the first result..?
def connect(self, destination_id):
""" connect to to the receiver or the media transport """
if self.sock is None:
self.open_socket()
self.destination_id = destination_id
data = {"type":"CONNECT","origin":{}}
namespace = "urn:x-cast:com.google.cast.tp.connection"
self.send_data(namespace, data)
def get_receiver_status(self):
""" send a status request to the receiver """
data = {"type":"GET_STATUS"}
namespace = "urn:x-cast:com.google.cast.receiver"
self.send_msg_with_response(namespace, data)
def get_media_status(self):
""" send a status request to the media player """
data = {"type":"GET_STATUS"}
namespace = "urn:x-cast:com.google.cast.media"
self.send_msg_with_response(namespace, data)
def load(self, content_url, content_type, sub, sub_language):
""" Launch the player app, load & play a URL """
self.connect("receiver-0")
self.get_receiver_status()
# we only set the receiver status for MEDIAPLAYER - so if it is set, the app is currenty running
if self.receiver_app_status is None:
data = {"type":"LAUNCH","appId":MEDIAPLAYER_APPID}
namespace = "urn:x-cast:com.google.cast.receiver"
self.send_msg_with_response(namespace, data)
# if there is still no receiver app status the launch failed.
if self.receiver_app_status is None:
self.close_socket()
sys.exit("Cannot launch the Media Player app")
session_id = str(self.receiver_app_status['sessionId'])
transport_id = str(self.receiver_app_status['transportId'])
self.connect(transport_id)
data = {"type":"LOAD",
"sessionId":session_id,
"media":{
"contentId":content_url,
"streamType":"buffered",
"contentType":content_type,
},
"autoplay":True,
"currentTime":0,
"customData":{
"payload":{
"title:":""
}
}
}
if sub:
if sub_language is None:
sub_language = "en-US"
data["media"].update({
"textTrackStyle":{
'backgroundColor':'#FFFFFF00'
},
"tracks": [{"trackId": 1,
"trackContentId": sub,
"type": "TEXT",
"language": sub_language,
"subtype": "SUBTITLES",
"name": "Englishx",
"trackContentType": "text/vtt",
}],
})
data["activeTrackIds"] = [1]
namespace = "urn:x-cast:com.google.cast.media"
resp = self.send_msg_with_response(namespace, data)
# wait for the player to return "BUFFERING", "PLAYING" or "IDLE"
if resp.get("type", "") == "MEDIA_STATUS":
if self.media_status != None:
player_state = self.media_status.get("playerState", "")
else:
player_state = ""
while player_state != "PLAYING" and player_state != "IDLE" and player_state != "BUFFERING":
time.sleep(2)
self.get_media_status()
if self.media_status != None:
player_state = self.media_status.get("playerState", "")
self.close_socket()
def control(self, command, parameters={}):
""" send a control command to the player """
self.connect("receiver-0")
self.get_receiver_status()
if self.receiver_app_status is None:
print >> sys.stderr, "No media player app running"
self.close_socket()
return
transport_id = str(self.receiver_app_status['transportId'])
self.connect(transport_id)
self.get_media_status()
media_session_id = 1
if self.media_status is not None:
media_session_id = self.media_status['mediaSessionId']
data = {"type":command, "mediaSessionId":media_session_id}
data.update(parameters) # for additional parameters
namespace = "urn:x-cast:com.google.cast.media"
self.send_msg_with_response(namespace, data)
self.close_socket()
def get_status(self):
""" get the receiver and media status """
self.connect("receiver-0")
self.get_receiver_status()
if self.receiver_app_status is not None:
transport_id = str(self.receiver_app_status['transportId'])
self.connect(transport_id)
self.get_media_status()
application_list = []
if self.current_applications is not None:
for application in self.current_applications:
application_list.append({
'appId':application.get('appId', ""),
'displayName':application.get('displayName', ""),
'statusText':application.get('statusText', "")})
status = {'receiver_status':self.receiver_app_status,
'media_status':self.media_status,
'host':self.host,
'client':self.sock.getsockname(),
'applications':application_list}
self.close_socket()
return status
def is_idle(self):
""" return the IDLE state of the player """
status = self.get_status()
if status['media_status'] is None:
if status['receiver_status'] is None:
return True
else:
return status['receiver_status'].get("statusText", "") == u"Ready To Cast" or status['receiver_status'].get("statusText", "") == u"Default Media Receiver"
else:
return status['media_status'].get("playerState", "") == u"IDLE"
def pause(self):
""" pause """
self.control("PAUSE")
def play(self):
""" unpause """
self.control("PLAY")
def stop(self):
""" stop """
self.control("STOP")
self.connect("receiver-0")
data = {"type":"STOP"}
namespace = "urn:x-cast:com.google.cast.receiver"
self.send_msg_with_response(namespace, data)
self.close_socket()
def set_volume(self, level):
""" set the receiver volume - a float value in level for absolute level or "+" / "-" indicates up or down"""
self.connect("receiver-0")
if level in ("+", "-"):
self.get_receiver_status()
if self.volume_status is not None:
curr_level = self.volume_status['level']
if level == "+":
level = 0.1 + curr_level
elif level == "-":
level = curr_level - 0.1
data = {"type":"SET_VOLUME", "volume":{"muted":False, "level":level} }
namespace = "urn:x-cast:com.google.cast.receiver"
self.send_msg_with_response(namespace, data)
self.close_socket()
def get_volume_ext(self):
""" get the current volume level """
self.get_status()
vol = None
if self.volume_status is not None:
vol = self.volume_status.get('level', None)
return vol
def get_volume(self):
""" get the current volume level """
self.get_status()
vol = None
if self.volume_status is not None:
vol = self.volume_status.get('level', None)
return vol
def set_volume_up(self):
""" increase volume by one step """
self.set_volume("+")
def set_volume_down(self):
""" decrease volume by one step """
self.set_volume("-")