-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcast.html
163 lines (138 loc) · 6.25 KB
/
cast.html
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
<!--
Copyright 2017 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
This sample demonstrates how to build your own Receiver for use with Google
Cast.
-->
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="about:blank"></link>
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
<script src="//console.re/connector.js" data-channel="cx-chromecast-1234" id="consolerescript"></script>
</head>
<body>
<cast-media-player id="player"></cast-media-player>
<script>
var isPlaying = false;
console.re.log('Starting receiver...');
const context = cast.framework.CastReceiverContext.getInstance();
const playerManager = context.getPlayerManager();
var sessionToken = null;
playerManager.addEventListener(cast.framework.events.EventType.ALL,
function (event) {
console.re.log('event: %s', event);
});
playerManager.addEventListener(cast.framework.events.EventType.TIME_UPDATE,
function (event) {
if (event.currentMediaTime > 1)
{
isPlaying = true;
}
});
playerManager.setMediaPlaybackInfoHandler((loadRequest, playbackConfig) => {
var gNEOriginal = shaka.Player.prototype.getNetworkingEngine;
shaka.Player.prototype.getNetworkingEngine = function() {
var networkingEngine = gNEOriginal.apply(this, arguments);
networkingEngine.registerResponseFilter(function(type, response) {
if (type == shaka.net.NetworkingEngine.RequestType.LICENSE) {
console.re.log('networkingEngine : %s', response);
}
});
return networkingEngine;
};
console.re.log('load request: %s', loadRequest);
playbackConfig.licenseUrl = loadRequest.media.customData.licServer;
if (loadRequest.media.customData.licServer.indexOf('.asmx') >= 0) {
playbackConfig.protectionSystem = cast.framework.ContentProtection.PLAYREADY;
}
else {
playbackConfig.protectionSystem = cast.framework.ContentProtection.WIDEVINE;
}
return getPreauthToken(loadRequest.media.customData.tokenOrUrl).then(tokenData => {
playbackConfig.licenseHandler = licenseData => {
var asBase64 = shaka.util.Uint8ArrayUtils.toBase64(licenseData);
console.re.log('licenseData length: %s, data: %s', licenseData.length, asBase64);
return licenseData;
};
playbackConfig.licenseRequestHandler = requestInfo => {
console.re.log('tokenData: %s', tokenData);
Object.assign(requestInfo.headers, tokenData);
if (isPlaying)
{
requestInfo.url = loadRequest.media.customData.renewUrl;
}
else
{
requestInfo.url = loadRequest.media.customData.licServer;
}
//requestInfo.headers['nv-tenant-id'] = loadRequest.media.customData.tenantId;
//requestInfo.headers['PreAuthorization'] = token;
console.re.log('licenseRequestHandler requestInfo: %s', requestInfo);
}
console.re.log('playbackConfig: %s', playbackConfig);
return playbackConfig;
});
});
function getPreauthToken(tokenOrUrl) {
console.re.log('getPreauthToken, tokenOrUrl: %s', tokenOrUrl);
if (tokenOrUrl.indexOf('http') >= 0) {
return get(tokenOrUrl).then(json => {
return {'PreAuthorization':json.preAuthToken};
});
}
else {
return new Promise(function (resolve, reject) {
resolve({'nv-authorizations':tokenOrUrl});
});
}
}
function get(url, responseType, headers) {
// Return a new promise.
return new Promise(function (resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.responseType = responseType;
req.open('GET', url);
for (var key in headers) {
if (headers.hasOwnProperty(key)) {
console.log(key + " -> " + headers[key]);
req.setRequestHeader(key, headers[key]);
}
}
req.onload = function () {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(JSON.parse(req.response));
}
else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = function () {
reject(Error("Network Error"));
};
// Make the request
req.send();
});
}
var playbackConfig = new cast.framework.PlaybackConfig();
context.start({ playbackConfig: playbackConfig });
</script>
</body>
</html>