Skip to content

Commit

Permalink
Merge pull request #6157 from decentraland/update/livekit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza authored Apr 11, 2024
2 parents 9aa4fec + 312aa46 commit 85ed986
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 123 deletions.
151 changes: 43 additions & 108 deletions browser-interface/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion browser-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"fp-future": "^1.0.1",
"gifuct-js": "^2.1.2",
"hls.js": "^1.3.4",
"livekit-client": "^1.8.0",
"livekit-client": "^2.0.10",
"mitt": "^3.0.0",
"mz-observable": "^1.0.1",
"redux": "^4.2.1",
Expand Down
23 changes: 13 additions & 10 deletions browser-interface/packages/shared/comms/adapters/LivekitAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
public readonly events = mitt<CommsAdapterEvents>()

private disposed = false
private readonly room: Room
private voiceHandler: VoiceHandler

constructor(private config: LivekitConfig) {
this.room = new Room()
static async init(config: LivekitConfig) {
const room = new Room()
const sid = await room.getSid()
return new LivekitAdapter(config, room, sid)
}

private constructor(private config: LivekitConfig, private readonly room: Room, private sid: string) {
this.voiceHandler = createLiveKitVoiceHandler(this.room, this.config.globalAudioStream)

this.room
Expand All @@ -58,13 +61,13 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {

this.config.logger.log(this.room.name, 'disconnected from room', reason, {
liveKitParticipantSid: this.room.localParticipant.sid,
liveKitRoomSid: this.room.sid
liveKitRoomSid: this.sid
})
trackEvent('disconnection_cause', {
context: 'livekit-adapter',
message: `Got RoomEvent.Disconnected. Reason: ${reason}`,
liveKitParticipantSid: this.room.localParticipant.sid,
liveKitRoomSid: this.room.sid
liveKitRoomSid: this.sid
})
const kicked = reason === DisconnectReason.DUPLICATE_IDENTITY
this.do_disconnect(kicked).catch((err) => {
Expand Down Expand Up @@ -108,14 +111,14 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
}

try {
await this.room.localParticipant.publishData(data, reliable ? DataPacket_Kind.RELIABLE : DataPacket_Kind.LOSSY)
await this.room.localParticipant.publishData(data, { reliable })
} catch (err: any) {
// NOTE: for tracking purposes only, this is not a "code" error, this is a failed connection or a problem with the livekit instance
trackEvent('error', {
context: 'livekit-adapter',
message: `Error trying to send data. Reason: ${err.message}`,
stack: err.stack,
saga_stack: `room session id: ${this.room.sid}, participant id: ${this.room.localParticipant.sid}, state: ${state}`
saga_stack: `room session id: ${this.sid}, participant id: ${this.room.localParticipant.sid}, state: ${state}`
})
await this.disconnect()
}
Expand Down Expand Up @@ -144,12 +147,12 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {

getActiveVideoStreams(): Map<string, ActiveVideoStreams> {
const result = new Map<string, ActiveVideoStreams>()
const participants = this.room.participants
const participants = this.room.remoteParticipants

for (const [sid, participant] of participants) {
if (participant.videoTracks.size > 0) {
if (participant.videoTrackPublications.size > 0) {
const participantTracks = new Map<string, Track>()
for (const [videoSid, track] of participant.videoTracks) {
for (const [videoSid, track] of participant.videoTrackPublications) {
if (track.videoTrack?.mediaStream) {
participantTracks.set(videoSid, track.videoTrack)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function createLiveKitVoiceHandler(room: Room, globalAudioStream: GlobalA
participantsInfo.set(participant.identity, $)

participant.on(ParticipantEvent.IsSpeakingChanged, (talking: boolean) => {
const audioPublication = participant.getTrack(Track.Source.Microphone)
const audioPublication = participant.getTrackPublication(Track.Source.Microphone)
if (audioPublication && audioPublication.track) {
const audioTrack = audioPublication.track as RemoteAudioTrack
onUserTalkingCallback(participant.identity, audioTrack.isMuted ? false : talking)
Expand Down Expand Up @@ -200,7 +200,7 @@ export function createLiveKitVoiceHandler(room: Room, globalAudioStream: GlobalA
)
}

for (const [_, participant] of room.participants) {
for (const [_, participant] of room.remoteParticipants) {
const address = participant.identity
const peer = getPeer(address)
const participantInfo = participantsInfo.get(address)
Expand All @@ -209,7 +209,7 @@ export function createLiveKitVoiceHandler(room: Room, globalAudioStream: GlobalA
const profile = getCurrentUserProfile(state)
if (profile) {
const muted = !shouldPlayVoice(state, profile, address)
const audioPublication = participant.getTrack(Track.Source.Microphone)
const audioPublication = participant.getTrackPublication(Track.Source.Microphone)
if (audioPublication && audioPublication.track) {
const audioTrack = audioPublication.track as RemoteAudioTrack
audioTrack.setMuted(muted)
Expand Down
2 changes: 1 addition & 1 deletion browser-interface/packages/shared/comms/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async function connectAdapter(connStr: string, identity: ExplorerIdentity): Prom
throw new Error('No access token')
}

const livekitAdapter = new LivekitAdapter({
const livekitAdapter = await LivekitAdapter.init({
logger: commsLogger,
url: theUrl.origin + theUrl.pathname,
token,
Expand Down

0 comments on commit 85ed986

Please sign in to comment.