50458360578ed5699e66af12837ec3bf466fd178
[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 #include <gst/gst.h>
25 #include <gst/base/base-prelude.h>
26
27 G_BEGIN_DECLS
28
29 /**************************
30  * GstAggregator Structs  *
31  *************************/
32
33 typedef struct _GstAggregator GstAggregator;
34 typedef struct _GstAggregatorPrivate GstAggregatorPrivate;
35 typedef struct _GstAggregatorClass GstAggregatorClass;
36
37 /************************
38  * GstAggregatorPad API *
39  ***********************/
40
41 #define GST_TYPE_AGGREGATOR_PAD            (gst_aggregator_pad_get_type())
42 #define GST_AGGREGATOR_PAD(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPad))
43 #define GST_AGGREGATOR_PAD_CAST(obj)       ((GstAggregatorPad *)(obj))
44 #define GST_AGGREGATOR_PAD_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPadClass))
45 #define GST_AGGREGATOR_PAD_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_AGGREGATOR_PAD, GstAggregatorPadClass))
46 #define GST_IS_AGGREGATOR_PAD(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGGREGATOR_PAD))
47 #define GST_IS_AGGREGATOR_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGGREGATOR_PAD))
48
49 /****************************
50  * GstAggregatorPad Structs *
51  ***************************/
52
53 typedef struct _GstAggregatorPad GstAggregatorPad;
54 typedef struct _GstAggregatorPadClass GstAggregatorPadClass;
55 typedef struct _GstAggregatorPadPrivate GstAggregatorPadPrivate;
56
57 /**
58  * GstAggregatorPad:
59  * @segment: last segment received.
60  *
61  * The implementation the GstPad to use with #GstAggregator
62  */
63 struct _GstAggregatorPad
64 {
65   GstPad                       parent;
66
67   /*< public >*/
68   /* Protected by the OBJECT_LOCK */
69   GstSegment segment;
70
71   /* < private > */
72   GstAggregatorPadPrivate   *  priv;
73
74   gpointer _gst_reserved[GST_PADDING];
75 };
76
77 /**
78  * GstAggregatorPadClass:
79  * @flush:       Optional
80  *               Called when the pad has received a flush stop, this is the place
81  *               to flush any information specific to the pad, it allows for individual
82  *               pads to be flushed while others might not be.
83  * @skip_buffer: Optional
84  *               Called before input buffers are queued in the pad, return %TRUE
85  *               if the buffer should be skipped.
86  *
87  */
88 struct _GstAggregatorPadClass
89 {
90   GstPadClass   parent_class;
91
92   GstFlowReturn (*flush)       (GstAggregatorPad * aggpad, GstAggregator * aggregator);
93   gboolean      (*skip_buffer) (GstAggregatorPad * aggpad, GstAggregator * aggregator, GstBuffer * buffer);
94
95   /*< private >*/
96   gpointer      _gst_reserved[GST_PADDING_LARGE];
97 };
98
99 GST_BASE_API
100 GType gst_aggregator_pad_get_type           (void);
101
102 /****************************
103  * GstAggregatorPad methods *
104  ***************************/
105
106 GST_BASE_API
107 GstBuffer * gst_aggregator_pad_pop_buffer   (GstAggregatorPad *  pad);
108
109 GST_BASE_API
110 GstBuffer * gst_aggregator_pad_peek_buffer  (GstAggregatorPad *  pad);
111
112 GST_BASE_API
113 gboolean    gst_aggregator_pad_drop_buffer  (GstAggregatorPad *  pad);
114
115 GST_BASE_API
116 gboolean    gst_aggregator_pad_has_buffer   (GstAggregatorPad * pad);
117
118 GST_BASE_API
119 gboolean    gst_aggregator_pad_is_eos       (GstAggregatorPad *  pad);
120
121 /*********************
122  * GstAggregator API *
123  ********************/
124
125 #define GST_TYPE_AGGREGATOR            (gst_aggregator_get_type())
126 #define GST_AGGREGATOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGGREGATOR,GstAggregator))
127 #define GST_AGGREGATOR_CAST(obj)       ((GstAggregator *)(obj))
128 #define GST_AGGREGATOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGGREGATOR,GstAggregatorClass))
129 #define GST_AGGREGATOR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_AGGREGATOR,GstAggregatorClass))
130 #define GST_IS_AGGREGATOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGGREGATOR))
131 #define GST_IS_AGGREGATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGGREGATOR))
132
133 #define GST_AGGREGATOR_FLOW_NEED_DATA             GST_FLOW_CUSTOM_ERROR
134
135 /**
136  * GstAggregator:
137  * @srcpad: the aggregator's source pad
138  *
139  * Aggregator base class object structure.
140  */
141 struct _GstAggregator
142 {
143   GstElement               parent;
144
145   /*< public >*/
146   GstPad                *  srcpad;
147
148   /*< private >*/
149   GstAggregatorPrivate  *  priv;
150
151   gpointer                 _gst_reserved[GST_PADDING_LARGE];
152 };
153
154 /**
155  * GstAggregatorClass:
156  * @flush:          Optional.
157  *                  Called after a successful flushing seek, once all the flush
158  *                  stops have been received. Flush pad-specific data in
159  *                  #GstAggregatorPad->flush.
160  * @clip:           Optional.
161  *                  Called when a buffer is received on a sink pad, the task of
162  *                  clipping it and translating it to the current segment falls
163  *                  on the subclass. The function should use the segment of data
164  *                  and the negotiated media type on the pad to perform
165  *                  clipping of input buffer. This function takes ownership of
166  *                  buf and should output a buffer or return NULL in
167  *                  if the buffer should be dropped.
168  * @finish_buffer:  Optional.
169  *                  Called when a subclass calls gst_aggregator_finish_buffer()
170  *                  from their aggregate function to push out a buffer.
171  *                  Subclasses can override this to modify or decorate buffers
172  *                  before they get pushed out. This function takes ownership
173  *                  of the buffer passed. Subclasses that override this method
174  *                  should always chain up to the parent class virtual method.
175  * @sink_event:     Optional.
176  *                  Called when an event is received on a sink pad, the subclass
177  *                  should always chain up.
178  * @sink_query:     Optional.
179  *                  Called when a query is received on a sink pad, the subclass
180  *                  should always chain up.
181  * @src_event:      Optional.
182  *                  Called when an event is received on the src pad, the subclass
183  *                  should always chain up.
184  * @src_query:      Optional.
185  *                  Called when a query is received on the src pad, the subclass
186  *                  should always chain up.
187  * @src_activate:   Optional.
188  *                  Called when the src pad is activated, it will start/stop its
189  *                  pad task right after that call.
190  * @aggregate:      Mandatory.
191  *                  Called when buffers are queued on all sinkpads. Classes
192  *                  should iterate the GstElement->sinkpads and peek or steal
193  *                  buffers from the #GstAggregatorPads. If the subclass returns
194  *                  GST_FLOW_EOS, sending of the eos event will be taken care
195  *                  of. Once / if a buffer has been constructed from the
196  *                  aggregated buffers, the subclass should call _finish_buffer.
197  * @stop:           Optional.
198  *                  Called when the element goes from PAUSED to READY.
199  *                  The subclass should free all resources and reset its state.
200  * @start:          Optional.
201  *                  Called when the element goes from READY to PAUSED.
202  *                  The subclass should get ready to process
203  *                  aggregated buffers.
204  * @get_next_time:  Optional.
205  *                  Called when the element needs to know the running time of the next
206  *                  rendered buffer for live pipelines. This causes deadline
207  *                  based aggregation to occur. Defaults to returning
208  *                  GST_CLOCK_TIME_NONE causing the element to wait for buffers
209  *                  on all sink pads before aggregating.
210  * @create_new_pad: Optional.
211  *                  Called when a new pad needs to be created. Allows subclass that
212  *                  don't have a single sink pad template to provide a pad based
213  *                  on the provided information.
214  * @update_src_caps: Lets subclasses update the #GstCaps representing
215  *                   the src pad caps before usage.  The result should end up
216  *                   in @ret. Return %GST_AGGREGATOR_FLOW_NEED_DATA to indicate that the
217  *                   element needs more information (caps, a buffer, etc) to
218  *                   choose the correct caps. Should return ANY caps if the
219  *                   stream has not caps at all.
220  * @fixate_src_caps: Optional.
221  *                   Fixate and return the src pad caps provided.  The function takes
222  *                   ownership of @caps and returns a fixated version of
223  *                   @caps. @caps is not guaranteed to be writable.
224  * @negotiated_src_caps: Optional.
225  *                       Notifies subclasses what caps format has been negotiated
226  * @decide_allocation: Optional.
227  *                     Allows the subclass to influence the allocation choices.
228  *                     Setup the allocation parameters for allocating output
229  *                     buffers. The passed in query contains the result of the
230  *                     downstream allocation query.
231  * @propose_allocation: Optional.
232  *                     Allows the subclass to handle the allocation query from upstream.
233  *
234  * The aggregator base class will handle in a thread-safe way all manners of
235  * concurrent flushes, seeks, pad additions and removals, leaving to the
236  * subclass the responsibility of clipping buffers, and aggregating buffers in
237  * the way the implementor sees fit.
238  *
239  * It will also take care of event ordering (stream-start, segment, eos).
240  *
241  * Basically, a simple implementation will override @aggregate, and call
242  * _finish_buffer from inside that function.
243  */
244 struct _GstAggregatorClass {
245   GstElementClass   parent_class;
246
247   GstFlowReturn     (*flush)          (GstAggregator    *  aggregator);
248
249   GstBuffer *       (*clip)           (GstAggregator    *  aggregator,
250                                        GstAggregatorPad *  aggregator_pad,
251                                        GstBuffer        *  buf);
252
253   GstFlowReturn     (*finish_buffer)  (GstAggregator    * aggregator,
254                                        GstBuffer        * buffer);
255
256   /* sinkpads virtual methods */
257   gboolean          (*sink_event)     (GstAggregator    *  aggregator,
258                                        GstAggregatorPad *  aggregator_pad,
259                                        GstEvent         *  event);
260
261   gboolean          (*sink_query)     (GstAggregator    *  aggregator,
262                                        GstAggregatorPad *  aggregator_pad,
263                                        GstQuery         *  query);
264
265   /* srcpad virtual methods */
266   gboolean          (*src_event)      (GstAggregator    *  aggregator,
267                                        GstEvent         *  event);
268
269   gboolean          (*src_query)      (GstAggregator    *  aggregator,
270                                        GstQuery         *  query);
271
272   gboolean          (*src_activate)   (GstAggregator    *  aggregator,
273                                        GstPadMode          mode,
274                                        gboolean            active);
275
276   GstFlowReturn     (*aggregate)      (GstAggregator    *  aggregator,
277                                        gboolean            timeout);
278
279   gboolean          (*stop)           (GstAggregator    *  aggregator);
280
281   gboolean          (*start)          (GstAggregator    *  aggregator);
282
283   GstClockTime      (*get_next_time)  (GstAggregator    *  aggregator);
284
285   GstAggregatorPad * (*create_new_pad) (GstAggregator  * self,
286                                         GstPadTemplate * templ,
287                                         const gchar    * req_name,
288                                         const GstCaps  * caps);
289
290   /**
291    * GstAggregatorClass::update_src_caps:
292    * @ret: (out) (allow-none):
293    */
294   GstFlowReturn     (*update_src_caps) (GstAggregator *  self,
295                                         GstCaps       *  caps,
296                                         GstCaps       ** ret);
297   GstCaps *         (*fixate_src_caps) (GstAggregator *  self,
298                                         GstCaps       *  caps);
299   gboolean          (*negotiated_src_caps) (GstAggregator *  self,
300                                             GstCaps      *  caps);
301   gboolean          (*decide_allocation) (GstAggregator * self,
302                                           GstQuery * query);
303   gboolean          (*propose_allocation) (GstAggregator * self,
304                                            GstAggregatorPad * pad,
305                                            GstQuery * decide_query,
306                                            GstQuery * query);
307   /*< private >*/
308   gpointer          _gst_reserved[GST_PADDING_LARGE];
309 };
310
311 /************************************
312  * GstAggregator convenience macros *
313  ***********************************/
314
315 /**
316  * GST_AGGREGATOR_SRC_PAD:
317  * @agg: a #GstAggregator
318  *
319  * Convenience macro to access the source pad of #GstAggregator
320  *
321  * Since: 1.6
322  */
323 #define GST_AGGREGATOR_SRC_PAD(agg) (((GstAggregator *)(agg))->srcpad)
324
325 /*************************
326  * GstAggregator methods *
327  ************************/
328
329 GST_BASE_API
330 GstFlowReturn  gst_aggregator_finish_buffer         (GstAggregator                *  aggregator,
331                                                      GstBuffer                    *  buffer);
332
333 GST_BASE_API
334 void           gst_aggregator_set_src_caps          (GstAggregator                *  self,
335                                                      GstCaps                      *  caps);
336
337 GST_BASE_API
338 void           gst_aggregator_set_latency           (GstAggregator                *  self,
339                                                      GstClockTime                    min_latency,
340                                                      GstClockTime                    max_latency);
341
342 GST_BASE_API
343 GType gst_aggregator_get_type(void);
344
345 GST_BASE_API
346 GstClockTime  gst_aggregator_get_latency           (GstAggregator                 *  self);
347
348 GST_BASE_API
349 GstBufferPool * gst_aggregator_get_buffer_pool     (GstAggregator                 * self);
350
351 GST_BASE_API
352 void            gst_aggregator_get_allocator       (GstAggregator                 * self,
353                                                     GstAllocator                 ** allocator,
354                                                     GstAllocationParams           * params);
355
356 GST_BASE_API
357 GstClockTime    gst_aggregator_simple_get_next_time (GstAggregator                * self);
358
359
360 G_END_DECLS
361
362 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
363 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAggregator, gst_object_unref)
364 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAggregatorPad, gst_object_unref)
365 #endif
366
367 #endif /* __GST_AGGREGATOR_H__ */