other-source.xml 13.9 KB
Newer Older
1 2 3

<!-- ############ chapter ############# -->

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
4
<chapter id="chapter-other-source" xreflabel="Writing a Source">
5 6
  <title>Writing a Source</title>
  <para>
7 8 9 10 11 12 13
    Source elements are the start of a data streaming pipeline. Source
    elements have no sink pads and have one or more source pads. We will
    focus on single-sourcepad elements here, but the concepts apply equally
    well to multi-sourcepad elements. This chapter will explain the essentials
    of source elements, which features it should implement and which it
    doesn't have to, and how source elements will interact with other
    elements in a pipeline.
14
  </para>
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

  <sect1 id="section-source-getfn" xreflabel="The get()-function">
    <title>The get()-function</title>
    <para>
      Source elements have the special option of having a
      <function>_get ()</function>-function rather than a
      <function>_loop ()</function>- or <function>_chain
      ()</function>-function. A <function>_get ()</function>-function is
      called by the scheduler every time the next elements needs data. Apart
      from corner cases, every source element will want to be <function>_get
      ()</function>-based.
    </para>
    <programlisting>
static GstData *	gst_my_source_get	(GstPad *pad);

static void
gst_my_source_init (GstMySource *src)
{
[..]
  gst_pad_set_get_function (src->srcpad, gst_my_source_get);
}

static GstData *
gst_my_source_get (GstPad *pad)
{
  GstBuffer *buffer;

  buffer = gst_buffer_new ();
  GST_BUFFER_DATA (buf) = g_strdup ("hello pipeline!");
  GST_BUFFER_SIZE (buf) = strlen (GST_BUFFER_DATA (buf));
  /* terminating '/0' */
  GST_BUFFER_MAZSIZE (buf) = GST_BUFFER_SIZE (buf) + 1;

  return GST_DATA (buffer);
}
    </programlisting>
  </sect1>

  <sect1 id="section-source-padfn" xreflabel="Events, querying and converting">
    <title>Events, querying and converting</title>
    <para>
      One of the most important functions of source elements is to
      implement correct query, convert and event handling functions.
      Those will continuously describe the current state of the stream.
      Query functions can be used to get stream properties such as current
      position and length. This can be used by fellow elements to convert
Piotr Fusik's avatar
Piotr Fusik committed
61
      this same value into a different unit, or by applications to provide
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
      information about the length/position of the stream to the user.
      Conversion functions are used to convert such values from one unit
      to another. Lastly, events are mostly used to seek to positions
      inside the stream. Any function is essentially optional, but the
      element should try to provide as much information as it knows. Note
      that elements providing an event function should also list their
      supported events in an <function>_get_event_mask ()</function>
      function. Elements supporting query operations should list the
      supported operations in a <function>_get_query_types
      ()</function> function. Elements supporting either conversion
      or query operations should also implement a <function>_get_formats
      ()</function> function.
    </para>
    <para>
      An example source element could, for example, be an element that
      continuously generates a wave tone at 44,1 kHz, mono, 16-bit. This
      element will generate 44100 audio samples per second or 88,2 kB/s.
      This information can be used to implement such functions:
    </para>
    <programlisting>
static GstFormat *	gst_my_source_format_list	(GstPad      *pad);
static GstQueryType *	gst_my_source_query_list	(GstPad      *pad);

static gboolean		gst_my_source_convert		(GstPad      *pad,
							 GstFormat    from_fmt,
							 gint64       from_val,
							 GstFormat   *to_fmt,
							 gint64      *to_val);
static gboolean		gst_my_source_query		(GstPad      *pad,
							 GstQueryType type,
							 GstFormat   *to_fmt,
							 gint64      *to_val);

static void
gst_my_source_init (GstMySource *src)
{
[..]
  gst_pad_set_convert_function (src->srcpad, gst_my_source_convert);
  gst_pad_set_formats_function (src->srcpad, gst_my_source_format_list);
  gst_pad_set_query_function (src->srcpad, gst_my_source_query);
  gst_pad_set_query_type_function (src->srcpad, gst_my_source_query_list);
}

/*
 * This function returns an enumeration of supported GstFormat
 * types in the query() or convert() functions. See gst/gstformat.h
 * for a full list.
 */

static GstFormat *
gst_my_source_format_list (GstPad *pad)
{
  static const GstFormat formats[] = {
    GST_FORMAT_TIME,
    GST_FORMAT_DEFAULT, /* means "audio samples" */
    GST_FORMAT_BYTES,
    0
  };

  return formats;
}

/*
 * This function returns an enumeration of the supported query()
 * operations. Since we generate audio internally, we only provide
 * an indication of how many samples we've played so far. File sources
 * or such elements could also provide GST_QUERY_TOTAL for the total
 * stream length, or other things. See gst/gstquery.h for details.
 */

