/
cast_proxy.js
751 lines (643 loc) · 21.3 KB
/
cast_proxy.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
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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.cast.CastProxy');
goog.require('goog.asserts');
goog.require('shaka.Player');
goog.require('shaka.cast.CastSender');
goog.require('shaka.cast.CastUtils');
goog.require('shaka.log');
goog.require('shaka.util.Error');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.FakeEvent');
goog.require('shaka.util.FakeEventTarget');
goog.require('shaka.util.IDestroyable');
/**
* @event shaka.cast.CastProxy.CastStatusChangedEvent
* @description Fired when cast status changes. The status change will be
* reflected in canCast() and isCasting().
* @property {string} type
* 'caststatuschanged'
* @exportDoc
*/
/**
* @summary A proxy to switch between local and remote playback for Chromecast
* in a way that is transparent to the app's controls.
*
* @implements {shaka.util.IDestroyable}
* @export
*/
shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
/**
* @param {!HTMLMediaElement} video The local video element associated with
* the local Player instance.
* @param {!shaka.Player} player A local Player instance.
* @param {string} receiverAppId The ID of the cast receiver application.
* If blank, casting will not be available, but the proxy will still
* function otherwise.
*/
constructor(video, player, receiverAppId) {
super();
/** @private {HTMLMediaElement} */
this.localVideo_ = video;
/** @private {shaka.Player} */
this.localPlayer_ = player;
/** @private {Object} */
this.videoProxy_ = null;
/** @private {Object} */
this.playerProxy_ = null;
/** @private {shaka.util.FakeEventTarget} */
this.videoEventTarget_ = null;
/** @private {shaka.util.FakeEventTarget} */
this.playerEventTarget_ = null;
/** @private {shaka.util.EventManager} */
this.eventManager_ = null;
/** @private {string} */
this.receiverAppId_ = receiverAppId;
/** @private {!Map} */
this.compiledToExternNames_ = new Map();
/** @private {shaka.cast.CastSender} */
this.sender_ = new shaka.cast.CastSender(
receiverAppId,
() => this.onCastStatusChanged_(),
() => this.onFirstCastStateUpdate_(),
(targetName, event) => this.onRemoteEvent_(targetName, event),
() => this.onResumeLocal_(),
() => this.getInitState_());
this.init_();
}
/**
* Destroys the proxy and the underlying local Player.
*
* @param {boolean=} forceDisconnect If true, force the receiver app to shut
* down by disconnecting. Does nothing if not connected.
* @override
* @export
*/
destroy(forceDisconnect) {
if (forceDisconnect) {
this.sender_.forceDisconnect();
}
if (this.eventManager_) {
this.eventManager_.release();
this.eventManager_ = null;
}
const waitFor = [];
if (this.localPlayer_) {
waitFor.push(this.localPlayer_.destroy());
this.localPlayer_ = null;
}
if (this.sender_) {
waitFor.push(this.sender_.destroy());
this.sender_ = null;
}
this.localVideo_ = null;
this.videoProxy_ = null;
this.playerProxy_ = null;
// FakeEventTarget implements IReleasable
super.release();
return Promise.all(waitFor);
}
/**
* Get a proxy for the video element that delegates to local and remote video
* elements as appropriate.
*
* @suppress {invalidCasts} to cast proxy Objects to unrelated types
* @return {!HTMLMediaElement}
* @export
*/
getVideo() {
return /** @type {!HTMLMediaElement} */(this.videoProxy_);
}
/**
* Get a proxy for the Player that delegates to local and remote Player
* objects as appropriate.
*
* @suppress {invalidCasts} to cast proxy Objects to unrelated types
* @return {!shaka.Player}
* @export
*/
getPlayer() {
return /** @type {!shaka.Player} */(this.playerProxy_);
}
/**
* @return {boolean} True if the cast API is available and there are
* receivers.
* @export
*/
canCast() {
return this.sender_.apiReady() && this.sender_.hasReceivers();
}
/**
* @return {boolean} True if we are currently casting.
* @export
*/
isCasting() {
return this.sender_.isCasting();
}
/**
* @return {string} The name of the Cast receiver device, if isCasting().
* @export
*/
receiverName() {
return this.sender_.receiverName();
}
/**
* @return {!Promise} Resolved when connected to a receiver. Rejected if the
* connection fails or is canceled by the user.
* @export
*/
async cast() {
const initState = this.getInitState_();
// TODO: transfer manually-selected tracks?
// TODO: transfer side-loaded text tracks?
await this.sender_.cast(initState);
if (!this.localPlayer_) {
// We've already been destroyed.
return;
}
// Unload the local manifest when casting succeeds.
await this.localPlayer_.unload();
}
/**
* Set application-specific data.
*
* @param {Object} appData Application-specific data to relay to the receiver.
* @export
*/
setAppData(appData) {
this.sender_.setAppData(appData);
}
/**
* Show a dialog where user can choose to disconnect from the cast connection.
* @export
*/
suggestDisconnect() {
this.sender_.showDisconnectDialog();
}
/**
* Force the receiver app to shut down by disconnecting.
* @export
*/
forceDisconnect() {
this.sender_.forceDisconnect();
}
/**
* @param {string} newAppId
* @export
*/
async changeReceiverId(newAppId) {
if (newAppId == this.receiverAppId_) {
// Nothing to change
return;
}
this.receiverAppId_ = newAppId;
// Destroy the old sender
this.sender_.forceDisconnect();
await this.sender_.destroy();
this.sender_ = null;
// Create the new one
this.sender_ = new shaka.cast.CastSender(
newAppId,
() => this.onCastStatusChanged_(),
() => this.onFirstCastStateUpdate_(),
(targetName, event) => this.onRemoteEvent_(targetName, event),
() => this.onResumeLocal_(),
() => this.getInitState_());
this.sender_.init();
}
/**
* Initialize the Proxies and the Cast sender.
* @private
*/
init_() {
this.sender_.init();
this.eventManager_ = new shaka.util.EventManager();
for (const name of shaka.cast.CastUtils.VideoEvents) {
this.eventManager_.listen(this.localVideo_, name,
(event) => this.videoProxyLocalEvent_(event));
}
for (const key in shaka.Player.EventName) {
const name = shaka.Player.EventName[key];
this.eventManager_.listen(this.localPlayer_, name,
(event) => this.playerProxyLocalEvent_(event));
}
// We would like to use Proxy here, but it is not supported on Safari.
this.videoProxy_ = {};
for (const k in this.localVideo_) {
Object.defineProperty(this.videoProxy_, k, {
configurable: false,
enumerable: true,
get: () => this.videoProxyGet_(k),
set: (value) => { this.videoProxySet_(k, value); },
});
}
this.playerProxy_ = {};
this.iterateOverPlayerMethods_((name, method) => {
goog.asserts.assert(this.playerProxy_, 'Must have player proxy!');
Object.defineProperty(this.playerProxy_, name, {
configurable: false,
enumerable: true,
get: () => this.playerProxyGet_(name),
});
});
if (COMPILED) {
this.mapCompiledToUncompiledPlayerMethodNames_();
}
this.videoEventTarget_ = new shaka.util.FakeEventTarget();
this.videoEventTarget_.dispatchTarget =
/** @type {EventTarget} */(this.videoProxy_);
this.playerEventTarget_ = new shaka.util.FakeEventTarget();
this.playerEventTarget_.dispatchTarget =
/** @type {EventTarget} */(this.playerProxy_);
}
/**
* Maps compiled to uncompiled player names so we can figure out
* which method to call in compiled build, while casting.
* @private
*/
mapCompiledToUncompiledPlayerMethodNames_() {
// In compiled mode, UI tries to access player methods
// by their internal renamed names, but the proxy object doesn't know about
// those.
// See https://github.com/google/shaka-player/issues/2130 for details.
const methodsToNames = new Map();
this.iterateOverPlayerMethods_((name, method) => {
if (methodsToNames.has(method)) {
// If two method names, point to the same method, add them to the
// map as aliases of each other.
const name2 = methodsToNames.get(method);
// Assumes that the compiled name is shorter
if (name.length < name2.length) {
this.compiledToExternNames_.set(name, name2);
} else {
this.compiledToExternNames_.set(name2, name);
}
} else {
methodsToNames.set(method, name);
}
});
}
/**
* Iterates over all of the methods of the player, including inherited methods
* from FakeEventTarget.
* @param {function(string, function())} operation
* @private
*/
iterateOverPlayerMethods_(operation) {
goog.asserts.assert(this.localPlayer_, 'Must have player!');
const player = /** @type {!Object} */ (this.localPlayer_);
// Avoid accessing any over-written methods in the prototype chain.
const seenNames = new Set();
/**
* @param {string} name
* @return {boolean}
*/
function shouldAddToTheMap(name) {
if (name == 'constructor') {
// Don't proxy the constructor.
return false;
}
const method = /** @type {Object} */(player)[name];
if (typeof method != 'function') {
// Don't proxy non-methods.
return false;
}
// Add if the map does not already have it
return !seenNames.has(name);
}
// First, look at the methods on the object itself, so this can properly
// proxy any methods not on the prototype (for example, in the mock player).
for (const key in player) {
if (shouldAddToTheMap(key)) {
seenNames.add(key);
operation(key, player[key]);
}
}
// The exact length of the prototype chain might vary; for resiliency, this
// will just look at the entire chain, rather than assuming a set length.
let proto = /** @type {!Object} */ (Object.getPrototypeOf(player));
const objProto = /** @type {!Object} */ (Object.getPrototypeOf({}));
while (proto && proto != objProto) { // Don't proxy Object methods.
for (const name of Object.getOwnPropertyNames(proto)) {
if (shouldAddToTheMap(name)) {
seenNames.add(name);
operation(name, (player)[name]);
}
}
proto = /** @type {!Object} */ (Object.getPrototypeOf(proto));
}
}
/**
* @return {shaka.cast.CastUtils.InitStateType} initState Video and player
* state to be sent to the receiver.
* @private
*/
getInitState_() {
const initState = {
'video': {},
'player': {},
'playerAfterLoad': {},
'manifest': this.localPlayer_.getAssetUri(),
'startTime': null,
};
// Pause local playback before capturing state.
this.localVideo_.pause();
for (const name of shaka.cast.CastUtils.VideoInitStateAttributes) {
initState['video'][name] = this.localVideo_[name];
}
// If the video is still playing, set the startTime.
// Has no effect if nothing is loaded.
if (!this.localVideo_.ended) {
initState['startTime'] = this.localVideo_.currentTime;
}
for (const pair of shaka.cast.CastUtils.PlayerInitState) {
const getter = pair[0];
const setter = pair[1];
const value = /** @type {Object} */(this.localPlayer_)[getter]();
initState['player'][setter] = value;
}
for (const pair of shaka.cast.CastUtils.PlayerInitAfterLoadState) {
const getter = pair[0];
const setter = pair[1];
const value = /** @type {Object} */(this.localPlayer_)[getter]();
initState['playerAfterLoad'][setter] = value;
}
return initState;
}
/**
* Dispatch an event to notify the app that the status has changed.
* @private
*/
onCastStatusChanged_() {
const event = new shaka.util.FakeEvent('caststatuschanged');
this.dispatchEvent(event);
}
/**
* Dispatch a synthetic play or pause event to ensure that the app correctly
* knows that the player is playing, if joining an existing receiver.
* @private
*/
onFirstCastStateUpdate_() {
const type = this.videoProxy_['paused'] ? 'pause' : 'play';
const fakeEvent = new shaka.util.FakeEvent(type);
this.videoEventTarget_.dispatchEvent(fakeEvent);
}
/**
* Transfer remote state back and resume local playback.
* @private
*/
onResumeLocal_() {
// Transfer back the player state.
for (const pair of shaka.cast.CastUtils.PlayerInitState) {
const getter = pair[0];
const setter = pair[1];
const value = this.sender_.get('player', getter)();
/** @type {Object} */(this.localPlayer_)[setter](value);
}
// Get the most recent manifest URI and ended state.
const assetUri = this.sender_.get('player', 'getAssetUri')();
const ended = this.sender_.get('video', 'ended');
let manifestReady = Promise.resolve();
const autoplay = this.localVideo_.autoplay;
let startTime = null;
// If the video is still playing, set the startTime.
// Has no effect if nothing is loaded.
if (!ended) {
startTime = this.sender_.get('video', 'currentTime');
}
// Now load the manifest, if present.
if (assetUri) {
// Don't autoplay the content until we finish setting up initial state.
this.localVideo_.autoplay = false;
manifestReady = this.localPlayer_.load(assetUri, startTime);
}
// Get the video state into a temp variable since we will apply it async.
const videoState = {};
for (const name of shaka.cast.CastUtils.VideoInitStateAttributes) {
videoState[name] = this.sender_.get('video', name);
}
// Finally, take on video state and player's "after load" state.
manifestReady.then(() => {
if (!this.localVideo_) {
// We've already been destroyed.
return;
}
for (const name of shaka.cast.CastUtils.VideoInitStateAttributes) {
this.localVideo_[name] = videoState[name];
}
for (const pair of shaka.cast.CastUtils.PlayerInitAfterLoadState) {
const getter = pair[0];
const setter = pair[1];
const value = this.sender_.get('player', getter)();
/** @type {Object} */(this.localPlayer_)[setter](value);
}
// Restore the original autoplay setting.
this.localVideo_.autoplay = autoplay;
if (assetUri) {
// Resume playback with transferred state.
this.localVideo_.play();
}
}, (error) => {
// Pass any errors through to the app.
goog.asserts.assert(error instanceof shaka.util.Error,
'Wrong error type!');
const eventType = shaka.Player.EventName.Error;
const data = (new Map()).set('detail', error);
const event = new shaka.util.FakeEvent(eventType, data);
this.localPlayer_.dispatchEvent(event);
});
}
/**
* @param {string} name
* @return {?}
* @private
*/
videoProxyGet_(name) {
if (name == 'addEventListener') {
return (type, listener, options) => {
return this.videoEventTarget_.addEventListener(type, listener, options);
};
}
if (name == 'removeEventListener') {
return (type, listener, options) => {
return this.videoEventTarget_.removeEventListener(
type, listener, options);
};
}
// If we are casting, but the first update has not come in yet, use local
// values, but not local methods.
if (this.sender_.isCasting() && !this.sender_.hasRemoteProperties()) {
const value = this.localVideo_[name];
if (typeof value != 'function') {
return value;
}
}
// Use local values and methods if we are not casting.
if (!this.sender_.isCasting()) {
let value = this.localVideo_[name];
if (typeof value == 'function') {
// eslint-disable-next-line no-restricted-syntax
value = value.bind(this.localVideo_);
}
return value;
}
return this.sender_.get('video', name);
}
/**
* @param {string} name
* @param {?} value
* @private
*/
videoProxySet_(name, value) {
if (!this.sender_.isCasting()) {
this.localVideo_[name] = value;
return;
}
this.sender_.set('video', name, value);
}
/**
* @param {!Event} event
* @private
*/
videoProxyLocalEvent_(event) {
if (this.sender_.isCasting()) {
// Ignore any unexpected local events while casting. Events can still be
// fired by the local video and Player when we unload() after the Cast
// connection is complete.
return;
}
// Convert this real Event into a FakeEvent for dispatch from our
// FakeEventListener.
const fakeEvent = shaka.util.FakeEvent.fromRealEvent(event);
this.videoEventTarget_.dispatchEvent(fakeEvent);
}
/**
* @param {string} name
* @return {?}
* @private
*/
playerProxyGet_(name) {
// If name is a shortened compiled name, get the original version
// from our map.
if (this.compiledToExternNames_.has(name)) {
name = this.compiledToExternNames_.get(name);
}
if (name == 'addEventListener') {
return (type, listener, options) => {
return this.playerEventTarget_.addEventListener(
type, listener, options);
};
}
if (name == 'removeEventListener') {
return (type, listener, options) => {
return this.playerEventTarget_.removeEventListener(
type, listener, options);
};
}
if (name == 'getMediaElement') {
return () => this.videoProxy_;
}
if (name == 'getSharedConfiguration') {
shaka.log.warning(
'Can\'t share configuration across a network. Returning copy.');
return this.sender_.get('player', 'getConfiguration');
}
if (name == 'getNetworkingEngine') {
// Always returns a local instance, in case you need to make a request.
// Issues a warning, in case you think you are making a remote request
// or affecting remote filters.
if (this.sender_.isCasting()) {
shaka.log.warning('NOTE: getNetworkingEngine() is always local!');
}
return () => this.localPlayer_.getNetworkingEngine();
}
if (name == 'getDrmEngine') {
// Always returns a local instance.
if (this.sender_.isCasting()) {
shaka.log.warning('NOTE: getDrmEngine() is always local!');
}
return () => this.localPlayer_.getDrmEngine();
}
if (name == 'getAdManager') {
// Always returns a local instance.
if (this.sender_.isCasting()) {
shaka.log.warning('NOTE: getAdManager() is always local!');
}
return () => this.localPlayer_.getAdManager();
}
if (name == 'setVideoContainer') {
// Always returns a local instance.
if (this.sender_.isCasting()) {
shaka.log.warning('NOTE: setVideoContainer() is always local!');
}
return (container) => this.localPlayer_.setVideoContainer(container);
}
if (this.sender_.isCasting()) {
// These methods are unavailable or otherwise stubbed during casting.
if (name == 'getManifest' || name == 'drmInfo') {
return () => {
shaka.log.alwaysWarn(name + '() does not work while casting!');
return null;
};
}
if (name == 'attach' || name == 'detach') {
return () => {
shaka.log.alwaysWarn(name + '() does not work while casting!');
return Promise.resolve();
};
}
} // if (this.sender_.isCasting())
// If we are casting, but the first update has not come in yet, use local
// getters, but not local methods.
if (this.sender_.isCasting() && !this.sender_.hasRemoteProperties()) {
if (shaka.cast.CastUtils.PlayerGetterMethods[name]) {
const value = /** @type {Object} */(this.localPlayer_)[name];
goog.asserts.assert(typeof value == 'function',
'only methods on Player');
// eslint-disable-next-line no-restricted-syntax
return value.bind(this.localPlayer_);
}
}
// Use local getters and methods if we are not casting.
if (!this.sender_.isCasting()) {
const value = /** @type {Object} */(this.localPlayer_)[name];
goog.asserts.assert(typeof value == 'function',
'only methods on Player');
// eslint-disable-next-line no-restricted-syntax
return value.bind(this.localPlayer_);
}
return this.sender_.get('player', name);
}
/**
* @param {!Event} event
* @private
*/
playerProxyLocalEvent_(event) {
if (this.sender_.isCasting()) {
// Ignore any unexpected local events while casting.
return;
}
this.playerEventTarget_.dispatchEvent(event);
}
/**
* @param {string} targetName
* @param {!shaka.util.FakeEvent} event
* @private
*/
onRemoteEvent_(targetName, event) {
goog.asserts.assert(this.sender_.isCasting(),
'Should only receive remote events while casting');
if (!this.sender_.isCasting()) {
// Ignore any unexpected remote events.
return;
}
if (targetName == 'video') {
this.videoEventTarget_.dispatchEvent(event);
} else if (targetName == 'player') {
this.playerEventTarget_.dispatchEvent(event);
}
}
};