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,6 +56,13 @@ async function doConnect(
audioOptions: AudioCaptureOptions
): Promise<void> {
await livekitRoom!.connect(sfuConfig!.url, sfuConfig!.jwt);
const hasMicrophoneTrack = Array.from(
livekitRoom?.localParticipant.audioTracks.values()
).some((track: LocalTrackPublication) => {
return track.source == Track.Source.Microphone;
});
// We create a track in case there isn't any.
if (!hasMicrophoneTrack) {
const audioTracks = await livekitRoom!.localParticipant.createTracks({
audio: audioOptions,
});
@ -65,6 +74,7 @@ async function doConnect(
await livekitRoom?.localParticipant.publishTrack(audioTracks[0]);
}
}
export function useECConnectionState(
initialAudioOptions: AudioCaptureOptions,