pipewire.c 31 KB

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