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