frontends.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. Frontends
  2. =========
  3. Initialization and Shutdown
  4. ---------------------------
  5. To initialize libobs, you must call :c:func:`obs_startup()`,
  6. :c:func:`obs_reset_video()`, and then :c:func:`obs_reset_audio()`.
  7. After that, modules typically should be loaded.
  8. You can load individual modules manually by calling
  9. :c:func:`obs_open_module()`. After loading, the
  10. :c:func:`obs_open_module()` function, you must then call
  11. :c:func:`obs_init_module()` to initialize the module.
  12. You can load modules automatically via two functions:
  13. :c:func:`obs_add_module_path()` and :c:func:`obs_load_all_modules()`.
  14. After all plugin modules have been loaded, call
  15. :c:func:`obs_post_load_modules()`.
  16. Certain modules may optionally use a configuration storage directory,
  17. which is set as a parameter to :c:func:`obs_startup()`.
  18. When it's time to shut down the frontend, make sure to release all
  19. references to any objects, free any data, and then call
  20. :c:func:`obs_shutdown()`. If for some reason any libobs objects have
  21. not been released, they will be destroyed automatically and a warning
  22. will be logged.
  23. To detect if any general memory allocations have not been freed, call
  24. the :c:func:`bnum_allocs()` to get the number of allocations remaining.
  25. If the number remaining is above 0, there are memory leaks.
  26. See :ref:`obs_init_shutdown_reference` for more information.
  27. Reconfiguring Video
  28. -------------------
  29. Any time after initialization, video settings can be reconfigured by
  30. calling :c:func:`obs_reset_video()` as long as no outputs are active.
  31. Audio was originally intended to have this capability as well, but
  32. currently is not able to be reset once initialized; libobs must be fully
  33. shutdown in order to reconfigure audio settings.
  34. Displays
  35. --------
  36. Displays as the name implies are used for display/preview panes. To use
  37. displays, you must have a native window handle or identifier to draw on.
  38. First you must call :c:func:`obs_display_create()` to initialize the
  39. display, then you must assign a draw callback with
  40. :c:func:`obs_display_add_draw_callback()`. If you need to remove a draw
  41. callback, call :c:func:`obs_display_remove_draw_callback()` similarly.
  42. When drawing, to draw the main preview window (if any), call
  43. :c:func:`obs_render_main_texture()`. If you need to render a specific
  44. source on a secondary display, you can increment its "showing" state
  45. with :c:func:`obs_source_inc_showing()` while it's showing in the
  46. secondary display, draw it with :c:func:`obs_source_video_render()` in
  47. the draw callback, then when it's no longer showing in the secondary
  48. display, call :c:func:`obs_source_dec_showing()`.
  49. If the display needs to be resized, call :c:func:`obs_display_resize()`.
  50. If the display needs a custom background color other than black, call
  51. :c:func:`obs_display_set_background_color()`.
  52. If the display needs to be temporarily disabled, call
  53. :c:func:`obs_display_set_enabled()` to disable, and
  54. :c:func:`obs_display_enabled()` to get its enabled/disabled state.
  55. Then call :c:func:`obs_display_destroy()` to destroy the display when
  56. it's no longer needed.
  57. *(Important note: do not use more than one display widget within the
  58. hierarchy of the same base window; this will cause presentation stalls
  59. on macOS.)*
  60. For an example of how displays are used with Qt, see
  61. `UI/qt-display.hpp`_ and `UI/qt-display.cpp`_.
  62. See :ref:`display_reference` for more information.
  63. Saving/Loading Objects and Object Management
  64. --------------------------------------------
  65. The frontend is generally expected to manage its own objects, however
  66. for sources, there are some helper functions to allow easier
  67. saving/loading all sources: :c:func:`obs_save_sources()` and
  68. :c:func:`obs_load_sources()`. With those functions, all sources that
  69. aren't private will automatically be saved and loaded. You can also
  70. save/load individual sources manually by using
  71. :c:func:`obs_save_source()` and :c:func:`obs_load_source()`.
  72. *(Author's note: I should not have written those helper functions; the
  73. downside is I had to add "private" sources that aren't saveable via the*
  74. :c:func:`obs_source_create_private()` *function. Just one of the many
  75. minor design flaws that can occur during long-term development.)*
  76. For outputs, encoders, and services, there are no helper functions, so
  77. usually you'd get their settings individually and save them as json.
  78. (See :c:func:`obs_output_get_settings()`). You don't have to save each
  79. object to different files individually; you'd save multiple objects
  80. together in a bigger :c:type:`obs_data_t` object, then save that via
  81. :c:func:`obs_data_save_json_safe()`, then load everything again via
  82. :c:func:`obs_data_create_from_json_file_safe()`.
  83. Signals
  84. -------
  85. The core, as well as scenes and sources, have a set of standard signals
  86. that are used to determine when something happens or changes.
  87. Typically the most important signals are the
  88. :ref:`output_signal_handler_reference`: the **start**, **stop**,
  89. **starting**, **stopping**, **reconnect**, **reconnect_success**
  90. signals in particular.
  91. Most other signals for scenes/sources are optional if you are the only
  92. thing controlling their state. However, it's generally recommended to
  93. watch most signals when possible for consistency. See
  94. :ref:`source_signal_handler_reference` and :ref:`scene_signal_reference`
  95. for more information.
  96. For example, let's say you wanted to connect a callback to the **stop**
  97. signal of an output. The **stop** signal has two parameters: *output*
  98. and *code*. A callback for this signal would typically look something
  99. like this:
  100. .. code:: cpp
  101. static void output_stopped(void *my_data, calldata_t *cd)
  102. {
  103. obs_output_t *output = calldata_ptr(cd, "output");
  104. int code = calldata_int(cd, "code");
  105. [...]
  106. }
  107. *(Note that callbacks are not thread-safe.)*
  108. Then to connect it to the **stop** signal, you use the
  109. :c:func:`signal_handler_connect()` with the callback. In this case for
  110. example:
  111. .. code:: cpp
  112. signal_handler_t *handler = obs_output_get_signal_handler(output);
  113. signal_handler_connect(handler, "stop", output_stopped);
  114. .. _displaying_sources:
  115. Displaying Sources
  116. ------------------
  117. Sources are displayed on stream/recording via :ref:`output_channels`
  118. with the :c:func:`obs_set_output_source()` function. There are 64
  119. channels that you can assign sources to, which will draw on top of each
  120. other in ascending index order. Typically, a normal source shouldn't be
  121. directly assigned with this function; you would use a scene or a
  122. transition containing scenes.
  123. To draw one or more sources together with a specific transform applied
  124. to them, scenes are used. To create a scene, you call
  125. :c:func:`obs_scene_create()`. Child sources are referenced using scene
  126. items, and then specific transforms are applied to those scene items.
  127. Scene items are not sources but containers for sources; the same source
  128. can be referenced by multiple scene items within the same scene, or can
  129. be referenced in multiple scenes. To create a scene item that
  130. references a source, you call :c:func:`obs_scene_add()`, which returns a
  131. new reference to a scene item.
  132. To change the transform of a scene item, you typically would call a
  133. function like :c:func:`obs_sceneitem_set_pos()` to change its position,
  134. :c:func:`obs_sceneitem_set_rot()` to change its rotation, or
  135. :c:func:`obs_sceneitem_set_scale()` to change its scaling. Scene items
  136. can also force scaling in to a custom size constraint referred to as a
  137. "bounding box"; a bounding box will force the source to be drawn at a
  138. specific size and with specific scaling constraint within that size. To
  139. use a bounding box, you call the
  140. :c:func:`obs_sceneitem_set_bounds_type()`,
  141. :c:func:`obs_sceneitem_set_bounds()`, and
  142. :c:func:`obs_sceneitem_set_bounds_alignment()`. Though the easiest way
  143. to handle everything related to transforms is to use the
  144. :c:func:`obs_sceneitem_set_info2()` and
  145. :c:func:`obs_sceneitem_get_info2()` functions. See
  146. :ref:`scene_item_reference` for all the functions related to scene
  147. items.
  148. Usually, a smooth transition between multiple scenes is required. To do
  149. this, transitions are used. To create a transition, you use
  150. :c:func:`obs_source_create()` or :c:func:`obs_source_create_private()`
  151. like any other source. Then, to activate a transition, you call
  152. :c:func:`obs_transition_start()`. When the transition is not active and
  153. is only displaying one source, it performs a pass-through to the current
  154. displaying source. See :ref:`transitions` for more functions related to
  155. using transitions.
  156. The recommended way to set up your structure is to have a transition as
  157. the source that is used as the main output source, then your scene as a
  158. child of the transition, then your sources as children in the scene.
  159. When you need to switch to a new scene, simply call
  160. :c:func:`obs_transition_start()`.
  161. Outputs, Encoders, and Services
  162. -------------------------------
  163. Outputs, encoders, and services are all used together, and managed a bit
  164. differently than sources. There currently is no global function to
  165. save/load them, that must be accomplished manually for now via their
  166. settings if needed.
  167. Encoders are used with outputs that expect encoded data (which is almost
  168. all typical outputs), such as standard file recording or streaming.
  169. Services are used with outputs to a stream; the `RTMP output`_ is the
  170. quintessential example of this.
  171. Here's an example of how an output would be used with encoders and
  172. services:
  173. .. code:: cpp
  174. obs_encoder_set_video(my_h264_encoder, obs_get_video());
  175. obs_encoder_set_audio(my_aac_encoder, obs_get_audio());
  176. obs_output_set_video_encoder(my_output, my_h264_encoder);
  177. obs_output_set_audio_encoder(my_output, my_aac_encoder);
  178. obs_output_set_service(my_output, my_service); /* if a stream */
  179. obs_output_start(my_output);
  180. Once the output has started successfully, it automatically starts
  181. capturing the video and/or audio from the current video/audio output
  182. (i.e. any sources that are assigned to the :ref:`output_channels`).
  183. If the output fails to start up, it will send the **stop** signal with
  184. an error code in the *code* parameter, possibly accompanied by a
  185. translated error message stored that can be obtained via the
  186. :c:func:`obs_output_get_last_error()` function.
  187. .. --------------------------------------------------------------------
  188. .. _RTMP Output: https://github.com/obsproject/obs-studio/blob/master/plugins/obs-outputs/rtmp-stream.c
  189. .. _UI/qt-display.hpp: https://github.com/obsproject/obs-studio/blob/master/UI/qt-display.hpp
  190. .. _UI/qt-display.cpp: https://github.com/obsproject/obs-studio/blob/master/UI/qt-display.cpp