plugins.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. Plugins
  2. =======
  3. Almost all custom functionality is added through plugin modules, which
  4. are typically dynamic libraries or scripts. The ability to capture
  5. and/or output audio/video, make a recording, output to an RTMP stream,
  6. encode in x264 are all examples of things that are accomplished via
  7. plugin modules.
  8. Plugins can implement sources, outputs, encoders, and services.
  9. Plugin Module Headers
  10. ---------------------
  11. These are some notable headers commonly used by plugins:
  12. - `libobs/obs-module.h`_ -- The primary header used for creating plugin
  13. modules. This file automatically includes the following files:
  14. - `libobs/obs.h`_ -- The main libobs header. This file automatically
  15. includes the following files:
  16. - `libobs/obs-source.h`_ -- Used for implementing sources in plugin
  17. modules
  18. - `libobs/obs-output.h`_ -- Used for implementing outputs in plugin
  19. modules
  20. - `libobs/obs-encoder.h`_ -- Used for implementing encoders in
  21. plugin modules
  22. - `libobs/obs-service.h`_ -- Used for implementing services in
  23. plugin modules
  24. - `libobs/obs-data.h`_ -- Used for managing settings for libobs
  25. objects
  26. - `libobs/obs-properties.h`_ -- Used for generating properties for
  27. libobs objects
  28. - `libobs/graphics/graphics.h`_ -- Used for graphics rendering
  29. Common Directory Structure and CMakeLists.txt
  30. ---------------------------------------------
  31. The common way source files are organized is to have one file for plugin
  32. initialization, and then specific files for each individual object
  33. you're implementing. For example, if you were to create a plugin called
  34. 'my-plugin', you'd have something like my-plugin.c where plugin
  35. initialization is done, my-source.c for the definition of a custom
  36. source, my-output.c for the definition of a custom output, etc. (This
  37. is not a rule of course)
  38. This is an example of a common directory structure for a native plugin
  39. module::
  40. my-plugin/data/locale/en-US.ini
  41. my-plugin/CMakeLists.txt
  42. my-plugin/my-plugin.c
  43. my-plugin/my-source.c
  44. my-plugin/my-output.c
  45. my-plugin/my-encoder.c
  46. my-plugin/my-service.c
  47. This would be an example of a common CMakeLists.txt file associated with
  48. these files::
  49. # my-plugin/CMakeLists.txt
  50. project(my-plugin)
  51. set(my-plugin_SOURCES
  52. my-plugin.c
  53. my-source.c
  54. my-output.c
  55. my-encoder.c
  56. my-service.c)
  57. add_library(my-plugin MODULE
  58. ${my-plugin_SOURCES})
  59. target_link_libraries(my-plugin
  60. libobs)
  61. install_obs_plugin_with_data(my-plugin data)
  62. Native Plugin Initialization
  63. ----------------------------
  64. To create a native plugin module, you will need to include the
  65. `libobs/obs-module.h`_ header, use :c:func:`OBS_DECLARE_MODULE()` macro,
  66. then create a definition of the function :c:func:`obs_module_load()`.
  67. In your :c:func:`obs_module_load()` function, you then register any of
  68. your custom sources, outputs, encoders, or services. See the
  69. :doc:`reference-modules` for more information.
  70. The following is an example of my-plugin.c, which would register one
  71. object of each type:
  72. .. code:: cpp
  73. /* my-plugin.c */
  74. #include <obs-module.h>
  75. /* Defines common functions (required) */
  76. OBS_DECLARE_MODULE()
  77. /* Implements common ini-based locale (optional) */
  78. OBS_MODULE_USE_DEFAULT_LOCALE("my-plugin", "en-US")
  79. extern struct obs_source_info my_source; /* Defined in my-source.c */
  80. extern struct obs_output_info my_output; /* Defined in my-output.c */
  81. extern struct obs_encoder_info my_encoder; /* Defined in my-encoder.c */
  82. extern struct obs_service_info my_service; /* Defined in my-service.c */
  83. bool obs_module_load(void)
  84. {
  85. obs_register_source(&my_source);
  86. obs_register_output(&my_output);
  87. obs_register_encoder(&my_encoder);
  88. obs_register_service(&my_service);
  89. return true;
  90. }
  91. .. _plugins_sources:
  92. Sources
  93. -------
  94. Sources are used to render video and/or audio on stream. Things such as
  95. capturing displays/games/audio, playing a video, showing an image, or
  96. playing audio. Sources can also be used to implement audio and video
  97. filters as well as transitions. The `libobs/obs-source.h`_ file is the
  98. dedicated header for implementing sources. See the
  99. :doc:`reference-sources` for more information.
  100. For example, to implement a source object, you need to define an
  101. :c:type:`obs_source_info` structure and fill it out with information and
  102. callbacks related to your source:
  103. .. code:: cpp
  104. /* my-source.c */
  105. [...]
  106. struct obs_source_info my_source {
  107. .id = "my_source",
  108. .type = OBS_SOURCE_TYPE_INPUT,
  109. .output_flags = OBS_SOURCE_VIDEO,
  110. .get_name = my_source_name,
  111. .create = my_source_create,
  112. .destroy = my_source_destroy,
  113. .update = my_source_update,
  114. .video_render = my_source_render,
  115. .get_width = my_source_width,
  116. .get_height = my_source_height
  117. };
  118. Then, in my-plugin.c, you would call :c:func:`obs_register_source()` in
  119. :c:func:`obs_module_load()` to register the source with libobs.
  120. .. code:: cpp
  121. /* my-plugin.c */
  122. [...]
  123. extern struct obs_source_info my_source; /* Defined in my-source.c */
  124. bool obs_module_load(void)
  125. {
  126. obs_register_source(&my_source);
  127. [...]
  128. return true;
  129. }
  130. Some simple examples of sources:
  131. - Synchronous video source: The `image source <https://github.com/jp9000/obs-studio/blob/master/plugins/image-source/image-source.c>`_
  132. - Asynchronous video source: The `random texture test source <https://github.com/jp9000/obs-studio/blob/master/test/test-input/test-random.c>`_
  133. - Audio source: The `sine wave test source <https://github.com/jp9000/obs-studio/blob/master/test/test-input/test-sinewave.c>`_
  134. - Video filter: The `test video filter <https://github.com/jp9000/obs-studio/blob/master/test/test-input/test-filter.c>`_
  135. - Audio filter: The `gain audio filter <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-filters/gain-filter.c>`_
  136. .. _plugins_outputs:
  137. Outputs
  138. -------
  139. Outputs allow the ability to output the currently rendering audio/video.
  140. Streaming and recording are two common examples of outputs, but not the
  141. only types of outputs. Outputs can receive the raw data or receive
  142. encoded data. The `libobs/obs-output.h`_ file is the dedicated header
  143. for implementing outputs. See the :doc:`reference-outputs` for more
  144. information.
  145. For example, to implement an output object, you need to define an
  146. :c:type:`obs_output_info` structure and fill it out with information and
  147. callbacks related to your output:
  148. .. code:: cpp
  149. /* my-output.c */
  150. [...]
  151. struct obs_output_info my_output {
  152. .id = "my_output",
  153. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED,
  154. .get_name = my_output_name,
  155. .create = my_output_create,
  156. .destroy = my_output_destroy,
  157. .start = my_output_start,
  158. .stop = my_output_stop,
  159. .encoded_packet = my_output_data,
  160. .get_total_bytes = my_output_total_bytes,
  161. .encoded_video_codecs = "h264",
  162. .encoded_audio_codecs = "aac"
  163. };
  164. Then, in my-plugin.c, you would call :c:func:`obs_register_output()` in
  165. :c:func:`obs_module_load()` to register the output with libobs.
  166. .. code:: cpp
  167. /* my-plugin.c */
  168. [...]
  169. extern struct obs_output_info my_output; /* Defined in my-output.c */
  170. bool obs_module_load(void)
  171. {
  172. obs_register_output(&my_output);
  173. [...]
  174. return true;
  175. }
  176. Some examples of outputs:
  177. - Encoded video/audio outputs:
  178. - The `FLV output <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-outputs/flv-output.c>`_
  179. - The `FFmpeg muxer output <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-ffmpeg/obs-ffmpeg-mux.c>`_
  180. - The `RTMP stream output <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-outputs/rtmp-stream.c>`_
  181. - Raw video/audio outputs:
  182. - The `FFmpeg output <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-ffmpeg/obs-ffmpeg-output.c>`_
  183. .. _plugins_encoders:
  184. Encoders
  185. --------
  186. Encoders are OBS-specific implementations of video/audio encoders, which
  187. are used with outputs that use encoders. x264, NVENC, Quicksync are
  188. examples of encoder implementations. The `libobs/obs-encoder.h`_ file
  189. is the dedicated header for implementing encoders. See the
  190. :doc:`reference-encoders` for more information.
  191. For example, to implement an encoder object, you need to define an
  192. :c:type:`obs_encoder_info` structure and fill it out with information
  193. and callbacks related to your encoder:
  194. .. code:: cpp
  195. /* my-encoder.c */
  196. [...]
  197. struct obs_encoder_info my_encoder_encoder = {
  198. .id = "my_encoder",
  199. .type = OBS_ENCODER_VIDEO,
  200. .codec = "h264",
  201. .get_name = my_encoder_name,
  202. .create = my_encoder_create,
  203. .destroy = my_encoder_destroy,
  204. .encode = my_encoder_encode,
  205. .update = my_encoder_update,
  206. .get_extra_data = my_encoder_extra_data,
  207. .get_sei_data = my_encoder_sei,
  208. .get_video_info = my_encoder_video_info
  209. };
  210. Then, in my-plugin.c, you would call :c:func:`obs_register_encoder()`
  211. in :c:func:`obs_module_load()` to register the encoder with libobs.
  212. .. code:: cpp
  213. /* my-plugin.c */
  214. [...]
  215. extern struct obs_encoder_info my_encoder; /* Defined in my-encoder.c */
  216. bool obs_module_load(void)
  217. {
  218. obs_register_encoder(&my_encoder);
  219. [...]
  220. return true;
  221. }
  222. **IMPORTANT NOTE:** Encoder settings currently have a few expected
  223. common setting values that should have a specific naming convention:
  224. - **"bitrate"** - This value should be used for both video and audio
  225. encoders: bitrate, in kilobits.
  226. - **"rate_control"** - This is a setting used for video encoders.
  227. It's generally expected to have at least a "CBR" rate control.
  228. Other common rate controls are "VBR", "CQP".
  229. - **"keyint_sec"** - For video encoders, sets the keyframe interval
  230. value, in seconds, or closest possible approximation. *(Author's
  231. note: This should have have been "keyint", in frames.)*
  232. Examples of encoders:
  233. - Video encoders:
  234. - The `x264 encoder <https://github.com/jp9000/obs-studio/tree/master/plugins/obs-x264>`_
  235. - The `FFmpeg NVENC encoder <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-ffmpeg/obs-ffmpeg-nvenc.c>`_
  236. - The `Quicksync encoder <https://github.com/jp9000/obs-studio/tree/master/plugins/obs-qsv11>`_
  237. - Audio encoders:
  238. - The `FFmpeg AAC/Opus encoder <https://github.com/jp9000/obs-studio/blob/master/plugins/obs-ffmpeg/obs-ffmpeg-audio-encoders.c>`_
  239. .. _plugins_services:
  240. Services
  241. --------
  242. Services are custom implementations of streaming services, which are
  243. used with outputs that stream. For example, you could have a custom
  244. implementation for streaming to Twitch, and another for YouTube to allow
  245. the ability to log in and use their APIs to do things such as get the
  246. RTMP servers or control the channel. The `libobs/obs-service.h`_ file
  247. is the dedicated header for implementing services. See the
  248. :doc:`reference-services` for more information.
  249. *(Author's note: the service API is incomplete as of this writing)*
  250. For example, to implement a service object, you need to define an
  251. :c:type:`obs_service_info` structure and fill it out with information
  252. and callbacks related to your service:
  253. .. code:: cpp
  254. /* my-service.c */
  255. [...]
  256. struct obs_service_info my_service_service = {
  257. .id = "my_service",
  258. .get_name = my_service_name,
  259. .create = my_service_create,
  260. .destroy = my_service_destroy,
  261. .encode = my_service_encode,
  262. .update = my_service_update,
  263. .get_url = my_service_url,
  264. .get_key = my_service_key
  265. };
  266. Then, in my-plugin.c, you would call :c:func:`obs_register_service()` in
  267. :c:func:`obs_module_load()` to register the service with libobs.
  268. .. code:: cpp
  269. /* my-plugin.c */
  270. [...]
  271. extern struct obs_service_info my_service; /* Defined in my-service.c */
  272. bool obs_module_load(void)
  273. {
  274. obs_register_service(&my_service);
  275. [...]
  276. return true;
  277. }
  278. The only two existing services objects are the "common RTMP services"
  279. and "custom RTMP service" objects in `plugins/rtmp-services
  280. <https://github.com/jp9000/obs-studio/tree/master/plugins/rtmp-services>`_
  281. Settings
  282. --------
  283. Settings (see `libobs/obs-data.h`_) are used to get or set settings data
  284. typically associated with libobs objects, and can then be saved and
  285. loaded via Json text. See the :doc:`reference-settings` for more
  286. information.
  287. The *obs_data_t* is the equivalent of a Json object, where it's a string
  288. table of sub-objects, and the *obs_data_array_t* is similarly used to
  289. store an array of *obs_data_t* objects, similar to Json arrays (though
  290. not quite identical).
  291. To create an *obs_data_t* or *obs_data_array_t* object, you'd call the
  292. :c:func:`obs_data_create()` or :c:func:`obs_data_array_create()`
  293. functions. *obs_data_t* and *obs_data_array_t* objects are reference
  294. counted, so when you are finished with the object, call
  295. :c:func:`obs_data_release()` or :c:func:`obs_data_array_release()` to
  296. release those references. Any time an *obs_data_t* or
  297. *obs_data_array_t* object is returned by a function, their references
  298. are incremented, so you must release those references each time.
  299. To set values for an *obs_data_t* object, you'd use one of the following
  300. functions:
  301. .. code:: cpp
  302. /* Set functions */
  303. EXPORT void obs_data_set_string(obs_data_t *data, const char *name, const char *val);
  304. EXPORT void obs_data_set_int(obs_data_t *data, const char *name, long long val);
  305. EXPORT void obs_data_set_double(obs_data_t *data, const char *name, double val);
  306. EXPORT void obs_data_set_bool(obs_data_t *data, const char *name, bool val);
  307. EXPORT void obs_data_set_obj(obs_data_t *data, const char *name, obs_data_t *obj);
  308. EXPORT void obs_data_set_array(obs_data_t *data, const char *name, obs_data_array_t *array);
  309. Similarly, to get a value from an *obs_data_t* object, you'd use one of
  310. the following functions:
  311. .. code:: cpp
  312. /* Get functions */
  313. EXPORT const char *obs_data_get_string(obs_data_t *data, const char *name);
  314. EXPORT long long obs_data_get_int(obs_data_t *data, const char *name);
  315. EXPORT double obs_data_get_double(obs_data_t *data, const char *name);
  316. EXPORT bool obs_data_get_bool(obs_data_t *data, const char *name);
  317. EXPORT obs_data_t *obs_data_get_obj(obs_data_t *data, const char *name);
  318. EXPORT obs_data_array_t *obs_data_get_array(obs_data_t *data, const char *name);
  319. Unlike typical Json data objects, the *obs_data_t* object can also set
  320. default values. This allows the ability to control what is returned if
  321. there is no value assigned to a specific string in an *obs_data_t*
  322. object when that data is loaded from a Json string or Json file. Each
  323. libobs object also has a *get_defaults* callback which allows setting
  324. the default settings for the object on creation.
  325. These functions control the default values are as follows:
  326. .. code:: cpp
  327. /* Default value functions. */
  328. EXPORT void obs_data_set_default_string(obs_data_t *data, const char *name, const char *val);
  329. EXPORT void obs_data_set_default_int(obs_data_t *data, const char *name, long long val);
  330. EXPORT void obs_data_set_default_double(obs_data_t *data, const char *name, double val);
  331. EXPORT void obs_data_set_default_bool(obs_data_t *data, const char *name, bool val);
  332. EXPORT void obs_data_set_default_obj(obs_data_t *data, const char *name, obs_data_t *obj);
  333. Properties
  334. ----------
  335. Properties (see `libobs/obs-properties.h`_) are used to automatically
  336. generate user interface to modify settings for a libobs object (if
  337. desired). Each libobs object has a *get_properties* callback which is
  338. used to generate properties. The properties API defines specific
  339. properties that are linked to the object's settings, and the front-end
  340. uses those properties to generate widgets in order to allow the user to
  341. modify the settings. For example, if you had a boolean setting, you
  342. would use :c:func:`obs_properties_add_bool()` to allow the user to be
  343. able to change that setting. See the :doc:`reference-properties` for
  344. more information.
  345. An example of this:
  346. .. code:: cpp
  347. static obs_properties_t *my_source_properties(void *data)
  348. {
  349. obs_properties_t *ppts = obs_properties_create();
  350. obs_properties_add_bool(ppts, "my_bool",
  351. obs_module_text("MyBool"));
  352. UNUSED_PARAMETER(data);
  353. return ppts;
  354. }
  355. [...]
  356. struct obs_source_info my_source {
  357. .get_properties = my_source_properties,
  358. [...]
  359. };
  360. The *data* parameter is the object's data if the object is present.
  361. Typically this is unused and probably shouldn't be used if possible. It
  362. can be null if the properties are retrieved without an object associated
  363. with it.
  364. Properties can also be modified depending on what settings are shown.
  365. For example, you can mark certain properties as disabled or invisible
  366. depending on what a particular setting is set to using the
  367. :c:func:`obs_property_set_modified_callback()` function.
  368. For example, if you wanted boolean property A to hide text property B:
  369. .. code:: cpp
  370. static bool setting_a_modified(obs_properties_t *ppts,
  371. obs_property_t *p, obs_data_t *settings)
  372. {
  373. bool enabled = obs_data_get_bool(settings, "setting_a");
  374. p = obs_properties_get(ppts, "setting_b");
  375. obs_property_set_enabled(p, enabled);
  376. /* return true to update property widgets, false
  377. otherwise */
  378. return true;
  379. }
  380. [...]
  381. static obs_properties_t *my_source_properties(void *data)
  382. {
  383. obs_properties_t *ppts = obs_properties_create();
  384. obs_property_t *p;
  385. p = obs_properties_add_bool(ppts, "setting_a",
  386. obs_module_text("SettingA"));
  387. obs_property_set_modified_callback(p, setting_a_modified);
  388. obs_properties_add_text(ppts, "setting_b",
  389. obs_module_text("SettingB"),
  390. OBS_TEXT_DEFAULT);
  391. return ppts;
  392. }
  393. Localization
  394. ------------
  395. Typically, most plugins bundled with OBS Studio will use a simple
  396. ini-file localization method, where each file is a different language.
  397. When using this method, the :c:func:`OBS_MODULE_USE_DEFAULT_LOCALE()`
  398. macro is used which will automatically load/destroy the locale data with
  399. no extra effort on part of the plugin. Then the
  400. :c:func:`obs_module_text()` function (which is automatically declared as
  401. an extern by `libobs/obs-module.h`_) is used when text lookup is needed.
  402. There are two exports the module used to load/destroy locale: the
  403. :c:func:`obs_module_set_locale()` export, and the
  404. :c:func:`obs_module_free_locale()` export. The
  405. :c:func:`obs_module_set_locale()` export is called by libobs to set the
  406. current language, and then the :c:func:`obs_module_free_locale()` export
  407. is called by libobs on destruction of the module. If you wish to
  408. implement a custom locale implementation for your plugin, you'd want to
  409. define these exports along with the :c:func:`obs_module_text()` extern
  410. yourself instead of relying on the
  411. :c:func:`OBS_MODULE_USE_DEFAULT_LOCALE()` macro.
  412. .. ---------------------------------------------------------------------------
  413. .. _libobs/obs-module.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-module.h
  414. .. _libobs/obs.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs.h
  415. .. _libobs/obs-source.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-source.h
  416. .. _libobs/obs-output.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-output.h
  417. .. _libobs/obs-encoder.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-encoder.h
  418. .. _libobs/obs-service.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-service.h
  419. .. _libobs/obs-data.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-data.h
  420. .. _libobs/obs-properties.h: https://github.com/jp9000/obs-studio/blob/master/libobs/obs-properties.h
  421. .. _libobs/graphics/graphics.h: https://github.com/jp9000/obs-studio/blob/master/libobs/graphics/graphics.h