add RTP encoder/decoder for G722, PCMA, PCMU

This commit is contained in:
aler9
2022-11-15 17:22:40 +01:00
parent f07fad893f
commit 8984685105
14 changed files with 311 additions and 53 deletions

View File

@@ -29,6 +29,7 @@ Features:
* Switch transport protocol automatically
* Pause without disconnecting from the server
* Generate RTCP sender reports
* Server
* Handle requests from clients
* Sessions and connections are independent
@@ -42,13 +43,23 @@ Features:
* Write TLS-encrypted streams
* Compute and provide SSRC, RTP-Info to clients
* Generate RTCP sender reports
* Utilities
* Encode and decode codec-specific frames to/from RTP packets. The following codecs are supported:
* Video
* H264
* VP8
* VP9
* Audio
* G722
* LPCM
* MPEG4-Audio (AAC)
* Opus
* PCMA
* PCMU
* Parse RTSP elements: requests, responses, SDP
* Parse H264 elements and formats: RTP/H264, Annex-B, AVCC, anti-competition, DTS
* Parse MPEG4-audio (AAC) elements and formats: RTP/MPEG4-audio, ADTS, MPEG4-audio configurations
* Parse Opus elements: RTP/Opus
* Parse VP8 elements: RTP/VP8
* Parse VP9 elements: RTP/VP9
* Parse H264 elements and formats: Annex-B, AVCC, anti-competition, DTS
* Parse MPEG4-audio (AAC) element and formats: ADTS, MPEG4-audio configurations
## Table of contents
@@ -60,11 +71,14 @@ Features:
* [client-query](examples/client-query/main.go)
* [client-read](examples/client-read/main.go)
* [client-read-codec-g722](examples/client-read-codec-g722/main.go)
* [client-read-codec-h264](examples/client-read-codec-h264/main.go)
* [client-read-codec-h264-convert-to-jpeg](examples/client-read-codec-h264-convert-to-jpeg/main.go)
* [client-read-codec-h264-save-to-disk](examples/client-read-codec-h264-save-to-disk/main.go)
* [client-read-codec-mpeg4audio](examples/client-read-codec-mpeg4audio/main.go)
* [client-read-codec-opus](examples/client-read-codec-opus/main.go)
* [client-read-codec-pcma](examples/client-read-codec-pcma/main.go)
* [client-read-codec-pcmu](examples/client-read-codec-pcmu/main.go)
* [client-read-codec-vp8](examples/client-read-codec-vp8/main.go)
* [client-read-codec-vp9](examples/client-read-codec-vp9/main.go)
* [client-read-partial](examples/client-read-partial/main.go)

View File

@@ -0,0 +1,73 @@
package main
import (
"log"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/url"
)
// This example shows how to
// 1. connect to a RTSP server and read all tracks on a path
// 2. check if there's a G722 track
// 3. get G722 frames of that track
func main() {
c := gortsplib.Client{}
// parse URL
u, err := url.Parse("rtsp://localhost:8554/mystream")
if err != nil {
panic(err)
}
// connect to the server
err = c.Start(u.Scheme, u.Host)
if err != nil {
panic(err)
}
defer c.Close()
// find published tracks
tracks, baseURL, _, err := c.Describe(u)
if err != nil {
panic(err)
}
// find the G722 track
track := func() *gortsplib.TrackG722 {
for _, track := range tracks {
if tt, ok := track.(*gortsplib.TrackG722); ok {
return tt
}
}
return nil
}()
if track == nil {
panic("G722 track not found")
}
// setup decoder
dec := track.CreateDecoder()
// called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
// decode an G722 packet from the RTP packet
op, _, err := dec.Decode(ctx.Packet)
if err != nil {
return
}
// print
log.Printf("received G722 frame of size %d\n", len(op))
}
// setup and read the G722 track only
err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil {
panic(err)
}
// wait until a fatal error
panic(c.Wait())
}

View File

@@ -0,0 +1,73 @@
package main
import (
"log"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/url"
)
// This example shows how to
// 1. connect to a RTSP server and read all tracks on a path
// 2. check if there's a PCMA track
// 3. get PCMA frames of that track
func main() {
c := gortsplib.Client{}
// parse URL
u, err := url.Parse("rtsp://localhost:8554/mystream")
if err != nil {
panic(err)
}
// connect to the server
err = c.Start(u.Scheme, u.Host)
if err != nil {
panic(err)
}
defer c.Close()
// find published tracks
tracks, baseURL, _, err := c.Describe(u)
if err != nil {
panic(err)
}
// find the PCMA track
track := func() *gortsplib.TrackPCMA {
for _, track := range tracks {
if tt, ok := track.(*gortsplib.TrackPCMA); ok {
return tt
}
}
return nil
}()
if track == nil {
panic("PCMA track not found")
}
// setup decoder
dec := track.CreateDecoder()
// called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
// decode an PCMA packet from the RTP packet
op, _, err := dec.Decode(ctx.Packet)
if err != nil {
return
}
// print
log.Printf("received PCMA frame of size %d\n", len(op))
}
// setup and read the PCMA track only
err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil {
panic(err)
}
// wait until a fatal error
panic(c.Wait())
}

