forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track_mpeg4audio.go
178 lines (153 loc) · 3.87 KB
/
track_mpeg4audio.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
170
171
172
173
174
175
176
177
178
package gortsplib
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/cobalt-robotics/gortsplib/pkg/mpeg4audio"
)
// TrackMPEG4Audio is a MPEG-4 audio track.
type TrackMPEG4Audio struct {
PayloadType uint8
Config *mpeg4audio.Config
SizeLength int
IndexLength int
IndexDeltaLength int
trackBase
}
func newTrackMPEG4AudioFromMediaDescription(
control string,
payloadType uint8,
md *psdp.MediaDescription,
) (*TrackMPEG4Audio, 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)
}
t := &TrackMPEG4Audio{
PayloadType: payloadType,
trackBase: trackBase{
control: control,
},
}
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)
}
switch strings.ToLower(tmp[0]) {
case "config":
enc, err := hex.DecodeString(tmp[1])
if err != nil {
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
t.Config = &mpeg4audio.Config{}
err = t.Config.Unmarshal(enc)
if err != nil {
return nil, fmt.Errorf("invalid AAC config (%v)", tmp[1])
}
case "sizelength":
val, err := strconv.ParseUint(tmp[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid AAC SizeLength (%v)", tmp[1])
}
t.SizeLength = int(val)
case "indexlength":
val, err := strconv.ParseUint(tmp[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid AAC IndexLength (%v)", tmp[1])
}
t.IndexLength = int(val)
case "indexdeltalength":
val, err := strconv.ParseUint(tmp[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid AAC IndexDeltaLength (%v)", tmp[1])
}
t.IndexDeltaLength = int(val)
}
}
if t.Config == nil {
return nil, fmt.Errorf("config is missing (%v)", v)
}
if t.SizeLength == 0 {
return nil, fmt.Errorf("sizelength is missing (%v)", v)
}
return t, nil
}
// ClockRate returns the track clock rate.
func (t *TrackMPEG4Audio) ClockRate() int {
return t.Config.SampleRate
}
func (t *TrackMPEG4Audio) clone() Track {
return &TrackMPEG4Audio{
PayloadType: t.PayloadType,
Config: t.Config,
SizeLength: t.SizeLength,
IndexLength: t.IndexLength,
IndexDeltaLength: t.IndexDeltaLength,
trackBase: t.trackBase,
}
}
// MediaDescription returns the track media description in SDP format.
func (t *TrackMPEG4Audio) MediaDescription() *psdp.MediaDescription {
enc, err := t.Config.Marshal()
if err != nil {
return nil
}
typ := strconv.FormatInt(int64(t.PayloadType), 10)
sampleRate := t.Config.SampleRate
if t.Config.ExtensionSampleRate != 0 {
sampleRate = t.Config.ExtensionSampleRate
}
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(sampleRate), 10) +
"/" + strconv.FormatInt(int64(t.Config.ChannelCount), 10),
},
{
Key: "fmtp",
Value: typ + " profile-level-id=1; " +
"mode=AAC-hbr; " +
func() string {
if t.SizeLength > 0 {
return fmt.Sprintf("sizelength=%d; ", t.SizeLength)
}
return ""
}() +
func() string {
if t.IndexLength > 0 {
return fmt.Sprintf("indexlength=%d; ", t.IndexLength)
}
return ""
}() +
func() string {
if t.IndexDeltaLength > 0 {
return fmt.Sprintf("indexdeltalength=%d; ", t.IndexDeltaLength)
}
return ""
}() +
"config=" + hex.EncodeToString(enc),
},
{
Key: "control",
Value: t.control,
},
},
}
}