Squashed 'third_party/rawrtc/rawrtc-data-channel/' content from commit 7b1b8d57c

Change-Id: I84850720e2b51961981d55f67238f4d282314fff
git-subtree-dir: third_party/rawrtc/rawrtc-data-channel
git-subtree-split: 7b1b8d57c6d07da18cc0de8bbca8cc5e8bd06eae
diff --git a/src/data_transport/attributes.c b/src/data_transport/attributes.c
new file mode 100644
index 0000000..6888ab6
--- /dev/null
+++ b/src/data_transport/attributes.c
@@ -0,0 +1,25 @@
+#include "transport.h"
+#include <rawrtcdc/data_transport.h>
+#include <rawrtcc/code.h>
+#include <re.h>
+
+/**
+ * Get the data transport's type and underlying transport reference.
+ * `*internal_transportp` must be unreferenced.
+ */
+enum rawrtc_code rawrtc_data_transport_get_transport(
+    enum rawrtc_data_transport_type* const typep,  // de-referenced
+    void** const internal_transportp,  // de-referenced
+    struct rawrtc_data_transport* const transport) {
+    // Check arguments
+    if (!typep || !transport) {
+        return RAWRTC_CODE_INVALID_ARGUMENT;
+    }
+
+    // Set type & transport
+    *typep = transport->type;
+    *internal_transportp = mem_ref(transport->transport);
+
+    // Done
+    return RAWRTC_CODE_SUCCESS;
+}
diff --git a/src/data_transport/meson.build b/src/data_transport/meson.build
new file mode 100644
index 0000000..0227c31
--- /dev/null
+++ b/src/data_transport/meson.build
@@ -0,0 +1,5 @@
+sources += files([
+    'attributes.c',
+    'transport.c',
+    'utils.c',
+])
diff --git a/src/data_transport/transport.c b/src/data_transport/transport.c
new file mode 100644
index 0000000..62be56d
--- /dev/null
+++ b/src/data_transport/transport.c
@@ -0,0 +1,57 @@
+#include "transport.h"
+#include <rawrtcdc/config.h>
+#include <rawrtcdc/data_transport.h>
+#include <rawrtcc/code.h>
+#include <re.h>
+
+#define DEBUG_MODULE "data-transport"
+//#define RAWRTC_DEBUG_MODULE_LEVEL 7 // Note: Uncomment this to debug this module only
+#include <rawrtcc/debug.h>
+
+/*
+ * Destructor for an existing data transport.
+ */
+static void rawrtc_data_transport_destroy(void* arg) {
+    struct rawrtc_data_transport* const transport = arg;
+
+    // Un-reference
+    mem_deref(transport->transport);
+}
+
+/*
+ * Create a data transport instance.
+ */
+enum rawrtc_code rawrtc_data_transport_create(
+    struct rawrtc_data_transport** const transportp,  // de-referenced
+    enum rawrtc_data_transport_type const type,
+    void* const internal_transport,  // referenced
+    rawrtc_data_transport_channel_create_handler const channel_create_handler,
+    rawrtc_data_transport_channel_close_handler const channel_close_handler,
+    rawrtc_data_transport_channel_send_handler const channel_send_handler,
+    rawrtc_data_transport_channel_set_streaming_handler const channel_set_streaming_handler) {
+    struct rawrtc_data_transport* transport;
+
+    // Check arguments
+    if (!transportp || !internal_transport || !channel_create_handler) {
+        return RAWRTC_CODE_INVALID_ARGUMENT;
+    }
+
+    // Allocate
+    transport = mem_zalloc(sizeof(*transport), rawrtc_data_transport_destroy);
+    if (!transport) {
+        return RAWRTC_CODE_NO_MEMORY;
+    }
+
+    // Set fields
+    transport->type = type;
+    transport->transport = mem_ref(internal_transport);
+    transport->channel_create = channel_create_handler;
+    transport->channel_close = channel_close_handler;
+    transport->channel_send = channel_send_handler;
+    transport->channel_set_streaming = channel_set_streaming_handler;
+
+    // Set pointer & done
+    DEBUG_PRINTF("Created data transport of type %s\n", rawrtc_data_transport_type_to_str(type));
+    *transportp = transport;
+    return RAWRTC_CODE_SUCCESS;
+}
diff --git a/src/data_transport/transport.h b/src/data_transport/transport.h
new file mode 100644
index 0000000..3b5593e
--- /dev/null
+++ b/src/data_transport/transport.h
@@ -0,0 +1,57 @@
+#pragma once
+#include <rawrtcdc/data_channel.h>
+#include <rawrtcdc/data_channel_parameters.h>
+#include <rawrtcdc/data_transport.h>
+#include <rawrtcc/code.h>
+#include <re.h>
+
+/*
+ * Create the data channel (transport handler).
+ */
+typedef enum rawrtc_code (*rawrtc_data_transport_channel_create_handler)(
+    struct rawrtc_data_transport* const transport,
+    struct rawrtc_data_channel* const channel,  // referenced
+    struct rawrtc_data_channel_parameters const* const parameters  // read-only
+);
+
+/*
+ * Close the data channel (transport handler).
+ */
+typedef enum rawrtc_code (*rawrtc_data_transport_channel_close_handler)(
+    struct rawrtc_data_channel* const channel);
+
+/*
+ * Send data via the data channel (transport handler).
+ */
+typedef enum rawrtc_code (*rawrtc_data_transport_channel_send_handler)(
+    struct rawrtc_data_channel* const channel,
+    struct mbuf* buffer,  // nullable (if size 0), referenced
+    bool const is_binary);
+
+/*
+ * Check if the data transport allows changing the delivery mode
+ * (transport handler).
+ */
+typedef enum rawrtc_code (*rawrtc_data_transport_channel_set_streaming_handler)(
+    struct rawrtc_data_channel* const channel, bool const on);
+
+/**
+ * Generic data transport.
+ */
+struct rawrtc_data_transport {
+    enum rawrtc_data_transport_type type;
+    void* transport;
+    rawrtc_data_transport_channel_create_handler channel_create;
+    rawrtc_data_transport_channel_close_handler channel_close;
+    rawrtc_data_transport_channel_send_handler channel_send;
+    rawrtc_data_transport_channel_set_streaming_handler channel_set_streaming;
+};
+
+enum rawrtc_code rawrtc_data_transport_create(
+    struct rawrtc_data_transport** const transportp,  // de-referenced
+    enum rawrtc_data_transport_type const type,
+    void* const internal_transport,  // referenced
+    rawrtc_data_transport_channel_create_handler const channel_create_handler,
+    rawrtc_data_transport_channel_close_handler const channel_close_handler,
+    rawrtc_data_transport_channel_send_handler const channel_send_handler,
+    rawrtc_data_transport_channel_set_streaming_handler const channel_set_streaming_handler);
diff --git a/src/data_transport/utils.c b/src/data_transport/utils.c
new file mode 100644
index 0000000..1216eea
--- /dev/null
+++ b/src/data_transport/utils.c
@@ -0,0 +1,13 @@
+#include <rawrtcdc/data_transport.h>
+
+/*
+ * Translate a data transport type to str.
+ */
+char const* rawrtc_data_transport_type_to_str(enum rawrtc_data_transport_type const type) {
+    switch (type) {
+        case RAWRTC_DATA_TRANSPORT_TYPE_SCTP:
+            return "SCTP";
+        default:
+            return "???";
+    }
+}