/
track_h264.go
209 lines (174 loc) · 4.25 KB
/
track_h264.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package gortsplib
import (
"encoding/base64"
"encoding/hex"
"fmt"
"strconv"
"strings"
"sync"
psdp "github.com/pion/sdp/v3"
"github.com/kuartis/gortsplib_go/pkg/base"
)
func trackH264GetSPSPPS(md *psdp.MediaDescription) ([]byte, []byte, error) {
v, ok := md.Attribute("fmtp")
if !ok {
return nil, nil, fmt.Errorf("fmtp attribute is missing")
}
tmp := strings.SplitN(v, " ", 2)
if len(tmp) != 2 {
return nil, nil, fmt.Errorf("invalid fmtp attribute (%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, nil, fmt.Errorf("invalid fmtp attribute (%v)", v)
}
if tmp[0] == "sprop-parameter-sets" {
tmp := strings.Split(tmp[1], ",")
if len(tmp) < 2 {
return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
}
sps, err := base64.StdEncoding.DecodeString(tmp[0])
if err != nil {
return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
}
pps, err := base64.StdEncoding.DecodeString(tmp[1])
if err != nil {
return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v)
}
return sps, pps, nil
}
}
return nil, nil, fmt.Errorf("sprop-parameter-sets is missing (%v)", v)
}
// TrackH264 is a H264 track.
type TrackH264 struct {
control string
payloadType uint8
sps []byte
pps []byte
extradata []byte
mutex sync.RWMutex
}
// NewTrackH264 allocates a TrackH264.
func NewTrackH264(payloadType uint8, sps []byte, pps []byte, extradata []byte) (*TrackH264, error) {
return &TrackH264{
payloadType: payloadType,
sps: sps,
pps: pps,
extradata: extradata,
}, nil
}
func newTrackH264FromMediaDescription(
payloadType uint8,
md *psdp.MediaDescription) (*TrackH264, error) {
control := trackFindControl(md)
t := &TrackH264{
control: control,
payloadType: payloadType,
}
sps, pps, err := trackH264GetSPSPPS(md)
if err == nil {
t.sps = sps
t.pps = pps
}
return t, nil
}
// ClockRate returns the track clock rate.
func (t *TrackH264) ClockRate() int {
return 90000
}
func (t *TrackH264) clone() Track {
return &TrackH264{
control: t.control,
payloadType: t.payloadType,
sps: t.sps,
pps: t.pps,
extradata: t.extradata,
}
}
// GetControl gets the track control.
func (t *TrackH264) GetControl() string {
return t.control
}
// SetControl sets the track control.
func (t *TrackH264) SetControl(c string) {
t.control = c
}
func (t *TrackH264) url(contentBase *base.URL) (*base.URL, error) {
return trackURL(t, contentBase)
}
// SPS returns the track SPS.
func (t *TrackH264) SPS() []byte {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.sps
}
// PPS returns the track PPS.
func (t *TrackH264) PPS() []byte {
t.mutex.RLock()
defer t.mutex.RUnlock()
return t.pps
}
// ExtraData returns the track extra data.
func (t *TrackH264) ExtraData() []byte {
return t.extradata
}
// SetSPS sets the track SPS.
func (t *TrackH264) SetSPS(v []byte) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.sps = v
}
// SetPPS sets the track PPS.
func (t *TrackH264) SetPPS(v []byte) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.pps = v
}
// MediaDescription returns the media description in SDP format.
func (t *TrackH264) MediaDescription() *psdp.MediaDescription {
t.mutex.RLock()
defer t.mutex.RUnlock()
typ := strconv.FormatInt(int64(t.payloadType), 10)
fmtp := typ + " packetization-mode=1"
var tmp []string
if t.sps != nil {
tmp = append(tmp, base64.StdEncoding.EncodeToString(t.sps))
}
if t.pps != nil {
tmp = append(tmp, base64.StdEncoding.EncodeToString(t.pps))
}
if t.extradata != nil {
tmp = append(tmp, base64.StdEncoding.EncodeToString(t.extradata))
}
fmtp += "; sprop-parameter-sets=" + strings.Join(tmp, ",")
if len(t.sps) >= 4 {
fmtp += "; profile-level-id=" + strings.ToUpper(hex.EncodeToString(t.sps[1:4]))
}
return &psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{typ},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: typ + " H264/90000",
},
{
Key: "fmtp",
Value: fmtp,
},
{
Key: "control",
Value: t.control,
},
},
}
}