]> Sergey Matveev's repositories - go-opus.git/commitdiff
Setter and getter API for DTX on encoders
authorDejian Xu <xudejian2008@gmail.com>
Mon, 29 Aug 2016 12:53:18 +0000 (20:53 +0800)
committerHraban Luyat <hraban@0brg.net>
Mon, 29 Aug 2016 13:03:01 +0000 (14:03 +0100)
Go-style DTX setter and getter api on the encoder.

PR hraban/opus#4

encoder.go
encoder_test.go [new file with mode: 0644]

index ce27a06c9d2f59f746db28b6cdbe820485f5abe2..6b79c26640f01c7fb3a72405f95fd452076e71c3 100644 (file)
@@ -12,6 +12,16 @@ import (
 /*
 #cgo pkg-config: opus
 #include <opus/opus.h>
+
+int bridge_set_dtx(OpusEncoder *st, int use_dtx) {
+       return opus_encoder_ctl(st, OPUS_SET_DTX(use_dtx));
+}
+
+int bridge_get_dtx(OpusEncoder *st) {
+       int dtx = 0;
+       opus_encoder_ctl(st, OPUS_GET_DTX(&dtx));
+       return dtx;
+}
 */
 import "C"
 
@@ -107,3 +117,17 @@ func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error) {
        }
        return n, nil
 }
+
+// Configures the encoder's use of discontinuous transmission (DTX).
+func (enc *Encoder) UseDTX(use bool) {
+       dtx := 0
+       if use {
+               dtx = 1
+       }
+       C.bridge_set_dtx(enc.p, C.int(dtx))
+}
+
+func (enc *Encoder) GetDTX() bool {
+       dtx := C.bridge_get_dtx(enc.p)
+       return dtx != 0
+}
diff --git a/encoder_test.go b/encoder_test.go
new file mode 100644 (file)
index 0000000..015a714
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright © 2015, 2016 Hraban Luyat <hraban@0brg.net>
+//
+// License for use of this code is detailed in the LICENSE file
+
+package opus
+
+import "testing"
+
+func TestUseDTX(t *testing.T) {
+       enc, err := NewEncoder(8000, 1, APPLICATION_VOIP)
+       if err != nil || enc == nil {
+               t.Errorf("Error creating new encoder: %v", err)
+       }
+       vals := []bool{true, false}
+       for _, dtx := range vals {
+               enc.UseDTX(dtx)
+               gotv := enc.GetDTX()
+               if gotv != dtx {
+                       t.Errorf("Error set dtx: expect dtx=%v, got dtx=%v", dtx, gotv)
+               }
+       }
+}