Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Comms Rooms for each scene #6024

Draft
wants to merge 23 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 27 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 @@ -82,7 +82,7 @@
"fp-future": "^1.0.1",
"gifuct-js": "^2.1.2",
"hls.js": "^1.3.4",
"livekit-client": "^1.8.0",
"livekit-client": "^1.15.5",
"mitt": "^3.0.0",
"mz-observable": "^1.0.1",
"redux": "^4.2.1",
Expand Down
10 changes: 8 additions & 2 deletions browser-interface/packages/shared/comms/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ export const SET_ROOM_CONNECTION = '[COMMS] setRoomConnection'
export const setRoomConnection = (room: RoomConnection | undefined) => action(SET_ROOM_CONNECTION, room)
export type SetRoomConnectionAction = ReturnType<typeof setRoomConnection>

export const SET_SCENE_ROOM_CONNECTION = '[COMMS] setSceneRoomConnection'
export const setSceneRoomConnection = (sceneId: string, room: RoomConnection) =>
action(SET_SCENE_ROOM_CONNECTION, { sceneId, room })
export type SetSceneRoomConnectionAction = ReturnType<typeof setSceneRoomConnection>

export const HANDLE_ROOM_DISCONNECTION = '[COMMS] handleRoomDisconnection'
export const handleRoomDisconnection = (room: RoomConnection) => action(HANDLE_ROOM_DISCONNECTION, { context: room })
export type HandleRoomDisconnection = ReturnType<typeof handleRoomDisconnection>

export const SET_LIVEKIT_ADAPTER = '[COMMS] setLiveKitAdapter'
export const setLiveKitAdapter = (livekitAdapter: LivekitAdapter | undefined) => action(SET_LIVEKIT_ADAPTER, livekitAdapter)
export type SetLiveKitAdapterAction = ReturnType<typeof setLiveKitAdapter>
export const setLiveKitAdapter = (livekitAdapter: LivekitAdapter | undefined) =>
action(SET_LIVEKIT_ADAPTER, livekitAdapter)
export type SetLiveKitAdapterAction = ReturnType<typeof setLiveKitAdapter>
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 Down Expand Up @@ -78,7 +78,7 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
})
}

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

Expand All @@ -103,13 +103,16 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {
}

if (state !== ConnectionState.Connected) {
this.config.logger.log(`Skip sending message because connection state is ${state}`)
this.config.logger.log(`Skip sending message because connection state is ${state} ${this.room.name}`)
return
}

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',
Expand Down Expand Up @@ -160,4 +163,8 @@ export class LivekitAdapter implements MinimumCommunicationsAdapter {

return result
}

async getParticipants(): Promise<string[]> {
return Array.from(this.room.participants.values()).map((p) => p.identity)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +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 []
}
}
Loading