pipewire.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. };
  252. #define N_SUPPORTED_FORMATS \
  253. (sizeof(supported_formats) / sizeof(supported_formats[0]))
  254. static bool lookup_format_info_from_spa_format(
  255. uint32_t spa_format, uint32_t *out_drm_format,
  256. enum gs_color_format *out_gs_format, bool *out_swap_red_blue)
  257. {
  258. for (size_t i = 0; i < N_SUPPORTED_FORMATS; i++) {
  259. if (supported_formats[i].spa_format != spa_format)
  260. continue;
  261. if (out_drm_format)
  262. *out_drm_format = supported_formats[i].drm_format;
  263. if (out_gs_format)
  264. *out_gs_format = supported_formats[i].gs_format;
  265. if (out_swap_red_blue)
  266. *out_swap_red_blue = supported_formats[i].swap_red_blue;
  267. return true;
  268. }
  269. return false;
  270. }
  271. static void swap_texture_red_blue(gs_texture_t *texture)
  272. {
  273. GLuint gl_texure = *(GLuint *)gs_texture_get_obj(texture);
  274. glBindTexture(GL_TEXTURE_2D, gl_texure);
  275. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  276. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  277. glBindTexture(GL_TEXTURE_2D, 0);
  278. }
  279. static inline struct spa_pod *build_format(struct spa_pod_builder *b,
  280. struct obs_video_info *ovi,
  281. uint32_t format, uint64_t *modifiers,
  282. size_t modifier_count)
  283. {
  284. struct spa_pod_frame format_frame;
  285. /* Make an object of type SPA_TYPE_OBJECT_Format and id SPA_PARAM_EnumFormat.
  286. * The object type is important because it defines the properties that are
  287. * acceptable. The id gives more context about what the object is meant to
  288. * contain. In this case we enumerate supported formats. */
  289. spa_pod_builder_push_object(b, &format_frame, SPA_TYPE_OBJECT_Format,
  290. SPA_PARAM_EnumFormat);
  291. /* add media type and media subtype properties */
  292. spa_pod_builder_add(b, SPA_FORMAT_mediaType,
  293. SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
  294. spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype,
  295. SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
  296. /* formats */
  297. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
  298. /* modifier */
  299. if (modifier_count > 0) {
  300. struct spa_pod_frame modifier_frame;
  301. /* build an enumeration of modifiers */
  302. spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier,
  303. SPA_POD_PROP_FLAG_MANDATORY |
  304. SPA_POD_PROP_FLAG_DONT_FIXATE);
  305. spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum,
  306. 0);
  307. /* The first element of choice pods is the preferred value. Here
  308. * we arbitrarily pick the first modifier as the preferred one.
  309. */
  310. spa_pod_builder_long(b, modifiers[0]);
  311. /* modifiers from an array */
  312. for (uint32_t i = 0; i < modifier_count; i++)
  313. spa_pod_builder_long(b, modifiers[i]);
  314. spa_pod_builder_pop(b, &modifier_frame);
  315. }
  316. /* add size and framerate ranges */
  317. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size,
  318. SPA_POD_CHOICE_RANGE_Rectangle(
  319. &SPA_RECTANGLE(320, 240), // Arbitrary
  320. &SPA_RECTANGLE(1, 1),
  321. &SPA_RECTANGLE(8192, 4320)),
  322. SPA_FORMAT_VIDEO_framerate,
  323. SPA_POD_CHOICE_RANGE_Fraction(
  324. &SPA_FRACTION(ovi->fps_num, ovi->fps_den),
  325. &SPA_FRACTION(0, 1), &SPA_FRACTION(360, 1)),
  326. 0);
  327. return spa_pod_builder_pop(b, &format_frame);
  328. }
  329. static bool build_format_params(obs_pipewire *obs_pw,
  330. struct spa_pod_builder *pod_builder,
  331. const struct spa_pod ***param_list,
  332. uint32_t *n_params)
  333. {
  334. uint32_t params_count = 0;
  335. const struct spa_pod **params;
  336. params =
  337. bzalloc(2 * obs_pw->format_info.num * sizeof(struct spa_pod *));
  338. if (!params) {
  339. blog(LOG_ERROR,
  340. "[pipewire] Failed to allocate memory for param pointers");
  341. return false;
  342. }
  343. if (!check_pw_version(&obs_pw->server_version, 0, 3, 33))
  344. goto build_shm;
  345. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  346. if (obs_pw->format_info.array[i].modifiers.num == 0) {
  347. continue;
  348. }
  349. params[params_count++] = build_format(
  350. pod_builder, &obs_pw->video_info,
  351. obs_pw->format_info.array[i].spa_format,
  352. obs_pw->format_info.array[i].modifiers.array,
  353. obs_pw->format_info.array[i].modifiers.num);
  354. }
  355. build_shm:
  356. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  357. params[params_count++] = build_format(
  358. pod_builder, &obs_pw->video_info,
  359. obs_pw->format_info.array[i].spa_format, NULL, 0);
  360. }
  361. *param_list = params;
  362. *n_params = params_count;
  363. return true;
  364. }
  365. static bool drm_format_available(uint32_t drm_format, uint32_t *drm_formats,
  366. size_t n_drm_formats)
  367. {
  368. for (size_t j = 0; j < n_drm_formats; j++) {
  369. if (drm_format == drm_formats[j]) {
  370. return true;
  371. }
  372. }
  373. return false;
  374. }
  375. static void init_format_info(obs_pipewire *obs_pw)
  376. {
  377. da_init(obs_pw->format_info);
  378. obs_enter_graphics();
  379. enum gs_dmabuf_flags dmabuf_flags;
  380. uint32_t *drm_formats = NULL;
  381. size_t n_drm_formats;
  382. bool capabilities_queried = gs_query_dmabuf_capabilities(
  383. &dmabuf_flags, &drm_formats, &n_drm_formats);
  384. for (size_t i = 0; i < N_SUPPORTED_FORMATS; i++) {
  385. struct format_info *info;
  386. info = da_push_back_new(obs_pw->format_info);
  387. da_init(info->modifiers);
  388. info->spa_format = supported_formats[i].spa_format;
  389. info->drm_format = supported_formats[i].drm_format;
  390. if (!capabilities_queried ||
  391. !drm_format_available(supported_formats[i].drm_format,
  392. drm_formats, n_drm_formats))
  393. continue;
  394. size_t n_modifiers;
  395. uint64_t *modifiers = NULL;
  396. if (gs_query_dmabuf_modifiers_for_format(
  397. supported_formats[i].drm_format, &modifiers,
  398. &n_modifiers)) {
  399. da_push_back_array(info->modifiers, modifiers,
  400. n_modifiers);
  401. }
  402. bfree(modifiers);
  403. if (dmabuf_flags &
  404. GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED) {
  405. uint64_t modifier_implicit = DRM_FORMAT_MOD_INVALID;
  406. da_push_back(info->modifiers, &modifier_implicit);
  407. }
  408. }
  409. obs_leave_graphics();
  410. bfree(drm_formats);
  411. }
  412. static void clear_format_info(obs_pipewire *obs_pw)
  413. {
  414. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  415. da_free(obs_pw->format_info.array[i].modifiers);
  416. }
  417. da_free(obs_pw->format_info);
  418. }
  419. static void remove_modifier_from_format(obs_pipewire *obs_pw,
  420. uint32_t spa_format, uint64_t modifier)
  421. {
  422. for (size_t i = 0; i < obs_pw->format_info.num; i++) {
  423. if (obs_pw->format_info.array[i].spa_format != spa_format)
  424. continue;
  425. if (!check_pw_version(&obs_pw->server_version, 0, 3, 40)) {
  426. da_erase_range(
  427. obs_pw->format_info.array[i].modifiers, 0,
  428. obs_pw->format_info.array[i].modifiers.num - 1);
  429. continue;
  430. }
  431. int idx = da_find(obs_pw->format_info.array[i].modifiers,
  432. &modifier, 0);
  433. while (idx != -1) {
  434. da_erase(obs_pw->format_info.array[i].modifiers, idx);
  435. idx = da_find(obs_pw->format_info.array[i].modifiers,
  436. &modifier, 0);
  437. }
  438. }
  439. }
  440. static void renegotiate_format(void *data, uint64_t expirations)
  441. {
  442. UNUSED_PARAMETER(expirations);
  443. obs_pipewire *obs_pw = (obs_pipewire *)data;
  444. const struct spa_pod **params = NULL;
  445. blog(LOG_INFO, "[pipewire] Renegotiating stream");
  446. pw_thread_loop_lock(obs_pw->thread_loop);
  447. uint8_t params_buffer[2048];
  448. struct spa_pod_builder pod_builder =
  449. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  450. uint32_t n_params;
  451. if (!build_format_params(obs_pw, &pod_builder, &params, &n_params)) {
  452. teardown_pipewire(obs_pw);
  453. pw_thread_loop_unlock(obs_pw->thread_loop);
  454. return;
  455. }
  456. pw_stream_update_params(obs_pw->stream, params, n_params);
  457. pw_thread_loop_unlock(obs_pw->thread_loop);
  458. bfree(params);
  459. }
  460. /* ------------------------------------------------- */
  461. static void on_process_cb(void *user_data)
  462. {
  463. obs_pipewire *obs_pw = user_data;
  464. struct spa_meta_cursor *cursor;
  465. uint32_t drm_format;
  466. struct spa_meta_header *header;
  467. struct spa_meta_region *region;
  468. struct spa_meta_videotransform *video_transform;
  469. struct spa_buffer *buffer;
  470. struct pw_buffer *b;
  471. bool swap_red_blue = false;
  472. bool has_buffer = true;
  473. /* Find the most recent buffer */
  474. b = NULL;
  475. while (true) {
  476. struct pw_buffer *aux =
  477. pw_stream_dequeue_buffer(obs_pw->stream);
  478. if (!aux)
  479. break;
  480. if (b)
  481. pw_stream_queue_buffer(obs_pw->stream, b);
  482. b = aux;
  483. }
  484. if (!b) {
  485. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  486. return;
  487. }
  488. buffer = b->buffer;
  489. header = spa_buffer_find_meta_data(buffer, SPA_META_Header,
  490. sizeof(*header));
  491. if (header && (header->flags & SPA_META_HEADER_FLAG_CORRUPTED) > 0) {
  492. blog(LOG_ERROR, "[pipewire] buffer is corrupt");
  493. pw_stream_queue_buffer(obs_pw->stream, b);
  494. return;
  495. }
  496. obs_enter_graphics();
  497. // Workaround for kwin behaviour pre 5.27.5
  498. // Workaround for mutter behaviour pre GNOME 43
  499. // Only check this if !SPA_META_Header, once supported platforms update.
  500. has_buffer = buffer->datas[0].chunk->size != 0;
  501. if (!has_buffer)
  502. goto read_metadata;
  503. if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
  504. uint32_t planes = buffer->n_datas;
  505. uint32_t offsets[planes];
  506. uint32_t strides[planes];
  507. uint64_t modifiers[planes];
  508. int fds[planes];
  509. bool use_modifiers;
  510. bool corrupt = false;
  511. #ifdef DEBUG_PIPEWIRE
  512. blog(LOG_DEBUG,
  513. "[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
  514. buffer->datas[0].fd, buffer->datas[0].chunk->stride,
  515. buffer->datas[0].chunk->offset,
  516. obs_pw->format.info.raw.size.width,
  517. obs_pw->format.info.raw.size.height);
  518. #endif
  519. if (!lookup_format_info_from_spa_format(
  520. obs_pw->format.info.raw.format, &drm_format, NULL,
  521. NULL)) {
  522. blog(LOG_ERROR,
  523. "[pipewire] unsupported DMA buffer format: %d",
  524. obs_pw->format.info.raw.format);
  525. goto read_metadata;
  526. }
  527. for (uint32_t plane = 0; plane < planes; plane++) {
  528. fds[plane] = buffer->datas[plane].fd;
  529. offsets[plane] = buffer->datas[plane].chunk->offset;
  530. strides[plane] = buffer->datas[plane].chunk->stride;
  531. modifiers[plane] = obs_pw->format.info.raw.modifier;
  532. corrupt |= (buffer->datas[plane].chunk->flags &
  533. SPA_CHUNK_FLAG_CORRUPTED) > 0;
  534. }
  535. if (corrupt) {
  536. blog(LOG_DEBUG,
  537. "[pipewire] buffer contains corrupted data");
  538. goto read_metadata;
  539. }
  540. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  541. use_modifiers = obs_pw->format.info.raw.modifier !=
  542. DRM_FORMAT_MOD_INVALID;
  543. obs_pw->texture = gs_texture_create_from_dmabuf(
  544. obs_pw->format.info.raw.size.width,
  545. obs_pw->format.info.raw.size.height, drm_format,
  546. GS_BGRX, planes, fds, strides, offsets,
  547. use_modifiers ? modifiers : NULL);
  548. if (obs_pw->texture == NULL) {
  549. remove_modifier_from_format(
  550. obs_pw, obs_pw->format.info.raw.format,
  551. obs_pw->format.info.raw.modifier);
  552. pw_loop_signal_event(
  553. pw_thread_loop_get_loop(obs_pw->thread_loop),
  554. obs_pw->reneg);
  555. }
  556. } else {
  557. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  558. enum gs_color_format gs_format;
  559. if (!lookup_format_info_from_spa_format(
  560. obs_pw->format.info.raw.format, NULL, &gs_format,
  561. &swap_red_blue)) {
  562. blog(LOG_ERROR,
  563. "[pipewire] unsupported buffer format: %d",
  564. obs_pw->format.info.raw.format);
  565. goto read_metadata;
  566. }
  567. if ((buffer->datas[0].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) >
  568. 0) {
  569. blog(LOG_DEBUG,
  570. "[pipewire] buffer contains corrupted data");
  571. goto read_metadata;
  572. }
  573. if (buffer->datas[0].chunk->size == 0) {
  574. blog(LOG_DEBUG,
  575. "[pipewire] buffer contains empty data");
  576. goto read_metadata;
  577. }
  578. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  579. obs_pw->texture = gs_texture_create(
  580. obs_pw->format.info.raw.size.width,
  581. obs_pw->format.info.raw.size.height, gs_format, 1,
  582. (const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
  583. }
  584. if (swap_red_blue)
  585. swap_texture_red_blue(obs_pw->texture);
  586. /* Video Crop */
  587. region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop,
  588. sizeof(*region));
  589. if (region && spa_meta_region_is_valid(region)) {
  590. #ifdef DEBUG_PIPEWIRE
  591. blog(LOG_DEBUG,
  592. "[pipewire] Crop Region available (%dx%d+%d+%d)",
  593. region->region.position.x, region->region.position.y,
  594. region->region.size.width, region->region.size.height);
  595. #endif
  596. obs_pw->crop.x = region->region.position.x;
  597. obs_pw->crop.y = region->region.position.y;
  598. obs_pw->crop.width = region->region.size.width;
  599. obs_pw->crop.height = region->region.size.height;
  600. obs_pw->crop.valid = true;
  601. } else {
  602. obs_pw->crop.valid = false;
  603. }
  604. /* Video Transform */
  605. video_transform = spa_buffer_find_meta_data(
  606. buffer, SPA_META_VideoTransform, sizeof(*video_transform));
  607. if (video_transform)
  608. obs_pw->transform = video_transform->transform;
  609. else
  610. obs_pw->transform = SPA_META_TRANSFORMATION_None;
  611. read_metadata:
  612. /* Cursor */
  613. cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor,
  614. sizeof(*cursor));
  615. obs_pw->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
  616. if (obs_pw->cursor.visible && obs_pw->cursor.valid) {
  617. struct spa_meta_bitmap *bitmap = NULL;
  618. enum gs_color_format gs_format;
  619. if (cursor->bitmap_offset)
  620. bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset,
  621. struct spa_meta_bitmap);
  622. if (bitmap)
  623. g_clear_pointer(&obs_pw->cursor.texture,
  624. gs_texture_destroy);
  625. if (bitmap && bitmap->size.width > 0 &&
  626. bitmap->size.height > 0 &&
  627. lookup_format_info_from_spa_format(
  628. bitmap->format, NULL, &gs_format, &swap_red_blue)) {
  629. const uint8_t *bitmap_data;
  630. bitmap_data =
  631. SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
  632. obs_pw->cursor.hotspot_x = cursor->hotspot.x;
  633. obs_pw->cursor.hotspot_y = cursor->hotspot.y;
  634. obs_pw->cursor.width = bitmap->size.width;
  635. obs_pw->cursor.height = bitmap->size.height;
  636. assert(obs_pw->cursor.texture == NULL);
  637. obs_pw->cursor.texture = gs_texture_create(
  638. obs_pw->cursor.width, obs_pw->cursor.height,
  639. gs_format, 1, &bitmap_data, GS_DYNAMIC);
  640. if (swap_red_blue)
  641. swap_texture_red_blue(obs_pw->cursor.texture);
  642. }
  643. obs_pw->cursor.x = cursor->position.x;
  644. obs_pw->cursor.y = cursor->position.y;
  645. }
  646. pw_stream_queue_buffer(obs_pw->stream, b);
  647. obs_leave_graphics();
  648. }
  649. static void on_param_changed_cb(void *user_data, uint32_t id,
  650. const struct spa_pod *param)
  651. {
  652. obs_pipewire *obs_pw = user_data;
  653. struct spa_pod_builder pod_builder;
  654. const struct spa_pod *params[5];
  655. uint32_t n_params = 0;
  656. uint32_t buffer_types;
  657. uint8_t params_buffer[1024];
  658. int result;
  659. if (!param || id != SPA_PARAM_Format)
  660. return;
  661. result = spa_format_parse(param, &obs_pw->format.media_type,
  662. &obs_pw->format.media_subtype);
  663. if (result < 0)
  664. return;
  665. if (obs_pw->format.media_type != SPA_MEDIA_TYPE_video ||
  666. obs_pw->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
  667. return;
  668. spa_format_video_raw_parse(param, &obs_pw->format.info.raw);
  669. buffer_types = 1 << SPA_DATA_MemPtr;
  670. bool has_modifier =
  671. spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) !=
  672. NULL;
  673. if (has_modifier || check_pw_version(&obs_pw->server_version, 0, 3, 24))
  674. buffer_types |= 1 << SPA_DATA_DmaBuf;
  675. blog(LOG_INFO, "[pipewire] Negotiated format:");
  676. blog(LOG_INFO, "[pipewire] Format: %d (%s)",
  677. obs_pw->format.info.raw.format,
  678. spa_debug_type_find_name(spa_type_video_format,
  679. obs_pw->format.info.raw.format));
  680. if (has_modifier) {
  681. blog(LOG_INFO, "[pipewire] Modifier: 0x%" PRIx64,
  682. obs_pw->format.info.raw.modifier);
  683. }
  684. blog(LOG_INFO, "[pipewire] Size: %dx%d",
  685. obs_pw->format.info.raw.size.width,
  686. obs_pw->format.info.raw.size.height);
  687. blog(LOG_INFO, "[pipewire] Framerate: %d/%d",
  688. obs_pw->format.info.raw.framerate.num,
  689. obs_pw->format.info.raw.framerate.denom);
  690. /* Video crop */
  691. pod_builder =
  692. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  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_VideoCrop),
  696. SPA_PARAM_META_size,
  697. SPA_POD_Int(sizeof(struct spa_meta_region)));
  698. /* Cursor */
  699. params[n_params++] = spa_pod_builder_add_object(
  700. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  701. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor),
  702. SPA_PARAM_META_size,
  703. SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64),
  704. CURSOR_META_SIZE(1, 1),
  705. CURSOR_META_SIZE(1024, 1024)));
  706. /* Buffer options */
  707. params[n_params++] = spa_pod_builder_add_object(
  708. &pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
  709. SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types));
  710. /* Meta header */
  711. params[n_params++] = spa_pod_builder_add_object(
  712. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  713. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
  714. SPA_PARAM_META_size,
  715. SPA_POD_Int(sizeof(struct spa_meta_header)));
  716. #if PW_CHECK_VERSION(0, 3, 62)
  717. if (check_pw_version(&obs_pw->server_version, 0, 3, 62)) {
  718. /* Video transformation */
  719. params[n_params++] = spa_pod_builder_add_object(
  720. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  721. SPA_PARAM_META_type,
  722. SPA_POD_Id(SPA_META_VideoTransform),
  723. SPA_PARAM_META_size,
  724. SPA_POD_Int(sizeof(struct spa_meta_videotransform)));
  725. }
  726. #endif
  727. pw_stream_update_params(obs_pw->stream, params, n_params);
  728. obs_pw->negotiated = true;
  729. }
  730. static void on_state_changed_cb(void *user_data, enum pw_stream_state old,
  731. enum pw_stream_state state, const char *error)
  732. {
  733. UNUSED_PARAMETER(old);
  734. obs_pipewire *obs_pw = user_data;
  735. blog(LOG_INFO, "[pipewire] Stream %p state: \"%s\" (error: %s)",
  736. obs_pw->stream, pw_stream_state_as_string(state),
  737. error ? error : "none");
  738. }
  739. static const struct pw_stream_events stream_events = {
  740. PW_VERSION_STREAM_EVENTS,
  741. .state_changed = on_state_changed_cb,
  742. .param_changed = on_param_changed_cb,
  743. .process = on_process_cb,
  744. };
  745. static void on_core_info_cb(void *user_data, const struct pw_core_info *info)
  746. {
  747. obs_pipewire *obs_pw = user_data;
  748. update_pw_versions(obs_pw, info->version);
  749. }
  750. static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res,
  751. const char *message)
  752. {
  753. obs_pipewire *obs_pw = user_data;
  754. blog(LOG_ERROR, "[pipewire] Error id:%u seq:%d res:%d (%s): %s", id,
  755. seq, res, g_strerror(res), message);
  756. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  757. }
  758. static void on_core_done_cb(void *user_data, uint32_t id, int seq)
  759. {
  760. obs_pipewire *obs_pw = user_data;
  761. if (id == PW_ID_CORE && obs_pw->server_version_sync == seq)
  762. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  763. }
  764. static const struct pw_core_events core_events = {
  765. PW_VERSION_CORE_EVENTS,
  766. .info = on_core_info_cb,
  767. .done = on_core_done_cb,
  768. .error = on_core_error_cb,
  769. };
  770. /* obs_source_info methods */
  771. obs_pipewire *obs_pipewire_create(int pipewire_fd)
  772. {
  773. obs_pipewire *obs_pw;
  774. obs_pw = bzalloc(sizeof(obs_pipewire));
  775. obs_pw->pipewire_fd = pipewire_fd;
  776. init_format_info(obs_pw);
  777. obs_pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  778. obs_pw->context = pw_context_new(
  779. pw_thread_loop_get_loop(obs_pw->thread_loop), NULL, 0);
  780. if (pw_thread_loop_start(obs_pw->thread_loop) < 0) {
  781. blog(LOG_WARNING, "Error starting threaded mainloop");
  782. bfree(obs_pw);
  783. return NULL;
  784. }
  785. pw_thread_loop_lock(obs_pw->thread_loop);
  786. /* Core */
  787. obs_pw->core = pw_context_connect_fd(
  788. obs_pw->context, fcntl(obs_pw->pipewire_fd, F_DUPFD_CLOEXEC, 5),
  789. NULL, 0);
  790. if (!obs_pw->core) {
  791. blog(LOG_WARNING, "Error creating PipeWire core: %m");
  792. pw_thread_loop_unlock(obs_pw->thread_loop);
  793. bfree(obs_pw);
  794. return NULL;
  795. }
  796. pw_core_add_listener(obs_pw->core, &obs_pw->core_listener, &core_events,
  797. obs_pw);
  798. // Dispatch to receive the info core event
  799. obs_pw->server_version_sync = pw_core_sync(obs_pw->core, PW_ID_CORE,
  800. obs_pw->server_version_sync);
  801. pw_thread_loop_wait(obs_pw->thread_loop);
  802. pw_thread_loop_unlock(obs_pw->thread_loop);
  803. return obs_pw;
  804. }
  805. void obs_pipewire_destroy(obs_pipewire *obs_pw)
  806. {
  807. if (!obs_pw)
  808. return;
  809. teardown_pipewire(obs_pw);
  810. destroy_session(obs_pw);
  811. clear_format_info(obs_pw);
  812. bfree(obs_pw);
  813. }
  814. void obs_pipewire_connect_stream(obs_pipewire *obs_pw, int pipewire_node,
  815. const char *stream_name,
  816. struct pw_properties *stream_properties)
  817. {
  818. struct spa_pod_builder pod_builder;
  819. const struct spa_pod **params = NULL;
  820. uint32_t n_params;
  821. uint8_t params_buffer[2048];
  822. pw_thread_loop_lock(obs_pw->thread_loop);
  823. /* Signal to renegotiate */
  824. obs_pw->reneg =
  825. pw_loop_add_event(pw_thread_loop_get_loop(obs_pw->thread_loop),
  826. renegotiate_format, obs_pw);
  827. blog(LOG_DEBUG, "[pipewire] registered event %p", obs_pw->reneg);
  828. /* Stream */
  829. obs_pw->stream =
  830. pw_stream_new(obs_pw->core, stream_name, stream_properties);
  831. pw_stream_add_listener(obs_pw->stream, &obs_pw->stream_listener,
  832. &stream_events, obs_pw);
  833. blog(LOG_INFO, "[pipewire] Created stream %p", obs_pw->stream);
  834. /* Stream parameters */
  835. pod_builder =
  836. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  837. obs_get_video_info(&obs_pw->video_info);
  838. if (!build_format_params(obs_pw, &pod_builder, &params, &n_params)) {
  839. pw_thread_loop_unlock(obs_pw->thread_loop);
  840. return;
  841. }
  842. pw_stream_connect(obs_pw->stream, PW_DIRECTION_INPUT, pipewire_node,
  843. PW_STREAM_FLAG_AUTOCONNECT |
  844. PW_STREAM_FLAG_MAP_BUFFERS,
  845. params, n_params);
  846. blog(LOG_INFO, "[pipewire] Playing stream %p", obs_pw->stream);
  847. pw_thread_loop_unlock(obs_pw->thread_loop);
  848. bfree(params);
  849. }
  850. void obs_pipewire_show(obs_pipewire *obs_pw)
  851. {
  852. if (obs_pw->stream)
  853. pw_stream_set_active(obs_pw->stream, true);
  854. }
  855. void obs_pipewire_hide(obs_pipewire *obs_pw)
  856. {
  857. if (obs_pw->stream)
  858. pw_stream_set_active(obs_pw->stream, false);
  859. }
  860. uint32_t obs_pipewire_get_width(obs_pipewire *obs_pw)
  861. {
  862. bool has_crop;
  863. if (!obs_pw->negotiated)
  864. return 0;
  865. has_crop = has_effective_crop(obs_pw);
  866. switch (obs_pw->transform) {
  867. case SPA_META_TRANSFORMATION_Flipped:
  868. case SPA_META_TRANSFORMATION_None:
  869. case SPA_META_TRANSFORMATION_Flipped180:
  870. case SPA_META_TRANSFORMATION_180:
  871. return has_crop ? obs_pw->crop.width
  872. : obs_pw->format.info.raw.size.width;
  873. case SPA_META_TRANSFORMATION_Flipped90:
  874. case SPA_META_TRANSFORMATION_90:
  875. case SPA_META_TRANSFORMATION_Flipped270:
  876. case SPA_META_TRANSFORMATION_270:
  877. return has_crop ? obs_pw->crop.height
  878. : obs_pw->format.info.raw.size.height;
  879. }
  880. }
  881. uint32_t obs_pipewire_get_height(obs_pipewire *obs_pw)
  882. {
  883. bool has_crop;
  884. if (!obs_pw->negotiated)
  885. return 0;
  886. has_crop = has_effective_crop(obs_pw);
  887. switch (obs_pw->transform) {
  888. case SPA_META_TRANSFORMATION_Flipped:
  889. case SPA_META_TRANSFORMATION_None:
  890. case SPA_META_TRANSFORMATION_Flipped180:
  891. case SPA_META_TRANSFORMATION_180:
  892. return has_crop ? obs_pw->crop.height
  893. : obs_pw->format.info.raw.size.height;
  894. case SPA_META_TRANSFORMATION_Flipped90:
  895. case SPA_META_TRANSFORMATION_90:
  896. case SPA_META_TRANSFORMATION_Flipped270:
  897. case SPA_META_TRANSFORMATION_270:
  898. return has_crop ? obs_pw->crop.width
  899. : obs_pw->format.info.raw.size.width;
  900. }
  901. }
  902. void obs_pipewire_video_render(obs_pipewire *obs_pw, gs_effect_t *effect)
  903. {
  904. bool rotated;
  905. int flip = 0;
  906. gs_eparam_t *image;
  907. if (!obs_pw->texture)
  908. return;
  909. image = gs_effect_get_param_by_name(effect, "image");
  910. gs_effect_set_texture(image, obs_pw->texture);
  911. rotated = push_rotation(obs_pw);
  912. flip = get_buffer_flip(obs_pw);
  913. /* There is a SPA_VIDEO_FLAG_PREMULTIPLIED_ALPHA flag, but it does not
  914. * seem to be fully implemented nor ever set. Just assume premultiplied
  915. * always, which seems to be the default convention.
  916. *
  917. * See https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3126
  918. *
  919. * The cursor bitmap alpha mode is also not specified, and the
  920. * convention there also seems to be premultiplied. */
  921. gs_blend_state_push();
  922. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  923. if (has_effective_crop(obs_pw)) {
  924. gs_draw_sprite_subregion(obs_pw->texture, flip, obs_pw->crop.x,
  925. obs_pw->crop.y, obs_pw->crop.width,
  926. obs_pw->crop.height);
  927. } else {
  928. gs_draw_sprite(obs_pw->texture, flip, 0, 0);
  929. }
  930. if (rotated)
  931. gs_matrix_pop();
  932. if (obs_pw->cursor.visible && obs_pw->cursor.valid &&
  933. obs_pw->cursor.texture) {
  934. float cursor_x = obs_pw->cursor.x - obs_pw->cursor.hotspot_x;
  935. float cursor_y = obs_pw->cursor.y - obs_pw->cursor.hotspot_y;
  936. gs_matrix_push();
  937. gs_matrix_translate3f(cursor_x, cursor_y, 0.0f);
  938. gs_effect_set_texture(image, obs_pw->cursor.texture);
  939. gs_draw_sprite(obs_pw->texture, 0, obs_pw->cursor.width,
  940. obs_pw->cursor.height);
  941. gs_matrix_pop();
  942. }
  943. gs_blend_state_pop();
  944. }
  945. void obs_pipewire_set_cursor_visible(obs_pipewire *obs_pw, bool cursor_visible)
  946. {
  947. obs_pw->cursor.visible = cursor_visible;
  948. }