forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datachannel.go
145 lines (114 loc) · 2.6 KB
/
datachannel.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
package sfu
import (
"context"
"errors"
"sync"
"time"
"github.com/pion/webrtc/v4"
)
var (
ErrDataChannelExists = errors.New("error: data channel already exists")
)
type SFUDataChannel struct {
label string
clientIDs []string
isOrdered bool
}
type SFUDataChannelList struct {
dataChannels map[string]*SFUDataChannel
mu sync.Mutex
}
type DataChannelOptions struct {
Ordered bool
ClientIDs []string // empty means all clients
}
type Data struct {
FromID string `json:"from_id"`
ToID string `json:"to_id"`
SentAt time.Time `json:"sent_at"`
Data interface{} `json:"data"`
}
type DataChannelList struct {
dataChannels map[string]*webrtc.DataChannel
mu sync.Mutex
}
func NewSFUDataChannel(label string, opts DataChannelOptions) *SFUDataChannel {
return &SFUDataChannel{
label: label,
clientIDs: opts.ClientIDs,
isOrdered: opts.Ordered,
}
}
func (s *SFUDataChannel) ClientIDs() []string {
return s.clientIDs
}
func (s *SFUDataChannel) IsOrdered() bool {
return s.isOrdered
}
func NewSFUDataChannelList() *SFUDataChannelList {
return &SFUDataChannelList{
dataChannels: make(map[string]*SFUDataChannel),
mu: sync.Mutex{},
}
}
func (s *SFUDataChannelList) Add(label string, opts DataChannelOptions) *SFUDataChannel {
s.mu.Lock()
defer s.mu.Unlock()
sfuDC := NewSFUDataChannel(label, opts)
s.dataChannels[label] = sfuDC
return sfuDC
}
func (s *SFUDataChannelList) Remove(dc *SFUDataChannel) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.dataChannels, dc.label)
}
func (s *SFUDataChannelList) Get(label string) *SFUDataChannel {
s.mu.Lock()
defer s.mu.Unlock()
dc, ok := s.dataChannels[label]
if !ok {
return nil
}
return dc
}
func DefaultDataChannelOptions() DataChannelOptions {
return DataChannelOptions{
Ordered: true,
ClientIDs: []string{},
}
}
func NewDataChannelList(ctx context.Context) *DataChannelList {
list := &DataChannelList{
dataChannels: make(map[string]*webrtc.DataChannel),
mu: sync.Mutex{},
}
return list
}
func (d *DataChannelList) Add(dc *webrtc.DataChannel) {
d.mu.Lock()
defer d.mu.Unlock()
d.dataChannels[dc.Label()] = dc
}
func (d *DataChannelList) Remove(dc *webrtc.DataChannel) {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.dataChannels, dc.Label())
}
func (d *DataChannelList) Get(label string) *webrtc.DataChannel {
d.mu.Lock()
defer d.mu.Unlock()
dc, ok := d.dataChannels[label]
if !ok {
return nil
}
return dc
}
func (d *DataChannelList) Clear() {
d.mu.Lock()
defer d.mu.Unlock()
for label, dc := range d.dataChannels {
dc.Close()
delete(d.dataChannels, label)
}
}