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"););
55 DATA_CHANNEL_PPID_WEBRTC_CONTROL = 50,
56 DATA_CHANNEL_PPID_WEBRTC_STRING = 51,
57 DATA_CHANNEL_PPID_WEBRTC_BINARY_PARTIAL = 52, /* deprecated */
58 DATA_CHANNEL_PPID_WEBRTC_BINARY = 53,
59 DATA_CHANNEL_PPID_WEBRTC_STRING_PARTIAL = 54, /* deprecated */
60 DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY = 56,
61 DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY = 57,
66 CHANNEL_TYPE_RELIABLE = 0x00,
67 CHANNEL_TYPE_RELIABLE_UNORDERED = 0x80,
68 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT = 0x01,
69 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT_UNORDERED = 0x81,
70 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED = 0x02,
71 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED = 0x82,
72 } DataChannelReliabilityType;
76 CHANNEL_MESSAGE_ACK = 0x02,
77 CHANNEL_MESSAGE_OPEN = 0x03,
81 priority_type_to_uint (GstWebRTCPriorityType pri)
84 case GST_WEBRTC_PRIORITY_TYPE_VERY_LOW:
86 case GST_WEBRTC_PRIORITY_TYPE_LOW:
88 case GST_WEBRTC_PRIORITY_TYPE_MEDIUM:
90 case GST_WEBRTC_PRIORITY_TYPE_HIGH:
93 g_assert_not_reached ();
97 static GstWebRTCPriorityType
98 priority_uint_to_type (guint16 val)
101 return GST_WEBRTC_PRIORITY_TYPE_VERY_LOW;
103 return GST_WEBRTC_PRIORITY_TYPE_LOW;
105 return GST_WEBRTC_PRIORITY_TYPE_MEDIUM;
106 return GST_WEBRTC_PRIORITY_TYPE_HIGH;
110 construct_open_packet (WebRTCDataChannel * channel)
113 gsize label_len = strlen (channel->parent.label);
114 gsize proto_len = strlen (channel->parent.protocol);
115 gsize size = 12 + label_len + proto_len;
116 DataChannelReliabilityType reliability = 0;
117 guint32 reliability_param = 0;
123 * 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
124 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 * | Message Type | Channel Type | Priority |
126 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127 * | Reliability Parameter |
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * | Label Length | Protocol Length |
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 gst_byte_writer_init_with_size (&w, size, FALSE);
143 if (!gst_byte_writer_put_uint8 (&w, (guint8) CHANNEL_MESSAGE_OPEN))
144 g_return_val_if_reached (NULL);
146 if (!channel->parent.ordered)
148 if (channel->parent.max_retransmits != -1) {
150 reliability_param = channel->parent.max_retransmits;
152 if (channel->parent.max_packet_lifetime != -1) {
154 reliability_param = channel->parent.max_packet_lifetime;
157 priority = priority_type_to_uint (channel->parent.priority);
159 if (!gst_byte_writer_put_uint8 (&w, (guint8) reliability))
160 g_return_val_if_reached (NULL);
161 if (!gst_byte_writer_put_uint16_be (&w, (guint16) priority))
162 g_return_val_if_reached (NULL);
163 if (!gst_byte_writer_put_uint32_be (&w, (guint32) reliability_param))
164 g_return_val_if_reached (NULL);
165 if (!gst_byte_writer_put_uint16_be (&w, (guint16) label_len))
166 g_return_val_if_reached (NULL);
167 if (!gst_byte_writer_put_uint16_be (&w, (guint16) proto_len))
168 g_return_val_if_reached (NULL);
169 if (!gst_byte_writer_put_data (&w, (guint8 *) channel->parent.label,
171 g_return_val_if_reached (NULL);
172 if (!gst_byte_writer_put_data (&w, (guint8 *) channel->parent.protocol,
174 g_return_val_if_reached (NULL);
176 buf = gst_byte_writer_reset_and_get_buffer (&w);
178 /* send reliable and ordered */
179 gst_sctp_buffer_add_send_meta (buf, DATA_CHANNEL_PPID_WEBRTC_CONTROL, TRUE,
180 GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE, 0);
186 construct_ack_packet (WebRTCDataChannel * channel)
193 * 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
194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
199 gst_byte_writer_init_with_size (&w, 1, FALSE);
201 if (!gst_byte_writer_put_uint8 (&w, (guint8) CHANNEL_MESSAGE_ACK))
202 g_return_val_if_reached (NULL);
204 buf = gst_byte_writer_reset_and_get_buffer (&w);
206 /* send reliable and ordered */
207 gst_sctp_buffer_add_send_meta (buf, DATA_CHANNEL_PPID_WEBRTC_CONTROL, TRUE,
208 GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE, 0);
213 typedef void (*ChannelTask) (GstWebRTCDataChannel * channel,
218 GstWebRTCDataChannel *channel;
221 GDestroyNotify notify;
224 static GstStructure *
225 _execute_task (GstWebRTCBin * webrtc, struct task *task)
228 task->func (task->channel, task->user_data);
234 _free_task (struct task *task)
236 gst_object_unref (task->channel);
239 task->notify (task->user_data);
244 _channel_enqueue_task (WebRTCDataChannel * channel, ChannelTask func,
245 gpointer user_data, GDestroyNotify notify)
247 struct task *task = g_new0 (struct task, 1);
249 task->channel = gst_object_ref (channel);
251 task->user_data = user_data;
252 task->notify = notify;
254 gst_webrtc_bin_enqueue_task (channel->webrtcbin,
255 (GstWebRTCBinFunc) _execute_task, task, (GDestroyNotify) _free_task,
260 _channel_store_error (WebRTCDataChannel * channel, GError * error)
262 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
264 GST_WARNING_OBJECT (channel, "Error: %s",
265 error ? error->message : "Unknown");
266 if (!channel->stored_error)
267 channel->stored_error = error;
269 g_clear_error (&error);
271 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
275 _emit_on_open (WebRTCDataChannel * channel, gpointer user_data)
277 gst_webrtc_data_channel_on_open (GST_WEBRTC_DATA_CHANNEL (channel));
281 _transport_closed (WebRTCDataChannel * channel)
284 gboolean both_sides_closed;
286 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
287 error = channel->stored_error;
288 channel->stored_error = NULL;
291 channel->peer_closed && channel->parent.buffered_amount <= 0;
292 if (both_sides_closed || error) {
293 channel->peer_closed = FALSE;
295 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
298 gst_webrtc_data_channel_on_error (GST_WEBRTC_DATA_CHANNEL (channel), error);
299 g_clear_error (&error);
301 if (both_sides_closed || error) {
302 gst_webrtc_data_channel_on_close (GST_WEBRTC_DATA_CHANNEL (channel));
307 _close_sctp_stream (WebRTCDataChannel * channel, gpointer user_data)
311 GST_INFO_OBJECT (channel, "Closing outgoing SCTP stream %i label \"%s\"",
312 channel->parent.id, channel->parent.label);
314 pad = gst_element_get_static_pad (channel->appsrc, "src");
315 peer = gst_pad_get_peer (pad);
316 gst_object_unref (pad);
319 GstElement *sctpenc = gst_pad_get_parent_element (peer);
322 gst_element_release_request_pad (sctpenc, peer);
323 gst_object_unref (sctpenc);
325 gst_object_unref (peer);
328 _transport_closed (channel);
332 _close_procedure (WebRTCDataChannel * channel, gpointer user_data)
334 /* https://www.w3.org/TR/webrtc/#data-transport-closing-procedure */
335 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
336 if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_CLOSED) {
337 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
339 } else if (channel->parent.ready_state ==
340 GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING) {
341 _channel_enqueue_task (channel, (ChannelTask) _transport_closed, NULL,
343 } else if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_OPEN) {
344 channel->parent.ready_state = GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING;
345 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
346 g_object_notify (G_OBJECT (channel), "ready-state");
348 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
349 if (channel->parent.buffered_amount <= 0) {
350 _channel_enqueue_task (channel, (ChannelTask) _close_sctp_stream,
355 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
359 _on_sctp_stream_reset (WebRTCSCTPTransport * sctp, guint stream_id,
360 WebRTCDataChannel * channel)
362 if (channel->parent.id == stream_id) {
363 GST_INFO_OBJECT (channel,
364 "Received channel close for SCTP stream %i label \"%s\"",
365 channel->parent.id, channel->parent.label);
367 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
368 channel->peer_closed = TRUE;
369 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
371 _channel_enqueue_task (channel, (ChannelTask) _close_procedure,
372 GUINT_TO_POINTER (stream_id), NULL);
377 webrtc_data_channel_close (GstWebRTCDataChannel * channel)
379 _close_procedure (WEBRTC_DATA_CHANNEL (channel), NULL);
383 _parse_control_packet (WebRTCDataChannel * channel, guint8 * data,
384 gsize size, GError ** error)
392 g_return_val_if_reached (GST_FLOW_ERROR);
394 g_return_val_if_reached (GST_FLOW_ERROR);
396 gst_byte_reader_init (&r, data, size);
398 if (!gst_byte_reader_get_uint8 (&r, &message_type))
399 g_return_val_if_reached (GST_FLOW_ERROR);
401 if (message_type == CHANNEL_MESSAGE_ACK) {
403 GST_INFO_OBJECT (channel, "Received channel ack");
405 } else if (message_type == CHANNEL_MESSAGE_OPEN) {
407 guint32 reliability_param;
408 guint16 priority, label_len, proto_len;
413 GST_INFO_OBJECT (channel, "Received channel open");
415 if (channel->parent.negotiated) {
416 g_set_error (error, GST_WEBRTC_BIN_ERROR,
417 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
418 "Data channel was signalled as negotiated already");
419 g_return_val_if_reached (GST_FLOW_ERROR);
425 if (!gst_byte_reader_get_uint8 (&r, &reliability))
427 if (!gst_byte_reader_get_uint16_be (&r, &priority))
429 if (!gst_byte_reader_get_uint32_be (&r, &reliability_param))
431 if (!gst_byte_reader_get_uint16_be (&r, &label_len))
433 if (!gst_byte_reader_get_uint16_be (&r, &proto_len))
436 label = g_new0 (gchar, (gsize) label_len + 1);
437 proto = g_new0 (gchar, (gsize) proto_len + 1);
439 if (!gst_byte_reader_get_data (&r, label_len, &src))
441 memcpy (label, src, label_len);
442 label[label_len] = '\0';
443 if (!gst_byte_reader_get_data (&r, proto_len, &src))
445 memcpy (proto, src, proto_len);
446 proto[proto_len] = '\0';
448 g_free (channel->parent.label);
449 channel->parent.label = label;
450 g_free (channel->parent.protocol);
451 channel->parent.protocol = proto;
452 channel->parent.priority = priority_uint_to_type (priority);
453 channel->parent.ordered = !(reliability & 0x80);
454 if (reliability & 0x01) {
455 channel->parent.max_retransmits = reliability_param;
456 channel->parent.max_packet_lifetime = -1;
457 } else if (reliability & 0x02) {
458 channel->parent.max_retransmits = -1;
459 channel->parent.max_packet_lifetime = reliability_param;
461 channel->parent.max_retransmits = -1;
462 channel->parent.max_packet_lifetime = -1;
464 channel->opened = TRUE;
466 GST_INFO_OBJECT (channel, "Received channel open for SCTP stream %i "
467 "label \"%s\" protocol %s ordered %s", channel->parent.id,
468 channel->parent.label, channel->parent.protocol,
469 channel->parent.ordered ? "true" : "false");
471 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
473 GST_INFO_OBJECT (channel, "Sending channel ack");
474 buffer = construct_ack_packet (channel);
476 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
477 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
478 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
480 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
481 if (ret != GST_FLOW_OK) {
482 g_set_error (error, GST_WEBRTC_BIN_ERROR,
483 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
484 "Could not send ack packet");
490 g_set_error (error, GST_WEBRTC_BIN_ERROR,
491 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
492 "Unknown message type in control protocol");
493 return GST_FLOW_ERROR;
500 g_set_error (error, GST_WEBRTC_BIN_ERROR,
501 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE, "Failed to parse packet");
502 g_return_val_if_reached (GST_FLOW_ERROR);
507 on_sink_eos (GstAppSink * sink, gpointer user_data)
518 buffer_unmap_and_unref (struct map_info *info)
520 gst_buffer_unmap (info->buffer, &info->map_info);
521 gst_buffer_unref (info->buffer);
526 _emit_have_data (WebRTCDataChannel * channel, GBytes * data)
528 gst_webrtc_data_channel_on_message_data (GST_WEBRTC_DATA_CHANNEL (channel),
533 _emit_have_string (GstWebRTCDataChannel * channel, gchar * str)
535 gst_webrtc_data_channel_on_message_string (GST_WEBRTC_DATA_CHANNEL (channel),
540 _data_channel_have_sample (WebRTCDataChannel * channel, GstSample * sample,
543 GstSctpReceiveMeta *receive;
545 GstFlowReturn ret = GST_FLOW_OK;
547 GST_LOG_OBJECT (channel, "Received sample %" GST_PTR_FORMAT, sample);
549 g_return_val_if_fail (channel->sctp_transport != NULL, GST_FLOW_ERROR);
551 buffer = gst_sample_get_buffer (sample);
553 g_set_error (error, GST_WEBRTC_BIN_ERROR,
554 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE, "No buffer to handle");
555 return GST_FLOW_ERROR;
557 receive = gst_sctp_buffer_get_receive_meta (buffer);
559 g_set_error (error, GST_WEBRTC_BIN_ERROR,
560 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
561 "No SCTP Receive meta on the buffer");
562 return GST_FLOW_ERROR;
565 switch (receive->ppid) {
566 case DATA_CHANNEL_PPID_WEBRTC_CONTROL:{
567 GstMapInfo info = GST_MAP_INFO_INIT;
568 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
569 g_set_error (error, GST_WEBRTC_BIN_ERROR,
570 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
571 "Failed to map received buffer");
572 ret = GST_FLOW_ERROR;
574 ret = _parse_control_packet (channel, info.data, info.size, error);
575 gst_buffer_unmap (buffer, &info);
579 case DATA_CHANNEL_PPID_WEBRTC_STRING:
580 case DATA_CHANNEL_PPID_WEBRTC_STRING_PARTIAL:{
581 GstMapInfo info = GST_MAP_INFO_INIT;
582 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
583 g_set_error (error, GST_WEBRTC_BIN_ERROR,
584 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
585 "Failed to map received buffer");
586 ret = GST_FLOW_ERROR;
588 gchar *str = g_strndup ((gchar *) info.data, info.size);
589 _channel_enqueue_task (channel, (ChannelTask) _emit_have_string, str,
591 gst_buffer_unmap (buffer, &info);
595 case DATA_CHANNEL_PPID_WEBRTC_BINARY:
596 case DATA_CHANNEL_PPID_WEBRTC_BINARY_PARTIAL:{
597 struct map_info *info = g_new0 (struct map_info, 1);
598 if (!gst_buffer_map (buffer, &info->map_info, GST_MAP_READ)) {
599 g_set_error (error, GST_WEBRTC_BIN_ERROR,
600 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
601 "Failed to map received buffer");
602 ret = GST_FLOW_ERROR;
604 GBytes *data = g_bytes_new_with_free_func (info->map_info.data,
605 info->map_info.size, (GDestroyNotify) buffer_unmap_and_unref, info);
606 info->buffer = gst_buffer_ref (buffer);
607 _channel_enqueue_task (channel, (ChannelTask) _emit_have_data, data,
608 (GDestroyNotify) g_bytes_unref);
612 case DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY:
613 _channel_enqueue_task (channel, (ChannelTask) _emit_have_data, NULL,
616 case DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY:
617 _channel_enqueue_task (channel, (ChannelTask) _emit_have_string, NULL,
621 g_set_error (error, GST_WEBRTC_BIN_ERROR,
622 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
623 "Unknown SCTP PPID %u received", receive->ppid);
624 ret = GST_FLOW_ERROR;
632 on_sink_preroll (GstAppSink * sink, gpointer user_data)
634 WebRTCDataChannel *channel = user_data;
635 GstSample *sample = gst_app_sink_pull_preroll (sink);
639 /* This sample also seems to be provided by the sample callback
640 ret = _data_channel_have_sample (channel, sample); */
642 gst_sample_unref (sample);
643 } else if (gst_app_sink_is_eos (sink)) {
646 ret = GST_FLOW_ERROR;
649 if (ret != GST_FLOW_OK) {
650 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
657 on_sink_sample (GstAppSink * sink, gpointer user_data)
659 WebRTCDataChannel *channel = user_data;
660 GstSample *sample = gst_app_sink_pull_sample (sink);
662 GError *error = NULL;
665 ret = _data_channel_have_sample (channel, sample, &error);
666 gst_sample_unref (sample);
667 } else if (gst_app_sink_is_eos (sink)) {
670 ret = GST_FLOW_ERROR;
674 _channel_store_error (channel, error);
676 if (ret != GST_FLOW_OK) {
677 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
683 static GstAppSinkCallbacks sink_callbacks = {
690 webrtc_data_channel_start_negotiation (WebRTCDataChannel * channel)
694 g_return_if_fail (!channel->parent.negotiated);
695 g_return_if_fail (channel->parent.id != -1);
696 g_return_if_fail (channel->sctp_transport != NULL);
698 buffer = construct_open_packet (channel);
700 GST_INFO_OBJECT (channel, "Sending channel open for SCTP stream %i "
701 "label \"%s\" protocol %s ordered %s", channel->parent.id,
702 channel->parent.label, channel->parent.protocol,
703 channel->parent.ordered ? "true" : "false");
705 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
706 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
707 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
709 if (gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc),
710 buffer) == GST_FLOW_OK) {
711 channel->opened = TRUE;
712 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
714 GError *error = NULL;
715 g_set_error (&error, GST_WEBRTC_BIN_ERROR,
716 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
717 "Failed to send DCEP open packet");
718 _channel_store_error (channel, error);
719 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
724 _get_sctp_reliability (WebRTCDataChannel * channel,
725 GstSctpSendMetaPartiallyReliability * reliability, guint * rel_param)
727 if (channel->parent.max_retransmits != -1) {
728 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_RTX;
729 *rel_param = channel->parent.max_retransmits;
730 } else if (channel->parent.max_packet_lifetime != -1) {
731 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_TTL;
732 *rel_param = channel->parent.max_packet_lifetime;
734 *reliability = GST_SCTP_SEND_META_PARTIAL_RELIABILITY_NONE;
740 _is_within_max_message_size (WebRTCDataChannel * channel, gsize size)
742 return size <= channel->sctp_transport->max_message_size;
746 webrtc_data_channel_send_data (GstWebRTCDataChannel * base_channel,
749 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (base_channel);
750 GstSctpSendMetaPartiallyReliability reliability;
757 buffer = gst_buffer_new ();
758 ppid = DATA_CHANNEL_PPID_WEBRTC_BINARY_EMPTY;
763 data = (guint8 *) g_bytes_get_data (bytes, &size);
764 g_return_if_fail (data != NULL);
765 if (!_is_within_max_message_size (channel, size)) {
766 GError *error = NULL;
767 g_set_error (&error, GST_WEBRTC_BIN_ERROR,
768 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
769 "Requested to send data that is too large");
770 _channel_store_error (channel, error);
771 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL,
776 buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, data, size,
777 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
778 ppid = DATA_CHANNEL_PPID_WEBRTC_BINARY;
781 _get_sctp_reliability (channel, &reliability, &rel_param);
782 gst_sctp_buffer_add_send_meta (buffer, ppid, channel->parent.ordered,
783 reliability, rel_param);
785 GST_LOG_OBJECT (channel, "Sending data using buffer %" GST_PTR_FORMAT,
788 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
789 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
790 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
792 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
794 if (ret != GST_FLOW_OK) {
795 GError *error = NULL;
796 g_set_error (&error, GST_WEBRTC_BIN_ERROR,
797 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE, "Failed to send data");
798 _channel_store_error (channel, error);
799 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
804 webrtc_data_channel_send_string (GstWebRTCDataChannel * base_channel,
807 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (base_channel);
808 GstSctpSendMetaPartiallyReliability reliability;
814 if (!channel->parent.negotiated)
815 g_return_if_fail (channel->opened);
816 g_return_if_fail (channel->sctp_transport != NULL);
819 buffer = gst_buffer_new ();
820 ppid = DATA_CHANNEL_PPID_WEBRTC_STRING_EMPTY;
822 gsize size = strlen (str);
823 gchar *str_copy = g_strdup (str);
825 if (!_is_within_max_message_size (channel, size)) {
826 GError *error = NULL;
827 g_set_error (&error, GST_WEBRTC_BIN_ERROR,
828 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE,
829 "Requested to send a string that is too large");
830 _channel_store_error (channel, error);
831 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL,
837 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, str_copy,
838 size, 0, size, str_copy, g_free);
839 ppid = DATA_CHANNEL_PPID_WEBRTC_STRING;
842 _get_sctp_reliability (channel, &reliability, &rel_param);
843 gst_sctp_buffer_add_send_meta (buffer, ppid, channel->parent.ordered,
844 reliability, rel_param);
846 GST_TRACE_OBJECT (channel, "Sending string using buffer %" GST_PTR_FORMAT,
849 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
850 channel->parent.buffered_amount += gst_buffer_get_size (buffer);
851 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
853 ret = gst_app_src_push_buffer (GST_APP_SRC (channel->appsrc), buffer);
855 if (ret != GST_FLOW_OK) {
856 GError *error = NULL;
857 g_set_error (&error, GST_WEBRTC_BIN_ERROR,
858 GST_WEBRTC_BIN_ERROR_DATA_CHANNEL_FAILURE, "Failed to send string");
859 _channel_store_error (channel, error);
860 _channel_enqueue_task (channel, (ChannelTask) _close_procedure, NULL, NULL);
865 _on_sctp_notify_state_unlocked (GObject * sctp_transport,
866 WebRTCDataChannel * channel)
868 GstWebRTCSCTPTransportState state;
870 g_object_get (sctp_transport, "state", &state, NULL);
871 if (state == GST_WEBRTC_SCTP_TRANSPORT_STATE_CONNECTED) {
872 if (channel->parent.negotiated)
873 _channel_enqueue_task (channel, (ChannelTask) _emit_on_open, NULL, NULL);
878 _on_sctp_notify_state (GObject * sctp_transport, GParamSpec * pspec,
879 WebRTCDataChannel * channel)
881 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
882 _on_sctp_notify_state_unlocked (sctp_transport, channel);
883 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
887 _emit_low_threshold (WebRTCDataChannel * channel, gpointer user_data)
889 gst_webrtc_data_channel_on_buffered_amount_low (GST_WEBRTC_DATA_CHANNEL
893 static GstPadProbeReturn
894 on_appsrc_data (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
896 WebRTCDataChannel *channel = user_data;
900 if (GST_PAD_PROBE_INFO_TYPE (info) & (GST_PAD_PROBE_TYPE_BUFFER)) {
901 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
902 size = gst_buffer_get_size (buffer);
903 } else if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_BUFFER_LIST) {
904 GstBufferList *list = GST_PAD_PROBE_INFO_BUFFER_LIST (info);
905 size = gst_buffer_list_calculate_size (list);
909 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
910 prev_amount = channel->parent.buffered_amount;
911 channel->parent.buffered_amount -= size;
912 GST_TRACE_OBJECT (channel, "checking low-threshold: prev %"
913 G_GUINT64_FORMAT " low-threshold %" G_GUINT64_FORMAT " buffered %"
914 G_GUINT64_FORMAT, prev_amount,
915 channel->parent.buffered_amount_low_threshold,
916 channel->parent.buffered_amount);
917 if (prev_amount >= channel->parent.buffered_amount_low_threshold
918 && channel->parent.buffered_amount <
919 channel->parent.buffered_amount_low_threshold) {
920 _channel_enqueue_task (channel, (ChannelTask) _emit_low_threshold, NULL,
924 if (channel->parent.ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_CLOSING
925 && channel->parent.buffered_amount <= 0) {
926 _channel_enqueue_task (channel, (ChannelTask) _close_sctp_stream, NULL,
929 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
932 return GST_PAD_PROBE_OK;
936 gst_webrtc_data_channel_constructed (GObject * object)
938 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (object);
942 caps = gst_caps_new_any ();
944 channel->appsrc = gst_element_factory_make ("appsrc", NULL);
945 gst_object_ref_sink (channel->appsrc);
946 pad = gst_element_get_static_pad (channel->appsrc, "src");
948 channel->src_probe = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_DATA_BOTH,
949 (GstPadProbeCallback) on_appsrc_data, channel, NULL);
951 channel->appsink = gst_element_factory_make ("appsink", NULL);
952 gst_object_ref_sink (channel->appsink);
953 g_object_set (channel->appsink, "sync", FALSE, "async", FALSE, "caps", caps,
955 gst_app_sink_set_callbacks (GST_APP_SINK (channel->appsink), &sink_callbacks,
958 gst_object_unref (pad);
959 gst_caps_unref (caps);
963 gst_webrtc_data_channel_finalize (GObject * object)
965 WebRTCDataChannel *channel = WEBRTC_DATA_CHANNEL (object);
967 if (channel->src_probe) {
968 GstPad *pad = gst_element_get_static_pad (channel->appsrc, "src");
969 gst_pad_remove_probe (pad, channel->src_probe);
970 gst_object_unref (pad);
971 channel->src_probe = 0;
974 if (channel->sctp_transport)
975 g_signal_handlers_disconnect_by_data (channel->sctp_transport, channel);
976 g_clear_object (&channel->sctp_transport);
978 g_clear_object (&channel->appsrc);
979 g_clear_object (&channel->appsink);
981 G_OBJECT_CLASS (parent_class)->finalize (object);
985 webrtc_data_channel_class_init (WebRTCDataChannelClass * klass)
987 GObjectClass *gobject_class = (GObjectClass *) klass;
988 GstWebRTCDataChannelClass *channel_class =
989 (GstWebRTCDataChannelClass *) klass;
991 gobject_class->constructed = gst_webrtc_data_channel_constructed;
992 gobject_class->finalize = gst_webrtc_data_channel_finalize;
994 channel_class->send_data = webrtc_data_channel_send_data;
995 channel_class->send_string = webrtc_data_channel_send_string;
996 channel_class->close = webrtc_data_channel_close;
1000 webrtc_data_channel_init (WebRTCDataChannel * channel)
1005 _data_channel_set_sctp_transport (WebRTCDataChannel * channel,
1006 WebRTCSCTPTransport * sctp)
1008 g_return_if_fail (GST_IS_WEBRTC_DATA_CHANNEL (channel));
1009 g_return_if_fail (GST_IS_WEBRTC_SCTP_TRANSPORT (sctp));
1011 GST_WEBRTC_DATA_CHANNEL_LOCK (channel);
1012 if (channel->sctp_transport)
1013 g_signal_handlers_disconnect_by_data (channel->sctp_transport, channel);
1015 gst_object_replace ((GstObject **) & channel->sctp_transport,
1019 g_signal_connect (sctp, "stream-reset", G_CALLBACK (_on_sctp_stream_reset),
1021 g_signal_connect (sctp, "notify::state", G_CALLBACK (_on_sctp_notify_state),
1024 GST_WEBRTC_DATA_CHANNEL_UNLOCK (channel);
1028 webrtc_data_channel_link_to_sctp (WebRTCDataChannel * channel,
1029 WebRTCSCTPTransport * sctp_transport)
1031 if (sctp_transport && !channel->sctp_transport) {
1034 g_object_get (channel, "id", &id, NULL);
1036 if (sctp_transport->association_established && id != -1) {
1039 _data_channel_set_sctp_transport (channel, sctp_transport);
1040 pad_name = g_strdup_printf ("sink_%u", id);
1041 if (!gst_element_link_pads (channel->appsrc, "src",
1042 channel->sctp_transport->sctpenc, pad_name))
1043 g_warn_if_reached ();
1046 _on_sctp_notify_state_unlocked (G_OBJECT (sctp_transport), channel);