Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Prevent CastSender initialization on CastProxy on unsupported browsers #7995

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions lib/cast/cast_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
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_(),
androidReceiverCompatible);

this.sender_ = null;

this.init_();
if (window.chrome) {
this.sender_ = new shaka.cast.CastSender(
receiverAppId,
() => this.onCastStatusChanged_(),
() => this.onFirstCastStateUpdate_(),
(targetName, event) => this.onRemoteEvent_(targetName, event),
() => this.onResumeLocal_(),
() => this.getInitState_(),
androidReceiverCompatible);
this.init_();
} else {
this.initWithoutSender_();
}
}

/**
Expand Down Expand Up @@ -163,6 +167,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
canCast() {
if (!this.sender_) {
return false;
}
return this.sender_.apiReady() && this.sender_.hasReceivers();
}

Expand All @@ -171,6 +178,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
isCasting() {
if (!this.sender_) {
return false;
}
return this.sender_.isCasting();
}

Expand All @@ -179,6 +189,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
receiverName() {
if (!this.sender_) {
return '';
}
return this.sender_.receiverName();
}

Expand All @@ -188,6 +201,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
async cast() {
if (!this.sender_) {
return;
}
// TODO: transfer manually-selected tracks?
// TODO: transfer side-loaded text tracks?

Expand All @@ -208,6 +224,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
setAppData(appData) {
if (!this.sender_) {
return;
}
this.sender_.setAppData(appData);
}

Expand All @@ -216,6 +235,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
suggestDisconnect() {
if (!this.sender_) {
return;
}
this.sender_.showDisconnectDialog();
}

Expand All @@ -224,6 +246,9 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
* @export
*/
forceDisconnect() {
if (!this.sender_) {
return;
}
this.sender_.forceDisconnect();
}

Expand All @@ -243,6 +268,10 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
this.receiverAppId_ = newAppId;
this.androidReceiverCompatible_ = newCastAndroidReceiver;

if (!this.sender_) {
return;
}

// Destroy the old sender
this.sender_.forceDisconnect();
await this.sender_.destroy();
Expand All @@ -262,6 +291,15 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
this.sender_.init();
}

/**
* Initialize the Proxies without Cast sender.
* @private
*/
initWithoutSender_() {
this.videoProxy_ = /** @type {Object} */(this.localVideo_);
this.playerProxy_ = /** @type {Object} */(this.localPlayer_);
}

/**
* Initialize the Proxies and the Cast sender.
* @private
Expand Down
5 changes: 5 additions & 0 deletions test/cast/cast_proxy_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ describe('CastProxy', () => {
/** @type {shaka.cast.CastProxy} */
let proxy;

const originalWindowChrome = window.chrome;

beforeEach(() => {
window['chrome'] = originalWindowChrome || {};

mockCastSenderConstructor = jasmine.createSpy('CastSender constructor');
mockCastSenderConstructor.and.callFake(createMockCastSender);
shaka.cast.CastSender = Util.spyFunc(mockCastSenderConstructor);
Expand All @@ -42,6 +46,7 @@ describe('CastProxy', () => {
} finally {
shaka.cast.CastSender = originalCastSender;
}
window['chrome'] = originalWindowChrome;
});

describe('constructor', () => {
Expand Down