mirror of
https://github.com/aler9/gortsplib
synced 2025-10-08 16:40:09 +08:00
move RTP decoders/encoders into pkt/rtpcodecs
This commit is contained in:
26
pkg/rtpcodecs/rtpsimpleaudio/decoder.go
Normal file
26
pkg/rtpcodecs/rtpsimpleaudio/decoder.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package rtpsimpleaudio
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
|
||||
"github.com/aler9/gortsplib/pkg/rtptimedec"
|
||||
)
|
||||
|
||||
// Decoder is a RTP/simple audio decoder.
|
||||
type Decoder struct {
|
||||
SampleRate int
|
||||
|
||||
timeDecoder *rtptimedec.Decoder
|
||||
}
|
||||
|
||||
// Init initializes the decoder.
|
||||
func (d *Decoder) Init() {
|
||||
d.timeDecoder = rtptimedec.New(d.SampleRate)
|
||||
}
|
||||
|
||||
// Decode decodes an audio frame from a RTP packet.
|
||||
func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
|
||||
return pkt.Payload, d.timeDecoder.Decode(pkt.Timestamp), nil
|
||||
}
|
67
pkg/rtpcodecs/rtpsimpleaudio/decoder_test.go
Normal file
67
pkg/rtpcodecs/rtpsimpleaudio/decoder_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package rtpsimpleaudio
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var cases = []struct {
|
||||
name string
|
||||
frame []byte
|
||||
pts time.Duration
|
||||
pkt *rtp.Packet
|
||||
}{
|
||||
{
|
||||
"single",
|
||||
[]byte{0x01, 0x02, 0x03, 0x04},
|
||||
25 * time.Millisecond,
|
||||
&rtp.Packet{
|
||||
Header: rtp.Header{
|
||||
Version: 2,
|
||||
Marker: false,
|
||||
PayloadType: 0,
|
||||
SequenceNumber: 17645,
|
||||
Timestamp: 2289526557,
|
||||
SSRC: 0x9dbb7812,
|
||||
},
|
||||
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
for _, ca := range cases {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
d := &Decoder{
|
||||
SampleRate: 8000,
|
||||
}
|
||||
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},
|
||||
}
|
||||
_, _, err := d.Decode(&pkt)
|
||||
require.NoError(t, err)
|
||||
|
||||
frame, pts, err := d.Decode(ca.pkt)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ca.pts, pts)
|
||||
|
||||
require.Equal(t, ca.frame, frame)
|
||||
})
|
||||
}
|
||||
}
|
93
pkg/rtpcodecs/rtpsimpleaudio/encoder.go
Normal file
93
pkg/rtpcodecs/rtpsimpleaudio/encoder.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package rtpsimpleaudio
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
)
|
||||
|
||||
const (
|
||||
rtpVersion = 2
|
||||
)
|
||||
|
||||
func randUint32() uint32 {
|
||||
var b [4]byte
|
||||
rand.Read(b[:])
|
||||
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
|
||||
}
|
||||
|
||||
// Encoder is a RTP/simple audio encoder.
|
||||
type Encoder struct {
|
||||
// payload type of packets.
|
||||
PayloadType uint8
|
||||
|
||||
// SSRC of packets (optional).
|
||||
// It defaults to a random value.
|
||||
SSRC *uint32
|
||||
|
||||
// initial sequence number of packets (optional).
|
||||
// It defaults to a random value.
|
||||
InitialSequenceNumber *uint16
|
||||
|
||||
// initial timestamp of packets (optional).
|
||||
// It defaults to a random value.
|
||||
InitialTimestamp *uint32
|
||||
|
||||
// maximum size of packet payloads (optional).
|
||||
// It defaults to 1460.
|
||||
PayloadMaxSize int
|
||||
|
||||
SampleRate int
|
||||
|
||||
sequenceNumber uint16
|
||||
}
|
||||
|
||||
// Init initializes the encoder.
|
||||
func (e *Encoder) Init() {
|
||||
if e.SSRC == nil {
|
||||
v := randUint32()
|
||||
e.SSRC = &v
|
||||
}
|
||||
if e.InitialSequenceNumber == nil {
|
||||
v := uint16(randUint32())
|
||||
e.InitialSequenceNumber = &v
|
||||
}
|
||||
if e.InitialTimestamp == nil {
|
||||
v := randUint32()
|
||||
e.InitialTimestamp = &v
|
||||
}
|
||||
if e.PayloadMaxSize == 0 {
|
||||
e.PayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
|
||||
}
|
||||
|
||||
e.sequenceNumber = *e.InitialSequenceNumber
|
||||
}
|
||||
|
||||
func (e *Encoder) encodeTimestamp(ts time.Duration) uint32 {
|
||||
return *e.InitialTimestamp + uint32(ts.Seconds()*float64(e.SampleRate))
|
||||
}
|
||||
|
||||
// Encode encodes an audio frame into a RTP packet.
|
||||
func (e *Encoder) Encode(frame []byte, pts time.Duration) (*rtp.Packet, error) {
|
||||
if len(frame) > e.PayloadMaxSize {
|
||||
return nil, fmt.Errorf("frame is too big")
|
||||
}
|
||||
|
||||
pkt := &rtp.Packet{
|
||||
Header: rtp.Header{
|
||||
Version: rtpVersion,
|
||||
PayloadType: e.PayloadType,
|
||||
SequenceNumber: e.sequenceNumber,
|
||||
Timestamp: e.encodeTimestamp(pts),
|
||||
SSRC: *e.SSRC,
|
||||
Marker: false,
|
||||
},
|
||||
Payload: frame,
|
||||
}
|
||||
|
||||
e.sequenceNumber++
|
||||
|
||||
return pkt, nil
|
||||
}
|
46
pkg/rtpcodecs/rtpsimpleaudio/encoder_test.go
Normal file
46
pkg/rtpcodecs/rtpsimpleaudio/encoder_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package rtpsimpleaudio
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
for _, ca := range cases {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
e := &Encoder{
|
||||
PayloadType: 0,
|
||||
SampleRate: 8000,
|
||||
SSRC: func() *uint32 {
|
||||
v := uint32(0x9dbb7812)
|
||||
return &v
|
||||
}(),
|
||||
InitialSequenceNumber: func() *uint16 {
|
||||
v := uint16(0x44ed)
|
||||
return &v
|
||||
}(),
|
||||
InitialTimestamp: func() *uint32 {
|
||||
v := uint32(0x88776655)
|
||||
return &v
|
||||
}(),
|
||||
}
|
||||
e.Init()
|
||||
|
||||
pkt, err := e.Encode(ca.frame, ca.pts)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ca.pkt, pkt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeRandomInitialState(t *testing.T) {
|
||||
e := &Encoder{
|
||||
PayloadType: 0,
|
||||
SampleRate: 8000,
|
||||
}
|
||||
e.Init()
|
||||
require.NotEqual(t, nil, e.SSRC)
|
||||
require.NotEqual(t, nil, e.InitialSequenceNumber)
|
||||
require.NotEqual(t, nil, e.InitialTimestamp)
|
||||
}
|
2
pkg/rtpcodecs/rtpsimpleaudio/rtpsimpleaudio.go
Normal file
2
pkg/rtpcodecs/rtpsimpleaudio/rtpsimpleaudio.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package rtpsimpleaudio contains a RTP decoder and encoder for audio codecs that fit in a single packet.
|
||||
package rtpsimpleaudio
|
Reference in New Issue
Block a user