summaryrefslogtreecommitdiff
path: root/gst/sdp/gstsdpsrc.c
blob: 96b483d23ad07106671f520a672336126cc11182 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2016 Sebastian Dröge <sebastian@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 "gstsdpsrc.h"
#include <gst/app/app.h>
#include <string.h>

GST_DEBUG_CATEGORY_STATIC (sdp_src_debug);
#define GST_CAT_DEFAULT sdp_src_debug

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("stream_%u",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("application/x-rtp"));

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_SDP
};

static void gst_sdp_src_handler_init (gpointer g_iface, gpointer iface_data);

#define gst_sdp_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstSdpSrc, gst_sdp_src, GST_TYPE_BIN,
    G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_sdp_src_handler_init));

static void
gst_sdp_src_finalize (GObject * object)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (object);

  if (self->sdp_buffer)
    gst_buffer_unref (self->sdp_buffer);
  g_free (self->location);
  g_free (self->sdp);

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

static void
gst_sdp_src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_value_set_string (value, self->location);
      break;
    case PROP_SDP:
      g_value_set_string (value, self->sdp);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_sdp_src_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_free (self->location);
      self->location = g_value_dup_string (value);
      break;
    case PROP_SDP:
      g_free (self->sdp);
      self->sdp = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
pad_added_cb (GstElement * element, GstPad * pad, gpointer user_data)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (user_data);
  GstPad *ghost;

  ghost =
      gst_ghost_pad_new_from_template (GST_PAD_NAME (pad), pad,
      gst_static_pad_template_get (&src_template));
  gst_pad_set_active (ghost, TRUE);
  gst_element_add_pad (GST_ELEMENT_CAST (self), ghost);
  g_object_set_data (G_OBJECT (pad), "GstSdpSrc.ghostpad", ghost);
}

static void
pad_removed_cb (GstElement * element, GstPad * pad, gpointer user_data)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (user_data);
  GstPad *ghost;

  ghost = g_object_get_data (G_OBJECT (pad), "GstSdpSrc.ghostpad");
  if (ghost) {
    g_object_set_data (G_OBJECT (pad), "GstSdpSrc.ghostpad", NULL);

    gst_pad_set_active (ghost, FALSE);
    gst_element_remove_pad (GST_ELEMENT_CAST (self), ghost);
  }
}

static void
no_more_pads_cb (GstElement * element, gpointer user_data)
{
  gst_element_no_more_pads (GST_ELEMENT_CAST (user_data));
}

static void
remove_pad (const GValue * item, gpointer user_data)
{
  GstElement *self = user_data;
  GstPad *pad = g_value_get_object (item);

  gst_element_remove_pad (self, pad);
}

