From cc22bea6299cf9c672c5744a5e8856f17c21a8b4 Mon Sep 17 00:00:00 2001 From: Kevin Hanna Date: Tue, 14 Jan 2025 14:50:35 +0000 Subject: [PATCH] media: Only use Safari17 handler when flag is eanbled --- .changeset/cyan-elephants-shake.md | 5 +++ .../__tests__/getMediasoupDevice.spec.ts | 39 +++++++++++++++---- .../media/src/utils/getMediasoupDevice.ts | 10 ++++- 3 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 .changeset/cyan-elephants-shake.md diff --git a/.changeset/cyan-elephants-shake.md b/.changeset/cyan-elephants-shake.md new file mode 100644 index 000000000..db978b89d --- /dev/null +++ b/.changeset/cyan-elephants-shake.md @@ -0,0 +1,5 @@ +--- +"@whereby.com/media": patch +--- + +Fix Safari17 Mediasoup handler selection logic diff --git a/packages/media/src/utils/__tests__/getMediasoupDevice.spec.ts b/packages/media/src/utils/__tests__/getMediasoupDevice.spec.ts index dbb0d7dd3..07fbbbcd3 100644 --- a/packages/media/src/utils/__tests__/getMediasoupDevice.spec.ts +++ b/packages/media/src/utils/__tests__/getMediasoupDevice.spec.ts @@ -63,13 +63,36 @@ describe("getMediasoupClient", () => { }); }); - describe.each(["AppleCoreMedia", "AppleWebkit", "Safari", "iphone", "ipad"])( - "when the userAgent matches %s", - (userAgent) => { - beforeEach(() => { - (global as any).userAgent.mockReturnValue(userAgent); - }); + describe.each(["applecoremedia", "applewebkit", "safari"])("when the userAgent matches %s", (userAgent) => { + beforeEach(() => { + (global as any).userAgent.mockReturnValue(userAgent); + }); + + it("returns a Safari12 device", () => { + const factory = jest.fn(); + Safari17.createFactory.mockImplementation(() => factory); + + getMediasoupDevice({ safari17HandlerOn: false }); + + expect(mediasoupClient.Device).toHaveBeenCalledWith({ handlerName: "Safari12" }); + }); + }); + describe.each(["iphone", "ipad"])("when the userAgent matches %s", (userAgent) => { + beforeEach(() => { + (global as any).userAgent.mockReturnValue(userAgent); + }); + + it("returns a Safari12 device", () => { + const factory = jest.fn(); + Safari17.createFactory.mockImplementation(() => factory); + + getMediasoupDevice({ safari17HandlerOn: false }); + + expect(mediasoupClient.Device).toHaveBeenCalledWith({ handlerName: "Safari12" }); + }); + + describe("when the safari17HandlerOn feature is enabled", () => { it("returns a Safari17 device", () => { const factory = jest.fn(); Safari17.createFactory.mockImplementation(() => factory); @@ -78,6 +101,6 @@ describe("getMediasoupClient", () => { expect(mediasoupClient.Device).toHaveBeenCalledWith({ handlerFactory: factory }); }); - }, - ); + }); + }); }); diff --git a/packages/media/src/utils/getMediasoupDevice.ts b/packages/media/src/utils/getMediasoupDevice.ts index 6877feb9a..ce37b8f73 100644 --- a/packages/media/src/utils/getMediasoupDevice.ts +++ b/packages/media/src/utils/getMediasoupDevice.ts @@ -12,7 +12,7 @@ export const getMediasoupDevice = (features: Record } let handlerName: SupportedDevice = - detectDevice() || (/applecoremedia|applewebkit|safari/i.test(navigator.userAgent) ? "Safari17" : undefined); + detectDevice() || (/applecoremedia|applewebkit|safari/i.test(navigator.userAgent) ? "Safari12" : undefined); if (handlerName === "Safari12" && typeof navigator === "object" && typeof navigator.userAgent === "string") { const uaParser = new UAParser(navigator.userAgent); @@ -26,7 +26,13 @@ export const getMediasoupDevice = (features: Record // Since custom browsers on iOS/iPadOS are using webkit under the hood, we must use // the Safari handler even if detected as something else (like Chrome) - if (/iphone|ipad/i.test(navigator.userAgent)) handlerName = "Safari17"; + if (/iphone|ipad/i.test(navigator.userAgent)) { + if (features.safari17HandlerOn) { + handlerName = "Safari17"; + } else { + handlerName = "Safari12"; + } + } if (handlerName === "Safari17") { // we use a custom patched version of the Safari handler that fixes simulcast bandwidth limiting