-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathactive_speaker_observer.go
91 lines (74 loc) · 2.75 KB
/
active_speaker_observer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mediasoup
import (
"encoding/json"
"github.com/go-logr/logr"
)
type ActiveSpeakerObserverOptions struct {
Interval int `json:"interval"`
AppData interface{} `json:"appData,omitempty"`
}
type ActiveSpeakerObserverActivity struct {
// Producer is the dominant audio producer instance.
Producer *Producer
}
// ActiveSpeakerObserver monitors the speech activity of the selected audio producers. It just
// handles audio producers (if addProducer() is called with a video producer it will fail).
//
// Implementation of Dominant Speaker Identification for Multipoint Videoconferencing by Ilana
// Volfin and Israel Cohen. This implementation uses the RTP Audio Level extension from RFC-6464
// for the input signal. This has been ported from DominantSpeakerIdentification.java in Jitsi.
// Audio levels used for speech detection are read from an RTP header extension. No decoding of
// audio data is done. See RFC6464 for more information.
//
// - @emits dominantspeaker - (activity *ActiveSpeakerObserverActivity)
type ActiveSpeakerObserver struct {
IRtpObserver
logger logr.Logger
onDominantSpeaker func(speaker *ActiveSpeakerObserverActivity)
}
func newActiveSpeakerObserver(params rtpObserverParams) *ActiveSpeakerObserver {
o := &ActiveSpeakerObserver{
IRtpObserver: newRtpObserver(params),
logger: NewLogger("ActiveSpeakerObserver"),
}
o.handleWorkerNotifications(params)
return o
}
// Deprecated
//
// - @emits dominantspeaker - (activity *ActiveSpeakerObserverActivity)
func (o *ActiveSpeakerObserver) Observer() IEventEmitter {
return o.IRtpObserver.Observer()
}
// OnDominantSpeaker set handler on "dominantspeaker" event
func (o *ActiveSpeakerObserver) OnDominantSpeaker(handler func(speaker *ActiveSpeakerObserverActivity)) {
o.onDominantSpeaker = handler
}
func (o *ActiveSpeakerObserver) handleWorkerNotifications(params rtpObserverParams) {
rtpObserverId := params.internal.RtpObserverId
getProducerById := params.getProducerById
type eventInfo struct {
ProducerId string `json:"producerId,omitempty"`
}
params.channel.Subscribe(rtpObserverId, func(event string, data []byte) {
switch event {
case "dominantspeaker":
event := eventInfo{}
if err := json.Unmarshal(data, &event); err != nil {
o.logger.Error(err, "unmarshal dominantspeaker failed", "data", json.RawMessage(data))
break
}
dominantSpeaker := &ActiveSpeakerObserverActivity{
Producer: getProducerById(event.ProducerId),
}
o.SafeEmit("dominantspeaker", dominantSpeaker)
// Emit observer event.
o.Observer().SafeEmit("dominantspeaker", dominantSpeaker)
if handler := o.onDominantSpeaker; handler != nil {
handler(dominantSpeaker)
}
default:
o.logger.Error(nil, "ignoring unknown event", "event", event)
}
})
}