forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiverApp.js
271 lines (221 loc) · 6.71 KB
/
receiverApp.js
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
/**
* @license
* Copyright 2015 Google Inc.
*
* 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 receiverApplicable 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.
*/
/**
* Implements the receiverApplication layer of the receiver.
* @class
*/
var receiverApp = function() {};
/**
* The video element owned by the receiverApp.
*
* @private {HTMLVideoElement}
*/
receiverApp.video_ = null;
/**
* The video resolution debug element owned by the receiverApp.
*
* @private {?string}
*/
receiverApp.videoResDebug_ = null;
/**
* The buffered ahead debug element owned by the receiverApp.
*
* @private {?string}
*/
receiverApp.bufferedAheadDebug_ = null;
/**
* The buffered behind debug element owned by the receiverApp.
*
* @private {?string}
*/
receiverApp.bufferedBehindDebug_ = null;
/**
* The player object owned by the receiverApp.
*
* @private {shaka.player.Player}
*/
receiverApp.player_ = null;
/**
* The receiverApp's bandwidth estimator, which will persist across playbacks.
* This will allow second and subsequent playbacks to benefit from earlier
* bandwidth estimations and avoid starting at a low-quality stream.
*
* @private {shaka.util.IBandwidthEstimator}
*/
receiverApp.estimator_ = null;
/**
* The current video configuration.
*/
receiverApp.config = {
'preferredLanguage': 'en-US',
'wvLicenseServerUrlInput' : null
};
/**
* Initializes the receiverApplication.
*/
receiverApp.init = function() {
shaka.polyfill.installAll();
receiverApp.video_ =
/** @type {!HTMLVideoElement} */ (document.getElementById('video'));
receiverApp.player_ = new shaka.player.Player(
/** @type {!HTMLVideoElement} */ (receiverApp.video_));
receiverApp.player_.addEventListener('error', receiverApp.onPlayerError_);
receiverApp.estimator_ = new shaka.util.EWMABandwidthEstimator();
window.setInterval(receiverApp.updateDebugInfo_, 50);
receiver.initialize();
// current time updates
receiverApp.video_.addEventListener('timeupdate', function() {
receiver.broadcast('currentTime', receiverApp.video_.currentTime);
var buffered = receiverApp.video_.buffered;
receiver.broadcast('buffered', {
'length': buffered.length,
'start': buffered.start(0),
'end': buffered.end(0)
});
});
receiverApp.video_.addEventListener('playing', function() {
receiverApp.onBuffering(false);
receiver.broadcast('playing', receiverApp.video_.duration);
receiver.cancelIdleTimeout();
});
receiverApp.video_.addEventListener('pause', function() {
receiver.broadcast('paused', null);
receiver.setIdleTimeout(receiver.IDLE_TIMEOUT_);
});
receiverApp.video_.addEventListener('volumechange', function() {
receiver.broadcast('volume', {
'volume': receiverApp.video_.volume,
'muted': receiverApp.video_.muted
});
});
receiverApp.video_.addEventListener('ended', function() {
receiver.setIdleTimeout(receiver.IDLE_TIMEOUT_);
});
};
/**
* Loads a dash stream.
* @param {appUtils.StreamState} streamInfo
*/
receiverApp.loadDashStream = function(streamInfo) {
receiverApp.onBuffering(true);
console.assert(receiverApp.estimator_);
if (receiverApp.estimator_.getDataAge() >= 3600) {
// Disregard any bandwidth data older than one hour. The user may have
// changed networks if they are on a laptop or mobile device.
receiverApp.estimator_ = new shaka.util.EWMABandwidthEstimator();
}
var estimator = /** @type {!shaka.util.IBandwidthEstimator} */(
receiverApp.estimator_);
var abrManager = new shaka.media.SimpleAbrManager();
receiverApp.load_(
new shaka.player.DashVideoSource(
streamInfo.manifest,
appUtils.interpretContentProtection.bind(
null,
receiverApp.player_,
receiverApp.config.wvLicenseServerUrlInput),
estimator,
abrManager), streamInfo.time);
};
/**
* Plays the video.
*/
receiverApp.play = function() {
receiverApp.video_.play();
};
/**
* Pauses the video.
*/
receiverApp.pause = function() {
receiverApp.video_.pause();
};
/**
* Sets the current time of the video.
* @param {number} time
*/
receiverApp.setCurrentTime = function(time) {
receiverApp.video_.currentTime = time;
};
/**
* Updates the buffering display.
* @param {boolean} buffering True if the video is buffering.
*/
receiverApp.onBuffering = function(buffering) {
var bufferingSpinner = document.getElementById('bufferingSpinner');
bufferingSpinner.style.display = buffering ? 'inherit' : 'none';
};
/**
* Sets the current volume of the video.
* @param {number} volume
*/
receiverApp.setVolume = function(volume) {
receiverApp.video_.volume = volume;
};
/**
* Mutes the video.
* @param {boolean} mute
*/
receiverApp.mute = function(mute) {
receiverApp.video_.muted = mute;
};
/**
* Loads the given video source into the player.
* @param {!shaka.player.IVideoSource} videoSource
* @param {number} time The time the video should start at.
* @private
*/
receiverApp.load_ = function(videoSource, time) {
console.assert(receiverApp.player_ != null);
receiverApp.player_.configure(
{'preferredLanguage': receiverApp.config.preferredLanguage});
receiverApp.player_.setPlaybackStartTime(time);
// Error already handled through error event.
receiverApp.player_.load(videoSource).catch(function() {});
};
/**
* Update the debug information.
* @private
*/
receiverApp.updateDebugInfo_ = function() {
var debugMsg = appUtils.getVideoResDebug(receiverApp.video_);
var bufferInfo = appUtils.getBufferDebug(receiverApp.video_);
var debugInfo = {
'videoResDebug': debugMsg,
'bufferedAheadDebug': bufferInfo[0],
'bufferedBehindDebug': bufferInfo[1]
};
receiver.broadcast('debugInfo', debugInfo);
};
/**
* Called when the player generates an error.
* @param {!Event} event
* @private
*/
receiverApp.onPlayerError_ = function(event) {
console.error('Player error', event);
receiverApp.player_.unload();
receiverApp.player_ = null;
receiverApp.onBuffering(false);
receiver.broadcast('error', event.detail.name + ': ' + event.detail.message);
receiver.setIdleTimeout(receiver.IDLE_TIMEOUT_);
};
if (document.readyState == 'complete' ||
document.readyState == 'interactive') {
receiverApp.init();
} else {
document.addEventListener('DOMContentLoaded', receiverApp.init);
}