c2f92071b694948f2a23bba24c11001cd7103d38
[platform/upstream/gstreamer.git] / gst / gstbufferpool.h
1 /* GStreamer
2  * Copyright (C) 2010 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstbufferpool.h: Header for GstBufferPool object
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifndef __GST_BUFFER_POOL_H__
24 #define __GST_BUFFER_POOL_H__
25
26 #include <gst/gstminiobject.h>
27 #include <gst/gstatomicqueue.h>
28 #include <gst/gstpoll.h>
29 #include <gst/gstclock.h>
30 #include <gst/gstpad.h>
31 #include <gst/gstbuffer.h>
32
33 G_BEGIN_DECLS
34
35 typedef struct _GstBufferPoolPrivate GstBufferPoolPrivate;
36 typedef struct _GstBufferPoolClass GstBufferPoolClass;
37
38 #define GST_TYPE_BUFFER_POOL                 (gst_buffer_pool_get_type())
39 #define GST_IS_BUFFER_POOL(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_BUFFER_POOL))
40 #define GST_IS_BUFFER_POOL_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_BUFFER_POOL))
41 #define GST_BUFFER_POOL_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolClass))
42 #define GST_BUFFER_POOL(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_BUFFER_POOL, GstBufferPool))
43 #define GST_BUFFER_POOL_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_BUFFER_POOL, GstBufferPoolClass))
44 #define GST_BUFFER_POOL_CAST(obj)            ((GstBufferPool *)(obj))
45
46 /**
47  * GstBufferPoolFlags:
48  * @GST_BUFFER_POOL_FLAG_NONE: no flags
49  * @GST_BUFFER_POOL_FLAG_KEY_UNIT: buffer is keyframe
50  * @GST_BUFFER_POOL_FLAG_DONTWAIT: don't wait for buffer. This makes the
51  * acquire_buffer method return GST_FLOW_UNEXPECTED.
52  * @GST_BUFFER_POOL_FLAG_DISCONT: buffer is discont
53  * @GST_BUFFER_POOL_FLAG_LAST: last flag, subclasses can use private flags
54  *    starting from this value.
55  *
56  * Additional flags to control the allocation of a buffer
57  */
58 typedef enum {
59   GST_BUFFER_POOL_FLAG_NONE     = 0,
60   GST_BUFFER_POOL_FLAG_KEY_UNIT = (1 << 0),
61   GST_BUFFER_POOL_FLAG_DONTWAIT = (1 << 1),
62   GST_BUFFER_POOL_FLAG_DISCONT  = (1 << 2),
63   GST_BUFFER_POOL_FLAG_LAST     = (1 << 16),
64 } GstBufferPoolFlags;
65
66 /**
67  * GstBufferPoolParams:
68  * @format: the format of @start and @stop
69  * @start: the start position
70  * @stop: the stop position
71  * @flags: additional flags
72  *
73  * Parameters passed to the gst_buffer_pool_acquire_buffer() function to control the
74  * allocation of the buffer.
75  *
76  * The default implementation ignores the @start and @stop members but other
77  * implementations can use this extra information to decide what buffer to
78  * return.
79  */
80 typedef struct _GstBufferPoolParams {
81   GstFormat          format;
82   gint64             start;
83   gint64             stop;
84   GstBufferPoolFlags flags;
85
86   /*< private >*/
87   gpointer _gst_reserved[GST_PADDING];
88 } GstBufferPoolParams;
89
90 /**
91  * GST_BUFFER_POOL_IS_FLUSHING:
92  * @pool: a GstBufferPool
93  *
94  * Check if the bufferpool is flushing. Subclasses might want to check the
95  * state of the pool in the acquire function.
96  */
97 #define GST_BUFFER_POOL_IS_FLUSHING(pool)  (g_atomic_int_get (&pool->flushing))
98
99 /**
100  * GstBufferPool:
101  * @object: the parent structure
102  *
103  * The structure of a #GstBufferPool. Use the associated macros to access the public
104  * variables.
105  */
106 struct _GstBufferPool {
107   GstObject            object;
108
109   /*< private >*/
110   gboolean             active;
111   gboolean             flushing;
112   gboolean             started;
113   gint                 outstanding;
114   GstAtomicQueue      *queue;
115   GstPoll             *poll;
116
117   gboolean             configured;
118   GstStructure        *config;
119
120   GstBufferPoolPrivate *priv;
121
122   gpointer _gst_reserved[GST_PADDING];
123 };
124
125 /**
126  * GstBufferPoolClass:
127  * @object_class:  Object parent class
128  * @get_options: get a list of options supported by this pool
129  * @set_config: apply the bufferpool configuration. The default configuration
130  *              will parse the default config parameters
131  * @start: start the bufferpool. The default implementation will preallocate
132  *         min-buffers buffers and put them in the queue
133  * @stop: stop the bufferpool. the default implementation will free the
134  *        preallocated buffers. This function is called when all the buffers are
135  *        returned to the pool.
136  * @acquire_buffer: get a new buffer from the pool. The default implementation
137  *        will take a buffer from the queue and optionally wait for a buffer to
138  *        be released when there are no buffers available.
139  * @alloc_buffer: allocate a buffer. the default implementation allocates
140  *        buffers from the default memory allocator and with the configured
141  *        size, prefix and alignment. All metadata that is present on the
142  *        allocated buffer will be marked as #GST_META_FLAG_POOLED and will not
143  *        be removed from the buffer in @reset_buffer.
144  * @reset_buffer: reset the buffer to its state when it was freshly allocated.
145  *        The default implementation will clear the flags, timestamps and
146  *        will remove the metadata added after alloc_buffer.
147  * @release_buffer: release a buffer back in the pool. The default
148  *        implementation will put the buffer back in the queue and notify any
149  *        blocking acquire_buffer calls.
150  * @free_buffer: free a buffer. The default implementation unrefs the buffer.
151  *
152  * The GstBufferPool class.
153  */
154 struct _GstBufferPoolClass {
155   GstObjectClass    object_class;
156
157   /* vmethods */
158   const gchar ** (*get_options)    (GstBufferPool *pool);
159   gboolean       (*set_config)     (GstBufferPool *pool, GstStructure *config);
160
161   gboolean       (*start)          (GstBufferPool *pool);
162   gboolean       (*stop)           (GstBufferPool *pool);
163
164   GstFlowReturn  (*acquire_buffer) (GstBufferPool *pool, GstBuffer **buffer,
165                                     GstBufferPoolParams *params);
166   GstFlowReturn  (*alloc_buffer)   (GstBufferPool *pool, GstBuffer **buffer,
167                                     GstBufferPoolParams *params);
168   void           (*reset_buffer)   (GstBufferPool *pool, GstBuffer *buffer,
169                                     GstBufferPoolParams *params);
170   void           (*release_buffer) (GstBufferPool *pool, GstBuffer *buffer);
171   void           (*free_buffer)    (GstBufferPool *pool, GstBuffer *buffer);
172
173   /*< private >*/
174   gpointer _gst_reserved[GST_PADDING];
175 };
176
177 GType       gst_buffer_pool_get_type (void);
178
179 /* allocation */
180 GstBufferPool *  gst_buffer_pool_new  (void);
181
182 /* state management */
183 gboolean         gst_buffer_pool_set_active      (GstBufferPool *pool, gboolean active);
184 gboolean         gst_buffer_pool_is_active       (GstBufferPool *pool);
185
186 gboolean         gst_buffer_pool_set_config      (GstBufferPool *pool, GstStructure *config);
187 GstStructure *   gst_buffer_pool_get_config      (GstBufferPool *pool);
188
189 const gchar **   gst_buffer_pool_get_options     (GstBufferPool *pool);
190 gboolean         gst_buffer_pool_has_option      (GstBufferPool *pool, const gchar *option);
191
192 /* helpers for configuring the config structure */
193 void             gst_buffer_pool_config_set      (GstStructure *config, const GstCaps *caps,
194                                                   guint size, guint min_buffers, guint max_buffers,
195                                                   guint prefix, guint align);
196 gboolean         gst_buffer_pool_config_get      (GstStructure *config, const GstCaps **caps,
197                                                   guint *size, guint *min_buffers, guint *max_buffers,
198                                                   guint *prefix, guint *align);
199
200 /* options */
201 guint            gst_buffer_pool_config_n_options   (GstStructure *config);
202 void             gst_buffer_pool_config_add_option  (GstStructure *config, const gchar *option);
203 const gchar *    gst_buffer_pool_config_get_option  (GstStructure *config, guint index);
204 gboolean         gst_buffer_pool_config_has_option  (GstStructure *config, const gchar *option);
205
206 /* buffer management */
207 GstFlowReturn    gst_buffer_pool_acquire_buffer  (GstBufferPool *pool, GstBuffer **buffer,
208                                                   GstBufferPoolParams *params);
209 void             gst_buffer_pool_release_buffer  (GstBufferPool *pool, GstBuffer *buffer);
210
211 G_END_DECLS
212
213 #endif /* __GST_BUFFER_POOL_H__ */