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.Bus; 26 27 private import glib.ConstructionException; 28 private import glib.Source; 29 private import gobject.ObjectG; 30 private import gobject.Signals; 31 private import gstreamer.Message; 32 private import gstreamer.ObjectGst; 33 private import gstreamerc.gstreamer; 34 public import gstreamerc.gstreamertypes; 35 public import gtkc.gdktypes; 36 37 38 /** 39 * The #GstBus is an object responsible for delivering #GstMessage packets in 40 * a first-in first-out way from the streaming threads (see #GstTask) to the 41 * application. 42 * 43 * Since the application typically only wants to deal with delivery of these 44 * messages from one thread, the GstBus will marshall the messages between 45 * different threads. This is important since the actual streaming of media 46 * is done in another thread than the application. 47 * 48 * The GstBus provides support for #GSource based notifications. This makes it 49 * possible to handle the delivery in the glib mainloop. 50 * 51 * The #GSource callback function gst_bus_async_signal_func() can be used to 52 * convert all bus messages into signal emissions. 53 * 54 * A message is posted on the bus with the gst_bus_post() method. With the 55 * gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a 56 * previously posted message. 57 * 58 * The bus can be polled with the gst_bus_poll() method. This methods blocks 59 * up to the specified timeout value until one of the specified messages types 60 * is posted on the bus. The application can then gst_bus_pop() the messages 61 * from the bus to handle them. 62 * Alternatively the application can register an asynchronous bus function 63 * using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will 64 * install a #GSource in the default glib main loop and will deliver messages 65 * a short while after they have been posted. Note that the main loop should 66 * be running for the asynchronous callbacks. 67 * 68 * It is also possible to get messages from the bus without any thread 69 * marshalling with the gst_bus_set_sync_handler() method. This makes it 70 * possible to react to a message in the same thread that posted the 71 * message on the bus. This should only be used if the application is able 72 * to deal with messages from different threads. 73 * 74 * Every #GstPipeline has one bus. 75 * 76 * Note that a #GstPipeline will set its bus into flushing state when changing 77 * from READY to NULL state. 78 */ 79 public class Bus : ObjectGst 80 { 81 /** the main Gtk struct */ 82 protected GstBus* gstBus; 83 84 /** Get the main Gtk struct */ 85 public GstBus* getBusStruct() 86 { 87 return gstBus; 88 } 89 90 /** the main Gtk struct as a void* */ 91 protected override void* getStruct() 92 { 93 return cast(void*)gstBus; 94 } 95 96 protected override void setStruct(GObject* obj) 97 { 98 gstBus = cast(GstBus*)obj; 99 super.setStruct(obj); 100 } 101 102 /** 103 * Sets our main struct and passes it to the parent class. 104 */ 105 public this (GstBus* gstBus, bool ownedRef = false) 106 { 107 this.gstBus = gstBus; 108 super(cast(GstObject*)gstBus, ownedRef); 109 } 110 111 /** 112 * Adds a bus watch to the default main context with the default priority. 113 * This function is used to receive asynchronous messages in the main loop. 114 * The watch can be removed using g_source_remove() or by returning FALSE 115 * from func. 116 * MT safe. 117 * Params: 118 * dlg = A function to call when a message is received. 119 * Returns: 120 * The event source id. 121 */ 122 public uint addWatch( bool delegate(Message) dlg ) 123 { 124 onWatchListener = dlg; 125 return gst_bus_add_watch(gstBus, cast(GstBusFunc)&watchCallBack, cast(void*)this); 126 } 127 128 bool delegate(Message) onWatchListener; 129 130 extern(C) static int watchCallBack(GstBus* bus, GstMessage* msg, Bus bus_d )//gpointer data) 131 { 132 Message msg_d = new Message( msg ); 133 134 return bus_d.onWatchListener( msg_d ); 135 } 136 137 /** 138 * Use this for making an XOverlay. 139 * Sets the synchronous handler on the bus. The function will be called 140 * every time a new message is posted on the bus. Note that the function 141 * will be called in the same thread context as the posting object. This 142 * function is usually only called by the creator of the bus. Applications 143 * should handle messages asynchronously using the gst_bus watch and poll 144 * functions. 145 * You cannot replace an existing sync_handler. You can pass NULL to this 146 * function, which will clear the existing handler. 147 * Params: 148 * dlg = The handler function to install 149 */ 150 public void setSyncHandler( GstBusSyncReply delegate(Message) dlg ) 151 { 152 onSyncHandlerListener = dlg; 153 gst_bus_set_sync_handler(gstBus, cast(GstBusSyncHandler)&syncHandlerCallBack, cast(void*)this, null); 154 } 155 156 GstBusSyncReply delegate(Message) onSyncHandlerListener; 157 158 extern(C) static GstBusSyncReply syncHandlerCallBack(GstBus* bus, GstMessage* msg, Bus bus_d) 159 { 160 Message msg_d = new Message( msg ); 161 162 return bus_d.onSyncHandlerListener( msg_d ); 163 } 164 165 /** 166 */ 167 168 public static GType getType() 169 { 170 return gst_bus_get_type(); 171 } 172 173 /** 174 * Creates a new #GstBus instance. 175 * 176 * Return: a new #GstBus instance 177 * 178 * Throws: ConstructionException GTK+ fails to create the object. 179 */ 180 public this() 181 { 182 auto p = gst_bus_new(); 183 184 if(p is null) 185 { 186 throw new ConstructionException("null returned by new"); 187 } 188 189 this(cast(GstBus*) p, true); 190 } 191 192 /** 193 * Adds a bus signal watch to the default main context with the default priority 194 * (%G_PRIORITY_DEFAULT). It is also possible to use a non-default 195 * main context set up using g_main_context_push_thread_default() (before 196 * one had to create a bus watch source and attach it to the desired main 197 * context 'manually'). 198 * 199 * After calling this statement, the bus will emit the "message" signal for each 200 * message posted on the bus. 201 * 202 * This function may be called multiple times. To clean up, the caller is 203 * responsible for calling gst_bus_remove_signal_watch() as many times as this 204 * function is called. 205 * 206 * MT safe. 207 */ 208 public void addSignalWatch() 209 { 210 gst_bus_add_signal_watch(gstBus); 211 } 212 213 /** 214 * Adds a bus signal watch to the default main context with the given @priority 215 * (e.g. %G_PRIORITY_DEFAULT). It is also possible to use a non-default main 216 * context set up using g_main_context_push_thread_default() 217 * (before one had to create a bus watch source and attach it to the desired 218 * main context 'manually'). 219 * 220 * After calling this statement, the bus will emit the "message" signal for each 221 * message posted on the bus when the main loop is running. 222 * 223 * This function may be called multiple times. To clean up, the caller is 224 * responsible for calling gst_bus_remove_signal_watch() as many times as this 225 * function is called. 226 * 227 * There can only be a single bus watch per bus, you must remove any signal 228 * watch before you can set another type of watch. 229 * 230 * MT safe. 231 * 232 * Params: 233 * priority = The priority of the watch. 234 */ 235 public void addSignalWatchFull(int priority) 236 { 237 gst_bus_add_signal_watch_full(gstBus, priority); 238 } 239 240 /** 241 * Adds a bus watch to the default main context with the given @priority (e.g. 242 * %G_PRIORITY_DEFAULT). It is also possible to use a non-default main 243 * context set up using g_main_context_push_thread_default() (before 244 * one had to create a bus watch source and attach it to the desired main 245 * context 'manually'). 246 * 247 * This function is used to receive asynchronous messages in the main loop. 248 * There can only be a single bus watch per bus, you must remove it before you 249 * can set a new one. 250 * 251 * The bus watch will only work if a GLib main loop is being run. 252 * 253 * When @func is called, the message belongs to the caller; if you want to 254 * keep a copy of it, call gst_message_ref() before leaving @func. 255 * 256 * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE 257 * from @func. If the watch was added to the default main context it is also 258 * possible to remove the watch using g_source_remove(). 259 * 260 * MT safe. 261 * 262 * Params: 263 * priority = The priority of the watch. 264 * func = A function to call when a message is received. 265 * userData = user data passed to @func. 266 * notify = the function to call when the source is removed. 267 * 268 * Return: The event source id or 0 if @bus already got an event source. 269 */ 270 public uint addWatchFull(int priority, GstBusFunc func, void* userData, GDestroyNotify notify) 271 { 272 return gst_bus_add_watch_full(gstBus, priority, func, userData, notify); 273 } 274 275 /** 276 * A helper #GstBusFunc that can be used to convert all asynchronous messages 277 * into signals. 278 * 279 * Params: 280 * message = the #GstMessage received 281 * data = user data 282 * 283 * Return: %TRUE 284 */ 285 public bool asyncSignalFunc(Message message, void* data) 286 { 287 return gst_bus_async_signal_func(gstBus, (message is null) ? null : message.getMessageStruct(), data) != 0; 288 } 289 290 /** 291 * Create watch for this bus. The GSource will be dispatched whenever 292 * a message is on the bus. After the GSource is dispatched, the 293 * message is popped off the bus and unreffed. 294 * 295 * Return: a #GSource that can be added to a mainloop. 296 */ 297 public Source createWatch() 298 { 299 auto p = gst_bus_create_watch(gstBus); 300 301 if(p is null) 302 { 303 return null; 304 } 305 306 return new Source(cast(GSource*) p); 307 } 308 309 /** 310 * Instructs GStreamer to stop emitting the "sync-message" signal for this bus. 311 * See gst_bus_enable_sync_message_emission() for more information. 312 * 313 * In the event that multiple pieces of code have called 314 * gst_bus_enable_sync_message_emission(), the sync-message emissions will only 315 * be stopped after all calls to gst_bus_enable_sync_message_emission() were 316 * "cancelled" by calling this function. In this way the semantics are exactly 317 * the same as gst_object_ref() that which calls enable should also call 318 * disable. 319 * 320 * MT safe. 321 */ 322 public void disableSyncMessageEmission() 323 { 324 gst_bus_disable_sync_message_emission(gstBus); 325 } 326 327 /** 328 * Instructs GStreamer to emit the "sync-message" signal after running the bus's 329 * sync handler. This function is here so that code can ensure that they can 330 * synchronously receive messages without having to affect what the bin's sync 331 * handler is. 332 * 333 * This function may be called multiple times. To clean up, the caller is 334 * responsible for calling gst_bus_disable_sync_message_emission() as many times 335 * as this function is called. 336 * 337 * While this function looks similar to gst_bus_add_signal_watch(), it is not 338 * exactly the same -- this function enables <emphasis>synchronous</emphasis> emission of 339 * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback 340 * to pop messages off the bus <emphasis>asynchronously</emphasis>. The sync-message signal 341 * comes from the thread of whatever object posted the message; the "message" 342 * signal is marshalled to the main thread via the main loop. 343 * 344 * MT safe. 345 */ 346 public void enableSyncMessageEmission() 347 { 348 gst_bus_enable_sync_message_emission(gstBus); 349 } 350 351 /** 352 * Check if there are pending messages on the bus that 353 * should be handled. 354 * 355 * Return: %TRUE if there are messages on the bus to be handled, %FALSE 356 * otherwise. 357 * 358 * MT safe. 359 */ 360 public bool havePending() 361 { 362 return gst_bus_have_pending(gstBus) != 0; 363 } 364 365 /** 366 * Peek the message on the top of the bus' queue. The message will remain 367 * on the bus' message queue. A reference is returned, and needs to be unreffed 368 * by the caller. 369 * 370 * Return: the #GstMessage that is on the 371 * bus, or %NULL if the bus is empty. 372 * 373 * MT safe. 374 */ 375 public Message peek() 376 { 377 auto p = gst_bus_peek(gstBus); 378 379 if(p is null) 380 { 381 return null; 382 } 383 384 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 385 } 386 387 /** 388 * Poll the bus for messages. Will block while waiting for messages to come. 389 * You can specify a maximum time to poll with the @timeout parameter. If 390 * @timeout is negative, this function will block indefinitely. 391 * 392 * All messages not in @events will be popped off the bus and will be ignored. 393 * It is not possible to use message enums beyond #GST_MESSAGE_EXTENDED in the 394 * @events mask 395 * 396 * Because poll is implemented using the "message" signal enabled by 397 * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message" 398 * signal to be emitted for every message that poll sees. Thus a "message" 399 * signal handler will see the same messages that this function sees -- neither 400 * will steal messages from the other. 401 * 402 * This function will run a main loop from the default main context when 403 * polling. 404 * 405 * You should never use this function, since it is pure evil. This is 406 * especially true for GUI applications based on Gtk+ or Qt, but also for any 407 * other non-trivial application that uses the GLib main loop. As this function 408 * runs a GLib main loop, any callback attached to the default GLib main 409 * context may be invoked. This could be timeouts, GUI events, I/O events etc.; 410 * even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks 411 * may do things you do not expect, e.g. destroy the main application window or 412 * some other resource; change other application state; display a dialog and 413 * run another main loop until the user clicks it away. In short, using this 414 * function may add a lot of complexity to your code through unexpected 415 * re-entrancy and unexpected changes to your application's state. 416 * 417 * For 0 timeouts use gst_bus_pop_filtered() instead of this function; for 418 * other short timeouts use gst_bus_timed_pop_filtered(); everything else is 419 * better handled by setting up an asynchronous bus watch and doing things 420 * from there. 421 * 422 * Params: 423 * events = a mask of #GstMessageType, representing the set of message types to 424 * poll for (note special handling of extended message types below) 425 * timeout = the poll timeout, as a #GstClockTime, or #GST_CLOCK_TIME_NONE to poll 426 * indefinitely. 427 * 428 * Return: the message that was received, 429 * or %NULL if the poll timed out. The message is taken from the 430 * bus and needs to be unreffed with gst_message_unref() after 431 * usage. 432 */ 433 public Message poll(GstMessageType events, GstClockTime timeout) 434 { 435 auto p = gst_bus_poll(gstBus, events, timeout); 436 437 if(p is null) 438 { 439 return null; 440 } 441 442 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 443 } 444 445 /** 446 * Get a message from the bus. 447 * 448 * Return: the #GstMessage that is on the 449 * bus, or %NULL if the bus is empty. The message is taken from 450 * the bus and needs to be unreffed with gst_message_unref() after 451 * usage. 452 * 453 * MT safe. 454 */ 455 public Message pop() 456 { 457 auto p = gst_bus_pop(gstBus); 458 459 if(p is null) 460 { 461 return null; 462 } 463 464 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 465 } 466 467 /** 468 * Get a message matching @type from the bus. Will discard all messages on 469 * the bus that do not match @type and that have been posted before the first 470 * message that does match @type. If there is no message matching @type on 471 * the bus, all messages will be discarded. It is not possible to use message 472 * enums beyond #GST_MESSAGE_EXTENDED in the @events mask. 473 * 474 * Params: 475 * types = message types to take into account 476 * 477 * Return: the next #GstMessage matching 478 * @type that is on the bus, or %NULL if the bus is empty or there 479 * is no message matching @type. The message is taken from the bus 480 * and needs to be unreffed with gst_message_unref() after usage. 481 * 482 * MT safe. 483 */ 484 public Message popFiltered(GstMessageType types) 485 { 486 auto p = gst_bus_pop_filtered(gstBus, types); 487 488 if(p is null) 489 { 490 return null; 491 } 492 493 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 494 } 495 496 /** 497 * Post a message on the given bus. Ownership of the message 498 * is taken by the bus. 499 * 500 * Params: 501 * message = the #GstMessage to post 502 * 503 * Return: %TRUE if the message could be posted, %FALSE if the bus is flushing. 504 * 505 * MT safe. 506 */ 507 public bool post(Message message) 508 { 509 return gst_bus_post(gstBus, (message is null) ? null : message.getMessageStruct()) != 0; 510 } 511 512 /** 513 * Removes a signal watch previously added with gst_bus_add_signal_watch(). 514 * 515 * MT safe. 516 */ 517 public void removeSignalWatch() 518 { 519 gst_bus_remove_signal_watch(gstBus); 520 } 521 522 /** 523 * Removes an installed bus watch from @bus. 524 * 525 * Return: %TRUE on success or %FALSE if @bus has no event source. 526 * 527 * Since: 1.6 528 */ 529 public bool removeWatch() 530 { 531 return gst_bus_remove_watch(gstBus) != 0; 532 } 533 534 /** 535 * If @flushing, flush out and unref any messages queued in the bus. Releases 536 * references to the message origin objects. Will flush future messages until 537 * gst_bus_set_flushing() sets @flushing to %FALSE. 538 * 539 * MT safe. 540 * 541 * Params: 542 * flushing = whether or not to flush the bus 543 */ 544 public void setFlushing(bool flushing) 545 { 546 gst_bus_set_flushing(gstBus, flushing); 547 } 548 549 /** 550 * A helper GstBusSyncHandler that can be used to convert all synchronous 551 * messages into signals. 552 * 553 * Params: 554 * message = the #GstMessage received 555 * data = user data 556 * 557 * Return: GST_BUS_PASS 558 */ 559 public GstBusSyncReply syncSignalHandler(Message message, void* data) 560 { 561 return gst_bus_sync_signal_handler(gstBus, (message is null) ? null : message.getMessageStruct(), data); 562 } 563 564 /** 565 * Get a message from the bus, waiting up to the specified timeout. 566 * 567 * If @timeout is 0, this function behaves like gst_bus_pop(). If @timeout is 568 * #GST_CLOCK_TIME_NONE, this function will block forever until a message was 569 * posted on the bus. 570 * 571 * Params: 572 * timeout = a timeout 573 * 574 * Return: the #GstMessage that is on the 575 * bus after the specified timeout or %NULL if the bus is empty 576 * after the timeout expired. The message is taken from the bus 577 * and needs to be unreffed with gst_message_unref() after usage. 578 * 579 * MT safe. 580 */ 581 public Message timedPop(GstClockTime timeout) 582 { 583 auto p = gst_bus_timed_pop(gstBus, timeout); 584 585 if(p is null) 586 { 587 return null; 588 } 589 590 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 591 } 592 593 /** 594 * Get a message from the bus whose type matches the message type mask @types, 595 * waiting up to the specified timeout (and discarding any messages that do not 596 * match the mask provided). 597 * 598 * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If 599 * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a 600 * matching message was posted on the bus. 601 * 602 * Params: 603 * timeout = a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever 604 * types = message types to take into account, GST_MESSAGE_ANY for any type 605 * 606 * Return: a #GstMessage matching the 607 * filter in @types, or %NULL if no matching message was found on 608 * the bus until the timeout expired. The message is taken from 609 * the bus and needs to be unreffed with gst_message_unref() after 610 * usage. 611 * 612 * MT safe. 613 */ 614 public Message timedPopFiltered(GstClockTime timeout, GstMessageType types) 615 { 616 auto p = gst_bus_timed_pop_filtered(gstBus, timeout, types); 617 618 if(p is null) 619 { 620 return null; 621 } 622 623 return ObjectG.getDObject!(Message)(cast(GstMessage*) p); 624 } 625 626 int[string] connectedSignals; 627 628 void delegate(Message, Bus)[] onMessageListeners; 629 /** 630 * A message has been posted on the bus. This signal is emitted from a 631 * GSource added to the mainloop. this signal will only be emitted when 632 * there is a mainloop running. 633 * 634 * Params: 635 * message = the message that has been posted asynchronously 636 */ 637 void addOnMessage(void delegate(Message, Bus) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 638 { 639 if ( "message" !in connectedSignals ) 640 { 641 Signals.connectData( 642 this, 643 "message", 644 cast(GCallback)&callBackMessage, 645 cast(void*)this, 646 null, 647 connectFlags); 648 connectedSignals["message"] = 1; 649 } 650 onMessageListeners ~= dlg; 651 } 652 extern(C) static void callBackMessage(GstBus* busStruct, GstMessage* message, Bus _bus) 653 { 654 foreach ( void delegate(Message, Bus) dlg; _bus.onMessageListeners ) 655 { 656 dlg(ObjectG.getDObject!(Message)(message), _bus); 657 } 658 } 659 660 void delegate(Message, Bus)[] onSyncMessageListeners; 661 /** 662 * A message has been posted on the bus. This signal is emitted from the 663 * thread that posted the message so one has to be careful with locking. 664 * 665 * This signal will not be emitted by default, you have to call 666 * gst_bus_enable_sync_message_emission() before. 667 * 668 * Params: 669 * message = the message that has been posted synchronously 670 */ 671 void addOnSyncMessage(void delegate(Message, Bus) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 672 { 673 if ( "sync-message" !in connectedSignals ) 674 { 675 Signals.connectData( 676 this, 677 "sync-message", 678 cast(GCallback)&callBackSyncMessage, 679 cast(void*)this, 680 null, 681 connectFlags); 682 connectedSignals["sync-message"] = 1; 683 } 684 onSyncMessageListeners ~= dlg; 685 } 686 extern(C) static void callBackSyncMessage(GstBus* busStruct, GstMessage* message, Bus _bus) 687 { 688 foreach ( void delegate(Message, Bus) dlg; _bus.onSyncMessageListeners ) 689 { 690 dlg(ObjectG.getDObject!(Message)(message), _bus); 691 } 692 } 693 }