Skip to content

Commit 45b7e57

Browse files
committed
pbReader renamed variables for more clarity
and delete decoder on opus errors
1 parent 50f3950 commit 45b7e57

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

audio/sources/pbReader/pbReader.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type PbReader struct {
1919
sync.RWMutex
2020
enabled bool
2121
name string
22-
codecs map[string]audiocodec.Decoder
22+
decoders map[string]audiocodec.Decoder
2323
decoder audiocodec.Decoder
2424
callback audio.OnDataCb
2525
lastUser string
@@ -30,7 +30,7 @@ func NewPbReader() (*PbReader, error) {
3030

3131
pbr := &PbReader{
3232
name: "ProtoBufReader",
33-
codecs: make(map[string]audiocodec.Decoder),
33+
decoders: make(map[string]audiocodec.Decoder),
3434
lastUser: "",
3535
}
3636

@@ -121,15 +121,15 @@ func (pbr *PbReader) Enqueue(data []byte) error {
121121
// users arrive at the same time. This ends up in a very distorted
122122
// audio. Therefore we create a new decoder on demand for each txUser
123123
if pbr.lastUser != txUser {
124-
codec, ok := pbr.codecs[txUser]
124+
codec, ok := pbr.decoders[txUser]
125125
if !ok {
126126
switch codecName {
127127
case "opus":
128128
newCodec, err := newOpusDecoder(channels)
129129
if err != nil {
130130
return (err)
131131
}
132-
pbr.codecs[txUser] = newCodec
132+
pbr.decoders[txUser] = newCodec
133133
pbr.decoder = newCodec
134134
case "pcm":
135135
// in case of PCM we might have to resample the audio
@@ -143,6 +143,11 @@ func (pbr *PbReader) Enqueue(data []byte) error {
143143

144144
num, err := pbr.decoder.Decode(msg.Data, buf)
145145
if err != nil {
146+
// in case the txUser has switched from stereo to mono
147+
// the samples won't fit into buf anymore. Therefore we
148+
// simple ignore the sample and delete the decoder for that user
149+
delete(pbr.decoders, txUser)
150+
pbr.lastUser = ""
146151
return err
147152
}
148153

0 commit comments

Comments
 (0)