static GstQueryType *
gst_my_source_query_list (GstPad *pad)
{
  static const GstQueryType query_types[] = {
    GST_QUERY_POSITION,
    0,
  };

  return query_types;
}

/*
 * And below are the logical implementations.
 */

static gboolean
gst_my_source_convert (GstPad    *pad,
		       GstFormat  from_fmt,
		       gint64     from_val,
		       GstFormat *to_fmt,
		       gint64    *to_val)
{
  gboolean res = TRUE;
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));

  switch (from_fmt) {
    case GST_FORMAT_TIME:
      switch (*to_fmt) {
        case GST_FORMAT_TIME:
          /* nothing */
          break;

        case GST_FORMAT_BYTES:
          *to_val = from_val / (GST_SECOND / (44100 * 2));
          break;

        case GST_FORMAT_DEFAULT:
          *to_val = from_val / (GST_SECOND / 44100);
          break;

        default:
          res = FALSE;
          break;
      }
      break;

    case GST_FORMAT_BYTES:
      switch (*to_fmt) {
        case GST_FORMAT_TIME:
          *to_val = from_val * (GST_SECOND / (44100 * 2));
          break;

        case GST_FORMAT_BYTES:
          /* nothing */
          break;

        case GST_FORMAT_DEFAULT:
          *to_val = from_val / 2;
          break;

        default:
          res = FALSE;
          break;
      }
      break;

    case GST_FORMAT_DEFAULT:
      switch (*to_fmt) {
        case GST_FORMAT_TIME:
          *to_val = from_val * (GST_SECOND / 44100);
          break;

        case GST_FORMAT_BYTES:
          *to_val = from_val * 2;
          break;

        case GST_FORMAT_DEFAULT:
          /* nothing */
          break;

        default:
          res = FALSE;
          break;
      }
      break;

    default:
      res = FALSE;
      break;
  }

  return res;
}

static gboolean
gst_my_source_query (GstPad      *pad,
		     GstQueryType type,
		     GstFormat   *to_fmt,
		     gint64      *to_val)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));
  gboolean res = TRUE;

  switch (type) {
    case GST_QUERY_POSITION:
      res = gst_pad_convert (pad, GST_FORMAT_BYTES, src->total_bytes,
                             to_fmt, to_val);
      break;

    default:
      res = FALSE;
      break;
  }

  return res;
}
    </programlisting>
    <para>
      Be sure to increase src->total_bytes after each call to your
      <function>_get ()</function> function.
    </para>
    <para>
      Event handling has already been explained previously in the events
      chapter.
    </para>
  </sect1>

  <sect1 id="section-source-sync" xreflabel="Time, clocking and synchronization">
    <title>Time, clocking and synchronization</title>
    <para>
      The above example does not provide any timing info, but will suffice
      for elementary data sources such as a file source or network data
      source element. Things become slightly more complicated, but still
      very simple, if we create artificial video or audio data sources,
      such as a video test image source or an artificial audio source (e.g.
267
      <classname>audiotestsrc</classname>).
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
      It will become more complicated if we want the element to be a
      realtime capture source, such as a video4linux source (for reading
      video frames from a TV card) or an ALSA source (for reading data
      from soundcards supported by an ALSA-driver). Here, we will need to
      make the element aware of timing and clocking.
    </para>
    <para>
      Timestamps can essentially be generated from all the information
      given above without any difficulty. We could add a very small amount
      of code to generate perfectly timestamped buffers from our
      <function>_get ()</function>-function:
    </para>
    <programlisting>
static void
gst_my_source_init (GstMySource *src)
{
[..]
  src->total_bytes = 0;
}

static GstData *
gst_my_source_get (GstPad *pad)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));
  GstBuffer *buf;
  GstFormat fmt = GST_FORMAT_TIME;
[..]
  GST_BUFFER_DURATION (buf) = GST_BUFFER_SIZE (buf) * (GST_SECOND / (44100 * 2));
  GST_BUFFER_TIMESTAMP (buf) = src->total_bytes * (GST_SECOND / (44100 * 2));
  src->total_bytes += GST_BUFFER_SIZE (buf);

  return GST_DATA (buf);
}

