mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-12 10:34:32 +08:00
057d171c71
* move on:change to <select> * add changeset * move device logic to separate file + fix safari incompatability * improve selected logic * add stream_utils unit tests * refactor default device logic --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
export function get_devices(): Promise<MediaDeviceInfo[]> {
|
|
return navigator.mediaDevices.enumerateDevices();
|
|
}
|
|
|
|
export function handle_error(error: string): void {
|
|
throw new Error(error);
|
|
}
|
|
|
|
export function set_local_stream(
|
|
local_stream: MediaStream | null,
|
|
video_source: HTMLVideoElement
|
|
): void {
|
|
video_source.srcObject = local_stream;
|
|
video_source.muted = true;
|
|
video_source.play();
|
|
}
|
|
|
|
export async function get_video_stream(
|
|
include_audio: boolean,
|
|
video_source: HTMLVideoElement,
|
|
device_id?: string
|
|
): Promise<MediaStream> {
|
|
const size = {
|
|
width: { ideal: 1920 },
|
|
height: { ideal: 1440 }
|
|
};
|
|
|
|
const constraints = {
|
|
video: device_id ? { deviceId: { exact: device_id }, ...size } : size,
|
|
audio: include_audio
|
|
};
|
|
|
|
return navigator.mediaDevices
|
|
.getUserMedia(constraints)
|
|
.then((local_stream: MediaStream) => {
|
|
set_local_stream(local_stream, video_source);
|
|
return local_stream;
|
|
});
|
|
}
|
|
|
|
export function set_available_devices(
|
|
devices: MediaDeviceInfo[]
|
|
): MediaDeviceInfo[] {
|
|
const cameras = devices.filter(
|
|
(device: MediaDeviceInfo) => device.kind === "videoinput"
|
|
);
|
|
|
|
return cameras;
|
|
}
|