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 gstreamer.Bin;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gstreamer.ChildProxyIF;
32 private import gstreamer.ChildProxyT;
33 private import gstreamer.Element;
34 private import gstreamer.Iterator;
35 private import gstreamer.Pad;
36 private import gstreamerc.gstreamer;
37 public  import gstreamerc.gstreamertypes;
38 public  import gtkc.gdktypes;
39 
40 
41 /**
42  * #GstBin is an element that can contain other #GstElement, allowing them to be
43  * managed as a group.
44  * Pads from the child elements can be ghosted to the bin, see #GstGhostPad.
45  * This makes the bin look like any other elements and enables creation of
46  * higher-level abstraction elements.
47  * 
48  * A new #GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
49  * want to create a toplevel bin because a normal bin doesn't have a bus or
50  * handle clock distribution of its own.
51  * 
52  * After the bin has been created you will typically add elements to it with
53  * gst_bin_add(). You can remove elements with gst_bin_remove().
54  * 
55  * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
56  * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
57  * purposes and will query the parent bins when the element is not found in the
58  * current bin.
59  * 
60  * An iterator of elements in a bin can be retrieved with
61  * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
62  * elements in a bin.
63  * 
64  * gst_object_unref() is used to drop your reference to the bin.
65  * 
66  * The #GstBin::element-added signal is fired whenever a new element is added to
67  * the bin. Likewise the #GstBin::element-removed signal is fired whenever an
68  * element is removed from the bin.
69  * 
70  * <refsect2><title>Notes</title>
71  * <para>
72  * A #GstBin internally intercepts every #GstMessage posted by its children and
73  * implements the following default behaviour for each of them:
74  * <variablelist>
75  * <varlistentry>
76  * <term>GST_MESSAGE_EOS</term>
77  * <listitem><para>This message is only posted by sinks in the PLAYING
78  * state. If all sinks posted the EOS message, this bin will post and EOS
79  * message upwards.</para></listitem>
80  * </varlistentry>
81  * <varlistentry>
82  * <term>GST_MESSAGE_SEGMENT_START</term>
83  * <listitem><para>just collected and never forwarded upwards.
84  * The messages are used to decide when all elements have completed playback
85  * of their segment.</para></listitem>
86  * </varlistentry>
87  * <varlistentry>
88  * <term>GST_MESSAGE_SEGMENT_DONE</term>
89  * <listitem><para> Is posted by #GstBin when all elements that posted
90  * a SEGMENT_START have posted a SEGMENT_DONE.</para></listitem>
91  * </varlistentry>
92  * <varlistentry>
93  * <term>GST_MESSAGE_DURATION_CHANGED</term>
94  * <listitem><para> Is posted by an element that detected a change
95  * in the stream duration. The default bin behaviour is to clear any
96  * cached duration values so that the next duration query will perform
97  * a full duration recalculation. The duration change is posted to the
98  * application so that it can refetch the new duration with a duration
99  * query. Note that these messages can be posted before the bin is
100  * prerolled, in which case the duration query might fail.
101  * </para></listitem>
102  * </varlistentry>
103  * <varlistentry>
104  * <term>GST_MESSAGE_CLOCK_LOST</term>
105  * <listitem><para> This message is posted by an element when it
106  * can no longer provide a clock. The default bin behaviour is to
107  * check if the lost clock was the one provided by the bin. If so and
108  * the bin is currently in the PLAYING state, the message is forwarded to
109  * the bin parent.
110  * This message is also generated when a clock provider is removed from
111  * the bin. If this message is received by the application, it should
112  * PAUSE the pipeline and set it back to PLAYING to force a new clock
113  * distribution.
114  * </para></listitem>
115  * </varlistentry>
116  * <varlistentry>
117  * <term>GST_MESSAGE_CLOCK_PROVIDE</term>
118  * <listitem><para> This message is generated when an element
119  * can provide a clock. This mostly happens when a new clock
120  * provider is added to the bin. The default behaviour of the bin is to
121  * mark the currently selected clock as dirty, which will perform a clock
122  * recalculation the next time the bin is asked to provide a clock.
123  * This message is never sent tot the application but is forwarded to
124  * the parent of the bin.
125  * </para></listitem>
126  * </varlistentry>
127  * <varlistentry>
128  * <term>OTHERS</term>
129  * <listitem><para> posted upwards.</para></listitem>
130  * </varlistentry>
131  * </variablelist>
132  * 
133  * 
134  * A #GstBin implements the following default behaviour for answering to a
135  * #GstQuery:
136  * <variablelist>
137  * <varlistentry>
138  * <term>GST_QUERY_DURATION</term>
139  * <listitem><para>If the query has been asked before with the same format
140  * and the bin is a toplevel bin (ie. has no parent),
141  * use the cached previous value. If no previous value was cached, the
142  * query is sent to all sink elements in the bin and the MAXIMUM of all
143  * values is returned. If the bin is a toplevel bin the value is cached.
144  * If no sinks are available in the bin, the query fails.
145  * </para></listitem>
146  * </varlistentry>
147  * <varlistentry>
148  * <term>GST_QUERY_POSITION</term>
149  * <listitem><para>The query is sent to all sink elements in the bin and the
150  * MAXIMUM of all values is returned. If no sinks are available in the bin,
151  * the query fails.
152  * </para></listitem>
153  * </varlistentry>
154  * <varlistentry>
155  * <term>OTHERS</term>
156  * <listitem><para>the query is forwarded to all sink elements, the result
157  * of the first sink that answers the query successfully is returned. If no
158  * sink is in the bin, the query fails.</para></listitem>
159  * </varlistentry>
160  * </variablelist>
161  * 
162  * A #GstBin will by default forward any event sent to it to all sink elements.
163  * If all the sinks return %TRUE, the bin will also return %TRUE, else %FALSE is
164  * returned. If no sinks are in the bin, the event handler will return %TRUE.
165  * 
166  * </para>
167  * </refsect2>
168  */
169 public class Bin : Element, ChildProxyIF
170 {
171 	/** the main Gtk struct */
172 	protected GstBin* gstBin;
173 
174 	/** Get the main Gtk struct */
175 	public GstBin* getBinStruct()
176 	{
177 		return gstBin;
178 	}
179 
180 	/** the main Gtk struct as a void* */
181 	protected override void* getStruct()
182 	{
183 		return cast(void*)gstBin;
184 	}
185 
186 	protected override void setStruct(GObject* obj)
187 	{
188 		gstBin = cast(GstBin*)obj;
189 		super.setStruct(obj);
190 	}
191 
192 	/**
193 	 * Sets our main struct and passes it to the parent class.
194 	 */
195 	public this (GstBin* gstBin, bool ownedRef = false)
196 	{
197 		this.gstBin = gstBin;
198 		super(cast(GstElement*)gstBin, ownedRef);
199 	}
200 
201 	// add the ChildProxy capabilities
202 	mixin ChildProxyT!(GstBin);
203 
204 	/** */
205 	public this(Element elem)
206 	{
207 		super( elem.getElementStruct(), true );
208 		this.gstBin = cast(GstBin*)elem.getElementStruct();
209 	}
210 
211 	/**
212 	 */
213 
214 	public static GType getType()
215 	{
216 		return gst_bin_get_type();
217 	}
218 
219 	/**
220 	 * Creates a new bin with the given name.
221 	 *
222 	 * Params:
223 	 *     name = the name of the new bin
224 	 *
225 	 * Return: a new #GstBin
226 	 *
227 	 * Throws: ConstructionException GTK+ fails to create the object.
228 	 */
229 	public this(string name)
230 	{
231 		auto p = gst_bin_new(Str.toStringz(name));
232 		
233 		if(p is null)
234 		{
235 			throw new ConstructionException("null returned by new");
236 		}
237 		
238 		this(cast(GstBin*) p);
239 	}
240 
241 	/**
242 	 * Adds the given element to the bin.  Sets the element's parent, and thus
243 	 * takes ownership of the element. An element can only be added to one bin.
244 	 *
245 	 * If the element's pads are linked to other pads, the pads will be unlinked
246 	 * before the element is added to the bin.
247 	 *
248 	 * <note>
249 	 * When you add an element to an already-running pipeline, you will have to
250 	 * take care to set the state of the newly-added element to the desired
251 	 * state (usually PLAYING or PAUSED, same you set the pipeline to originally)
252 	 * with gst_element_set_state(), or use gst_element_sync_state_with_parent().
253 	 * The bin or pipeline will not take care of this for you.
254 	 * </note>
255 	 *
256 	 * MT safe.
257 	 *
258 	 * Params:
259 	 *     element = the #GstElement to add
260 	 *
261 	 * Return: %TRUE if the element could be added, %FALSE if
262 	 *     the bin does not want to accept the element.
263 	 */
264 	public bool add(Element element)
265 	{
266 		return gst_bin_add(gstBin, (element is null) ? null : element.getElementStruct()) != 0;
267 	}
268 
269 	/**
270 	 * Recursively looks for elements with an unlinked pad of the given
271 	 * direction within the specified bin and returns an unlinked pad
272 	 * if one is found, or %NULL otherwise. If a pad is found, the caller
273 	 * owns a reference to it and should use gst_object_unref() on the
274 	 * pad when it is not needed any longer.
275 	 *
276 	 * Params:
277 	 *     direction = whether to look for an unlinked source or sink pad
278 	 *
279 	 * Return: unlinked pad of the given
280 	 *     direction, %NULL.
281 	 */
282 	public Pad findUnlinkedPad(GstPadDirection direction)
283 	{
284 		auto p = gst_bin_find_unlinked_pad(gstBin, direction);
285 		
286 		if(p is null)
287 		{
288 			return null;
289 		}
290 		
291 		return ObjectG.getDObject!(Pad)(cast(GstPad*) p, true);
292 	}
293 
294 	/**
295 	 * Looks for an element inside the bin that implements the given
296 	 * interface. If such an element is found, it returns the element.
297 	 * You can cast this element to the given interface afterwards.  If you want
298 	 * all elements that implement the interface, use
299 	 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
300 	 *
301 	 * MT safe.  Caller owns returned reference.
302 	 *
303 	 * Params:
304 	 *     iface = the #GType of an interface
305 	 *
306 	 * Return: A #GstElement inside the bin implementing the interface
307 	 */
308 	public Element getByInterface(GType iface)
309 	{
310 		auto p = gst_bin_get_by_interface(gstBin, iface);
311 		
312 		if(p is null)
313 		{
314 			return null;
315 		}
316 		
317 		return ObjectG.getDObject!(Element)(cast(GstElement*) p, true);
318 	}
319 
320 	/**
321 	 * Gets the element with the given name from a bin. This
322 	 * function recurses into child bins.
323 	 *
324 	 * Returns %NULL if no element with the given name is found in the bin.
325 	 *
326 	 * MT safe.  Caller owns returned reference.
327 	 *
328 	 * Params:
329 	 *     name = the element name to search for
330 	 *
331 	 * Return: the #GstElement with the given
332 	 *     name, or %NULL
333 	 */
334 	public Element getByName(string name)
335 	{
336 		auto p = gst_bin_get_by_name(gstBin, Str.toStringz(name));
337 		
338 		if(p is null)
339 		{
340 			return null;
341 		}
342 		
343 		return ObjectG.getDObject!(Element)(cast(GstElement*) p, true);
344 	}
345 
346 	/**
347 	 * Gets the element with the given name from this bin. If the
348 	 * element is not found, a recursion is performed on the parent bin.
349 	 *
350 	 * Returns %NULL if:
351 	 * - no element with the given name is found in the bin
352 	 *
353 	 * MT safe.  Caller owns returned reference.
354 	 *
355 	 * Params:
356 	 *     name = the element name to search for
357 	 *
358 	 * Return: the #GstElement with the given
359 	 *     name, or %NULL
360 	 */
361 	public Element getByNameRecurseUp(string name)
362 	{
363 		auto p = gst_bin_get_by_name_recurse_up(gstBin, Str.toStringz(name));
364 		
365 		if(p is null)
366 		{
367 			return null;
368 		}
369 		
370 		return ObjectG.getDObject!(Element)(cast(GstElement*) p, true);
371 	}
372 
373 	/**
374 	 * Looks for all elements inside the bin that implements the given
375 	 * interface. You can safely cast all returned elements to the given interface.
376 	 * The function recurses inside child bins. The iterator will yield a series
377 	 * of #GstElement that should be unreffed after use.
378 	 *
379 	 * MT safe.  Caller owns returned value.
380 	 *
381 	 * Params:
382 	 *     iface = the #GType of an interface
383 	 *
384 	 * Return: a #GstIterator of #GstElement
385 	 *     for all elements in the bin implementing the given interface,
386 	 *     or %NULL
387 	 */
388 	public Iterator iterateAllByInterface(GType iface)
389 	{
390 		auto p = gst_bin_iterate_all_by_interface(gstBin, iface);
391 		
392 		if(p is null)
393 		{
394 			return null;
395 		}
396 		
397 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
398 	}
399 
400 	/**
401 	 * Gets an iterator for the elements in this bin.
402 	 *
403 	 * MT safe.  Caller owns returned value.
404 	 *
405 	 * Return: a #GstIterator of #GstElement,
406 	 *     or %NULL
407 	 */
408 	public Iterator iterateElements()
409 	{
410 		auto p = gst_bin_iterate_elements(gstBin);
411 		
412 		if(p is null)
413 		{
414 			return null;
415 		}
416 		
417 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
418 	}
419 
420 	/**
421 	 * Gets an iterator for the elements in this bin.
422 	 * This iterator recurses into GstBin children.
423 	 *
424 	 * MT safe.  Caller owns returned value.
425 	 *
426 	 * Return: a #GstIterator of #GstElement,
427 	 *     or %NULL
428 	 */
429 	public Iterator iterateRecurse()
430 	{
431 		auto p = gst_bin_iterate_recurse(gstBin);
432 		
433 		if(p is null)
434 		{
435 			return null;
436 		}
437 		
438 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
439 	}
440 
441 	/**
442 	 * Gets an iterator for all elements in the bin that have the
443 	 * #GST_ELEMENT_FLAG_SINK flag set.
444 	 *
445 	 * MT safe.  Caller owns returned value.
446 	 *
447 	 * Return: a #GstIterator of #GstElement,
448 	 *     or %NULL
449 	 */
450 	public Iterator iterateSinks()
451 	{
452 		auto p = gst_bin_iterate_sinks(gstBin);
453 		
454 		if(p is null)
455 		{
456 			return null;
457 		}
458 		
459 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
460 	}
461 
462 	/**
463 	 * Gets an iterator for the elements in this bin in topologically
464 	 * sorted order. This means that the elements are returned from
465 	 * the most downstream elements (sinks) to the sources.
466 	 *
467 	 * This function is used internally to perform the state changes
468 	 * of the bin elements and for clock selection.
469 	 *
470 	 * MT safe.  Caller owns returned value.
471 	 *
472 	 * Return: a #GstIterator of #GstElement,
473 	 *     or %NULL
474 	 */
475 	public Iterator iterateSorted()
476 	{
477 		auto p = gst_bin_iterate_sorted(gstBin);
478 		
479 		if(p is null)
480 		{
481 			return null;
482 		}
483 		
484 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
485 	}
486 
487 	/**
488 	 * Gets an iterator for all elements in the bin that have the
489 	 * #GST_ELEMENT_FLAG_SOURCE flag set.
490 	 *
491 	 * MT safe.  Caller owns returned value.
492 	 *
493 	 * Return: a #GstIterator of #GstElement,
494 	 *     or %NULL
495 	 */
496 	public Iterator iterateSources()
497 	{
498 		auto p = gst_bin_iterate_sources(gstBin);
499 		
500 		if(p is null)
501 		{
502 			return null;
503 		}
504 		
505 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
506 	}
507 
508 	/**
509 	 * Query @bin for the current latency using and reconfigures this latency to all the
510 	 * elements with a LATENCY event.
511 	 *
512 	 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
513 	 * is posted on the bus.
514 	 *
515 	 * This function simply emits the 'do-latency' signal so any custom latency
516 	 * calculations will be performed.
517 	 *
518 	 * Return: %TRUE if the latency could be queried and reconfigured.
519 	 */
520 	public bool recalculateLatency()
521 	{
522 		return gst_bin_recalculate_latency(gstBin) != 0;
523 	}
524 
525 	/**
526 	 * Removes the element from the bin, unparenting it as well.
527 	 * Unparenting the element means that the element will be dereferenced,
528 	 * so if the bin holds the only reference to the element, the element
529 	 * will be freed in the process of removing it from the bin.  If you
530 	 * want the element to still exist after removing, you need to call
531 	 * gst_object_ref() before removing it from the bin.
532 	 *
533 	 * If the element's pads are linked to other pads, the pads will be unlinked
534 	 * before the element is removed from the bin.
535 	 *
536 	 * MT safe.
537 	 *
538 	 * Params:
539 	 *     element = the #GstElement to remove
540 	 *
541 	 * Return: %TRUE if the element could be removed, %FALSE if
542 	 *     the bin does not want to remove the element.
543 	 */
544 	public bool remove(Element element)
545 	{
546 		return gst_bin_remove(gstBin, (element is null) ? null : element.getElementStruct()) != 0;
547 	}
548 
549 	int[string] connectedSignals;
550 
551 	bool delegate(Bin)[] onDoLatencyListeners;
552 	/**
553 	 * Will be emitted when the bin needs to perform latency calculations. This
554 	 * signal is only emitted for toplevel bins or when async-handling is
555 	 * enabled.
556 	 *
557 	 * Only one signal handler is invoked. If no signals are connected, the
558 	 * default handler is invoked, which will query and distribute the lowest
559 	 * possible latency to all sinks.
560 	 *
561 	 * Connect to this signal if the default latency calculations are not
562 	 * sufficient, like when you need different latencies for different sinks in
563 	 * the same pipeline.
564 	 */
565 	void addOnDoLatency(bool delegate(Bin) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
566 	{
567 		if ( "do-latency" !in connectedSignals )
568 		{
569 			Signals.connectData(
570 				this,
571 				"do-latency",
572 				cast(GCallback)&callBackDoLatency,
573 				cast(void*)this,
574 				null,
575 				connectFlags);
576 			connectedSignals["do-latency"] = 1;
577 		}
578 		onDoLatencyListeners ~= dlg;
579 	}
580 	extern(C) static int callBackDoLatency(GstBin* binStruct, Bin _bin)
581 	{
582 		foreach ( bool delegate(Bin) dlg; _bin.onDoLatencyListeners )
583 		{
584 			if ( dlg(_bin) )
585 			{
586 				return 1;
587 			}
588 		}
589 		
590 		return 0;
591 	}
592 
593 	void delegate(Element, Bin)[] onElementAddedListeners;
594 	/**
595 	 * Will be emitted after the element was added to the bin.
596 	 *
597 	 * Params:
598 	 *     element = the #GstElement that was added to the bin
599 	 */
600 	void addOnElementAdded(void delegate(Element, Bin) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
601 	{
602 		if ( "element-added" !in connectedSignals )
603 		{
604 			Signals.connectData(
605 				this,
606 				"element-added",
607 				cast(GCallback)&callBackElementAdded,
608 				cast(void*)this,
609 				null,
610 				connectFlags);
611 			connectedSignals["element-added"] = 1;
612 		}
613 		onElementAddedListeners ~= dlg;
614 	}
615 	extern(C) static void callBackElementAdded(GstBin* binStruct, GstElement* element, Bin _bin)
616 	{
617 		foreach ( void delegate(Element, Bin) dlg; _bin.onElementAddedListeners )
618 		{
619 			dlg(ObjectG.getDObject!(Element)(element), _bin);
620 		}
621 	}
622 
623 	void delegate(Element, Bin)[] onElementRemovedListeners;
624 	/**
625 	 * Will be emitted after the element was removed from the bin.
626 	 *
627 	 * Params:
628 	 *     element = the #GstElement that was removed from the bin
629 	 */
630 	void addOnElementRemoved(void delegate(Element, Bin) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
631 	{
632 		if ( "element-removed" !in connectedSignals )
633 		{
634 			Signals.connectData(
635 				this,
636 				"element-removed",
637 				cast(GCallback)&callBackElementRemoved,
638 				cast(void*)this,
639 				null,
640 				connectFlags);
641 			connectedSignals["element-removed"] = 1;
642 		}
643 		onElementRemovedListeners ~= dlg;
644 	}
645 	extern(C) static void callBackElementRemoved(GstBin* binStruct, GstElement* element, Bin _bin)
646 	{
647 		foreach ( void delegate(Element, Bin) dlg; _bin.onElementRemovedListeners )
648 		{
649 			dlg(ObjectG.getDObject!(Element)(element), _bin);
650 		}
651 	}
652 }