1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gst.base.BaseParse;
26 
27 private import gst.base.BaseParseFrame;
28 private import gst.base.c.functions;
29 public  import gst.base.c.types;
30 private import gstreamer.Element;
31 private import gstreamer.TagList;
32 
33 
34 /**
35  * This base class is for parser elements that process data and splits it
36  * into separate audio/video/whatever frames.
37  * 
38  * It provides for:
39  * 
40  * * provides one sink pad and one source pad
41  * * handles state changes
42  * * can operate in pull mode or push mode
43  * * handles seeking in both modes
44  * * handles events (SEGMENT/EOS/FLUSH)
45  * * handles queries (POSITION/DURATION/SEEKING/FORMAT/CONVERT)
46  * * handles flushing
47  * 
48  * The purpose of this base class is to provide the basic functionality of
49  * a parser and share a lot of rather complex code.
50  * 
51  * # Description of the parsing mechanism:
52  * 
53  * ## Set-up phase
54  * 
55  * * #GstBaseParse calls #GstBaseParseClass.start() to inform subclass
56  * that data processing is about to start now.
57  * 
58  * * #GstBaseParse class calls #GstBaseParseClass.set_sink_caps() to
59  * inform the subclass about incoming sinkpad caps. Subclass could
60  * already set the srcpad caps accordingly, but this might be delayed
61  * until calling gst_base_parse_finish_frame() with a non-queued frame.
62  * 
63  * * At least at this point subclass needs to tell the #GstBaseParse class
64  * how big data chunks it wants to receive (minimum frame size ). It can
65  * do this with gst_base_parse_set_min_frame_size().
66  * 
67  * * #GstBaseParse class sets up appropriate data passing mode (pull/push)
68  * and starts to process the data.
69  * 
70  * ## Parsing phase
71  * 
72  * * #GstBaseParse gathers at least min_frame_size bytes of data either
73  * by pulling it from upstream or collecting buffers in an internal
74  * #GstAdapter.
75  * 
76  * * A buffer of (at least) min_frame_size bytes is passed to subclass
77  * with #GstBaseParseClass.handle_frame(). Subclass checks the contents
78  * and can optionally return #GST_FLOW_OK along with an amount of data
79  * to be skipped to find a valid frame (which will result in a
80  * subsequent DISCONT).  If, otherwise, the buffer does not hold a
81  * complete frame, #GstBaseParseClass.handle_frame() can merely return
82  * and will be called again when additional data is available.  In push
83  * mode this amounts to an additional input buffer (thus minimal
84  * additional latency), in pull mode this amounts to some arbitrary
85  * reasonable buffer size increase.
86  * 
87  * Of course, gst_base_parse_set_min_frame_size() could also be used if
88  * a very specific known amount of additional data is required.  If,
89  * however, the buffer holds a complete valid frame, it can pass the
90  * size of this frame to gst_base_parse_finish_frame().
91  * 
92  * If acting as a converter, it can also merely indicate consumed input
93  * data while simultaneously providing custom output data.  Note that
94  * baseclass performs some processing (such as tracking overall consumed
95  * data rate versus duration) for each finished frame, but other state
96  * is only updated upon each call to #GstBaseParseClass.handle_frame()
97  * (such as tracking upstream input timestamp).
98  * 
99  * Subclass is also responsible for setting the buffer metadata
100  * (e.g. buffer timestamp and duration, or keyframe if applicable).
101  * (although the latter can also be done by #GstBaseParse if it is
102  * appropriately configured, see below).  Frame is provided with
103  * timestamp derived from upstream (as much as generally possible),
104  * duration obtained from configuration (see below), and offset
105  * if meaningful (in pull mode).
106  * 
107  * Note that #GstBaseParseClass.handle_frame() might receive any small
108  * amount of input data when leftover data is being drained (e.g. at
109  * EOS).
110  * 
111  * * As part of finish frame processing, just prior to actually pushing
112  * the buffer in question, it is passed to
113  * #GstBaseParseClass.pre_push_frame() which gives subclass yet one last
114  * chance to examine buffer metadata, or to send some custom (tag)
115  * events, or to perform custom (segment) filtering.
116  * 
117  * * During the parsing process #GstBaseParseClass will handle both srcpad
118  * and sinkpad events. They will be passed to subclass if
119  * #GstBaseParseClass.event() or #GstBaseParseClass.src_event()
120  * implementations have been provided.
121  * 
122  * ## Shutdown phase
123  * 
124  * * #GstBaseParse class calls #GstBaseParseClass.stop() to inform the
125  * subclass that data parsing will be stopped.
126  * 
127  * Subclass is responsible for providing pad template caps for source and
128  * sink pads. The pads need to be named "sink" and "src". It also needs to
129  * set the fixed caps on srcpad, when the format is ensured (e.g.  when
130  * base class calls subclass' #GstBaseParseClass.set_sink_caps() function).
131  * 
132  * This base class uses %GST_FORMAT_DEFAULT as a meaning of frames. So,
133  * subclass conversion routine needs to know that conversion from
134  * %GST_FORMAT_TIME to %GST_FORMAT_DEFAULT must return the
135  * frame number that can be found from the given byte position.
136  * 
137  * #GstBaseParse uses subclasses conversion methods also for seeking (or
138  * otherwise uses its own default one, see also below).
139  * 
140  * Subclass @start and @stop functions will be called to inform the beginning
141  * and end of data processing.
142  * 
143  * Things that subclass need to take care of:
144  * 
145  * * Provide pad templates
146  * * Fixate the source pad caps when appropriate
147  * * Inform base class how big data chunks should be retrieved. This is
148  * done with gst_base_parse_set_min_frame_size() function.
149  * * Examine data chunks passed to subclass with
150  * #GstBaseParseClass.handle_frame() and pass proper frame(s) to
151  * gst_base_parse_finish_frame(), and setting src pad caps and timestamps
152  * on frame.
153  * * Provide conversion functions
154  * * Update the duration information with gst_base_parse_set_duration()
155  * * Optionally passthrough using gst_base_parse_set_passthrough()
156  * * Configure various baseparse parameters using
157  * gst_base_parse_set_average_bitrate(), gst_base_parse_set_syncable()
158  * and gst_base_parse_set_frame_rate().
159  * 
160  * * In particular, if subclass is unable to determine a duration, but
161  * parsing (or specs) yields a frames per seconds rate, then this can be
162  * provided to #GstBaseParse to enable it to cater for buffer time
163  * metadata (which will be taken from upstream as much as
164  * possible). Internally keeping track of frame durations and respective
165  * sizes that have been pushed provides #GstBaseParse with an estimated
166  * bitrate. A default #GstBaseParseClass.convert() (used if not
167  * overridden) will then use these rates to perform obvious conversions.
168  * These rates are also used to update (estimated) duration at regular
169  * frame intervals.
170  */
171 public class BaseParse : Element
172 {
173 	/** the main Gtk struct */
174 	protected GstBaseParse* gstBaseParse;
175 
176 	/** Get the main Gtk struct */
177 	public GstBaseParse* getBaseParseStruct(bool transferOwnership = false)
178 	{
179 		if (transferOwnership)
180 			ownedRef = false;
181 		return gstBaseParse;
182 	}
183 
184 	/** the main Gtk struct as a void* */
185 	protected override void* getStruct()
186 	{
187 		return cast(void*)gstBaseParse;
188 	}
189 
190 	/**
191 	 * Sets our main struct and passes it to the parent class.
192 	 */
193 	public this (GstBaseParse* gstBaseParse, bool ownedRef = false)
194 	{
195 		this.gstBaseParse = gstBaseParse;
196 		super(cast(GstElement*)gstBaseParse, ownedRef);
197 	}
198 
199 
200 	/** */
201 	public static GType getType()
202 	{
203 		return gst_base_parse_get_type();
204 	}
205 
206 	/**
207 	 * Adds an entry to the index associating @offset to @ts.  It is recommended
208 	 * to only add keyframe entries.  @force allows to bypass checks, such as
209 	 * whether the stream is (upstream) seekable, another entry is already "close"
210 	 * to the new entry, etc.
211 	 *
212 	 * Params:
213 	 *     offset = offset of entry
214 	 *     ts = timestamp associated with offset
215 	 *     key = whether entry refers to keyframe
216 	 *     force = add entry disregarding sanity checks
217 	 *
218 	 * Returns: #gboolean indicating whether entry was added
219 	 */
220 	public bool addIndexEntry(ulong offset, GstClockTime ts, bool key, bool force)
221 	{
222 		return gst_base_parse_add_index_entry(gstBaseParse, offset, ts, key, force) != 0;
223 	}
224 
225 	/**
226 	 * Default implementation of #GstBaseParseClass.convert().
227 	 *
228 	 * Params:
229 	 *     srcFormat = #GstFormat describing the source format.
230 	 *     srcValue = Source value to be converted.
231 	 *     destFormat = #GstFormat defining the converted format.
232 	 *     destValue = Pointer where the conversion result will be put.
233 	 *
234 	 * Returns: %TRUE if conversion was successful.
235 	 */
236 	public bool convertDefault(GstFormat srcFormat, long srcValue, GstFormat destFormat, out long destValue)
237 	{
238 		return gst_base_parse_convert_default(gstBaseParse, srcFormat, srcValue, destFormat, &destValue) != 0;
239 	}
240 
241 	/**
242 	 * Drains the adapter until it is empty. It decreases the min_frame_size to
243 	 * match the current adapter size and calls chain method until the adapter
244 	 * is emptied or chain returns with error.
245 	 *
246 	 * Since: 1.12
247 	 */
248 	public void drain()
249 	{
250 		gst_base_parse_drain(gstBaseParse);
251 	}
252 
253 	/**
254 	 * Collects parsed data and pushes this downstream.
255 	 * Source pad caps must be set when this is called.
256 	 *
257 	 * If @frame's out_buffer is set, that will be used as subsequent frame data.
258 	 * Otherwise, @size samples will be taken from the input and used for output,
259 	 * and the output's metadata (timestamps etc) will be taken as (optionally)
260 	 * set by the subclass on @frame's (input) buffer (which is otherwise
261 	 * ignored for any but the above purpose/information).
262 	 *
263 	 * Note that the latter buffer is invalidated by this call, whereas the
264 	 * caller retains ownership of @frame.
265 	 *
266 	 * Params:
267 	 *     frame = a #GstBaseParseFrame
268 	 *     size = consumed input data represented by frame
269 	 *
270 	 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
271 	 */
272 	public GstFlowReturn finishFrame(BaseParseFrame frame, int size)
273 	{
274 		return gst_base_parse_finish_frame(gstBaseParse, (frame is null) ? null : frame.getBaseParseFrameStruct(), size);
275 	}
276 
277 	/**
278 	 * Sets the parser subclass's tags and how they should be merged with any
279 	 * upstream stream tags. This will override any tags previously-set
280 	 * with gst_base_parse_merge_tags().
281 	 *
282 	 * Note that this is provided for convenience, and the subclass is
283 	 * not required to use this and can still do tag handling on its own.
284 	 *
285 	 * Params:
286 	 *     tags = a #GstTagList to merge, or NULL to unset
287 	 *         previously-set tags
288 	 *     mode = the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
289 	 *
290 	 * Since: 1.6
291 	 */
292 	public void mergeTags(TagList tags, GstTagMergeMode mode)
293 	{
294 		gst_base_parse_merge_tags(gstBaseParse, (tags is null) ? null : tags.getTagListStruct(), mode);
295 	}
296 
297 	/**
298 	 * Pushes the frame's buffer downstream, sends any pending events and
299 	 * does some timestamp and segment handling. Takes ownership of
300 	 * frame's buffer, though caller retains ownership of @frame.
301 	 *
302 	 * This must be called with sinkpad STREAM_LOCK held.
303 	 *
304 	 * Params:
305 	 *     frame = a #GstBaseParseFrame
306 	 *
307 	 * Returns: #GstFlowReturn
308 	 */
309 	public GstFlowReturn pushFrame(BaseParseFrame frame)
310 	{
311 		return gst_base_parse_push_frame(gstBaseParse, (frame is null) ? null : frame.getBaseParseFrameStruct());
312 	}
313 
314 	/**
315 	 * Optionally sets the average bitrate detected in media (if non-zero),
316 	 * e.g. based on metadata, as it will be posted to the application.
317 	 *
318 	 * By default, announced average bitrate is estimated. The average bitrate
319 	 * is used to estimate the total duration of the stream and to estimate
320 	 * a seek position, if there's no index and the format is syncable
321 	 * (see gst_base_parse_set_syncable()).
322 	 *
323 	 * Params:
324 	 *     bitrate = average bitrate in bits/second
325 	 */
326 	public void setAverageBitrate(uint bitrate)
327 	{
328 		gst_base_parse_set_average_bitrate(gstBaseParse, bitrate);
329 	}
330 
331 	/**
332 	 * Sets the duration of the currently playing media. Subclass can use this
333 	 * when it is able to determine duration and/or notices a change in the media
334 	 * duration.  Alternatively, if @interval is non-zero (default), then stream
335 	 * duration is determined based on estimated bitrate, and updated every @interval
336 	 * frames.
337 	 *
338 	 * Params:
339 	 *     fmt = #GstFormat.
340 	 *     duration = duration value.
341 	 *     interval = how often to update the duration estimate based on bitrate, or 0.
342 	 */
343 	public void setDuration(GstFormat fmt, long duration, int interval)
344 	{
345 		gst_base_parse_set_duration(gstBaseParse, fmt, duration, interval);
346 	}
347 
348 	/**
349 	 * If frames per second is configured, parser can take care of buffer duration
350 	 * and timestamping.  When performing segment clipping, or seeking to a specific
351 	 * location, a corresponding decoder might need an initial @lead_in and a
352 	 * following @lead_out number of frames to ensure the desired segment is
353 	 * entirely filled upon decoding.
354 	 *
355 	 * Params:
356 	 *     fpsNum = frames per second (numerator).
357 	 *     fpsDen = frames per second (denominator).
358 	 *     leadIn = frames needed before a segment for subsequent decode
359 	 *     leadOut = frames needed after a segment
360 	 */
361 	public void setFrameRate(uint fpsNum, uint fpsDen, uint leadIn, uint leadOut)
362 	{
363 		gst_base_parse_set_frame_rate(gstBaseParse, fpsNum, fpsDen, leadIn, leadOut);
364 	}
365 
366 	/**
367 	 * Set if frames carry timing information which the subclass can (generally)
368 	 * parse and provide.  In particular, intrinsic (rather than estimated) time
369 	 * can be obtained following a seek.
370 	 *
371 	 * Params:
372 	 *     hasTiming = whether frames carry timing information
373 	 */
374 	public void setHasTimingInfo(bool hasTiming)
375 	{
376 		gst_base_parse_set_has_timing_info(gstBaseParse, hasTiming);
377 	}
378 
379 	/**
380 	 * By default, the base class might try to infer PTS from DTS and vice
381 	 * versa.  While this is generally correct for audio data, it may not
382 	 * be otherwise. Sub-classes implementing such formats should disable
383 	 * timestamp inferring.
384 	 *
385 	 * Params:
386 	 *     inferTs = %TRUE if parser should infer DTS/PTS from each other
387 	 */
388 	public void setInferTs(bool inferTs)
389 	{
390 		gst_base_parse_set_infer_ts(gstBaseParse, inferTs);
391 	}
392 
393 	/**
394 	 * Sets the minimum and maximum (which may likely be equal) latency introduced
395 	 * by the parsing process.  If there is such a latency, which depends on the
396 	 * particular parsing of the format, it typically corresponds to 1 frame duration.
397 	 *
398 	 * Params:
399 	 *     minLatency = minimum parse latency
400 	 *     maxLatency = maximum parse latency
401 	 */
402 	public void setLatency(GstClockTime minLatency, GstClockTime maxLatency)
403 	{
404 		gst_base_parse_set_latency(gstBaseParse, minLatency, maxLatency);
405 	}
406 
407 	/**
408 	 * Subclass can use this function to tell the base class that it needs to
409 	 * be given buffers of at least @min_size bytes.
410 	 *
411 	 * Params:
412 	 *     minSize = Minimum size in bytes of the data that this base class should
413 	 *         give to subclass.
414 	 */
415 	public void setMinFrameSize(uint minSize)
416 	{
417 		gst_base_parse_set_min_frame_size(gstBaseParse, minSize);
418 	}
419 
420 	/**
421 	 * Set if the nature of the format or configuration does not allow (much)
422 	 * parsing, and the parser should operate in passthrough mode (which only
423 	 * applies when operating in push mode). That is, incoming buffers are
424 	 * pushed through unmodified, i.e. no #GstBaseParseClass.handle_frame()
425 	 * will be invoked, but #GstBaseParseClass.pre_push_frame() will still be
426 	 * invoked, so subclass can perform as much or as little is appropriate for
427 	 * passthrough semantics in #GstBaseParseClass.pre_push_frame().
428 	 *
429 	 * Params:
430 	 *     passthrough = %TRUE if parser should run in passthrough mode
431 	 */
432 	public void setPassthrough(bool passthrough)
433 	{
434 		gst_base_parse_set_passthrough(gstBaseParse, passthrough);
435 	}
436 
437 	/**
438 	 * By default, the base class will guess PTS timestamps using a simple
439 	 * interpolation (previous timestamp + duration), which is incorrect for
440 	 * data streams with reordering, where PTS can go backward. Sub-classes
441 	 * implementing such formats should disable PTS interpolation.
442 	 *
443 	 * Params:
444 	 *     ptsInterpolate = %TRUE if parser should interpolate PTS timestamps
445 	 */
446 	public void setPtsInterpolation(bool ptsInterpolate)
447 	{
448 		gst_base_parse_set_pts_interpolation(gstBaseParse, ptsInterpolate);
449 	}
450 
451 	/**
452 	 * Set if frame starts can be identified. This is set by default and
453 	 * determines whether seeking based on bitrate averages
454 	 * is possible for a format/stream.
455 	 *
456 	 * Params:
457 	 *     syncable = set if frame starts can be identified
458 	 */
459 	public void setSyncable(bool syncable)
460 	{
461 		gst_base_parse_set_syncable(gstBaseParse, syncable);
462 	}
463 
464 	/**
465 	 * This function should only be called from a @handle_frame implementation.
466 	 *
467 	 * #GstBaseParse creates initial timestamps for frames by using the last
468 	 * timestamp seen in the stream before the frame starts.  In certain
469 	 * cases, the correct timestamps will occur in the stream after the
470 	 * start of the frame, but before the start of the actual picture data.
471 	 * This function can be used to set the timestamps based on the offset
472 	 * into the frame data that the picture starts.
473 	 *
474 	 * Params:
475 	 *     offset = offset into current buffer
476 	 *
477 	 * Since: 1.2
478 	 */
479 	public void setTsAtOffset(size_t offset)
480 	{
481 		gst_base_parse_set_ts_at_offset(gstBaseParse, offset);
482 	}
483 }