gstbasesrc.c 75 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3
 *               2000,2005 Wim Taymans <wim@fluendo.com>
4
 *
5
 * gstbasesrc.c:
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23 24 25
/**
 * SECTION:gstbasesrc
 * @short_description: Base class for getrange based source elements
26
 * @see_also: #GstPushSrc, #GstBaseTransform, #GstBaseSink
27
 *
28 29 30 31 32 33 34 35
 * This is a generice base class for source elements. The following
 * types of sources are supported:
 * <itemizedlist>
 *   <listitem><para>random access sources like files</para></listitem>
 *   <listitem><para>seekable sources</para></listitem>
 *   <listitem><para>live sources</para></listitem>
 * </itemizedlist>
 *
36 37
 * <refsect2>
 * <para>
38 39
 * The source can be configured to operate in any #GstFormat with the
 * gst_base_src_set_format() method. The currently set format determines 
40
 * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT 
41
 * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
42 43
 * </para>
 * <para>
44
 * #GstBaseSrc always supports push mode scheduling. If the following
45 46
 * conditions are met, it also supports pull mode scheduling:
 * <itemizedlist>
47
 *   <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
48
 *   </listitem>
49
 *   <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
50
 *   </listitem>
51
 * </itemizedlist>
52 53
 * </para>
 * <para>
54
 * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any 
55
 * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE. 
56 57
 * </para>
 * <para>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
58
 * If all the conditions are met for operating in pull mode, #GstBaseSrc is
59 60
 * automatically seekable in push mode as well. The following conditions must 
 * be met to make the element seekable in push mode when the format is not
61
 * #GST_FORMAT_BYTES:
62
 * <itemizedlist>
63
 *   <listitem><para>
64
 *     #GstBaseSrc::is_seekable returns %TRUE.
65 66
 *   </para></listitem>
 *   <listitem><para>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
67
 *     #GstBaseSrc::query can convert all supported seek formats to the
68 69 70
 *     internal format as set with gst_base_src_set_format().
 *   </para></listitem>
 *   <listitem><para>
71
 *     #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
72
 *   </para></listitem>
73
 * </itemizedlist>
74 75
 * </para>
 * <para>
76 77 78 79 80 81
 * When the element does not meet the requirements to operate in pull mode,
 * the offset and length in the #GstBaseSrc::create method should be ignored.
 * It is recommended to subclass #GstPushSrc instead, in this situation. If the
 * element can operate in pull mode but only with specific offsets and
 * lengths, it is allowed to generate an error when the wrong values are passed
 * to the #GstBaseSrc::create function.
82 83
 * </para>
 * <para>
84 85 86 87
 * #GstBaseSrc has support for live sources. Live sources are sources that when
 * paused discard data, such as audio or video capture devices. A typical live
 * source also produces data at a fixed rate and thus provides a clock to publish
 * this rate.
88
 * Use gst_base_src_set_live() to activate the live source mode.
89 90
 * </para>
 * <para>
91
 * A live source does not produce data in the PAUSED state. This means that the 
92
 * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
93
 * To signal the pipeline that the element will not produce data, the return
94
 * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
95 96
 * </para>
 * <para>
97
 * A typical live source will timestamp the buffers it creates with the 
98
 * current running time of the pipeline. This is one reason why a live source
99
 * can only produce data in the PLAYING state, when the clock is actually 
100
 * distributed and running. 
101 102
 * </para>
 * <para>
103
 * Live sources that synchronize and block on the clock (an audio source, for
104 105 106 107
 * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
 * function was interrupted by a state change to PAUSED.
 * </para>
 * <para>
108
 * The #GstBaseSrc::get_times method can be used to implement pseudo-live 
109
 * sources.
110
 * It only makes sense to implement the ::get_times function if the source is 
111 112 113 114 115
 * a live source. The ::get_times function should return timestamps starting
 * from 0, as if it were a non-live source. The base class will make sure that
 * the timestamps are transformed into the current running_time.
 * The base source will then wait for the calculated running_time before pushing
 * out the buffer.
116 117
 * </para>
 * <para>
118 119 120 121 122 123
 * For live sources, the base class will by default report a latency of 0.
 * For pseudo live sources, the base class will by default measure the difference
 * between the first buffer timestamp and the start time of get_times and will
 * report this value as the latency. 
 * Subclasses should override the query function when this behaviour is not
 * acceptable.
124 125
 * </para>
 * <para>
126 127
 * There is only support in #GstBaseSrc for exactly one source pad, which 
 * should be named "src". A source implementation (subclass of #GstBaseSrc) 
128
 * should install a pad template in its base_init function, like so:
129 130
 * </para>
 * <para>
131 132 133 134 135 136 137 138 139 140 141 142 143
 * <programlisting>
 * static void
 * my_element_base_init (gpointer g_class)
 * {
 *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
 *   // srctemplate should be a #GstStaticPadTemplate with direction
 *   // #GST_PAD_SRC and name "src"
 *   gst_element_class_add_pad_template (gstelement_class,
 *       gst_static_pad_template_get (&amp;srctemplate));
 *   // see #GstElementDetails
 *   gst_element_class_set_details (gstelement_class, &amp;details);
 * }
 * </programlisting>
144 145 146 147 148
 * </para>
 * <title>Controlled shutdown of live sources in applications</title>
 * <para>
 * Applications that record from a live source may want to stop recording
 * in a controlled way, so that the recording is stopped, but the data
149
 * already in the pipeline is processed to the end (remember that many live
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
 * sources would go on recording forever otherwise). For that to happen the
 * application needs to make the source stop recording and send an EOS
 * event down the pipeline. The application would then wait for an
 * EOS message posted on the pipeline's bus to know when all data has
 * been processed and the pipeline can safely be stopped.
 * </para>
 * <para>
 * Since GStreamer 0.10.3 an application may simply set the source
 * element to NULL or READY state to make it send an EOS event downstream.
 * The application should lock the state of the source afterwards, so that
 * shutting down the pipeline from PLAYING doesn't temporarily start up the
 * source element for a second time:
 * <programlisting>
 * ...
 * // stop recording
165 166
 * gst_element_set_state (audio_source, #GST_STATE_NULL);
 * gst_element_set_locked_state (audio_source, %TRUE);
167 168 169 170 171 172 173 174
 * ...
 * </programlisting>
 * Now the application should wait for an EOS message
 * to be posted on the pipeline's bus. Once it has received
 * an EOS message, it may safely shut down the entire pipeline:
 * <programlisting>
 * ...
 * // everything done - shut down pipeline
175 176
 * gst_element_set_state (pipeline, #GST_STATE_NULL);
 * gst_element_set_locked_state (audio_source, %FALSE);
177 178 179 180
 * ...
 * </programlisting>
 * </para>
 * <para>
181 182 183 184 185
 * Note that setting the source element to NULL or READY when the 
 * pipeline is in the PAUSED state may cause a deadlock since the streaming
 * thread might be blocked in PREROLL.
 * </para>
 * <para>
186
 * Last reviewed on 2007-09-13 (0.10.15)
187 188
 * </para>
 * </refsect2>
189
 */
