pipewire.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /* pipewire.c
  2. *
  3. * Copyright 2020 Georges Basile Stavracas Neto <[email protected]>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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 General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * SPDX-License-Identifier: GPL-2.0-or-later
  19. */
  20. #include "pipewire.h"
  21. #include <util/darray.h>
  22. #include <gio/gio.h>
  23. #include <gio/gunixfdlist.h>
  24. #include <fcntl.h>
  25. #include <glad/glad.h>
  26. #include <linux/dma-buf.h>
  27. #include <libdrm/drm_fourcc.h>
  28. #include <pipewire/pipewire.h>
  29. #include <spa/param/video/format-utils.h>
  30. #include <spa/debug/format.h>
  31. #include <spa/debug/types.h>
  32. #include <spa/param/video/type-info.h>
  33. #include <spa/utils/result.h>
  34. #define CURSOR_META_SIZE(width, height) \
  35. (sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + \
  36. width * height * 4)
  37. struct obs_pw_version {
  38. int major;
  39. int minor;
  40. int micro;
  41. };
  42. struct format_info {
  43. uint32_t spa_format;
  44. uint32_t drm_format;
  45. DARRAY(uint64_t) modifiers;
  46. };
  47. struct _obs_pipewire_data {
  48. uint32_t pipewire_node;
  49. int pipewire_fd;
  50. gs_texture_t *texture;
  51. struct pw_thread_loop *thread_loop;
  52. struct pw_context *context;
  53. struct pw_core *core;
  54. struct spa_hook core_listener;
  55. int server_version_sync;
  56. struct obs_pw_version server_version;
  57. struct pw_stream *stream;
  58. struct spa_hook stream_listener;
  59. struct spa_source *reneg;
  60. struct spa_video_info format;
  61. struct {
  62. bool valid;
  63. int x, y;
  64. uint32_t width, height;
  65. } crop;
  66. struct {
  67. bool visible;
  68. bool valid;
  69. int x, y;
  70. int hotspot_x, hotspot_y;
  71. int width, height;
  72. gs_texture_t *texture;
  73. } cursor;
  74. struct obs_video_info video_info;
  75. bool negotiated;
  76. DARRAY(struct format_info) format_info;
  77. };
  78. /* auxiliary methods */
  79. static bool parse_pw_version(struct obs_pw_version *dst, const char *version)
  80. {
  81. int n_matches = sscanf(version, "%d.%d.%d", &dst->major, &dst->minor,
  82. &dst->micro);
  83. return n_matches == 3;
  84. }
  85. static bool check_pw_version(const struct obs_pw_version *pw_version, int major,
  86. int minor, int micro)
  87. {
  88. if (pw_version->major != major)
  89. return pw_version->major > major;
  90. if (pw_version->minor != minor)
  91. return pw_version->minor > minor;
  92. return pw_version->micro >= micro;
  93. }
  94. static void update_pw_versions(obs_pipewire_data *obs_pw, const char *version)
  95. {
  96. blog(LOG_INFO, "[pipewire] Server version: %s", version);
  97. blog(LOG_INFO, "[pipewire] Library version: %s",
  98. pw_get_library_version());
  99. blog(LOG_INFO, "[pipewire] Header version: %s",
  100. pw_get_headers_version());
  101. if (!parse_pw_version(&obs_pw->server_version, version))
  102. blog(LOG_WARNING, "[pipewire] failed to parse server version");
  103. }
  104. static void teardown_pipewire(obs_pipewire_data *obs_pw)
  105. {
  106. if (obs_pw->thread_loop) {
  107. pw_thread_loop_wait(obs_pw->thread_loop);
  108. pw_thread_loop_stop(obs_pw->thread_loop);
  109. }
  110. if (obs_pw->stream)
  111. pw_stream_disconnect(obs_pw->stream);
  112. g_clear_pointer(&obs_pw->stream, pw_stream_destroy);
  113. g_clear_pointer(&obs_pw->context, pw_context_destroy);
  114. g_clear_pointer(&obs_pw->thread_loop, pw_thread_loop_destroy);
  115. if (obs_pw->pipewire_fd > 0) {
  116. close(obs_pw->pipewire_fd);
  117. obs_pw->pipewire_fd = 0;
  118. }
  119. obs_pw->negotiated = false;
  120. }
  121. static void destroy_session(obs_pipewire_data *obs_pw)
  122. {
  123. obs_enter_graphics();
  124. g_clear_pointer(&obs_pw->cursor.texture, gs_texture_destroy);
  125. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  126. obs_leave_graphics();
  127. }
  128. static inline bool has_effective_crop(obs_pipewire_data *obs_pw)
  129. {
  130. return obs_pw->crop.valid &&
  131. (obs_pw->crop.x != 0 || obs_pw->crop.y != 0 ||
  132. obs_pw->crop.width < obs_pw->format.info.raw.size.width ||
  133. obs_pw->crop.height < obs_pw->format.info.raw.size.height);
  134. }
  135. static const struct {
  136. uint32_t spa_format;
  137. uint32_t drm_format;
  138. enum gs_color_format gs_format;
  139. bool swap_red_blue;
  140. const char *pretty_name;
  141. } supported_formats[] = {
  142. {
  143. SPA_VIDEO_FORMAT_BGRA,
  144. DRM_FORMAT_ARGB8888,
  145. GS_BGRA,
  146. false,
  147. "ARGB8888",
  148. },
  149. {
  150. SPA_VIDEO_FORMAT_RGBA,
  151. DRM_FORMAT_ABGR8888,
  152. GS_RGBA,
  153. false,
  154. "ABGR8888",
  155. },
  156. {
  157. SPA_VIDEO_FORMAT_BGRx,
  158. DRM_FORMAT_XRGB8888,
  159. GS_BGRX,
  160. false,
  161. "XRGB8888",
  162. },
  163. {
  164. SPA_VIDEO_FORMAT_RGBx,
  165. DRM_FORMAT_XBGR8888,
  166. GS_BGRX,
  167. true,
  168. "XBGR8888",
  169. },
  170. };
  171. #define N_SUPPORTED_FORMATS \
  172. (sizeof(supported_formats) / sizeof(supported_formats[0]))
  173. static bool lookup_format_info_from_spa_format(
  174. uint32_t spa_format, uint32_t *out_drm_format,
  175. enum gs_color_format *out_gs_format, bool *out_swap_red_blue)
  176. {
  177. for (size_t i = 0; i < N_SUPPORTED_FORMATS; i++) {
  178. if (supported_formats[i].spa_format != spa_format)
  179. continue;
  180. if (out_drm_format)
  181. *out_drm_format = supported_formats[i].drm_format;
  182. if (out_gs_format)
  183. *out_gs_format = supported_formats[i].gs_format;
  184. if (out_swap_red_blue)
  185. *out_swap_red_blue = supported_formats[i].swap_red_blue;
  186. return true;
  187. }
  188. return false;
  189. }
  190. static void swap_texture_red_blue(gs_texture_t *texture)
  191. {
  192. GLuint gl_texure = *(GLuint *)gs_texture_get_obj(texture);
  193. glBindTexture(GL_TEXTURE_2D, gl_texure);
  194. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  195. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  196. glBindTexture(GL_TEXTURE_2D, 0);
  197. }
  198. static inline struct spa_pod *build_format(struct spa_pod_builder *b,
  199. struct obs_video_info *ovi,
  200. uint32_t format, uint64_t *modifiers,
  201. size_t modifier_count)
  202. {
  203. struct spa_pod_frame format_frame;
  204. /* Make an object of type SPA_TYPE_OBJECT_Format and id SPA_PARAM_EnumFormat.
  205. * The object type is important because it defines the properties that are
  206. * acceptable. The id gives more context about what the object is meant to
  207. * contain. In this case we enumerate supported formats. */
  208. spa_pod_builder_push_object(b, &format_frame, SPA_TYPE_OBJECT_Format,
  209. SPA_PARAM_EnumFormat);
  210. /* add media type and media subtype properties */
  211. spa_pod_builder_add(b, SPA_FORMAT_mediaType,
  212. SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
  213. spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype,
  214. SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
  215. /* formats */
  216. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
  217. /* modifier */
  218. if (modifier_count > 0) {
  219. struct spa_pod_frame modifier_frame;
  220. /* build an enumeration of modifiers */
  221. spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier,
  222. SPA_POD_PROP_FLAG_MANDATORY |
  223. SPA_POD_PROP_FLAG_DONT_FIXATE);
  224. spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum,
  225. 0);
  226. /* The first element of choice pods is the preferred value. Here
  227. * we arbitrarily pick the first modifier as the preferred one.
  228. */
  229. spa_pod_builder_long(b, modifiers[0]);
  230. /* modifiers from an array */
  231. for (uint32_t i = 0; i < modifier_count; i++)
  232. spa_pod_builder_long(b, modifiers[i]);
  233. spa_pod_builder_pop(b, &modifier_frame);
  234. }
  235. /* add size and framerate ranges */
  236. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size,
  237. SPA_POD_CHOICE_RANGE_Rectangle(
  238. &SPA_RECTANGLE(320, 240), // Arbitrary
  239. &SPA_RECTANGLE(1, 1),
  240. &SPA_RECTANGLE(8192, 4320)),
  241. SPA_FORMAT_VIDEO_framerate,
  242. SPA_POD_CHOICE_RANGE_Fraction(
  243. &SPA_FRACTION(ovi->fps_num, ovi->fps_den),
  244. &SPA_FRACTION(0, 1), &SPA_FRACTION(360, 1)),
  245. 0);
  246. return spa_pod_builder_pop(b, &format_frame);
  247. }
  248. static bool build_format_params(obs_pipewire_data *obs_pw,
  249. struct spa_pod_builder *pod_builder,
  250. const struct spa_pod ***param_list,
  251. uint32_t *n_params)
  252. {
  253. uint32_t params_count = 0;
  254. const struct spa_pod **params;
  255. params =
  256. bzalloc(2 * obs_pw->format_info.num * sizeof(struct spa_pod *));
  257. if (!params) {
  258. blog(LOG_ERROR,
  259. "[pipewire] Failed to allocate memory for param pointers");
  260. return false;
  261. }
  262. if (!check_pw_version(&obs_pw->server_version, 0, 3, 33))
  263. goto build_shm;
  264. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  265. if (obs_pw->format_info.array[i].modifiers.num == 0) {
  266. continue;
  267. }
  268. params[params_count++] = build_format(
  269. pod_builder, &obs_pw->video_info,
  270. obs_pw->format_info.array[i].spa_format,
  271. obs_pw->format_info.array[i].modifiers.array,
  272. obs_pw->format_info.array[i].modifiers.num);
  273. }
  274. build_shm:
  275. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  276. params[params_count++] = build_format(
  277. pod_builder, &obs_pw->video_info,
  278. obs_pw->format_info.array[i].spa_format, NULL, 0);
  279. }
  280. *param_list = params;
  281. *n_params = params_count;
  282. return true;
  283. }
  284. static bool drm_format_available(uint32_t drm_format, uint32_t *drm_formats,
  285. size_t n_drm_formats)
  286. {
  287. for (size_t j = 0; j < n_drm_formats; j++) {
  288. if (drm_format == drm_formats[j]) {
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. static void init_format_info(obs_pipewire_data *obs_pw)
  295. {
  296. da_init(obs_pw->format_info);
  297. obs_enter_graphics();
  298. enum gs_dmabuf_flags dmabuf_flags;
  299. uint32_t *drm_formats = NULL;
  300. size_t n_drm_formats;
  301. bool capabilities_queried = gs_query_dmabuf_capabilities(
  302. &dmabuf_flags, &drm_formats, &n_drm_formats);
  303. for (size_t i = 0; i < N_SUPPORTED_FORMATS; i++) {
  304. struct format_info *info;
  305. info = da_push_back_new(obs_pw->format_info);
  306. da_init(info->modifiers);
  307. info->spa_format = supported_formats[i].spa_format;
  308. info->drm_format = supported_formats[i].drm_format;
  309. if (!capabilities_queried ||
  310. !drm_format_available(supported_formats[i].drm_format,
  311. drm_formats, n_drm_formats))
  312. continue;
  313. size_t n_modifiers;
  314. uint64_t *modifiers = NULL;
  315. if (gs_query_dmabuf_modifiers_for_format(
  316. supported_formats[i].drm_format, &modifiers,
  317. &n_modifiers)) {
  318. da_push_back_array(info->modifiers, modifiers,
  319. n_modifiers);
  320. }
  321. bfree(modifiers);
  322. if (dmabuf_flags &
  323. GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED) {
  324. uint64_t modifier_implicit = DRM_FORMAT_MOD_INVALID;
  325. da_push_back(info->modifiers, &modifier_implicit);
  326. }
  327. }
  328. obs_leave_graphics();
  329. bfree(drm_formats);
  330. }
  331. static void clear_format_info(obs_pipewire_data *obs_pw)
  332. {
  333. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  334. da_free(obs_pw->format_info.array[i].modifiers);
  335. }
  336. da_free(obs_pw->format_info);
  337. }
  338. static void remove_modifier_from_format(obs_pipewire_data *obs_pw,
  339. uint32_t spa_format, uint64_t modifier)
  340. {
  341. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  342. if (obs_pw->format_info.array[i].spa_format != spa_format)
  343. continue;
  344. if (!check_pw_version(&obs_pw->server_version, 0, 3, 40)) {
  345. da_erase_range(
  346. obs_pw->format_info.array[i].modifiers, 0,
  347. obs_pw->format_info.array[i].modifiers.num - 1);
  348. continue;
  349. }
  350. int idx = da_find(obs_pw->format_info.array[i].modifiers,
  351. &modifier, 0);
  352. while (idx != -1) {
  353. da_erase(obs_pw->format_info.array[i].modifiers, idx);
  354. idx = da_find(obs_pw->format_info.array[i].modifiers,
  355. &modifier, 0);
  356. }
  357. }
  358. }
  359. static void renegotiate_format(void *data, uint64_t expirations)
  360. {
  361. UNUSED_PARAMETER(expirations);
  362. obs_pipewire_data *obs_pw = (obs_pipewire_data *)data;
  363. const struct spa_pod **params = NULL;
  364. blog(LOG_INFO, "[pipewire] Renegotiating stream");
  365. pw_thread_loop_lock(obs_pw->thread_loop);
  366. uint8_t params_buffer[2048];
  367. struct spa_pod_builder pod_builder =
  368. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  369. uint32_t n_params;
  370. if (!build_format_params(obs_pw, &pod_builder, &params, &n_params)) {
  371. teardown_pipewire(obs_pw);
  372. pw_thread_loop_unlock(obs_pw->thread_loop);
  373. return;
  374. }
  375. pw_stream_update_params(obs_pw->stream, params, n_params);
  376. pw_thread_loop_unlock(obs_pw->thread_loop);
  377. bfree(params);
  378. }
  379. /* ------------------------------------------------- */
  380. static void on_process_cb(void *user_data)
  381. {
  382. obs_pipewire_data *obs_pw = user_data;
  383. struct spa_meta_cursor *cursor;
  384. uint32_t drm_format;
  385. struct spa_meta_region *region;
  386. struct spa_buffer *buffer;
  387. struct pw_buffer *b;
  388. bool swap_red_blue = false;
  389. bool has_buffer;
  390. /* Find the most recent buffer */
  391. b = NULL;
  392. while (true) {
  393. struct pw_buffer *aux =
  394. pw_stream_dequeue_buffer(obs_pw->stream);
  395. if (!aux)
  396. break;
  397. if (b)
  398. pw_stream_queue_buffer(obs_pw->stream, b);
  399. b = aux;
  400. }
  401. if (!b) {
  402. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  403. return;
  404. }
  405. buffer = b->buffer;
  406. has_buffer = buffer->datas[0].chunk->size != 0;
  407. obs_enter_graphics();
  408. if (!has_buffer)
  409. goto read_metadata;
  410. if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
  411. uint32_t planes = buffer->n_datas;
  412. uint32_t offsets[planes];
  413. uint32_t strides[planes];
  414. uint64_t modifiers[planes];
  415. int fds[planes];
  416. bool use_modifiers;
  417. blog(LOG_DEBUG,
  418. "[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
  419. buffer->datas[0].fd, buffer->datas[0].chunk->stride,
  420. buffer->datas[0].chunk->offset,
  421. obs_pw->format.info.raw.size.width,
  422. obs_pw->format.info.raw.size.height);
  423. if (!lookup_format_info_from_spa_format(
  424. obs_pw->format.info.raw.format, &drm_format, NULL,
  425. NULL)) {
  426. blog(LOG_ERROR,
  427. "[pipewire] unsupported DMA buffer format: %d",
  428. obs_pw->format.info.raw.format);
  429. goto read_metadata;
  430. }
  431. for (uint32_t plane = 0; plane < planes; plane++) {
  432. fds[plane] = buffer->datas[plane].fd;
  433. offsets[plane] = buffer->datas[plane].chunk->offset;
  434. strides[plane] = buffer->datas[plane].chunk->stride;
  435. modifiers[plane] = obs_pw->format.info.raw.modifier;
  436. }
  437. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  438. use_modifiers = obs_pw->format.info.raw.modifier !=
  439. DRM_FORMAT_MOD_INVALID;
  440. obs_pw->texture = gs_texture_create_from_dmabuf(
  441. obs_pw->format.info.raw.size.width,
  442. obs_pw->format.info.raw.size.height, drm_format,
  443. GS_BGRX, planes, fds, strides, offsets,
  444. use_modifiers ? modifiers : NULL);
  445. if (obs_pw->texture == NULL) {
  446. remove_modifier_from_format(
  447. obs_pw, obs_pw->format.info.raw.format,
  448. obs_pw->format.info.raw.modifier);
  449. pw_loop_signal_event(
  450. pw_thread_loop_get_loop(obs_pw->thread_loop),
  451. obs_pw->reneg);
  452. }
  453. } else {
  454. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  455. enum gs_color_format gs_format;
  456. if (!lookup_format_info_from_spa_format(
  457. obs_pw->format.info.raw.format, NULL, &gs_format,
  458. &swap_red_blue)) {
  459. blog(LOG_ERROR,
  460. "[pipewire] unsupported DMA buffer format: %d",
  461. obs_pw->format.info.raw.format);
  462. goto read_metadata;
  463. }
  464. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  465. obs_pw->texture = gs_texture_create(
  466. obs_pw->format.info.raw.size.width,
  467. obs_pw->format.info.raw.size.height, gs_format, 1,
  468. (const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
  469. }
  470. if (swap_red_blue)
  471. swap_texture_red_blue(obs_pw->texture);
  472. /* Video Crop */
  473. region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop,
  474. sizeof(*region));
  475. if (region && spa_meta_region_is_valid(region)) {
  476. blog(LOG_DEBUG,
  477. "[pipewire] Crop Region available (%dx%d+%d+%d)",
  478. region->region.position.x, region->region.position.y,
  479. region->region.size.width, region->region.size.height);
  480. obs_pw->crop.x = region->region.position.x;
  481. obs_pw->crop.y = region->region.position.y;
  482. obs_pw->crop.width = region->region.size.width;
  483. obs_pw->crop.height = region->region.size.height;
  484. obs_pw->crop.valid = true;
  485. } else {
  486. obs_pw->crop.valid = false;
  487. }
  488. read_metadata:
  489. /* Cursor */
  490. cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor,
  491. sizeof(*cursor));
  492. obs_pw->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
  493. if (obs_pw->cursor.visible && obs_pw->cursor.valid) {
  494. struct spa_meta_bitmap *bitmap = NULL;
  495. enum gs_color_format gs_format;
  496. if (cursor->bitmap_offset)
  497. bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset,
  498. struct spa_meta_bitmap);
  499. if (bitmap && bitmap->size.width > 0 &&
  500. bitmap->size.height > 0 &&
  501. lookup_format_info_from_spa_format(
  502. bitmap->format, NULL, &gs_format, &swap_red_blue)) {
  503. const uint8_t *bitmap_data;
  504. bitmap_data =
  505. SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
  506. obs_pw->cursor.hotspot_x = cursor->hotspot.x;
  507. obs_pw->cursor.hotspot_y = cursor->hotspot.y;
  508. obs_pw->cursor.width = bitmap->size.width;
  509. obs_pw->cursor.height = bitmap->size.height;
  510. g_clear_pointer(&obs_pw->cursor.texture,
  511. gs_texture_destroy);
  512. obs_pw->cursor.texture = gs_texture_create(
  513. obs_pw->cursor.width, obs_pw->cursor.height,
  514. gs_format, 1, &bitmap_data, GS_DYNAMIC);
  515. if (swap_red_blue)
  516. swap_texture_red_blue(obs_pw->cursor.texture);
  517. }
  518. obs_pw->cursor.x = cursor->position.x;
  519. obs_pw->cursor.y = cursor->position.y;
  520. }
  521. pw_stream_queue_buffer(obs_pw->stream, b);
  522. obs_leave_graphics();
  523. }
  524. static void on_param_changed_cb(void *user_data, uint32_t id,
  525. const struct spa_pod *param)
  526. {
  527. obs_pipewire_data *obs_pw = user_data;
  528. struct spa_pod_builder pod_builder;
  529. const struct spa_pod *params[3];
  530. uint32_t buffer_types;
  531. uint8_t params_buffer[1024];
  532. int result;
  533. if (!param || id != SPA_PARAM_Format)
  534. return;
  535. result = spa_format_parse(param, &obs_pw->format.media_type,
  536. &obs_pw->format.media_subtype);
  537. if (result < 0)
  538. return;
  539. if (obs_pw->format.media_type != SPA_MEDIA_TYPE_video ||
  540. obs_pw->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
  541. return;
  542. spa_format_video_raw_parse(param, &obs_pw->format.info.raw);
  543. buffer_types = 1 << SPA_DATA_MemPtr;
  544. bool has_modifier =
  545. spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) !=
  546. NULL;
  547. if (has_modifier || check_pw_version(&obs_pw->server_version, 0, 3, 24))
  548. buffer_types |= 1 << SPA_DATA_DmaBuf;
  549. blog(LOG_INFO, "[pipewire] Negotiated format:");
  550. blog(LOG_INFO, "[pipewire] Format: %d (%s)",
  551. obs_pw->format.info.raw.format,
  552. spa_debug_type_find_name(spa_type_video_format,
  553. obs_pw->format.info.raw.format));
  554. if (has_modifier) {
  555. blog(LOG_INFO, "[pipewire] Modifier: %" PRIu64,
  556. obs_pw->format.info.raw.modifier);
  557. }
  558. blog(LOG_INFO, "[pipewire] Size: %dx%d",
  559. obs_pw->format.info.raw.size.width,
  560. obs_pw->format.info.raw.size.height);
  561. blog(LOG_INFO, "[pipewire] Framerate: %d/%d",
  562. obs_pw->format.info.raw.framerate.num,
  563. obs_pw->format.info.raw.framerate.denom);
  564. /* Video crop */
  565. pod_builder =
  566. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  567. params[0] = spa_pod_builder_add_object(
  568. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  569. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
  570. SPA_PARAM_META_size,
  571. SPA_POD_Int(sizeof(struct spa_meta_region)));
  572. /* Cursor */
  573. params[1] = spa_pod_builder_add_object(
  574. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  575. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor),
  576. SPA_PARAM_META_size,
  577. SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64),
  578. CURSOR_META_SIZE(1, 1),
  579. CURSOR_META_SIZE(1024, 1024)));
  580. /* Buffer options */
  581. params[2] = spa_pod_builder_add_object(
  582. &pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
  583. SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types));
  584. pw_stream_update_params(obs_pw->stream, params, 3);
  585. obs_pw->negotiated = true;
  586. }
  587. static void on_state_changed_cb(void *user_data, enum pw_stream_state old,
  588. enum pw_stream_state state, const char *error)
  589. {
  590. UNUSED_PARAMETER(old);
  591. obs_pipewire_data *obs_pw = user_data;
  592. blog(LOG_INFO, "[pipewire] Stream %p state: \"%s\" (error: %s)",
  593. obs_pw->stream, pw_stream_state_as_string(state),
  594. error ? error : "none");
  595. }
  596. static const struct pw_stream_events stream_events = {
  597. PW_VERSION_STREAM_EVENTS,
  598. .state_changed = on_state_changed_cb,
  599. .param_changed = on_param_changed_cb,
  600. .process = on_process_cb,
  601. };
  602. static void on_core_info_cb(void *user_data, const struct pw_core_info *info)
  603. {
  604. obs_pipewire_data *obs_pw = user_data;
  605. update_pw_versions(obs_pw, info->version);
  606. }
  607. static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res,
  608. const char *message)
  609. {
  610. obs_pipewire_data *obs_pw = user_data;
  611. blog(LOG_ERROR, "[pipewire] Error id:%u seq:%d res:%d (%s): %s", id,
  612. seq, res, g_strerror(res), message);
  613. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  614. }
  615. static void on_core_done_cb(void *user_data, uint32_t id, int seq)
  616. {
  617. obs_pipewire_data *obs_pw = user_data;
  618. if (id == PW_ID_CORE && obs_pw->server_version_sync == seq)
  619. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  620. }
  621. static const struct pw_core_events core_events = {
  622. PW_VERSION_CORE_EVENTS,
  623. .info = on_core_info_cb,
  624. .done = on_core_done_cb,
  625. .error = on_core_error_cb,
  626. };
  627. static void play_pipewire_stream(obs_pipewire_data *obs_pw)
  628. {
  629. struct spa_pod_builder pod_builder;
  630. const struct spa_pod **params = NULL;
  631. uint32_t n_params;
  632. uint8_t params_buffer[2048];
  633. obs_pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  634. obs_pw->context = pw_context_new(
  635. pw_thread_loop_get_loop(obs_pw->thread_loop), NULL, 0);
  636. if (pw_thread_loop_start(obs_pw->thread_loop) < 0) {
  637. blog(LOG_WARNING, "Error starting threaded mainloop");
  638. return;
  639. }
  640. pw_thread_loop_lock(obs_pw->thread_loop);
  641. /* Core */
  642. obs_pw->core = pw_context_connect_fd(
  643. obs_pw->context, fcntl(obs_pw->pipewire_fd, F_DUPFD_CLOEXEC, 5),
  644. NULL, 0);
  645. if (!obs_pw->core) {
  646. blog(LOG_WARNING, "Error creating PipeWire core: %m");
  647. pw_thread_loop_unlock(obs_pw->thread_loop);
  648. return;
  649. }
  650. pw_core_add_listener(obs_pw->core, &obs_pw->core_listener, &core_events,
  651. obs_pw);
  652. /* Signal to renegotiate */
  653. obs_pw->reneg =
  654. pw_loop_add_event(pw_thread_loop_get_loop(obs_pw->thread_loop),
  655. renegotiate_format, obs_pw);
  656. blog(LOG_DEBUG, "[pipewire] registered event %p", obs_pw->reneg);
  657. // Dispatch to receive the info core event
  658. obs_pw->server_version_sync = pw_core_sync(obs_pw->core, PW_ID_CORE,
  659. obs_pw->server_version_sync);
  660. pw_thread_loop_wait(obs_pw->thread_loop);
  661. /* Stream */
  662. obs_pw->stream = pw_stream_new(
  663. obs_pw->core, "OBS Studio",
  664. pw_properties_new(PW_KEY_MEDIA_TYPE, "Video",
  665. PW_KEY_MEDIA_CATEGORY, "Capture",
  666. PW_KEY_MEDIA_ROLE, "Screen", NULL));
  667. pw_stream_add_listener(obs_pw->stream, &obs_pw->stream_listener,
  668. &stream_events, obs_pw);
  669. blog(LOG_INFO, "[pipewire] Created stream %p", obs_pw->stream);
  670. /* Stream parameters */
  671. pod_builder =
  672. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  673. obs_get_video_info(&obs_pw->video_info);
  674. if (!build_format_params(obs_pw, &pod_builder, &params, &n_params)) {
  675. pw_thread_loop_unlock(obs_pw->thread_loop);
  676. teardown_pipewire(obs_pw);
  677. return;
  678. }
  679. pw_stream_connect(
  680. obs_pw->stream, PW_DIRECTION_INPUT, obs_pw->pipewire_node,
  681. PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params,
  682. n_params);
  683. blog(LOG_INFO, "[pipewire] Playing stream %p", obs_pw->stream);
  684. pw_thread_loop_unlock(obs_pw->thread_loop);
  685. bfree(params);
  686. }
  687. /* obs_source_info methods */
  688. void *obs_pipewire_create(int pipewire_fd, int pipewire_node)
  689. {
  690. obs_pipewire_data *obs_pw = bzalloc(sizeof(obs_pipewire_data));
  691. obs_pw->pipewire_fd = pipewire_fd;
  692. obs_pw->pipewire_node = pipewire_node;
  693. init_format_info(obs_pw);
  694. play_pipewire_stream(obs_pw);
  695. return obs_pw;
  696. }
  697. void obs_pipewire_destroy(obs_pipewire_data *obs_pw)
  698. {
  699. if (!obs_pw)
  700. return;
  701. teardown_pipewire(obs_pw);
  702. destroy_session(obs_pw);
  703. clear_format_info(obs_pw);
  704. bfree(obs_pw);
  705. }
  706. void obs_pipewire_show(obs_pipewire_data *obs_pw)
  707. {
  708. if (obs_pw->stream)
  709. pw_stream_set_active(obs_pw->stream, true);
  710. }
  711. void obs_pipewire_hide(obs_pipewire_data *obs_pw)
  712. {
  713. if (obs_pw->stream)
  714. pw_stream_set_active(obs_pw->stream, false);
  715. }
  716. uint32_t obs_pipewire_get_width(obs_pipewire_data *obs_pw)
  717. {
  718. if (!obs_pw->negotiated)
  719. return 0;
  720. if (obs_pw->crop.valid)
  721. return obs_pw->crop.width;
  722. else
  723. return obs_pw->format.info.raw.size.width;
  724. }
  725. uint32_t obs_pipewire_get_height(obs_pipewire_data *obs_pw)
  726. {
  727. if (!obs_pw->negotiated)
  728. return 0;
  729. if (obs_pw->crop.valid)
  730. return obs_pw->crop.height;
  731. else
  732. return obs_pw->format.info.raw.size.height;
  733. }
  734. void obs_pipewire_video_render(obs_pipewire_data *obs_pw, gs_effect_t *effect)
  735. {
  736. gs_eparam_t *image;
  737. if (!obs_pw->texture)
  738. return;
  739. image = gs_effect_get_param_by_name(effect, "image");
  740. gs_effect_set_texture(image, obs_pw->texture);
  741. if (has_effective_crop(obs_pw)) {
  742. gs_draw_sprite_subregion(obs_pw->texture, 0, obs_pw->crop.x,
  743. obs_pw->crop.y, obs_pw->crop.width,
  744. obs_pw->crop.height);
  745. } else {
  746. gs_draw_sprite(obs_pw->texture, 0, 0, 0);
  747. }
  748. if (obs_pw->cursor.visible && obs_pw->cursor.valid &&
  749. obs_pw->cursor.texture) {
  750. float cursor_x = obs_pw->cursor.x - obs_pw->cursor.hotspot_x;
  751. float cursor_y = obs_pw->cursor.y - obs_pw->cursor.hotspot_y;
  752. gs_matrix_push();
  753. gs_matrix_translate3f(cursor_x, cursor_y, 0.0f);
  754. gs_effect_set_texture(image, obs_pw->cursor.texture);
  755. gs_draw_sprite(obs_pw->texture, 0, obs_pw->cursor.width,
  756. obs_pw->cursor.height);
  757. gs_matrix_pop();
  758. }
  759. }
  760. void obs_pipewire_set_cursor_visible(obs_pipewire_data *obs_pw,
  761. bool cursor_visible)
  762. {
  763. obs_pw->cursor.visible = cursor_visible;
  764. }