~ubuntu-branches/ubuntu/trusty/gstreamer1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to docs/pwg/advanced-negotiation.xml

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2012-10-08 09:59:20 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20121008095920-3k2vlenl0zf6lu7i
Tags: 1.0.1-1
* New upstream stable release:
  + debian/libgstreamer.symbols:
    - Add new symbols.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
    </para>
58
58
    <para>
59
59
      In order for caps negotiation on non-fixed links to work correctly,
60
 
      pads can optionally implement a function that tells peer elements what
61
 
      formats it supports and/or prefers. When upstream renegotiation is
 
60
      pads can optionally implement a query function that tells peer elements
 
61
      what formats it supports and/or prefers. When upstream renegotiation is
62
62
      triggered, this becomes important.
63
63
    </para>
64
64
    <para>
65
 
      Downstream elements are notified of a newly set caps only when data
66
 
      is actually passing their pad. This is because caps is attached to
67
 
      buffers during data flow. So when the vorbis decoder sets a caps on
 
65
      Downstream elements are notified of a newly set caps with a
 
66
      GST_EVENT_CAPS on the sinkpad. So when the vorbis decoder sets a caps on
68
67
      its source pad (to configure the output format), the converter will
69
 
      not yet be notified. Instead, the converter will only be notified
70
 
      when the decoder pushes a buffer over its source pad to the converter.
71
 
      Right before calling the chain-function in the converter, &GStreamer;
72
 
      will check whether the format that was previously negotiated still
73
 
      applies to this buffer. If not, it first calls the setcaps-function
74
 
      of the converter to configure it for the new format. Only after that
75
 
      will it call the chain function of the converter.
 
68
      receive a caps event. 
 
69
      When an element receives a buffer, it should check if it has received
 
70
      all needed format information in a CAPS event previously. If it hasn't,
 
71
      it should return an error from the chain function.
76
72
    </para>
77
73
  </sect1>
78
74
 
87
83
    </para>
88
84
    <programlisting>
89
85
[..]
90
 
  pad = gst_pad_new_from_template (..);
 
86
  pad = gst_pad_new_from_static_template (..);
91
87
  gst_pad_use_fixed_caps (pad);
92
88
[..]
93
89
    </programlisting>
99
95
[..]
100
96
  caps = gst_caps_new_simple ("audio/x-raw",
101
97
      "format", G_TYPE_STRING, GST_AUDIO_NE(F32),
102
 
      "buffer-frames", G_TYPE_INT, &lt;bytes-per-frame&gt;,
103
98
      "rate", G_TYPE_INT, &lt;samplerate&gt;,
104
99
      "channels", G_TYPE_INT, &lt;num-channels&gt;, NULL);