190 191 192 193 194

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

195 196 197
#include <stdlib.h>
#include <string.h>

198
#include "gstbasesrc.h"
Wim Taymans's avatar
Wim Taymans committed
199
#include "gsttypefindhelper.h"
200
#include <gst/gstmarshal.h>
201
#include <gst/gst-i18n-lib.h>
202

203 204
GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
#define GST_CAT_DEFAULT gst_base_src_debug
205

206 207 208 209 210 211 212
#define GST_LIVE_GET_LOCK(elem)               (GST_BASE_SRC_CAST(elem)->live_lock)
#define GST_LIVE_LOCK(elem)                   g_mutex_lock(GST_LIVE_GET_LOCK(elem))
#define GST_LIVE_TRYLOCK(elem)                g_mutex_trylock(GST_LIVE_GET_LOCK(elem))
#define GST_LIVE_UNLOCK(elem)                 g_mutex_unlock(GST_LIVE_GET_LOCK(elem))
#define GST_LIVE_GET_COND(elem)               (GST_BASE_SRC_CAST(elem)->live_cond)
#define GST_LIVE_WAIT(elem)                   g_cond_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem))
#define GST_LIVE_TIMED_WAIT(elem, timeval)    g_cond_timed_wait (GST_LIVE_GET_COND (elem), GST_LIVE_GET_LOCK (elem),\
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
213
                                                                                timeval)
