Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.
Merged
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
3 changes: 3 additions & 0 deletions packages/shared/renderer/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function convertCurrentRealmType(realm: Realm, contentServerUrl: string): Curren

function* updateUserVoicePlayingRenderer(action: VoicePlayingUpdateAction) {
const { playing, userId } = action.payload
yield call(waitForRendererInstance)
getUnityInstance().SetUserTalking(userId, playing)
}

Expand All @@ -120,6 +121,7 @@ function* updatePlayerVoiceRecordingRenderer(action: VoiceRecordingUpdateAction)
}

function* updateChangeVoiceChatHandler(action: SetVoiceChatHandlerAction) {
yield call(waitForRendererInstance)
if (action.payload.voiceHandler) {
getUnityInstance().SetVoiceChatStatus({ isConnected: true })
} else {
Expand All @@ -129,6 +131,7 @@ function* updateChangeVoiceChatHandler(action: SetVoiceChatHandlerAction) {

function* handleVoiceChatError(action: SetVoiceChatErrorAction) {
const message = action.payload.message
yield call(waitForRendererInstance)
if (message) {
getUnityInstance().ShowNotification({
type: NotificationType.GENERIC,
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/voiceChat/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const setVoiceChatPolicy = (policy: VoicePolicy) => action(SET_VOICE_CHAT
export type SetVoiceChatPolicyAction = ReturnType<typeof setVoiceChatPolicy>

export const SET_VOICE_CHAT_MEDIA = '[VC] setVoiceChatMedia'
export const setVoiceChatMedia = (media: any | undefined) => action(SET_VOICE_CHAT_MEDIA, { media })
export const setVoiceChatMedia = (media: MediaStream | undefined) => action(SET_VOICE_CHAT_MEDIA, { media })
export type SetVoiceChatMediaAction = ReturnType<typeof setVoiceChatMedia>

export type VoiceChatActions =
Expand Down
17 changes: 14 additions & 3 deletions packages/shared/voiceChat/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { getCurrentUserProfile } from '../profiles/selectors'
import { Package, VoiceFragment } from '../comms/interface/types'
import { setupPeer } from '../comms/peers'
import { getPeer } from '../comms/peers'
import { store } from '../store/isolatedStore'
import { getVoiceHandler, shouldPlayVoice } from './selectors'
import { voiceChatLogger } from './context'
import { trackEvent } from 'shared/analytics'

export function processVoiceFragment(message: Package<VoiceFragment>) {
const state = store.getState()
const voiceHandler = getVoiceHandler(state)
const profile = getCurrentUserProfile(state)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Copy Markdown
Member

@kuruk-mm kuruk-mm Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is from opus voice chat. I just moved it from the sagas to this new file. I tried to modify it least as possible.


const peerTrackingInfo = setupPeer(message.sender)
// use getPeed instead of setupPeer to only reproduce voice messages from
// known avatars
const peerTrackingInfo = getPeer(message.sender)

if (
voiceHandler &&
profile &&
peerTrackingInfo &&
peerTrackingInfo.ethereumAddress &&
peerTrackingInfo.position &&
shouldPlayVoice(state, profile, peerTrackingInfo.ethereumAddress)
) {
voiceHandler
.playEncodedAudio(peerTrackingInfo.ethereumAddress, peerTrackingInfo.position, message.data)
.catch((e) => voiceChatLogger.error('Error playing encoded audio!', e))
.catch((e: any) => {
trackEvent('error', {
context: 'voice-chat',
message: e.message,
stack: ''
})
voiceChatLogger.error('Error playing encoded audio!', e)
})
}
}
10 changes: 5 additions & 5 deletions packages/shared/voiceChat/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ function* handleJoinVoiceChat() {
const commsContext = yield select(getCommsContext)
const voiceChatState: VoiceChatState = yield select(getVoiceChatState)
if (commsContext) {
const voiceHandler =
const voiceHandler: VoiceHandler =
voiceChatState.liveKitRoom !== null
? yield createLiveKitVoiceHandler(voiceChatState.liveKitRoom)
? yield call(createLiveKitVoiceHandler, voiceChatState.liveKitRoom)
: createOpusVoiceHandler(commsContext.worldInstanceConnection)

yield put(clearVoiceChatError())
Expand All @@ -146,7 +146,6 @@ function* handleJoinVoiceChat() {
})

voiceHandler.onError((message) => {
//store.dispatch(setVoiceChatError(message))
put(setVoiceChatError(message))
})

Expand Down Expand Up @@ -191,7 +190,7 @@ function* handleLeaveVoiceChat() {

function* handleVoiceChatMedia({ payload }: SetVoiceChatMediaAction) {
const voiceHandler: VoiceHandler | null = yield select(getVoiceHandler)
if (voiceHandler) {
if (voiceHandler && payload.media) {
yield voiceHandler.setInputStream(payload.media)
}
}
Expand Down Expand Up @@ -223,13 +222,14 @@ function* handleVoiceChatMute(action: SetVoiceChatMuteAction) {

let audioRequestPending = false

async function requestMediaDevice() {
async function requestMediaDevice(deviceId?: string) {
if (!audioRequestPending) {
audioRequestPending = true

try {
const media = await navigator.mediaDevices.getUserMedia({
audio: {
deviceId,
channelCount: 1,
sampleRate: VOICE_CHAT_SAMPLE_RATE,
echoCancellation: true,
Expand Down