2 * Copyright (C) 2018 Matthew Waters <matthew@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:gstwebrtc-datachannel
22 * @short_description: RTCDataChannel object
23 * @title: GstWebRTCDataChannel
24 * @see_also: #GstWebRTCRTPTransceiver
26 * <http://w3c.github.io/webrtc-pc/#dom-rtcsctptransport>
33 #include "webrtcdatachannel.h"
34 #include <gst/app/gstappsink.h>
35 #include <gst/app/gstappsrc.h>
36 #include <gst/base/gstbytereader.h>
37 #include <gst/base/gstbytewriter.h>
38 #include <gst/sctp/sctpreceivemeta.h>
39 #include <gst/sctp/sctpsendmeta.h>
41 #include "gstwebrtcbin.h"
44 #define GST_CAT_DEFAULT webrtc_data_channel_debug
45 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
47 #define webrtc_data_channel_parent_class parent_class
48 G_DEFINE_TYPE_WITH_CODE (WebRTCDataChannel, webrtc_data_channel,
49 GST_TYPE_WEBRTC_DATA_CHANNEL,
50 GST_DEBUG_CATEGORY_INIT (webrtc_data_channel_debug, "webrtcdatachannel", 0,
51 "webrtcdatachannel"););
53 G_LOCK_DEFINE_STATIC (outstanding_channels_lock);
54 static GList *outstanding_channels;
58 DATA_CHANNEL_PPID_WEBRTC_CONTROL = 50,
59 DATA_CHANNEL_PPID_WEBRTC_STRING = 51,
60 DATA_CHANNEL_PPID_WEBRTC_BINARY_PARTIAL = 52, /* deprecated */
61 DATA_CHANNEL_PPID_WEBRTC_BINARY = 53,
62 DATA_CHANNEL_PPID_WEBRTC_STRING_PARTIAL = 54, /* deprecated */
63 DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY = 56,
64 DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY = 57,
69 CHANNEL_TYPE_RELIABLE = 0x00,
70 CHANNEL_TYPE_RELIABLE_UNORDERED = 0x80,
71 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT = 0x01,
72 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT_UNORDERED = 0x81,
73 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED = 0x02,
74 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED = 0x82,
75 } DataChannelReliabilityType;
79 CHANNEL_MESSAGE_ACK = 0x02,
80 CHANNEL_MESSAGE_OPEN = 0x03,
84 priority_type_to_uint (GstWebRTCPriorityType pri)
87 case GST_WEBRTC_PRIORITY_TYPE_VERY_LOW:
89 case GST_WEBRTC_PRIORITY_TYPE_LOW:
91 case GST_WEBRTC_PRIORITY_TYPE_MEDIUM:
93 case GST_WEBRTC_PRIORITY_TYPE_HIGH:
96 g_assert_not_reached ();
100 static GstWebRTCPriorityType
101 priority_uint_to_type (guint16 val)
104 return GST_WEBRTC_PRIORITY_TYPE_VERY_LOW;
106 return GST_WEBRTC_PRIORITY_TYPE_LOW;
108 return GST_WEBRTC_PRIORITY_TYPE_MEDIUM;
109 return GST_WEBRTC_PRIORITY_TYPE_HIGH;
113 construct_open_packet (WebRTCDataChannel * channel)
116 gsize label_len = strlen (channel->parent.label);
117 gsize proto_len = strlen (channel->parent.protocol);
118 gsize size = 12 + label_len + proto_len;
119 DataChannelReliabilityType reliability = 0;
120 guint32 reliability_param = 0;
126 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
127 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 * | Message Type | Channel Type | Priority |
129 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * | Reliability Parameter |
131 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * | Label Length | Protocol Length |
133 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 gst_byte_writer_init_with_size (&w, size, FALSE);
146 if (!gst_byte_writer_put_uint8 (&w, (guint8) CHANNEL_MESSAGE_OPEN))
147 g_return_val_if_reached (NULL);
149 if (!channel->parent.ordered)
151 if (channel->parent.max_retransmits != -1) {
153 reliability_param = channel->parent.max_retransmits;
155 if (channel->parent.max_packet_lifetime != -1) {
157 reliability_param = channel->parent.max_packet_lifetime;
160 priority = priority_type_to_uint (channel->parent.priority);
162 if (!gst_byte_writer_put_uint8 (&w, (guint8) reliability))
163 g_return_val_if_reached (NULL);
164 if (!gst_byte_writer_put_uint16_be (&w, (guint16) priority))
165 g_return_val_if_reached (NULL);
166 if (!gst_byte_writer_put_uint32_be (&w, (guint32) reliability_param))
167 g_return_val_if_reached (NULL);
168 if (!gst_byte_writer_put_uint16_be (&w, (guint16) label_len))
169 g_return_val_if_reached (NULL);
170 if (!gst_byte_writer_put_uint16_be (&w, (guint16) proto_len))
171 g_return_val_if_reached (NULL);
172 if (!gst_byte_writer_put_data (&w, (guint8 *) channel->parent.label,
174 g_return_val_if_reached (NULL);
175 if (!gst_byte_writer_put_data (&w, (guint8 *) channel->parent.protocol,
177 g_return_val_if_reached (NULL);
179 buf = gst_byte_writer_reset_and_get_buffer (&w);
181 /* send reliable and ordered */
182 gst_sctp_buffer_add_send_meta (buf, DATA_CHANNEL_PPID_WEBRTC_CONTROL, TRUE,
183 GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE, 0);
189 construct_ack_packet (WebRTCDataChannel * channel)
196 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202 gst_byte_writer_init_with_size (&w, 1, FALSE);
204 if (!gst_byte_writer_put_uint8 (&w, (guint8) CHANNEL_MESSAGE_ACK))
205 g_return_val_if_reached (NULL);
207 buf = gst_byte_writer_reset_and_get_buffer (&w);
209 /* send reliable and ordered */
210 gst_sctp_buffer_add_send_meta (buf, DATA_CHANNEL_PPID_WEBRTC_CONTROL, TRUE,
211 GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE, 0);
216 typedef void (*ChannelTask) (GstWebRTCDataChannel * channel,
221 GstWebRTCDataChannel *channel;
224 GDestroyNotify notify;
227 static GstStructure *
228 _execute_task (GstWebRTCBin * webrtc, struct task *task)
231 task->func (task->channel, task->user_data);
237 _free_task (struct task *task)
239 gst_object_unref (task->channel);
242 task->notify (task->user_data);
247 _channel_enqueue_task (WebRTCDataChannel * channel, ChannelTask func,
248 gpointer user_data, GDestroyNotify notify)
250 struct task *task = g_new0 (struct task, 1);
252 task->channel = gst_object_ref (channel);
254 task->user_data = user_data;
255 task->notify = notify;
257 gst_webrtc_bin_enqueue_task (channel->webrtcbin,
258 (GstWebRTCBinFunc) _execute_task, task, (GDestroyNotify) _free_task,
263 _channel_store_error (WebRTCDataChannel * channel, GError * error)
265 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
267 GST_WARNING_OBJECT (channel, "Error: %s",
268 error ? error->message : "Unknown");
269 if (!channel->stored_error)
270 channel->stored_error = error;
272 g_clear_error (&error);
274 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
278 _emit_on_open (WebRTCDataChannel * channel, gpointer user_data)
280 gst_webrtc_data_channel_on_open (GST_WEBRTC_DATA_CHANNEL (channel));
284 _transport_closed (WebRTCDataChannel * channel)
287 gboolean both_sides_closed;
289 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
290 error = channel->stored_error;
291 channel->stored_error = NULL;
294 channel->peer_closed && channel->parent.buffered_amount <= 0;
295 if (both_sides_closed || error) {
296 channel->peer_closed = FALSE;
298 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
301 gst_webrtc_data_channel_on_error (GST_WEBRTC_DATA_CHANNEL (channel), error);
302 g_clear_error (&error);
304 if (both_sides_closed || error) {
305 gst_webrtc_data_channel_on_close (GST_WEBRTC_DATA_CHANNEL (channel));
310 _close_sctp_stream (WebRTCDataChannel * channel, gpointer user_data)
314 GST_INFO_OBJECT (channel, "Closing outgoing SCTP stream %i label \"%s\"",
315 channel->parent.id, channel->parent.label);
317 pad = gst_element_get_static_pad (channel->appsrc, "src");
318 peer = gst_pad_get_peer (pad);
319 gst_object_unref (pad);
322 GstElement *sctpenc = gst_pad_get_parent_element (peer);
325 gst_element_release_request_pad (sctpenc, peer);
326 gst_object_unref (sctpenc);
328 gst_object_unref (peer);
331 _transport_closed (channel);
335 _close_procedure (WebRTCDataChannel * channel, gpointer user_data)
337 /* https://www.w3.org/TR/webrtc/#data-transport-closing-procedure */
338 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
339 if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_CLOSED) {
340 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
342 } else if (channel->parent.ready_state ==
343 GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING) {
344 _channel_enqueue_task (channel, (ChannelTask) _transport_closed, NULL,
346 } else if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_OPEN) {
347 channel->parent.ready_state = GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING;
348 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
349 g_object_notify (G_OBJECT (channel), "ready-state");
351 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
352 if (channel->parent.buffered_amount <= 0) {
353 _channel_enqueue_task (channel, (ChannelTask) _close_sctp_stream,
358 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
362 _on_sctp_stream_reset (WebRTCSCTPTransport * sctp, guint stream_id,
363 WebRTCDataChannel * channel)
365 if (channel->parent.id == stream_id) {
366 GST_INFO_OBJECT (channel,
367 "Received channel close for SCTP stream %i label \"%s\"",
368 channel->parent.id, channel->parent.label);
370 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
371 channel->peer_closed = TRUE;
372 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
374 _channel_enqueue_task (channel, (ChannelTask) _close_procedure,
375 GUINT_TO_POINTER (stream_id), NULL);
380 webrtc_data_channel_close (GstWebRTCDataChannel * channel)
382 _close_procedure (WEBRTC_DATA_CHANNEL (channel), NULL);
386 _parse_control_packet (WebRTCDataChannel * channel, guint8 * data,
387 gsize size, GError ** error)
395 g_return_val_if_reached (GST_FLOW_ERROR);
397 g_return_val_if_reached (GST_FLOW_ERROR);
399 gst_byte_reader_init (&r, data, size);
401 if (!gst_byte_reader_get_uint8 (&r, &message_type))
402 g_return_val_if_reached (GST_FLOW_ERROR);
404 if (message_type == CHANNEL_MESSAGE_ACK) {
406 GST_INFO_OBJECT (channel, "Received channel ack");
408 } else if (message_type == CHANNEL_MESSAGE_OPEN) {
410 guint32 reliability_param;
411 guint16 priority, label_len, proto_len;
416 GST_INFO_OBJECT (channel, "Received channel open");
418 if (channel->parent.negotiated) {
419 g_set_error (error, GST_WEBRTC_ERROR,
420 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
421 "Data channel was signalled as negotiated already");
422 g_return_val_if_reached (GST_FLOW_ERROR);
428 if (!gst_byte_reader_get_uint8 (&r, &reliability))
430 if (!gst_byte_reader_get_uint16_be (&r, &priority))
432 if (!gst_byte_reader_get_uint32_be (&r, &reliability_param))
434 if (!gst_byte_reader_get_uint16_be (&r, &label_len))
436 if (!gst_byte_reader_get_uint16_be (&r, &proto_len))
439 label = g_new0 (gchar, (gsize) label_len + 1);
440 proto = g_new0 (gchar, (gsize) proto_len + 1);
442 if (!gst_byte_reader_get_data (&r, label_len, &src))
444 memcpy (label, src, label_len);
445 label[label_len] = '\0';
446 if (!gst_byte_reader_get_data (&r, proto_len, &src))
448 memcpy (proto, src, proto_len);
449 proto[proto_len] = '\0';
451 g_free (channel->parent.label);
452 channel->parent.label = label;
453 g_free (channel->parent.protocol);
454 channel->parent.protocol = proto;
455 channel->parent.priority = priority_uint_to_type (priority);
456 channel->parent.ordered = !(reliability & 0x80);
457 if (reliability & 0x01) {
458 channel->parent.max_retransmits = reliability_param;
459 channel->parent.max_packet_lifetime = -1;
460 } else if (reliability & 0x02) {
461 channel->parent.max_retransmits = -1;
462 channel->parent.max_packet_lifetime = reliability_param;
464 channel->parent.max_retransmits = -1;
465 channel->parent.max_packet_lifetime = -1;
467 channel->opened = TRUE;
469 GST_INFO_OBJECT (channel, "Received channel open for SCTP stream %i "
470 "label \"%s\" protocol %s ordered %s", channel->parent.id,
471 channel->parent.label, channel->parent.protocol,
472 channel->parent.ordered ? "true" : "false");
474 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
476 GST_INFO_OBJECT (channel, "Sending channel ack");
477 buffer = construct_ack_packet (channel);
479 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
480 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
481 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
483 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
484 if (ret != GST_FLOW_OK) {
485 g_set_error (error, GST_WEBRTC_ERROR,
486 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE, "Could not send ack packet");
492 g_set_error (error, GST_WEBRTC_ERROR,
493 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
494 "Unknown message type in control protocol");
495 return GST_FLOW_ERROR;
502 g_set_error (error, GST_WEBRTC_ERROR,
503 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE, "Failed to parse packet");
504 g_return_val_if_reached (GST_FLOW_ERROR);
509 on_sink_eos (GstAppSink * sink, gpointer user_data)
520 buffer_unmap_and_unref (struct map_info *info)
522 gst_buffer_unmap (info->buffer, &info->map_info);
523 gst_buffer_unref (info->buffer);
528 _emit_have_data (WebRTCDataChannel * channel, GBytes * data)
530 gst_webrtc_data_channel_on_message_data (GST_WEBRTC_DATA_CHANNEL (channel),
535 _emit_have_string (GstWebRTCDataChannel * channel, gchar * str)
537 gst_webrtc_data_channel_on_message_string (GST_WEBRTC_DATA_CHANNEL (channel),
542 _data_channel_have_sample (WebRTCDataChannel * channel, GstSample * sample,
545 GstSctpReceiveMeta *receive;
547 GstFlowReturn ret = GST_FLOW_OK;
549 GST_LOG_OBJECT (channel, "Received sample %" GST_PTR_FORMAT, sample);
551 g_return_val_if_fail (channel->sctp_transport != NULL, GST_FLOW_ERROR);
553 buffer = gst_sample_get_buffer (sample);
555 g_set_error (error, GST_WEBRTC_ERROR,
556 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE, "No buffer to handle");
557 return GST_FLOW_ERROR;
559 receive = gst_sctp_buffer_get_receive_meta (buffer);
561 g_set_error (error, GST_WEBRTC_ERROR,
562 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
563 "No SCTP Receive meta on the buffer");
564 return GST_FLOW_ERROR;
567 switch (receive->ppid) {
568 case DATA_CHANNEL_PPID_WEBRTC_CONTROL:{
569 GstMapInfo info = GST_MAP_INFO_INIT;
570 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
571 g_set_error (error, GST_WEBRTC_ERROR,
572 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
573 "Failed to map received buffer");
574 ret = GST_FLOW_ERROR;
576 ret = _parse_control_packet (channel, info.data, info.size, error);
577 gst_buffer_unmap (buffer, &info);
581 case DATA_CHANNEL_PPID_WEBRTC_STRING:
582 case DATA_CHANNEL_PPID_WEBRTC_STRING_PARTIAL:{
583 GstMapInfo info = GST_MAP_INFO_INIT;
584 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
585 g_set_error (error, GST_WEBRTC_ERROR,
586 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
587 "Failed to map received buffer");
588 ret = GST_FLOW_ERROR;
590 gchar *str = g_strndup ((gchar *) info.data, info.size);
591 _channel_enqueue_task (channel, (ChannelTask) _emit_have_string, str,
593 gst_buffer_unmap (buffer, &info);
597 case DATA_CHANNEL_PPID_WEBRTC_BINARY:
598 case DATA_CHANNEL_PPID_WEBRTC_BINARY_PARTIAL:{
599 struct map_info *info = g_new0 (struct map_info, 1);
600 if (!gst_buffer_map (buffer, &info->map_info, GST_MAP_READ)) {
601 g_set_error (error, GST_WEBRTC_ERROR,
602 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
603 "Failed to map received buffer");
604 ret = GST_FLOW_ERROR;
606 GBytes *data = g_bytes_new_with_free_func (info->map_info.data,
607 info->map_info.size, (GDestroyNotify) buffer_unmap_and_unref, info);
608 info->buffer = gst_buffer_ref (buffer);
609 _channel_enqueue_task (channel, (ChannelTask) _emit_have_data, data,
610 (GDestroyNotify) g_bytes_unref);
614 case DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY:
615 _channel_enqueue_task (channel, (ChannelTask) _emit_have_data, NULL,
618 case DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY:
619 _channel_enqueue_task (channel, (ChannelTask) _emit_have_string, NULL,
623 g_set_error (error, GST_WEBRTC_ERROR,
624 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
625 "Unknown SCTP PPID %u received", receive->ppid);
626 ret = GST_FLOW_ERROR;
634 on_sink_preroll (GstAppSink * sink, gpointer user_data)
636 WebRTCDataChannel *channel = user_data;
637 GstSample *sample = gst_app_sink_pull_preroll (sink);
641 /* This sample also seems to be provided by the sample callback
642 ret = _data_channel_have_sample (channel, sample); */
644 gst_sample_unref (sample);
645 } else if (gst_app_sink_is_eos (sink)) {
648 ret = GST_FLOW_ERROR;
651 if (ret != GST_FLOW_OK) {
652 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
659 on_sink_sample (GstAppSink * sink, gpointer user_data)
661 WebRTCDataChannel *channel = user_data;
662 GstSample *sample = gst_app_sink_pull_sample (sink);
664 GError *error = NULL;
667 ret = _data_channel_have_sample (channel, sample, &error);
668 gst_sample_unref (sample);
669 } else if (gst_app_sink_is_eos (sink)) {
672 ret = GST_FLOW_ERROR;
676 _channel_store_error (channel, error);
678 if (ret != GST_FLOW_OK) {
679 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
685 static GstAppSinkCallbacks sink_callbacks = {
692 webrtc_data_channel_start_negotiation (WebRTCDataChannel * channel)
696 g_return_if_fail (!channel->parent.negotiated);
697 g_return_if_fail (channel->parent.id != -1);
698 g_return_if_fail (channel->sctp_transport != NULL);
700 buffer = construct_open_packet (channel);
702 GST_INFO_OBJECT (channel, "Sending channel open for SCTP stream %i "
703 "label \"%s\" protocol %s ordered %s", channel->parent.id,
704 channel->parent.label, channel->parent.protocol,
705 channel->parent.ordered ? "true" : "false");
707 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
708 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
709 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
710 g_object_notify (G_OBJECT (&channel->parent), "buffered-amount");
712 if (gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc),
713 buffer) == GST_FLOW_OK) {
714 channel->opened = TRUE;
715 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
717 GError *error = NULL;
718 g_set_error (&error, GST_WEBRTC_ERROR,
719 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
720 "Failed to send DCEP open packet");
721 _channel_store_error (channel, error);
722 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
727 _get_sctp_reliability (WebRTCDataChannel * channel,
728 GstSctpSendMetaPartiallyReliability * reliability, guint * rel_param)
730 if (channel->parent.max_retransmits != -1) {
731 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_RTX;
732 *rel_param = channel->parent.max_retransmits;
733 } else if (channel->parent.max_packet_lifetime != -1) {
734 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_TTL;
735 *rel_param = channel->parent.max_packet_lifetime;
737 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE;
743 _is_within_max_message_size (WebRTCDataChannel * channel, gsize size)
745 return size <= channel->sctp_transport->max_message_size;
749 webrtc_data_channel_send_data (GstWebRTCDataChannel * base_channel,
752 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (base_channel);
753 GstSctpSendMetaPartiallyReliability reliability;
760 buffer = gst_buffer_new ();
761 ppid = DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY;
766 data = (guint8 *) g_bytes_get_data (bytes, &size);
767 g_return_if_fail (data != NULL);
768 if (!_is_within_max_message_size (channel, size)) {
769 GError *error = NULL;
770 g_set_error (&error, GST_WEBRTC_ERROR,
771 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
772 "Requested to send data that is too large");
773 _channel_store_error (channel, error);
774 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL,
779 buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, data, size,
780 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
781 ppid = DATA_CHANNEL_PPID_WEBRTC_BINARY;
784 _get_sctp_reliability (channel, &reliability, &rel_param);
785 gst_sctp_buffer_add_send_meta (buffer, ppid, channel->parent.ordered,
786 reliability, rel_param);
788 GST_LOG_OBJECT (channel, "Sending data using buffer %" GST_PTR_FORMAT,
791 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
792 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
793 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
794 g_object_notify (G_OBJECT (&channel->parent), "buffered-amount");
796 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
798 if (ret != GST_FLOW_OK) {
799 GError *error = NULL;
800 g_set_error (&error, GST_WEBRTC_ERROR,
801 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE, "Failed to send data");
802 _channel_store_error (channel, error);
803 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
808 webrtc_data_channel_send_string (GstWebRTCDataChannel * base_channel,
811 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (base_channel);
812 GstSctpSendMetaPartiallyReliability reliability;
818 if (!channel->parent.negotiated)
819 g_return_if_fail (channel->opened);
820 g_return_if_fail (channel->sctp_transport != NULL);
823 buffer = gst_buffer_new ();
824 ppid = DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY;
826 gsize size = strlen (str);
829 if (!_is_within_max_message_size (channel, size)) {
830 GError *error = NULL;
831 g_set_error (&error, GST_WEBRTC_ERROR,
832 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE,
833 "Requested to send a string that is too large");
834 _channel_store_error (channel, error);
835 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL,
840 str_copy = g_strdup (str);
842 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, str_copy,
843 size, 0, size, str_copy, g_free);
844 ppid = DATA_CHANNEL_PPID_WEBRTC_STRING;
847 _get_sctp_reliability (channel, &reliability, &rel_param);
848 gst_sctp_buffer_add_send_meta (buffer, ppid, channel->parent.ordered,
849 reliability, rel_param);
851 GST_TRACE_OBJECT (channel, "Sending string using buffer %" GST_PTR_FORMAT,
854 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
855 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
856 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
857 g_object_notify (G_OBJECT (&channel->parent), "buffered-amount");
859 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
861 if (ret != GST_FLOW_OK) {
862 GError *error = NULL;
863 g_set_error (&error, GST_WEBRTC_ERROR,
864 GST_WEBRTC_ERROR_DATA_CHANNEL_FAILURE, "Failed to send string");
865 _channel_store_error (channel, error);
866 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
871 _on_sctp_notify_state_unlocked (GObject * sctp_transport,
872 WebRTCDataChannel * channel)
874 GstWebRTCSCTPTransportState state;
876 g_object_get (sctp_transport, "state", &state, NULL);
877 if (state == GST_WEBRTC_SCTP_TRANSPORT_STATE_CONNECTED) {
878 if (channel->parent.negotiated)
879 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
883 static WebRTCDataChannel *
884 ensure_channel_alive (WebRTCDataChannel * channel)
886 /* ghetto impl of, does the channel still exist?.
887 * Needed because g_signal_handler_disconnect*() will not disconnect any
888 * running functions and _finalize() implementation can complete and
889 * invalidate channel */
890 G_LOCK (outstanding_channels_lock);
891 if (g_list_find (outstanding_channels, channel)) {
892 g_object_ref (channel);
894 G_UNLOCK (outstanding_channels_lock);
897 G_UNLOCK (outstanding_channels_lock);
903 _on_sctp_notify_state (GObject * sctp_transport, GParamSpec * pspec,
904 WebRTCDataChannel * channel)
906 if (!(channel = ensure_channel_alive (channel)))
909 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
910 _on_sctp_notify_state_unlocked (sctp_transport, channel);
911 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
913 g_object_unref (channel);
917 _emit_low_threshold (WebRTCDataChannel * channel, gpointer user_data)
919 gst_webrtc_data_channel_on_buffered_amount_low (GST_WEBRTC_DATA_CHANNEL
923 static GstPadProbeReturn
924 on_appsrc_data (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
926 WebRTCDataChannel *channel = user_data;
930 if (GST_PAD_PROBE_INFO_TYPE (info) & (GST_PAD_PROBE_TYPE_BUFFER)) {
931 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
932 size = gst_buffer_get_size (buffer);
933 } else if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
934 GstBufferList *list = GST_PAD_PROBE_INFO_BUFFER_LIST (info);
935 size = gst_buffer_list_calculate_size (list);
939 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
940 prev_amount = channel->parent.buffered_amount;
941 channel->parent.buffered_amount -= size;
942 GST_TRACE_OBJECT (channel, "checking low-threshold: prev %"
943 G_GUINT64_FORMAT " low-threshold %" G_GUINT64_FORMAT " buffered %"
944 G_GUINT64_FORMAT, prev_amount,
945 channel->parent.buffered_amount_low_threshold,
946 channel->parent.buffered_amount);
947 if (prev_amount >= channel->parent.buffered_amount_low_threshold
948 && channel->parent.buffered_amount <
949 channel->parent.buffered_amount_low_threshold) {
950 _channel_enqueue_task (channel, (ChannelTask) _emit_low_threshold, NULL,
954 if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING
955 && channel->parent.buffered_amount <= 0) {
956 _channel_enqueue_task (channel, (ChannelTask) _close_sctp_stream, NULL,
959 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
960 g_object_notify (G_OBJECT (&channel->parent), "buffered-amount");
963 return GST_PAD_PROBE_OK;
967 gst_webrtc_data_channel_constructed (GObject * object)
969 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (object);
973 caps = gst_caps_new_any ();
975 channel->appsrc = gst_element_factory_make ("appsrc", NULL);
976 gst_object_ref_sink (channel->appsrc);
977 pad = gst_element_get_static_pad (channel->appsrc, "src");
979 channel->src_probe = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_BOTH,
980 (GstPadProbeCallback) on_appsrc_data, channel, NULL);
982 channel->appsink = gst_element_factory_make ("appsink", NULL);
983 gst_object_ref_sink (channel->appsink);
984 g_object_set (channel->appsink, "sync", FALSE, "async", FALSE, "caps", caps,
986 gst_app_sink_set_callbacks (GST_APP_SINK (channel->appsink), &sink_callbacks,
989 gst_object_unref (pad);
990 gst_caps_unref (caps);
994 gst_webrtc_data_channel_dispose (GObject * object)
996 G_LOCK (outstanding_channels_lock);
997 outstanding_channels = g_list_remove (outstanding_channels, object);
998 G_UNLOCK (outstanding_channels_lock);
1000 G_OBJECT_CLASS (parent_class)->dispose (object);
1004 gst_webrtc_data_channel_finalize (GObject * object)
1006 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (object);
1008 if (channel->src_probe) {
1009 GstPad *pad = gst_element_get_static_pad (channel->appsrc, "src");
1010 gst_pad_remove_probe (pad, channel->src_probe);
1011 gst_object_unref (pad);
1012 channel->src_probe = 0;
1015 if (channel->sctp_transport)
1016 g_signal_handlers_disconnect_by_data (channel->sctp_transport, channel);
1017 g_clear_object (&channel->sctp_transport);
1019 g_clear_object (&channel->appsrc);
1020 g_clear_object (&channel->appsink);
1022 G_OBJECT_CLASS (parent_class)->finalize (object);
1026 webrtc_data_channel_class_init (WebRTCDataChannelClass * klass)
1028 GObjectClass *gobject_class = (GObjectClass *) klass;
1029 GstWebRTCDataChannelClass *channel_class =
1030 (GstWebRTCDataChannelClass *) klass;
1032 gobject_class->constructed = gst_webrtc_data_channel_constructed;
1033 gobject_class->dispose = gst_webrtc_data_channel_dispose;
1034 gobject_class->finalize = gst_webrtc_data_channel_finalize;
1036 channel_class->send_data = webrtc_data_channel_send_data;
1037 channel_class->send_string = webrtc_data_channel_send_string;
1038 channel_class->close = webrtc_data_channel_close;
1042 webrtc_data_channel_init (WebRTCDataChannel * channel)
1044 G_LOCK (outstanding_channels_lock);
1045 outstanding_channels = g_list_prepend (outstanding_channels, channel);
1046 G_UNLOCK (outstanding_channels_lock);
1050 _data_channel_set_sctp_transport (WebRTCDataChannel * channel,
1051 WebRTCSCTPTransport * sctp)
1053 g_return_if_fail (GST_IS_WEBRTC_DATA_CHANNEL (channel));
1054 g_return_if_fail (GST_IS_WEBRTC_SCTP_TRANSPORT (sctp));
1056 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
1057 if (channel->sctp_transport)
1058 g_signal_handlers_disconnect_by_data (channel->sctp_transport, channel);
1059 GST_TRACE ("%p set sctp %p", channel, sctp);
1061 gst_object_replace ((GstObject **) & channel->sctp_transport,
1065 g_signal_connect (sctp, "stream-reset", G_CALLBACK (_on_sctp_stream_reset),
1067 g_signal_connect (sctp, "notify::state", G_CALLBACK (_on_sctp_notify_state),
1070 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
1074 webrtc_data_channel_link_to_sctp (WebRTCDataChannel * channel,
1075 WebRTCSCTPTransport * sctp_transport)
1077 if (sctp_transport && !channel->sctp_transport) {
1080 g_object_get (channel, "id", &id, NULL);
1082 if (sctp_transport->association_established && id != -1) {
1085 _data_channel_set_sctp_transport (channel, sctp_transport);
1086 pad_name = g_strdup_printf ("sink_%u", id);
1087 if (!gst_element_link_pads (channel->appsrc, "src",
1088 channel->sctp_transport->sctpenc, pad_name))
1089 g_warn_if_reached ();
1092 _on_sctp_notify_state_unlocked (G_OBJECT (sctp_transport), channel);