Skip to content

Commit

Permalink
support G711 multiple channels and custom sample rates
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jan 8, 2024
1 parent f9eb8e5 commit 3495448
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 50 deletions.
9 changes: 7 additions & 2 deletions client_play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3384,8 +3384,13 @@ func TestClientPlayBackChannel(t *testing.T) {
Body: mediasToSDP([]*description.Media{
testH264Media,
{
Type: description.MediaTypeAudio,
Formats: []format.Format{&format.G711{}},
Type: description.MediaTypeAudio,
Formats: []format.Format{&format.G711{
PayloadTyp: 8,
MULaw: false,
SampleRate: 8000,
ChannelCount: 1,
}},
IsBackChannel: true,
},
}),
Expand Down
9 changes: 7 additions & 2 deletions examples/client-record-format-g711/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ func main() {
// create a description that contains a G711 format
desc := &description.Session{
Medias: []*description.Media{{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.G711{}},
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.G711{
PayloadTyp: 8,
MULaw: false,
SampleRate: 8000,
ChannelCount: 1,
}},
}},
}

Expand Down
45 changes: 36 additions & 9 deletions pkg/description/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ var casesSession = []struct {
Type: MediaTypeAudio,
Control: "rtsp://10.0.100.50/profile5/media.smp/trackID=a",
Formats: []format.Format{&format.G711{
MULaw: true,
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
}},
},
{
Expand Down Expand Up @@ -140,7 +143,10 @@ var casesSession = []struct {
Type: MediaTypeAudio,
Control: "trackID=2",
Formats: []format.Format{&format.G711{
MULaw: true,
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
}},
},
{
Expand Down Expand Up @@ -324,10 +330,16 @@ var casesSession = []struct {
ClockRat: 8000,
},
&format.G711{
MULaw: true,
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
},
&format.G711{
MULaw: false,
PayloadTyp: 8,
MULaw: false,
SampleRate: 8000,
ChannelCount: 1,
},
&format.Generic{
PayloadTyp: 106,
Expand Down Expand Up @@ -516,13 +528,23 @@ var casesSession = []struct {
{
Type: MediaTypeAudio,
Control: "rtsp://192.168.0.1/audio",
Formats: []format.Format{&format.G711{MULaw: true}},
Formats: []format.Format{&format.G711{
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
}},
},
{
Type: MediaTypeAudio,
IsBackChannel: true,
Control: "rtsp://192.168.0.1/audioback",
Formats: []format.Format{&format.G711{MULaw: true}},
Formats: []format.Format{&format.G711{
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
}},
},
},
},
Expand Down Expand Up @@ -671,9 +693,14 @@ var casesSession = []struct {
},
Medias: []*Media{
{
ID: "1",
Type: MediaTypeAudio,
Formats: []format.Format{&format.G711{MULaw: true}},
ID: "1",
Type: MediaTypeAudio,
Formats: []format.Format{&format.G711{
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
}},
},
{
ID: "2",
Expand Down
3 changes: 3 additions & 0 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func Unmarshal(mediaType string, payloadType uint8, rtpMap string, fmtp map[stri
codec == "aal2-g726-40") && clock == "8000":
return &G726{}

case codec == "pcma", codec == "pcmu":
return &G711{}

case codec == "l8", codec == "l16", codec == "l24":
return &LPCM{}
}
Expand Down
54 changes: 46 additions & 8 deletions pkg/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,65 @@ var casesFormat = []struct {
encFmtp map[string]string
}{
{
"audio g711 pcma",
"audio g711 pcma static payload type",
"audio",
8,
"",
nil,
&G711{},
&G711{
PayloadTyp: 8,
MULaw: false,
SampleRate: 8000,
ChannelCount: 1,
},
"PCMA/8000",
nil,
},
{
"audio g711 pcmu",
"audio g711 pcmu static payload type",
"audio",
0,
"",
nil,
&G711{
MULaw: true,
PayloadTyp: 0,
MULaw: true,
SampleRate: 8000,
ChannelCount: 1,
},
"PCMU/8000",
nil,
},
{
"audio g711 pcma dynamic payload type",
"audio",
96,
"PCMA/16000/2",
nil,
&G711{
PayloadTyp: 96,
MULaw: false,
SampleRate: 16000,
ChannelCount: 2,
},
"PCMA/16000/2",
nil,
},
{
"audio g711 pcmu dynamic payload type",
"audio",
96,
"PCMU/16000/2",
nil,
&G711{
PayloadTyp: 96,
MULaw: true,
SampleRate: 16000,
ChannelCount: 2,
},
"PCMU/16000/2",
nil,
},
{
"audio g722",
"audio",
Expand Down Expand Up @@ -125,7 +163,7 @@ var casesFormat = []struct {
nil,
},
{
"audio lpcm 8",
"audio lpcm 8 dynamic payload type",
"audio",
97,
"L8/48000/2",
Expand All @@ -140,7 +178,7 @@ var casesFormat = []struct {
nil,
},
{
"audio lpcm 16",
"audio lpcm 16 dynamic payload type",
"audio",
97,
"L16/96000/2",
Expand All @@ -155,7 +193,7 @@ var casesFormat = []struct {
nil,
},
{
"audio lpcm 16 rfc3551 stereo",
"audio lpcm 16 static payload type",
"audio",
10,
"",
Expand All @@ -170,7 +208,7 @@ var casesFormat = []struct {
nil,
},
{
"audio lpcm 16 rfc3551 mono",
"audio lpcm 16 static payload type",
"audio",
11,
"",
Expand Down
83 changes: 68 additions & 15 deletions pkg/format/g711.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
package format

import (
"strconv"
"strings"

"github.com/pion/rtp"

"github.com/bluenviron/gortsplib/v4/pkg/format/rtpsimpleaudio"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtplpcm"
)

// G711 is the RTP format for the G711 codec, encoded with mu-law or A-law.
// Specification: https://datatracker.ietf.org/doc/html/rfc3551
type G711 struct {
// whether to use mu-law. Otherwise, A-law is used.
MULaw bool
PayloadTyp uint8
MULaw bool
SampleRate int
ChannelCount int
}

func (f *G711) unmarshal(ctx *unmarshalContext) error {
f.MULaw = (ctx.payloadType == 0)
f.PayloadTyp = ctx.payloadType

if ctx.payloadType == 0 {
f.MULaw = true
f.SampleRate = 8000
f.ChannelCount = 1
return nil
}

if ctx.payloadType == 8 {
f.MULaw = false
f.SampleRate = 8000
f.ChannelCount = 1
return nil
}

f.MULaw = (ctx.codec == "pcmu")

tmp := strings.SplitN(ctx.clock, "/", 2)

tmp1, err := strconv.ParseUint(tmp[0], 10, 31)
if err != nil {
return err
}

Check warning on line 45 in pkg/format/g711.go

View check run for this annotation

Codecov / codecov/patch

pkg/format/g711.go#L44-L45

Added lines #L44 - L45 were not covered by tests
f.SampleRate = int(tmp1)

if len(tmp) >= 2 {
tmp1, err := strconv.ParseUint(tmp[1], 10, 31)
if err != nil {
return err
}

Check warning on line 52 in pkg/format/g711.go

View check run for this annotation

Codecov / codecov/patch

pkg/format/g711.go#L51-L52

Added lines #L51 - L52 were not covered by tests
f.ChannelCount = int(tmp1)
} else {
f.ChannelCount = 1
}

Check warning on line 56 in pkg/format/g711.go

View check run for this annotation

Codecov / codecov/patch

pkg/format/g711.go#L54-L56

Added lines #L54 - L56 were not covered by tests

return nil
}

Expand All @@ -30,18 +70,26 @@ func (f *G711) ClockRate() int {

// PayloadType implements Format.
func (f *G711) PayloadType() uint8 {
if f.MULaw {
return 0
}
return 8
return f.PayloadTyp
}

// RTPMap implements Format.
func (f *G711) RTPMap() string {
ret := ""

if f.MULaw {
return "PCMU/8000"
ret += "PCMU"
} else {
ret += "PCMA"
}
return "PCMA/8000"

ret += "/" + strconv.FormatInt(int64(f.SampleRate), 10)

if f.ChannelCount != 1 {
ret += "/" + strconv.FormatInt(int64(f.ChannelCount), 10)
}

return ret
}

// FMTP implements Format.
Expand All @@ -55,8 +103,11 @@ func (f *G711) PTSEqualsDTS(*rtp.Packet) bool {
}

// CreateDecoder creates a decoder able to decode the content of the format.
func (f *G711) CreateDecoder() (*rtpsimpleaudio.Decoder, error) {
d := &rtpsimpleaudio.Decoder{}
func (f *G711) CreateDecoder() (*rtplpcm.Decoder, error) {
d := &rtplpcm.Decoder{
BitDepth: 8,
ChannelCount: f.ChannelCount,
}

err := d.Init()
if err != nil {
Expand All @@ -67,9 +118,11 @@ func (f *G711) CreateDecoder() (*rtpsimpleaudio.Decoder, error) {
}

// CreateEncoder creates an encoder able to encode the content of the format.
func (f *G711) CreateEncoder() (*rtpsimpleaudio.Encoder, error) {
e := &rtpsimpleaudio.Encoder{
PayloadType: f.PayloadType(),
func (f *G711) CreateEncoder() (*rtplpcm.Encoder, error) {
e := &rtplpcm.Encoder{
PayloadType: f.PayloadType(),
BitDepth: 8,
ChannelCount: f.ChannelCount,
}

err := e.Init()
Expand Down

0 comments on commit 3495448

Please sign in to comment.