-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal-messenger.go
152 lines (125 loc) · 3.05 KB
/
signal-messenger.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
package main
import (
"context"
"encoding/json"
"errors"
"io"
"log"
"github.com/pion/webrtc/v3"
)
var _ SignalICE = &SignalMessenger{}
var _ SignalIngress = &SignalMessenger{}
var _ SignalEgress = &SignalMessenger{}
type SignalMessenger struct {
offer chan string
answer chan string
ican chan *webrtc.ICECandidateInit
io Messenger
}
func NewSignalMessenger(ctx context.Context, io Messenger) (s *SignalMessenger) {
s = &SignalMessenger{
offer: make(chan string),
answer: make(chan string),
ican: make(chan *webrtc.ICECandidateInit),
io: io,
}
go s.readLoop(ctx)
return
}
func (s *SignalMessenger) readLoop(ctx context.Context) {
for {
if ctx.Err() != nil {
break
}
b, err := s.io.Read(ctx)
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) {
break
}
if err != nil {
log.Println("signal io: read failed:", err)
break
}
msg := &SignalMessengerMessage{}
if err := json.Unmarshal(b, msg); err != nil {
log.Println("signalio: received unknwon message:", string(b))
continue
}
switch msg.Type {
case signalMessengerOffer:
chanSend(ctx, s.offer, msg.Data)
case signalMessengerAnswer:
chanSend(ctx, s.answer, msg.Data)
case signalMessengerIceCandidate:
chanSend(ctx, s.ican, msg.ICECandidateInit())
}
}
}
func (s *SignalMessenger) SendOffer(ctx context.Context, offer string) (err error) {
msg := SignalMessengerMessage{
Type: signalMessengerOffer,
Data: offer,
}
b, err := json.Marshal(msg)
if err != nil {
return
}
return s.io.Write(ctx, b)
}
func (s *SignalMessenger) RecvAnswer(ctx context.Context) (answer string, err error) {
return chanRecv(ctx, s.answer)
}
func (s *SignalMessenger) RecvOffer(ctx context.Context) (offer string, err error) {
return chanRecv(ctx, s.offer)
}
func (s *SignalMessenger) SendAnswer(ctx context.Context, answer string) (err error) {
msg := SignalMessengerMessage{
Type: signalMessengerAnswer,
Data: answer,
}
b, err := json.Marshal(msg)
if err != nil {
return
}
return s.io.Write(ctx, b)
}
func (s *SignalMessenger) SendICECandidate(ctx context.Context, ic *webrtc.ICECandidate) (err error) {
if ic == nil {
return
}
b, err := json.Marshal(ic.ToJSON())
if err != nil {
return err
}
msg := SignalMessengerMessage{
Type: signalMessengerIceCandidate,
Data: string(b),
}
b, err = json.Marshal(msg)
if err != nil {
return
}
return s.io.Write(ctx, b)
}
func (s *SignalMessenger) RecvICECandidate(ctx context.Context) (ic *webrtc.ICECandidateInit, err error) {
return chanRecv(ctx, s.ican)
}
func (s *SignalMessenger) Close() (err error) {
chanClose(s.answer)
chanClose(s.offer)
chanClose(s.ican)
return s.io.Close()
}
const (
signalMessengerOffer = "OFFER"
signalMessengerAnswer = "ANSWER"
signalMessengerIceCandidate = "ICE_CANDIDATE"
)
type SignalMessengerMessage struct {
Type string `json:"type"`
Data string `json:"data"`
}
func (s *SignalMessengerMessage) ICECandidateInit() (ican *webrtc.ICECandidateInit) {
ican = &webrtc.ICECandidateInit{}
json.Unmarshal([]byte(s.Data), ican)
return
}