]> Sergey Matveev's repositories - go-opus.git/commitdiff
BREAKING: Return error vals in encoder CTL API
authorHraban Luyat <hraban@0brg.net>
Fri, 30 Dec 2016 22:49:17 +0000 (22:49 +0000)
committerHraban Luyat <hraban@0brg.net>
Fri, 30 Dec 2016 22:49:17 +0000 (22:49 +0000)
Wasn't right to ignore these

encoder.go
encoder_test.go

index 3ad8330510edf9bb527beeab9661770b10804e71..cf41dbf4cbf30af29fa7541f4383616a988ab249 100644 (file)
@@ -13,26 +13,22 @@ import (
 #cgo pkg-config: opus
 #include <opus/opus.h>
 
-void
+int
 bridge_encoder_set_dtx(OpusEncoder *st, opus_int32 use_dtx)
 {
-       opus_encoder_ctl(st, OPUS_SET_DTX(use_dtx));
+       return opus_encoder_ctl(st, OPUS_SET_DTX(use_dtx));
 }
 
-opus_int32
-bridge_encoder_get_dtx(OpusEncoder *st)
+int
+bridge_encoder_get_dtx(OpusEncoder *st, opus_int32 *dtx)
 {
-       opus_int32 dtx = 0;
-       opus_encoder_ctl(st, OPUS_GET_DTX(&dtx));
-       return dtx;
+       return opus_encoder_ctl(st, OPUS_GET_DTX(dtx));
 }
 
-opus_int32
-bridge_encoder_get_sample_rate(OpusEncoder *st)
+int
+bridge_encoder_get_sample_rate(OpusEncoder *st, opus_int32 *sample_rate)
 {
-       opus_int32 sample_rate = 0;
-       int ret = opus_encoder_ctl(st, OPUS_GET_SAMPLE_RATE(&sample_rate));
-       return sample_rate;
+       return opus_encoder_ctl(st, OPUS_GET_SAMPLE_RATE(sample_rate));
 }
 
 
@@ -206,24 +202,37 @@ func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error) {
 }
 
 // UseDTX configures the encoder's use of discontinuous transmission (DTX).
-func (enc *Encoder) UseDTX(use bool) {
+func (enc *Encoder) UseDTX(use bool) error {
        dtx := 0
        if use {
                dtx = 1
        }
-       C.bridge_encoder_set_dtx(enc.p, C.opus_int32(dtx))
+       res := C.bridge_encoder_set_dtx(enc.p, C.opus_int32(dtx))
+       if res != C.OPUS_OK {
+               return Error(res)
+       }
+       return nil
 }
 
 // DTX reports whether this encoder is configured to use discontinuous
 // transmission (DTX).
-func (enc *Encoder) DTX() bool {
-       dtx := C.bridge_encoder_get_dtx(enc.p)
-       return dtx != 0
+func (enc *Encoder) DTX() (bool, error) {
+       var dtx C.opus_int32
+       res := C.bridge_encoder_get_dtx(enc.p, &dtx)
+       if res != C.OPUS_OK {
+               return false, Error(res)
+       }
+       return dtx != 0, nil
 }
 
 // SampleRate returns the encoder sample rate in Hz.
-func (enc *Encoder) SampleRate() int {
-       return int(C.bridge_encoder_get_sample_rate(enc.p))
+func (enc *Encoder) SampleRate() (int, error) {
+       var sr C.opus_int32
+       res := C.bridge_encoder_get_sample_rate(enc.p, &sr)
+       if res != C.OPUS_OK {
+               return 0, Error(res)
+       }
+       return int(sr), nil
 }
 
 // SetBitrate sets the bitrate of the Encoder
index 88da66ccdc228a4b7d35e5367ab03fe4888882df..d6e6d59322897ceb0490c9ea0952afb4496a77b5 100644 (file)
@@ -36,8 +36,14 @@ func TestEncoderDTX(t *testing.T) {
        }
        vals := []bool{true, false}
        for _, dtx := range vals {
-               enc.UseDTX(dtx)
-               gotv := enc.DTX()
+               err := enc.UseDTX(dtx)
+               if err != nil {
+                       t.Fatalf("Error setting DTX to %t: %v", dtx, err)
+               }
+               gotv, err := enc.DTX()
+               if err != nil {
+                       t.Fatalf("Error getting DTX (%t): %v", dtx, err)
+               }
                if gotv != dtx {
                        t.Errorf("Error set dtx: expect dtx=%v, got dtx=%v", dtx, gotv)
                }
@@ -51,7 +57,10 @@ func TestEncoderSampleRate(t *testing.T) {
                if err != nil || enc == nil {
                        t.Fatalf("Error creating new encoder with sample_rate %d Hz: %v", f, err)
                }
-               f2 := enc.SampleRate()
+               f2, err := enc.SampleRate()
+               if err != nil {
+                       t.Fatalf("Error getting sample rate (%d Hz): %v", f, err)
+               }
                if f != f2 {
                        t.Errorf("Unexpected sample rate reported by %d Hz encoder: %d", f, f2)
                }