Skip to content

Commit

Permalink
Mostly cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Jun 19, 2023
1 parent 6c4e890 commit 7739c95
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 109 deletions.
55 changes: 41 additions & 14 deletions src/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,45 +104,69 @@ export function detectDevice(): BuiltinHandlerName | undefined
const osName = os.name?.toLowerCase() ?? '';
const osVersion = parseFloat(os.version ?? '0');

const isChrome = [ 'chrome', 'chromium', 'mobile chrome', 'chrome webview', 'chrome headless' ].includes(browserName) && osName !== 'ios';
const isIOS = osName === 'ios';

const isChrome =
[
'chrome',
'chromium',
'mobile chrome',
'chrome webview',
'chrome headless'
].includes(browserName);

const isFirefox =
[
'firefox',
'mobile firefox',
'mobile focus'
].includes(browserName);

const isSafari =
[
'safari',
'mobile safari'
].includes(browserName);

const isEdge = [ 'edge' ].includes(browserName);

// Chrome, Chromium, and Edge.
if ((isChrome || browserName === 'edge') && browserVersion >= 111)
if ((isChrome || isEdge) && !isIOS && browserVersion >= 111)
{
return 'Chrome111';
}
else if (
(isChrome && browserVersion >= 74) ||
(browserName === 'edge' && browserVersion >= 88)
(isChrome && !isIOS && browserVersion >= 74) ||
(isEdge && !isIOS && browserVersion >= 88)
)
{
return 'Chrome74';
}
else if (isChrome && browserVersion >= 70)
else if (isChrome && !isIOS && browserVersion >= 70)
{
return 'Chrome70';
}
else if (isChrome && browserVersion >= 67)
else if (isChrome && !isIOS && browserVersion >= 67)
{
return 'Chrome67';
}
else if (isChrome && browserVersion >= 55)
else if (isChrome && !isIOS && browserVersion >= 55)
{
return 'Chrome55';
}
// Firefox.
else if ([ 'firefox', 'mobile firefox' ].includes(browserName) && browserVersion >= 60 && osName !== 'ios')
else if (isFirefox && !isIOS && browserVersion >= 60)
{
return 'Firefox60';
}
// Firefox on iOS (so Safari).
else if ([ 'firefox', 'mobile firefox', 'firefox focus' ].includes(browserName) && osName === 'ios' && osVersion >= 14.3)
else if (isFirefox && isIOS && osVersion >= 14.3)
{
return 'Safari12';
}
// Safari with Unified-Plan support enabled.
else if (
[ 'safari', 'mobile safari' ].includes(browserName) &&
isSafari &&
browserVersion >= 12 &&
typeof RTCRtpTransceiver !== 'undefined' &&
RTCRtpTransceiver.prototype.hasOwnProperty('currentDirection')
Expand All @@ -151,17 +175,20 @@ export function detectDevice(): BuiltinHandlerName | undefined
return 'Safari12';
}
// Safari with Plab-B support.
else if ([ 'safari', 'mobile safari' ].includes(browserName) && browserVersion >= 11)
else if (isSafari && browserVersion >= 11)
{
return 'Safari11';
}
// Old Edge with ORTC support.
else if (browserName === 'edge' && browserVersion >= 11 && browserVersion <= 18)
else if (isEdge && !isIOS && browserVersion >= 11 && browserVersion <= 18)
{
return 'Edge11';
}
// Best effort for WebKit based browsers.
else if (engineName === 'webkit' && osName === 'ios' && osVersion >= 14.3 &&
// Best effort for WebKit based browsers in iOS.
else if (
engineName === 'webkit' &&
isIOS &&
osVersion >= 14.3 &&
typeof RTCRtpTransceiver !== 'undefined' &&
RTCRtpTransceiver.prototype.hasOwnProperty('currentDirection'))
{
Expand Down
30 changes: 18 additions & 12 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
*/

import * as sdpTransform from 'sdp-transform';
import { AwaitQueue } from 'awaitqueue';
import { FakeMediaStreamTrack } from 'fake-mediastreamtrack';
import * as mediasoupClient from '../';
import { UnsupportedError, InvalidStateError } from '../errors';
import * as utils from '../utils';
import { RemoteSdp } from '../handlers/sdp/RemoteSdp';
import { FakeHandler } from '../handlers/FakeHandler';
import * as fakeParameters from './fakeParameters';
import { AwaitQueue } from 'awaitqueue';
import uaTestCases from './ua.json';
import { uaTestCases } from './uaTestCases';

const {
Device,
Expand Down Expand Up @@ -1701,32 +1701,38 @@ test('parseScalabilityMode() works', () =>
expect(parseScalabilityMode('S200T3')).toEqual({ spatialLayers: 1, temporalLayers: 1 });
}, 500);

describe('detectDevice() Test UA Variants', () =>
describe('detectDevice() assigns proper handler based on UserAgent', () =>
{
const originalNavigator = global.navigator;

uaTestCases.forEach((uaTestCase) =>
for (const uaTestCase of uaTestCases)
{
test(`detectDevice() Test UA - ${uaTestCase.desc}`, () =>
test(uaTestCase.desc, () =>
{
global.navigator = {
// @ts-ignore
global.navigator =
{
userAgent : uaTestCase.ua
} as any;
};

const originalRTCRtpTransceiver = global.RTCRtpTransceiver;

if (uaTestCase.expect === 'Safari12')
{
global.RTCRtpTransceiver = class Dummy
{
currentDirection() {}
currentDirection()
{}
} as any;
}

expect(detectDevice()).toBe(uaTestCase.expect);
// cleanup

// Cleanup.
global.RTCRtpTransceiver = originalRTCRtpTransceiver;

}, 100);
});
// cleanup
}

// Cleanup.
global.navigator = originalNavigator;
});
81 changes: 0 additions & 81 deletions src/tests/ua.json

This file was deleted.

92 changes: 92 additions & 0 deletions src/tests/uaTestCases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { BuiltinHandlerName } from '../Device';

type UATestCase =
{
desc: string;
ua: string;
expect?: BuiltinHandlerName;
};

export const uaTestCases: UATestCase[] =
[
{
desc : 'Microsoft Edge 100',
ua : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.1108.55 Safari/537.36 Edg/100.0.1108.55',
expect : 'Chrome74'
},
{
desc : 'Mac (Intel) Chrome 112',
ua : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
expect : 'Chrome111'
},
{
desc : 'Generic Android Chrome 112',
ua : 'Mozilla/5.0 (Linux; Android 13; M2012K11AG) AppleWebKit/537.36 (KHTML, like Gecko) Soul/4.0 Chrome/112.0.5615.135 Mobile Safari/537.36',
expect : 'Chrome111'
},
{
desc : 'Motorola Edge Chrome 104',
ua : 'Mozilla/5.0 (Linux; Android 10; motorola edge) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Mobile Safari/537.36',
expect : 'Chrome74'
},
{
desc : 'Microsoft Edge 44',
ua : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763',
expect : 'Edge11'
},
{
desc : 'Firefox 68 (Android)',
ua : 'Mozilla/5.0 (Android 10; Mobile; rv:68.10.0) Gecko/68.10.0 Firefox/68.10.0',
expect : 'Firefox60'
},
{
desc : 'In-app WebView (Android)',
ua : 'Mozilla/5.0 (Linux; Android 11; G91 Pro Build/RP1A.200720.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.130 Mobile Safari/537.36',
expect : 'Chrome111'
},
{
desc : 'Safari 11',
ua : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_13) AppleWebKit/537.36 (KHTML, like Gecko) Version/11.0.92 Safari/619.28',
expect : 'Safari11'
},
{
desc : 'Safari 11 (iPad)',
ua : 'Mozilla/5.0 (iPad; CPU OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1',
expect : 'Safari11'
},
{
desc : 'Brave',
ua : 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.4044.113 Safari/5370.36 Brave/9085',
expect : 'Chrome111'
},
{
desc : 'In-app WebView (Android) (Facebook)',
ua : 'Mozilla/5.0 (Linux; Android 12; SM-S908U1 Build/SP1A.210812.016; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/100.0.4896.88 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/377.0.0.22.107;]',
expect : 'Chrome74'
},
{
desc : 'Firefox (Linux)',
ua : 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:109.0) Gecko/20100101 Firefox/114.0',
expect : 'Firefox60'
},
{
desc : 'Firefox (iOS) - Unsupported',
ua : 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/114.0 Mobile/15E148 Safari/605.1.15',
expect : undefined
},
{
desc : 'In-app WKWebView (iOS) (TikTok)',
ua : 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_8 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 musical_ly_21.1.0 JsSdk/2.0 NetType/4G Channel/App Store ByteLocale/ru Region/RU ByteFullLocale/ru-RU isDarkMode/1 WKWebView/1 BytedanceWebview/d8a21c6',
expect : 'Safari12'
},
{
desc : 'In-app WkWebView (iOS) (WeChat)',
ua : 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.37(0x1800252f) NetType/WIFI Language/zh_CN',
expect : 'Safari12'
},
{
desc : 'Chrome Mobile (iOS)',
ua : 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/114.0.5735.124 Mobile/15E148 Safari/604.1',
expect : 'Safari12'
}
];
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"strict": true,
"outDir": "lib",
"declaration": true,
"declarationMap": true,
"resolveJsonModule": true,
"declarationMap": true
},
"include": [ "src" ]
}

0 comments on commit 7739c95

Please sign in to comment.