105
100
  if (!gst_pad_set_caps (pad, caps)) {
158
153
      <para>
159
154
        Many elements, particularly effects and converters, will be able
160
155
        to parse the format of the stream from their input caps, and decide
161
 
        the output format right at that time already. When renegotiation
162
 
        takes place, some may merely need to "forward" the renegotiation
163
 
        backwards upstream (more on that later). For those elements, all
164
 
        (downstream) caps negotiation can be done in something that we
165
 
        call the <function>_setcaps ()</function> function. This function is
166
 
        called when a buffer is pushed over a pad, but the format on this
167
 
        buffer is not the same as the format that was previously negotiated
168
 
        (or, similarly, no format was negotiated yet so far).
 
156
        the output format right at that time already. For those elements, all
 
157
        (downstream) caps negotiation can be done from the 
 
158
        <function>_event ()</function> function when a GST_EVENT_CAPS is
 
159
        received on the sinkpad. This CAPS event is received whenever the
 
160
        format changes or when no format was negotiated yet. It will always
 
161
        be called before you receive the buffer in the format specified in
 
162
        the CAPS event.
169
163
      </para>
170
164
      <para>
171
 
        In the <function>_setcaps ()</function>-function, the element can
172
 
        forward the caps to the next element and, if that pad accepts the
 
165
        In the <function>_event ()</function>-function, the element can
 
166
        forward the CAPS event to the next element and, if that pad accepts the
173
167
        format too, the element can parse the relevant parameters from the
174
168
        caps and configure itself internally. The caps passed to this function
175
169
        is <emphasis>always</emphasis> a subset of the template caps, so
187
181
--><!-- example-end forwardcaps.c a -->
188
182
<!-- example-begin forwardcaps.c b -->
189
183
static gboolean
190
 
gst_my_filter_setcaps (GstPad  *pad,
191
 
                       GstCaps *caps)
 
184
gst_my_filter_sink_event (GstPad    *pad,
 
185
                          GstObject *parent,
 
186
                          GstEvent  *event)
192
187
{
193
 
  GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
194
 
  GstStructure *s;
195
 
 
196
 
  /* forward-negotiate */
197
 
  if (!gst_pad_set_caps (filter-&gt;srcpad, caps))
198
 
    return FALSE;
199
 
 
200
 
  /* negotiation succeeded, so now configure ourselves */
201
 
  s = gst_caps_get_structure (caps, 0);
202
 
  gst_structure_get_int (s, "rate", &amp;filter-&gt;samplerate);
203
 
  gst_structure_get_int (s, "channels", &amp;filter-&gt;channels);
204
 
 
205
 
  return TRUE;
 
188
  gboolean ret;
 
189
  GstMyFilter *filter = GST_MY_FILTER (parent);
 
190
 
 
191
  switch (GST_EVENT_TYPE (event)) {
 
192
    case GST_EVENT_CAPS:
 
193
    {
 
194
      GstCaps *caps;
 
195
      GstStructure *s;
 
196
 
 
197
      gst_event_parse_caps (event, &amp;caps);
 
198
 
 
199
      /* forward-negotiate */
 
200
      ret = gst_pad_set_caps (filter-&gt;srcpad, caps);
 
201
      if (!ret)
 
202
        return FALSE;
 
203
 
 
204
      /* negotiation succeeded, so now configure ourselves */
 
205
      s = gst_caps_get_structure (caps, 0);
 
206
      gst_structure_get_int (s, "rate", &amp;filter-&gt;samplerate);
 
207
      gst_structure_get_int (s, "channels", &amp;filter-&gt;channels);
 
208
      break;
 
209
    }
 
210
    default:
 
211
      ret = gst_pad_event_default (pad, parent, event);
 
212
      break;
 
213
  }
 
214
  return ret;
206
215
}
207
216
<!-- example-end forwardcaps.c b -->
208
217
<!-- example-begin forwardcaps.c c --><!--
219
228
        then it should call <function>gst_pad_get_allowed_caps ()</function>
220
229
        on its sourcepad to get a list of supported formats on the outputs,
221
230
        and pick the first. The return value of that function is guaranteed
222
 
        to be a subset of the template caps.
 
231
        to be a subset of the template caps or NULL when there is no peer.
223
232
      </para>
224
233
      <para>
225
234
        Let's look at the example of an element that can convert between
246
255
--><!-- example-end convertcaps.c a -->
247
256
<!-- example-begin convertcaps.c b -->
248
257
static gboolean
249
 
gst_my_filter_setcaps (GstPad  *pad,
 
258
gst_my_filter_setcaps (GstMyFilter *filter,
250
259
                       GstCaps *caps)
251
260
{
252
 
  GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
253
 
 
254
261
  if (gst_pad_set_caps (filter-&gt;sinkpad, caps)) {
255
262
    filter-&gt;passthrough = TRUE;
256
263
  } else {
282
289
  return TRUE;
283
290
}
284
291
 
 
292
static gboolean
 
293
gst_my_filter_sink_event (GstPad    *pad,
 
294
                          GstObject *parent,
 
295
                          GstEvent  *event)
 
296
{
 
297
  gboolean ret;
 
298
  GstMyFilter *filter = GST_MY_FILTER (parent);
 
299
 
 
300
  switch (GST_EVENT_TYPE (event)) {
 
301
    case GST_EVENT_CAPS:
 
302
    {
 
303
      GstCaps *caps;
 
304
 
 
305
      gst_event_parse_caps (event, &amp;caps);
 
306
      ret = gst_my_filter_setcaps (filter, caps);
 
307
      break;
 
308
    }
 
309
    default:
 
310
      ret = gst_pad_event_default (pad, parent, event);
 
311
      break;
 
312
  }
 
313
  return ret;
 
314
}
 
315
 
285
316
static GstFlowReturn
286
317
gst_my_filter_chain (GstPad    *pad,
 
318
                     GstObject *parent,
287
319
                     GstBuffer *buf)
288
320
{
289
 
  GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
 
321
  GstMyFilter *filter = GST_MY_FILTER (parent);
290
322
  GstBuffer *out;
291
323
 
292
324
  /* push on if in passthrough mode */
322
354
        Fortunately, the code required to do so is very similar to the last
323
355
        code example in <xref linkend="section-nego-downstream-embed"/>, with
324
356
        the difference being that the caps is selected in the <function>_chain
325
 
        ()</function>-function rather than in the <function>_setcaps
 
357
        ()</function>-function rather than in the <function>_event
326
358
        ()</function>-function. The rest, as for getting all allowed caps from
327
359
        the source pad, fixating and such, is all the same. Re-negotiation,
328
360
        which will be handled in the next section, is very different for such
341
373
      or because the audio channel configuration changed.
342
374
    </para>
343
375
    <para>
344
 
      Upstream caps renegotiation is done in the <function>gst_pad_alloc_buffer
345
 
      ()</function>-function. The idea here is that an element requesting a
346
 
      buffer from downstream, has to specify the type of that buffer. If
347
 
      renegotiation is to take place, this type will no longer apply, and the
348
 
      downstream element will set a new caps on the provided buffer. The element
349
 
      should then reconfigure itself to push buffers with the returned caps. The
350
 
      source pad's setcaps will be called once the buffer is pushed.
 
376
      Upstream caps renegotiation is requested by sending a GST_EVENT_RECONFIGURE
 
377
      event upstream. The idea is that it will instruct the upstream element
 
378
      to reconfigure its caps by doing a new query for the allowed caps and then
 
379
      choosing a new caps. The element that sends out the RECONFIGURE event
 
380
      would influence the selection of the new caps by returning the new
 
381
      prefered caps from its GST_QUERY_CAPS query function. The RECONFIGURE
 
382
      event will set the GST_PAD_FLAG_NEED_RECONFIGURE on all pads that it
 
383
      travels over.
351
384
    </para>
352
385
    <para>
353
386
      It is important to note here that different elements actually have
356
389
    <itemizedlist>
357
390
      <listitem>
358
391
        <para>
359
 
          Elements should implement a <quote>padalloc</quote>-function in
360
 
          order to be able to change format on renegotiation. This is also
361
 
          true for filters and converters.
362
 
        </para>
363
 
      </listitem>
364
 
      <listitem>
365
 
        <para>
366
 
          Elements should allocate new buffers using
367
 
          <function>gst_pad_alloc_buffer ()</function>.
368
 
        </para>
369
 
      </listitem>
370
 
      <listitem>
371
 
        <para>
372
 
          Elements that are renegotiable should implement a
373
 
          <quote>setcaps</quote>-function on their sourcepad as well.
 
392
          Elements that can be reconfigured on the srcpad should check its
 
393
          NEED_RECONFIGURE flag with
 
394
          <function>gst_pad_check_reconfigure ()</function> and it should
 
395
          start renegotiation when the function returns TRUE.
 
396
        </para>
 
397
      </listitem>
 
398
      <listitem>
 
399
        <para>
 
400
          Elements that want to propose a new format upstream need to send
 
401
          a RECONFIGURE event and be prepared to answer the CAPS query with
 
402
          the new prefered format. It should be noted that when there is no
 
403
          upstream element that can (or wants) to renegotiate, the element
 
404
          needs to deal with the currently configured format.
374
405
        </para>
375
406
      </listitem>
376
407
    </itemizedlist>
377
 
    <para>
378
 
      Unfortunately, not all details here have been worked out yet, so this
379
 
      documentation is incomplete. FIXME.
380
 
    </para>
381
408
  </sect1>
382
409
 
383
 
  <sect1 id="section-nego-getcaps" xreflabel="Implementing a getcaps function">
384
 
    <title>Implementing a getcaps function</title>
 
410
  <sect1 id="section-nego-getcaps" xreflabel="Implementing a CAPS query function">
 
411
    <title>Implementing a CAPS query function</title>
385
412
    <para>
386
 
      A <function>_getcaps ()</function>-function is called when a peer
387
 
      element would like to know which formats this element supports, and
388
 
      in what order of preference. The return value should be all formats
389
 
      that this elements supports, taking into account limitations of peer
390
 
      elements further downstream or upstream, sorted by order of preference,
391
 
      highest preference first.
 
413
      A <function>_query ()</function>-function with the GST_QUERY_CAPS query
 
414
      type is called when a peer element would like to know which formats
 
415
      this pad supports, and in what order of preference. The return value
 
416
      should be all formats that this elements supports, taking into account
 
417
      limitations of peer elements further downstream or upstream, sorted by
 
418
      order of preference, highest preference first.
392
419
    </para>
393
420
    <para>
394
421
    </para>
396
423
#include "init.func"
397
424
--><!-- example-end getcaps.c a -->
398
425
<!-- example-begin getcaps.c b -->
399
 
static GstCaps *
400
 
gst_my_filter_getcaps (GstPad *pad)
 
426
static gboolean
 
427
gst_my_filter_query (GstPad *pad, GstObject * parent, GstQuery * query)
401
428
{
402
 
  GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
403
 
  GstPad *otherpad = (pad == filter-&gt;srcpad) ? filter-&gt;sinkpad :
404
 
                                                  filter-&gt;srcpad;
405
 
  GstCaps *othercaps = gst_pad_get_allowed_caps (otherpad), *caps;
406
 
  gint i;
407
 
 
408
 
  /* We support *any* samplerate, indifferent from the samplerate
409
 
   * supported by the linked elements on both sides. */
410
 
  for (i = 0; i &lt; gst_caps_get_size (othercaps); i++) {
411
 
    GstStructure *structure = gst_caps_get_structure (othercaps, i);
412
 
 
413
 
    gst_structure_remove_field (structure, "rate");
 
429
  gboolean ret;
 
430
  GstMyFilter *filter = GST_MY_FILTER (parent);
 
431
 
 
432
  switch (GST_QUERY_TYPE (query)) {
 
433
    case GST_QUERY_CAPS
 
434
    {
 
435
      GstPad *otherpad;
 
436
      GstCaps *temp, *caps, *filter, *tcaps;
 
437
      gint i;
 
438
 
 
439
      otherpad = (pad == filter-&gt;srcpad) ? filter-&gt;sinkpad :
 
440
                                              filter-&gt;srcpad;
 
441
      caps = gst_pad_get_allowed_caps (otherpad);
 
442
 
 
443
      gst_query_parse_caps (query, &amp;filter);
 
444
 
 
445
      /* We support *any* samplerate, indifferent from the samplerate
 
446
       * supported by the linked elements on both sides. */
 
447
      for (i = 0; i &lt; gst_caps_get_size (caps); i++) {
 
448
        GstStructure *structure = gst_caps_get_structure (caps, i);
 
449
 
 
450
        gst_structure_remove_field (structure, "rate");
 
451
      }
 
452
 
 
453
      /* make sure we only return results that intersect our
 
454
       * padtemplate */
 
455
      tcaps = gst_pad_get_pad_template_caps (pad);
 
456
      if (tcaps) {
 
457
        temp = gst_caps_intersect (caps, tcaps);
 
458
        gst_caps_unref (caps);
 
459
        gst_caps_unref (tcaps);
 
460
        caps = temp;
 
461
      }
 
462
      /* filter against the query filter when needed */
 
463
      if (filter) {
 
464
        temp = gst_caps_intersect (caps, filter);
 
465
        gst_caps_unref (caps);
 
466
        caps = temp;
 
467
      }
 
468
      gst_query_set_caps_result (query, caps);
 
469
      gst_caps_unref (caps);
 
470
      ret = TRUE;
 
471
      break;
 
472
    }
 
473
    default:
 
474
      ret = gst_pad_query_default (pad, parent, query);
 
475
      break;
414
476
  }
415
 
  caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
416
 
  gst_caps_unref (othercaps);
417
 
 
418
 
  return caps;
 
477
  return ret;
419
478
}
420
479
<!-- example-end getcaps.c b -->
421
480
<!-- example-begin getcaps.c c --><!--