mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 23:02:45 +08:00
merge MPEG4AudioGeneric and MPEG4AudioLATM (#430)
This commit is contained in:
53
README.md
53
README.md
@@ -97,42 +97,41 @@ Features:
|
|||||||
|
|
||||||
## RTP Payload Formats
|
## RTP Payload Formats
|
||||||
|
|
||||||
In RTSP, media streams are routed between server and clients by using RTP packets. Conversion between RTP packets and codec-specific frames happens by using a payload format. This library recognizes the following payload formats:
|
In RTSP, media streams are routed between server and clients by using RTP packets, which are encoded in a specific, codec-dependent, format, that is declared during the handshake. This library supports the following formats:
|
||||||
|
|
||||||
### Video
|
### Video
|
||||||
|
|
||||||
|format / codec|variant|documentation|encoder and decoder available|
|
|format|documentation|encoder and decoder available|
|
||||||
|--------------|-------|-------------|-----------------------------|
|
|------|-------------|-----------------------------|
|
||||||
|AV1||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#AV1)|:heavy_check_mark:|
|
|AV1|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#AV1)|:heavy_check_mark:|
|
||||||
|VP9||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#VP9)|:heavy_check_mark:|
|
|VP9|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#VP9)|:heavy_check_mark:|
|
||||||
|VP8||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#VP8)|:heavy_check_mark:|
|
|VP8|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#VP8)|:heavy_check_mark:|
|
||||||
|H265||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#H265)|:heavy_check_mark:|
|
|H265|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#H265)|:heavy_check_mark:|
|
||||||
|H264||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#H264)|:heavy_check_mark:|
|
|H264|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#H264)|:heavy_check_mark:|
|
||||||
|MPEG-4 Video (H263, Xvid)||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG4VideoES)|:heavy_check_mark:|
|
|MPEG-4 Video (H263, Xvid)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG4Video)|:heavy_check_mark:|
|
||||||
|MPEG-1/2 Video||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG1Video)|:heavy_check_mark:|
|
|MPEG-1/2 Video|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG1Video)|:heavy_check_mark:|
|
||||||
|M-JPEG||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MJPEG)|:heavy_check_mark:|
|
|M-JPEG|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MJPEG)|:heavy_check_mark:|
|
||||||
|
|
||||||
### Audio
|
### Audio
|
||||||
|
|
||||||
|format / codec|variant|documentation|encoder and decoder available|
|
|format|documentation|encoder and decoder available|
|
||||||
|--------------|-------|-------------|-----------------------------|
|
|------|-------------|-----------------------------|
|
||||||
|Opus||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Opus)|:heavy_check_mark:|
|
|Opus|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Opus)|:heavy_check_mark:|
|
||||||
|Vorbis||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Vorbis)||
|
|Vorbis|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Vorbis)||
|
||||||
|MPEG-4 Audio (AAC)|Generic (RFC3640)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG4AudioGeneric)|:heavy_check_mark:|
|
|MPEG-4 Audio (AAC)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG4Audio)|:heavy_check_mark:|
|
||||||
|MPEG-4 Audio (AAC)|LATM (RFC6416)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG4AudioLATM)|:heavy_check_mark:|
|
|MPEG-1/2 Audio (MP3)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG1Audio)|:heavy_check_mark:|
|
||||||
|MPEG-1/2 Audio (MP3)||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEG1Audio)|:heavy_check_mark:|
|
|AC-3|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#AC3)|:heavy_check_mark:|
|
||||||
|AC-3||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#AC3)|:heavy_check_mark:|
|
|Speex|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Speex)||
|
||||||
|Speex||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#Speex)||
|
|G726|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G726)||
|
||||||
|G726||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G726)||
|
|G722|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G722)|:heavy_check_mark:|
|
||||||
|G722||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G722)|:heavy_check_mark:|
|
|G711 (PCMA, PCMU)|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G711)|:heavy_check_mark:|
|
||||||
|G711 (PCMA, PCMU)||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#G711)|:heavy_check_mark:|
|
|LPCM|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#LPCM)|:heavy_check_mark:|
|
||||||
|LPCM||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#LPCM)|:heavy_check_mark:|
|
|
||||||
|
|
||||||
### Other
|
### Other
|
||||||
|
|
||||||
|format / codec|variant|documentation|encoder and decoder available|
|
|format|documentation|encoder and decoder available|
|
||||||
|--------------|-------|-------------|-----------------------------|
|
|------|-------------|-----------------------------|
|
||||||
|MPEG-TS||[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEGTS)||
|
|MPEG-TS|[link](https://pkg.go.dev/github.com/bluenviron/gortsplib/v4/pkg/format#MPEGTS)||
|
||||||
|
|
||||||
## Specifications
|
## Specifications
|
||||||
|
|
||||||
|
@@ -92,11 +92,8 @@ func Unmarshal(mediaType string, payloadType uint8, rtpMap string, fmtp map[stri
|
|||||||
case codec == "vorbis":
|
case codec == "vorbis":
|
||||||
return &Vorbis{}
|
return &Vorbis{}
|
||||||
|
|
||||||
case codec == "mpeg4-generic":
|
case codec == "mpeg4-generic", codec == "mp4a-latm":
|
||||||
return &MPEG4AudioGeneric{}
|
return &MPEG4Audio{}
|
||||||
|
|
||||||
case codec == "mp4a-latm":
|
|
||||||
return &MPEG4AudioLATM{}
|
|
||||||
|
|
||||||
case payloadType == 14:
|
case payloadType == 14:
|
||||||
return &MPEG1Audio{}
|
return &MPEG1Audio{}
|
||||||
|
@@ -343,12 +343,13 @@ var casesFormat = []struct {
|
|||||||
"object": "2",
|
"object": "2",
|
||||||
"config": "400026203fc0",
|
"config": "400026203fc0",
|
||||||
},
|
},
|
||||||
&MPEG4AudioLATM{
|
&MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
PayloadTyp: 96,
|
PayloadTyp: 96,
|
||||||
ProfileLevelID: 1,
|
ProfileLevelID: 1,
|
||||||
Bitrate: intPtr(64000),
|
Bitrate: intPtr(64000),
|
||||||
CPresent: false,
|
CPresent: false,
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
@@ -382,12 +383,13 @@ var casesFormat = []struct {
|
|||||||
"config": "400026103fc0",
|
"config": "400026103fc0",
|
||||||
"sbr-enabled": "1",
|
"sbr-enabled": "1",
|
||||||
},
|
},
|
||||||
&MPEG4AudioLATM{
|
&MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
PayloadTyp: 110,
|
PayloadTyp: 110,
|
||||||
ProfileLevelID: 15,
|
ProfileLevelID: 15,
|
||||||
CPresent: false,
|
CPresent: false,
|
||||||
SBREnabled: boolPtr(true),
|
SBREnabled: boolPtr(true),
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
@@ -421,13 +423,14 @@ var casesFormat = []struct {
|
|||||||
"config": "40005623101fe0",
|
"config": "40005623101fe0",
|
||||||
"sbr-enabled": "1",
|
"sbr-enabled": "1",
|
||||||
},
|
},
|
||||||
&MPEG4AudioLATM{
|
&MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
PayloadTyp: 110,
|
PayloadTyp: 110,
|
||||||
ProfileLevelID: 44,
|
ProfileLevelID: 44,
|
||||||
CPresent: false,
|
CPresent: false,
|
||||||
SBREnabled: boolPtr(true),
|
SBREnabled: boolPtr(true),
|
||||||
Bitrate: intPtr(64000),
|
Bitrate: intPtr(64000),
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
@@ -463,12 +466,13 @@ var casesFormat = []struct {
|
|||||||
"cpresent": "0",
|
"cpresent": "0",
|
||||||
"config": "4001d613101fe0",
|
"config": "4001d613101fe0",
|
||||||
},
|
},
|
||||||
&MPEG4AudioLATM{
|
&MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
PayloadTyp: 110,
|
PayloadTyp: 110,
|
||||||
ProfileLevelID: 48,
|
ProfileLevelID: 48,
|
||||||
Bitrate: intPtr(64000),
|
Bitrate: intPtr(64000),
|
||||||
CPresent: false,
|
CPresent: false,
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
@@ -501,11 +505,12 @@ var casesFormat = []struct {
|
|||||||
"cpresent": "0",
|
"cpresent": "0",
|
||||||
"config": "40002310",
|
"config": "40002310",
|
||||||
},
|
},
|
||||||
&MPEG4AudioLATM{
|
&MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
PayloadTyp: 110,
|
PayloadTyp: 110,
|
||||||
ProfileLevelID: 30,
|
ProfileLevelID: 30,
|
||||||
CPresent: false,
|
CPresent: false,
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
@@ -1141,7 +1146,7 @@ func FuzzUnmarshalLPCM(f *testing.F) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func FuzzUnmarshalMPEG4VideoES(f *testing.F) {
|
func FuzzUnmarshalMPEG4Video(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, a, b string) {
|
f.Fuzz(func(t *testing.T, a, b string) {
|
||||||
Unmarshal("video", 96, "MP4V-ES/90000", map[string]string{ //nolint:errcheck
|
Unmarshal("video", 96, "MP4V-ES/90000", map[string]string{ //nolint:errcheck
|
||||||
"profile-level-id": a,
|
"profile-level-id": a,
|
||||||
|
332
pkg/format/mpeg4_audio.go
Normal file
332
pkg/format/mpeg4_audio.go
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
package format
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
||||||
|
"github.com/pion/rtp"
|
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MPEG4Audio is a RTP format for a MPEG-4 Audio codec.
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
||||||
|
type MPEG4Audio struct {
|
||||||
|
// payload type of packets.
|
||||||
|
PayloadTyp uint8
|
||||||
|
|
||||||
|
// use RFC6416 (LATM) instead of RFC3640 (generic).
|
||||||
|
LATM bool
|
||||||
|
|
||||||
|
// profile level ID.
|
||||||
|
ProfileLevelID int
|
||||||
|
|
||||||
|
// generic only
|
||||||
|
Config *mpeg4audio.Config
|
||||||
|
SizeLength int
|
||||||
|
IndexLength int
|
||||||
|
IndexDeltaLength int
|
||||||
|
|
||||||
|
// LATM only
|
||||||
|
Bitrate *int
|
||||||
|
CPresent bool
|
||||||
|
StreamMuxConfig *mpeg4audio.StreamMuxConfig
|
||||||
|
SBREnabled *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *MPEG4Audio) unmarshal(ctx *unmarshalContext) error {
|
||||||
|
f.PayloadTyp = ctx.payloadType
|
||||||
|
f.LATM = (ctx.codec != "mpeg4-generic")
|
||||||
|
|
||||||
|
if !f.LATM {
|
||||||
|
for key, val := range ctx.fmtp {
|
||||||
|
switch key {
|
||||||
|
case "streamtype":
|
||||||
|
if val != "5" { // AudioStream in ISO 14496-1
|
||||||
|
return fmt.Errorf("streamtype of AAC must be 5")
|
||||||
|
}
|
||||||
|
|
||||||
|
case "mode":
|
||||||
|
if strings.ToLower(val) != "aac-hbr" {
|
||||||
|
return fmt.Errorf("unsupported AAC mode: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "profile-level-id":
|
||||||
|
tmp, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid profile-level-id: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.ProfileLevelID = int(tmp)
|
||||||
|
|
||||||
|
case "config":
|
||||||
|
enc, err := hex.DecodeString(val)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid AAC config: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Config = &mpeg4audio.Config{}
|
||||||
|
err = f.Config.Unmarshal(enc)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid AAC config: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "sizelength":
|
||||||
|
n, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil || n > 100 {
|
||||||
|
return fmt.Errorf("invalid AAC SizeLength: %v", val)
|
||||||
|
}
|
||||||
|
f.SizeLength = int(n)
|
||||||
|
|
||||||
|
case "indexlength":
|
||||||
|
n, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil || n > 100 {
|
||||||
|
return fmt.Errorf("invalid AAC IndexLength: %v", val)
|
||||||
|
}
|
||||||
|
f.IndexLength = int(n)
|
||||||
|
|
||||||
|
case "indexdeltalength":
|
||||||
|
n, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil || n > 100 {
|
||||||
|
return fmt.Errorf("invalid AAC IndexDeltaLength: %v", val)
|
||||||
|
}
|
||||||
|
f.IndexDeltaLength = int(n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Config == nil {
|
||||||
|
return fmt.Errorf("config is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.SizeLength == 0 {
|
||||||
|
return fmt.Errorf("sizelength is missing")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// default value set by specification
|
||||||
|
f.ProfileLevelID = 30
|
||||||
|
f.CPresent = true
|
||||||
|
|
||||||
|
for key, val := range ctx.fmtp {
|
||||||
|
switch key {
|
||||||
|
case "profile-level-id":
|
||||||
|
tmp, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid profile-level-id: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.ProfileLevelID = int(tmp)
|
||||||
|
|
||||||
|
case "bitrate":
|
||||||
|
tmp, err := strconv.ParseUint(val, 10, 31)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid bitrate: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
v := int(tmp)
|
||||||
|
f.Bitrate = &v
|
||||||
|
|
||||||
|
case "cpresent":
|
||||||
|
f.CPresent = (val == "1")
|
||||||
|
|
||||||
|
case "config":
|
||||||
|
enc, err := hex.DecodeString(val)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid AAC config: %v", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.StreamMuxConfig = &mpeg4audio.StreamMuxConfig{}
|
||||||
|
err = f.StreamMuxConfig.Unmarshal(enc)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid AAC config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "sbr-enabled":
|
||||||
|
v := (val == "1")
|
||||||
|
f.SBREnabled = &v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.CPresent {
|
||||||
|
if f.StreamMuxConfig != nil {
|
||||||
|
return fmt.Errorf("config and cpresent can't be used at the same time")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if f.StreamMuxConfig == nil {
|
||||||
|
return fmt.Errorf("config is missing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Codec implements Format.
|
||||||
|
func (f *MPEG4Audio) Codec() string {
|
||||||
|
return "MPEG-4 Audio"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClockRate implements Format.
|
||||||
|
func (f *MPEG4Audio) ClockRate() int {
|
||||||
|
if !f.LATM {
|
||||||
|
return f.Config.SampleRate
|
||||||
|
}
|
||||||
|
return f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig.SampleRate
|
||||||
|
}
|
||||||
|
|
||||||
|
// PayloadType implements Format.
|
||||||
|
func (f *MPEG4Audio) PayloadType() uint8 {
|
||||||
|
return f.PayloadTyp
|
||||||
|
}
|
||||||
|
|
||||||
|
// RTPMap implements Format.
|
||||||
|
func (f *MPEG4Audio) RTPMap() string {
|
||||||
|
if !f.LATM {
|
||||||
|
sampleRate := f.Config.SampleRate
|
||||||
|
if f.Config.ExtensionSampleRate != 0 {
|
||||||
|
sampleRate = f.Config.ExtensionSampleRate
|
||||||
|
}
|
||||||
|
|
||||||
|
channelCount := f.Config.ChannelCount
|
||||||
|
if f.Config.ExtensionType == mpeg4audio.ObjectTypePS {
|
||||||
|
channelCount = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return "mpeg4-generic/" + strconv.FormatInt(int64(sampleRate), 10) +
|
||||||
|
"/" + strconv.FormatInt(int64(channelCount), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
aoc := f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig
|
||||||
|
|
||||||
|
sampleRate := aoc.SampleRate
|
||||||
|
if aoc.ExtensionSampleRate != 0 {
|
||||||
|
sampleRate = aoc.ExtensionSampleRate
|
||||||
|
}
|
||||||
|
|
||||||
|
channelCount := aoc.ChannelCount
|
||||||
|
if aoc.ExtensionType == mpeg4audio.ObjectTypePS {
|
||||||
|
channelCount = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return "MP4A-LATM/" + strconv.FormatInt(int64(sampleRate), 10) +
|
||||||
|
"/" + strconv.FormatInt(int64(channelCount), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FMTP implements Format.
|
||||||
|
func (f *MPEG4Audio) FMTP() map[string]string {
|
||||||
|
if !f.LATM {
|
||||||
|
enc, err := f.Config.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
profileLevelID := f.ProfileLevelID
|
||||||
|
if profileLevelID == 0 { // support legacy definition which didn't include profile-level-id
|
||||||
|
profileLevelID = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmtp := map[string]string{
|
||||||
|
"streamtype": "5",
|
||||||
|
"mode": "AAC-hbr",
|
||||||
|
"profile-level-id": strconv.FormatInt(int64(profileLevelID), 10),
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.SizeLength > 0 {
|
||||||
|
fmtp["sizelength"] = strconv.FormatInt(int64(f.SizeLength), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.IndexLength > 0 {
|
||||||
|
fmtp["indexlength"] = strconv.FormatInt(int64(f.IndexLength), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.IndexDeltaLength > 0 {
|
||||||
|
fmtp["indexdeltalength"] = strconv.FormatInt(int64(f.IndexDeltaLength), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmtp["config"] = hex.EncodeToString(enc)
|
||||||
|
|
||||||
|
return fmtp
|
||||||
|
}
|
||||||
|
|
||||||
|
enc, err := f.StreamMuxConfig.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmtp := map[string]string{
|
||||||
|
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
|
||||||
|
"config": hex.EncodeToString(enc),
|
||||||
|
"object": strconv.FormatInt(int64(f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig.Type), 10),
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Bitrate != nil {
|
||||||
|
fmtp["bitrate"] = strconv.FormatInt(int64(*f.Bitrate), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.CPresent {
|
||||||
|
fmtp["cpresent"] = "1"
|
||||||
|
} else {
|
||||||
|
fmtp["cpresent"] = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.SBREnabled != nil {
|
||||||
|
if *f.SBREnabled {
|
||||||
|
fmtp["SBR-enabled"] = "1"
|
||||||
|
} else {
|
||||||
|
fmtp["SBR-enabled"] = "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmtp
|
||||||
|
}
|
||||||
|
|
||||||
|
// PTSEqualsDTS implements Format.
|
||||||
|
func (f *MPEG4Audio) PTSEqualsDTS(*rtp.Packet) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDecoder creates a decoder able to decode the content of the format.
|
||||||
|
func (f *MPEG4Audio) CreateDecoder() (*rtpmpeg4audio.Decoder, error) {
|
||||||
|
d := &rtpmpeg4audio.Decoder{
|
||||||
|
LATM: f.LATM,
|
||||||
|
SizeLength: f.SizeLength,
|
||||||
|
IndexLength: f.IndexLength,
|
||||||
|
IndexDeltaLength: f.IndexDeltaLength,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := d.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateEncoder creates an encoder able to encode the content of the format.
|
||||||
|
func (f *MPEG4Audio) CreateEncoder() (*rtpmpeg4audio.Encoder, error) {
|
||||||
|
e := &rtpmpeg4audio.Encoder{
|
||||||
|
LATM: f.LATM,
|
||||||
|
PayloadType: f.PayloadTyp,
|
||||||
|
SizeLength: f.SizeLength,
|
||||||
|
IndexLength: f.IndexLength,
|
||||||
|
IndexDeltaLength: f.IndexDeltaLength,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := e.Init()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConfig returns the MPEG-4 Audio configuration.
|
||||||
|
func (f *MPEG4Audio) GetConfig() *mpeg4audio.Config {
|
||||||
|
if !f.LATM {
|
||||||
|
return f.Config
|
||||||
|
}
|
||||||
|
return f.StreamMuxConfig.Programs[0].Layers[0].AudioSpecificConfig
|
||||||
|
}
|
@@ -1,200 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
|
||||||
"github.com/pion/rtp"
|
|
||||||
|
|
||||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audio"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MPEG4Audio is an alias for MPEG4AudioGeneric.
|
|
||||||
type MPEG4Audio = MPEG4AudioGeneric
|
|
||||||
|
|
||||||
// MPEG4AudioGeneric is a RTP format for a MPEG-4 Audio codec.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
|
||||||
type MPEG4AudioGeneric struct {
|
|
||||||
PayloadTyp uint8
|
|
||||||
ProfileLevelID int
|
|
||||||
Config *mpeg4audio.Config
|
|
||||||
SizeLength int
|
|
||||||
IndexLength int
|
|
||||||
IndexDeltaLength int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *MPEG4AudioGeneric) unmarshal(ctx *unmarshalContext) error {
|
|
||||||
f.PayloadTyp = ctx.payloadType
|
|
||||||
|
|
||||||
for key, val := range ctx.fmtp {
|
|
||||||
switch key {
|
|
||||||
case "streamtype":
|
|
||||||
if val != "5" { // AudioStream in ISO 14496-1
|
|
||||||
return fmt.Errorf("streamtype of AAC must be 5")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "mode":
|
|
||||||
if strings.ToLower(val) != "aac-hbr" {
|
|
||||||
return fmt.Errorf("unsupported AAC mode: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "profile-level-id":
|
|
||||||
tmp, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid profile-level-id: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.ProfileLevelID = int(tmp)
|
|
||||||
|
|
||||||
case "config":
|
|
||||||
enc, err := hex.DecodeString(val)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid AAC config: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Config = &mpeg4audio.Config{}
|
|
||||||
err = f.Config.Unmarshal(enc)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid AAC config: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "sizelength":
|
|
||||||
n, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil || n > 100 {
|
|
||||||
return fmt.Errorf("invalid AAC SizeLength: %v", val)
|
|
||||||
}
|
|
||||||
f.SizeLength = int(n)
|
|
||||||
|
|
||||||
case "indexlength":
|
|
||||||
n, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil || n > 100 {
|
|
||||||
return fmt.Errorf("invalid AAC IndexLength: %v", val)
|
|
||||||
}
|
|
||||||
f.IndexLength = int(n)
|
|
||||||
|
|
||||||
case "indexdeltalength":
|
|
||||||
n, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil || n > 100 {
|
|
||||||
return fmt.Errorf("invalid AAC IndexDeltaLength: %v", val)
|
|
||||||
}
|
|
||||||
f.IndexDeltaLength = int(n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.Config == nil {
|
|
||||||
return fmt.Errorf("config is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.SizeLength == 0 {
|
|
||||||
return fmt.Errorf("sizelength is missing")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Codec implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) Codec() string {
|
|
||||||
return "MPEG-4 Audio"
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClockRate implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) ClockRate() int {
|
|
||||||
return f.Config.SampleRate
|
|
||||||
}
|
|
||||||
|
|
||||||
// PayloadType implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) PayloadType() uint8 {
|
|
||||||
return f.PayloadTyp
|
|
||||||
}
|
|
||||||
|
|
||||||
// RTPMap implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) RTPMap() string {
|
|
||||||
sampleRate := f.Config.SampleRate
|
|
||||||
if f.Config.ExtensionSampleRate != 0 {
|
|
||||||
sampleRate = f.Config.ExtensionSampleRate
|
|
||||||
}
|
|
||||||
|
|
||||||
channelCount := f.Config.ChannelCount
|
|
||||||
if f.Config.ExtensionType == mpeg4audio.ObjectTypePS {
|
|
||||||
channelCount = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
return "mpeg4-generic/" + strconv.FormatInt(int64(sampleRate), 10) +
|
|
||||||
"/" + strconv.FormatInt(int64(channelCount), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FMTP implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) FMTP() map[string]string {
|
|
||||||
enc, err := f.Config.Marshal()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
profileLevelID := f.ProfileLevelID
|
|
||||||
if profileLevelID == 0 { // support legacy definition which didn't include profile-level-id
|
|
||||||
profileLevelID = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
fmtp := map[string]string{
|
|
||||||
"streamtype": "5",
|
|
||||||
"mode": "AAC-hbr",
|
|
||||||
"profile-level-id": strconv.FormatInt(int64(profileLevelID), 10),
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.SizeLength > 0 {
|
|
||||||
fmtp["sizelength"] = strconv.FormatInt(int64(f.SizeLength), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.IndexLength > 0 {
|
|
||||||
fmtp["indexlength"] = strconv.FormatInt(int64(f.IndexLength), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.IndexDeltaLength > 0 {
|
|
||||||
fmtp["indexdeltalength"] = strconv.FormatInt(int64(f.IndexDeltaLength), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmtp["config"] = hex.EncodeToString(enc)
|
|
||||||
|
|
||||||
return fmtp
|
|
||||||
}
|
|
||||||
|
|
||||||
// PTSEqualsDTS implements Format.
|
|
||||||
func (f *MPEG4AudioGeneric) PTSEqualsDTS(*rtp.Packet) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateDecoder creates a decoder able to decode the content of the format.
|
|
||||||
func (f *MPEG4AudioGeneric) CreateDecoder() (*rtpmpeg4audio.Decoder, error) {
|
|
||||||
d := &rtpmpeg4audio.Decoder{
|
|
||||||
SizeLength: f.SizeLength,
|
|
||||||
IndexLength: f.IndexLength,
|
|
||||||
IndexDeltaLength: f.IndexDeltaLength,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := d.Init()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return d, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateEncoder creates an encoder able to encode the content of the format.
|
|
||||||
func (f *MPEG4AudioGeneric) CreateEncoder() (*rtpmpeg4audio.Encoder, error) {
|
|
||||||
e := &rtpmpeg4audio.Encoder{
|
|
||||||
PayloadType: f.PayloadTyp,
|
|
||||||
SizeLength: f.SizeLength,
|
|
||||||
IndexLength: f.IndexLength,
|
|
||||||
IndexDeltaLength: f.IndexDeltaLength,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := e.Init()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e, nil
|
|
||||||
}
|
|
@@ -1,55 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMPEG4AudioGenericAttributes(t *testing.T) {
|
|
||||||
format := &MPEG4AudioGeneric{
|
|
||||||
PayloadTyp: 96,
|
|
||||||
Config: &mpeg4audio.Config{
|
|
||||||
Type: mpeg4audio.ObjectTypeAACLC,
|
|
||||||
SampleRate: 48000,
|
|
||||||
ChannelCount: 2,
|
|
||||||
},
|
|
||||||
SizeLength: 13,
|
|
||||||
IndexLength: 3,
|
|
||||||
IndexDeltaLength: 3,
|
|
||||||
}
|
|
||||||
require.Equal(t, "MPEG-4 Audio", format.Codec())
|
|
||||||
require.Equal(t, 48000, format.ClockRate())
|
|
||||||
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMPEG4AudioGenericDecEncoder(t *testing.T) {
|
|
||||||
format := &MPEG4AudioGeneric{
|
|
||||||
PayloadTyp: 96,
|
|
||||||
Config: &mpeg4audio.Config{
|
|
||||||
Type: mpeg4audio.ObjectTypeAACLC,
|
|
||||||
SampleRate: 48000,
|
|
||||||
ChannelCount: 2,
|
|
||||||
},
|
|
||||||
SizeLength: 13,
|
|
||||||
IndexLength: 3,
|
|
||||||
IndexDeltaLength: 3,
|
|
||||||
}
|
|
||||||
|
|
||||||
enc, err := format.CreateEncoder()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
pkts, err := enc.Encode([][]byte{{0x01, 0x02, 0x03, 0x04}})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, format.PayloadType(), pkts[0].PayloadType)
|
|
||||||
|
|
||||||
dec, err := format.CreateDecoder()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
byts, err := dec.Decode(pkts[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, [][]byte{{0x01, 0x02, 0x03, 0x04}}, byts)
|
|
||||||
}
|
|
@@ -1,181 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
|
||||||
"github.com/pion/rtp"
|
|
||||||
|
|
||||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audiolatm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MPEG4AudioLATM is a RTP format for a MPEG-4 Audio codec.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
|
||||||
type MPEG4AudioLATM struct {
|
|
||||||
PayloadTyp uint8
|
|
||||||
ProfileLevelID int
|
|
||||||
Bitrate *int
|
|
||||||
CPresent bool
|
|
||||||
Config *mpeg4audio.StreamMuxConfig
|
|
||||||
SBREnabled *bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *MPEG4AudioLATM) unmarshal(ctx *unmarshalContext) error {
|
|
||||||
f.PayloadTyp = ctx.payloadType
|
|
||||||
|
|
||||||
// default value set by specification
|
|
||||||
f.ProfileLevelID = 30
|
|
||||||
f.CPresent = true
|
|
||||||
|
|
||||||
for key, val := range ctx.fmtp {
|
|
||||||
switch key {
|
|
||||||
case "profile-level-id":
|
|
||||||
tmp, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid profile-level-id: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.ProfileLevelID = int(tmp)
|
|
||||||
|
|
||||||
case "bitrate":
|
|
||||||
tmp, err := strconv.ParseUint(val, 10, 31)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid bitrate: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
v := int(tmp)
|
|
||||||
f.Bitrate = &v
|
|
||||||
|
|
||||||
case "cpresent":
|
|
||||||
f.CPresent = (val == "1")
|
|
||||||
|
|
||||||
case "config":
|
|
||||||
enc, err := hex.DecodeString(val)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid AAC config: %v", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Config = &mpeg4audio.StreamMuxConfig{}
|
|
||||||
err = f.Config.Unmarshal(enc)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid AAC config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "sbr-enabled":
|
|
||||||
v := (val == "1")
|
|
||||||
f.SBREnabled = &v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.CPresent {
|
|
||||||
if f.Config != nil {
|
|
||||||
return fmt.Errorf("config and cpresent can't be used at the same time")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if f.Config == nil {
|
|
||||||
return fmt.Errorf("config is missing")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Codec implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) Codec() string {
|
|
||||||
return "MPEG-4 Audio"
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClockRate implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) ClockRate() int {
|
|
||||||
return f.Config.Programs[0].Layers[0].AudioSpecificConfig.SampleRate
|
|
||||||
}
|
|
||||||
|
|
||||||
// PayloadType implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) PayloadType() uint8 {
|
|
||||||
return f.PayloadTyp
|
|
||||||
}
|
|
||||||
|
|
||||||
// RTPMap implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) RTPMap() string {
|
|
||||||
aoc := f.Config.Programs[0].Layers[0].AudioSpecificConfig
|
|
||||||
|
|
||||||
sampleRate := aoc.SampleRate
|
|
||||||
if aoc.ExtensionSampleRate != 0 {
|
|
||||||
sampleRate = aoc.ExtensionSampleRate
|
|
||||||
}
|
|
||||||
|
|
||||||
channelCount := aoc.ChannelCount
|
|
||||||
if aoc.ExtensionType == mpeg4audio.ObjectTypePS {
|
|
||||||
channelCount = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
return "MP4A-LATM/" + strconv.FormatInt(int64(sampleRate), 10) +
|
|
||||||
"/" + strconv.FormatInt(int64(channelCount), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FMTP implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) FMTP() map[string]string {
|
|
||||||
enc, err := f.Config.Marshal()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fmtp := map[string]string{
|
|
||||||
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
|
|
||||||
"config": hex.EncodeToString(enc),
|
|
||||||
"object": strconv.FormatInt(int64(f.Config.Programs[0].Layers[0].AudioSpecificConfig.Type), 10),
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.Bitrate != nil {
|
|
||||||
fmtp["bitrate"] = strconv.FormatInt(int64(*f.Bitrate), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.CPresent {
|
|
||||||
fmtp["cpresent"] = "1"
|
|
||||||
} else {
|
|
||||||
fmtp["cpresent"] = "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.SBREnabled != nil {
|
|
||||||
if *f.SBREnabled {
|
|
||||||
fmtp["SBR-enabled"] = "1"
|
|
||||||
} else {
|
|
||||||
fmtp["SBR-enabled"] = "0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmtp
|
|
||||||
}
|
|
||||||
|
|
||||||
// PTSEqualsDTS implements Format.
|
|
||||||
func (f *MPEG4AudioLATM) PTSEqualsDTS(*rtp.Packet) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateDecoder creates a decoder able to decode the content of the format.
|
|
||||||
func (f *MPEG4AudioLATM) CreateDecoder() (*rtpmpeg4audiolatm.Decoder, error) {
|
|
||||||
d := &rtpmpeg4audiolatm.Decoder{}
|
|
||||||
|
|
||||||
err := d.Init()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return d, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateEncoder creates an encoder able to encode the content of the format.
|
|
||||||
func (f *MPEG4AudioLATM) CreateEncoder() (*rtpmpeg4audiolatm.Encoder, error) {
|
|
||||||
e := &rtpmpeg4audiolatm.Encoder{
|
|
||||||
PayloadType: f.PayloadTyp,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := e.Init()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e, nil
|
|
||||||
}
|
|
@@ -1,64 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
|
||||||
"github.com/pion/rtp"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMPEG4AudioLATMAttributes(t *testing.T) {
|
|
||||||
format := &MPEG4AudioLATM{
|
|
||||||
PayloadTyp: 96,
|
|
||||||
ProfileLevelID: 1,
|
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
|
||||||
Type: 2,
|
|
||||||
SampleRate: 44100,
|
|
||||||
ChannelCount: 2,
|
|
||||||
},
|
|
||||||
LatmBufferFullness: 255,
|
|
||||||
}},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
require.Equal(t, "MPEG-4 Audio", format.Codec())
|
|
||||||
require.Equal(t, 44100, format.ClockRate())
|
|
||||||
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMPEG4AudioLATMDecEncoder(t *testing.T) {
|
|
||||||
format := &MPEG4AudioLATM{
|
|
||||||
PayloadTyp: 96,
|
|
||||||
ProfileLevelID: 1,
|
|
||||||
Config: &mpeg4audio.StreamMuxConfig{
|
|
||||||
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
|
||||||
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
|
||||||
AudioSpecificConfig: &mpeg4audio.Config{
|
|
||||||
Type: 2,
|
|
||||||
SampleRate: 48000,
|
|
||||||
ChannelCount: 2,
|
|
||||||
},
|
|
||||||
LatmBufferFullness: 255,
|
|
||||||
}},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
enc, err := format.CreateEncoder()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
pkts, err := enc.Encode([]byte{0x01, 0x02, 0x03, 0x04})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, format.PayloadType(), pkts[0].PayloadType)
|
|
||||||
|
|
||||||
dec, err := format.CreateDecoder()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
byts, err := dec.Decode(pkts[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, byts)
|
|
||||||
}
|
|
126
pkg/format/mpeg4_audio_test.go
Normal file
126
pkg/format/mpeg4_audio_test.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package format
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pion/rtp"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMPEG4AudioAttributes(t *testing.T) {
|
||||||
|
t.Run("generic", func(t *testing.T) {
|
||||||
|
format := &MPEG4Audio{
|
||||||
|
PayloadTyp: 96,
|
||||||
|
Config: &mpeg4audio.Config{
|
||||||
|
Type: mpeg4audio.ObjectTypeAACLC,
|
||||||
|
SampleRate: 48000,
|
||||||
|
ChannelCount: 2,
|
||||||
|
},
|
||||||
|
SizeLength: 13,
|
||||||
|
IndexLength: 3,
|
||||||
|
IndexDeltaLength: 3,
|
||||||
|
}
|
||||||
|
require.Equal(t, "MPEG-4 Audio", format.Codec())
|
||||||
|
require.Equal(t, 48000, format.ClockRate())
|
||||||
|
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
||||||
|
require.Equal(t, &mpeg4audio.Config{
|
||||||
|
Type: mpeg4audio.ObjectTypeAACLC,
|
||||||
|
SampleRate: 48000,
|
||||||
|
ChannelCount: 2,
|
||||||
|
}, format.GetConfig())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("latm", func(t *testing.T) {
|
||||||
|
format := &MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
|
PayloadTyp: 96,
|
||||||
|
ProfileLevelID: 1,
|
||||||
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
|
Type: 2,
|
||||||
|
SampleRate: 44100,
|
||||||
|
ChannelCount: 2,
|
||||||
|
},
|
||||||
|
LatmBufferFullness: 255,
|
||||||
|
}},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
require.Equal(t, "MPEG-4 Audio", format.Codec())
|
||||||
|
require.Equal(t, 44100, format.ClockRate())
|
||||||
|
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
||||||
|
require.Equal(t, &mpeg4audio.Config{
|
||||||
|
Type: 2,
|
||||||
|
SampleRate: 44100,
|
||||||
|
ChannelCount: 2,
|
||||||
|
}, format.GetConfig())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMPEG4AudioDecEncoder(t *testing.T) {
|
||||||
|
t.Run("generic", func(t *testing.T) {
|
||||||
|
format := &MPEG4Audio{
|
||||||
|
PayloadTyp: 96,
|
||||||
|
Config: &mpeg4audio.Config{
|
||||||
|
Type: mpeg4audio.ObjectTypeAACLC,
|
||||||
|
SampleRate: 48000,
|
||||||
|
ChannelCount: 2,
|
||||||
|
},
|
||||||
|
SizeLength: 13,
|
||||||
|
IndexLength: 3,
|
||||||
|
IndexDeltaLength: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
enc, err := format.CreateEncoder()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
pkts, err := enc.Encode([][]byte{{0x01, 0x02, 0x03, 0x04}})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, format.PayloadType(), pkts[0].PayloadType)
|
||||||
|
|
||||||
|
dec, err := format.CreateDecoder()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
byts, err := dec.Decode(pkts[0])
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, [][]byte{{0x01, 0x02, 0x03, 0x04}}, byts)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("latm", func(t *testing.T) {
|
||||||
|
format := &MPEG4Audio{
|
||||||
|
LATM: true,
|
||||||
|
PayloadTyp: 96,
|
||||||
|
ProfileLevelID: 1,
|
||||||
|
StreamMuxConfig: &mpeg4audio.StreamMuxConfig{
|
||||||
|
Programs: []*mpeg4audio.StreamMuxConfigProgram{{
|
||||||
|
Layers: []*mpeg4audio.StreamMuxConfigLayer{{
|
||||||
|
AudioSpecificConfig: &mpeg4audio.Config{
|
||||||
|
Type: 2,
|
||||||
|
SampleRate: 48000,
|
||||||
|
ChannelCount: 2,
|
||||||
|
},
|
||||||
|
LatmBufferFullness: 255,
|
||||||
|
}},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
enc, err := format.CreateEncoder()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
pkts, err := enc.Encode([][]byte{{0x01, 0x02, 0x03, 0x04}})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, format.PayloadType(), pkts[0].PayloadType)
|
||||||
|
|
||||||
|
dec, err := format.CreateDecoder()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
byts, err := dec.Decode(pkts[0])
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, [][]byte{{0x01, 0x02, 0x03, 0x04}}, byts)
|
||||||
|
})
|
||||||
|
}
|
@@ -13,12 +13,9 @@ import (
|
|||||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4video"
|
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4video"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MPEG4Video is an alias for MPEG4VideoES.
|
// MPEG4Video is a RTP format for a MPEG-4 Video codec.
|
||||||
type MPEG4Video = MPEG4VideoES
|
|
||||||
|
|
||||||
// MPEG4VideoES is a RTP format for a MPEG-4 Video codec.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.1
|
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.1
|
||||||
type MPEG4VideoES struct {
|
type MPEG4Video struct {
|
||||||
PayloadTyp uint8
|
PayloadTyp uint8
|
||||||
ProfileLevelID int
|
ProfileLevelID int
|
||||||
Config []byte
|
Config []byte
|
||||||
@@ -26,7 +23,7 @@ type MPEG4VideoES struct {
|
|||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *MPEG4VideoES) unmarshal(ctx *unmarshalContext) error {
|
func (f *MPEG4Video) unmarshal(ctx *unmarshalContext) error {
|
||||||
f.PayloadTyp = ctx.payloadType
|
f.PayloadTyp = ctx.payloadType
|
||||||
f.ProfileLevelID = 1 // default value imposed by specification
|
f.ProfileLevelID = 1 // default value imposed by specification
|
||||||
|
|
||||||
@@ -58,27 +55,27 @@ func (f *MPEG4VideoES) unmarshal(ctx *unmarshalContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Codec implements Format.
|
// Codec implements Format.
|
||||||
func (f *MPEG4VideoES) Codec() string {
|
func (f *MPEG4Video) Codec() string {
|
||||||
return "MPEG-4 Video"
|
return "MPEG-4 Video"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClockRate implements Format.
|
// ClockRate implements Format.
|
||||||
func (f *MPEG4VideoES) ClockRate() int {
|
func (f *MPEG4Video) ClockRate() int {
|
||||||
return 90000
|
return 90000
|
||||||
}
|
}
|
||||||
|
|
||||||
// PayloadType implements Format.
|
// PayloadType implements Format.
|
||||||
func (f *MPEG4VideoES) PayloadType() uint8 {
|
func (f *MPEG4Video) PayloadType() uint8 {
|
||||||
return f.PayloadTyp
|
return f.PayloadTyp
|
||||||
}
|
}
|
||||||
|
|
||||||
// RTPMap implements Format.
|
// RTPMap implements Format.
|
||||||
func (f *MPEG4VideoES) RTPMap() string {
|
func (f *MPEG4Video) RTPMap() string {
|
||||||
return "MP4V-ES/90000"
|
return "MP4V-ES/90000"
|
||||||
}
|
}
|
||||||
|
|
||||||
// FMTP implements Format.
|
// FMTP implements Format.
|
||||||
func (f *MPEG4VideoES) FMTP() map[string]string {
|
func (f *MPEG4Video) FMTP() map[string]string {
|
||||||
fmtp := map[string]string{
|
fmtp := map[string]string{
|
||||||
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
|
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
|
||||||
}
|
}
|
||||||
@@ -91,12 +88,12 @@ func (f *MPEG4VideoES) FMTP() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PTSEqualsDTS implements Format.
|
// PTSEqualsDTS implements Format.
|
||||||
func (f *MPEG4VideoES) PTSEqualsDTS(*rtp.Packet) bool {
|
func (f *MPEG4Video) PTSEqualsDTS(*rtp.Packet) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateDecoder creates a decoder able to decode the content of the format.
|
// CreateDecoder creates a decoder able to decode the content of the format.
|
||||||
func (f *MPEG4VideoES) CreateDecoder() (*rtpmpeg4video.Decoder, error) {
|
func (f *MPEG4Video) CreateDecoder() (*rtpmpeg4video.Decoder, error) {
|
||||||
d := &rtpmpeg4video.Decoder{}
|
d := &rtpmpeg4video.Decoder{}
|
||||||
|
|
||||||
err := d.Init()
|
err := d.Init()
|
||||||
@@ -108,7 +105,7 @@ func (f *MPEG4VideoES) CreateDecoder() (*rtpmpeg4video.Decoder, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateEncoder creates an encoder able to encode the content of the format.
|
// CreateEncoder creates an encoder able to encode the content of the format.
|
||||||
func (f *MPEG4VideoES) CreateEncoder() (*rtpmpeg4video.Encoder, error) {
|
func (f *MPEG4Video) CreateEncoder() (*rtpmpeg4video.Encoder, error) {
|
||||||
e := &rtpmpeg4video.Encoder{
|
e := &rtpmpeg4video.Encoder{
|
||||||
PayloadType: f.PayloadTyp,
|
PayloadType: f.PayloadTyp,
|
||||||
}
|
}
|
||||||
@@ -122,14 +119,14 @@ func (f *MPEG4VideoES) CreateEncoder() (*rtpmpeg4video.Encoder, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SafeSetParams sets the codec parameters.
|
// SafeSetParams sets the codec parameters.
|
||||||
func (f *MPEG4VideoES) SafeSetParams(config []byte) {
|
func (f *MPEG4Video) SafeSetParams(config []byte) {
|
||||||
f.mutex.Lock()
|
f.mutex.Lock()
|
||||||
defer f.mutex.Unlock()
|
defer f.mutex.Unlock()
|
||||||
f.Config = config
|
f.Config = config
|
||||||
}
|
}
|
||||||
|
|
||||||
// SafeParams returns the codec parameters.
|
// SafeParams returns the codec parameters.
|
||||||
func (f *MPEG4VideoES) SafeParams() []byte {
|
func (f *MPEG4Video) SafeParams() []byte {
|
||||||
f.mutex.RLock()
|
f.mutex.RLock()
|
||||||
defer f.mutex.RUnlock()
|
defer f.mutex.RUnlock()
|
||||||
return f.Config
|
return f.Config
|
@@ -1,3 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
// TODO
|
|
@@ -1,3 +0,0 @@
|
|||||||
package format
|
|
||||||
|
|
||||||
// TODO
|
|
@@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMPEG4VideoESAttributes(t *testing.T) {
|
func TestMPEG4VideoAttributes(t *testing.T) {
|
||||||
format := &MPEG4VideoES{
|
format := &MPEG4Video{
|
||||||
PayloadTyp: 96,
|
PayloadTyp: 96,
|
||||||
ProfileLevelID: 1,
|
ProfileLevelID: 1,
|
||||||
Config: []byte{0x01, 0x02, 0x03},
|
Config: []byte{0x01, 0x02, 0x03},
|
||||||
@@ -18,8 +18,8 @@ func TestMPEG4VideoESAttributes(t *testing.T) {
|
|||||||
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
require.Equal(t, true, format.PTSEqualsDTS(&rtp.Packet{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMPEG4VideoESDecEncoder(t *testing.T) {
|
func TestMPEG4VideoDecEncoder(t *testing.T) {
|
||||||
format := &MPEG4VideoES{
|
format := &MPEG4Video{
|
||||||
PayloadTyp: 96,
|
PayloadTyp: 96,
|
||||||
}
|
}
|
||||||
|
|
@@ -3,11 +3,54 @@ package rtpmpeg4audio
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audiogeneric"
|
"github.com/pion/rtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMorePacketsNeeded is an alis for rtpmpeg4audiogeneric.ErrMorePacketsNeeded.
|
// ErrMorePacketsNeeded is returned when more packets are needed.
|
||||||
var ErrMorePacketsNeeded = errors.New("need more packets")
|
var ErrMorePacketsNeeded = errors.New("need more packets")
|
||||||
|
|
||||||
// Decoder is an alias for rtpmpeg4audiogeneric.Decoder.
|
func joinFragments(fragments [][]byte, size int) []byte {
|
||||||
type Decoder = rtpmpeg4audiogeneric.Decoder
|
ret := make([]byte, size)
|
||||||
|
n := 0
|
||||||
|
for _, p := range fragments {
|
||||||
|
n += copy(ret[n:], p)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoder is a RTP/MPEG-4 Audio decoder.
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
||||||
|
type Decoder struct {
|
||||||
|
// use RFC6416 (LATM) instead of RFC3640 (generic).
|
||||||
|
LATM bool
|
||||||
|
|
||||||
|
// Generic-only
|
||||||
|
// The number of bits in which the AU-size field is encoded in the AU-header.
|
||||||
|
SizeLength int
|
||||||
|
// The number of bits in which the AU-Index is encoded in the first AU-header.
|
||||||
|
IndexLength int
|
||||||
|
// The number of bits in which the AU-Index-delta field is encoded in any non-first AU-header.
|
||||||
|
IndexDeltaLength int
|
||||||
|
|
||||||
|
firstAUParsed bool
|
||||||
|
adtsMode bool
|
||||||
|
fragments [][]byte
|
||||||
|
fragmentsSize int
|
||||||
|
fragmentsExpected int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the decoder.
|
||||||
|
func (d *Decoder) Init() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes AUs from a RTP packet.
|
||||||
|
// It returns the AUs and the PTS of the first AU.
|
||||||
|
// The PTS of subsequent AUs can be calculated by adding time.Second*mpeg4audio.SamplesPerAccessUnit/clockRate.
|
||||||
|
func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
|
||||||
|
if !d.LATM {
|
||||||
|
return d.decodeGeneric(pkt)
|
||||||
|
}
|
||||||
|
return d.decodeLATM(pkt)
|
||||||
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package rtpmpeg4audiogeneric
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/bits"
|
"github.com/bluenviron/mediacommon/pkg/bits"
|
||||||
@@ -9,45 +8,7 @@ import (
|
|||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMorePacketsNeeded is returned when more packets are needed.
|
func (d *Decoder) decodeGeneric(pkt *rtp.Packet) ([][]byte, error) {
|
||||||
var ErrMorePacketsNeeded = errors.New("need more packets")
|
|
||||||
|
|
||||||
func joinFragments(fragments [][]byte, size int) []byte {
|
|
||||||
ret := make([]byte, size)
|
|
||||||
n := 0
|
|
||||||
for _, p := range fragments {
|
|
||||||
n += copy(ret[n:], p)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoder is a RTP/MPEG-4 Audio decoder.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
|
||||||
type Decoder struct {
|
|
||||||
// The number of bits in which the AU-size field is encoded in the AU-header.
|
|
||||||
SizeLength int
|
|
||||||
|
|
||||||
// The number of bits in which the AU-Index is encoded in the first AU-header.
|
|
||||||
IndexLength int
|
|
||||||
|
|
||||||
// The number of bits in which the AU-Index-delta field is encoded in any non-first AU-header.
|
|
||||||
IndexDeltaLength int
|
|
||||||
|
|
||||||
firstAUParsed bool
|
|
||||||
adtsMode bool
|
|
||||||
fragments [][]byte
|
|
||||||
fragmentsSize int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the decoder.
|
|
||||||
func (d *Decoder) Init() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode decodes AUs from a RTP packet.
|
|
||||||
// It returns the AUs and the PTS of the first AU.
|
|
||||||
// The PTS of subsequent AUs can be calculated by adding time.Second*mpeg4audio.SamplesPerAccessUnit/clockRate.
|
|
||||||
func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
|
|
||||||
if len(pkt.Payload) < 2 {
|
if len(pkt.Payload) < 2 {
|
||||||
d.fragments = d.fragments[:0] // discard pending fragments
|
d.fragments = d.fragments[:0] // discard pending fragments
|
||||||
return nil, fmt.Errorf("payload is too short")
|
return nil, fmt.Errorf("payload is too short")
|
||||||
@@ -130,12 +91,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([][]byte, error) {
|
|||||||
d.fragments = d.fragments[:0]
|
d.fragments = d.fragments[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
aus, err = d.removeADTS(aus)
|
return d.removeADTS(aus)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return aus, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readAUHeaders(buf []byte, headersLen int) ([]uint64, error) {
|
func (d *Decoder) readAUHeaders(buf []byte, headersLen int) ([]uint64, error) {
|
@@ -1,4 +1,4 @@
|
|||||||
package rtpmpeg4audiogeneric
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecodeGeneric(t *testing.T) {
|
||||||
for _, ca := range cases {
|
for _, ca := range casesGeneric {
|
||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
d := &Decoder{
|
d := &Decoder{
|
||||||
SizeLength: ca.sizeLength,
|
SizeLength: ca.sizeLength,
|
||||||
@@ -41,7 +41,7 @@ func TestDecode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeADTS(t *testing.T) {
|
func TestDecodeGenericADTS(t *testing.T) {
|
||||||
d := &Decoder{
|
d := &Decoder{
|
||||||
SizeLength: 13,
|
SizeLength: 13,
|
||||||
IndexLength: 3,
|
IndexLength: 3,
|
||||||
@@ -69,7 +69,7 @@ func TestDecodeADTS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FuzzDecoder(f *testing.F) {
|
func FuzzDecoderGeneric(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, a []byte, am bool, b []byte, bm bool) {
|
f.Fuzz(func(t *testing.T, a []byte, am bool, b []byte, bm bool) {
|
||||||
d := &Decoder{
|
d := &Decoder{
|
||||||
SizeLength: 13,
|
SizeLength: 13,
|
@@ -1,41 +1,13 @@
|
|||||||
package rtpmpeg4audiolatm
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMorePacketsNeeded is returned when more packets are needed.
|
func (d *Decoder) decodeLATM(pkt *rtp.Packet) ([][]byte, error) {
|
||||||
var ErrMorePacketsNeeded = errors.New("need more packets")
|
|
||||||
|
|
||||||
func joinFragments(fragments [][]byte, size int) []byte {
|
|
||||||
ret := make([]byte, size)
|
|
||||||
n := 0
|
|
||||||
for _, p := range fragments {
|
|
||||||
n += copy(ret[n:], p)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoder is a RTP/MPEG-4 Audio decoder.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
|
||||||
type Decoder struct {
|
|
||||||
fragments [][]byte
|
|
||||||
fragmentsSize int
|
|
||||||
fragmentsExpected int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the decoder.
|
|
||||||
func (d *Decoder) Init() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode decodes an AU from a RTP packet.
|
|
||||||
// It returns the AU and its PTS.
|
|
||||||
func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, error) {
|
|
||||||
var au []byte
|
var au []byte
|
||||||
buf := pkt.Payload
|
buf := pkt.Payload
|
||||||
|
|
||||||
@@ -79,5 +51,5 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, error) {
|
|||||||
d.fragments = d.fragments[:0]
|
d.fragments = d.fragments[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return au, nil
|
return [][]byte{au}, nil
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package rtpmpeg4audiolatm
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -7,19 +7,21 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecodeLATM(t *testing.T) {
|
||||||
for _, ca := range cases {
|
for _, ca := range casesLATM {
|
||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
d := &Decoder{}
|
d := &Decoder{
|
||||||
|
LATM: true,
|
||||||
|
}
|
||||||
err := d.Init()
|
err := d.Init()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
var au []byte
|
var aus [][]byte
|
||||||
|
|
||||||
for _, pkt := range ca.pkts {
|
for _, pkt := range ca.pkts {
|
||||||
clone := pkt.Clone()
|
clone := pkt.Clone()
|
||||||
|
|
||||||
au, err = d.Decode(pkt)
|
aus, err = d.Decode(pkt)
|
||||||
|
|
||||||
// test input integrity
|
// test input integrity
|
||||||
require.Equal(t, clone, pkt)
|
require.Equal(t, clone, pkt)
|
||||||
@@ -31,17 +33,19 @@ func TestDecode(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, ca.au, au)
|
require.Equal(t, ca.au, aus[0])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeOtherData(t *testing.T) {
|
func TestDecodeLATMOtherData(t *testing.T) {
|
||||||
d := &Decoder{}
|
d := &Decoder{
|
||||||
|
LATM: true,
|
||||||
|
}
|
||||||
err := d.Init()
|
err := d.Init()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
au, err := d.Decode(&rtp.Packet{
|
aus, err := d.Decode(&rtp.Packet{
|
||||||
Header: rtp.Header{
|
Header: rtp.Header{
|
||||||
Version: 2,
|
Version: 2,
|
||||||
Marker: true,
|
Marker: true,
|
||||||
@@ -55,12 +59,14 @@ func TestDecodeOtherData(t *testing.T) {
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, []byte{1, 2, 3, 4}, au)
|
require.Equal(t, []byte{1, 2, 3, 4}, aus[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func FuzzDecoder(f *testing.F) {
|
func FuzzDecoderLATM(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, a []byte, am bool, b []byte, bm bool) {
|
f.Fuzz(func(t *testing.T, a []byte, am bool, b []byte, bm bool) {
|
||||||
d := &Decoder{}
|
d := &Decoder{
|
||||||
|
LATM: true,
|
||||||
|
}
|
||||||
d.Init() //nolint:errcheck
|
d.Init() //nolint:errcheck
|
||||||
|
|
||||||
d.Decode(&rtp.Packet{ //nolint:errcheck
|
d.Decode(&rtp.Packet{ //nolint:errcheck
|
@@ -1,8 +1,88 @@
|
|||||||
package rtpmpeg4audio
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audiogeneric"
|
"crypto/rand"
|
||||||
|
|
||||||
|
"github.com/pion/rtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encoder is an alias for rtpmpeg4audiogeneric.Encoder.
|
const (
|
||||||
type Encoder = rtpmpeg4audiogeneric.Encoder
|
rtpVersion = 2
|
||||||
|
defaultPayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
|
||||||
|
)
|
||||||
|
|
||||||
|
func randUint32() (uint32, error) {
|
||||||
|
var b [4]byte
|
||||||
|
_, err := rand.Read(b[:])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encoder is a RTP/MPEG4-audio encoder.
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
||||||
|
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
||||||
|
type Encoder struct {
|
||||||
|
// payload type of packets.
|
||||||
|
PayloadType uint8
|
||||||
|
|
||||||
|
// use RFC6416 (LATM) instead of RFC3640 (generic).
|
||||||
|
LATM bool
|
||||||
|
|
||||||
|
// The number of bits in which the AU-size field is encoded in the AU-header.
|
||||||
|
SizeLength int
|
||||||
|
|
||||||
|
// The number of bits in which the AU-Index is encoded in the first AU-header.
|
||||||
|
IndexLength int
|
||||||
|
|
||||||
|
// The number of bits in which the AU-Index-delta field is encoded in any non-first AU-header.
|
||||||
|
IndexDeltaLength int
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// maximum size of packet payloads (optional).
|
||||||
|
// It defaults to 1460.
|
||||||
|
PayloadMaxSize int
|
||||||
|
|
||||||
|
sequenceNumber uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the encoder.
|
||||||
|
func (e *Encoder) Init() error {
|
||||||
|
if e.SSRC == nil {
|
||||||
|
v, err := randUint32()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
e.SSRC = &v
|
||||||
|
}
|
||||||
|
if e.InitialSequenceNumber == nil {
|
||||||
|
v, err := randUint32()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v2 := uint16(v)
|
||||||
|
e.InitialSequenceNumber = &v2
|
||||||
|
}
|
||||||
|
if e.PayloadMaxSize == 0 {
|
||||||
|
e.PayloadMaxSize = defaultPayloadMaxSize
|
||||||
|
}
|
||||||
|
|
||||||
|
e.sequenceNumber = *e.InitialSequenceNumber
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes AUs into RTP packets.
|
||||||
|
func (e *Encoder) Encode(aus [][]byte) ([]*rtp.Packet, error) {
|
||||||
|
if !e.LATM {
|
||||||
|
return e.encodeGeneric(aus)
|
||||||
|
}
|
||||||
|
return e.encodeLATM(aus)
|
||||||
|
}
|
||||||
|
@@ -1,29 +1,13 @@
|
|||||||
package rtpmpeg4audiogeneric
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
|
|
||||||
"github.com/bluenviron/mediacommon/pkg/bits"
|
"github.com/bluenviron/mediacommon/pkg/bits"
|
||||||
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func packetCountGeneric(avail, le int) int {
|
||||||
rtpVersion = 2
|
|
||||||
defaultPayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
|
|
||||||
)
|
|
||||||
|
|
||||||
func randUint32() (uint32, error) {
|
|
||||||
var b [4]byte
|
|
||||||
_, err := rand.Read(b[:])
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func packetCount(avail, le int) int {
|
|
||||||
n := le / avail
|
n := le / avail
|
||||||
if (le % avail) != 0 {
|
if (le % avail) != 0 {
|
||||||
n++
|
n++
|
||||||
@@ -31,76 +15,20 @@ func packetCount(avail, le int) int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encoder is a RTP/MPEG4-audio encoder.
|
func (e *Encoder) encodeGeneric(aus [][]byte) ([]*rtp.Packet, error) {
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc3640
|
|
||||||
type Encoder struct {
|
|
||||||
// payload type of packets.
|
|
||||||
PayloadType uint8
|
|
||||||
|
|
||||||
// The number of bits in which the AU-size field is encoded in the AU-header.
|
|
||||||
SizeLength int
|
|
||||||
|
|
||||||
// The number of bits in which the AU-Index is encoded in the first AU-header.
|
|
||||||
IndexLength int
|
|
||||||
|
|
||||||
// The number of bits in which the AU-Index-delta field is encoded in any non-first AU-header.
|
|
||||||
IndexDeltaLength int
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
// maximum size of packet payloads (optional).
|
|
||||||
// It defaults to 1460.
|
|
||||||
PayloadMaxSize int
|
|
||||||
|
|
||||||
sequenceNumber uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the encoder.
|
|
||||||
func (e *Encoder) Init() error {
|
|
||||||
if e.SSRC == nil {
|
|
||||||
v, err := randUint32()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
e.SSRC = &v
|
|
||||||
}
|
|
||||||
if e.InitialSequenceNumber == nil {
|
|
||||||
v, err := randUint32()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
v2 := uint16(v)
|
|
||||||
e.InitialSequenceNumber = &v2
|
|
||||||
}
|
|
||||||
if e.PayloadMaxSize == 0 {
|
|
||||||
e.PayloadMaxSize = defaultPayloadMaxSize
|
|
||||||
}
|
|
||||||
|
|
||||||
e.sequenceNumber = *e.InitialSequenceNumber
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode encodes AUs into RTP packets.
|
|
||||||
func (e *Encoder) Encode(aus [][]byte) ([]*rtp.Packet, error) {
|
|
||||||
var rets []*rtp.Packet
|
var rets []*rtp.Packet
|
||||||
var batch [][]byte
|
var batch [][]byte
|
||||||
timestamp := uint32(0)
|
timestamp := uint32(0)
|
||||||
|
|
||||||
// split AUs into batches
|
// split AUs into batches
|
||||||
for _, au := range aus {
|
for _, au := range aus {
|
||||||
if e.lenAggregated(batch, au) <= e.PayloadMaxSize {
|
if e.lenGenericAggregated(batch, au) <= e.PayloadMaxSize {
|
||||||
// add to existing batch
|
// add to existing batch
|
||||||
batch = append(batch, au)
|
batch = append(batch, au)
|
||||||
} else {
|
} else {
|
||||||
// write current batch
|
// write current batch
|
||||||
if batch != nil {
|
if batch != nil {
|
||||||
pkts, err := e.writeBatch(batch, timestamp)
|
pkts, err := e.writeGenericBatch(batch, timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -114,7 +42,7 @@ func (e *Encoder) Encode(aus [][]byte) ([]*rtp.Packet, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write last batch
|
// write last batch
|
||||||
pkts, err := e.writeBatch(batch, timestamp)
|
pkts, err := e.writeGenericBatch(batch, timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -123,15 +51,15 @@ func (e *Encoder) Encode(aus [][]byte) ([]*rtp.Packet, error) {
|
|||||||
return rets, nil
|
return rets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) writeBatch(aus [][]byte, timestamp uint32) ([]*rtp.Packet, error) {
|
func (e *Encoder) writeGenericBatch(aus [][]byte, timestamp uint32) ([]*rtp.Packet, error) {
|
||||||
if len(aus) != 1 || e.lenAggregated(aus, nil) < e.PayloadMaxSize {
|
if len(aus) != 1 || e.lenGenericAggregated(aus, nil) < e.PayloadMaxSize {
|
||||||
return e.writeAggregated(aus, timestamp)
|
return e.writeGenericAggregated(aus, timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.writeFragmented(aus[0], timestamp)
|
return e.writeGenericFragmented(aus[0], timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) writeFragmented(au []byte, timestamp uint32) ([]*rtp.Packet, error) {
|
func (e *Encoder) writeGenericFragmented(au []byte, timestamp uint32) ([]*rtp.Packet, error) {
|
||||||
auHeadersLen := e.SizeLength + e.IndexLength
|
auHeadersLen := e.SizeLength + e.IndexLength
|
||||||
auHeadersLenBytes := auHeadersLen / 8
|
auHeadersLenBytes := auHeadersLen / 8
|
||||||
if (auHeadersLen % 8) != 0 {
|
if (auHeadersLen % 8) != 0 {
|
||||||
@@ -140,7 +68,7 @@ func (e *Encoder) writeFragmented(au []byte, timestamp uint32) ([]*rtp.Packet, e
|
|||||||
|
|
||||||
avail := e.PayloadMaxSize - 2 - auHeadersLenBytes
|
avail := e.PayloadMaxSize - 2 - auHeadersLenBytes
|
||||||
le := len(au)
|
le := len(au)
|
||||||
packetCount := packetCount(avail, le)
|
packetCount := packetCountGeneric(avail, le)
|
||||||
|
|
||||||
ret := make([]*rtp.Packet, packetCount)
|
ret := make([]*rtp.Packet, packetCount)
|
||||||
le = avail
|
le = avail
|
||||||
@@ -183,7 +111,7 @@ func (e *Encoder) writeFragmented(au []byte, timestamp uint32) ([]*rtp.Packet, e
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
|
func (e *Encoder) lenGenericAggregated(aus [][]byte, addAU []byte) int {
|
||||||
n := 2 // AU-headers-length
|
n := 2 // AU-headers-length
|
||||||
|
|
||||||
// AU-headers
|
// AU-headers
|
||||||
@@ -218,8 +146,8 @@ func (e *Encoder) lenAggregated(aus [][]byte, addAU []byte) int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) writeAggregated(aus [][]byte, timestamp uint32) ([]*rtp.Packet, error) {
|
func (e *Encoder) writeGenericAggregated(aus [][]byte, timestamp uint32) ([]*rtp.Packet, error) {
|
||||||
payload := make([]byte, e.lenAggregated(aus, nil))
|
payload := make([]byte, e.lenGenericAggregated(aus, nil))
|
||||||
|
|
||||||
// AU-headers
|
// AU-headers
|
||||||
written := 0
|
written := 0
|
@@ -1,4 +1,4 @@
|
|||||||
package rtpmpeg4audiogeneric
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -32,7 +32,7 @@ func mergeBytes(vals ...[]byte) []byte {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
var cases = []struct {
|
var casesGeneric = []struct {
|
||||||
name string
|
name string
|
||||||
sizeLength int
|
sizeLength int
|
||||||
indexLength int
|
indexLength int
|
||||||
@@ -498,8 +498,8 @@ var cases = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncodeGeneric(t *testing.T) {
|
||||||
for _, ca := range cases {
|
for _, ca := range casesGeneric {
|
||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
e := &Encoder{
|
e := &Encoder{
|
||||||
PayloadType: 96,
|
PayloadType: 96,
|
||||||
@@ -518,16 +518,3 @@ func TestEncode(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeRandomInitialState(t *testing.T) {
|
|
||||||
e := &Encoder{
|
|
||||||
PayloadType: 96,
|
|
||||||
SizeLength: 13,
|
|
||||||
IndexLength: 3,
|
|
||||||
IndexDeltaLength: 3,
|
|
||||||
}
|
|
||||||
err := e.Init()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEqual(t, nil, e.SSRC)
|
|
||||||
require.NotEqual(t, nil, e.InitialSequenceNumber)
|
|
||||||
}
|
|
76
pkg/format/rtpmpeg4audio/encoder_latm.go
Normal file
76
pkg/format/rtpmpeg4audio/encoder_latm.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package rtpmpeg4audio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pion/rtp"
|
||||||
|
|
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e *Encoder) packetCountLATM(auLen int, plil int) int {
|
||||||
|
totalLen := plil + auLen
|
||||||
|
n := totalLen / e.PayloadMaxSize
|
||||||
|
if (totalLen % e.PayloadMaxSize) != 0 {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Encoder) encodeLATM(aus [][]byte) ([]*rtp.Packet, error) {
|
||||||
|
var rets []*rtp.Packet
|
||||||
|
|
||||||
|
for i, au := range aus {
|
||||||
|
timestamp := uint32(i) * mpeg4audio.SamplesPerAccessUnit
|
||||||
|
|
||||||
|
add, err := e.encodeLATMSingle(au, timestamp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rets = append(rets, add...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rets, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Encoder) encodeLATMSingle(au []byte, timestamp uint32) ([]*rtp.Packet, error) {
|
||||||
|
auLen := len(au)
|
||||||
|
plil := payloadLengthInfoEncodeSize(auLen)
|
||||||
|
packetCount := e.packetCountLATM(auLen, plil)
|
||||||
|
|
||||||
|
ret := make([]*rtp.Packet, packetCount)
|
||||||
|
le := e.PayloadMaxSize - plil
|
||||||
|
|
||||||
|
for i := range ret {
|
||||||
|
if i == (packetCount - 1) {
|
||||||
|
le = len(au)
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload []byte
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
payload = make([]byte, plil+le)
|
||||||
|
payloadLengthInfoEncode(plil, auLen, payload)
|
||||||
|
copy(payload[plil:], au[:le])
|
||||||
|
au = au[le:]
|
||||||
|
le = e.PayloadMaxSize
|
||||||
|
} else {
|
||||||
|
payload = au[:le]
|
||||||
|
au = au[le:]
|
||||||
|
}
|
||||||
|
|
||||||
|
ret[i] = &rtp.Packet{
|
||||||
|
Header: rtp.Header{
|
||||||
|
Version: rtpVersion,
|
||||||
|
PayloadType: e.PayloadType,
|
||||||
|
SequenceNumber: e.sequenceNumber,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
SSRC: *e.SSRC,
|
||||||
|
Marker: (i == packetCount-1),
|
||||||
|
},
|
||||||
|
Payload: payload,
|
||||||
|
}
|
||||||
|
|
||||||
|
e.sequenceNumber++
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package rtpmpeg4audiolatm
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -8,31 +8,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uint16Ptr(v uint16) *uint16 {
|
var casesLATM = []struct {
|
||||||
return &v
|
|
||||||
}
|
|
||||||
|
|
||||||
func uint32Ptr(v uint32) *uint32 {
|
|
||||||
return &v
|
|
||||||
}
|
|
||||||
|
|
||||||
func mergeBytes(vals ...[]byte) []byte {
|
|
||||||
size := 0
|
|
||||||
for _, v := range vals {
|
|
||||||
size += len(v)
|
|
||||||
}
|
|
||||||
res := make([]byte, size)
|
|
||||||
|
|
||||||
pos := 0
|
|
||||||
for _, v := range vals {
|
|
||||||
n := copy(res[pos:], v)
|
|
||||||
pos += n
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
var cases = []struct {
|
|
||||||
name string
|
name string
|
||||||
au []byte
|
au []byte
|
||||||
pkts []*rtp.Packet
|
pkts []*rtp.Packet
|
||||||
@@ -137,10 +113,11 @@ var cases = []struct {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncodeLATM(t *testing.T) {
|
||||||
for _, ca := range cases {
|
for _, ca := range casesLATM {
|
||||||
t.Run(ca.name, func(t *testing.T) {
|
t.Run(ca.name, func(t *testing.T) {
|
||||||
e := &Encoder{
|
e := &Encoder{
|
||||||
|
LATM: true,
|
||||||
PayloadType: 96,
|
PayloadType: 96,
|
||||||
SSRC: uint32Ptr(0x9dbb7812),
|
SSRC: uint32Ptr(0x9dbb7812),
|
||||||
InitialSequenceNumber: uint16Ptr(0x44ed),
|
InitialSequenceNumber: uint16Ptr(0x44ed),
|
||||||
@@ -148,19 +125,9 @@ func TestEncode(t *testing.T) {
|
|||||||
err := e.Init()
|
err := e.Init()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
pkts, err := e.Encode(ca.au)
|
pkts, err := e.Encode([][]byte{ca.au})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, ca.pkts, pkts)
|
require.Equal(t, ca.pkts, pkts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeRandomInitialState(t *testing.T) {
|
|
||||||
e := &Encoder{
|
|
||||||
PayloadType: 96,
|
|
||||||
}
|
|
||||||
err := e.Init()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEqual(t, nil, e.SSRC)
|
|
||||||
require.NotEqual(t, nil, e.InitialSequenceNumber)
|
|
||||||
}
|
|
20
pkg/format/rtpmpeg4audio/encoder_test.go
Normal file
20
pkg/format/rtpmpeg4audio/encoder_test.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package rtpmpeg4audio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncodeRandomInitialState(t *testing.T) {
|
||||||
|
e := &Encoder{
|
||||||
|
PayloadType: 96,
|
||||||
|
SizeLength: 13,
|
||||||
|
IndexLength: 3,
|
||||||
|
IndexDeltaLength: 3,
|
||||||
|
}
|
||||||
|
err := e.Init()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEqual(t, nil, e.SSRC)
|
||||||
|
require.NotEqual(t, nil, e.InitialSequenceNumber)
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package rtpmpeg4audiolatm
|
package rtpmpeg4audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
@@ -1,2 +0,0 @@
|
|||||||
// Package rtpmpeg4audiogeneric contains a RTP/MPEG-4 Audio decoder and encoder.
|
|
||||||
package rtpmpeg4audiogeneric
|
|
@@ -1,120 +0,0 @@
|
|||||||
package rtpmpeg4audiolatm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
rtpVersion = 2
|
|
||||||
defaultPayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
|
|
||||||
)
|
|
||||||
|
|
||||||
func randUint32() (uint32, error) {
|
|
||||||
var b [4]byte
|
|
||||||
_, err := rand.Read(b[:])
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoder is a RTP/MPEG4-audio encoder.
|
|
||||||
// Specification: https://datatracker.ietf.org/doc/html/rfc6416#section-7.3
|
|
||||||
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
|
|
||||||
|
|
||||||
// maximum size of packet payloads (optional).
|
|
||||||
// It defaults to 1460.
|
|
||||||
PayloadMaxSize int
|
|
||||||
|
|
||||||
sequenceNumber uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init initializes the encoder.
|
|
||||||
func (e *Encoder) Init() error {
|
|
||||||
if e.SSRC == nil {
|
|
||||||
v, err := randUint32()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
e.SSRC = &v
|
|
||||||
}
|
|
||||||
if e.InitialSequenceNumber == nil {
|
|
||||||
v, err := randUint32()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
v2 := uint16(v)
|
|
||||||
e.InitialSequenceNumber = &v2
|
|
||||||
}
|
|
||||||
if e.PayloadMaxSize == 0 {
|
|
||||||
e.PayloadMaxSize = defaultPayloadMaxSize
|
|
||||||
}
|
|
||||||
|
|
||||||
e.sequenceNumber = *e.InitialSequenceNumber
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Encoder) packetCount(auLen int, plil int) int {
|
|
||||||
totalLen := plil + auLen
|
|
||||||
n := totalLen / e.PayloadMaxSize
|
|
||||||
if (totalLen % e.PayloadMaxSize) != 0 {
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode encodes an access unit into RTP packets.
|
|
||||||
func (e *Encoder) Encode(au []byte) ([]*rtp.Packet, error) {
|
|
||||||
auLen := len(au)
|
|
||||||
plil := payloadLengthInfoEncodeSize(auLen)
|
|
||||||
packetCount := e.packetCount(auLen, plil)
|
|
||||||
|
|
||||||
ret := make([]*rtp.Packet, packetCount)
|
|
||||||
le := e.PayloadMaxSize - plil
|
|
||||||
|
|
||||||
for i := range ret {
|
|
||||||
if i == (packetCount - 1) {
|
|
||||||
le = len(au)
|
|
||||||
}
|
|
||||||
|
|
||||||
var payload []byte
|
|
||||||
|
|
||||||
if i == 0 {
|
|
||||||
payload = make([]byte, plil+le)
|
|
||||||
payloadLengthInfoEncode(plil, auLen, payload)
|
|
||||||
copy(payload[plil:], au[:le])
|
|
||||||
au = au[le:]
|
|
||||||
le = e.PayloadMaxSize
|
|
||||||
} else {
|
|
||||||
payload = au[:le]
|
|
||||||
au = au[le:]
|
|
||||||
}
|
|
||||||
|
|
||||||
ret[i] = &rtp.Packet{
|
|
||||||
Header: rtp.Header{
|
|
||||||
Version: rtpVersion,
|
|
||||||
PayloadType: e.PayloadType,
|
|
||||||
SequenceNumber: e.sequenceNumber,
|
|
||||||
SSRC: *e.SSRC,
|
|
||||||
Marker: (i == packetCount-1),
|
|
||||||
},
|
|
||||||
Payload: payload,
|
|
||||||
}
|
|
||||||
|
|
||||||
e.sequenceNumber++
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
@@ -1,2 +0,0 @@
|
|||||||
// Package rtpmpeg4audiolatm contains a RTP/MPEG-4 Audio decoder and encoder.
|
|
||||||
package rtpmpeg4audiolatm
|
|
Reference in New Issue
Block a user