Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed Apr 17, 2024
1 parent e5be732 commit a5a60ac
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 63 deletions.
28 changes: 14 additions & 14 deletions browser-interface/package-lock.json

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

4 changes: 2 additions & 2 deletions browser-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
},
"dependencies": {
"@dcl/crypto": "^3.3.1",
"@dcl/ecs": "https://sdk-team-cdn.decentraland.org/@dcl/js-sdk-toolchain/branch/feat/sdk7-audio-stream-audio-event-component/@dcl/ecs/dcl-ecs-7.4.14-8544623185.commit-305d325.tgz",
"@dcl/ecs": "7.4.16",
"@dcl/ecs-math": "^1.0.3",
"@dcl/ecs-quests": "^1.3.1",
"@dcl/feature-flags": "^1.1.0",
"@dcl/hashing": "^1.1.3",
"@dcl/kernel-interface": "^2.0.0-20230512115658.commit-b582e05",
"@dcl/legacy-ecs": "^6.11.11",
"@dcl/protocol": "https://sdk-team-cdn.decentraland.org/@dcl/protocol/branch//dcl-protocol-1.0.0-8544517864.commit-0416b65.tgz",
"@dcl/protocol": "1.0.0-8691799990.commit-4ba546c",
"@dcl/rpc": "^1.1.1",
"@dcl/scene-runtime": "7.0.6-20240220184109.commit-cf1e4e2",
"@dcl/schemas": "^9.1.1",
Expand Down
37 changes: 37 additions & 0 deletions browser-interface/packages/shared/world/runtime-7/engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Engine, IEngine, Transport } from '@dcl/ecs/dist-cjs'
import {
MediaState,
AudioEvent as defineAudioEvent,
Transform as defineTransform,
PlayerIdentityData as definePlayerIdentityData,
AvatarBase as defineAvatarBase,
Expand All @@ -20,6 +22,8 @@ import { Avatar } from '@dcl/schemas'
import { prepareAvatar } from '../../../lib/decentraland/profiles/transformations/profileToRendererFormat'
import { deepEqual } from '../../../lib/javascript/deepEqual'
import { positionObservable } from '../positionThings'
import { getSceneWorkerBySceneID, getSceneWorkerBySceneNumber } from '../parcelSceneManager'
import { SceneWorker } from '../SceneWorker'

export type IInternalEngine = {
engine: IEngine
Expand All @@ -37,6 +41,16 @@ type LocalProfileChange = {
triggerEmote: EmoteData
}

type State = {
sceneId: string | number
entityId: Entity
state: MediaState
}

type AudioStreamChange = {
changeState: State
}

function getUserData(userId: string) {
const dataFromStore = getProfileFromStore(store.getState(), userId)
if (!dataFromStore) return undefined
Expand All @@ -47,7 +61,30 @@ function getUserData(userId: string) {
}
}

function getScene(id: string | number): SceneWorker | undefined {
if (typeof id === 'string') return getSceneWorkerBySceneID(id)
if (typeof id === 'number') return getSceneWorkerBySceneNumber(id)
return undefined
}

function getEngineFromScene(scene?: SceneWorker): IEngine | undefined {
return scene?.rpcContext.internalEngine?.engine
}

export const localProfileChanged = mitt<LocalProfileChange>()
export const audioStreamEmitter = mitt<AudioStreamChange>()

// AudioStream updates
audioStreamEmitter.on('changeState', ({ entityId, sceneId, state }) => {
const scene = getScene(sceneId)
const engine = getEngineFromScene(scene)

if (!engine) return

const AudioEvent = defineAudioEvent(engine)
AudioEvent.addValue(entityId, { state, timestamp: Date.now() })
})
// end of AudioStream updates

/**
* We used this engine as an internal engine to add information to the worker.
Expand Down
70 changes: 23 additions & 47 deletions browser-interface/packages/unity-interface/audioStream.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/////////////////////////////////// AUDIO STREAMING ///////////////////////////////////
import { Entity, IEngine } from '@dcl/ecs/dist-cjs'
import { AudioEvent as defineAudioEvent, AudioState } from '@dcl/ecs/dist-cjs/components'
import { SceneWorker } from '../shared/world/SceneWorker'
import { getSceneWorkerBySceneID, getSceneWorkerBySceneNumber } from '../shared/world/parcelSceneManager'
import { Entity } from '@dcl/ecs/dist-cjs'
import { MediaState } from '@dcl/ecs/dist-cjs/components'

import { audioStreamEmitter } from '../shared/world/runtime-7/engine'

type AudioEvents = HTMLMediaElementEventMap
type GlobalProps = {
playToken: number
entityId: Entity | undefined
engine: IEngine | undefined
entityId?: Entity
sceneId?: string | number
}

let globalProps: GlobalProps = {
playToken: 0,
entityId: undefined,
engine: undefined
sceneId: undefined,
entityId: undefined
}

function setGlobalProps(props: GlobalProps) {
Expand All @@ -36,48 +36,36 @@ const audioStreamSource = new Audio()
AUDIO_EVENTS.forEach(($) => audioStreamSource.addEventListener($, listen))

function listen(ev: AudioEvents[keyof AudioEvents]) {
const { entityId, engine } = globalProps
const { entityId, sceneId } = globalProps

if (!entityId || !engine) return
if (!entityId || !sceneId) return

const AudioEvent = defineAudioEvent(engine)
const state = getAudioEvent(ev.type)
const timestamp = Date.now()

AudioEvent.addValue(entityId, { state, timestamp })
audioStreamEmitter.emit('changeState', { entityId, state, sceneId })
}

function getAudioEvent(type: string): AudioState {
function getAudioEvent(type: string): MediaState {
switch (type) {
case 'loadeddata':
return AudioState.AS_READY
return MediaState.MS_READY
case 'error':
return AudioState.AS_ERROR
return MediaState.MS_ERROR
case 'seeking':
return AudioState.AS_SEEKING
return MediaState.MS_SEEKING
case 'loadstart':
return AudioState.AS_LOADING
return MediaState.MS_LOADING
case 'waiting':
return AudioState.AS_BUFFERING
return MediaState.MS_BUFFERING
case 'playing':
return AudioState.AS_PLAYING
return MediaState.MS_PLAYING
case 'pause':
return AudioState.AS_PAUSED
return MediaState.MS_PAUSED
default:
return AudioState.AS_NONE
return MediaState.MS_NONE
}
}

function getScene(id: string | number): SceneWorker | undefined {
if (typeof id === 'string') return getSceneWorkerBySceneID(id)
if (typeof id === 'number') return getSceneWorkerBySceneNumber(id)
return undefined
}

function getEngineFromScene(scene?: SceneWorker): IEngine | undefined {
return scene?.rpcContext.internalEngine?.engine
}

export async function setAudioStream(url: string, play: boolean, volume: number) {
const isSameSrc =
audioStreamSource.src.length > 1 && (encodeURI(url) === audioStreamSource.src || url === audioStreamSource.src)
Expand All @@ -104,26 +92,14 @@ export async function setAudioStreamForEntity(
sceneId: string | number,
entityId: Entity
) {
setGlobalProps({ ...globalProps, sceneId, entityId })
void setAudioStream(url, play, volume)

const scene = getScene(sceneId)
const engine = getEngineFromScene(scene)

setGlobalProps({ ...globalProps, engine, entityId })
}

export async function killAudioStream(sceneId: number, entityId: Entity) {
setGlobalProps({ ...globalProps, sceneId: undefined, entityId: undefined })
void setAudioStream(audioStreamSource.src, false, audioStreamSource.volume)

const scene = getScene(sceneId)
const engine = getEngineFromScene(scene)

if (engine) {
const AudioEvent = defineAudioEvent(engine)
AudioEvent.addValue(entityId, { state: AudioState.AS_NONE, timestamp: Date.now() })
}

setGlobalProps({ ...globalProps, engine: undefined, entityId: undefined })
audioStreamEmitter.emit('changeState', { entityId, state: MediaState.MS_NONE, sceneId })
}

// audioStreamSource play might be requested without user interaction
Expand Down

0 comments on commit a5a60ac

Please sign in to comment.