Add server-side spatial audio with HRTF, occlusion and adaptive quality
This commit is contained in:
@@ -152,6 +152,8 @@ type Room struct {
|
||||
onStateChangeMu sync.Mutex
|
||||
localParticipantListener types.LocalParticipantListener
|
||||
participantTelemetryListener types.ParticipantTelemetryListener
|
||||
spatial *spatialAudioRoom
|
||||
spatialErr error
|
||||
}
|
||||
|
||||
type ParticipantOptions struct {
|
||||
@@ -289,6 +291,10 @@ func NewRoom(
|
||||
}),
|
||||
}
|
||||
r.trackManager = NewRoomTrackManager(r.logger)
|
||||
r.spatial, r.spatialErr = newSpatialAudioRoom(r)
|
||||
if r.spatialErr != nil {
|
||||
r.logger.Errorw("spatial audio initialization failed", r.spatialErr)
|
||||
}
|
||||
r.localParticipantListener = &localParticipantListener{room: r}
|
||||
r.participantTelemetryListener = &participantTelemetryListener{room: r}
|
||||
|
||||
@@ -451,6 +457,12 @@ func (r *Room) Join(
|
||||
if r.IsClosed() {
|
||||
return ErrRoomClosed
|
||||
}
|
||||
if r.spatialErr != nil {
|
||||
return spatialJoinError(r.spatialErr)
|
||||
}
|
||||
if r.spatial != nil && len(r.participants) >= r.spatial.config.MaxParticipants {
|
||||
return ErrMaxParticipantsExceeded
|
||||
}
|
||||
|
||||
if r.participants[participant.Identity()] != nil {
|
||||
return ErrAlreadyJoined
|
||||
@@ -595,6 +607,9 @@ func (r *Room) ResumeParticipant(
|
||||
if err := p.SendParticipantUpdate(updates); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.spatial != nil {
|
||||
go r.attachSpatial(p)
|
||||
}
|
||||
|
||||
_ = p.SendRoomUpdate(r.ToProto())
|
||||
p.ICERestart(iceConfig)
|
||||
@@ -727,6 +742,13 @@ func (r *Room) ResolveMediaTrackForSubscriber(sub types.LocalParticipant, trackI
|
||||
res.TrackRemovedNotifier = r.trackManager.GetOrCreateTrackRemoveNotifier(trackID)
|
||||
res.PublisherIdentity = info.PublisherIdentity
|
||||
res.PublisherID = info.PublisherID
|
||||
if output, ok := info.Track.(*spatialPublication); ok {
|
||||
res.HasPermission = output.target == sub.ID()
|
||||
return res
|
||||
}
|
||||
if r.spatial != nil && info.Track.Kind() == livekit.TrackType_AUDIO {
|
||||
return res
|
||||
}
|
||||
|
||||
pub := r.GetParticipantByID(info.PublisherID)
|
||||
// when publisher is not found, we will assume it doesn't have permission to access
|
||||
@@ -820,6 +842,9 @@ func (r *Room) Close(reason types.RoomCloseReason) {
|
||||
r.lock.Unlock()
|
||||
|
||||
r.logger.Infow("closing room", "reason", reason)
|
||||
if r.spatial != nil {
|
||||
r.spatial.close()
|
||||
}
|
||||
participantCloseReason := reason.ToParticipantCloseReason()
|
||||
for _, p := range r.GetParticipants() {
|
||||
_ = p.Close(true, participantCloseReason, false)
|
||||
@@ -1075,6 +1100,9 @@ func (r *Room) createJoinResponseLocked(
|
||||
// a ParticipantImpl in the room added a new track, subscribe other participants to it
|
||||
func (r *Room) onTrackPublished(participant types.Participant, track types.MediaTrack) {
|
||||
r.trackManager.AddTrack(track, participant.Identity(), participant.ID())
|
||||
if r.spatial != nil {
|
||||
r.spatial.published(participant, track)
|
||||
}
|
||||
|
||||
// publish participant update, since track state is changed
|
||||
r.broadcastParticipantState(participant, broadcastOptions{skipSource: true})
|
||||
@@ -1082,6 +1110,9 @@ func (r *Room) onTrackPublished(participant types.Participant, track types.Media
|
||||
r.lock.RLock()
|
||||
// subscribe all existing participants to this MediaTrack
|
||||
for _, existingParticipant := range r.participants {
|
||||
if r.spatial != nil && track.Kind() == livekit.TrackType_AUDIO {
|
||||
continue
|
||||
}
|
||||
if existingParticipant == participant {
|
||||
// skip publishing participant
|
||||
continue
|
||||
@@ -1245,6 +1276,9 @@ func (r *Room) onStateChange(p types.LocalParticipant) {
|
||||
|
||||
switch p.State() {
|
||||
case livekit.ParticipantInfo_ACTIVE:
|
||||
if r.spatial != nil {
|
||||
go r.attachSpatial(p)
|
||||
}
|
||||
// subscribe participant to existing published tracks
|
||||
r.subscribeToExistingTracks(p, false)
|
||||
|
||||
@@ -1290,6 +1324,12 @@ func (r *Room) onStateChange(p types.LocalParticipant) {
|
||||
}
|
||||
|
||||
func (r *Room) onDataMessage(source types.LocalParticipant, kind livekit.DataPacket_Kind, dp *livekit.DataPacket) {
|
||||
if r.spatial != nil && r.spatial.scene(source, kind, dp) {
|
||||
return
|
||||
}
|
||||
if r.spatial != nil && r.spatial.pose(source, dp) {
|
||||
return
|
||||
}
|
||||
if kind == livekit.DataPacket_RELIABLE && source != nil && dp.GetSequence() > 0 {
|
||||
data, err := proto.Marshal(dp)
|
||||
if err != nil {
|
||||
@@ -1455,6 +1495,9 @@ func (r *Room) RemoveParticipant(
|
||||
}
|
||||
r.lock.Unlock()
|
||||
r.protoProxy.MarkDirty(immediateChange)
|
||||
if r.spatial != nil {
|
||||
r.spatial.remove(p)
|
||||
}
|
||||
|
||||
if !p.HasConnected() {
|
||||
fields := append(
|
||||
@@ -1518,6 +1561,9 @@ func (r *Room) subscribeToExistingTracks(p types.LocalParticipant, isSync bool)
|
||||
// subscribe to all
|
||||
if autoSubscribe {
|
||||
for _, track := range op.GetPublishedTracks() {
|
||||
if r.spatial != nil && track.Kind() == livekit.TrackType_AUDIO {
|
||||
continue
|
||||
}
|
||||
trackIDs = append(trackIDs, track.ID())
|
||||
p.SubscribeToTrack(track.ID(), isSync)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user