302 303
static GstStateChangeReturn
gst_my_source_change_state (GstElement *element, GstStateChange transition)
304
{
305
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
306 307
  GstMySource *src = GST_MY_SOURCE (element);

308 309 310 311
  /* First, handle upwards state changes */
  switch (transition) {
    case GST_STATE_READY_TO_PAUSED:
      /* do something */
312 313 314 315 316
      break;
    default:
      break;
  }

317 318 319 320 321 322 323 324 325 326 327 328
  ret = GST_ELEMENT_CLASS (parent_class)-&gt;change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  /* Now handle downwards state changes after chaining up */
  switch (transition) {
    case GST_STATE_PAUSED_TO_READY:
      src->total_bytes = 0;
      break;
    default:
      break;
  }
329

330
  return ret;
331 332 333 334 335 336 337
}
    </programlisting>
    <para>
      That wasn't too hard. Now, let's assume real-time elements. Those
      can either have hardware-timing, in which case we can rely on backends
      to provide sync for us (in which case you probably want to provide a
      clock), or we will have to emulate that internally (e.g. to acquire
338 339
      sync in artificial data elements such as
      <classname>audiotestsrc</classname>).
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      Let's first look at the second option (software sync). The first option
      (hardware sync + providing a clock) does not require any special code
      with respect to timing, and the clocking section already explained how
      to provide a clock.
    </para>
    <programlisting>
enum {
  ARG_0,
[..]
  ARG_SYNC,
[..]
};

static void
gst_my_source_class_init (GstMySourceClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
[..]
  g_object_class_install_property (object_class, ARG_SYNC,
      g_param_spec_boolean ("sync", "Sync", "Synchronize to clock",
360 361
                            FALSE, G_PARAM_READWRITE |
                                G_PARAM_STATIC_STRINGS));
362 363 364 365 366 367 368 369 370 371
[..]
}

static void
gst_my_source_init (GstMySource *src)
{
[..]
  src->sync = FALSE;
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
372
static GstData *
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
gst_my_source_get (GstPad *pad)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));
  GstBuffer *buf;
[..]
  if (src->sync) {
    /* wait on clock */
    gst_element_wait (GST_ELEMENT (src), GST_BUFFER_TIMESTAMP (buf));
  }

  return GST_DATA (buf);
}

static void
gst_my_source_get_property (GObject    *object,
			    guint       prop_id,
			    GParamSpec *pspec,
			    GValue     *value)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));

  switch (prop_id) {
[..]
    case ARG_SYNC:
      g_value_set_boolean (value, src->sync);
      break;
[..]
  }
}

static void
gst_my_source_get_property (GObject      *object,
			    guint         prop_id,
			    GParamSpec   *pspec,
			    const GValue *value)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));

  switch (prop_id) {
[..]
    case ARG_SYNC:
      src->sync = g_value_get_boolean (value);
      break;
[..]
  }
}
    </programlisting>
    <para>
      Most of this is GObject wrapping code. The actual code to do
      software-sync (in the <function>_get ()</function>-function)
      is relatively small.
    </para>
  </sect1>
  <sect1 id="section-source-buffers" xreflabel="Using special memory">
    <title>Using special memory</title>
    <para>
      In some cases, it might be useful to use specially allocated memory
      (e.g. <function>mmap ()</function>'ed DMA'able memory) in
      your buffers, and those will require special handling when they are
      being dereferenced. For this, &GStreamer; uses the concept of
      buffer-free functions. Those are special functions pointers that an
      element can set on buffers that it created itself. The given function
      will be called when the buffer has been dereferenced, so that the
      element can clean up or re-use memory internally rather than using
      the default implementation (which simply calls
      <function>g_free ()</function> on the data pointer).
    </para>
    <programlisting>
static void
gst_my_source_buffer_free (GstBuffer *buf)
{
  GstMySource *src = GST_MY_SOURCE (GST_BUFFER_PRIVATE (buf));

  /* do useful things here, like re-queueing the buffer which
   * makes it available for DMA again. The default handler will
   * not free this buffer because of the GST_BUFFER_DONTFREE
   * flag. */
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
452
static GstData *
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
gst_my_source_get (GstPad *pad)
{
  GstMySource *src = GST_MY_SOURCE (gst_pad_get_parent (pad));
  GstBuffer *buf;
[..]
  buf = gst_buffer_new ();
  GST_BUFFER_FREE_DATA_FUNC (buf) = gst_my_source_buffer_free;
  GST_BUFFER_PRIVATE (buf) = src;
  GST_BUFFER_FLAG_SET (buf, GST_BUFFER_READONLY | GST_BUFFER_DONTFREE);
[..]

  return GST_DATA (buf);
}
    </programlisting>
    <para>
      Note that this concept should <emphasis>not</emphasis> be used to
      decrease the number of calls made to functions such as
      <function>g_malloc ()</function> inside your element. We
      have better ways of doing that elsewhere (&GStreamer; core, Glib,
      Glibc, Linux kernel, etc.).
    </para>
  </sect1>
475
</chapter>