static GstStateChangeReturn
gst_sdp_src_change_state (GstElement * element, GstStateChange transition)
{
  GstSdpSrc *self = GST_SDP_SRC_CAST (element);
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      GST_OBJECT_LOCK (self);
      if (self->sdp_buffer)
        gst_buffer_unref (self->sdp_buffer);
      self->sdp_buffer = NULL;

      if (self->location && strcmp (self->location, "sdp://") != 0) {
        /* Do nothing */
      } else if (self->sdp) {
        guint sdp_len = strlen (self->sdp);

        self->sdp_buffer =
            gst_buffer_new_wrapped (g_strndup (self->sdp, sdp_len),
            sdp_len + 1);
      } else {
        ret = GST_STATE_CHANGE_FAILURE;
      }
      GST_OBJECT_UNLOCK (self);

      if (ret != GST_STATE_CHANGE_FAILURE) {
        if (self->sdp_buffer) {
          GstCaps *caps = gst_caps_new_empty_simple ("application/sdp");

          self->src = gst_element_factory_make ("appsrc", NULL);
          g_object_set (self->src, "caps", caps, "emit-signals", FALSE, NULL);
          gst_caps_unref (caps);
        } else {
          self->src = gst_element_factory_make ("filesrc", NULL);
          g_object_set (self->src, "location", self->location + 6, NULL);
        }

        self->demux = gst_element_factory_make ("sdpdemux", NULL);
        g_signal_connect (self->demux, "pad-added", G_CALLBACK (pad_added_cb),
            self);
        g_signal_connect (self->demux, "pad-removed",
            G_CALLBACK (pad_removed_cb), self);
        g_signal_connect (self->demux, "no-more-pads",
            G_CALLBACK (no_more_pads_cb), self);
        gst_bin_add_many (GST_BIN_CAST (self), self->src, self->demux, NULL);
        gst_element_link_pads (self->src, "src", self->demux, "sink");
      }
      break;
    default:
      break;
  }

  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:{
      GstIterator *it;

      it = gst_element_iterate_src_pads (GST_ELEMENT_CAST (self));
      while (gst_iterator_foreach (it, remove_pad, self) == GST_ITERATOR_RESYNC)
        gst_iterator_resync (it);
      gst_iterator_free (it);

      if (self->src) {
        gst_bin_remove (GST_BIN_CAST (self), self->src);
        self->src = NULL;
      }
      if (self->demux) {
        gst_bin_remove (GST_BIN_CAST (self), self->demux);
        self->demux = NULL;
      }
      break;
    }
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      if (ret != GST_STATE_CHANGE_FAILURE)
        ret = GST_STATE_CHANGE_NO_PREROLL;
      if (self->sdp_buffer) {
        if (gst_app_src_push_buffer (GST_APP_SRC_CAST (self->src),
                gst_buffer_ref (self->sdp_buffer)) != GST_FLOW_OK)
          ret = GST_STATE_CHANGE_FAILURE;
        else
          gst_app_src_end_of_stream (GST_APP_SRC_CAST (self->src));
      }
      break;
    default:
      break;
  }

  return ret;
}

static void
gst_sdp_src_class_init (GstSdpSrcClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *element_class = (GstElementClass *) klass;

  GST_DEBUG_CATEGORY_INIT (sdp_src_debug, "sdpsrc", 0, "SDP Source");

  gobject_class->finalize = gst_sdp_src_finalize;
  gobject_class->set_property = gst_sdp_src_set_property;
  gobject_class->get_property = gst_sdp_src_get_property;

  g_object_class_install_property (gobject_class, PROP_LOCATION,
      g_param_spec_string ("location",
          "Location",
          "URI to SDP file (sdp:///path/to/file)", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_SDP,
      g_param_spec_string ("sdp",
          "SDP",
          "SDP description used instead of location", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_template));

  gst_element_class_set_static_metadata (element_class, "SDP Source",
      "Source/Network/RTP",
      "Stream RTP based on an SDP",
      "Sebastian Dröge <sebastian@centricular.com>");

  element_class->change_state = GST_DEBUG_FUNCPTR (gst_sdp_src_change_state);
}

static void
gst_sdp_src_init (GstSdpSrc * self)
{
}

static GstURIType
gst_sdp_src_get_uri_type (GType type)
{
  return GST_URI_SRC;
}

static const gchar *const *
gst_sdp_src_get_protocols (GType type)
{
  static const gchar *protocols[] = { "sdp", 0 };

  return protocols;
}

static gchar *
gst_sdp_src_get_uri (GstURIHandler * handler)
{
  gchar *uri = NULL;

  g_object_get (handler, "location", &uri, NULL);

  return uri;
}

static gboolean
gst_sdp_src_set_uri (GstURIHandler * handler, const gchar * uri,
    GError ** error)
{
  if (uri && !g_str_has_prefix (uri, "sdp://")) {
    g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
        "Invalid SDP URI");
    return FALSE;
  }

  g_object_set (handler, "location", uri, NULL);

  return TRUE;
}

static void
gst_sdp_src_handler_init (gpointer g_iface, gpointer iface_data)
{
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;

  iface->get_type = gst_sdp_src_get_uri_type;
  iface->get_protocols = gst_sdp_src_get_protocols;
  iface->get_uri = gst_sdp_src_get_uri;
  iface->set_uri = gst_sdp_src_set_uri;
}