Check for existing track before creating one. (#1459)

Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
Timo 2023-09-18 12:43:03 +02:00 committed by GitHub
parent cd5c0c9b55
commit b979fc85bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,8 +17,10 @@ limitations under the License.
import {
AudioCaptureOptions,
ConnectionState,
LocalTrackPublication,
Room,
RoomEvent,
Track,
} from "livekit-client";
import { useCallback, useEffect, useRef, useState } from "react";
import { logger } from "matrix-js-sdk/src/logger";
@ -54,16 +56,24 @@ async function doConnect(
audioOptions: AudioCaptureOptions
): Promise<void> {
await livekitRoom!.connect(sfuConfig!.url, sfuConfig!.jwt);
const audioTracks = await livekitRoom!.localParticipant.createTracks({
audio: audioOptions,
const hasMicrophoneTrack = Array.from(
livekitRoom?.localParticipant.audioTracks.values()
).some((track: LocalTrackPublication) => {
return track.source == Track.Source.Microphone;
});
if (audioTracks.length < 1) {
logger.info("Tried to pre-create local audio track but got no tracks");
return;
}
if (!audioEnabled) await audioTracks[0].mute();
// We create a track in case there isn't any.
if (!hasMicrophoneTrack) {
const audioTracks = await livekitRoom!.localParticipant.createTracks({
audio: audioOptions,
});
if (audioTracks.length < 1) {
logger.info("Tried to pre-create local audio track but got no tracks");
return;
}
if (!audioEnabled) await audioTracks[0].mute();
await livekitRoom?.localParticipant.publishTrack(audioTracks[0]);
await livekitRoom?.localParticipant.publishTrack(audioTracks[0]);
}
}
export function useECConnectionState(