mirror of
https://github.com/aler9/gortsplib
synced 2025-10-08 00:20:05 +08:00
move RTP decoders/encoders into pkt/rtpcodecs
This commit is contained in:
108
pkg/rtpcodecs/rtplpcm/decoder_test.go
Normal file
108
pkg/rtpcodecs/rtplpcm/decoder_test.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package rtplpcm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var cases = []struct {
|
||||
name string
|
||||
samples []byte
|
||||
pts time.Duration
|
||||
pkts []*rtp.Packet
|
||||
}{
|
||||
{
|
||||
"single",
|
||||
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
|
||||
25 * time.Millisecond,
|
||||
[]*rtp.Packet{
|
||||
{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: false,
|
||||
PayloadType: 96,
|
||||
SequenceNumber: 17645,
|
||||
Timestamp: 2289527557,
|
||||
SSRC: 0x9dbb7812,
|
||||
},
|
||||
Payload: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"splitted",
|
||||
bytes.Repeat([]byte{0x41, 0x42, 0x43}, 680),
|
||||
25 * time.Millisecond,
|
||||
[]*rtp.Packet{
|
||||
{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: false,
|
||||
PayloadType: 96,
|
||||
SequenceNumber: 17645,
|
||||
Timestamp: 2289527557,
|
||||
SSRC: 0x9dbb7812,
|
||||
},
|
||||
Payload: bytes.Repeat([]byte{0x41, 0x42, 0x43}, 486),
|
||||
},
|
||||
{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: false,
|
||||
PayloadType: 96,
|
||||
SequenceNumber: 17646,
|
||||
Timestamp: 2289527800,
|
||||
SSRC: 0x9dbb7812,
|
||||
},
|
||||
Payload: bytes.Repeat([]byte{0x41, 0x42, 0x43}, 194),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
for _, ca := range cases {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
d := &Decoder{
|
||||
BitDepth: 24,
|
||||
SampleRate: 48000,
|
||||
ChannelCount: 2,
|
||||
}
|
||||
d.Init()
|
||||
|
||||
// send an initial packet downstream
|
||||
// in order to compute the right timestamp,
|
||||
// that is relative to the initial packet
|
||||
pkt := rtp.Packet{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: false,
|
||||
PayloadType: 0,
|
||||
SequenceNumber: 17645,
|
||||
Timestamp: 2289526357,
|
||||
SSRC: 0x9dbb7812,
|
||||
},
|
||||
Payload: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
|
||||
}
|
||||
_, _, err := d.Decode(&pkt)
|
||||
require.NoError(t, err)
|
||||
|
||||
var samples []byte
|
||||
expPTS := ca.pts
|
||||
|
||||
for _, pkt := range ca.pkts {
|
||||
partial, pts, err := d.Decode(pkt)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expPTS, pts)
|
||||
samples = append(samples, partial...)
|
||||
expPTS += time.Duration(len(partial)/(24*2/8)) * time.Second / 48000
|
||||
}
|
||||
|
||||
require.Equal(t, ca.samples, samples)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user