pipewire.c 30 KB

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