summaryrefslogtreecommitdiff
path: root/ext/webrtc/icestream.c
blob: 924c9cbd761e0a1b2f2618bbba5bd3249f8fed06 (plain)
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
/* GStreamer
 * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "icestream.h"
#include "nicetransport.h"

#define GST_CAT_DEFAULT gst_webrtc_ice_stream_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

enum
{
  SIGNAL_0,
  LAST_SIGNAL,
};

enum
{
  PROP_0,
  PROP_ICE,
  PROP_STREAM_ID,
};

//static guint gst_webrtc_ice_stream_signals[LAST_SIGNAL] = { 0 };

struct _GstWebRTCICEStreamPrivate
{
  gboolean gathered;
  GList *transports;
  gboolean gathering_started;
};

#define gst_webrtc_ice_stream_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstWebRTCICEStream, gst_webrtc_ice_stream,
    GST_TYPE_OBJECT, G_ADD_PRIVATE (GstWebRTCICEStream)
    GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_stream_debug,
        "webrtcicestream", 0, "webrtcicestream"););

static void
gst_webrtc_ice_stream_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);

  switch (prop_id) {
    case PROP_ICE:
      /* XXX: weak-ref this? */
      stream->ice = g_value_get_object (value);
      break;
    case PROP_STREAM_ID:
      stream->stream_id = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_webrtc_ice_stream_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);

  switch (prop_id) {
    case PROP_ICE:
      g_value_set_object (value, stream->ice);
      break;
    case PROP_STREAM_ID:
      g_value_set_uint (value, stream->stream_id);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_webrtc_ice_stream_finalize (GObject * object)
{
  GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);

  g_list_free (stream->priv->transports);
  stream->priv->transports = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
_on_candidate_gathering_done (NiceAgent * agent, guint stream_id,
    GstWebRTCICEStream * ice)
{
  GList *l;

  if (stream_id != ice->stream_id)
    return;

  GST_DEBUG_OBJECT (ice, "%u gathering done", stream_id);

  ice->priv->gathered = TRUE;

  for (l = ice->priv->transports; l; l = l->next) {
    GstWebRTCICETransport *ice = l->data;

    gst_webrtc_ice_transport_gathering_state_change (ice,
        GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE);
  }
}

GstWebRTCICETransport *
gst_webrtc_ice_stream_find_transport (GstWebRTCICEStream * stream,
    GstWebRTCICEComponent component)
{
  GstWebRTCICEComponent trans_comp;
  GstWebRTCICETransport *ret;
  GList *l;

  g_return_val_if_fail (GST_IS_WEBRTC_ICE_STREAM (stream), NULL);

  for (l = stream->priv->transports; l; l = l->next) {
    GstWebRTCICETransport *trans = l->data;
    g_object_get (trans, "component", &trans_comp, NULL);

    if (component == trans_comp)
      return gst_object_ref (trans);
  }

  ret =
      GST_WEBRTC_ICE_TRANSPORT (gst_webrtc_nice_transport_new (stream,
          component));
  stream->priv->transports = g_list_prepend (stream->priv->transports, ret);

  return ret;
}

static void
gst_webrtc_ice_stream_constructed (GObject * object)
{
  GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);
  NiceAgent *agent;

  g_object_get (stream->ice, "agent", &agent, NULL);
  g_signal_connect (agent, "candidate-gathering-done",
      G_CALLBACK (_on_candidate_gathering_done), stream);

  g_object_unref (agent);

  G_OBJECT_CLASS (parent_class)->constructed (object);
}

gboolean
gst_webrtc_ice_stream_gather_candidates (GstWebRTCICEStream * stream)
{
  NiceAgent *agent;
  GList *l;

  g_return_val_if_fail (GST_IS_WEBRTC_ICE_STREAM (stream), FALSE);

  GST_DEBUG_OBJECT (stream, "start gathering candidates");

  if (stream->priv->gathered)
    return TRUE;

  for (l = stream->priv->transports; l; l = l->next) {
    GstWebRTCICETransport *trans = l->data;

    gst_webrtc_ice_transport_gathering_state_change (trans,
        GST_WEBRTC_ICE_GATHERING_STATE_GATHERING);
  }

  g_object_get (stream->ice, "agent", &agent, NULL);

  if (!stream->priv->gathering_started) {
    if (stream->ice->min_rtp_port != 0 || stream->ice->max_rtp_port != 65535) {
      if (stream->ice->min_rtp_port > stream->ice->max_rtp_port) {
        GST_ERROR_OBJECT (stream->ice,
            "invalid port range: min-rtp-port %d must be <= max-rtp-port %d",
            stream->ice->min_rtp_port, stream->ice->max_rtp_port);
        return FALSE;
      }

      nice_agent_set_port_range (agent, stream->stream_id,
          NICE_COMPONENT_TYPE_RTP, stream->ice->min_rtp_port,
          stream->ice->max_rtp_port);
    }
    /* mark as gathering started to prevent changing ports again */
    stream->priv->gathering_started = TRUE;
  }

  if (!nice_agent_gather_candidates (agent, stream->stream_id)) {
    g_object_unref (agent);
    return FALSE;
  }

  for (l = stream->priv->transports; l; l = l->next) {
    GstWebRTCNiceTransport *trans = l->data;

    gst_webrtc_nice_transport_update_buffer_size (trans);
  }

  g_object_unref (agent);
  return TRUE;
}

static void
gst_webrtc_ice_stream_class_init (GstWebRTCICEStreamClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;

  gobject_class->constructed = gst_webrtc_ice_stream_constructed;
  gobject_class->get_property = gst_webrtc_ice_stream_get_property;
  gobject_class->set_property = gst_webrtc_ice_stream_set_property;
  gobject_class->finalize = gst_webrtc_ice_stream_finalize;

  g_object_class_install_property (gobject_class,
      PROP_ICE,
      g_param_spec_object ("ice",
          "ICE", "ICE agent associated with this stream",
          GST_TYPE_WEBRTC_ICE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class,
      PROP_STREAM_ID,
      g_param_spec_uint ("stream-id",
          "ICE stream id", "ICE stream id associated with this stream",
          0, G_MAXUINT, 0,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

static void
gst_webrtc_ice_stream_init (GstWebRTCICEStream * ice)
{
  ice->priv = gst_webrtc_ice_stream_get_instance_private (ice);
}

GstWebRTCICEStream *
gst_webrtc_ice_stream_new (GstWebRTCICE * ice, guint stream_id)
{
  return g_object_new (GST_TYPE_WEBRTC_ICE_STREAM, "ice", ice,
      "stream-id", stream_id, NULL);
}