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  * Conversion parameters:
26  * inFile  = GstBus.html
27  * outPack = gstreamer
28  * outFile = Bus
29  * strct   = GstBus
30  * realStrct=
31  * ctorStrct=
32  * clss    = Bus
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- gst_bus_
41  * 	- gst_
42  * omit structs:
43  * omit prefixes:
44  * omit code:
45  * 	- gst_bus_add_watch
46  * 	- gst_bus_set_sync_handler
47  * omit signals:
48  * imports:
49  * 	- glib.Str
50  * 	- gstreamer.Message
51  * 	- glib.Source
52  * structWrap:
53  * 	- GSource* -> Source
54  * 	- GstBus* -> Bus
55  * 	- GstMessage* -> Message
56  * module aliases:
57  * local aliases:
58  * overrides:
59  */
60 
61 module gstreamer.Bus;
62 
63 public  import gstreamerc.gstreamertypes;
64 
65 private import gstreamerc.gstreamer;
66 private import glib.ConstructionException;
67 private import gobject.ObjectG;
68 
69 private import gobject.Signals;
70 public  import gtkc.gdktypes;
71 
72 private import glib.Str;
73 private import gstreamer.Message;
74 private import glib.Source;
75 
76 
77 
78 private import gstreamer.ObjectGst;
79 
80 /**
81  * Description
82  * The GstBus is an object responsible for delivering GstMessages in
83  * a first-in first-out way from the streaming threads to the application.
84  * Since the application typically only wants to deal with delivery of these
85  * messages from one thread, the GstBus will marshall the messages between
86  * different threads. This is important since the actual streaming of media
87  * is done in another thread than the application.
88  * The GstBus provides support for GSource based notifications. This makes it
89  * possible to handle the delivery in the glib mainloop.
90  * The GSource callback function gst_bus_async_signal_func() can be used to
91  * convert all bus messages into signal emissions.
92  * A message is posted on the bus with the gst_bus_post() method. With the
93  * gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a
94  * previously posted message.
95  * The bus can be polled with the gst_bus_poll() method. This methods blocks
96  * up to the specified timeout value until one of the specified messages types
97  * is posted on the bus. The application can then _pop() the messages from the
98  * bus to handle them.
99  * Alternatively the application can register an asynchronous bus function
100  * using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will
101  * install a GSource in the default glib main loop and will deliver messages
102  * a short while after they have been posted. Note that the main loop should
103  * be running for the asynchronous callbacks.
104  * It is also possible to get messages from the bus without any thread
105  * marshalling with the gst_bus_set_sync_handler() method. This makes it
106  * possible to react to a message in the same thread that posted the
107  * message on the bus. This should only be used if the application is able
108  * to deal with messages from different threads.
109  * Every GstPipeline has one bus.
110  * Note that a GstPipeline will set its bus into flushing state when changing
111  * from READY to NULL state.
112  * Last reviewed on 2006-03-12 (0.10.5)
113  */
114 public class Bus : ObjectGst
115 {
116 	
117 	/** the main Gtk struct */
118 	protected GstBus* gstBus;
119 	
120 	
121 	public GstBus* getBusStruct()
122 	{
123 		return gstBus;
124 	}
125 	
126 	
127 	/** the main Gtk struct as a void* */
128 	protected override void* getStruct()
129 	{
130 		return cast(void*)gstBus;
131 	}
132 	
133 	/**
134 	 * Sets our main struct and passes it to the parent class
135 	 */
136 	public this (GstBus* gstBus)
137 	{
138 		super(cast(GstObject*)gstBus);
139 		this.gstBus = gstBus;
140 	}
141 	
142 	protected override void setStruct(GObject* obj)
143 	{
144 		super.setStruct(obj);
145 		gstBus = cast(GstBus*)obj;
146 	}
147 	
148 	/**
149 	 * Adds a bus watch to the default main context with the default priority.
150 	 * This function is used to receive asynchronous messages in the main loop.
151 	 * The watch can be removed using g_source_remove() or by returning FALSE
152 	 * from func.
153 	 * MT safe.
154 	 * Params:
155 	 *  dlg = A function to call when a message is received.
156 	 * Returns:
157 	 *  The event source id.
158 	 */
159 	public uint addWatch( bool delegate(Message) dlg )
160 	{
161 		onWatchListener = dlg;
162 		return gst_bus_add_watch(gstBus, cast(GstBusFunc)&watchCallBack, cast(void*)this);
163 	}
164 	
165 	bool delegate(Message) onWatchListener;
166 	
167 	extern(C) static gboolean watchCallBack(GstBus* bus, GstMessage* msg, Bus bus_d )//gpointer data)
168 	{
169 		Message msg_d = new Message( msg );
170 		
171 		return bus_d.onWatchListener( msg_d );
172 	}
173 	
174 	/**
175 	 * Use this for making an XOverlay.
176 	 * Sets the synchronous handler on the bus. The function will be called
177 	 * every time a new message is posted on the bus. Note that the function
178 	 * will be called in the same thread context as the posting object. This
179 	 * function is usually only called by the creator of the bus. Applications
180 	 * should handle messages asynchronously using the gst_bus watch and poll
181 	 * functions.
182 	 * You cannot replace an existing sync_handler. You can pass NULL to this
183 	 * function, which will clear the existing handler.
184 	 * Params:
185 	 *  dlg = The handler function to install
186 	 */
187 	public void setSyncHandler( GstBusSyncReply delegate(Message) dlg )
188 	{
189 		onSyncHandlerListener = dlg;
190 		gst_bus_set_sync_handler(gstBus, cast(GstBusSyncHandler)&syncHandlerCallBack, cast(void*)this);
191 	}
192 	
193 	GstBusSyncReply delegate(Message) onSyncHandlerListener;
194 	
195 	extern(C) static GstBusSyncReply syncHandlerCallBack(GstBus * bus, GstMessage * msg, Bus bus_d )//GstPipeline * pipeline)
196 	//extern(C) static gboolean watchCallBack(GstBus* bus, GstMessage* msg, Bus bus_d )//gpointer data)
197 	{
198 		Message msg_d = new Message( msg );
199 		
200 		return bus_d.onSyncHandlerListener( msg_d );
201 	}
202 	
203 	/**
204 	 */
205 	int[string] connectedSignals;
206 	
207 	void delegate(Message, Bus)[] onMessageListeners;
208 	/**
209 	 * A message has been posted on the bus. This signal is emitted from a
210 	 * GSource added to the mainloop. this signal will only be emitted when
211 	 * there is a mainloop running.
212 	 */
213 	void addOnMessage(void delegate(Message, Bus) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
214 	{
215 		if ( !("message" in connectedSignals) )
216 		{
217 			Signals.connectData(
218 			getStruct(),
219 			"message",
220 			cast(GCallback)&callBackMessage,
221 			cast(void*)this,
222 			null,
223 			connectFlags);
224 			connectedSignals["message"] = 1;
225 		}
226 		onMessageListeners ~= dlg;
227 	}
228 	extern(C) static void callBackMessage(GstBus* busStruct, GstMessage* message, Bus _bus)
229 	{
230 		foreach ( void delegate(Message, Bus) dlg ; _bus.onMessageListeners )
231 		{
232 			dlg(ObjectG.getDObject!(Message)(message), _bus);
233 		}
234 	}
235 	
236 	void delegate(Message, Bus)[] onSyncMessageListeners;
237 	/**
238 	 * A message has been posted on the bus. This signal is emitted from the
239 	 * thread that posted the message so one has to be careful with locking.
240 	 * This signal will not be emitted by default, you have to set up
241 	 * gst_bus_sync_signal_handler() as a sync handler if you want this
242 	 * gst_bus_set_sync_handler (bus, gst_bus_sync_signal_handler, yourdata);
243 	 * See Also
244 	 * GstMessage, GstElement
245 	 */
246 	void addOnSyncMessage(void delegate(Message, Bus) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
247 	{
248 		if ( !("sync-message" in connectedSignals) )
249 		{
250 			Signals.connectData(
251 			getStruct(),
252 			"sync-message",
253 			cast(GCallback)&callBackSyncMessage,
254 			cast(void*)this,
255 			null,
256 			connectFlags);
257 			connectedSignals["sync-message"] = 1;
258 		}
259 		onSyncMessageListeners ~= dlg;
260 	}
261 	extern(C) static void callBackSyncMessage(GstBus* busStruct, GstMessage* message, Bus _bus)
262 	{
263 		foreach ( void delegate(Message, Bus) dlg ; _bus.onSyncMessageListeners )
264 		{
265 			dlg(ObjectG.getDObject!(Message)(message), _bus);
266 		}
267 	}
268 	
269 	
270 	/**
271 	 * Creates a new GstBus instance.
272 	 * Throws: ConstructionException GTK+ fails to create the object.
273 	 */
274 	public this ()
275 	{
276 		// GstBus* gst_bus_new (void);
277 		auto p = gst_bus_new();
278 		if(p is null)
279 		{
280 			throw new ConstructionException("null returned by gst_bus_new()");
281 		}
282 		this(cast(GstBus*) p);
283 	}
284 	
285 	/**
286 	 * Post a message on the given bus. Ownership of the message
287 	 * is taken by the bus.
288 	 * Params:
289 	 * message = The GstMessage to post
290 	 * Returns: TRUE if the message could be posted, FALSE if the bus is flushing. MT safe.
291 	 */
292 	public int post(Message message)
293 	{
294 		// gboolean gst_bus_post (GstBus *bus,  GstMessage *message);
295 		return gst_bus_post(gstBus, (message is null) ? null : message.getMessageStruct());
296 	}
297 	
298 	/**
299 	 * Check if there are pending messages on the bus that
300 	 * should be handled.
301 	 * Returns: TRUE if there are messages on the bus to be handled, FALSE otherwise. MT safe.
302 	 */
303 	public int havePending()
304 	{
305 		// gboolean gst_bus_have_pending (GstBus *bus);
306 		return gst_bus_have_pending(gstBus);
307 	}
308 	
309 	/**
310 	 * Peek the message on the top of the bus' queue. The message will remain
311 	 * on the bus' message queue. A reference is returned, and needs to be unreffed
312 	 * by the caller.
313 	 * Returns: The GstMessage that is on the bus, or NULL if the bus is empty. MT safe.
314 	 */
315 	public Message peek()
316 	{
317 		// GstMessage* gst_bus_peek (GstBus *bus);
318 		auto p = gst_bus_peek(gstBus);
319 		
320 		if(p is null)
321 		{
322 			return null;
323 		}
324 		
325 		return ObjectG.getDObject!(Message)(cast(GstMessage*) p);
326 	}
327 	
328 	/**
329 	 * Get a message from the bus.
330 	 * Returns: The GstMessage that is on the bus, or NULL if the bus is empty. The message is taken from the bus and needs to be unreffed with gst_message_unref() after usage. MT safe.
331 	 */
332 	public Message pop()
333 	{
334 		// GstMessage* gst_bus_pop (GstBus *bus);
335 		auto p = gst_bus_pop(gstBus);
336 		
337 		if(p is null)
338 		{
339 			return null;
340 		}
341 		
342 		return ObjectG.getDObject!(Message)(cast(GstMessage*) p);
343 	}
344 	
345 	/**
346 	 * Get a message from the bus, waiting up to the specified timeout.
347 	 * If timeout is 0, this function behaves like gst_bus_pop(). If timeout is
348 	 * GST_CLOCK_TIME_NONE, this function will block forever until a message was
349 	 * posted on the bus.
350 	 * Params:
351 	 * timeout = a timeout
352 	 * Returns: The GstMessage that is on the bus after the specified timeout or NULL if the bus is empty after the timeout expired. The message is taken from the bus and needs to be unreffed with gst_message_unref() after usage. MT safe. Since 0.10.12
353 	 */
354 	public Message timedPop(GstClockTime timeout)
355 	{
356 		// GstMessage* gst_bus_timed_pop (GstBus *bus,  GstClockTime timeout);
357 		auto p = gst_bus_timed_pop(gstBus, timeout);
358 		
359 		if(p is null)
360 		{
361 			return null;
362 		}
363 		
364 		return ObjectG.getDObject!(Message)(cast(GstMessage*) p);
365 	}
366 	
367 	/**
368 	 * If flushing, flush out and unref any messages queued in the bus. Releases
369 	 * references to the message origin objects. Will flush future messages until
370 	 * gst_bus_set_flushing() sets flushing to FALSE.
371 	 * MT safe.
372 	 * Params:
373 	 * flushing = whether or not to flush the bus
374 	 */
375 	public void setFlushing(int flushing)
376 	{
377 		// void gst_bus_set_flushing (GstBus *bus,  gboolean flushing);
378 		gst_bus_set_flushing(gstBus, flushing);
379 	}
380 	
381 	/**
382 	 * A helper GstBusSyncHandler that can be used to convert all synchronous
383 	 * messages into signals.
384 	 * Params:
385 	 * message = the GstMessage received
386 	 * data = user data
387 	 * Returns: GST_BUS_PASS
388 	 */
389 	public GstBusSyncReply syncSignalHandler(Message message, void* data)
390 	{
391 		// GstBusSyncReply gst_bus_sync_signal_handler (GstBus *bus,  GstMessage *message,  gpointer data);
392 		return gst_bus_sync_signal_handler(gstBus, (message is null) ? null : message.getMessageStruct(), data);
393 	}
394 	
395 	/**
396 	 * Create watch for this bus. The GSource will be dispatched whenever
397 	 * a message is on the bus. After the GSource is dispatched, the
398 	 * message is popped off the bus and unreffed.
399 	 * Returns: A GSource that can be added to a mainloop.
400 	 */
401 	public Source createWatch()
402 	{
403 		// GSource* gst_bus_create_watch (GstBus *bus);
404 		auto p = gst_bus_create_watch(gstBus);
405 		
406 		if(p is null)
407 		{
408 			return null;
409 		}
410 		
411 		return ObjectG.getDObject!(Source)(cast(GSource*) p);
412 	}
413 	
414 	/**
415 	 * Adds a bus watch to the default main context with the given priority.
416 	 * This function is used to receive asynchronous messages in the main loop.
417 	 * When func is called, the message belongs to the caller; if you want to
418 	 * keep a copy of it, call gst_message_ref() before leaving func.
419 	 * The watch can be removed using g_source_remove() or by returning FALSE
420 	 * from func.
421 	 * Params:
422 	 * priority = The priority of the watch.
423 	 * func = A function to call when a message is received.
424 	 * userData = user data passed to func.
425 	 * notify = the function to call when the source is removed.
426 	 * Returns: The event source id. MT safe.
427 	 */
428 	public uint addWatchFull(int priority, GstBusFunc func, void* userData, GDestroyNotify notify)
429 	{
430 		// guint gst_bus_add_watch_full (GstBus *bus,  gint priority,  GstBusFunc func,  gpointer user_data,  GDestroyNotify notify);
431 		return gst_bus_add_watch_full(gstBus, priority, func, userData, notify);
432 	}
433 	
434 	/**
435 	 * Instructs GStreamer to stop emitting the "sync-message" signal for this bus.
436 	 * See gst_bus_enable_sync_message_emission() for more information.
437 	 * In the event that multiple pieces of code have called
438 	 * gst_bus_enable_sync_message_emission(), the sync-message emissions will only
439 	 * be stopped after all calls to gst_bus_enable_sync_message_emission() were
440 	 * "cancelled" by calling this function. In this way the semantics are exactly
441 	 * the same as gst_object_ref() that which calls enable should also call
442 	 * disable.
443 	 * MT safe.
444 	 */
445 	public void disableSyncMessageEmission()
446 	{
447 		// void gst_bus_disable_sync_message_emission  (GstBus *bus);
448 		gst_bus_disable_sync_message_emission(gstBus);
449 	}
450 	
451 	/**
452 	 * Instructs GStreamer to emit the "sync-message" signal after running the bus's
453 	 * sync handler. This function is here so that code can ensure that they can
454 	 * synchronously receive messages without having to affect what the bin's sync
455 	 * handler is.
456 	 * This function may be called multiple times. To clean up, the caller is
457 	 * responsible for calling gst_bus_disable_sync_message_emission() as many times
458 	 * as this function is called.
459 	 * While this function looks similar to gst_bus_add_signal_watch(), it is not
460 	 * exactly the same -- this function enables synchronous emission of
461 	 * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback
462 	 * to pop messages off the bus asynchronously. The sync-message signal
463 	 * comes from the thread of whatever object posted the message; the "message"
464 	 * signal is marshalled to the main thread via the main loop.
465 	 * MT safe.
466 	 */
467 	public void enableSyncMessageEmission()
468 	{
469 		// void gst_bus_enable_sync_message_emission  (GstBus *bus);
470 		gst_bus_enable_sync_message_emission(gstBus);
471 	}
472 	
473 	/**
474 	 * A helper GstBusFunc that can be used to convert all asynchronous messages
475 	 * into signals.
476 	 * Params:
477 	 * message = the GstMessage received
478 	 * data = user data
479 	 * Returns: TRUE
480 	 */
481 	public int asyncSignalFunc(Message message, void* data)
482 	{
483 		// gboolean gst_bus_async_signal_func (GstBus *bus,  GstMessage *message,  gpointer data);
484 		return gst_bus_async_signal_func(gstBus, (message is null) ? null : message.getMessageStruct(), data);
485 	}
486 	
487 	/**
488 	 * Adds a bus signal watch to the default main context with the default
489 	 * priority.
490 	 * After calling this statement, the bus will emit the "message" signal for each
491 	 * message posted on the bus.
492 	 * This function may be called multiple times. To clean up, the caller is
493 	 * responsible for calling gst_bus_remove_signal_watch() as many times as this
494 	 * function is called.
495 	 * MT safe.
496 	 */
497 	public void addSignalWatch()
498 	{
499 		// void gst_bus_add_signal_watch (GstBus *bus);
500 		gst_bus_add_signal_watch(gstBus);
501 	}
502 	
503 	/**
504 	 * Adds a bus signal watch to the default main context with the given priority.
505 	 * After calling this statement, the bus will emit the "message" signal for each
506 	 * message posted on the bus when the main loop is running.
507 	 * This function may be called multiple times. To clean up, the caller is
508 	 * responsible for calling gst_bus_remove_signal_watch() as many times as this
509 	 * function is called.
510 	 * MT safe.
511 	 * Params:
512 	 * priority = The priority of the watch.
513 	 */
514 	public void addSignalWatchFull(int priority)
515 	{
516 		// void gst_bus_add_signal_watch_full (GstBus *bus,  gint priority);
517 		gst_bus_add_signal_watch_full(gstBus, priority);
518 	}
519 	
520 	/**
521 	 * Removes a signal watch previously added with gst_bus_add_signal_watch().
522 	 * MT safe.
523 	 */
524 	public void removeSignalWatch()
525 	{
526 		// void gst_bus_remove_signal_watch (GstBus *bus);
527 		gst_bus_remove_signal_watch(gstBus);
528 	}
529 	
530 	/**
531 	 * Poll the bus for messages. Will block while waiting for messages to come.
532 	 * You can specify a maximum time to poll with the timeout parameter. If
533 	 * timeout is negative, this function will block indefinitely.
534 	 * All messages not in events will be popped off the bus and will be ignored.
535 	 * Because poll is implemented using the "message" signal enabled by
536 	 * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message"
537 	 * signal to be emitted for every message that poll sees. Thus a "message"
538 	 * signal handler will see the same messages that this function sees -- neither
539 	 * will steal messages from the other.
540 	 * This function will run a main loop from the default main context when
541 	 * polling.
542 	 * Params:
543 	 * events = a mask of GstMessageType, representing the set of message types to
544 	 * poll for.
545 	 * timeout = the poll timeout, as a GstClockTimeDiff, or -1 to poll indefinitely.
546 	 * Returns: The message that was received, or NULL if the poll timed out. The message is taken from the bus and needs to be unreffed with gst_message_unref() after usage. Signal Details The "message" signal void user_function (GstBus *bus, GstMessage *message, gpointer user_data) : Run Last / Has Details A message has been posted on the bus. This signal is emitted from a GSource added to the mainloop. this signal will only be emitted when there is a mainloop running.
547 	 */
548 	public Message poll(GstMessageType events, GstClockTimeDiff timeout)
549 	{
550 		// GstMessage* gst_bus_poll (GstBus *bus,  GstMessageType events,  GstClockTimeDiff timeout);
551 		auto p = gst_bus_poll(gstBus, events, timeout);
552 		
553 		if(p is null)
554 		{
555 			return null;
556 		}
557 		
558 		return ObjectG.getDObject!(Message)(cast(GstMessage*) p);
559 	}
560 }