-
Notifications
You must be signed in to change notification settings - Fork 119
/
endpoint.go
134 lines (102 loc) · 4.14 KB
/
endpoint.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
package mediaserver
import (
"sync"
native "github.com/notedit/media-server-go/wrapper"
"github.com/notedit/sdp"
)
// Endpoint is an endpoint represent an UDP server socket.
// The endpoint will process STUN requests in order to be able to associate the remote ip:port with the registered transport and forward any further data comming from that transport.
// Being a server it is ICE-lite.
type Endpoint struct {
ip string
bundle native.RTPBundleTransport
candidate *sdp.CandidateInfo
mirroredStreams map[string]*IncomingStream
mirroredTracks map[string]*IncomingStreamTrack
fingerprint string
sync.Mutex
}
// NewEndpoint create a new endpoint with given ip
func NewEndpoint(ip string) *Endpoint {
endpoint := &Endpoint{}
endpoint.bundle = native.NewRTPBundleTransport()
endpoint.bundle.Init()
endpoint.fingerprint = native.MediaServerGetFingerprint()
endpoint.mirroredStreams = make(map[string]*IncomingStream)
endpoint.mirroredTracks = make(map[string]*IncomingStreamTrack)
endpoint.candidate = sdp.NewCandidateInfo("1", 1, "UDP", 33554431, ip, endpoint.bundle.GetLocalPort(), "host", "", 0)
return endpoint
}
// NewEndpointWithPort create a new endpint with given ip and port
func NewEndpointWithPort(ip string, port int) *Endpoint {
endpoint := &Endpoint{}
endpoint.bundle = native.NewRTPBundleTransport()
endpoint.bundle.Init(port)
endpoint.fingerprint = native.MediaServerGetFingerprint()
endpoint.candidate = sdp.NewCandidateInfo("1", 1, "UDP", 33554431, ip, endpoint.bundle.GetLocalPort(), "host", "", 0)
return endpoint
}
//SetAffinity Set cpu affinity
func (e *Endpoint) SetAffinity(cpu int) {
e.bundle.SetAffinity(cpu)
}
// CreateTransport create a new transport object and register it with the remote ICE username and password
// disableSTUNKeepAlive - Disable ICE/STUN keep alives, required for server to server transports, set this to false if you do not how to use it
func (e *Endpoint) CreateTransport(remoteSdp *sdp.SDPInfo, localSdp *sdp.SDPInfo, options ...bool) *Transport {
var localIce *sdp.ICEInfo
var localDtls *sdp.DTLSInfo
var localCandidates []*sdp.CandidateInfo
if localSdp == nil {
localIce = sdp.ICEInfoGenerate(true)
localDtls = sdp.NewDTLSInfo(remoteSdp.GetDTLS().GetSetup().Reverse(), "sha-256", e.fingerprint)
localCandidates = []*sdp.CandidateInfo{e.candidate}
} else {
localIce = localSdp.GetICE().Clone()
localDtls = localSdp.GetDTLS().Clone()
localCandidates = localSdp.GetCandidates()
}
remoteIce := remoteSdp.GetICE().Clone()
remoteDtls := remoteSdp.GetDTLS().Clone()
remoteCandidates := remoteSdp.GetCandidates()
localIce.SetLite(true)
localIce.SetEndOfCandidate(true)
disableSTUNKeepAlive := false
if len(options) > 0 {
disableSTUNKeepAlive = options[0]
}
transport := NewTransport(e.bundle, remoteIce, remoteDtls, remoteCandidates,
localIce, localDtls, localCandidates, disableSTUNKeepAlive)
return transport
}
// GetLocalCandidates Get local ICE candidates for this endpoint. It will be shared by all the transport associated to this endpoint.
func (e *Endpoint) GetLocalCandidates() []*sdp.CandidateInfo {
return []*sdp.CandidateInfo{e.candidate}
}
// GetDTLSFingerprint Get local DTLS fingerprint for this endpoint. It will be shared by all the transport associated to this endpoint
func (e *Endpoint) GetDTLSFingerprint() string {
return e.fingerprint
}
// CreateOffer create offer based on audio and video capability
// It generates a random ICE username and password and gets endpoint fingerprint
func (e *Endpoint) CreateOffer(video *sdp.Capability, audio *sdp.Capability) *sdp.SDPInfo {
dtls := sdp.NewDTLSInfo(sdp.SETUPACTPASS, "sha-256", e.fingerprint)
ice := sdp.GenerateICEInfo(true)
candidates := e.GetLocalCandidates()
capabilities := make(map[string]*sdp.Capability)
if video != nil {
capabilities["video"] = video
}
if audio != nil {
capabilities["audio"] = audio
}
return sdp.Create(ice, dtls, candidates, capabilities)
}
// Stop stop the endpoint UDP server and terminate any associated transport
func (e *Endpoint) Stop() {
if e.bundle == nil {
return
}
e.bundle.End()
native.DeleteRTPBundleTransport(e.bundle)
e.bundle = nil
}