forked from jiyeyuran/mediasoup-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_producer.go
279 lines (230 loc) · 5.86 KB
/
data_producer.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package mediasoup
import "sync/atomic"
type DataProducerOptions struct {
/**
* DataProducer id (just for Router.pipeToRouter() method).
*/
Id string `json:"id,omitempty"`
/**
* SCTP parameters defining how the endpoint is sending the data.
* Just if messages are sent over SCTP.
*/
SctpStreamParameters *SctpStreamParameters `json:"sctpStreamParameters,omitempty"`
/**
* A label which can be used to distinguish this DataChannel from others.
*/
Label string `json:"label,omitempty"`
/**
* Name of the sub-protocol used by this DataChannel.
*/
Protocol string `json:"protocol,omitempty"`
/**
* Custom application data.
*/
AppData interface{} `json:"app_data,omitempty"`
}
type DataProducerStat struct {
Type string
Timestamp int64
Label string
Protocol string
MessagesReceived int64
BytesReceived int64
}
/**
* DataProducer type.
*/
type DataProducerType = DataConsumerType
const (
DataProducerType_Sctp DataProducerType = DataConsumerType_Sctp
DataProducerType_Direct = DataConsumerType_Direct
)
type dataProducerParams struct {
// {
// routerId: string;
// transportId: string;
// dataProducerId: string;
// };
internal internalData
data dataProducerData
channel *Channel
payloadChannel *PayloadChannel
appData interface{}
}
type dataProducerData struct {
Type DataProducerType
SctpStreamParameters SctpStreamParameters
Label string
Protocol string
}
/**
* DataProducer
* @emits transportclose
* @emits @close
*/
type DataProducer struct {
IEventEmitter
logger Logger
internal internalData
data dataProducerData
channel *Channel
payloadChannel *PayloadChannel
appData interface{}
closed uint32
observer IEventEmitter
}
func newDataProducer(params dataProducerParams) *DataProducer {
logger := NewLogger("DataProducer")
logger.Debug("constructor()")
if params.appData == nil {
params.appData = H{}
}
p := &DataProducer{
IEventEmitter: NewEventEmitter(),
logger: logger,
internal: params.internal,
data: params.data,
channel: params.channel,
payloadChannel: params.payloadChannel,
appData: params.appData,
observer: NewEventEmitter(),
}
p.handleWorkerNotifications()
return p
}
// DataProducer id
func (p *DataProducer) Id() string {
return p.internal.DataProducerId
}
// Whether the DataProducer is closed.
func (p *DataProducer) Closed() bool {
return atomic.LoadUint32(&p.closed) > 0
}
// DataProducer type.
func (p *DataProducer) Type() DataConsumerType {
return p.data.Type
}
/**
* SCTP stream parameters.
*/
func (p *DataProducer) SctpStreamParameters() SctpStreamParameters {
return p.data.SctpStreamParameters
}
/**
* DataChannel label.
*/
func (p *DataProducer) Label() string {
return p.data.Label
}
/**
* DataChannel protocol.
*/
func (p *DataProducer) Protocol() string {
return p.data.Protocol
}
/**
* App custom data.
*/
func (p *DataProducer) AppData() interface{} {
return p.appData
}
/**
* Observer.
*
* @emits close
*/
func (p *DataProducer) Observer() IEventEmitter {
return p.observer
}
// Close the DataProducer.
func (p *DataProducer) Close() (err error) {
if atomic.CompareAndSwapUint32(&p.closed, 0, 1) {
p.logger.Debug("close()")
// Remove notification subscriptions.
p.channel.RemoveAllListeners(p.Id())
p.payloadChannel.RemoveAllListeners(p.Id())
response := p.channel.Request("dataProducer.close", p.internal)
if err = response.Err(); err != nil {
p.logger.Error("dataProducer close error: %s", err)
}
p.Emit("@close")
p.RemoveAllListeners()
// Emit observer event.
p.observer.SafeEmit("close")
p.observer.RemoveAllListeners()
}
return
}
// Transport was closed.
func (p *DataProducer) transportClosed() {
if atomic.CompareAndSwapUint32(&p.closed, 0, 1) {
p.logger.Debug("transportClosed()")
p.SafeEmit("transportclose")
p.RemoveAllListeners()
// Emit observer event.
p.observer.SafeEmit("close")
p.observer.RemoveAllListeners()
}
}
// Dump DataConsumer.
func (p *DataProducer) Dump() (dump DataProducerDump, err error) {
p.logger.Debug("dump()")
resp := p.channel.Request("dataProducer.dump", p.internal)
err = resp.Unmarshal(&dump)
return
}
// Get DataConsumer stats.
func (p *DataProducer) GetStats() (stats []*DataProducerStat, err error) {
p.logger.Debug("getStats()")
resp := p.channel.Request("dataProducer.getStats", p.internal)
err = resp.Unmarshal(&stats)
return
}
/**
* Send data.
*/
func (p *DataProducer) Send(data []byte, ppid ...int) (err error) {
/*
* +-------------------------------+----------+
* | Value | SCTP |
* | | PPID |
* +-------------------------------+----------+
* | WebRTC String | 51 |
* | WebRTC Binary Partial | 52 |
* | (Deprecated) | |
* | WebRTC Binary | 53 |
* | WebRTC String Partial | 54 |
* | (Deprecated) | |
* | WebRTC String Empty | 56 |
* | WebRTC Binary Empty | 57 |
* +-------------------------------+----------+
*/
ppidVal := 0
if len(ppid) == 0 {
if len(data) > 0 {
ppidVal = PPID_WEBRTC_BINARY
} else {
ppidVal = 57
}
} else {
ppidVal = ppid[0]
}
if ppidVal == 56 || ppidVal == 57 {
data = make([]byte, 1)
}
notifData := H{"ppid": ppidVal}
return p.payloadChannel.Notify("dataProducer.send", p.internal, notifData, data)
}
/**
* Send text.
*/
func (p *DataProducer) SendText(message string) error {
ppid := PPID_WEBRTC_STRING
if len(message) == 0 {
ppid = 56
}
return p.Send([]byte(message), ppid)
}
func (p *DataProducer) handleWorkerNotifications() {
// No need to subscribe to any event.
}