Skip to content

Commit

Permalink
Support screen pixel density for react native (#651)
Browse files Browse the repository at this point in the history
* react-native pixel ratio

* fix naming

* changeset

* pr comment fixes
  • Loading branch information
davidliu authored Apr 16, 2023
1 parent 8d7f854 commit 80ec8d7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-buckets-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Support screen pixel density for react-native
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from './room/utils';

import type { AudioAnalyserOptions } from './room/utils';
import type { LiveKitReactNativeInfo } from './room/types';

export * from './options';
export * from './room/errors';
Expand Down Expand Up @@ -73,4 +74,5 @@ export {
ElementInfo,
DefaultReconnectPolicy,
CriticalTimers,
LiveKitReactNativeInfo,
};
3 changes: 2 additions & 1 deletion src/room/track/RemoteVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TrackEvent } from '../events';
import { computeBitrate, VideoReceiverStats } from '../stats';
import CriticalTimers from '../timers';
import {
getDevicePixelRatio,
getIntersectionObserver,
getResizeObserver,
isWeb,
Expand Down Expand Up @@ -253,7 +254,7 @@ export default class RemoteVideoTrack extends RemoteTrack {
let maxHeight = 0;
for (const info of this.elementInfos) {
const pixelDensity = this.adaptiveStreamSettings?.pixelDensity ?? 1;
const pixelDensityValue = pixelDensity === 'screen' ? window.devicePixelRatio : pixelDensity;
const pixelDensityValue = pixelDensity === 'screen' ? getDevicePixelRatio() : pixelDensity;
const currentElementWidth = info.width() * pixelDensityValue;
const currentElementHeight = info.height() * pixelDensityValue;
if (currentElementWidth + currentElementHeight > maxWidth + maxHeight) {
Expand Down
6 changes: 6 additions & 0 deletions src/room/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export type DataPublishOptions = {
/** the topic under which the message gets published */
topic?: string;
};

export type LiveKitReactNativeInfo = {
// Corresponds to RN's PlatformOSType
platform: 'ios' | 'android' | 'windows' | 'macos' | 'web' | 'native';
devicePixelRatio: number;
};
35 changes: 30 additions & 5 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { protocolVersion, version } from '../version';
import type LocalAudioTrack from './track/LocalAudioTrack';
import type RemoteAudioTrack from './track/RemoteAudioTrack';
import { getNewAudioContext } from './track/utils';
import type { LiveKitReactNativeInfo } from './types';

const separator = '|';

Expand Down Expand Up @@ -127,21 +128,45 @@ export function isReactNative(): boolean {
return navigator.product == 'ReactNative';
}

function getLKReactNativeInfo(): LiveKitReactNativeInfo | undefined {
// global defined only for ReactNative.
// @ts-ignore
if (global && global.LiveKitReactNativeGlobal) {
// @ts-ignore
return global.LiveKitReactNativeGlobal as LiveKitReactNativeInfo;
}

return undefined;
}

export function getReactNativeOs(): string | undefined {
if (!isReactNative()) {
return undefined;
}

// global defined only for ReactNative.
// @ts-ignore
if (global && global.LiveKitReactNativeGlobal) {
// @ts-ignore
return global.LiveKitReactNativeGlobal.platform;
let info = getLKReactNativeInfo();
if (info) {
return info.platform;
}

return undefined;
}

export function getDevicePixelRatio(): number {
if (isWeb()) {
return window.devicePixelRatio;
}

if (isReactNative()) {
let info = getLKReactNativeInfo();
if (info) {
return info.devicePixelRatio;
}
}

return 1;
}

export function compareVersions(v1: string, v2: string): number {
const parts1 = v1.split('.');
const parts2 = v2.split('.');
Expand Down

0 comments on commit 80ec8d7

Please sign in to comment.