pipewire.c 26 KB

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