gstreamer_core/libs/gst/base/gstbasesrc.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
    31  *   <listitem><para>random access sources like files</para></listitem>
    31  *   <listitem><para>random access sources like files</para></listitem>
    32  *   <listitem><para>seekable sources</para></listitem>
    32  *   <listitem><para>seekable sources</para></listitem>
    33  *   <listitem><para>live sources</para></listitem>
    33  *   <listitem><para>live sources</para></listitem>
    34  * </itemizedlist>
    34  * </itemizedlist>
    35  *
    35  *
       
    36  * <refsect2>
       
    37  * <para>
    36  * The source can be configured to operate in any #GstFormat with the
    38  * The source can be configured to operate in any #GstFormat with the
    37  * gst_base_src_set_format() method. The currently set format determines 
    39  * gst_base_src_set_format() method. The currently set format determines 
    38  * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT 
    40  * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT 
    39  * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
    41  * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
    40  *
    42  * </para>
       
    43  * <para>
    41  * #GstBaseSrc always supports push mode scheduling. If the following
    44  * #GstBaseSrc always supports push mode scheduling. If the following
    42  * conditions are met, it also supports pull mode scheduling:
    45  * conditions are met, it also supports pull mode scheduling:
    43  * <itemizedlist>
    46  * <itemizedlist>
    44  *   <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
    47  *   <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
    45  *   </listitem>
    48  *   </listitem>
    46  *   <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
    49  *   <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
    47  *   </listitem>
    50  *   </listitem>
    48  * </itemizedlist>
    51  * </itemizedlist>
    49  * 
    52  * </para>
       
    53  * <para>
    50  * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any 
    54  * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any 
    51  * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE. 
    55  * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE. 
    52  * 
    56  * </para>
       
    57  * <para>
    53  * If all the conditions are met for operating in pull mode, #GstBaseSrc is
    58  * If all the conditions are met for operating in pull mode, #GstBaseSrc is
    54  * automatically seekable in push mode as well. The following conditions must 
    59  * automatically seekable in push mode as well. The following conditions must 
    55  * be met to make the element seekable in push mode when the format is not
    60  * be met to make the element seekable in push mode when the format is not
    56  * #GST_FORMAT_BYTES:
    61  * #GST_FORMAT_BYTES:
    57  * <itemizedlist>
    62  * <itemizedlist>
    64  *   </para></listitem>
    69  *   </para></listitem>
    65  *   <listitem><para>
    70  *   <listitem><para>
    66  *     #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
    71  *     #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
    67  *   </para></listitem>
    72  *   </para></listitem>
    68  * </itemizedlist>
    73  * </itemizedlist>
    69  * 
    74  * </para>
       
    75  * <para>
    70  * When the element does not meet the requirements to operate in pull mode,
    76  * When the element does not meet the requirements to operate in pull mode,
    71  * the offset and length in the #GstBaseSrc::create method should be ignored.
    77  * the offset and length in the #GstBaseSrc::create method should be ignored.
    72  * It is recommended to subclass #GstPushSrc instead, in this situation. If the
    78  * It is recommended to subclass #GstPushSrc instead, in this situation. If the
    73  * element can operate in pull mode but only with specific offsets and
    79  * element can operate in pull mode but only with specific offsets and
    74  * lengths, it is allowed to generate an error when the wrong values are passed
    80  * lengths, it is allowed to generate an error when the wrong values are passed
    75  * to the #GstBaseSrc::create function.
    81  * to the #GstBaseSrc::create function.
    76  * 
    82  * </para>
       
    83  * <para>
    77  * #GstBaseSrc has support for live sources. Live sources are sources that when
    84  * #GstBaseSrc has support for live sources. Live sources are sources that when
    78  * paused discard data, such as audio or video capture devices. A typical live
    85  * paused discard data, such as audio or video capture devices. A typical live
    79  * source also produces data at a fixed rate and thus provides a clock to publish
    86  * source also produces data at a fixed rate and thus provides a clock to publish
    80  * this rate.
    87  * this rate.
    81  * Use gst_base_src_set_live() to activate the live source mode.
    88  * Use gst_base_src_set_live() to activate the live source mode.
    82  * 
    89  * </para>
       
    90  * <para>
    83  * A live source does not produce data in the PAUSED state. This means that the 
    91  * A live source does not produce data in the PAUSED state. This means that the 
    84  * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
    92  * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
    85  * To signal the pipeline that the element will not produce data, the return
    93  * To signal the pipeline that the element will not produce data, the return
    86  * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
    94  * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
    87  * 
    95  * </para>
       
    96  * <para>
    88  * A typical live source will timestamp the buffers it creates with the 
    97  * A typical live source will timestamp the buffers it creates with the 
    89  * current running time of the pipeline. This is one reason why a live source
    98  * current running time of the pipeline. This is one reason why a live source
    90  * can only produce data in the PLAYING state, when the clock is actually 
    99  * can only produce data in the PLAYING state, when the clock is actually 
    91  * distributed and running. 
   100  * distributed and running. 
    92  * 
   101  * </para>
       
   102  * <para>
    93  * Live sources that synchronize and block on the clock (an audio source, for
   103  * Live sources that synchronize and block on the clock (an audio source, for
    94  * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
   104  * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
    95  * function was interrupted by a state change to PAUSED.
   105  * function was interrupted by a state change to PAUSED.
    96  * 
   106  * </para>
       
   107  * <para>
    97  * The #GstBaseSrc::get_times method can be used to implement pseudo-live 
   108  * The #GstBaseSrc::get_times method can be used to implement pseudo-live 
    98  * sources.
   109  * sources.
    99  * It only makes sense to implement the ::get_times function if the source is 
   110  * It only makes sense to implement the ::get_times function if the source is 
   100  * a live source. The ::get_times function should return timestamps starting
   111  * a live source. The ::get_times function should return timestamps starting
   101  * from 0, as if it were a non-live source. The base class will make sure that
   112  * from 0, as if it were a non-live source. The base class will make sure that
   102  * the timestamps are transformed into the current running_time.
   113  * the timestamps are transformed into the current running_time.
   103  * The base source will then wait for the calculated running_time before pushing
   114  * The base source will then wait for the calculated running_time before pushing
   104  * out the buffer.
   115  * out the buffer.
   105  * 
   116  * </para>
       
   117  * <para>
   106  * For live sources, the base class will by default report a latency of 0.
   118  * For live sources, the base class will by default report a latency of 0.
   107  * For pseudo live sources, the base class will by default measure the difference
   119  * For pseudo live sources, the base class will by default measure the difference
   108  * between the first buffer timestamp and the start time of get_times and will
   120  * between the first buffer timestamp and the start time of get_times and will
   109  * report this value as the latency. 
   121  * report this value as the latency. 
   110  * Subclasses should override the query function when this behaviour is not
   122  * Subclasses should override the query function when this behaviour is not
   111  * acceptable.
   123  * acceptable.
   112  * 
   124  * </para>
       
   125  * <para>
   113  * There is only support in #GstBaseSrc for exactly one source pad, which 
   126  * There is only support in #GstBaseSrc for exactly one source pad, which 
   114  * should be named "src". A source implementation (subclass of #GstBaseSrc) 
   127  * should be named "src". A source implementation (subclass of #GstBaseSrc) 
   115  * should install a pad template in its class_init function, like so:
   128  * should install a pad template in its class_init function, like so:
       
   129  * </para>
       
   130  * <para>
   116  * <programlisting>
   131  * <programlisting>
   117  * static void
   132  * static void
   118  * my_element_class_init (GstMyElementClass *klass)
   133  * my_element_class_init (GstMyElementClass *klass)
   119  * {
   134  * {
   120  *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   135  *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
   124  *       gst_static_pad_template_get (&amp;srctemplate));
   139  *       gst_static_pad_template_get (&amp;srctemplate));
   125  *   // see #GstElementDetails
   140  *   // see #GstElementDetails
   126  *   gst_element_class_set_details (gstelement_class, &amp;details);
   141  *   gst_element_class_set_details (gstelement_class, &amp;details);
   127  * }
   142  * }
   128  * </programlisting>
   143  * </programlisting>
   129  *
   144  * </para>
   130  * <refsect2>
       
   131  * <title>Controlled shutdown of live sources in applications</title>
   145  * <title>Controlled shutdown of live sources in applications</title>
   132  * <para>
   146  * <para>
   133  * Applications that record from a live source may want to stop recording
   147  * Applications that record from a live source may want to stop recording
   134  * in a controlled way, so that the recording is stopped, but the data
   148  * in a controlled way, so that the recording is stopped, but the data
   135  * already in the pipeline is processed to the end (remember that many live
   149  * already in the pipeline is processed to the end (remember that many live
   136  * sources would go on recording forever otherwise). For that to happen the
   150  * sources would go on recording forever otherwise). For that to happen the
   137  * application needs to make the source stop recording and send an EOS
   151  * application needs to make the source stop recording and send an EOS
   138  * event down the pipeline. The application would then wait for an
   152  * event down the pipeline. The application would then wait for an
   139  * EOS message posted on the pipeline's bus to know when all data has
   153  * EOS message posted on the pipeline's bus to know when all data has
   140  * been processed and the pipeline can safely be stopped.
   154  * been processed and the pipeline can safely be stopped.
   141  * 
   155  * </para>
       
   156  * <para>
   142  * Since GStreamer 0.10.16 an application may send an EOS event to a source
   157  * Since GStreamer 0.10.16 an application may send an EOS event to a source
   143  * element to make it perform the EOS logic (send EOS event downstream or post a
   158  * element to make it send an EOS event downstream. This can typically be done
   144  * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
       
   145  * with the gst_element_send_event() function on the element or its parent bin.
   159  * with the gst_element_send_event() function on the element or its parent bin.
   146  * 
   160  * </para>
       
   161  * <para>
   147  * After the EOS has been sent to the element, the application should wait for
   162  * After the EOS has been sent to the element, the application should wait for
   148  * an EOS message to be posted on the pipeline's bus. Once this EOS message is
   163  * an EOS message to be posted on the pipeline's bus. Once this EOS message is
   149  * received, it may safely shut down the entire pipeline.
   164  * received, it may safely shut down the entire pipeline.
   150  * 
   165  * </para>
       
   166  * <para>
   151  * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
   167  * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
   152  * is still available but deprecated as it is dangerous and less flexible.
   168  * is still available but deprecated as it is dangerous and less flexible.
   153  * 
   169  * </para>
       
   170  * <para>
   154  * Last reviewed on 2007-12-19 (0.10.16)
   171  * Last reviewed on 2007-12-19 (0.10.16)
   155  * </para>
   172  * </para>
   156  * </refsect2>
   173  * </refsect2>
   157  */
   174  */
   158 
   175 
   224 
   241 
   225   /* two segments to be sent in the streaming thread with STREAM_LOCK */
   242   /* two segments to be sent in the streaming thread with STREAM_LOCK */
   226   GstEvent *close_segment;
   243   GstEvent *close_segment;
   227   GstEvent *start_segment;
   244   GstEvent *start_segment;
   228 
   245 
   229   /* if EOS is pending (atomic) */
   246   /* if EOS is pending */
   230   gint pending_eos;
   247   gboolean pending_eos;
   231 
   248 
   232   /* startup latency is the time it takes between going to PLAYING and producing
   249   /* startup latency is the time it takes between going to PLAYING and producing
   233    * the first BUFFER with running_time 0. This value is included in the latency
   250    * the first BUFFER with running_time 0. This value is included in the latency
   234    * reporting. */
   251    * reporting. */
   235   GstClockTime latency;
   252   GstClockTime latency;
   236   /* timestamp offset, this is the offset add to the values of gst_times for
   253   /* timestamp offset, this is the offset add to the values of gst_times for
   237    * pseudo live sources */
   254    * pseudo live sources */
   238   GstClockTimeDiff ts_offset;
   255   GstClockTimeDiff ts_offset;
   239 
   256 
   240   gboolean do_timestamp;
   257   gboolean do_timestamp;
   241 
       
   242   /* stream sequence number */
       
   243   guint32 seqnum;
       
   244 
       
   245   /* pending tags to be pushed in the data stream */
       
   246   GList *pending_tags;
       
   247 };
   258 };
   248 
   259 
   249 static GstElementClass *parent_class = NULL;
   260 static GstElementClass *parent_class = NULL;
   250 
   261 
   251 static void gst_base_src_base_init (gpointer g_class);
   262 static void gst_base_src_base_init (gpointer g_class);
   259 
   270 
   260 
   271 
   261 GType
   272 GType
   262 gst_base_src_get_type (void)
   273 gst_base_src_get_type (void)
   263 {
   274 {
   264   static volatile gsize base_src_type = 0;
   275   static GType base_src_type = 0;
   265 
   276 
   266   if (g_once_init_enter (&base_src_type)) {
   277   if (G_UNLIKELY (base_src_type == 0)) {
   267     GType _type;
       
   268     static const GTypeInfo base_src_info = {
   278     static const GTypeInfo base_src_info = {
   269       sizeof (GstBaseSrcClass),
   279       sizeof (GstBaseSrcClass),
   270       (GBaseInitFunc) gst_base_src_base_init,
   280       (GBaseInitFunc) gst_base_src_base_init,
   271       NULL,
   281       NULL,
   272       (GClassInitFunc) gst_base_src_class_init,
   282       (GClassInitFunc) gst_base_src_class_init,
   275       sizeof (GstBaseSrc),
   285       sizeof (GstBaseSrc),
   276       0,
   286       0,
   277       (GInstanceInitFunc) gst_base_src_init,
   287       (GInstanceInitFunc) gst_base_src_init,
   278     };
   288     };
   279 
   289 
   280     _type = g_type_register_static (GST_TYPE_ELEMENT,
   290     base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
   281         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
   291         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
   282     g_once_init_leave (&base_src_type, _type);
       
   283   }
   292   }
   284   return base_src_type;
   293   return base_src_type;
   285 }
   294 }
   286 
       
   287 static GstCaps *gst_base_src_getcaps (GstPad * pad);
   295 static GstCaps *gst_base_src_getcaps (GstPad * pad);
   288 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
   296 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
   289 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
   297 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
   290 
   298 
   291 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
   299 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
   321 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
   329 static gboolean gst_base_src_default_check_get_range (GstBaseSrc * bsrc);
   322 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
   330 static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
   323     guint length, GstBuffer ** buf);
   331     guint length, GstBuffer ** buf);
   324 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
   332 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
   325     guint length, GstBuffer ** buf);
   333     guint length, GstBuffer ** buf);
   326 static gboolean gst_base_src_seekable (GstBaseSrc * src);
       
   327 
   334 
   328 static void
   335 static void
   329 gst_base_src_base_init (gpointer g_class)
   336 gst_base_src_base_init (gpointer g_class)
   330 {
   337 {
   331   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
   338   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
   348   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
   355   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
   349   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
   356   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
   350 
   357 
   351   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
   358   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
   352       g_param_spec_ulong ("blocksize", "Block size",
   359       g_param_spec_ulong ("blocksize", "Block size",
   353           "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
   360           "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
   354           DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   361           DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
   355   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
   362   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
   356       g_param_spec_int ("num-buffers", "num-buffers",
   363       g_param_spec_int ("num-buffers", "num-buffers",
   357           "Number of buffers to output before sending EOS (-1 = unlimited)",
   364           "Number of buffers to output before sending EOS", -1, G_MAXINT,
   358           -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
   365           DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
   359           G_PARAM_STATIC_STRINGS));
       
   360   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
   366   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
   361       g_param_spec_boolean ("typefind", "Typefind",
   367       g_param_spec_boolean ("typefind", "Typefind",
   362           "Run typefind before negotiating", DEFAULT_TYPEFIND,
   368           "Run typefind before negotiating", DEFAULT_TYPEFIND,
   363           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   369           G_PARAM_READWRITE));
   364   g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
   370   g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
   365       g_param_spec_boolean ("do-timestamp", "Do timestamp",
   371       g_param_spec_boolean ("do-timestamp", "Do timestamp",
   366           "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
   372           "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
   367           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   373           G_PARAM_READWRITE));
   368 
   374 
   369   gstelement_class->change_state =
   375   gstelement_class->change_state =
   370       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
   376       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
   371   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
   377   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_src_send_event);
   372   gstelement_class->get_query_types =
   378   gstelement_class->get_query_types =
   452   g_cond_free (basesrc->live_cond);
   458   g_cond_free (basesrc->live_cond);
   453 
   459 
   454   event_p = &basesrc->data.ABI.pending_seek;
   460   event_p = &basesrc->data.ABI.pending_seek;
   455   gst_event_replace (event_p, NULL);
   461   gst_event_replace (event_p, NULL);
   456 
   462 
   457   if (basesrc->priv->pending_tags) {
       
   458     g_list_foreach (basesrc->priv->pending_tags, (GFunc) gst_event_unref, NULL);
       
   459     g_list_free (basesrc->priv->pending_tags);
       
   460   }
       
   461 
       
   462   G_OBJECT_CLASS (parent_class)->finalize (object);
   463   G_OBJECT_CLASS (parent_class)->finalize (object);
   463 }
   464 }
   464 
   465 
   465 /**
   466 /**
   466  * gst_base_src_wait_playing:
   467  * gst_base_src_wait_playing:
   485 #endif
   486 #endif
   486 
   487 
   487 GstFlowReturn
   488 GstFlowReturn
   488 gst_base_src_wait_playing (GstBaseSrc * src)
   489 gst_base_src_wait_playing (GstBaseSrc * src)
   489 {
   490 {
   490   g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
       
   491 
       
   492   /* block until the state changes, or we get a flush, or something */
   491   /* block until the state changes, or we get a flush, or something */
   493   GST_DEBUG_OBJECT (src, "live source waiting for running state");
   492   GST_DEBUG_OBJECT (src, "live source waiting for running state");
   494   GST_LIVE_WAIT (src);
   493   GST_LIVE_WAIT (src);
   495   if (src->priv->flushing)
   494   if (src->priv->flushing)
   496     goto flushing;
   495     goto flushing;
   525 #endif
   524 #endif
   526 
   525 
   527 void
   526 void
   528 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
   527 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
   529 {
   528 {
   530   g_return_if_fail (GST_IS_BASE_SRC (src));
       
   531 
       
   532   GST_OBJECT_LOCK (src);
   529   GST_OBJECT_LOCK (src);
   533   src->is_live = live;
   530   src->is_live = live;
   534   GST_OBJECT_UNLOCK (src);
   531   GST_OBJECT_UNLOCK (src);
   535 }
   532 }
   536 
   533 
   549 gboolean
   546 gboolean
   550 gst_base_src_is_live (GstBaseSrc * src)
   547 gst_base_src_is_live (GstBaseSrc * src)
   551 {
   548 {
   552   gboolean result;
   549   gboolean result;
   553 
   550 
   554   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
       
   555 
       
   556   GST_OBJECT_LOCK (src);
   551   GST_OBJECT_LOCK (src);
   557   result = src->is_live;
   552   result = src->is_live;
   558   GST_OBJECT_UNLOCK (src);
   553   GST_OBJECT_UNLOCK (src);
   559 
   554 
   560   return result;
   555   return result;
   578 #endif
   573 #endif
   579 
   574 
   580 void
   575 void
   581 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
   576 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
   582 {
   577 {
   583   g_return_if_fail (GST_IS_BASE_SRC (src));
       
   584 
       
   585   gst_segment_init (&src->segment, format);
   578   gst_segment_init (&src->segment, format);
   586 }
   579 }
   587 
   580 
   588 /**
   581 /**
   589  * gst_base_src_query_latency:
   582  * gst_base_src_query_latency:
   610 gboolean
   603 gboolean
   611 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
   604 gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
   612     GstClockTime * min_latency, GstClockTime * max_latency)
   605     GstClockTime * min_latency, GstClockTime * max_latency)
   613 {
   606 {
   614   GstClockTime min;
   607   GstClockTime min;
   615 
       
   616   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
       
   617 
   608 
   618   GST_OBJECT_LOCK (src);
   609   GST_OBJECT_LOCK (src);
   619   if (live)
   610   if (live)
   620     *live = src->is_live;
   611     *live = src->is_live;
   621 
   612 
   639 
   630 
   640   return TRUE;
   631   return TRUE;
   641 }
   632 }
   642 
   633 
   643 /**
   634 /**
   644  * gst_base_src_set_blocksize:
       
   645  * @src: the source
       
   646  * @blocksize: the new blocksize in bytes
       
   647  *
       
   648  * Set the number of bytes that @src will push out with each buffer. When
       
   649  * @blocksize is set to -1, a default length will be used.
       
   650  *
       
   651  * Since: 0.10.22
       
   652  */
       
   653 #ifdef __SYMBIAN32__
       
   654 EXPORT_C
       
   655 #endif
       
   656 
       
   657 void
       
   658 gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
       
   659 {
       
   660   g_return_if_fail (GST_IS_BASE_SRC (src));
       
   661 
       
   662   GST_OBJECT_LOCK (src);
       
   663   src->blocksize = blocksize;
       
   664   GST_OBJECT_UNLOCK (src);
       
   665 }
       
   666 
       
   667 /**
       
   668  * gst_base_src_get_blocksize:
       
   669  * @src: the source
       
   670  *
       
   671  * Get the number of bytes that @src will push out with each buffer.
       
   672  *
       
   673  * Returns: the number of bytes pushed with each buffer.
       
   674  *
       
   675  * Since: 0.10.22
       
   676  */
       
   677 #ifdef __SYMBIAN32__
       
   678 EXPORT_C
       
   679 #endif
       
   680 
       
   681 gulong
       
   682 gst_base_src_get_blocksize (GstBaseSrc * src)
       
   683 {
       
   684   gulong res;
       
   685 
       
   686   g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
       
   687 
       
   688   GST_OBJECT_LOCK (src);
       
   689   res = src->blocksize;
       
   690   GST_OBJECT_UNLOCK (src);
       
   691 
       
   692   return res;
       
   693 }
       
   694 
       
   695 
       
   696 /**
       
   697  * gst_base_src_set_do_timestamp:
   635  * gst_base_src_set_do_timestamp:
   698  * @src: the source
   636  * @src: the source
   699  * @timestamp: enable or disable timestamping
   637  * @timestamp: enable or disable timestamping
   700  *
   638  *
   701  * Configure @src to automatically timestamp outgoing buffers based on the
   639  * Configure @src to automatically timestamp outgoing buffers based on the
   709 #endif
   647 #endif
   710 
   648 
   711 void
   649 void
   712 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
   650 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
   713 {
   651 {
   714   g_return_if_fail (GST_IS_BASE_SRC (src));
       
   715 
       
   716   GST_OBJECT_LOCK (src);
   652   GST_OBJECT_LOCK (src);
   717   src->priv->do_timestamp = timestamp;
   653   src->priv->do_timestamp = timestamp;
   718   GST_OBJECT_UNLOCK (src);
   654   GST_OBJECT_UNLOCK (src);
   719 }
   655 }
   720 
   656 
   734 
   670 
   735 gboolean
   671 gboolean
   736 gst_base_src_get_do_timestamp (GstBaseSrc * src)
   672 gst_base_src_get_do_timestamp (GstBaseSrc * src)
   737 {
   673 {
   738   gboolean res;
   674   gboolean res;
   739 
       
   740   g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
       
   741 
   675 
   742   GST_OBJECT_LOCK (src);
   676   GST_OBJECT_LOCK (src);
   743   res = src->priv->do_timestamp;
   677   res = src->priv->do_timestamp;
   744   GST_OBJECT_UNLOCK (src);
   678   GST_OBJECT_UNLOCK (src);
   745 
   679 
   861 
   795 
   862       gst_query_parse_duration (query, &format, NULL);
   796       gst_query_parse_duration (query, &format, NULL);
   863 
   797 
   864       GST_DEBUG_OBJECT (src, "duration query in format %s",
   798       GST_DEBUG_OBJECT (src, "duration query in format %s",
   865           gst_format_get_name (format));
   799           gst_format_get_name (format));
   866 
       
   867       switch (format) {
   800       switch (format) {
   868         case GST_FORMAT_PERCENT:
   801         case GST_FORMAT_PERCENT:
   869           gst_query_set_duration (query, GST_FORMAT_PERCENT,
   802           gst_query_set_duration (query, GST_FORMAT_PERCENT,
   870               GST_FORMAT_PERCENT_MAX);
   803               GST_FORMAT_PERCENT_MAX);
   871           res = TRUE;
   804           res = TRUE;
   897       break;
   830       break;
   898     }
   831     }
   899 
   832 
   900     case GST_QUERY_SEEKING:
   833     case GST_QUERY_SEEKING:
   901     {
   834     {
   902       GstFormat format;
   835       gst_query_set_seeking (query, src->segment.format,
   903 
   836           src->seekable, 0, src->segment.duration);
   904       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
   837       res = TRUE;
   905       if (format == src->segment.format) {
       
   906         gst_query_set_seeking (query, src->segment.format,
       
   907             gst_base_src_seekable (src), 0, src->segment.duration);
       
   908         res = TRUE;
       
   909       } else {
       
   910         /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
       
   911         /* Don't reply to the query to make up for demuxers which don't
       
   912          * handle the SEEKING query yet. Players like Totem will fall back
       
   913          * to the duration when the SEEKING query isn't answered. */
       
   914         res = FALSE;
       
   915       }
       
   916       break;
   838       break;
   917     }
   839     }
   918     case GST_QUERY_SEGMENT:
   840     case GST_QUERY_SEGMENT:
   919     {
   841     {
   920       gint64 start, stop;
   842       gint64 start, stop;
   975       gst_query_set_latency (query, live, min, max);
   897       gst_query_set_latency (query, live, min, max);
   976       break;
   898       break;
   977     }
   899     }
   978     case GST_QUERY_JITTER:
   900     case GST_QUERY_JITTER:
   979     case GST_QUERY_RATE:
   901     case GST_QUERY_RATE:
   980       res = FALSE;
       
   981       break;
       
   982     case GST_QUERY_BUFFERING:
       
   983     {
       
   984       GstFormat format;
       
   985       gint64 start, stop, estimated;
       
   986 
       
   987       gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
       
   988 
       
   989       GST_DEBUG_OBJECT (src, "buffering query in format %s",
       
   990           gst_format_get_name (format));
       
   991 
       
   992       if (src->random_access) {
       
   993         estimated = 0;
       
   994         start = 0;
       
   995         if (format == GST_FORMAT_PERCENT)
       
   996           stop = GST_FORMAT_PERCENT_MAX;
       
   997         else
       
   998           stop = src->segment.duration;
       
   999       } else {
       
  1000         estimated = -1;
       
  1001         start = -1;
       
  1002         stop = -1;
       
  1003       }
       
  1004       /* convert to required format. When the conversion fails, we can't answer
       
  1005        * the query. When the value is unknown, we can don't perform conversion
       
  1006        * but report TRUE. */
       
  1007       if (format != GST_FORMAT_PERCENT && stop != -1) {
       
  1008         res = gst_pad_query_convert (src->srcpad, src->segment.format,
       
  1009             stop, &format, &stop);
       
  1010       } else {
       
  1011         res = TRUE;
       
  1012       }
       
  1013       if (res && format != GST_FORMAT_PERCENT && start != -1)
       
  1014         res = gst_pad_query_convert (src->srcpad, src->segment.format,
       
  1015             start, &format, &start);
       
  1016 
       
  1017       gst_query_set_buffering_range (query, format, start, stop, estimated);
       
  1018       break;
       
  1019     }
       
  1020     default:
   902     default:
  1021       res = FALSE;
   903       res = FALSE;
  1022       break;
   904       break;
  1023   }
   905   }
  1024   GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
   906   GST_DEBUG_OBJECT (src, "query %s returns %d", GST_QUERY_TYPE_NAME (query),
  1056   if (segment->format == GST_FORMAT_BYTES) {
   938   if (segment->format == GST_FORMAT_BYTES) {
  1057     segment->time = segment->start;
   939     segment->time = segment->start;
  1058   } else if (segment->start == 0) {
   940   } else if (segment->start == 0) {
  1059     /* seek to start, we can implement a default for this. */
   941     /* seek to start, we can implement a default for this. */
  1060     segment->time = 0;
   942     segment->time = 0;
  1061   } else {
   943     res = TRUE;
       
   944   } else
  1062     res = FALSE;
   945     res = FALSE;
  1063     GST_INFO_OBJECT (src, "Can't do a default seek");
       
  1064   }
       
  1065 
   946 
  1066   return res;
   947   return res;
  1067 }
   948 }
  1068 
   949 
  1069 static gboolean
   950 static gboolean
  1213  * until 0.11
  1094  * until 0.11
  1214  */
  1095  */
  1215 static gboolean
  1096 static gboolean
  1216 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
  1097 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
  1217 {
  1098 {
  1218   gboolean res = TRUE, tres;
  1099   gboolean res = TRUE;
  1219   gdouble rate;
  1100   gdouble rate;
  1220   GstFormat seek_format, dest_format;
  1101   GstFormat seek_format, dest_format;
  1221   GstSeekFlags flags;
  1102   GstSeekFlags flags;
  1222   GstSeekType cur_type, stop_type;
  1103   GstSeekType cur_type, stop_type;
  1223   gint64 cur, stop;
  1104   gint64 cur, stop;
  1224   gboolean flush, playing;
  1105   gboolean flush, playing;
  1225   gboolean update;
  1106   gboolean update;
  1226   gboolean relative_seek = FALSE;
  1107   gboolean relative_seek = FALSE;
  1227   gboolean seekseg_configured = FALSE;
  1108   gboolean seekseg_configured = FALSE;
  1228   GstSegment seeksegment;
  1109   GstSegment seeksegment;
  1229   guint32 seqnum;
       
  1230   GstEvent *tevent;
       
  1231 
  1110 
  1232   GST_DEBUG_OBJECT (src, "doing seek");
  1111   GST_DEBUG_OBJECT (src, "doing seek");
  1233 
  1112 
  1234   dest_format = src->segment.format;
  1113   dest_format = src->segment.format;
  1235 
  1114 
  1241         SEEK_TYPE_IS_RELATIVE (stop_type);
  1120         SEEK_TYPE_IS_RELATIVE (stop_type);
  1242 
  1121 
  1243     if (dest_format != seek_format && !relative_seek) {
  1122     if (dest_format != seek_format && !relative_seek) {
  1244       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
  1123       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
  1245        * here before taking the stream lock, otherwise we must convert it later,
  1124        * here before taking the stream lock, otherwise we must convert it later,
  1246        * once we have the stream lock and can read the last configures segment
  1125        * once we have the stream lock and can read the current position */
  1247        * start and stop positions */
       
  1248       gst_segment_init (&seeksegment, dest_format);
  1126       gst_segment_init (&seeksegment, dest_format);
  1249 
  1127 
  1250       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
  1128       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
  1251         goto prepare_failed;
  1129         goto prepare_failed;
  1252 
  1130 
  1253       seekseg_configured = TRUE;
  1131       seekseg_configured = TRUE;
  1254     }
  1132     }
  1255 
  1133 
  1256     flush = flags & GST_SEEK_FLAG_FLUSH;
  1134     flush = flags & GST_SEEK_FLAG_FLUSH;
  1257     seqnum = gst_event_get_seqnum (event);
       
  1258   } else {
  1135   } else {
  1259     flush = FALSE;
  1136     flush = FALSE;
  1260     /* get next seqnum */
       
  1261     seqnum = gst_util_seqnum_next ();
       
  1262   }
  1137   }
  1263 
  1138 
  1264   /* send flush start */
  1139   /* send flush start */
  1265   if (flush) {
  1140   if (flush)
  1266     tevent = gst_event_new_flush_start ();
  1141     gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
  1267     gst_event_set_seqnum (tevent, seqnum);
  1142   else
  1268     gst_pad_push_event (src->srcpad, tevent);
       
  1269   } else
       
  1270     gst_pad_pause_task (src->srcpad);
  1143     gst_pad_pause_task (src->srcpad);
  1271 
  1144 
  1272   /* unblock streaming thread. */
  1145   /* unblock streaming thread. */
  1273   gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
  1146   gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
  1274 
  1147 
  1275   /* grab streaming lock, this should eventually be possible, either
  1148   /* grab streaming lock, this should eventually be possible, either
  1276    * because the task is paused, our streaming thread stopped 
  1149    * because the task is paused, our streaming thread stopped 
  1277    * or because our peer is flushing. */
  1150    * or because our peer is flushing. */
  1278   GST_PAD_STREAM_LOCK (src->srcpad);
  1151   GST_PAD_STREAM_LOCK (src->srcpad);
  1279   if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
       
  1280     /* we have seen this event before, issue a warning for now */
       
  1281     GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
       
  1282         seqnum);
       
  1283   } else {
       
  1284     src->priv->seqnum = seqnum;
       
  1285     GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
       
  1286   }
       
  1287 
  1152 
  1288   gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
  1153   gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
  1289 
  1154 
  1290   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
  1155   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
  1291    * copy the current segment info into the temp segment that we can actually
  1156    * copy the current segment info into the temp segment that we can actually
  1324     res = gst_base_src_do_seek (src, &seeksegment);
  1189     res = gst_base_src_do_seek (src, &seeksegment);
  1325   }
  1190   }
  1326 
  1191 
  1327   /* and prepare to continue streaming */
  1192   /* and prepare to continue streaming */
  1328   if (flush) {
  1193   if (flush) {
  1329     tevent = gst_event_new_flush_stop ();
       
  1330     gst_event_set_seqnum (tevent, seqnum);
       
  1331     /* send flush stop, peer will accept data and events again. We
  1194     /* send flush stop, peer will accept data and events again. We
  1332      * are not yet providing data as we still have the STREAM_LOCK. */
  1195      * are not yet providing data as we still have the STREAM_LOCK. */
  1333     gst_pad_push_event (src->srcpad, tevent);
  1196     gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
  1334   } else if (res && src->data.ABI.running) {
  1197   } else if (res && src->data.ABI.running) {
  1335     /* we are running the current segment and doing a non-flushing seek, 
  1198     /* we are running the current segment and doing a non-flushing seek, 
  1336      * close the segment first based on the last_stop. */
  1199      * close the segment first based on the last_stop. */
  1337     GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
  1200     GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
  1338         " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
  1201         " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
  1342       gst_event_unref (src->priv->close_segment);
  1205       gst_event_unref (src->priv->close_segment);
  1343     src->priv->close_segment =
  1206     src->priv->close_segment =
  1344         gst_event_new_new_segment_full (TRUE,
  1207         gst_event_new_new_segment_full (TRUE,
  1345         src->segment.rate, src->segment.applied_rate, src->segment.format,
  1208         src->segment.rate, src->segment.applied_rate, src->segment.format,
  1346         src->segment.start, src->segment.last_stop, src->segment.time);
  1209         src->segment.start, src->segment.last_stop, src->segment.time);
  1347     gst_event_set_seqnum (src->priv->close_segment, seqnum);
       
  1348   }
  1210   }
  1349 
  1211 
  1350   /* The subclass must have converted the segment to the processing format 
  1212   /* The subclass must have converted the segment to the processing format 
  1351    * by now */
  1213    * by now */
  1352   if (res && seeksegment.format != dest_format) {
  1214   if (res && seeksegment.format != dest_format) {
  1359    * out the new segment. */
  1221    * out the new segment. */
  1360   if (res) {
  1222   if (res) {
  1361     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
  1223     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
  1362 
  1224 
  1363     if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
  1225     if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
  1364       GstMessage *message;
  1226       gst_element_post_message (GST_ELEMENT (src),
  1365 
  1227           gst_message_new_segment_start (GST_OBJECT (src),
  1366       message = gst_message_new_segment_start (GST_OBJECT (src),
  1228               src->segment.format, src->segment.last_stop));
  1367           src->segment.format, src->segment.last_stop);
       
  1368       gst_message_set_seqnum (message, seqnum);
       
  1369 
       
  1370       gst_element_post_message (GST_ELEMENT (src), message);
       
  1371     }
  1229     }
  1372 
  1230 
  1373     /* for deriving a stop position for the playback segment from the seek
  1231     /* for deriving a stop position for the playback segment from the seek
  1374      * segment, we must take the duration when the stop is not set */
  1232      * segment, we must take the duration when the stop is not set */
  1375     if ((stop = src->segment.stop) == -1)
  1233     if ((stop = src->segment.stop) == -1)
  1387       src->priv->start_segment =
  1245       src->priv->start_segment =
  1388           gst_event_new_new_segment_full (FALSE,
  1246           gst_event_new_new_segment_full (FALSE,
  1389           src->segment.rate, src->segment.applied_rate, src->segment.format,
  1247           src->segment.rate, src->segment.applied_rate, src->segment.format,
  1390           src->segment.last_stop, stop, src->segment.time);
  1248           src->segment.last_stop, stop, src->segment.time);
  1391     } else {
  1249     } else {
  1392       /* reverse, we send data from last_stop to start */
  1250       /* reverse, we send data from stop to last_stop */
  1393       src->priv->start_segment =
  1251       src->priv->start_segment =
  1394           gst_event_new_new_segment_full (FALSE,
  1252           gst_event_new_new_segment_full (FALSE,
  1395           src->segment.rate, src->segment.applied_rate, src->segment.format,
  1253           src->segment.rate, src->segment.applied_rate, src->segment.format,
  1396           src->segment.start, src->segment.last_stop, src->segment.time);
  1254           src->segment.start, src->segment.last_stop, src->segment.time);
  1397     }
  1255     }
  1398     gst_event_set_seqnum (src->priv->start_segment, seqnum);
       
  1399   }
  1256   }
  1400 
  1257 
  1401   src->priv->discont = TRUE;
  1258   src->priv->discont = TRUE;
  1402   src->data.ABI.running = TRUE;
  1259   src->data.ABI.running = TRUE;
  1403   /* and restart the task in case it got paused explicitely or by
  1260   /* and restart the task in case it got paused explicitely or by
  1404    * the FLUSH_START event we pushed out. */
  1261    * the FLUSH_START event we pushed out. */
  1405   tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
  1262   gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
  1406       src->srcpad);
  1263       src->srcpad);
  1407   if (res && !tres)
       
  1408     res = FALSE;
       
  1409 
  1264 
  1410   /* and release the lock again so we can continue streaming */
  1265   /* and release the lock again so we can continue streaming */
  1411   GST_PAD_STREAM_UNLOCK (src->srcpad);
  1266   GST_PAD_STREAM_UNLOCK (src->srcpad);
  1412 
  1267 
  1413   return res;
  1268   return res;
  1447   GstBaseSrc *src;
  1302   GstBaseSrc *src;
  1448   gboolean result = FALSE;
  1303   gboolean result = FALSE;
  1449 
  1304 
  1450   src = GST_BASE_SRC (element);
  1305   src = GST_BASE_SRC (element);
  1451 
  1306 
  1452   GST_DEBUG_OBJECT (src, "reveived %s event", GST_EVENT_TYPE_NAME (event));
       
  1453 
       
  1454   switch (GST_EVENT_TYPE (event)) {
  1307   switch (GST_EVENT_TYPE (event)) {
  1455       /* bidirectional events */
  1308       /* bidirectional events */
  1456     case GST_EVENT_FLUSH_START:
  1309     case GST_EVENT_FLUSH_START:
  1457     case GST_EVENT_FLUSH_STOP:
  1310     case GST_EVENT_FLUSH_STOP:
  1458       /* sending random flushes downstream can break stuff,
  1311       /* sending random flushes downstream can break stuff,
  1459        * especially sync since all segment info will get flushed */
  1312        * especially sync since all segment info will get flushed */
  1460       break;
  1313       break;
  1461 
  1314 
  1462       /* downstream serialized events */
  1315       /* downstream serialized events */
  1463     case GST_EVENT_EOS:
  1316     case GST_EVENT_EOS:
  1464     {
  1317       /* queue EOS and make sure the task or pull function 
  1465       GstBaseSrcClass *bclass;
  1318        * performs the EOS actions. */
  1466 
       
  1467       bclass = GST_BASE_SRC_GET_CLASS (src);
       
  1468 
       
  1469       /* queue EOS and make sure the task or pull function performs the EOS
       
  1470        * actions. 
       
  1471        *
       
  1472        * We have two possibilities:
       
  1473        *
       
  1474        *  - Before we are to enter the _create function, we check the pending_eos
       
  1475        *    first and do EOS instead of entering it.
       
  1476        *  - If we are in the _create function or we did not manage to set the
       
  1477        *    flag fast enough and we are about to enter the _create function,
       
  1478        *    we unlock it so that we exit with WRONG_STATE immediatly. We then
       
  1479        *    check the EOS flag and do the EOS logic.
       
  1480        */
       
  1481       g_atomic_int_set (&src->priv->pending_eos, TRUE);
       
  1482       GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
       
  1483 
       
  1484       /* unlock the _create function so that we can check the pending_eos flag
       
  1485        * and we can do EOS. This will eventually release the LIVE_LOCK again so
       
  1486        * that we can grab it and stop the unlock again. We don't take the stream
       
  1487        * lock so that this operation is guaranteed to never block. */
       
  1488       if (bclass->unlock)
       
  1489         bclass->unlock (src);
       
  1490 
       
  1491       GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
       
  1492 
       
  1493       GST_LIVE_LOCK (src);
  1319       GST_LIVE_LOCK (src);
  1494       GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
  1320       src->priv->pending_eos = TRUE;
  1495       /* now stop the unlock of the streaming thread again. Grabbing the live
       
  1496        * lock is enough because that protects the create function. */
       
  1497       if (bclass->unlock_stop)
       
  1498         bclass->unlock_stop (src);
       
  1499       GST_LIVE_UNLOCK (src);
  1321       GST_LIVE_UNLOCK (src);
  1500 
       
  1501       result = TRUE;
  1322       result = TRUE;
  1502       break;
  1323       break;
  1503     }
       
  1504     case GST_EVENT_NEWSEGMENT:
  1324     case GST_EVENT_NEWSEGMENT:
  1505       /* sending random NEWSEGMENT downstream can break sync. */
  1325       /* sending random NEWSEGMENT downstream can break sync. */
  1506       break;
  1326       break;
  1507     case GST_EVENT_TAG:
  1327     case GST_EVENT_TAG:
  1508       /* Insert tag in the dataflow */
  1328       /* sending tags could be useful, FIXME insert in dataflow */
  1509       GST_OBJECT_LOCK (src);
       
  1510       src->priv->pending_tags = g_list_append (src->priv->pending_tags, event);
       
  1511       GST_OBJECT_UNLOCK (src);
       
  1512       event = NULL;
       
  1513       result = TRUE;
       
  1514       break;
  1329       break;
  1515     case GST_EVENT_BUFFERSIZE:
  1330     case GST_EVENT_BUFFERSIZE:
  1516       /* does not seem to make much sense currently */
  1331       /* does not seem to make much sense currently */
  1517       break;
  1332       break;
  1518 
  1333 
  1529         goto wrong_mode;
  1344         goto wrong_mode;
  1530       started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
  1345       started = GST_PAD_ACTIVATE_MODE (src->srcpad) == GST_ACTIVATE_PUSH;
  1531       GST_OBJECT_UNLOCK (src->srcpad);
  1346       GST_OBJECT_UNLOCK (src->srcpad);
  1532 
  1347 
  1533       if (started) {
  1348       if (started) {
  1534         GST_DEBUG_OBJECT (src, "performing seek");
       
  1535         /* when we are running in push mode, we can execute the
  1349         /* when we are running in push mode, we can execute the
  1536          * seek right now, we need to unlock. */
  1350          * seek right now, we need to unlock. */
  1537         result = gst_base_src_perform_seek (src, event, TRUE);
  1351         result = gst_base_src_perform_seek (src, event, TRUE);
  1538       } else {
  1352       } else {
  1539         GstEvent **event_p;
  1353         GstEvent **event_p;
  1540 
  1354 
  1541         /* else we store the event and execute the seek when we
  1355         /* else we store the event and execute the seek when we
  1542          * get activated */
  1356          * get activated */
  1543         GST_OBJECT_LOCK (src);
  1357         GST_OBJECT_LOCK (src);
  1544         GST_DEBUG_OBJECT (src, "queueing seek");
       
  1545         event_p = &src->data.ABI.pending_seek;
  1358         event_p = &src->data.ABI.pending_seek;
  1546         gst_event_replace ((GstEvent **) event_p, event);
  1359         gst_event_replace ((GstEvent **) event_p, event);
  1547         GST_OBJECT_UNLOCK (src);
  1360         GST_OBJECT_UNLOCK (src);
  1548         /* assume the seek will work */
  1361         /* assume the seek will work */
  1549         result = TRUE;
  1362         result = TRUE;
  1593     goto done;
  1406     goto done;
  1594   }
  1407   }
  1595 }
  1408 }
  1596 
  1409 
  1597 static gboolean
  1410 static gboolean
  1598 gst_base_src_seekable (GstBaseSrc * src)
       
  1599 {
       
  1600   GstBaseSrcClass *bclass;
       
  1601   bclass = GST_BASE_SRC_GET_CLASS (src);
       
  1602   if (bclass->is_seekable)
       
  1603     return bclass->is_seekable (src);
       
  1604   else
       
  1605     return FALSE;
       
  1606 }
       
  1607 
       
  1608 static gboolean
       
  1609 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
  1411 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
  1610 {
  1412 {
  1611   gboolean result;
  1413   gboolean result;
  1612 
  1414 
  1613   switch (GST_EVENT_TYPE (event)) {
  1415   switch (GST_EVENT_TYPE (event)) {
  1614     case GST_EVENT_SEEK:
  1416     case GST_EVENT_SEEK:
  1615       /* is normally called when in push mode */
  1417       /* is normally called when in push mode */
  1616       if (!gst_base_src_seekable (src))
  1418       if (!src->seekable)
  1617         goto not_seekable;
  1419         goto not_seekable;
  1618 
  1420 
  1619       result = gst_base_src_perform_seek (src, event, TRUE);
  1421       result = gst_base_src_perform_seek (src, event, TRUE);
  1620       break;
  1422       break;
  1621     case GST_EVENT_FLUSH_START:
  1423     case GST_EVENT_FLUSH_START:
  1677 
  1479 
  1678   src = GST_BASE_SRC (object);
  1480   src = GST_BASE_SRC (object);
  1679 
  1481 
  1680   switch (prop_id) {
  1482   switch (prop_id) {
  1681     case PROP_BLOCKSIZE:
  1483     case PROP_BLOCKSIZE:
  1682       gst_base_src_set_blocksize (src, g_value_get_ulong (value));
  1484       src->blocksize = g_value_get_ulong (value);
  1683       break;
  1485       break;
  1684     case PROP_NUM_BUFFERS:
  1486     case PROP_NUM_BUFFERS:
  1685       src->num_buffers = g_value_get_int (value);
  1487       src->num_buffers = g_value_get_int (value);
  1686       break;
  1488       break;
  1687     case PROP_TYPEFIND:
  1489     case PROP_TYPEFIND:
  1688       src->data.ABI.typefind = g_value_get_boolean (value);
  1490       src->data.ABI.typefind = g_value_get_boolean (value);
  1689       break;
  1491       break;
  1690     case PROP_DO_TIMESTAMP:
  1492     case PROP_DO_TIMESTAMP:
  1691       gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
  1493       src->priv->do_timestamp = g_value_get_boolean (value);
  1692       break;
  1494       break;
  1693     default:
  1495     default:
  1694       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1496       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1695       break;
  1497       break;
  1696   }
  1498   }
  1704 
  1506 
  1705   src = GST_BASE_SRC (object);
  1507   src = GST_BASE_SRC (object);
  1706 
  1508 
  1707   switch (prop_id) {
  1509   switch (prop_id) {
  1708     case PROP_BLOCKSIZE:
  1510     case PROP_BLOCKSIZE:
  1709       g_value_set_ulong (value, gst_base_src_get_blocksize (src));
  1511       g_value_set_ulong (value, src->blocksize);
  1710       break;
  1512       break;
  1711     case PROP_NUM_BUFFERS:
  1513     case PROP_NUM_BUFFERS:
  1712       g_value_set_int (value, src->num_buffers);
  1514       g_value_set_int (value, src->num_buffers);
  1713       break;
  1515       break;
  1714     case PROP_TYPEFIND:
  1516     case PROP_TYPEFIND:
  1715       g_value_set_boolean (value, src->data.ABI.typefind);
  1517       g_value_set_boolean (value, src->data.ABI.typefind);
  1716       break;
  1518       break;
  1717     case PROP_DO_TIMESTAMP:
  1519     case PROP_DO_TIMESTAMP:
  1718       g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
  1520       g_value_set_boolean (value, src->priv->do_timestamp);
  1719       break;
  1521       break;
  1720     default:
  1522     default:
  1721       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1523       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  1722       break;
  1524       break;
  1723   }
  1525   }
  2006       goto reached_num_buffers;
  1808       goto reached_num_buffers;
  2007     else
  1809     else
  2008       src->num_buffers_left--;
  1810       src->num_buffers_left--;
  2009   }
  1811   }
  2010 
  1812 
  2011   /* don't enter the create function if a pending EOS event was set. For the
       
  2012    * logic of the pending_eos, check the event function of this class. */
       
  2013   if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
       
  2014     goto eos;
       
  2015 
       
  2016   GST_DEBUG_OBJECT (src,
  1813   GST_DEBUG_OBJECT (src,
  2017       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
  1814       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
  2018       G_GINT64_FORMAT, offset, length, src->segment.time);
  1815       G_GINT64_FORMAT, offset, length, src->segment.time);
  2019 
  1816 
  2020   ret = bclass->create (src, offset, length, buf);
  1817   ret = bclass->create (src, offset, length, buf);
  2021 
       
  2022   /* The create function could be unlocked because we have a pending EOS. It's
       
  2023    * possible that we have a valid buffer from create that we need to
       
  2024    * discard when the create function returned _OK. */
       
  2025   if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
       
  2026     if (ret == GST_FLOW_OK) {
       
  2027       gst_buffer_unref (*buf);
       
  2028       *buf = NULL;
       
  2029     }
       
  2030     goto eos;
       
  2031   }
       
  2032 
       
  2033   if (G_UNLIKELY (ret != GST_FLOW_OK))
  1818   if (G_UNLIKELY (ret != GST_FLOW_OK))
  2034     goto not_ok;
  1819     goto not_ok;
  2035 
  1820 
  2036   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
  1821   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
  2037   if (offset == 0 && src->segment.time == 0
  1822   if (offset == 0 && src->segment.time == 0
  2038       && GST_BUFFER_TIMESTAMP (*buf) == -1)
  1823       && GST_BUFFER_TIMESTAMP (*buf) == -1)
  2039     GST_BUFFER_TIMESTAMP (*buf) = 0;
  1824     GST_BUFFER_TIMESTAMP (*buf) = 0;
  2040 
  1825 
  2041   /* set pad caps on the buffer if the buffer had no caps */
       
  2042   if (GST_BUFFER_CAPS (*buf) == NULL)
       
  2043     gst_buffer_set_caps (*buf, GST_PAD_CAPS (src->srcpad));
       
  2044 
       
  2045   /* now sync before pushing the buffer */
  1826   /* now sync before pushing the buffer */
  2046   status = gst_base_src_do_sync (src, *buf);
  1827   status = gst_base_src_do_sync (src, *buf);
  2047 
  1828 
  2048   /* waiting for the clock could have made us flushing */
  1829   /* waiting for the clock could have made us flushing */
  2049   if (G_UNLIKELY (src->priv->flushing))
  1830   if (G_UNLIKELY (src->priv->flushing))
  2050     goto flushing;
  1831     goto flushing;
       
  1832 
       
  1833   if (G_UNLIKELY (src->priv->pending_eos))
       
  1834     goto eos;
  2051 
  1835 
  2052   switch (status) {
  1836   switch (status) {
  2053     case GST_CLOCK_EARLY:
  1837     case GST_CLOCK_EARLY:
  2054       /* the buffer is too late. We currently don't drop the buffer. */
  1838       /* the buffer is too late. We currently don't drop the buffer. */
  2055       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
  1839       GST_DEBUG_OBJECT (src, "buffer too late!, returning anyway");
  2123     return GST_FLOW_WRONG_STATE;
  1907     return GST_FLOW_WRONG_STATE;
  2124   }
  1908   }
  2125 eos:
  1909 eos:
  2126   {
  1910   {
  2127     GST_DEBUG_OBJECT (src, "we are EOS");
  1911     GST_DEBUG_OBJECT (src, "we are EOS");
       
  1912     gst_buffer_unref (*buf);
       
  1913     *buf = NULL;
  2128     return GST_FLOW_UNEXPECTED;
  1914     return GST_FLOW_UNEXPECTED;
  2129   }
  1915   }
  2130 }
  1916 }
  2131 
  1917 
  2132 static GstFlowReturn
  1918 static GstFlowReturn
  2140 
  1926 
  2141   GST_LIVE_LOCK (src);
  1927   GST_LIVE_LOCK (src);
  2142   if (G_UNLIKELY (src->priv->flushing))
  1928   if (G_UNLIKELY (src->priv->flushing))
  2143     goto flushing;
  1929     goto flushing;
  2144 
  1930 
       
  1931   /* if we're EOS, return right away */
       
  1932   if (G_UNLIKELY (src->priv->pending_eos))
       
  1933     goto eos;
       
  1934 
  2145   res = gst_base_src_get_range (src, offset, length, buf);
  1935   res = gst_base_src_get_range (src, offset, length, buf);
  2146 
  1936 
  2147 done:
  1937 done:
  2148   GST_LIVE_UNLOCK (src);
  1938   GST_LIVE_UNLOCK (src);
  2149 
  1939 
  2154   /* ERRORS */
  1944   /* ERRORS */
  2155 flushing:
  1945 flushing:
  2156   {
  1946   {
  2157     GST_DEBUG_OBJECT (src, "we are flushing");
  1947     GST_DEBUG_OBJECT (src, "we are flushing");
  2158     res = GST_FLOW_WRONG_STATE;
  1948     res = GST_FLOW_WRONG_STATE;
       
  1949     goto done;
       
  1950   }
       
  1951 eos:
       
  1952   {
       
  1953     GST_DEBUG_OBJECT (src, "we are EOS");
       
  1954     res = GST_FLOW_UNEXPECTED;
  2159     goto done;
  1955     goto done;
  2160   }
  1956   }
  2161 }
  1957 }
  2162 
  1958 
  2163 static gboolean
  1959 static gboolean
  2224   GstBuffer *buf = NULL;
  2020   GstBuffer *buf = NULL;
  2225   GstFlowReturn ret;
  2021   GstFlowReturn ret;
  2226   gint64 position;
  2022   gint64 position;
  2227   gboolean eos;
  2023   gboolean eos;
  2228   gulong blocksize;
  2024   gulong blocksize;
  2229   GList *tags, *tmp;
       
  2230 
  2025 
  2231   eos = FALSE;
  2026   eos = FALSE;
  2232 
  2027 
  2233   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
  2028   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
  2234 
  2029 
  2235   GST_LIVE_LOCK (src);
  2030   GST_LIVE_LOCK (src);
  2236 
       
  2237   if (G_UNLIKELY (src->priv->flushing))
  2031   if (G_UNLIKELY (src->priv->flushing))
  2238     goto flushing;
  2032     goto flushing;
       
  2033 
       
  2034   /* if we're EOS, return right away */
       
  2035   if (G_UNLIKELY (src->priv->pending_eos))
       
  2036     goto eos;
  2239 
  2037 
  2240   src->priv->last_sent_eos = FALSE;
  2038   src->priv->last_sent_eos = FALSE;
  2241 
  2039 
  2242   blocksize = src->blocksize;
  2040   blocksize = src->blocksize;
  2243 
  2041 
  2256       }
  2054       }
  2257     }
  2055     }
  2258   } else
  2056   } else
  2259     position = -1;
  2057     position = -1;
  2260 
  2058 
  2261   GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
       
  2262       GST_TIME_ARGS (position), blocksize);
       
  2263 
       
  2264   ret = gst_base_src_get_range (src, position, blocksize, &buf);
  2059   ret = gst_base_src_get_range (src, position, blocksize, &buf);
  2265   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
  2060   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
  2266     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
  2061     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
  2267         gst_flow_get_name (ret));
  2062         gst_flow_get_name (ret));
  2268     GST_LIVE_UNLOCK (src);
  2063     GST_LIVE_UNLOCK (src);
  2278     src->priv->close_segment = NULL;
  2073     src->priv->close_segment = NULL;
  2279   }
  2074   }
  2280   if (G_UNLIKELY (src->priv->start_segment)) {
  2075   if (G_UNLIKELY (src->priv->start_segment)) {
  2281     gst_pad_push_event (pad, src->priv->start_segment);
  2076     gst_pad_push_event (pad, src->priv->start_segment);
  2282     src->priv->start_segment = NULL;
  2077     src->priv->start_segment = NULL;
  2283   }
       
  2284 
       
  2285   GST_OBJECT_LOCK (src);
       
  2286   /* take the tags */
       
  2287   tags = src->priv->pending_tags;
       
  2288   src->priv->pending_tags = NULL;
       
  2289   GST_OBJECT_UNLOCK (src);
       
  2290 
       
  2291   /* Push out pending tags if any */
       
  2292   if (G_UNLIKELY (tags != NULL)) {
       
  2293     for (tmp = tags; tmp; tmp = g_list_next (tmp)) {
       
  2294       GstEvent *ev = (GstEvent *) tmp->data;
       
  2295       gst_pad_push_event (pad, ev);
       
  2296     }
       
  2297     g_list_free (tags);
       
  2298   }
  2078   }
  2299 
  2079 
  2300   /* figure out the new position */
  2080   /* figure out the new position */
  2301   switch (src->segment.format) {
  2081   switch (src->segment.format) {
  2302     case GST_FORMAT_BYTES:
  2082     case GST_FORMAT_BYTES:
  2391     GST_DEBUG_OBJECT (src, "we are flushing");
  2171     GST_DEBUG_OBJECT (src, "we are flushing");
  2392     GST_LIVE_UNLOCK (src);
  2172     GST_LIVE_UNLOCK (src);
  2393     ret = GST_FLOW_WRONG_STATE;
  2173     ret = GST_FLOW_WRONG_STATE;
  2394     goto pause;
  2174     goto pause;
  2395   }
  2175   }
       
  2176 eos:
       
  2177   {
       
  2178     GST_DEBUG_OBJECT (src, "we are EOS");
       
  2179     GST_LIVE_UNLOCK (src);
       
  2180     ret = GST_FLOW_UNEXPECTED;
       
  2181     goto pause;
       
  2182   }
  2396 pause:
  2183 pause:
  2397   {
  2184   {
  2398     const gchar *reason = gst_flow_get_name (ret);
  2185     const gchar *reason = gst_flow_get_name (ret);
  2399     GstEvent *event;
       
  2400 
  2186 
  2401     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
  2187     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
  2402     src->data.ABI.running = FALSE;
  2188     src->data.ABI.running = FALSE;
  2403     gst_pad_pause_task (pad);
  2189     gst_pad_pause_task (pad);
  2404     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
  2190     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
  2405       if (ret == GST_FLOW_UNEXPECTED) {
  2191       if (ret == GST_FLOW_UNEXPECTED) {
  2406         /* perform EOS logic */
  2192         /* perform EOS logic */
  2407         if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
  2193         if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
  2408           GstMessage *message;
  2194           gst_element_post_message (GST_ELEMENT_CAST (src),
  2409 
  2195               gst_message_new_segment_done (GST_OBJECT_CAST (src),
  2410           message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
  2196                   src->segment.format, src->segment.last_stop));
  2411               src->segment.format, src->segment.last_stop);
       
  2412           gst_message_set_seqnum (message, src->priv->seqnum);
       
  2413           gst_element_post_message (GST_ELEMENT_CAST (src), message);
       
  2414         } else {
  2197         } else {
  2415           event = gst_event_new_eos ();
  2198           gst_pad_push_event (pad, gst_event_new_eos ());
  2416           gst_event_set_seqnum (event, src->priv->seqnum);
       
  2417           gst_pad_push_event (pad, event);
       
  2418           src->priv->last_sent_eos = TRUE;
  2199           src->priv->last_sent_eos = TRUE;
  2419         }
  2200         }
  2420       } else {
  2201       } else {
  2421         event = gst_event_new_eos ();
       
  2422         gst_event_set_seqnum (event, src->priv->seqnum);
       
  2423         /* for fatal errors we post an error message, post the error
  2202         /* for fatal errors we post an error message, post the error
  2424          * first so the app knows about the error first. */
  2203          * first so the app knows about the error first. */
  2425         GST_ELEMENT_ERROR (src, STREAM, FAILED,
  2204         GST_ELEMENT_ERROR (src, STREAM, FAILED,
  2426             (_("Internal data flow error.")),
  2205             (_("Internal data flow error.")),
  2427             ("streaming task paused, reason %s (%d)", reason, ret));
  2206             ("streaming task paused, reason %s (%d)", reason, ret));
  2428         gst_pad_push_event (pad, event);
  2207         gst_pad_push_event (pad, gst_event_new_eos ());
  2429         src->priv->last_sent_eos = TRUE;
  2208         src->priv->last_sent_eos = TRUE;
  2430       }
  2209       }
  2431     }
  2210     }
  2432     goto done;
  2211     goto done;
  2433   }
  2212   }
  2459   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
  2238   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
  2460   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
  2239   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
  2461   /* nothing or anything is allowed, we're done */
  2240   /* nothing or anything is allowed, we're done */
  2462   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
  2241   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
  2463     goto no_nego_needed;
  2242     goto no_nego_needed;
  2464 
       
  2465   if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
       
  2466     goto no_caps;
       
  2467 
  2243 
  2468   /* get the peer caps */
  2244   /* get the peer caps */
  2469   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
  2245   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
  2470   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
  2246   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
  2471   if (peercaps) {
  2247   if (peercaps) {
  2497       if (gst_caps_is_any (caps)) {
  2273       if (gst_caps_is_any (caps)) {
  2498         /* hmm, still anything, so element can do anything and
  2274         /* hmm, still anything, so element can do anything and
  2499          * nego is not needed */
  2275          * nego is not needed */
  2500         result = TRUE;
  2276         result = TRUE;
  2501       } else if (gst_caps_is_fixed (caps)) {
  2277       } else if (gst_caps_is_fixed (caps)) {
  2502         /* yay, fixed caps, use those then, it's possible that the subclass does
  2278         /* yay, fixed caps, use those then */
  2503          * not accept this caps after all and we have to fail. */
  2279         gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
  2504         result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
  2280         result = TRUE;
  2505       }
  2281       }
  2506     }
  2282     }
  2507     gst_caps_unref (caps);
  2283     gst_caps_unref (caps);
  2508   } else {
       
  2509     GST_DEBUG_OBJECT (basesrc, "no common caps");
       
  2510   }
  2284   }
  2511   return result;
  2285   return result;
  2512 
  2286 
  2513 no_nego_needed:
  2287 no_nego_needed:
  2514   {
  2288   {
  2515     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
  2289     GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
  2516     if (thiscaps)
  2290     if (thiscaps)
  2517       gst_caps_unref (thiscaps);
  2291       gst_caps_unref (thiscaps);
  2518     return TRUE;
  2292     return TRUE;
  2519   }
  2293   }
  2520 no_caps:
       
  2521   {
       
  2522     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
       
  2523         ("No supported formats found"),
       
  2524         ("This element did not produce valid caps"));
       
  2525     if (thiscaps)
       
  2526       gst_caps_unref (thiscaps);
       
  2527     return TRUE;
       
  2528   }
       
  2529 }
  2294 }
  2530 
  2295 
  2531 static gboolean
  2296 static gboolean
  2532 gst_base_src_negotiate (GstBaseSrc * basesrc)
  2297 gst_base_src_negotiate (GstBaseSrc * basesrc)
  2533 {
  2298 {
  2546 gst_base_src_start (GstBaseSrc * basesrc)
  2311 gst_base_src_start (GstBaseSrc * basesrc)
  2547 {
  2312 {
  2548   GstBaseSrcClass *bclass;
  2313   GstBaseSrcClass *bclass;
  2549   gboolean result;
  2314   gboolean result;
  2550   guint64 size;
  2315   guint64 size;
  2551   gboolean seekable;
       
  2552 
  2316 
  2553   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
  2317   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
  2554     return TRUE;
  2318     return TRUE;
  2555 
  2319 
  2556   GST_DEBUG_OBJECT (basesrc, "starting source");
  2320   GST_DEBUG_OBJECT (basesrc, "starting source");
  2591   GST_DEBUG_OBJECT (basesrc,
  2355   GST_DEBUG_OBJECT (basesrc,
  2592       "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
  2356       "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
  2593       G_GINT64_FORMAT, basesrc->segment.format, result, size,
  2357       G_GINT64_FORMAT, basesrc->segment.format, result, size,
  2594       basesrc->segment.duration);
  2358       basesrc->segment.duration);
  2595 
  2359 
  2596   seekable = gst_base_src_seekable (basesrc);
  2360   /* check if we can seek */
  2597   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
  2361   if (bclass->is_seekable)
       
  2362     basesrc->seekable = bclass->is_seekable (basesrc);
       
  2363   else
       
  2364     basesrc->seekable = FALSE;
       
  2365 
       
  2366   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
  2598 
  2367 
  2599   /* update for random access flag */
  2368   /* update for random access flag */
  2600   basesrc->random_access = seekable &&
  2369   basesrc->random_access = basesrc->seekable &&
  2601       basesrc->segment.format == GST_FORMAT_BYTES;
  2370       basesrc->segment.format == GST_FORMAT_BYTES;
  2602 
  2371 
  2603   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
  2372   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
  2604 
  2373 
  2605   /* run typefind if we are random_access and the typefinding is enabled. */
  2374   /* run typefind if we are random_access and the typefinding is enabled. */
  2606   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
  2375   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
  2607     GstCaps *caps;
  2376     GstCaps *caps;
  2608 
  2377 
  2609     if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
  2378     caps = gst_type_find_helper (basesrc->srcpad, size);
  2610       goto typefind_failed;
       
  2611 
       
  2612     gst_pad_set_caps (basesrc->srcpad, caps);
  2379     gst_pad_set_caps (basesrc->srcpad, caps);
  2613     gst_caps_unref (caps);
  2380     gst_caps_unref (caps);
  2614   } else {
  2381   } else {
  2615     /* use class or default negotiate function */
  2382     /* use class or default negotiate function */
  2616     if (!gst_base_src_negotiate (basesrc))
  2383     if (!gst_base_src_negotiate (basesrc))
  2629 could_not_negotiate:
  2396 could_not_negotiate:
  2630   {
  2397   {
  2631     GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
  2398     GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
  2632     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
  2399     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
  2633         ("Could not negotiate format"), ("Check your filtered caps, if any"));
  2400         ("Could not negotiate format"), ("Check your filtered caps, if any"));
  2634     /* we must call stop */
       
  2635     gst_base_src_stop (basesrc);
       
  2636     return FALSE;
       
  2637   }
       
  2638 typefind_failed:
       
  2639   {
       
  2640     GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
       
  2641     GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
       
  2642     /* we must call stop */
  2401     /* we must call stop */
  2643     gst_base_src_stop (basesrc);
  2402     gst_base_src_stop (basesrc);
  2644     return FALSE;
  2403     return FALSE;
  2645   }
  2404   }
  2646 }
  2405 }
  2691     *playing = basesrc->live_running;
  2450     *playing = basesrc->live_running;
  2692   basesrc->priv->flushing = flushing;
  2451   basesrc->priv->flushing = flushing;
  2693   if (flushing) {
  2452   if (flushing) {
  2694     /* if we are locked in the live lock, signal it to make it flush */
  2453     /* if we are locked in the live lock, signal it to make it flush */
  2695     basesrc->live_running = TRUE;
  2454     basesrc->live_running = TRUE;
  2696 
       
  2697     /* clear pending EOS if any */
  2455     /* clear pending EOS if any */
  2698     g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
  2456     basesrc->priv->pending_eos = FALSE;
  2699 
  2457 
  2700     /* step 1, now that we have the LIVE lock, clear our unlock request */
  2458     /* step 1, now that we have the LIVE lock, clear our unlock request */
  2701     if (bclass->unlock_stop)
  2459     if (bclass->unlock_stop)
  2702       bclass->unlock_stop (basesrc);
  2460       bclass->unlock_stop (basesrc);
  2703 
  2461 
  2936         no_preroll = TRUE;
  2694         no_preroll = TRUE;
  2937       }
  2695       }
  2938       break;
  2696       break;
  2939     case GST_STATE_CHANGE_PAUSED_TO_READY:
  2697     case GST_STATE_CHANGE_PAUSED_TO_READY:
  2940     {
  2698     {
  2941       GstEvent **event_p, *event;
  2699       GstEvent **event_p;
  2942 
  2700 
  2943       /* we don't need to unblock anything here, the pad deactivation code
  2701       /* we don't need to unblock anything here, the pad deactivation code
  2944        * already did this */
  2702        * already did this */
  2945 
  2703 
  2946       /* FIXME, deprecate this behaviour, it is very dangerous.
  2704       /* FIXME, deprecate this behaviour, it is very dangerous.
  2947        * the prefered way of sending EOS downstream is by sending
  2705        * the prefered way of sending EOS downstream is by sending
  2948        * the EOS event to the element */
  2706        * the EOS event to the element */
  2949       if (!basesrc->priv->last_sent_eos) {
  2707       if (!basesrc->priv->last_sent_eos) {
  2950         GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
  2708         GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
  2951         event = gst_event_new_eos ();
  2709         gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
  2952         gst_event_set_seqnum (event, basesrc->priv->seqnum);
       
  2953         gst_pad_push_event (basesrc->srcpad, event);
       
  2954         basesrc->priv->last_sent_eos = TRUE;
  2710         basesrc->priv->last_sent_eos = TRUE;
  2955       }
  2711       }
  2956       g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
  2712       basesrc->priv->pending_eos = FALSE;
  2957       event_p = &basesrc->data.ABI.pending_seek;
  2713       event_p = &basesrc->data.ABI.pending_seek;
  2958       gst_event_replace (event_p, NULL);
  2714       gst_event_replace (event_p, NULL);
  2959       event_p = &basesrc->priv->close_segment;
  2715       event_p = &basesrc->priv->close_segment;
  2960       gst_event_replace (event_p, NULL);
  2716       gst_event_replace (event_p, NULL);
  2961       event_p = &basesrc->priv->start_segment;
  2717       event_p = &basesrc->priv->start_segment;