aggregator: Implement propose allocation
[platform/upstream/gstreamer.git] / libs / gst / base / gstaggregator.h
1 /* GStreamer aggregator base class
2  * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@oencreed.com>
3  * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef __GST_AGGREGATOR_H__
22 #define __GST_AGGREGATOR_H__
23
24 #ifndef GST_USE_UNSTABLE_API
25 #warning "The Base library from gst-plugins-bad is unstable API and may change in future."
26 #warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
27 #endif
28
29 #include <gst/gst.h>
30
31 G_BEGIN_DECLS
32
33 /**************************
34  * GstAggregator Structs  *
35  *************************/
36
37 typedef struct _GstAggregator GstAggregator;
38 typedef struct _GstAggregatorPrivate GstAggregatorPrivate;
39 typedef struct _GstAggregatorClass GstAggregatorClass;
40
41 /************************
42  * GstAggregatorPad API *
43  ***********************/
44
45 #define GST_TYPE_AGGREGATOR_PAD            (gst_aggregator_pad_get_type())
46 #define GST_AGGREGATOR_PAD(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPad))
47 #define GST_AGGREGATOR_PAD_CAST(obj)       ((GstAggregatorPad *)(obj))
48 #define GST_AGGREGATOR_PAD_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPadClass))
49 #define GST_AGGREGATOR_PAD_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPadClass))
50 #define GST_IS_AGGREGATOR_PAD(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGGREGATOR_PAD))
51 #define GST_IS_AGGREGATOR_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGGREGATOR_PAD))
52
53 /****************************
54  * GstAggregatorPad Structs *
55  ***************************/
56
57 typedef struct _GstAggregatorPad GstAggregatorPad;
58 typedef struct _GstAggregatorPadClass GstAggregatorPadClass;
59 typedef struct _GstAggregatorPadPrivate GstAggregatorPadPrivate;
60
61 /**
62  * GstAggregatorPad:
63  * @buffer: currently queued buffer.
64  * @segment: last segment received.
65  *
66  * The implementation the GstPad to use with #GstAggregator
67  */
68 struct _GstAggregatorPad
69 {
70   GstPad                       parent;
71
72   /* Protected by the OBJECT_LOCK */
73   GstSegment segment;
74
75   /* < Private > */
76   GstAggregatorPadPrivate   *  priv;
77
78   gpointer _gst_reserved[GST_PADDING];
79 };
80
81 /**
82  * GstAggregatorPadClass:
83  * @flush:    Optional
84  *            Called when the pad has received a flush stop, this is the place
85  *            to flush any information specific to the pad, it allows for individual
86  *            pads to be flushed while others might not be.
87  *
88  */
89 struct _GstAggregatorPadClass
90 {
91   GstPadClass   parent_class;
92
93   GstFlowReturn (*flush)     (GstAggregatorPad * aggpad, GstAggregator * aggregator);
94
95   /*< private >*/
96   gpointer      _gst_reserved[GST_PADDING_LARGE];
97 };
98
99 GType gst_aggregator_pad_get_type           (void);
100
101 /****************************
102  * GstAggregatorPad methods *
103  ***************************/
104
105 GstBuffer * gst_aggregator_pad_steal_buffer (GstAggregatorPad *  pad);
106 GstBuffer * gst_aggregator_pad_get_buffer   (GstAggregatorPad *  pad);
107 gboolean    gst_aggregator_pad_drop_buffer  (GstAggregatorPad *  pad);
108 gboolean    gst_aggregator_pad_is_eos       (GstAggregatorPad *  pad);
109
110 /*********************
111  * GstAggregator API *
112  ********************/
113
114 #define GST_TYPE_AGGREGATOR            (gst_aggregator_get_type())
115 #define GST_AGGREGATOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGGREGATOR,GstAggregator))
116 #define GST_AGGREGATOR_CAST(obj)       ((GstAggregator *)(obj))
117 #define GST_AGGREGATOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGGREGATOR,GstAggregatorClass))
118 #define GST_AGGREGATOR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_AGGREGATOR,GstAggregatorClass))
119 #define GST_IS_AGGREGATOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGGREGATOR))
120 #define GST_IS_AGGREGATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGGREGATOR))
121
122 #define GST_AGGREGATOR_FLOW_NEED_DATA             GST_FLOW_CUSTOM_ERROR
123
124 /**
125  * GstAggregator:
126  * @srcpad: the aggregator's source pad
127  * @segment: the output segment
128  *
129  * Aggregator base class object structure.
130  */
131 struct _GstAggregator
132 {
133   GstElement               parent;
134
135   GstPad                *  srcpad;
136
137   /* Only access with the object lock held */
138   GstSegment               segment;
139
140   /*< private >*/
141   GstAggregatorPrivate  *  priv;
142
143   gpointer                 _gst_reserved[GST_PADDING_LARGE];
144 };
145
146 /**
147  * GstAggregatorClass:
148  * @sinkpads_type:  Optional.
149  *                  The type of the pads that should be created when
150  *                  GstElement.request_new_pad is called.
151  * @flush:          Optional.
152  *                  Called after a succesful flushing seek, once all the flush
153  *                  stops have been received. Flush pad-specific data in
154  *                  #GstAggregatorPad->flush.
155  * @clip:           Optional.
156  *                  Called when a buffer is received on a sink pad, the task of
157  *                  clipping it and translating it to the current segment falls
158  *                  on the subclass. The function should use the segment of data
159  *                  and the negotiated media type on the pad to perform
160  *                  clipping of inbuffer. This function takes ownership of
161  *                  buf and should output a buffer or return NULL in
162  *                  if the buffer should be dropped.
163  * @sink_event:     Optional.
164  *                  Called when an event is received on a sink pad, the subclass
165  *                  should always chain up.
166  * @sink_query:     Optional.
167  *                  Called when a query is received on a sink pad, the subclass
168  *                  should always chain up.
169  * @src_event:      Optional.
170  *                  Called when an event is received on the src pad, the subclass
171  *                  should always chain up.
172  * @src_query:      Optional.
173  *                  Called when a query is received on the src pad, the subclass
174  *                  should always chain up.
175  * @src_activate:   Optional.
176  *                  Called when the src pad is activated, it will start/stop its
177  *                  pad task right after that call.
178  * @aggregate:      Mandatory.
179  *                  Called when buffers are queued on all sinkpads. Classes
180  *                  should iterate the GstElement->sinkpads and peek or steal
181  *                  buffers from the #GstAggregatorPads. If the subclass returns
182  *                  GST_FLOW_EOS, sending of the eos event will be taken care
183  *                  of. Once / if a buffer has been constructed from the
184  *                  aggregated buffers, the subclass should call _finish_buffer.
185  * @stop:           Optional.
186  *                  Called when the element goes from PAUSED to READY.
187  *                  The subclass should free all resources and reset its state.
188  * @start:          Optional.
189  *                  Called when the element goes from READY to PAUSED.
190  *                  The subclass should get ready to process
191  *                  aggregated buffers.
192  * @get_next_time:  Optional.
193  *                  Called when the element needs to know the running time of the next
194  *                  rendered buffer for live pipelines. This causes deadline
195  *                  based aggregation to occur. Defaults to returning
196  *                  GST_CLOCK_TIME_NONE causing the element to wait for buffers
197  *                  on all sink pads before aggregating.
198  * @update_src_caps: Lets subclasses update the #GstCaps representing
199  *                   the src pad caps before usage.  The result should end up
200  *                   in @ret. Return %GST_AGGREGATOR_FLOW_NEED_DATA to indicate that the
201  *                   element needs more information (caps, a buffer, etc) to
202  *                   choose the correct caps. Should return ANY caps if the
203  *                   stream has not caps at all.
204  * @fixate_src_caps: Optional.
205  *                   Fixate and return the src pad caps provided.  The function takes
206  *                   ownership of @caps and returns a fixated version of
207  *                   @caps. @caps is not guaranteed to be writable.
208  * @negotiated_src_caps: Optional.
209  *                       Notifies subclasses what caps format has been negotiated
210  * @decide_allocation: Optional.
211  *                     Allows the subclass to influence the allocation choices.
212  *                     Setup the allocation parameters for allocating output
213  *                     buffers. The passed in query contains the result of the
214  *                     downstream allocation query.
215  *
216  * The aggregator base class will handle in a thread-safe way all manners of
217  * concurrent flushes, seeks, pad additions and removals, leaving to the
218  * subclass the responsibility of clipping buffers, and aggregating buffers in
219  * the way the implementor sees fit.
220  *
221  * It will also take care of event ordering (stream-start, segment, eos).
222  *
223  * Basically, a basic implementation will override @aggregate, and call
224  * _finish_buffer from inside that function.
225  */
226 struct _GstAggregatorClass {
227   GstElementClass   parent_class;
228
229   GType             sinkpads_type;
230
231   GstFlowReturn     (*flush)          (GstAggregator    *  aggregator);
232
233   GstBuffer *       (*clip)           (GstAggregator    *  aggregator,
234                                        GstAggregatorPad *  aggregator_pad,
235                                        GstBuffer        *  buf);
236
237   /* sinkpads virtual methods */
238   gboolean          (*sink_event)     (GstAggregator    *  aggregator,
239                                        GstAggregatorPad *  aggregator_pad,
240                                        GstEvent         *  event);
241
242   gboolean          (*sink_query)     (GstAggregator    *  aggregator,
243                                        GstAggregatorPad *  aggregator_pad,
244                                        GstQuery         *  query);
245
246   /* srcpad virtual methods */
247   gboolean          (*src_event)      (GstAggregator    *  aggregator,
248                                        GstEvent         *  event);
249
250   gboolean          (*src_query)      (GstAggregator    *  aggregator,
251                                        GstQuery         *  query);
252
253   gboolean          (*src_activate)   (GstAggregator    *  aggregator,
254                                        GstPadMode          mode,
255                                        gboolean            active);
256
257   GstFlowReturn     (*aggregate)      (GstAggregator    *  aggregator,
258                                        gboolean            timeout);
259
260   gboolean          (*stop)           (GstAggregator    *  aggregator);
261
262   gboolean          (*start)          (GstAggregator    *  aggregator);
263
264   GstClockTime      (*get_next_time)  (GstAggregator    *  aggregator);
265
266   GstAggregatorPad * (*create_new_pad) (GstAggregator  * self,
267                                         GstPadTemplate * templ,
268                                         const gchar    * req_name,
269                                         const GstCaps  * caps);
270   GstFlowReturn     (*update_src_caps) (GstAggregator *  self,
271                                         GstCaps       *  caps,
272                                         GstCaps       ** ret);
273   GstCaps *         (*fixate_src_caps) (GstAggregator *  self,
274                                         GstCaps       *  caps);
275   gboolean          (*negotiated_src_caps) (GstAggregator *  self,
276                                             GstCaps      *  caps);
277   gboolean          (*decide_allocation) (GstAggregator * self,
278                                           GstQuery * query);
279   gboolean          (*propose_allocation) (GstAggregator * self,
280                                            GstAggregatorPad * pad,
281                                            GstQuery * decide_query,
282                                            GstQuery * query);
283   /*< private >*/
284   gpointer          _gst_reserved[GST_PADDING_LARGE];
285 };
286
287 /************************************
288  * GstAggregator convenience macros *
289  ***********************************/
290
291 /**
292  * GST_AGGREGATOR_SRC_PAD:
293  * @agg: a #GstAggregator
294  *
295  * Convenience macro to access the source pad of #GstAggregator
296  *
297  * Since: 1.6
298  */
299 #define GST_AGGREGATOR_SRC_PAD(agg) (((GstAggregator *)(agg))->srcpad)
300
301 /*************************
302  * GstAggregator methods *
303  ************************/
304
305 GstFlowReturn  gst_aggregator_finish_buffer         (GstAggregator                *  self,
306                                                      GstBuffer                    *  buffer);
307 void           gst_aggregator_set_src_caps          (GstAggregator                *  self,
308                                                      GstCaps                      *  caps);
309
310 void           gst_aggregator_set_latency           (GstAggregator                *  self,
311                                                      GstClockTime                    min_latency,
312                                                      GstClockTime                    max_latency);
313
314 GType gst_aggregator_get_type(void);
315
316 /* API that should eventually land in GstElement itself (FIXME) */
317 typedef gboolean (*GstAggregatorPadForeachFunc)    (GstAggregator                 *  aggregator,
318                                                     GstAggregatorPad              *  aggregator_pad,
319                                                     gpointer                         user_data);
320
321 gboolean gst_aggregator_iterate_sinkpads           (GstAggregator                 *  self,
322                                                     GstAggregatorPadForeachFunc      func,
323                                                     gpointer                         user_data);
324
325 GstClockTime  gst_aggregator_get_latency           (GstAggregator                 *  self);
326
327 GstBufferPool * gst_aggregator_get_buffer_pool     (GstAggregator                 * self);
328 void          gst_aggregator_get_allocator         (GstAggregator                 * self,
329                                                     GstAllocator
330  ** allocator,
331                                                     GstAllocationParams
332   * params);
333
334
335 G_END_DECLS
336
337 #endif /* __GST_AGGREGATOR_H__ */