Skip to content

Commit b806c9b

Browse files
committed
feat: add Predefined resolutions
1 parent 21880fd commit b806c9b

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

example/src/App.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {
77
StatusBar,
88
Animated,
99
} from 'react-native';
10-
import { ApiVideoLiveStreamView } from '@api.video/react-native-livestream';
10+
import { ApiVideoLiveStreamView, PredefinedResolution } from '@api.video/react-native-livestream';
1111
import Icon from 'react-native-vector-icons/Ionicons';
1212
import styles, { button } from './style';
1313
import Settings from './components/settings';
1414
import assets from './assets';
1515
import { ZoomPicker } from './components/zoomSlider';
1616

1717
export interface ISettingsState {
18-
resolution: Resolution;
18+
resolution: PredefinedResolution;
1919
framerate: number;
2020
videoBitrate: number;
2121
audioBitrate: number;
@@ -35,7 +35,7 @@ export default function App() {
3535
message: string;
3636
}>({ display: false, message: '' });
3737
const [settings, setSettings] = React.useState<ISettingsState>({
38-
resolution: { width: 640, height: 360 },
38+
resolution: "360p",
3939
framerate: 30,
4040
videoBitrate: assets.sections.video.Bitrate.min,
4141
audioBitrate: 64000,

example/src/components/settings/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import Section from './sections';
1111
import styles from './style';
1212
import assets from '../../assets';
1313
import type { ISettingsState } from 'example/src/App';
14-
import type { Resolution } from '@api.video/react-native-livestream';
14+
import type { PredefinedResolution } from '@api.video/react-native-livestream';
15+
1516

1617
interface ISettingsProps {
1718
closeSettings: () => void;
@@ -49,7 +50,7 @@ const Settings: React.FC<ISettingsProps> = ({
4950
if (key === 'Resolution') {
5051
setSettingsState((_prev) => ({
5152
..._prev,
52-
resolution: value as Resolution,
53+
resolution: value as PredefinedResolution,
5354
}));
5455
return;
5556
}

src/NativeApiVideoLiveStreamView.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export type Resolution = Readonly<{
1515
height: Int32;
1616
}>;
1717

18+
export type PredefinedResolution = "240p" | "360p" | "480p" | "720p" | "1080p";
19+
20+
1821
export type OnConnectionFailedEvent = Readonly<{
1922
code: string;
2023
}>;

src/index.tsx

+29-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NativeApiVideoLiveStreamView, {
88
OnConnectionFailedEvent,
99
OnPermissionsDeniedEvent,
1010
OnStartStreamingEvent,
11+
PredefinedResolution,
1112
} from './NativeApiVideoLiveStreamView';
1213

1314
type ApiVideoLiveStreamProps = {
@@ -16,7 +17,7 @@ type ApiVideoLiveStreamProps = {
1617
video?: {
1718
bitrate?: number;
1819
fps?: number;
19-
resolution?: Resolution;
20+
resolution?: Resolution | PredefinedResolution;
2021
gopDuration?: number;
2122
};
2223
isMuted?: boolean;
@@ -76,20 +77,42 @@ const getDefaultBitrate = (resolution: Resolution): number => {
7677
}
7778
};
7879

80+
81+
function resolveResolution(resolution: Resolution | PredefinedResolution): Resolution {
82+
const predefinedResolutions: Record<PredefinedResolution, Resolution> = {
83+
"1080p": { width: 1920, height: 1080 },
84+
"720p": { width: 1280, height: 720 },
85+
"480p": { width: 858, height: 480 },
86+
"360p": { width: 640, height: 360 },
87+
"240p": { width: 352, height: 240 },
88+
};
89+
90+
if (typeof resolution === "string") {
91+
const predefined = predefinedResolutions[resolution];
92+
if (!predefined) {
93+
throw new Error("Unknown resolution");
94+
}
95+
return predefined;
96+
}
97+
return {
98+
width: Math.max(resolution.height, resolution.width),
99+
height: Math.min(resolution.height, resolution.width),
100+
};
101+
}
102+
79103
const ApiVideoLiveStreamView = forwardRef<
80104
ApiVideoLiveStreamMethods,
81105
ApiVideoLiveStreamProps
82106
>((props, forwardedRef) => {
107+
const resolution = resolveResolution(props.video?.resolution || "720p")
83108
const nativeLiveStreamProps: NativeLiveStreamProps = {
84109
...LIVE_STREAM_PROPS_DEFAULTS,
85110
...props,
86111
video: {
87112
...LIVE_STREAM_PROPS_DEFAULTS.video,
88-
bitrate: getDefaultBitrate(
89-
props.video?.resolution || { width: 1280, height: 720 }
90-
),
91-
resolution: { width: 1280, height: 720 },
113+
bitrate: getDefaultBitrate(resolution),
92114
...props.video,
115+
resolution,
93116
},
94117
audio: {
95118
...LIVE_STREAM_PROPS_DEFAULTS.audio,
@@ -193,5 +216,5 @@ const ApiVideoLiveStreamView = forwardRef<
193216
);
194217
});
195218

196-
export * from './types';
197219
export { ApiVideoLiveStreamView };
220+
export type { Resolution, PredefinedResolution} from './NativeApiVideoLiveStreamView';

src/types.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)