214 215 216
#define GST_LIVE_SIGNAL(elem)                 g_cond_signal (GST_LIVE_GET_COND (elem));
#define GST_LIVE_BROADCAST(elem)              g_cond_broadcast (GST_LIVE_GET_COND (elem));

217 218 219 220 221 222 223
/* BaseSrc signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

224 225 226
#define DEFAULT_BLOCKSIZE       4096
#define DEFAULT_NUM_BUFFERS     -1
#define DEFAULT_TYPEFIND	FALSE
227
#define DEFAULT_DO_TIMESTAMP	FALSE
228

229 230
enum
{
231 232
  PROP_0,
  PROP_BLOCKSIZE,
233
  PROP_NUM_BUFFERS,
234
  PROP_TYPEFIND,
235
  PROP_DO_TIMESTAMP
236 237
};

238 239 240 241 242 243 244
#define GST_BASE_SRC_GET_PRIVATE(obj)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SRC, GstBaseSrcPrivate))

struct _GstBaseSrcPrivate
{
  gboolean last_sent_eos;       /* last thing we did was send an EOS (we set this
                                 * to avoid the sending of two EOS in some cases) */
245
  gboolean discont;
246 247 248 249

  /* two segments to be sent in the streaming thread with STREAM_LOCK */
  GstEvent *close_segment;
  GstEvent *start_segment;
250 251 252 253

  /* startup latency is the time it takes between going to PLAYING and producing
   * the first BUFFER with running_time 0. This value is included in the latency
   * reporting. */
254 255 256 257
  GstClockTime latency;
  /* timestamp offset, this is the offset add to the values of gst_times for
   * pseudo live sources */
  GstClockTimeDiff ts_offset;
258 259

  gboolean do_timestamp;
260 261
};

262 263
static GstElementClass *parent_class = NULL;

264 265 266
static void gst_base_src_base_init (gpointer g_class);
static void gst_base_src_class_init (GstBaseSrcClass * klass);
static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
267 268
static void gst_base_src_finalize (GObject * object);

269 270

GType
271
gst_base_src_get_type (void)
272
{
273
  static GType base_src_type = 0;
274

275
  if (G_UNLIKELY (base_src_type == 0)) {
276
    static const GTypeInfo base_src_info = {
277
      sizeof (GstBaseSrcClass),
278
      (GBaseInitFunc) gst_base_src_base_init,
279
      NULL,
280
      (GClassInitFunc) gst_base_src_class_init,
281 282 283 284
      NULL,
      NULL,
      sizeof (GstBaseSrc),
      0,
285
      (GInstanceInitFunc) gst_base_src_init,
286 287
    };

288 289
    base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
        "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
290
  }
291
  return base_src_type;
292
}
293 294
static GstCaps *gst_base_src_getcaps (GstPad * pad);
static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
295
static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
296

297 298 299
static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
static void gst_base_src_set_property (GObject * object, guint prop_id,
300
    const GValue * value, GParamSpec * pspec);
301
static void gst_base_src_get_property (GObject * object, guint prop_id,
302
    GValue * value, GParamSpec * pspec);
303
static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
304
static gboolean gst_base_src_send_event (GstElement * elem, GstEvent * event);
305
static gboolean gst_base_src_default_event (GstBaseSrc * src, GstEvent * event);
306
static const GstQueryType *gst_base_src_get_query_types (GstElement * element);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
307

308
static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
309

310
static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
311 312 313
static gboolean gst_base_src_default_do_seek (GstBaseSrc * src,
    GstSegment * segment);
static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
314 315
static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
    GstEvent * event, GstSegment * segment);
316

317
static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
318
static gboolean gst_base_src_unlock_stop (GstBaseSrc * basesrc);
319 320
static gboolean gst_base_src_start (GstBaseSrc * basesrc);
static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
321

