]> Sergey Matveev's repositories - go-opus.git/commitdiff
Added get last packet duration in decoder ctls
authorelinor <elinor.alp@gmail.com>
Mon, 26 Mar 2018 04:54:20 +0000 (07:54 +0300)
committerelinor <elinor.alp@gmail.com>
Mon, 26 Mar 2018 04:54:20 +0000 (07:54 +0300)
decoder.go
decoder_test.go

index a131a23ec9ba082cd6e8bc743a35f9979404a68d..99b8e61024d6f368b178eeff833e715b60f84347 100644 (file)
@@ -12,6 +12,12 @@ import (
 /*
 #cgo pkg-config: opus
 #include <opus.h>
+
+int
+bridge_decoder_get_last_packet_duration(OpusDecoder *st, opus_int32 *samples)
+{
+       return opus_decoder_ctl(st, OPUS_GET_LAST_PACKET_DURATION(samples));
+}
 */
 import "C"
 
@@ -105,3 +111,13 @@ func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error) {
        }
        return n, nil
 }
+
+// Gets the duration (in samples) of the last packet successfully decoded or concealed.
+func (dec *Decoder) LastPacketDuration() (int,error){
+       var samples C.opus_int32
+       res := C.bridge_decoder_get_last_packet_duration(dec.p, &samples)
+       if res != C.OPUS_OK {
+               return 0, Error(res)
+       }
+       return int(samples), nil
+}
index ff7ebfaa0b2214b67cccf9cea471b1a2190061fd..d1e39b41c71f68e161cb8c13a28bf833650a106f 100644 (file)
@@ -30,3 +30,39 @@ func TestDecoderUnitialized(t *testing.T) {
                t.Errorf("Expected \"unitialized decoder\" error: %v", err)
        }
 }
+
+func TestDecoder_GetLastPacketDuration(t *testing.T) {
+       const G4 = 391.995
+       const SAMPLE_RATE = 48000
+       const FRAME_SIZE_MS = 60
+       const FRAME_SIZE = SAMPLE_RATE * FRAME_SIZE_MS / 1000
+       pcm := make([]int16, FRAME_SIZE)
+       enc, err := NewEncoder(SAMPLE_RATE, 1, AppVoIP)
+       if err != nil || enc == nil {
+               t.Fatalf("Error creating new encoder: %v", err)
+       }
+       addSine(pcm, SAMPLE_RATE, G4)
+
+       data := make([]byte, 1000)
+       n, err := enc.Encode(pcm, data)
+       if err != nil {
+               t.Fatalf("Couldn't encode data: %v", err)
+       }
+       data = data[:n]
+
+       dec, err := NewDecoder(SAMPLE_RATE, 1)
+       if err != nil || dec == nil {
+               t.Fatalf("Error creating new decoder: %v", err)
+       }
+       n, err = dec.Decode(data, pcm)
+       if err != nil {
+               t.Fatalf("Couldn't decode data: %v", err)
+       }
+       samples, err := dec.LastPacketDuration()
+       if err!=nil{
+               t.Fatalf("Couldn't get last packet duration: %v",err)
+       }
+       if samples!=n{
+               t.Fatalf("Wrong duration length. Expected %d. Got %d", n, samples)
+       }
+}