From 01412cbea5e27a75762445e08cb6b8fae448ef04 Mon Sep 17 00:00:00 2001 From: elinor Date: Mon, 26 Mar 2018 07:40:41 +0300 Subject: [PATCH] Added packet loss and inband fec encoder ctls --- encoder.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ encoder_test.go | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/encoder.go b/encoder.go index 0ffaa66..f8e890a 100644 --- a/encoder.go +++ b/encoder.go @@ -68,6 +68,30 @@ bridge_encoder_get_max_bandwidth(OpusEncoder *st, opus_int32 *max_bw) return opus_encoder_ctl(st, OPUS_GET_MAX_BANDWIDTH(max_bw)); } +int +bridge_encoder_set_inband_fec(OpusEncoder *st, opus_int32 fec) +{ + return opus_encoder_ctl(st, OPUS_SET_INBAND_FEC(fec)); +} + +int +bridge_encoder_get_inband_fec(OpusEncoder *st, opus_int32 *fec) +{ + return opus_encoder_ctl(st, OPUS_GET_INBAND_FEC(fec)); +} + +int +bridge_encoder_set_packet_loss_perc(OpusEncoder *st, opus_int32 loss_perc) +{ + return opus_encoder_ctl(st, OPUS_SET_PACKET_LOSS_PERC(loss_perc)); +} + +int +bridge_encoder_get_packet_loss_perc(OpusEncoder *st, opus_int32 *loss_perc) +{ + return opus_encoder_ctl(st, OPUS_GET_PACKET_LOSS_PERC(loss_perc)); +} + */ import "C" @@ -301,3 +325,43 @@ func (enc *Encoder) MaxBandwidth() (Bandwidth, error) { } return Bandwidth(maxBw), nil } + + +// SetInBandFEC configures the encoder's use of inband forward error +// correction (FEC) +func (enc *Encoder) SetInBandFEC(fec int) error { + res := C.bridge_encoder_set_inband_fec(enc.p, C.opus_int32(fec)) + if res != C.OPUS_OK { + return Error(res) + } + return nil +} + +// InBandFEC gets the encoder's configured inband forward error correction (FEC) +func (enc *Encoder) InBandFEC() (int, error) { + var fec C.opus_int32 + res := C.bridge_encoder_get_inband_fec(enc.p, &fec) + if res != C.OPUS_OK { + return 0, Error(res) + } + return int(fec), nil +} + +// SetPacketLossPerc configures the encoder's expected packet loss percentage. +func (enc *Encoder) SetPacketLossPerc(lossPerc int) error { + res := C.bridge_encoder_set_packet_loss_perc(enc.p, C.opus_int32(lossPerc)) + if res != C.OPUS_OK { + return Error(res) + } + return nil +} + +// PacketLossPerc gets the encoder's configured packet loss percentage. +func (enc *Encoder) PacketLossPerc() (int, error) { + var lossPerc C.opus_int32 + res := C.bridge_encoder_get_packet_loss_perc(enc.p, &lossPerc) + if res != C.OPUS_OK { + return 0, Error(res) + } + return int(lossPerc), nil +} diff --git a/encoder_test.go b/encoder_test.go index 086d789..63ae43a 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -259,3 +259,57 @@ func TestEncoder_SetGetMaxBandwidth(t *testing.T) { } } } + +func TestEncoder_SetGetInBandFEC(t *testing.T) { + enc, err := NewEncoder(8000, 1, AppVoIP) + if err != nil || enc == nil { + t.Errorf("Error creating new encoder: %v", err) + } + + + if err := enc.SetInBandFEC(1); err != nil { + t.Error("Error setting fec:", err) + } + + fec, err := enc.InBandFEC() + if err != nil { + t.Error("Error getting fec", err) + } + if fec !=1{ + t.Error("Wrong fec value ") + } + + if err := enc.SetInBandFEC(0); err != nil { + t.Error("Error setting fec:", err) + } + + fec, err = enc.InBandFEC() + if err != nil { + t.Error("Error getting fec", err) + } + if fec !=0{ + t.Error("Wrong fec value") + } +} + +func TestEncoder_SetGetPacketLossPerc(t *testing.T) { + enc, err := NewEncoder(8000, 1, AppVoIP) + if err != nil || enc == nil { + t.Errorf("Error creating new encoder: %v", err) + } + vals := []int{0, 5, 10, 20} + for _, lossPerc := range vals { + err := enc.SetPacketLossPerc(lossPerc) + if err != nil { + t.Error("Error setting loss percentage value:", err) + } + lp, err := enc.PacketLossPerc() + if err != nil { + t.Error("Error getting loss percentage value", err) + } + if lp != lossPerc { + t.Errorf("Unexpected encoder loss percentage value. Got %d, but expected %d", + lp, lossPerc) + } + } +} -- 2.48.1