forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsep.go
154 lines (137 loc) · 4.91 KB
/
jsep.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
package sdp
import (
"fmt"
"net"
"time"
)
// Constants for SDP attributes used in JSEP
const (
AttrKeyIdentity = "identity"
AttrKeyGroup = "group"
AttrKeySsrc = "ssrc"
AttrKeySsrcGroup = "ssrc-group"
AttrKeyMsidSemantic = "msid-semantic"
AttrKeyConnectionSetup = "setup"
AttrKeyMID = "mid"
AttrKeyICELite = "ice-lite"
AttrKeyRtcpMux = "rtcp-mux"
AttrKeyRtcpRsize = "rtcp-rsize"
)
// Constants for semantic tokens used in JSEP
const (
SemanticTokenLipSynchronization = "LS"
SemanticTokenFlowIdentification = "FID"
SemanticTokenForwardErrorCorrection = "FEC"
SemanticTokenWebRTCMediaStreams = "WMS"
)
// API to match draft-ietf-rtcweb-jsep
// Move to webrtc or its own package?
// NewJSEPSessionDescription creates a new SessionDescription with
// some settings that are required by the JSEP spec.
func NewJSEPSessionDescription(fingerprint string, identity bool) *SessionDescription {
d := &SessionDescription{
Version: 0,
Origin: Origin{
Username: "-",
SessionID: newSessionID(),
SessionVersion: uint64(time.Now().Unix()),
NetworkType: "IN",
AddressType: "IP4",
UnicastAddress: "0.0.0.0",
},
SessionName: "-",
TimeDescriptions: []TimeDescription{
{
Timing: Timing{
StartTime: 0,
StopTime: 0,
},
RepeatTimes: nil,
},
},
Attributes: []Attribute{
// "Attribute(ice-options:trickle)", // TODO: implement trickle ICE
Attribute("fingerprint:sha-256 " + fingerprint),
},
}
if identity {
d.WithPropertyAttribute(AttrKeyIdentity)
}
return d
}
// WithPropertyAttribute adds a property attribute 'a=key' to the session description
func (s *SessionDescription) WithPropertyAttribute(key string) *SessionDescription {
s.Attributes = append(s.Attributes, Attribute(key))
return s
}
// WithValueAttribute adds a value attribute 'a=key:value' to the session description
func (s *SessionDescription) WithValueAttribute(key, value string) *SessionDescription {
s.Attributes = append(s.Attributes, Attribute(fmt.Sprintf("%s:%s", key, value)))
return s
}
// WithMedia adds a media description to the session description
func (s *SessionDescription) WithMedia(md *MediaDescription) *SessionDescription {
s.MediaDescriptions = append(s.MediaDescriptions, md)
return s
}
// NewJSEPMediaDescription creates a new MediaName with
// some settings that are required by the JSEP spec.
func NewJSEPMediaDescription(codecType string, codecPrefs []string) *MediaDescription {
// TODO: handle codecPrefs
d := &MediaDescription{
MediaName: MediaName{
Media: codecType,
Port: RangedPort{Value: 9},
Protos: []string{"UDP", "TLS", "RTP", "SAVPF"},
},
ConnectionInformation: &ConnectionInformation{
NetworkType: "IN",
AddressType: "IP4",
Address: &Address{
IP: net.ParseIP("0.0.0.0"),
},
},
}
return d
}
// WithPropertyAttribute adds a property attribute 'a=key' to the media description
func (d *MediaDescription) WithPropertyAttribute(key string) *MediaDescription {
d.Attributes = append(d.Attributes, Attribute(key))
return d
}
// WithValueAttribute adds a value attribute 'a=key:value' to the media description
func (d *MediaDescription) WithValueAttribute(key, value string) *MediaDescription {
d.Attributes = append(d.Attributes, Attribute(fmt.Sprintf("%s:%s", key, value)))
return d
}
// WithICECredentials adds ICE credentials to the media description
func (d *MediaDescription) WithICECredentials(username, password string) *MediaDescription {
return d.
WithValueAttribute("ice-ufrag", username).
WithValueAttribute("ice-pwd", password)
}
// WithCodec adds codec information to the media description
func (d *MediaDescription) WithCodec(payloadType uint8, name string, clockrate uint32, channels uint16, fmtp string) *MediaDescription {
d.MediaName.Formats = append(d.MediaName.Formats, int(payloadType))
rtpmap := fmt.Sprintf("%d %s/%d", payloadType, name, clockrate)
if channels > 0 {
rtpmap = rtpmap + fmt.Sprintf("/%d", channels)
}
d.WithValueAttribute("rtpmap", rtpmap)
if fmtp != "" {
d.WithValueAttribute("fmtp", fmt.Sprintf("%d %s", payloadType, fmtp))
}
return d
}
// WithMediaSource adds media source information to the media description
func (d *MediaDescription) WithMediaSource(ssrc uint32, cname, streamLabel, label string) *MediaDescription {
return d.
WithValueAttribute("ssrc", fmt.Sprintf("%d cname:%s", ssrc, cname)). // Deprecated but not phased out?
WithValueAttribute("ssrc", fmt.Sprintf("%d msid:%s %s", ssrc, streamLabel, label)).
WithValueAttribute("ssrc", fmt.Sprintf("%d mslabel:%s", ssrc, streamLabel)). // Deprecated but not phased out?
WithValueAttribute("ssrc", fmt.Sprintf("%d label:%s", ssrc, label)) // Deprecated but not phased out?
}
// WithCandidate adds an ICE candidate to the media description
func (d *MediaDescription) WithCandidate(value string) *MediaDescription {
return d.WithValueAttribute("candidate", value)
}