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