forked from bluenviron/gortsplib
/
track_aac.go
169 lines (146 loc) · 3.87 KB
/
track_aac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gortsplib
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/Coimbra1984/gortsplib/pkg/aac"
)
// TrackAAC is an AAC track.
type TrackAAC struct {
trackBase
payloadType uint8
typ int
sampleRate int
channelCount int
aotSpecificConfig []byte
mpegConf []byte
}
// NewTrackAAC allocates a TrackAAC.
func NewTrackAAC(payloadType uint8, typ int, sampleRate int,
channelCount int, aotSpecificConfig []byte) (*TrackAAC, error) {
mpegConf, err := aac.MPEG4AudioConfig{
Type: aac.MPEG4AudioType(typ),
SampleRate: sampleRate,
ChannelCount: channelCount,
AOTSpecificConfig: aotSpecificConfig,
}.Encode()
if err != nil {
return nil, fmt.Errorf("invalid configuration: %s", err)
}
return &TrackAAC{
payloadType: payloadType,
typ: typ,
sampleRate: sampleRate,
channelCount: channelCount,
aotSpecificConfig: aotSpecificConfig,
mpegConf: mpegConf,
}, nil
}
func newTrackAACFromMediaDescription(
control string,
payloadType uint8,
md *psdp.MediaDescription) (*TrackAAC, error) {
v, ok := md.Attribute("fmtp")
if !ok {
return nil, fmt.Errorf("fmtp attribute is missing")
}
tmp := strings.SplitN(v, " ", 2)
if len(tmp) != 2 {
return nil, fmt.Errorf("invalid fmtp (%v)", v)
}
for _, kv := range strings.Split(tmp[1], ";") {
kv = strings.Trim(kv, " ")
if len(kv) == 0 {
continue
}
tmp := strings.SplitN(kv, "=", 2)
if len(tmp) != 2 {
return nil, fmt.Errorf("invalid fmtp (%v)", v)
}
if tmp[0] == "config" {
enc, err := hex.DecodeString(tmp[1])
if err != nil {
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
var mpegConf aac.MPEG4AudioConfig
err = mpegConf.Decode(enc)
if err != nil {
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
// re-encode the conf to normalize it
enc, _ = mpegConf.Encode()
return &TrackAAC{
trackBase: trackBase{
control: control,
},
payloadType: payloadType,
typ: int(mpegConf.Type),
sampleRate: mpegConf.SampleRate,
channelCount: mpegConf.ChannelCount,
aotSpecificConfig: mpegConf.AOTSpecificConfig,
mpegConf: enc,
}, nil
}
}
return nil, fmt.Errorf("config is missing (%v)", v)
}
// ClockRate returns the track clock rate.
func (t *TrackAAC) ClockRate() int {
return t.sampleRate
}
// Type returns the track MPEG4-audio type.
func (t *TrackAAC) Type() int {
return t.typ
}
// ChannelCount returns the track channel count.
func (t *TrackAAC) ChannelCount() int {
return t.channelCount
}
// AOTSpecificConfig returns the track AOT specific config.
func (t *TrackAAC) AOTSpecificConfig() []byte {
return t.aotSpecificConfig
}
func (t *TrackAAC) clone() Track {
return &TrackAAC{
trackBase: t.trackBase,
payloadType: t.payloadType,
typ: t.typ,
sampleRate: t.sampleRate,
channelCount: t.channelCount,
aotSpecificConfig: t.aotSpecificConfig,
mpegConf: t.mpegConf,
}
}
// MediaDescription returns the track media description in SDP format.
func (t *TrackAAC) MediaDescription() *psdp.MediaDescription {
typ := strconv.FormatInt(int64(t.payloadType), 10)
return &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{typ},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: typ + " mpeg4-generic/" + strconv.FormatInt(int64(t.sampleRate), 10) +
"/" + strconv.FormatInt(int64(t.channelCount), 10),
},
{
Key: "fmtp",
Value: typ + " profile-level-id=1; " +
"mode=AAC-hbr; " +
"sizelength=13; " +
"indexlength=3; " +
"indexdeltalength=3; " +
"config=" + hex.EncodeToString(t.mpegConf),
},
{
Key: "control",
Value: t.control,
},
},
}
}