James Kuszmaul | 6439136 | 2021-01-17 11:32:00 -0800 | [diff] [blame^] | 1 | #include "parameters.h" |
| 2 | #include <rawrtcdc/data_channel.h> |
| 3 | #include <rawrtcdc/data_channel_parameters.h> |
| 4 | #include <rawrtcc/code.h> |
| 5 | #include <rawrtcc/utils.h> |
| 6 | #include <re.h> |
| 7 | |
| 8 | /* |
| 9 | * Destructor for existing data channel parameters. |
| 10 | */ |
| 11 | static void rawrtc_data_channel_parameters_destroy(void* arg) { |
| 12 | struct rawrtc_data_channel_parameters* const parameters = arg; |
| 13 | |
| 14 | // Un-reference |
| 15 | mem_deref(parameters->label); |
| 16 | mem_deref(parameters->protocol); |
| 17 | } |
| 18 | |
| 19 | /* |
| 20 | * Common code to create or copy parameters. |
| 21 | */ |
| 22 | static enum rawrtc_code data_parameters_create( |
| 23 | struct rawrtc_data_channel_parameters** const parametersp, // de-referenced |
| 24 | char* const label, // referenced, nullable |
| 25 | enum rawrtc_data_channel_type const channel_type, |
| 26 | uint32_t const reliability_parameter, |
| 27 | char* const protocol, // referenced, nullable |
| 28 | bool const negotiated, |
| 29 | uint16_t const id) { |
| 30 | struct rawrtc_data_channel_parameters* parameters; |
| 31 | |
| 32 | // Check arguments |
| 33 | if (!parametersp) { |
| 34 | return RAWRTC_CODE_INVALID_ARGUMENT; |
| 35 | } |
| 36 | |
| 37 | // Allocate |
| 38 | parameters = mem_zalloc(sizeof(*parameters), rawrtc_data_channel_parameters_destroy); |
| 39 | if (!parameters) { |
| 40 | return RAWRTC_CODE_NO_MEMORY; |
| 41 | } |
| 42 | |
| 43 | // Set fields |
| 44 | parameters->label = label; |
| 45 | parameters->protocol = protocol; |
| 46 | parameters->channel_type = channel_type; |
| 47 | parameters->negotiated = negotiated; |
| 48 | if (negotiated) { |
| 49 | parameters->id = id; |
| 50 | } |
| 51 | |
| 52 | // Set reliability parameter |
| 53 | switch (channel_type) { |
| 54 | case RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_ORDERED: |
| 55 | case RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_UNORDERED: |
| 56 | parameters->reliability_parameter = 0; |
| 57 | break; |
| 58 | default: |
| 59 | parameters->reliability_parameter = reliability_parameter; |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | // Set pointer & done |
| 64 | *parametersp = parameters; |
| 65 | return RAWRTC_CODE_SUCCESS; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Create data channel parameters (internal). |
| 70 | */ |
| 71 | enum rawrtc_code rawrtc_data_channel_parameters_create_internal( |
| 72 | struct rawrtc_data_channel_parameters** const parametersp, // de-referenced |
| 73 | char* const label, // referenced, nullable |
| 74 | enum rawrtc_data_channel_type const channel_type, |
| 75 | uint32_t const reliability_parameter, |
| 76 | char* const protocol, // referenced, nullable |
| 77 | bool const negotiated, |
| 78 | uint16_t const id) { |
| 79 | enum rawrtc_code error; |
| 80 | |
| 81 | // Create parameters |
| 82 | error = data_parameters_create( |
| 83 | parametersp, label, channel_type, reliability_parameter, protocol, negotiated, id); |
| 84 | |
| 85 | if (!error) { |
| 86 | // Reference label & protocol |
| 87 | mem_ref(label); |
| 88 | mem_ref(protocol); |
| 89 | } |
| 90 | |
| 91 | // Done |
| 92 | return error; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Create data channel parameters. |
| 97 | * |
| 98 | * For `RAWRTC_DATA_CHANNEL_TYPE_RELIABLE_*`, the reliability parameter |
| 99 | * is being ignored. |
| 100 | * |
| 101 | * When using `RAWRTC_DATA_CHANNEL_TYPE_*_RETRANSMIT`, the reliability |
| 102 | * parameter specifies the number of times a retransmission occurs if |
| 103 | * not acknowledged before the message is being discarded. |
| 104 | * |
| 105 | * When using `RAWRTC_DATA_CHANNEL_TYPE_*_TIMED`, the reliability |
| 106 | * parameter specifies the time window in milliseconds during which |
| 107 | * (re-)transmissions may occur before the message is being discarded. |
| 108 | * |
| 109 | * `*parametersp` must be unreferenced. |
| 110 | * |
| 111 | * In case `negotiated` is set to `false`, the `id` is being ignored. |
| 112 | */ |
| 113 | enum rawrtc_code rawrtc_data_channel_parameters_create( |
| 114 | struct rawrtc_data_channel_parameters** const parametersp, // de-referenced |
| 115 | char const* const label, // copied, nullable |
| 116 | enum rawrtc_data_channel_type const channel_type, |
| 117 | uint32_t const reliability_parameter, |
| 118 | char const* const protocol, // copied, nullable |
| 119 | bool const negotiated, |
| 120 | uint16_t const id) { |
| 121 | char* copied_label; |
| 122 | char* copied_protocol; |
| 123 | enum rawrtc_code error; |
| 124 | |
| 125 | // Copy label |
| 126 | if (label) { |
| 127 | rawrtc_strdup(&copied_label, label); |
| 128 | } else { |
| 129 | copied_label = NULL; |
| 130 | } |
| 131 | |
| 132 | // Copy protocol |
| 133 | if (protocol) { |
| 134 | rawrtc_strdup(&copied_protocol, protocol); |
| 135 | } else { |
| 136 | copied_protocol = NULL; |
| 137 | } |
| 138 | |
| 139 | // Create parameters |
| 140 | error = data_parameters_create( |
| 141 | parametersp, copied_label, channel_type, reliability_parameter, copied_protocol, negotiated, |
| 142 | id); |
| 143 | |
| 144 | if (error) { |
| 145 | // Un-reference |
| 146 | mem_deref(copied_label); |
| 147 | mem_deref(copied_protocol); |
| 148 | } |
| 149 | |
| 150 | // Done |
| 151 | return error; |
| 152 | } |