-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgwNativeRTCDataChannel.c
393 lines (320 loc) · 10.7 KB
/
NgwNativeRTCDataChannel.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <glib.h>
#include <gst/gst.h>
#define GST_USE_UNSTABLE_API
#include <gst/webrtc/datachannel.h>
#include "NgwNativeRTCDataChannel.h"
struct _NgwNativeRTCDataChannel {
GObject parent_object;
GstWebRTCDataChannel *gstdatachannel;
};
G_DEFINE_TYPE(
NgwNativeRTCDataChannel,
ngw_native_rtc_data_channel,
G_TYPE_OBJECT)
enum
{
SIGNAL_ON_OPEN,
SIGNAL_ON_CLOSE,
SIGNAL_ON_ERROR,
SIGNAL_ON_MESSAGE_DATA,
SIGNAL_ON_MESSAGE_STRING,
SIGNAL_ON_BUFFERED_AMOUNT_LOW,
LAST_SIGNAL,
};
enum {
PROP_GSTDATACHANNEL = 1,
N_PROPERTIES
};
static guint datachannel_signals[LAST_SIGNAL] = { 0 };
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void ngw_native_rtc_data_channel_set_property(
GObject *, guint, const GValue *, GParamSpec *);
static void ngw_native_rtc_data_channel_get_property(
GObject *, guint, GValue *, GParamSpec *);
static void ngw_native_rtc_data_channel_constructed(GObject *);
static void ngw_native_rtc_data_channel_dispose(GObject *);
static void ngw_native_rtc_data_channel_class_init(
NgwNativeRTCDataChannelClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = ngw_native_rtc_data_channel_set_property;
object_class->get_property = ngw_native_rtc_data_channel_get_property;
object_class->constructed = ngw_native_rtc_data_channel_constructed;
object_class->dispose = ngw_native_rtc_data_channel_dispose;
obj_properties[PROP_GSTDATACHANNEL] =
g_param_spec_object("gst-data-channel",
"Gstreamer data channel",
"Underlying GstWebRTCDataChannel",
GST_TYPE_WEBRTC_DATA_CHANNEL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
/**
* NgwNativeRTCDataChannel::on-open:
* @object: the #NgwNativeRTCDataChannel
*/
datachannel_signals[SIGNAL_ON_OPEN] =
g_signal_new ("on-open", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
/**
* NgwNativeRTCDataChannel::on-close:
* @object: the #NgwNativeRTCDataChannel
*/
datachannel_signals[SIGNAL_ON_CLOSE] =
g_signal_new ("on-close", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
/**
* NgwNativeRTCDataChannel::on-error:
* @object: the #NgwNativeRTCDataChannel
* @error: the #GError thrown
*/
datachannel_signals[SIGNAL_ON_ERROR] =
g_signal_new ("on-error", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_ERROR);
/**
* NgwNativeRTCDataChannel::on-message-data:
* @object: the #NgwNativeRTCDataChannel
* @data: (nullable): a #GBytes of the data received
*/
datachannel_signals[SIGNAL_ON_MESSAGE_DATA] =
g_signal_new ("on-message-data", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_BYTES);
/**
* NgwNativeRTCDataChannel::on-message-string:
* @object: the #NgwNativeRTCDataChannel
* @data: (nullable): the data received as a string
*/
datachannel_signals[SIGNAL_ON_MESSAGE_STRING] =
g_signal_new ("on-message-string", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
/**
* NgwNativeRTCDataChannel::on-buffered-amount-low:
* @object: the #NgwNativeRTCDataChannel
*/
datachannel_signals[SIGNAL_ON_BUFFERED_AMOUNT_LOW] =
g_signal_new ("on-buffered-amount-low", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
static void ngw_native_rtc_data_channel_init(
NgwNativeRTCDataChannel * self)
{
self->gstdatachannel = NULL;
}
static void ngw_native_rtc_data_channel_set_property(
GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(object);
switch (property_id) {
case PROP_GSTDATACHANNEL:
if (self->gstdatachannel) // ???
g_object_unref(self->gstdatachannel);
self->gstdatachannel = g_value_dup_object(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void ngw_native_rtc_data_channel_get_property(
GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(object);
switch (property_id) {
case PROP_GSTDATACHANNEL:
g_value_set_object(value, self->gstdatachannel);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void on_buffered_amount_low_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data);
static void on_close_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data);
static void on_error_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
GError * error,
gpointer user_data);
static void on_message_data_callback (
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
GBytes * bytes,
gpointer user_data);
static void on_message_string_callback (
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gchar * string,
gpointer user_data);
static void on_open_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data);
static void ngw_native_rtc_data_channel_constructed(GObject * object)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(object);
g_object_connect(self->gstdatachannel,
"signal::on-buffered-amount-low", on_buffered_amount_low_callback, self,
"signal::on-close", on_close_callback, self,
"signal::on-error", on_error_callback, self,
"signal::on-message-data", on_message_data_callback, self,
"signal::on-message-string", on_message_string_callback, self,
"signal::on-open", on_open_callback, self,
NULL);
G_OBJECT_CLASS(ngw_native_rtc_data_channel_parent_class)->constructed(object);
}
static void ngw_native_rtc_data_channel_dispose(GObject * object)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(object);
g_signal_handlers_disconnect_by_data(self->gstdatachannel, self);
g_clear_object(&self->gstdatachannel);
}
static int ngw_native_rtc_data_channel_on_buffered_amount_low(gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_signal_emit(self, datachannel_signals[SIGNAL_ON_BUFFERED_AMOUNT_LOW], 0);
g_object_unref(self);
return G_SOURCE_REMOVE;
}
static void on_buffered_amount_low_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_idle_add(
ngw_native_rtc_data_channel_on_buffered_amount_low,
g_object_ref(self));
}
static int ngw_native_rtc_data_channel_on_close(gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_signal_emit(self, datachannel_signals[SIGNAL_ON_CLOSE], 0, NULL);
g_object_unref(self);
return G_SOURCE_REMOVE;
}
static void on_close_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_idle_add(
ngw_native_rtc_data_channel_on_close,
g_object_ref(self));
}
struct OnErrorData {
NgwNativeRTCDataChannel * self;
GError * error;
};
static int ngw_native_rtc_data_channel_on_error(gpointer user_data)
{
struct OnErrorData * data = user_data;
g_signal_emit(data->self,
datachannel_signals[SIGNAL_ON_ERROR], 0, data->error);
g_error_free(data->error);
g_object_unref(data->self);
g_free(data);
return G_SOURCE_REMOVE;
}
static void on_error_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
GError * error,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
struct OnErrorData * data = g_new(struct OnErrorData, 1);
*data = (struct OnErrorData) {
.self = g_object_ref(self),
.error = g_error_copy(error),
};
g_idle_add(ngw_native_rtc_data_channel_on_error, data);
}
struct OnMessageDataData {
NgwNativeRTCDataChannel * self;
GBytes * bytes;
};
static int ngw_native_rtc_data_channel_on_message_data(gpointer user_data)
{
struct OnMessageDataData * data = user_data;
g_signal_emit(data->self,
datachannel_signals[SIGNAL_ON_MESSAGE_DATA], 0, data->bytes);
g_bytes_unref(data->bytes);
g_object_unref(data->self);
g_free(data);
return G_SOURCE_REMOVE;
}
static void on_message_data_callback (
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
GBytes * bytes,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
struct OnMessageDataData * data = g_new(struct OnMessageDataData, 1);
*data = (struct OnMessageDataData) {
.self = g_object_ref(self),
.bytes = g_bytes_ref(bytes),
};
g_idle_add(ngw_native_rtc_data_channel_on_message_data, data);
}
struct OnMessageStringData {
NgwNativeRTCDataChannel * self;
gchar * string;
};
static int ngw_native_rtc_data_channel_on_message_string(gpointer user_data)
{
struct OnMessageStringData * data = user_data;
g_signal_emit(data->self,
datachannel_signals[SIGNAL_ON_MESSAGE_STRING], 0, data->string);
g_free(data->string);
g_object_unref(data->self);
g_free(data);
return G_SOURCE_REMOVE;
}
static void on_message_string_callback (
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gchar * string,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
struct OnMessageStringData * data = g_new(struct OnMessageStringData, 1);
*data = (struct OnMessageStringData) {
.self = g_object_ref(self),
.string = g_strdup(string),
};
g_idle_add(ngw_native_rtc_data_channel_on_message_string, data);
}
static int ngw_native_rtc_data_channel_on_open(gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_signal_emit (self, datachannel_signals[SIGNAL_ON_OPEN], 0, NULL);
g_object_unref(self);
return G_SOURCE_REMOVE;
}
static void on_open_callback(
GstWebRTCDataChannel * datachannel G_GNUC_UNUSED,
gpointer user_data)
{
NgwNativeRTCDataChannel * self = NGW_NATIVE_RTC_DATA_CHANNEL(user_data);
g_idle_add(
ngw_native_rtc_data_channel_on_open,
g_object_ref(self));
}
/**
* ngw_native_rtc_data_channel_new
* @gstdatachannel: underlying GstWebRTCDataChannel
*
* Returns: (transfer full): new instance
*/
NgwNativeRTCDataChannel * ngw_native_rtc_data_channel_new(
GstWebRTCDataChannel * gstdatachannel
)
{
return g_object_new(
NGW_NATIVE_RTC_DATA_CHANNEL_TYPE,
"gst-data-channel", gstdatachannel,
NULL
);
}