Skip to content

Commit

Permalink
pbReader renamed variables for more clarity
Browse files Browse the repository at this point in the history
and delete decoder on opus errors
  • Loading branch information
dh1tw committed Dec 18, 2021
1 parent 50f3950 commit 45b7e57
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions audio/sources/pbReader/pbReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PbReader struct {
sync.RWMutex
enabled bool
name string
codecs map[string]audiocodec.Decoder
decoders map[string]audiocodec.Decoder
decoder audiocodec.Decoder
callback audio.OnDataCb
lastUser string
Expand All @@ -30,7 +30,7 @@ func NewPbReader() (*PbReader, error) {

pbr := &PbReader{
name: "ProtoBufReader",
codecs: make(map[string]audiocodec.Decoder),
decoders: make(map[string]audiocodec.Decoder),
lastUser: "",
}

Expand Down Expand Up @@ -121,15 +121,15 @@ func (pbr *PbReader) Enqueue(data []byte) error {
// users arrive at the same time. This ends up in a very distorted
// audio. Therefore we create a new decoder on demand for each txUser
if pbr.lastUser != txUser {
codec, ok := pbr.codecs[txUser]
codec, ok := pbr.decoders[txUser]
if !ok {
switch codecName {
case "opus":
newCodec, err := newOpusDecoder(channels)
if err != nil {
return (err)
}
pbr.codecs[txUser] = newCodec
pbr.decoders[txUser] = newCodec
pbr.decoder = newCodec
case "pcm":
// in case of PCM we might have to resample the audio
Expand All @@ -143,6 +143,11 @@ func (pbr *PbReader) Enqueue(data []byte) error {

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

Expand Down

0 comments on commit 45b7e57

Please sign in to comment.