Skip to content

Commit

Permalink
chore: simplify voice chat (#6033)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoArregui authored Jan 2, 2024
1 parent 3048c8c commit 3f39030
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 1,330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ import type { VoiceHandler } from 'shared/voiceChat/VoiceHandler'
import { commsLogger } from '../logger'
import type { ActiveVideoStreams, CommsAdapterEvents, MinimumCommunicationsAdapter, SendHints } from './types'
import { createLiveKitVoiceHandler } from './voice/liveKitVoiceHandler'
import { GlobalAudioStream } from './voice/loopback'

export type LivekitConfig = {
url: string
token: string
logger: ILogger
globalAudioStream: GlobalAudioStream
voiceChatEnabled: boolean
}

export class LivekitAdapter implements MinimumCommunicationsAdapter {
public readonly events = mitt<CommsAdapterEvents>()

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

constructor(private config: LivekitConfig) {
this.room = new Room()

this.voiceHandler = createLiveKitVoiceHandler(this.room, this.config.globalAudioStream)
if (config.voiceChatEnabled) {
this.voiceHandler = createLiveKitVoiceHandler(this.room)
}

this.room
.on(RoomEvent.ParticipantConnected, (_: RemoteParticipant) => {
Expand All @@ -52,7 +52,6 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
this.config.logger.log(this.room.name, 'connection state changed', state)
})
.on(RoomEvent.Disconnected, (reason: DisconnectReason | undefined) => {
this.config.logger.log('[BOEDO]: on disconnect', reason, this.room.name)
if (this.disposed) {
return
}
Expand All @@ -79,7 +78,7 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
})
}

async createVoiceHandler(): Promise<VoiceHandler> {
async getVoiceHandler(): Promise<VoiceHandler | undefined> {
return this.voiceHandler
}

Expand Down Expand Up @@ -111,14 +110,16 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
try {
await this.room.localParticipant.publishData(data, reliable ? DataPacket_Kind.RELIABLE : DataPacket_Kind.LOSSY)
} catch (err: any) {
if (this.disposed) {
return
}
// 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}`
})
this.config.logger.log('[BOEDO]: error sending data', err, err.mesage)
await this.disconnect()
}
}
Expand All @@ -132,7 +133,6 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
return
}

this.config.logger.log('[BOEDO]: do_disconnect', this.room.name)
this.disposed = true
await this.room.disconnect().catch(commsLogger.error)
this.events.emit('DISCONNECTION', { kicked })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import mitt from 'mitt'
import { VoiceHandler } from 'shared/voiceChat/VoiceHandler'
import { CommsAdapterEvents, MinimumCommunicationsAdapter, SendHints } from './types'
import { createOpusVoiceHandler } from './voice/opusVoiceHandler'

export class OfflineAdapter implements MinimumCommunicationsAdapter {
events = mitt<CommsAdapterEvents>()

constructor() {}
async createVoiceHandler(): Promise<VoiceHandler> {
return createOpusVoiceHandler()
async getVoiceHandler(): Promise<VoiceHandler | undefined> {
return undefined
}
async disconnect(_error?: Error | undefined): Promise<void> {}
send(_data: Uint8Array, _hints: SendHints): void {}
async connect(): Promise<void> {}
async getParticipants() { return [] }
async getParticipants() {
return []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import {
Position,
ProfileRequest,
ProfileResponse,
Scene,
Voice
Scene
} from 'shared/protocol/decentraland/kernel/comms/rfc4/comms.gen'
import { lastPlayerPosition } from 'shared/world/positionThings'
import { CommsEvents, RoomConnection } from '../interface'
import { Rfc4RoomConnection } from '../logic/rfc-4-room-connection'
import { CommsAdapterEvents, SendHints } from './types'
import { VoiceHandler } from 'shared/voiceChat/VoiceHandler'
import { createOpusVoiceHandler } from './voice/opusVoiceHandler'

export class SimulationRoom implements RoomConnection {
events = mitt<CommsEvents>()
Expand Down Expand Up @@ -51,17 +49,17 @@ export class SimulationRoom implements RoomConnection {
send(_data: Uint8Array, _hints: SendHints): void {},
async connect(): Promise<void> {},
async disconnect(_error?: Error): Promise<void> {},
async createVoiceHandler() {
throw new Error('not implemented')
async getVoiceHandler(): Promise<VoiceHandler | undefined> {
return undefined
},
async getParticipants() {
return Array.from(peers.keys())
}
})
}

async createVoiceHandler(): Promise<VoiceHandler> {
return createOpusVoiceHandler()
async getVoiceHandler(): Promise<VoiceHandler | undefined> {
return this.roomConnection.getVoiceHandler()
}

async spawnPeer(): Promise<string> {
Expand Down Expand Up @@ -135,9 +133,6 @@ export class SimulationRoom implements RoomConnection {
async sendChatMessage(message: Chat): Promise<void> {
await this.roomConnection.sendChatMessage(message)
}
async sendVoiceMessage(message: Voice): Promise<void> {
await this.roomConnection.sendVoiceMessage(message)
}

update() {
let i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ExplorerIdentity } from 'shared/session/types'
import { Authenticator } from '@dcl/crypto'
import mitt from 'mitt'
import { CommsAdapterEvents, MinimumCommunicationsAdapter, SendHints } from './types'
import { createOpusVoiceHandler } from './voice/opusVoiceHandler'
import { VoiceHandler } from 'shared/voiceChat/VoiceHandler'
import { notifyStatusThroughChat } from 'shared/chat'
import { wsAsAsyncChannel } from '../logic/ws-async-channel'
Expand Down Expand Up @@ -36,7 +35,10 @@ export class WebSocketAdapter implements MinimumCommunicationsAdapter {

private ws: WebSocket | null = null

constructor(public url: string, private identity: ExplorerIdentity) {}
constructor(
public url: string,
private identity: ExplorerIdentity
) {}

async connect(): Promise<void> {
if (this.ws) throw new Error('Cannot call connect twice per IBrokerTransport')
Expand Down Expand Up @@ -131,8 +133,8 @@ export class WebSocketAdapter implements MinimumCommunicationsAdapter {
}
}

async createVoiceHandler(): Promise<VoiceHandler> {
return createOpusVoiceHandler()
async getVoiceHandler(): Promise<VoiceHandler | undefined> {
return undefined
}

handleWelcomeMessage(welcomeMessage: rfc5.WsWelcome, socket: WebSocket) {
Expand Down
2 changes: 1 addition & 1 deletion browser-interface/packages/shared/comms/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface MinimumCommunicationsAdapter {
*/
events: Emitter<CommsAdapterEvents>

createVoiceHandler(): Promise<VoiceHandler>
getVoiceHandler(): Promise<VoiceHandler | undefined>

getParticipants(): Promise<string[]>
}
Expand Down
12 changes: 0 additions & 12 deletions browser-interface/packages/shared/comms/adapters/voice/Html.ts

This file was deleted.

Loading

0 comments on commit 3f39030

Please sign in to comment.