322 323
static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
    GstStateChange transition);
324

325
static void gst_base_src_loop (GstPad * pad);
326 327
static gboolean gst_base_src_pad_check_get_range (GstPad * pad);
static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
328 329 330
static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
    guint length, GstBuffer ** buf);
static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
331 332 333
    guint length, GstBuffer ** buf);

static void
334
gst_base_src_base_init (gpointer g_class)
335
{
336
  GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
337 338 339
}

static void
340
gst_base_src_class_init (GstBaseSrcClass * klass)
341 342 343 344
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

345 346
  gobject_class = G_OBJECT_CLASS (klass);
  gstelement_class = GST_ELEMENT_CLASS (klass);
347

348 349
  g_type_class_add_private (klass, sizeof (GstBaseSrcPrivate));

350
  parent_class = g_type_class_peek_parent (klass);
351

352
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
353 354
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
355

356
  g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
357
      g_param_spec_ulong ("blocksize", "Block size",
358 359
          "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
          DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
360
  g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
361 362 363
      g_param_spec_int ("num-buffers", "num-buffers",
          "Number of buffers to output before sending EOS", -1, G_MAXINT,
          DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
364
  g_object_class_install_property (gobject_class, PROP_TYPEFIND,
365 366 367
      g_param_spec_boolean ("typefind", "Typefind",
          "Run typefind before negotiating", DEFAULT_TYPEFIND,
          G_PARAM_READWRITE));
368 369 370 371
  g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
      g_param_spec_boolean ("do-timestamp", "Do timestamp",
          "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
          G_PARAM_READWRITE));
372

373 374
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_base_src_change_state);
375
  gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
376 377
  gstelement_class->get_query_types =
      GST_DEBUG_FUNCPTR (gst_base_src_get_query_types);
378

379 380 381 382
  klass->negotiate = GST_DEBUG_FUNCPTR (gst_base_src_default_negotiate);
  klass->event = GST_DEBUG_FUNCPTR (gst_base_src_default_event);
  klass->do_seek = GST_DEBUG_FUNCPTR (gst_base_src_default_do_seek);
  klass->query = GST_DEBUG_FUNCPTR (gst_base_src_default_query);
383 384
  klass->check_get_range =
      GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
385 386
  klass->prepare_seek_segment =
      GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
387 388 389
}