View File

@@ -0,0 +1,73 @@
package main
import (
"log"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/url"
)
// This example shows how to
// 1. connect to a RTSP server and read all tracks on a path
// 2. check if there's a PCMU track
// 3. get PCMU frames of that track
func main() {
c := gortsplib.Client{}
// parse URL
u, err := url.Parse("rtsp://localhost:8554/mystream")
if err != nil {
panic(err)
}
// connect to the server
err = c.Start(u.Scheme, u.Host)
if err != nil {
panic(err)
}
defer c.Close()
// find published tracks
tracks, baseURL, _, err := c.Describe(u)
if err != nil {
panic(err)
}
// find the PCMU track
track := func() *gortsplib.TrackPCMU {
for _, track := range tracks {
if tt, ok := track.(*gortsplib.TrackPCMU); ok {
return tt
}
}
return nil
}()
if track == nil {
panic("PCMU track not found")
}
// setup decoder
dec := track.CreateDecoder()
// called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
// decode an PCMU packet from the RTP packet
op, _, err := dec.Decode(ctx.Packet)
if err != nil {
return
}
// print
log.Printf("received PCMU frame of size %d\n", len(op))
}
// setup and read the PCMU track only
err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil {
panic(err)
}
// wait until a fatal error
panic(c.Wait())
}

View File

@@ -1,2 +0,0 @@
// Package rtpopus contains a RTP/Opus decoder and encoder.
package rtpopus

View File

@@ -1,20 +1,17 @@
package rtpopus
package rtpsimpleaudio
import (
"time"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/aler9/gortsplib/pkg/rtptimedec"
)
// Decoder is a RTP/Opus decoder.
// Decoder is a RTP/simple audio decoder.
type Decoder struct {
// sample rate of input packets.
SampleRate int
cop codecs.OpusPacket
timeDecoder *rtptimedec.Decoder
}
@@ -23,9 +20,7 @@ func (d *Decoder) Init() {
d.timeDecoder = rtptimedec.New(d.SampleRate)
}
// Decode decodes a Opus packet from a RTP/Opus packet.
// It returns the Opus packet and its PTS.
// Decode decodes an audio frame from a RTP packet.
func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
_, err := d.cop.Unmarshal(pkt.Payload)
return d.cop.Payload, d.timeDecoder.Decode(pkt.Timestamp), err
return pkt.Payload, d.timeDecoder.Decode(pkt.Timestamp), nil
}

View File

