Skip to content

Commit 93c9f96

Browse files
authored
Fix attribute update. Pass attribute diff instead of old state. (#496)
1 parent f6800e7 commit 93c9f96

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

callback.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ import (
2020
"github.com/livekit/protocol/livekit"
2121
)
2222

23+
// ParticipantAttributesChangedFunc is callback for Participant attribute change event.
24+
// The function is called with an already updated participant state and the map of changes attributes.
25+
// Deleted attributes will have empty string value in the changed map.
26+
type ParticipantAttributesChangedFunc func(changed map[string]string, p Participant)
27+
2328
type ParticipantCallback struct {
2429
// for all participants
2530
OnTrackMuted func(pub TrackPublication, p Participant)
2631
OnTrackUnmuted func(pub TrackPublication, p Participant)
2732
OnMetadataChanged func(oldMetadata string, p Participant)
28-
OnAttributesChanged func(oldAttrs map[string]string, p Participant)
33+
OnAttributesChanged ParticipantAttributesChangedFunc
2934
OnIsSpeakingChanged func(p Participant)
3035
OnConnectionQualityChanged func(update *livekit.ConnectionQualityInfo, p Participant)
3136

@@ -44,7 +49,7 @@ func NewParticipantCallback() *ParticipantCallback {
4449
OnTrackMuted: func(pub TrackPublication, p Participant) {},
4550
OnTrackUnmuted: func(pub TrackPublication, p Participant) {},
4651
OnMetadataChanged: func(oldMetadata string, p Participant) {},
47-
OnAttributesChanged: func(oldAttrs map[string]string, p Participant) {},
52+
OnAttributesChanged: func(changed map[string]string, p Participant) {},
4853
OnIsSpeakingChanged: func(p Participant) {},
4954
OnConnectionQualityChanged: func(update *livekit.ConnectionQualityInfo, p Participant) {},
5055
OnTrackSubscribed: func(track *webrtc.TrackRemote, publication *RemoteTrackPublication, rp *RemoteParticipant) {},

integration_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ var (
4747

4848
func TestMain(m *testing.M) {
4949
keys := strings.Split(os.Getenv("LIVEKIT_KEYS"), ": ")
50-
apiKey, apiSecret = keys[0], keys[1]
50+
if len(keys) >= 2 {
51+
apiKey, apiSecret = keys[0], keys[1]
52+
}
5153

5254
os.Exit(m.Run())
5355
}

participant.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ func (p *baseParticipant) setConnectionQualityInfo(info *livekit.ConnectionQuali
197197
p.roomCallback.OnConnectionQualityChanged(info, p)
198198
}
199199

200+
func attributeChanges(old, cur map[string]string) map[string]string {
201+
diff := make(map[string]string)
202+
for k, v := range cur {
203+
if old[k] != v {
204+
diff[k] = v // added or changed
205+
}
206+
}
207+
for k := range old {
208+
if _, ok := cur[k]; !ok {
209+
diff[k] = "" // deleted
210+
}
211+
}
212+
return diff
213+
}
214+
200215
func (p *baseParticipant) updateInfo(pi *livekit.ParticipantInfo, participant Participant) bool {
201216
p.lock.Lock()
202217
if p.info != nil && p.info.Version > pi.Version {
@@ -211,27 +226,17 @@ func (p *baseParticipant) updateInfo(pi *livekit.ParticipantInfo, participant Pa
211226
oldMetadata := p.metadata
212227
p.metadata = pi.Metadata
213228
metaChanged := oldMetadata != p.metadata
214-
attrsChanged := len(pi.Attributes) != 0
215-
oldAttributes := maps.Clone(p.attributes)
216-
if p.attributes == nil {
217-
p.attributes = make(map[string]string)
218-
}
219-
for k, v := range pi.Attributes {
220-
if v == "" {
221-
delete(p.attributes, k)
222-
} else {
223-
p.attributes[k] = v
224-
}
225-
}
229+
attrsChanges := attributeChanges(p.attributes, pi.Attributes)
230+
p.attributes = maps.Clone(pi.Attributes)
226231
p.lock.Unlock()
227232

228233
if metaChanged {
229234
p.Callback.OnMetadataChanged(oldMetadata, participant)
230235
p.roomCallback.OnMetadataChanged(oldMetadata, participant)
231236
}
232-
if attrsChanged {
233-
p.Callback.OnAttributesChanged(oldAttributes, participant)
234-
p.roomCallback.OnAttributesChanged(oldAttributes, participant)
237+
if len(attrsChanges) != 0 {
238+
p.Callback.OnAttributesChanged(attrsChanges, participant)
239+
p.roomCallback.OnAttributesChanged(attrsChanges, participant)
235240
}
236241
return true
237242
}

participant_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package lksdk
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"testing"
6+
)
7+
8+
func TestAttributeChanges(t *testing.T) {
9+
diff := attributeChanges(map[string]string{
10+
"a": "1",
11+
"b": "2",
12+
}, map[string]string{
13+
"a": "2",
14+
"c": "3",
15+
})
16+
require.Equal(t, map[string]string{
17+
"a": "2",
18+
"b": "",
19+
"c": "3",
20+
}, diff)
21+
}

0 commit comments

Comments
 (0)