mirror of
https://github.com/HumanAIGC-Engineering/gradio-webrtc.git
synced 2026-02-05 18:09:23 +08:00
fix track_constraints support
Fixed an issue where track_constraints does not take effect in the audio-video mode.
This commit is contained in:
@@ -8,7 +8,7 @@ export function handle_error(error: string): void {
|
|||||||
|
|
||||||
export function set_local_stream(
|
export function set_local_stream(
|
||||||
local_stream: MediaStream | null,
|
local_stream: MediaStream | null,
|
||||||
video_source: HTMLVideoElement,
|
video_source: HTMLVideoElement
|
||||||
): void {
|
): void {
|
||||||
video_source.srcObject = local_stream;
|
video_source.srcObject = local_stream;
|
||||||
video_source.muted = true;
|
video_source.muted = true;
|
||||||
@@ -16,23 +16,31 @@ export function set_local_stream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function get_video_stream(
|
export async function get_video_stream(
|
||||||
include_audio: boolean,
|
include_audio: boolean | { deviceId: { exact: string } },
|
||||||
video_source: HTMLVideoElement,
|
video_source: HTMLVideoElement,
|
||||||
device_id?: string,
|
device_id?: string,
|
||||||
track_constraints?: MediaTrackConstraints,
|
track_constraints?:
|
||||||
|
| MediaTrackConstraints
|
||||||
|
| { video: MediaTrackConstraints; audio: MediaTrackConstraints }
|
||||||
): Promise<MediaStream> {
|
): Promise<MediaStream> {
|
||||||
const fallback_constraints = track_constraints || {
|
const video_fallback_constraints = (track_constraints as any)?.video ||
|
||||||
width: { ideal: 500 },
|
track_constraints || {
|
||||||
height: { ideal: 500 },
|
width: { ideal: 500 },
|
||||||
};
|
height: { ideal: 500 },
|
||||||
|
};
|
||||||
|
const audio_fallback_constraints = (track_constraints as any)?.audio ||
|
||||||
|
track_constraints;
|
||||||
|
|
||||||
const constraints = {
|
const constraints = {
|
||||||
video: device_id
|
video: device_id
|
||||||
? { deviceId: { exact: device_id }, ...fallback_constraints }
|
? { deviceId: { exact: device_id }, ...video_fallback_constraints }
|
||||||
: fallback_constraints,
|
: video_fallback_constraints,
|
||||||
audio: include_audio,
|
audio: include_audio
|
||||||
|
? typeof include_audio === "object"
|
||||||
|
? { ...include_audio, ...audio_fallback_constraints }
|
||||||
|
: audio_fallback_constraints
|
||||||
|
: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
return navigator.mediaDevices
|
return navigator.mediaDevices
|
||||||
.getUserMedia(constraints)
|
.getUserMedia(constraints)
|
||||||
.then((local_stream: MediaStream) => {
|
.then((local_stream: MediaStream) => {
|
||||||
@@ -43,10 +51,10 @@ export async function get_video_stream(
|
|||||||
|
|
||||||
export function set_available_devices(
|
export function set_available_devices(
|
||||||
devices: MediaDeviceInfo[],
|
devices: MediaDeviceInfo[],
|
||||||
kind: "videoinput" | "audioinput" = "videoinput",
|
kind: "videoinput" | "audioinput" = "videoinput"
|
||||||
): MediaDeviceInfo[] {
|
): MediaDeviceInfo[] {
|
||||||
const cameras = devices.filter(
|
const cameras = devices.filter(
|
||||||
(device: MediaDeviceInfo) => device.kind === kind,
|
(device: MediaDeviceInfo) => device.kind === kind
|
||||||
);
|
);
|
||||||
|
|
||||||
return cameras;
|
return cameras;
|
||||||
|
|||||||
Reference in New Issue
Block a user