summaryrefslogtreecommitdiff
path: root/gst/proxy/gstproxysrc.c
blob: 2d016513f262145656f3f1634cc5a1db2f8c503c (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
331
332
333
334
335
336
337
338
/*
 * Copyright (C) 2018 Centricular Ltd.
 *   Author: Sebastian Dröge <sebastian@centricular.com>
 *   Author: Nirbheek Chauhan <nirbheek@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.
 */

/**
 * SECTION:element-proxysrc
 * @title: proxysrc
 *
 * Proxysrc is a source element that proxies events, queries, and buffers from
 * another pipeline that contains a matching proxysink element. The purpose is
 * to allow two decoupled pipelines to function as though they are one without
 * having to manually shuttle buffers, events, queries, etc between the two.
 *
 * The element queues buffers from the matching proxysink to an internal queue,
 * so everything downstream is properly decoupled from the upstream pipeline.
 * However, the queue may get filled up if the downstream pipeline does not
 * accept buffers quickly enough; perhaps because it is not yet PLAYING.
 *
 * ## Usage
 * 
 * |[<!-- language="C" -->
 * GstElement *pipe1, *pipe2, *psink, *psrc;
 * GstClock *clock;
 *
 * pipe1 = gst_parse_launch ("audiotestsrc ! proxysink name=psink", NULL);
 * psink = gst_bin_get_by_name (GST_BIN (pipe1), "psink");
 *
 * pipe2 = gst_parse_launch ("proxysrc name=psrc ! autoaudiosink", NULL);
 * psrc = gst_bin_get_by_name (GST_BIN (pipe2), "psrc");
 *
 * // Connect the two pipelines
 * g_object_set (psrc, "proxysink", psink, NULL);
 *
 * // Both pipelines must agree on the timing information or we'll get glitches
 * // or overruns/underruns. Ideally, we should tell pipe1 to use the same clock
 * // as pipe2, but since that will be set asynchronously to the audio clock, it
 * // is simpler and likely accurate enough to use the system clock for both
 * // pipelines. If no element in either pipeline will provide a clock, this
 * // is not needed.
 * clock = gst_system_clock_obtain ();
 * gst_pipeline_use_clock (GST_PIPELINE (pipe1), clock);
 * gst_pipeline_use_clock (GST_PIPELINE (pipe2), clock);
 * g_object_unref (clock);
 *
 * // This is not really needed in this case since the pipelines are created and
 * // started at the same time. However, an application that dynamically
 * // generates pipelines must ensure that all the pipelines that will be
 * // connected together share the same base time.
 * gst_element_set_base_time (pipe1, 0);
 * gst_element_set_base_time (pipe2, 0);
 *
 * gst_element_set_state (pipe1, GST_STATE_PLAYING);
 * gst_element_set_state (pipe2, GST_STATE_PLAYING);
 * ]|
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstproxysrc.h"
#include "gstproxysink.h"
#include "gstproxy-priv.h"

#define GST_CAT_DEFAULT gst_proxy_src_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

enum
{
  PROP_0,
  PROP_PROXYSINK,
};

/* We're not subclassing from basesrc because we don't want any of the special
 * handling it has for events/queries/etc. We just pass-through everything. */

/* Our parent type is a GstBin instead of GstElement because we contain a queue
 * element */
#define parent_class gst_proxy_src_parent_class
G_DEFINE_TYPE (GstProxySrc, gst_proxy_src, GST_TYPE_BIN);

static gboolean gst_proxy_src_internal_src_query (GstPad * pad,
    GstObject * parent, GstQuery * query);
static gboolean gst_proxy_src_internal_src_event (GstPad * pad,
    GstObject * parent, GstEvent * event);

static GstStateChangeReturn gst_proxy_src_change_state (GstElement * element,
    GstStateChange transition);
static void gst_proxy_src_dispose (GObject * object);

static void
gst_proxy_src_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * spec)
{
  GstProxySrc *self = GST_PROXY_SRC (object);

  switch (prop_id) {
    case PROP_PROXYSINK:
      g_value_take_object (value, g_weak_ref_get (&self->proxysink));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
      break;
  }
}

static void
gst_proxy_src_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * spec)
{
  GstProxySrc *self = GST_PROXY_SRC (object);
  GstProxySink *sink;

  switch (prop_id) {
    case PROP_PROXYSINK:
      sink = g_value_dup_object (value);
      if (sink == NULL) {
        /* Unset proxysrc property on the existing proxysink to break the
         * connection in that direction */
        GstProxySink *old_sink = g_weak_ref_get (&self->proxysink);
        if (old_sink) {
          gst_proxy_sink_set_proxysrc (old_sink, NULL);
          g_object_unref (old_sink);
        }
        g_weak_ref_set (&self->proxysink, NULL);
      } else {
        /* Set proxysrc property on the new proxysink to point to us */
        gst_proxy_sink_set_proxysrc (sink, self);
        g_weak_ref_set (&self->proxysink, sink);
        g_object_unref (sink);
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
  }
}

static void
gst_proxy_src_class_init (GstProxySrcClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *gstelement_class = (GstElementClass *) klass;

  GST_DEBUG_CATEGORY_INIT (gst_proxy_src_debug, "proxysrc", 0, "proxy sink");

  gobject_class->dispose = gst_proxy_src_dispose;

  gobject_class->get_property = gst_proxy_src_get_property;
  gobject_class->set_property = gst_proxy_src_set_property;

  g_object_class_install_property (gobject_class, PROP_PROXYSINK,
      g_param_spec_object ("proxysink", "Proxysink", "Matching proxysink",
          GST_TYPE_PROXY_SINK, G_PARAM_READWRITE));

  gstelement_class->change_state = gst_proxy_src_change_state;
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&src_template));

  gst_element_class_set_static_metadata (gstelement_class, "Proxy source",
      "Source", "Proxy source for internal process communication",
      "Sebastian Dröge <sebastian@centricular.com>");
}