static void
390
gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
391 392 393 394
{
  GstPad *pad;
  GstPadTemplate *pad_template;

395 396
  basesrc->priv = GST_BASE_SRC_GET_PRIVATE (basesrc);

397 398 399
  basesrc->is_live = FALSE;
  basesrc->live_lock = g_mutex_new ();
  basesrc->live_cond = g_cond_new ();
400 401
  basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
  basesrc->num_buffers_left = -1;
402

403 404 405
  basesrc->can_activate_push = TRUE;
  basesrc->pad_mode = GST_ACTIVATE_NONE;

406 407 408 409
  pad_template =
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
  g_return_if_fail (pad_template != NULL);

410
  GST_DEBUG_OBJECT (basesrc, "creating src pad");
411 412
  pad = gst_pad_new_from_template (pad_template, "src");

413
  GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
414 415 416 417 418 419 420 421
  gst_pad_set_activatepush_function (pad,
      GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
  gst_pad_set_activatepull_function (pad,
      GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
  gst_pad_set_event_function (pad,
      GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
  gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
  gst_pad_set_checkgetrange_function (pad,
422
      GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
423
  gst_pad_set_getrange_function (pad,
424
      GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
425 426
  gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
  gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
427 428
  gst_pad_set_fixatecaps_function (pad,
      GST_DEBUG_FUNCPTR (gst_base_src_fixate));
429

430
  /* hold pointer to pad */
431
  basesrc->srcpad = pad;
432
  GST_DEBUG_OBJECT (basesrc, "adding src pad");
433 434 435
  gst_element_add_pad (GST_ELEMENT (basesrc), pad);

  basesrc->blocksize = DEFAULT_BLOCKSIZE;
Wim Taymans's avatar
Wim Taymans committed
436
  basesrc->clock_id = NULL;
437 438
  /* we operate in BYTES by default */
  gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
439
  basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
440
  basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
441

442
  GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
443 444

  GST_DEBUG_OBJECT (basesrc, "init done");
445 446
}

447 448 449 450
static void
gst_base_src_finalize (GObject * object)
{
  GstBaseSrc *basesrc;
451
  GstEvent **event_p;
452 453 454 455 456

  basesrc = GST_BASE_SRC (object);

  g_mutex_free (basesrc->live_lock);
  g_cond_free (basesrc->live_cond);
457 458 459

  event_p = &basesrc->data.ABI.pending_seek;
  gst_event_replace ((GstEvent **) event_p, NULL);
460 461 462 463

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

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/**
 * gst_base_src_wait_playing:
 * @src: the src
 *
 * If the #GstBaseSrcClass::create method performs its own synchronisation against
 * the clock it must unblock when going from PLAYING to the PAUSED state and call
 * this method before continuing to produce the remaining data.
 *
 * This function will block until a state change to PLAYING happens (in which
 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
 * to a state change to READY or a FLUSH event (in which case this function
 * returns #GST_FLOW_WRONG_STATE).
 *
 * Since: 0.10.12
 *
 * Returns: #GST_FLOW_OK if @src is PLAYING and processing can
 * continue. Any other return value should be returned from the create vmethod.
 */
GstFlowReturn
gst_base_src_wait_playing (GstBaseSrc * src)
{
  /* block until the state changes, or we get a flush, or something */
  GST_LIVE_LOCK (src);
  if (src->is_live) {
    while (G_UNLIKELY (!src->live_running)) {
      GST_DEBUG ("live source signal waiting");
      GST_LIVE_SIGNAL (src);
      GST_DEBUG ("live source waiting for running state");
      GST_LIVE_WAIT (src);
      GST_DEBUG ("live source unlocked");
    }
    /* FIXME, use another variable to signal stopping so that we don't
     * have to grab another lock. */
    GST_OBJECT_LOCK (src->srcpad);
    if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
      goto flushing;
    GST_OBJECT_UNLOCK (src->srcpad);
  }
  GST_LIVE_UNLOCK (src);

  return GST_FLOW_OK;

  /* ERRORS */
flushing:
  {
    GST_DEBUG_OBJECT (src, "pad is flushing");
    GST_OBJECT_UNLOCK (src->srcpad);
    GST_LIVE_UNLOCK (src);
    return GST_FLOW_WRONG_STATE;
  }
}

516 517 518 519 520
/**
 * gst_base_src_set_live:
 * @src: base source instance
 * @live: new live-mode
 *
521 522 523 524 525 526 527 528
 * If the element listens to a live source, @live should
 * be set to %TRUE. 
 *
 * A live source will not produce data in the PAUSED state and
 * will therefore not be able to participate in the PREROLL phase
 * of a pipeline. To signal this fact to the application and the 
 * pipeline, the state change return value of the live source will
 * be GST_STATE_CHANGE_NO_PREROLL.
529
 */
530
void
531
gst_base_src_set_live (GstBaseSrc * src, gboolean live)
532 533 534 535 536 537
{
  GST_LIVE_LOCK (src);
  src->is_live = live;
  GST_LIVE_UNLOCK (src);
}

538
/**
539
 * gst_base_src_is_live:
540 541 542 543 544 545
 * @src: base source instance
 *
 * Check if an element is in live mode.
 *
 * Returns: %TRUE if element is in live mode.
 */
546
gboolean
547
gst_base_src_is_live (GstBaseSrc * src)
548 549 550 551 552 553 554 555 556 557
{
  gboolean result;

  GST_LIVE_LOCK (src);
  result = src->is_live;
  GST_LIVE_UNLOCK (src);

  return result;
}

558 559 560 561 562 563 564 565 566 567
/**
 * gst_base_src_set_format:
 * @src: base source instance
 * @format: the format to use
 *
 * Sets the default format of the source. This will be the format used
 * for sending NEW_SEGMENT events and for performing seeks.
 *
 * If a format of GST_FORMAT_BYTES is set, the element will be able to
 * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
568 569
 *
 * @Since: 0.10.1
570 571 572 573 574 575 576
 */
void
gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
{
  gst_segment_init (&src->segment, format);
}

577 578 579 580 581 582 583 584
/**
 * gst_base_src_query_latency:
 * @src: the source
 * @live: if the source is live
 * @min_latency: the min latency of the source
 * @max_latency: the max latency of the source
 *
 * Query the source for the latency parameters. @live will be TRUE when @src is
585 586
 * configured as a live source. @min_latency will be set to the difference
 * between the running time and the timestamp of the first buffer.
587 588 589 590 591 592 593 594 595 596 597 598
 * @max_latency is always the undefined value of -1.
 *
 * This function is mostly used by subclasses. 
 *
 * Returns: TRUE if the query succeeded.
 *
 * Since: 0.10.13
 */
gboolean
gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
    GstClockTime * min_latency, GstClockTime * max_latency)
{
599 600
  GstClockTime min;

601 602 603
  GST_LIVE_LOCK (src);
  if (live)
    *live = src->is_live;
604 605 606 607

  /* if we have a startup latency, report this one, else report 0. Subclasses
   * are supposed to override the query function if they want something
   * else. */
608 609
  if (src->priv->latency != -1)
    min = src->priv->latency;
610 611 612
  else
    min = 0;

613
  if (min_latency)
614
    *min_latency = min;
615 616
  if (max_latency)
    *max_latency = -1;
617 618 619 620

  GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
      ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
      GST_TIME_ARGS (-1));
621 622 623 624 625
  GST_LIVE_UNLOCK (src);

  return TRUE;
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
/**
 * gst_base_src_set_do_timestamp:
 * @src: the source
 * @timestamp: enable or disable timestamping
 *
 * Configure @src to automatically timestamp outgoing buffers based on the
 * current running_time of the pipeline. This property is mostly useful for live
 * sources.
 *
 * Since: 0.10.15
 */
void
gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
{
  GST_OBJECT_LOCK (src);
  src->priv->do_timestamp = timestamp;
  GST_OBJECT_UNLOCK (src);
}

/**
 * gst_base_src_get_do_timestamp:
 * @src: the source
 *
 * Query if @src timestamps outgoing buffers based on the current running_time.
 *
 * Returns: %TRUE if the base class will automatically timestamp outgoing buffers.
 *
 * Since: 0.10.15
 */
gboolean
gst_base_src_get_do_timestamp (GstBaseSrc * src)
{
  gboolean res;

  GST_OBJECT_LOCK (src);
  res = src->priv->do_timestamp;
  GST_OBJECT_UNLOCK (src);

  return res;
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static gboolean
gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
{
  GstBaseSrcClass *bclass;
  GstBaseSrc *bsrc;
  gboolean res = TRUE;

  bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
  bclass = GST_BASE_SRC_GET_CLASS (bsrc);

  if (bclass->set_caps)
    res = bclass->set_caps (bsrc, caps);

  return res;
}

static GstCaps *
gst_base_src_getcaps (GstPad * pad)
{
  GstBaseSrcClass *bclass;
  GstBaseSrc *bsrc;
  GstCaps *caps = NULL;

  bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
  bclass = GST_BASE_SRC_GET_CLASS (bsrc);
  if (bclass->get_caps)
    caps = bclass->get_caps (bsrc);

  if (caps == NULL) {
    GstPadTemplate *pad_template;

    pad_template =
        gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
    if (pad_template != NULL) {
      caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
    }
  }
  return caps;
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static void
gst_base_src_fixate (GstPad * pad, GstCaps * caps)
{
  GstBaseSrcClass *bclass;
  GstBaseSrc *bsrc;

  bsrc = GST_BASE_SRC (gst_pad_get_parent (pad));
  bclass = GST_BASE_SRC_GET_CLASS (bsrc);

  if (bclass->fixate)
    bclass->fixate (bsrc, caps);

  gst_object_unref (bsrc);
}

722
static gboolean
723
gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
724
{
725
  gboolean res;
726

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
727
  switch (GST_QUERY_TYPE (query)) {
728
    case GST_QUERY_POSITION:
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
729 730 731
    {
      GstFormat format;

Wim Taymans's avatar
Wim Taymans committed
732
      gst_query_parse_position (query, &format, NULL);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
733
      switch (format) {
734
        case GST_FORMAT_PERCENT:
735
        {
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
          gint64 percent;
          gint64 position;
          gint64 duration;

          position = src->segment.last_stop;
          duration = src->segment.duration;

          if (position != -1 && duration != -1) {
            if (position < duration)
              percent = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, position,
                  duration);
            else
              percent = GST_FORMAT_PERCENT_MAX;
          } else
            percent = -1;

          gst_query_set_position (query, GST_FORMAT_PERCENT, percent);
753 754 755
          res = TRUE;
          break;
        }
Wim Taymans's avatar
Wim Taymans committed
756
        default:
757 758 759 760 761 762 763 764 765 766 767 768 769 770
        {
          gint64 position;

          position = src->segment.last_stop;

          if (position != -1) {
            /* convert to requested format */
            res =
                gst_pad_query_convert (src->srcpad, src->segment.format,
                position, &format, &position);
          } else
            res = TRUE;

          gst_query_set_position (query, format, position);
771
          break;
772
        }
Wim Taymans's avatar
Wim Taymans committed
773
      }
774
      break;
Wim Taymans's avatar
Wim Taymans committed
775 776 777 778 779 780
    }
    case GST_QUERY_DURATION:
    {
      GstFormat format;

      gst_query_parse_duration (query, &format, NULL);
Wim Taymans's avatar
Wim Taymans committed
781 782 783

      GST_DEBUG_OBJECT (src, "duration query in format %s",
          gst_format_get_name (format));
Wim Taymans's avatar
Wim Taymans committed
784 785 786 787
      switch (format) {
        case GST_FORMAT_PERCENT:
          gst_query_set_duration (query, GST_FORMAT_PERCENT,
              GST_FORMAT_PERCENT_MAX);
788 789
          res = TRUE;
          break;
790
        default:
791 792 793 794 795 796 797 798 799 800 801 802 803 804
        {
          gint64 duration;

          duration = src->segment.duration;

          if (duration != -1) {
            /* convert to requested format */
            res =
                gst_pad_query_convert (src->srcpad, src->segment.format,
                duration, &format, &duration);
          } else {
            res = TRUE;
          }
          gst_query_set_duration (query, format, duration);
805
          break;
806
        }
807
      }
808
      break;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
809 810 811
    }

    case GST_QUERY_SEEKING:
812 813 814
    {
      gst_query_set_seeking (query, src->segment.format,
          src->seekable, 0, src->segment.duration);
815 816
      res = TRUE;
      break;
817
    }
818 819 820 821
    case GST_QUERY_SEGMENT:
    {
      gint64 start, stop;

822
      /* no end segment configured, current duration then */
823
      if ((stop = src->segment.stop) == -1)
824 825
        stop = src->segment.duration;
      start = src->segment.start;
826

827 828 829 830 831 832 833 834
      /* adjust to stream time */
      if (src->segment.time != -1) {
        start -= src->segment.time;
        if (stop != -1)
          stop -= src->segment.time;
      }
      gst_query_set_segment (query, src->segment.rate, src->segment.format,
          start, stop);
835 836 837 838
      res = TRUE;
      break;
    }

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
839
    case GST_QUERY_FORMATS:
840
    {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
841 842
      gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
          GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
843 844
      res = TRUE;
      break;
845
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
846
    case GST_QUERY_CONVERT:
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    {
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;

      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);

      /* we can only convert between equal formats... */
      if (src_fmt == dest_fmt) {
        dest_val = src_val;
        res = TRUE;
      } else
        res = FALSE;

      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
863
    case GST_QUERY_LATENCY:
864 865 866 867 868 869 870
    {
      GstClockTime min, max;
      gboolean live;

      /* Subclasses should override and implement something usefull */
      res = gst_base_src_query_latency (src, &live, &min, &max);

871 872 873 874
      GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
          ", max %" GST_TIME_FORMAT, live, GST_TIME_ARGS (min),
          GST_TIME_ARGS (max));

875
      gst_query_set_latency (query, live, min, max);
876
      break;
877
    }
878 879
    case GST_QUERY_JITTER:
    case GST_QUERY_RATE:
880
    default:
881
      res = FALSE;
882
      break;
883
  }
Wim Taymans's avatar
Wim Taymans committed
884 885
  GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
      res);
886
  return res;
887 888
}

889
static gboolean
890
gst_base_src_query (GstPad * pad, GstQuery * query)
891
{
892
  GstBaseSrc *src;
893 894 895
  GstBaseSrcClass *bclass;
  gboolean result = FALSE;

896 897
  src = GST_BASE_SRC (gst_pad_get_parent (pad));

898 899
  bclass = GST_BASE_SRC_GET_CLASS (src);

900 901 902 903 904 905
  if (bclass->query)
    result = bclass->query (src, query);
  else
    result = gst_pad_query_default (pad, query);

  gst_object_unref (src);
906 907 908 909

  return result;
}

910
static gboolean
911
gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
912
{
913
  gboolean res = TRUE;
914

915 916 917 918 919 920 921 922 923 924 925
  /* update our offset if the start/stop position was updated */
  if (segment->format == GST_FORMAT_BYTES) {
    segment->last_stop = segment->start;
    segment->time = segment->start;
  } else if (segment->start == 0) {
    /* seek to start, we can implement a default for this. */
    segment->last_stop = 0;
    segment->time = 0;
    res = TRUE;
  } else
    res = FALSE;
926

927 928
  return res;
}
929

930 931 932 933 934
static gboolean
gst_base_src_do_seek (GstBaseSrc * src, GstSegment * segment)
{
  GstBaseSrcClass *bclass;
  gboolean result = FALSE;
935

936
  bclass = GST_BASE_SRC_GET_CLASS (src);
937

938 939 940 941
  if (bclass->do_seek)
    result = bclass->do_seek (src, segment);

  return result;
942 943
}

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
#define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))

static gboolean
gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
    GstSegment * segment)
{
  /* By default, we try one of 2 things:
   *   - For absolute seek positions, convert the requested position to our 
   *     configured processing format and place it in the output segment \
   *   - For relative seek positions, convert our current (input) values to the
   *     seek format, adjust by the relative seek offset and then convert back to
   *     the processing format
   */
  GstSeekType cur_type, stop_type;
  gint64 cur, stop;
  GstSeekFlags flags;
  GstFormat seek_format, dest_format;
  gdouble rate;
  gboolean update;
  gboolean res = TRUE;

  gst_event_parse_seek (event, &rate, &seek_format, &flags,
      &cur_type, &cur, &stop_type, &stop);
  dest_format = segment->format;

  if (seek_format == dest_format) {
    gst_segment_set_seek (segment, rate, seek_format, flags,
        cur_type, cur, stop_type, stop, &update);
    return TRUE;
  }

  if (cur_type != GST_SEEK_TYPE_NONE) {
    /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
    res =
        gst_pad_query_convert (src->srcpad, seek_format, cur, &dest_format,
        &cur);
    cur_type = GST_SEEK_TYPE_SET;
  }

  if (res && stop_type != GST_SEEK_TYPE_NONE) {
    /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
    res =
        gst_pad_query_convert (src->srcpad, seek_format, stop, &dest_format,
        &stop);
    stop_type = GST_SEEK_TYPE_SET;
  }

  /* And finally, configure our output segment in the desired format */
  gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
      stop_type, stop, &update);

  if (!res)
    goto no_format;

  return res;

no_format:
  {
    GST_DEBUG_OBJECT (src, "undefined format given, seek aborted.");
    return FALSE;
  }
}

static gboolean
gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
    GstSegment * seeksegment)
{
  GstBaseSrcClass *bclass;
  gboolean result = FALSE;

  bclass = GST_BASE_SRC_GET_CLASS (src);

  if (bclass->prepare_seek_segment)
    result = bclass->prepare_seek_segment (src, event, seeksegment);