basesrc: Make locking of the segment a bit more strict and update documentation
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesrc.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstbasesrc.h:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifndef __GST_BASE_SRC_H__
25 #define __GST_BASE_SRC_H__
26
27 #include <gst/gst.h>
28
29 G_BEGIN_DECLS
30
31 #define GST_TYPE_BASE_SRC               (gst_base_src_get_type())
32 #define GST_BASE_SRC(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_SRC,GstBaseSrc))
33 #define GST_BASE_SRC_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_SRC,GstBaseSrcClass))
34 #define GST_BASE_SRC_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_BASE_SRC, GstBaseSrcClass))
35 #define GST_IS_BASE_SRC(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_SRC))
36 #define GST_IS_BASE_SRC_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_SRC))
37 #define GST_BASE_SRC_CAST(obj)          ((GstBaseSrc *)(obj))
38
39 /**
40  * GstBaseSrcFlags:
41  * @GST_BASE_SRC_STARTED: has source been started
42  * @GST_BASE_SRC_FLAG_LAST: offset to define more flags
43  *
44  * The #GstElement flags that a basesrc element may have.
45  */
46 typedef enum {
47   GST_BASE_SRC_STARTED           = (GST_ELEMENT_FLAG_LAST << 0),
48   /* padding */
49   GST_BASE_SRC_FLAG_LAST         = (GST_ELEMENT_FLAG_LAST << 2)
50 } GstBaseSrcFlags;
51
52 typedef struct _GstBaseSrc GstBaseSrc;
53 typedef struct _GstBaseSrcClass GstBaseSrcClass;
54 typedef struct _GstBaseSrcPrivate GstBaseSrcPrivate;
55
56 /**
57  * GST_BASE_SRC_PAD:
58  * @obj: base source instance
59  *
60  * Gives the pointer to the #GstPad object of the element.
61  */
62 #define GST_BASE_SRC_PAD(obj)                 (GST_BASE_SRC_CAST (obj)->srcpad)
63
64
65 /**
66  * GstBaseSrc:
67  * @element: the parent element.
68  *
69  * The opaque #GstBaseSrc data structure.
70  */
71 struct _GstBaseSrc {
72   GstElement     element;
73
74   /*< protected >*/
75   GstPad        *srcpad;
76
77   /* available to subclass implementations */
78   /* MT-protected (with LIVE_LOCK) */
79   GMutex        *live_lock;
80   GCond         *live_cond;
81   gboolean       is_live;
82   gboolean       live_running;
83
84   /* MT-protected (with LOCK) */
85   gint           blocksize;     /* size of buffers when operating push based */
86   gboolean       can_activate_push;     /* some scheduling properties */
87   GstActivateMode pad_mode;
88   gboolean       seekable; /* not used anymore */
89   gboolean       random_access;
90
91   GstClockID     clock_id;      /* for syncing */
92   GstClockTime   end_time;
93
94   /* MT-protected (with STREAM_LOCK *and* OBJECT_LOCK) */
95   GstSegment     segment;
96   /* MT-protected (with STREAM_LOCK) */
97   gboolean       need_newsegment;
98
99   guint64        offset;        /* current offset in the resource, unused */
100   guint64        size;          /* total size of the resource, unused */
101
102   gint           num_buffers;
103   gint           num_buffers_left;
104
105   /*< private >*/
106   union {
107     struct {
108       /* FIXME: those fields should be moved into the private struct */
109       gboolean  typefind;
110       gboolean  running;
111       GstEvent *pending_seek;
112     } ABI;
113     gpointer       _gst_reserved[GST_PADDING_LARGE-1];
114   } data;
115
116   GstBaseSrcPrivate *priv;
117 };
118
119 /**
120  * GstBaseSrcClass:
121  * @parent_class: Element parent class
122  * @get_caps: Called to get the caps to report
123  * @set_caps: Notify subclass of changed output caps
124  * @negotiate: Negotiated the caps with the peer.
125  * @newsegment: Generate and send a new_segment event (UNUSED)
126  * @start: Start processing. Subclasses should open resources and prepare
127  *    to produce data.
128  * @stop: Stop processing. Subclasses should use this to close resources.
129  * @get_times: Given a buffer, return the start and stop time when it
130  *    should be pushed out. The base class will sync on the clock using
131  *    these times.
132  * @get_size: Return the total size of the resource, in the configured format.
133  * @is_seekable: Check if the source can seek
134  * @unlock: Unlock any pending access to the resource. Subclasses should
135  *    unblock any blocked function ASAP
136  * @unlock_stop: Clear the previous unlock request. Subclasses should clear
137  *    any state they set during unlock(), such as clearing command queues.
138  * @event: Override this to implement custom event handling.
139  * @create: Ask the subclass to create a buffer with offset and size.
140  * @do_seek: Perform seeking on the resource to the indicated segment.
141  * @prepare_seek_segment: Prepare the GstSegment that will be passed to the
142  *   do_seek vmethod for executing a seek request. Sub-classes should override
143  *   this if they support seeking in formats other than the configured native
144  *   format. By default, it tries to convert the seek arguments to the
145  *   configured native format and prepare a segment in that format.
146  *   Since: 0.10.13
147  * @query: Handle a requested query.
148  * @check_get_range: Check whether the source would support pull-based
149  *   operation if it were to be opened now. This vfunc is optional, but
150  *   should be implemented if possible to avoid unnecessary start/stop
151  *   cycles. The default implementation will open and close the resource
152  *   to find out whether get_range is supported, and that is usually
153  *   undesirable.
154  * @fixate: Called during negotiation if caps need fixating. Implement instead of
155  *   setting a fixate function on the source pad.
156  *
157  * Subclasses can override any of the available virtual methods or not, as
158  * needed. At the minimum, the @create method should be overridden to produce
159  * buffers.
160  */
161 struct _GstBaseSrcClass {
162   GstElementClass parent_class;
163
164   /*< public >*/
165   /* virtual methods for subclasses */
166
167   /* get caps from subclass */
168   GstCaps*      (*get_caps)     (GstBaseSrc *src);
169   /* notify the subclass of new caps */
170   gboolean      (*set_caps)     (GstBaseSrc *src, GstCaps *caps);
171
172   /* decide on caps */
173   gboolean      (*negotiate)    (GstBaseSrc *src);
174
175   /* generate and send a newsegment (UNUSED) */
176   gboolean      (*newsegment)   (GstBaseSrc *src);
177
178   /* start and stop processing, ideal for opening/closing the resource */
179   gboolean      (*start)        (GstBaseSrc *src);
180   gboolean      (*stop)         (GstBaseSrc *src);
181
182   /* given a buffer, return start and stop time when it should be pushed
183    * out. The base class will sync on the clock using these times. */
184   void          (*get_times)    (GstBaseSrc *src, GstBuffer *buffer,
185                                  GstClockTime *start, GstClockTime *end);
186
187   /* get the total size of the resource in bytes */
188   gboolean      (*get_size)     (GstBaseSrc *src, guint64 *size);
189
190   /* check if the resource is seekable */
191   gboolean      (*is_seekable)  (GstBaseSrc *src);
192   /* unlock any pending access to the resource. subclasses should unlock
193    * any function ASAP. */
194   gboolean      (*unlock)       (GstBaseSrc *src);
195
196   /* notify subclasses of an event */
197   gboolean      (*event)        (GstBaseSrc *src, GstEvent *event);
198
199   /* ask the subclass to create a buffer with offset and size */
200   GstFlowReturn (*create)       (GstBaseSrc *src, guint64 offset, guint size,
201                                  GstBuffer **buf);
202
203   /* additions that change padding... */
204   /* notify subclasses of a seek */
205   gboolean      (*do_seek)      (GstBaseSrc *src, GstSegment *segment);
206   /* notify subclasses of a query */
207   gboolean      (*query)        (GstBaseSrc *src, GstQuery *query);
208
209   /* check whether the source would support pull-based operation if
210    * it were to be opened now. This vfunc is optional, but should be
211    * implemented if possible to avoid unnecessary start/stop cycles.
212    * The default implementation will open and close the resource to
213    * find out whether get_range is supported and that is usually
214    * undesirable. */
215   gboolean      (*check_get_range) (GstBaseSrc *src);
216
217   /* called if, in negotiation, caps need fixating */
218   void          (*fixate)       (GstBaseSrc *src, GstCaps *caps);
219
220   /* Clear any pending unlock request, as we succeeded in unlocking */
221   gboolean      (*unlock_stop)  (GstBaseSrc *src);
222
223   /* Prepare the segment on which to perform do_seek(), converting to the
224    * current basesrc format. */
225   gboolean      (*prepare_seek_segment) (GstBaseSrc *src, GstEvent *seek,
226                                          GstSegment *segment);
227
228   /*< private >*/
229   gpointer       _gst_reserved[GST_PADDING_LARGE - 6];
230 };
231
232 GType gst_base_src_get_type (void);
233
234 GstFlowReturn   gst_base_src_wait_playing     (GstBaseSrc *src);
235
236 void            gst_base_src_set_live         (GstBaseSrc *src, gboolean live);
237 gboolean        gst_base_src_is_live          (GstBaseSrc *src);
238
239 void            gst_base_src_set_format       (GstBaseSrc *src, GstFormat format);
240
241 gboolean        gst_base_src_query_latency    (GstBaseSrc *src, gboolean * live,
242                                                GstClockTime * min_latency,
243                                                GstClockTime * max_latency);
244
245 void            gst_base_src_set_blocksize    (GstBaseSrc *src, gulong blocksize);
246 gulong          gst_base_src_get_blocksize    (GstBaseSrc *src);
247
248 void            gst_base_src_set_do_timestamp (GstBaseSrc *src, gboolean timestamp);
249 gboolean        gst_base_src_get_do_timestamp (GstBaseSrc *src);
250
251 gboolean        gst_base_src_new_seamless_segment (GstBaseSrc *src, gint64 start, gint64 stop, gint64 position);
252 G_END_DECLS
253
254 #endif /* __GST_BASE_SRC_H__ */