pipewire.c 25 KB

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