This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
signaler.go
214 lines (182 loc) · 4.42 KB
/
signaler.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
210
211
212
213
214
package dcnet
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"github.com/pions/webrtc"
)
// Signaler
type Signaler interface {
Dial() (*webrtc.RTCDataChannel, net.Addr, error)
Accept() (*webrtc.RTCDataChannel, net.Addr, error)
Close() error
Addr() net.Addr
}
// RWSignaler is a simple signaler over an io.ReadWriteCloser
// It plainly exchanges the SDP offer/answer without peer address.
type RWSignaler struct {
c io.ReadWriteCloser
config webrtc.RTCConfiguration // Maybe make this global?
}
// NewRWSignaler creates a new RWSignaler
func NewRWSignaler(c io.ReadWriteCloser, rtcconfig webrtc.RTCConfiguration) *RWSignaler {
s := &RWSignaler{
c: c,
config: rtcconfig,
}
return s
}
// Accept creates WebRTC DataChannels by signaling over the ReadWriteCloser
func (r *RWSignaler) Dial() (*webrtc.RTCDataChannel, net.Addr, error) {
return r.doSignal(true)
}
// Accept creates WebRTC DataChannels by signaling over the ReadWriteCloser
func (r *RWSignaler) Accept() (*webrtc.RTCDataChannel, net.Addr, error) {
return r.doSignal(false)
}
func (r *RWSignaler) doSignal(init bool) (*webrtc.RTCDataChannel, net.Addr, error) {
c, err := webrtc.New(r.config)
if err != nil {
return nil, nil, err
}
var dc *webrtc.RTCDataChannel
var addr net.Addr
if init {
dc, err = c.CreateDataChannel("data", nil)
if err != nil {
return nil, nil, err
}
// TODO: migrate to OnNegotiationNeeded when available
offer, err := c.CreateOffer(nil)
if err != nil {
return nil, nil, err
}
b, err := json.Marshal(offer)
if err != nil {
return nil, nil, err
}
fmt.Println("Sending initial offer", string(b))
fmt.Println()
f, err := NewRTPFrameWriter(len(b), r.c)
if err != nil {
return nil, nil, err
}
// TODO: Don't assume we can write the entire offer at once
_, err = f.Write(b)
if err != nil {
return nil, nil, err
}
addr = &NilAddr{}
}
go func() {
for {
f, err := NewRTPFrameReader(r.c)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
fmt.Println()
var desc webrtc.RTCSessionDescription
err = json.Unmarshal(b, &desc)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
if err := c.SetRemoteDescription(desc); err != nil {
panic(err)
}
fmt.Println("Got", desc.Type, string(b))
log.Println(desc.Type, webrtc.RTCSdpTypeOffer, desc.Type == webrtc.RTCSdpTypeOffer)
if desc.Type == webrtc.RTCSdpTypeOffer {
// Sets the LocalDescription, and starts our UDP listeners
answer, err := c.CreateAnswer(nil)
if err != nil {
panic(err)
}
b, err := json.Marshal(answer)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
fmt.Println("Sending answer", string(b))
fmt.Println()
f, err := NewRTPFrameWriter(len(b), r.c)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
// TODO: Don't assume we can write the entire answer at once
_, err = f.Write(b)
if err != nil {
// TODO: Return error from Accept()
log.Println(err)
}
}
}
}()
if dc == nil {
res := make(chan struct {
d *webrtc.RTCDataChannel
a net.Addr
})
c.OnDataChannel = func(d *webrtc.RTCDataChannel) {
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID)
res <- struct {
d *webrtc.RTCDataChannel
a net.Addr
}{
d: d,
a: &NilAddr{},
}
}
e := <-res
dc = e.d
addr = e.a
}
return dc, addr, nil
}
// Close closed the ReadWriteCloser
func (r *RWSignaler) Close() error {
return r.c.Close()
}
func (r *RWSignaler) Addr() net.Addr {
return &NilAddr{}
}
// MultiSignaler combines the power of many signalers
type MultiSignaler struct {
s []Signaler
}
// NewMultiSignaler creates a new MultiSignaler
func NewMultiSignaler(set ...Signaler) (*MultiSignaler, error) {
res := &MultiSignaler{
s: set,
}
return res, nil
}
func (s *MultiSignaler) Accept() (*webrtc.RTCDataChannel, net.Addr, error) {
// TODO: accept on all signalers
panic("TODO")
return nil, nil, nil
}
func (s *MultiSignaler) Close() error {
var closeErr error
for _, signaler := range s.s {
err := signaler.Close()
if err != nil {
closeErr = err
}
}
return closeErr
}
func (s *MultiSignaler) Addr() net.Addr {
// We assume all signalers use the same addr
return s.s[0].Addr()
}