@@ -1,4 +1,4 @@
package rtpopus
package rtpsimpleaudio
import (
"testing"
@@ -10,21 +10,21 @@ import (
var cases = []struct {
name string
op []byte
frame []byte
pts time.Duration
pkt *rtp.Packet
}{
{
"a",
"single",
[]byte{0x01, 0x02, 0x03, 0x04},
20 * time.Millisecond,
25 * time.Millisecond,
&rtp.Packet{
Header: rtp.Header{
Version: 2,
Marker: true,
PayloadType: 96,
Marker: false,
PayloadType: 0,
SequenceNumber: 17645,
Timestamp: 2289527317,
Timestamp: 2289526557,
SSRC: 0x9dbb7812,
},
Payload: []byte{0x01, 0x02, 0x03, 0x04},
@@ -36,7 +36,7 @@ func TestDecode(t *testing.T) {
for _, ca := range cases {
t.Run(ca.name, func(t *testing.T) {
d := &Decoder{
SampleRate: 48000,
SampleRate: 8000,
}
d.Init()
@@ -46,22 +46,22 @@ func TestDecode(t *testing.T) {
pkt := rtp.Packet{
Header: rtp.Header{
Version: 2,
Marker: true,
PayloadType: 96,
Marker: false,
PayloadType: 0,
SequenceNumber: 17645,
Timestamp: 2289526357,
SSRC: 0x9dbb7812,
},
Payload: []byte{0x00},
Payload: []byte{0x01, 0x02, 0x03, 0x04},
}
_, _, err := d.Decode(&pkt)
require.NoError(t, err)
op, pts, err := d.Decode(ca.pkt)
frame, pts, err := d.Decode(ca.pkt)
require.NoError(t, err)
require.Equal(t, ca.op, op)
require.Equal(t, ca.pts, pts)
require.Equal(t, ca.frame, frame)
})
}
}

View File

@@ -1,4 +1,4 @@
package rtpopus
package rtpsimpleaudio
import (
"crypto/rand"
@@ -6,7 +6,6 @@ import (
"time"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
)
const (
@@ -19,7 +18,7 @@ func randUint32() uint32 {
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
}
// Encoder is a RTP/Opus encoder.
// Encoder is a RTP/simple audio encoder.
type Encoder struct {
// payload type of packets.
PayloadType uint8
@@ -40,11 +39,9 @@ type Encoder struct {
// It defaults to 1460.
PayloadMaxSize int
// sample rate of packets.
SampleRate int
sequenceNumber uint16
op codecs.OpusPayloader
}
// Init initializes the encoder.
@@ -72,10 +69,10 @@ func (e *Encoder) encodeTimestamp(ts time.Duration) uint32 {
return *e.InitialTimestamp + uint32(ts.Seconds()*float64(e.SampleRate))
}
// Encode encodes an Opus packet into a RTP/Opus packet.
func (e *Encoder) Encode(op []byte, pts time.Duration) (*rtp.Packet, error) {
if len(op) > e.PayloadMaxSize {
return nil, fmt.Errorf("packet size exceeds maximum size")
// 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{
@@ -85,9 +82,9 @@ func (e *Encoder) Encode(op []byte, pts time.Duration) (*rtp.Packet, error) {
SequenceNumber: e.sequenceNumber,
Timestamp: e.encodeTimestamp(pts),
SSRC: *e.SSRC,
Marker: true,
Marker: false,
},
Payload: e.op.Payload(0, op)[0],
Payload: frame,
}
e.sequenceNumber++

View File

@@ -1,4 +1,4 @@
package rtpopus
package rtpsimpleaudio
import (
"testing"
@@ -10,8 +10,8 @@ func TestEncode(t *testing.T) {
for _, ca := range cases {
t.Run(ca.name, func(t *testing.T) {
e := &Encoder{
PayloadType: 96,
SampleRate: 48000,
PayloadType: 0,
SampleRate: 8000,
SSRC: func() *uint32 {
v := uint32(0x9dbb7812)
return &v
@@ -27,7 +27,7 @@ func TestEncode(t *testing.T) {
}
e.Init()
pkt, err := e.Encode(ca.op, ca.pts)
pkt, err := e.Encode(ca.frame, ca.pts)
require.NoError(t, err)
require.Equal(t, ca.pkt, pkt)
})
@@ -36,8 +36,8 @@ func TestEncode(t *testing.T) {
func TestEncodeRandomInitialState(t *testing.T) {
e := &Encoder{
PayloadType: 96,
SampleRate: 48000,
PayloadType: 0,
SampleRate: 8000,
}
e.Init()
require.NotEqual(t, nil, e.SSRC)

View File

@@ -0,0 +1,2 @@
// Package rtpsimpleaudio contains a RTP decoder and encoder for audio codecs that fit in a single packet.
package rtpsimpleaudio

View File

@@ -5,6 +5,8 @@ import (
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/aler9/gortsplib/pkg/rtpsimpleaudio"
)
// TrackG722 is a G722 track.
@@ -60,3 +62,12 @@ func (t *TrackG722) clone() Track {
trackBase: t.trackBase,
}
}
// CreateDecoder creates a decoder able to decode the content of the track.
func (t *TrackG722) CreateDecoder() *rtpsimpleaudio.Decoder {
d := &rtpsimpleaudio.Decoder{
SampleRate: 8000,
}
d.Init()
return d
}

View File

@@ -7,7 +7,7 @@ import (
psdp "github.com/pion/sdp/v3"
"github.com/aler9/gortsplib/pkg/rtpopus"
"github.com/aler9/gortsplib/pkg/rtpsimpleaudio"
)
// TrackOpus is a Opus track.
@@ -97,8 +97,8 @@ func (t *TrackOpus) clone() Track {
}
// CreateDecoder creates a decoder able to decode the content of the track.
func (t *TrackOpus) CreateDecoder() *rtpopus.Decoder {
d := &rtpopus.Decoder{
func (t *TrackOpus) CreateDecoder() *rtpsimpleaudio.Decoder {
d := &rtpsimpleaudio.Decoder{
SampleRate: t.SampleRate,
}
d.Init()

View File

@@ -5,6 +5,8 @@ import (
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/aler9/gortsplib/pkg/rtpsimpleaudio"
)
// TrackPCMA is a PCMA track.
@@ -60,3 +62,12 @@ func (t *TrackPCMA) clone() Track {
trackBase: t.trackBase,
}
}
// CreateDecoder creates a decoder able to decode the content of the track.
func (t *TrackPCMA) CreateDecoder() *rtpsimpleaudio.Decoder {
d := &rtpsimpleaudio.Decoder{
SampleRate: 8000,
}
d.Init()
return d
}

View File

@@ -5,6 +5,8 @@ import (
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/aler9/gortsplib/pkg/rtpsimpleaudio"
)
// TrackPCMU is a PCMU track.
@@ -60,3 +62,12 @@ func (t *TrackPCMU) clone() Track {
trackBase: t.trackBase,
}
}
// CreateDecoder creates a decoder able to decode the content of the track.
func (t *TrackPCMU) CreateDecoder() *rtpsimpleaudio.Decoder {
d := &rtpsimpleaudio.Decoder{
SampleRate: 8000,
}
d.Init()
return d
}