forked from bluenviron/gortsplib
/
track_opus.go
108 lines (95 loc) · 2.21 KB
/
track_opus.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
package gortsplib
import (
"fmt"
"strconv"
"strings"
psdp "github.com/pion/sdp/v3"
)
// TrackOpus is a Opus track.
type TrackOpus struct {
trackBase
payloadType uint8
sampleRate int
channelCount int
}
// NewTrackOpus allocates a TrackOpus.
func NewTrackOpus(payloadType uint8, sampleRate int, channelCount int) (*TrackOpus, error) {
return &TrackOpus{
payloadType: payloadType,
sampleRate: sampleRate,
channelCount: channelCount,
}, nil
}
func newTrackOpusFromMediaDescription(
control string,
payloadType uint8,
rtpmapPart1 string,
md *psdp.MediaDescription) (*TrackOpus, error) {
tmp := strings.SplitN(rtpmapPart1, "/", 3)
if len(tmp) != 3 {
return nil, fmt.Errorf("invalid rtpmap (%v)", rtpmapPart1)
}
sampleRate, err := strconv.ParseInt(tmp[1], 10, 64)
if err != nil {
return nil, err
}
channelCount, err := strconv.ParseInt(tmp[2], 10, 64)
if err != nil {
return nil, err
}
return &TrackOpus{
trackBase: trackBase{
control: control,
},
payloadType: payloadType,
sampleRate: int(sampleRate),
channelCount: int(channelCount),
}, nil
}
// ClockRate returns the track clock rate.
func (t *TrackOpus) ClockRate() int {
return t.sampleRate
}
func (t *TrackOpus) clone() Track {
return &TrackOpus{
trackBase: t.trackBase,
payloadType: t.payloadType,
sampleRate: t.sampleRate,
channelCount: t.channelCount,
}
}
// ChannelCount returns the channel count.
func (t *TrackOpus) ChannelCount() int {
return t.channelCount
}
// MediaDescription returns the track media description in SDP format.
func (t *TrackOpus) 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 + " opus/" + strconv.FormatInt(int64(t.sampleRate), 10) +
"/" + strconv.FormatInt(int64(t.channelCount), 10),
},
{
Key: "fmtp",
Value: typ + " sprop-stereo=" + func() string {
if t.channelCount == 2 {
return "1"
}
return "0"
}(),
},
{
Key: "control",
Value: t.control,
},
},
}
}