static void
gst_proxy_src_init (GstProxySrc * self)
{
  GstPad *srcpad, *sinkpad;
  GstPadTemplate *templ;

  GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_SOURCE);

  /* We feed incoming buffers into a queue to decouple the downstream pipeline
   * from the upstream pipeline */
  self->queue = gst_element_factory_make ("queue", NULL);
  gst_bin_add (GST_BIN (self), self->queue);

  srcpad = gst_element_get_static_pad (self->queue, "src");
  templ = gst_static_pad_template_get (&src_template);
  self->srcpad = gst_ghost_pad_new_from_template ("src", srcpad, templ);
  gst_object_unref (templ);
  gst_object_unref (srcpad);

  gst_element_add_pad (GST_ELEMENT (self), self->srcpad);

  /* A dummy sinkpad that's not actually used anywhere
   * Explanation for why this is needed is below */
  self->dummy_sinkpad = gst_pad_new ("dummy_sinkpad", GST_PAD_SINK);
  gst_object_set_parent (GST_OBJECT (self->dummy_sinkpad), GST_OBJECT (self));

  self->internal_srcpad = gst_pad_new ("internal_src", GST_PAD_SRC);
  gst_object_set_parent (GST_OBJECT (self->internal_srcpad),
      GST_OBJECT (self->dummy_sinkpad));
  gst_pad_set_event_function (self->internal_srcpad,
      gst_proxy_src_internal_src_event);
  gst_pad_set_query_function (self->internal_srcpad,
      gst_proxy_src_internal_src_query);

  /* We need to link internal_srcpad from proxysink to the sinkpad of our
   * queue. However, two pads can only be linked if they share a common parent.
   * Above, we set the parent of the dummy_sinkpad as proxysrc, and then we set
   * the parent of internal_srcpad as dummy_sinkpad. This causes both these pads
   * to share a parent allowing us to link them.
   * Yes, this is a hack/workaround. */
  sinkpad = gst_element_get_static_pad (self->queue, "sink");
  gst_pad_link (self->internal_srcpad, sinkpad);
  gst_object_unref (sinkpad);
}

static void
gst_proxy_src_dispose (GObject * object)
{
  GstProxySrc *self = GST_PROXY_SRC (object);

  gst_object_unparent (GST_OBJECT (self->dummy_sinkpad));
  self->dummy_sinkpad = NULL;

  gst_object_unparent (GST_OBJECT (self->internal_srcpad));
  self->internal_srcpad = NULL;

  g_weak_ref_set (&self->proxysink, NULL);

  G_OBJECT_CLASS (gst_proxy_src_parent_class)->dispose (object);
}

static GstStateChangeReturn
gst_proxy_src_change_state (GstElement * element, GstStateChange transition)
{
  GstElementClass *gstelement_class =
      GST_ELEMENT_CLASS (gst_proxy_src_parent_class);
  GstProxySrc *self = GST_PROXY_SRC (element);
  GstStateChangeReturn ret;

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

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      ret = GST_STATE_CHANGE_NO_PREROLL;
      gst_pad_set_active (self->internal_srcpad, TRUE);
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_pad_set_active (self->internal_srcpad, FALSE);
      break;
    default:
      break;
  }

  return ret;
}

static gboolean
gst_proxy_src_internal_src_query (GstPad * pad, GstObject * parent,
    GstQuery * query)
{
  GstProxySrc *self = GST_PROXY_SRC (gst_object_get_parent (parent));
  GstProxySink *sink;
  gboolean ret = FALSE;

  if (!self)
    return ret;

  GST_LOG_OBJECT (pad, "Handling query of type '%s'",
      gst_query_type_get_name (GST_QUERY_TYPE (query)));

  sink = g_weak_ref_get (&self->proxysink);
  if (sink) {
    GstPad *sinkpad;
    sinkpad = gst_proxy_sink_get_internal_sinkpad (sink);

    ret = gst_pad_peer_query (sinkpad, query);
    gst_object_unref (sinkpad);
    gst_object_unref (sink);
  }

  gst_object_unref (self);

  return ret;
}

static gboolean
gst_proxy_src_internal_src_event (GstPad * pad, GstObject * parent,
    GstEvent * event)
{
  GstProxySrc *self = GST_PROXY_SRC (gst_object_get_parent (parent));
  GstProxySink *sink;
  gboolean ret = FALSE;

  if (!self)
    return ret;

  GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));

  sink = g_weak_ref_get (&self->proxysink);
  if (sink) {
    GstPad *sinkpad;
    sinkpad = gst_proxy_sink_get_internal_sinkpad (sink);

    ret = gst_pad_push_event (sinkpad, event);
    gst_object_unref (sinkpad);
    gst_object_unref (sink);
  } else
    gst_event_unref (event);


  gst_object_unref (self);

  return ret;
}

/* Wrapper function for accessing private member */
GstPad *
gst_proxy_src_get_internal_srcpad (GstProxySrc * self)
{
  return gst_object_ref (self->internal_srcpad);
}