a8cb7ede3cd823c729da49f6d5faf65933fd9609
[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 /**
39  * GST_BUFFER_POOL_TRACE_NAME:
40  *
41  * The name used for tracing memory allocations.
42  */
43 #define GST_BUFFER_POOL_TRACE_NAME           "GstBufferPool"
44
45 #define GST_TYPE_BUFFER_POOL                 (gst_buffer_pool_get_type())
46 #define GST_IS_BUFFER_POOL(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_BUFFER_POOL))
47 #define GST_IS_BUFFER_POOL_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_BUFFER_POOL))
48 #define GST_BUFFER_POOL_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_BUFFER_POOL, GstBufferPoolClass))
49 #define GST_BUFFER_POOL(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_BUFFER_POOL, GstBufferPool))
50 #define GST_BUFFER_POOL_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_BUFFER_POOL, GstBufferPoolClass))
51 #define GST_BUFFER_POOL_CAST(obj)            ((GstBufferPool *)(obj))
52
53 /**
54  * GstBufferPoolFlags:
55  * @GST_BUFFER_POOL_FLAG_NONE: no flags
56  * @GST_BUFFER_POOL_FLAG_KEY_UNIT: buffer is keyframe
57  * @GST_BUFFER_POOL_FLAG_DONTWAIT: don't wait for buffer. This makes the
58  * acquire_buffer method return GST_FLOW_UNEXPECTED.
59  * @GST_BUFFER_POOL_FLAG_DISCONT: buffer is discont
60  *
61  * Additional flags to control the allocation of a buffer
62  */
63 typedef enum {
64   GST_BUFFER_POOL_FLAG_NONE     = 0,
65   GST_BUFFER_POOL_FLAG_KEY_UNIT = (1 << 0),
66   GST_BUFFER_POOL_FLAG_DONTWAIT = (1 << 1),
67   GST_BUFFER_POOL_FLAG_DISCONT  = (1 << 2),
68   GST_BUFFER_POOL_FLAG_LAST     = (1 << 16),
69 } GstBufferPoolFlags;
70
71 /**
72  * GstBufferPoolParams:
73  * @format: the format of @start and @stop
74  * @start: the start position
75  * @stop: the stop position
76  * @flags: additional flags
77  *
78  * Parameters passed to the gst_buffer_pool_acquire_buffer() function to control the
79  * allocation of the buffer.
80  *
81  * The default implementation ignores the @start and @stop members but other
82  * implementations can use this extra information to decide what buffer to
83  * return.
84  */
85 typedef struct _GstBufferPoolParams {
86   GstFormat          format;
87   gint64             start;
88   gint64             stop;
89   GstBufferPoolFlags flags;
90 } GstBufferPoolParams;
91
92 /**
93  * GST_BUFFER_POOL_IS_FLUSHING:
94  * @pool: a GstBufferPool
95  *
96  * Check if the bufferpool is flushing. Subclasses might want to check the
97  * state of the pool in the acquire function.
98  */
99 #define GST_BUFFER_POOL_IS_FLUSHING(pool)  (g_atomic_int_get (&pool->flushing))
100
101 /**
102  * GstBufferPool:
103  * @object: the parent structure
104  *
105  * The structure of a #GstBufferPool. Use the associated macros to access the public
106  * variables.
107  */
108 struct _GstBufferPool {
109   GstObject            object;
110
111   /*< private >*/
112   gboolean             active;
113   gboolean             flushing;
114   gboolean             started;
115   gint                 outstanding;
116   GstAtomicQueue      *queue;
117   GstPoll             *poll;
118
119   gboolean             configured;
120   GstStructure        *config;
121
122   GstBufferPoolPrivate *priv;
123
124   gpointer _gst_reserved[GST_PADDING];
125 };
126
127 /**
128  * GstBufferPoolClass:
129  * @object_class:  Object parent class
130  * @get_options: get a list of options supported by this pool
131  * @set_config: apply the bufferpool configuration. The default configuration
132  *              will parse the default config parameters
133  * @start: start the bufferpool. The default implementation will preallocate
134  *         min-buffers buffers and put them in the queue
135  * @stop: stop the bufferpool. the default implementation will free the
136  *        preallocated buffers. This function is called when all the buffers are
137  *        returned to the pool.
138  * @acquire_buffer: get a new buffer from the pool. The default implementation
139  *        will take a buffer from the queue and optionally wait for a buffer to
140  *        be released when there are no buffers available.
141  * @alloc_buffer: allocate a buffer. the default implementation allocates
142  *        buffers from the default memory allocator and with the configured
143  *        size, prefix and alignment.
144  * @reset_buffer: reset the buffer to its state when it was freshly allocated.
145  *        The default implementation will clear the flags and timestamps.
146  * @release_buffer: release a buffer back in the pool. The default
147  *        implementation will put the buffer back in the queue and notify any
148  *        blocking acquire_buffer calls.
149  * @free_buffer: free a buffer. The default implementation unrefs the buffer.
150  *
151  * The GstBufferPool class.
152  */
153 struct _GstBufferPoolClass {
154   GstObjectClass    object_class;
155
156   /* vmethods */
157   const gchar ** (*get_options)    (GstBufferPool *pool);
158   gboolean       (*set_config)     (GstBufferPool *pool, GstStructure *config);
159
160   gboolean       (*start)          (GstBufferPool *pool);
161   gboolean       (*stop)           (GstBufferPool *pool);
162
163   GstFlowReturn  (*acquire_buffer) (GstBufferPool *pool, GstBuffer **buffer,
164                                     GstBufferPoolParams *params);
165   GstFlowReturn  (*alloc_buffer)   (GstBufferPool *pool, GstBuffer **buffer,
166                                     GstBufferPoolParams *params);
167   void           (*reset_buffer)   (GstBufferPool *pool, GstBuffer *buffer,
168                                     GstBufferPoolParams *params);
169   void           (*release_buffer) (GstBufferPool *pool, GstBuffer *buffer);
170   void           (*free_buffer)    (GstBufferPool *pool, GstBuffer *buffer);
171
172   gpointer _gst_reserved[GST_PADDING];
173 };
174
175 GType       gst_buffer_pool_get_type (void);
176
177 /* allocation */
178 GstBufferPool *  gst_buffer_pool_new  (void);
179
180 /* state management */
181 gboolean         gst_buffer_pool_set_active      (GstBufferPool *pool, gboolean active);
182
183 gboolean         gst_buffer_pool_set_config      (GstBufferPool *pool, GstStructure *config);
184 GstStructure *   gst_buffer_pool_get_config      (GstBufferPool *pool);
185
186 const gchar **   gst_buffer_pool_get_options     (GstBufferPool *pool);
187
188 /* helpers for configuring the config structure */
189 void             gst_buffer_pool_config_set      (GstStructure *config, const GstCaps *caps,
190                                                   guint size, guint min_buffers, guint max_buffers,
191                                                   guint prefix, guint align);
192 gboolean         gst_buffer_pool_config_get      (GstStructure *config, const GstCaps **caps,
193                                                   guint *size, guint *min_buffers, guint *max_buffers,
194                                                   guint *prefix, guint *align);
195
196 /* options */
197 guint            gst_buffer_pool_config_n_options   (GstStructure *config);
198 void             gst_buffer_pool_config_add_option  (GstStructure *config, const gchar *option);
199 const gchar *    gst_buffer_pool_config_get_option  (GstStructure *config, guint index);
200 gboolean         gst_buffer_pool_config_has_option  (GstStructure *config, const gchar *option);
201
202 /* buffer management */
203 GstFlowReturn    gst_buffer_pool_acquire_buffer  (GstBufferPool *pool, GstBuffer **buffer,
204                                                   GstBufferPoolParams *params);
205 void             gst_buffer_pool_release_buffer  (GstBufferPool *pool, GstBuffer *buffer);
206
207 G_END_DECLS
208
209 #endif /* __GST_BUFFER_POOL_H__ */