1
0

pipewire.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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 "formats.h"
  22. #include <util/darray.h>
  23. #include <gio/gio.h>
  24. #include <gio/gunixfdlist.h>
  25. #include <glib/gstdio.h>
  26. #include <fcntl.h>
  27. #include <glad/glad.h>
  28. #include <libdrm/drm_fourcc.h>
  29. #include <pipewire/pipewire.h>
  30. #include <spa/param/video/format-utils.h>
  31. #include <spa/buffer/meta.h>
  32. #include <spa/debug/format.h>
  33. #include <spa/debug/types.h>
  34. #include <spa/param/video/type-info.h>
  35. #include <spa/utils/result.h>
  36. //#define DEBUG_PIPEWIRE
  37. #if !PW_CHECK_VERSION(0, 3, 62)
  38. enum spa_meta_videotransform_value {
  39. SPA_META_TRANSFORMATION_None = 0, /**< no transform */
  40. SPA_META_TRANSFORMATION_90, /**< 90 degree counter-clockwise */
  41. SPA_META_TRANSFORMATION_180, /**< 180 degree counter-clockwise */
  42. SPA_META_TRANSFORMATION_270, /**< 270 degree counter-clockwise */
  43. SPA_META_TRANSFORMATION_Flipped, /**< 180 degree flipped around the vertical axis. Equivalent
  44. * to a reflexion through the vertical line splitting the
  45. * bufffer in two equal sized parts */
  46. SPA_META_TRANSFORMATION_Flipped90, /**< flip then rotate around 90 degree counter-clockwise */
  47. SPA_META_TRANSFORMATION_Flipped180, /**< flip then rotate around 180 degree counter-clockwise */
  48. SPA_META_TRANSFORMATION_Flipped270, /**< flip then rotate around 270 degree counter-clockwise */
  49. };
  50. #define SPA_META_VideoTransform 8
  51. struct spa_meta_videotransform {
  52. uint32_t transform; /**< orientation transformation that was applied to the buffer */
  53. };
  54. #endif
  55. #define CURSOR_META_SIZE(width, height) \
  56. (sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + width * height * 4)
  57. struct obs_pw_version {
  58. int major;
  59. int minor;
  60. int micro;
  61. };
  62. struct format_info {
  63. uint32_t spa_format;
  64. uint32_t drm_format;
  65. DARRAY(uint64_t) modifiers;
  66. };
  67. struct _obs_pipewire {
  68. int pipewire_fd;
  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 sync_id;
  74. struct obs_pw_version server_version;
  75. struct pw_registry *registry;
  76. struct spa_hook registry_listener;
  77. GPtrArray *streams;
  78. };
  79. struct _obs_pipewire_stream {
  80. obs_pipewire *obs_pw;
  81. obs_source_t *source;
  82. gs_texture_t *texture;
  83. struct pw_stream *stream;
  84. struct spa_hook stream_listener;
  85. struct spa_source *reneg;
  86. struct spa_video_info format;
  87. enum spa_meta_videotransform_value transform;
  88. struct {
  89. bool valid;
  90. int x, y;
  91. uint32_t width, height;
  92. } crop;
  93. struct {
  94. bool visible;
  95. bool valid;
  96. int x, y;
  97. int hotspot_x, hotspot_y;
  98. int width, height;
  99. gs_texture_t *texture;
  100. } cursor;
  101. struct obs_video_info video_info;
  102. bool negotiated;
  103. DARRAY(struct format_info) format_info;
  104. struct {
  105. struct spa_rectangle rect;
  106. bool set;
  107. } resolution;
  108. struct {
  109. struct spa_fraction fraction;
  110. bool set;
  111. } framerate;
  112. struct {
  113. int acquire_syncobj_fd;
  114. int release_syncobj_fd;
  115. uint64_t acquire_point;
  116. uint64_t release_point;
  117. bool release_point_will_signal;
  118. bool set;
  119. } sync;
  120. };
  121. /* auxiliary methods */
  122. static bool parse_pw_version(struct obs_pw_version *dst, const char *version)
  123. {
  124. int n_matches = sscanf(version, "%d.%d.%d", &dst->major, &dst->minor, &dst->micro);
  125. return n_matches == 3;
  126. }
  127. static bool check_pw_version(const struct obs_pw_version *pw_version, int major, int minor, int micro)
  128. {
  129. if (pw_version->major != major)
  130. return pw_version->major > major;
  131. if (pw_version->minor != minor)
  132. return pw_version->minor > minor;
  133. return pw_version->micro >= micro;
  134. }
  135. static void update_pw_versions(obs_pipewire *obs_pw, const char *version)
  136. {
  137. blog(LOG_INFO, "[pipewire] Server version: %s", version);
  138. blog(LOG_INFO, "[pipewire] Library version: %s", pw_get_library_version());
  139. blog(LOG_INFO, "[pipewire] Header version: %s", pw_get_headers_version());
  140. if (!parse_pw_version(&obs_pw->server_version, version))
  141. blog(LOG_WARNING, "[pipewire] failed to parse server version");
  142. }
  143. static void teardown_pipewire(obs_pipewire *obs_pw)
  144. {
  145. if (obs_pw->thread_loop) {
  146. pw_thread_loop_wait(obs_pw->thread_loop);
  147. pw_thread_loop_stop(obs_pw->thread_loop);
  148. }
  149. if (obs_pw->registry) {
  150. pw_proxy_destroy((struct pw_proxy *)obs_pw->registry);
  151. obs_pw->registry = NULL;
  152. }
  153. g_clear_pointer(&obs_pw->context, pw_context_destroy);
  154. g_clear_pointer(&obs_pw->thread_loop, pw_thread_loop_destroy);
  155. if (obs_pw->pipewire_fd > 0) {
  156. close(obs_pw->pipewire_fd);
  157. obs_pw->pipewire_fd = 0;
  158. }
  159. }
  160. static inline bool has_effective_crop(obs_pipewire_stream *obs_pw_stream)
  161. {
  162. return obs_pw_stream->crop.valid && (obs_pw_stream->crop.x != 0 || obs_pw_stream->crop.y != 0 ||
  163. obs_pw_stream->crop.width < obs_pw_stream->format.info.raw.size.width ||
  164. obs_pw_stream->crop.height < obs_pw_stream->format.info.raw.size.height);
  165. }
  166. static int get_buffer_flip(obs_pipewire_stream *obs_pw_stream)
  167. {
  168. int flip = 0;
  169. switch (obs_pw_stream->transform) {
  170. case SPA_META_TRANSFORMATION_Flipped:
  171. case SPA_META_TRANSFORMATION_Flipped180:
  172. flip = GS_FLIP_U;
  173. break;
  174. case SPA_META_TRANSFORMATION_Flipped90:
  175. case SPA_META_TRANSFORMATION_Flipped270:
  176. flip = GS_FLIP_V;
  177. break;
  178. case SPA_META_TRANSFORMATION_None:
  179. case SPA_META_TRANSFORMATION_90:
  180. case SPA_META_TRANSFORMATION_180:
  181. case SPA_META_TRANSFORMATION_270:
  182. break;
  183. }
  184. return flip;
  185. }
  186. static bool push_rotation(obs_pipewire_stream *obs_pw_stream)
  187. {
  188. double offset_x = 0;
  189. double offset_y = 0;
  190. double rotation = 0;
  191. bool has_crop;
  192. has_crop = has_effective_crop(obs_pw_stream);
  193. switch (obs_pw_stream->transform) {
  194. case SPA_META_TRANSFORMATION_Flipped:
  195. case SPA_META_TRANSFORMATION_None:
  196. rotation = 0;
  197. break;
  198. case SPA_META_TRANSFORMATION_Flipped90:
  199. case SPA_META_TRANSFORMATION_90:
  200. rotation = 90;
  201. offset_x = 0;
  202. offset_y = has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  203. break;
  204. case SPA_META_TRANSFORMATION_Flipped180:
  205. case SPA_META_TRANSFORMATION_180:
  206. rotation = 180;
  207. offset_x = has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  208. offset_y = has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  209. break;
  210. case SPA_META_TRANSFORMATION_Flipped270:
  211. case SPA_META_TRANSFORMATION_270:
  212. rotation = 270;
  213. offset_x = has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  214. offset_y = 0;
  215. break;
  216. }
  217. if (rotation != 0) {
  218. gs_matrix_push();
  219. gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(rotation));
  220. gs_matrix_translate3f(-offset_x, -offset_y, 0.0f);
  221. }
  222. return rotation != 0;
  223. }
  224. static const uint32_t supported_formats_async[] = {
  225. SPA_VIDEO_FORMAT_RGBA,
  226. SPA_VIDEO_FORMAT_YUY2,
  227. };
  228. #define N_SUPPORTED_FORMATS_ASYNC (sizeof(supported_formats_async) / sizeof(supported_formats_async[0]))
  229. static const uint32_t supported_formats_sync[] = {
  230. SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_RGBA, SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_RGBx,
  231. #if PW_CHECK_VERSION(0, 3, 41)
  232. SPA_VIDEO_FORMAT_ABGR_210LE, SPA_VIDEO_FORMAT_xBGR_210LE,
  233. #endif
  234. };
  235. #define N_SUPPORTED_FORMATS_SYNC (sizeof(supported_formats_sync) / sizeof(supported_formats_sync[0]))
  236. static void swap_texture_red_blue(gs_texture_t *texture)
  237. {
  238. GLuint gl_texure = *(GLuint *)gs_texture_get_obj(texture);
  239. glBindTexture(GL_TEXTURE_2D, gl_texure);
  240. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  242. glBindTexture(GL_TEXTURE_2D, 0);
  243. }
  244. static inline struct spa_pod *build_format(obs_pipewire_stream *obs_pw_stream, struct spa_pod_builder *b,
  245. uint32_t format, uint64_t *modifiers, size_t modifier_count)
  246. {
  247. struct spa_rectangle max_resolution = SPA_RECTANGLE(8192, 4320);
  248. struct spa_rectangle min_resolution = SPA_RECTANGLE(1, 1);
  249. struct spa_rectangle resolution;
  250. struct spa_pod_frame format_frame;
  251. struct spa_fraction max_framerate;
  252. struct spa_fraction min_framerate;
  253. struct spa_fraction framerate;
  254. if (obs_pw_stream->framerate.set) {
  255. framerate = obs_pw_stream->framerate.fraction;
  256. min_framerate = obs_pw_stream->framerate.fraction;
  257. max_framerate = obs_pw_stream->framerate.fraction;
  258. } else {
  259. framerate = SPA_FRACTION(obs_pw_stream->video_info.fps_num, obs_pw_stream->video_info.fps_den);
  260. min_framerate = SPA_FRACTION(0, 1);
  261. max_framerate = SPA_FRACTION(360, 1);
  262. }
  263. if (obs_pw_stream->resolution.set) {
  264. resolution = obs_pw_stream->resolution.rect;
  265. min_resolution = obs_pw_stream->resolution.rect;
  266. max_resolution = obs_pw_stream->resolution.rect;
  267. } else {
  268. resolution =
  269. SPA_RECTANGLE(obs_pw_stream->video_info.output_width, obs_pw_stream->video_info.output_height);
  270. min_resolution = SPA_RECTANGLE(1, 1);
  271. max_resolution = SPA_RECTANGLE(8192, 4320);
  272. }
  273. /* Make an object of type SPA_TYPE_OBJECT_Format and id SPA_PARAM_EnumFormat.
  274. * The object type is important because it defines the properties that are
  275. * acceptable. The id gives more context about what the object is meant to
  276. * contain. In this case we enumerate supported formats. */
  277. spa_pod_builder_push_object(b, &format_frame, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
  278. /* add media type and media subtype properties */
  279. spa_pod_builder_add(b, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), 0);
  280. spa_pod_builder_add(b, SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), 0);
  281. /* formats */
  282. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_format, SPA_POD_Id(format), 0);
  283. /* modifier */
  284. if (modifier_count > 0) {
  285. struct spa_pod_frame modifier_frame;
  286. /* build an enumeration of modifiers */
  287. spa_pod_builder_prop(b, SPA_FORMAT_VIDEO_modifier,
  288. SPA_POD_PROP_FLAG_MANDATORY | SPA_POD_PROP_FLAG_DONT_FIXATE);
  289. spa_pod_builder_push_choice(b, &modifier_frame, SPA_CHOICE_Enum, 0);
  290. /* The first element of choice pods is the preferred value. Here
  291. * we arbitrarily pick the first modifier as the preferred one.
  292. */
  293. spa_pod_builder_long(b, modifiers[0]);
  294. /* modifiers from an array */
  295. for (uint32_t i = 0; i < modifier_count; i++)
  296. spa_pod_builder_long(b, modifiers[i]);
  297. spa_pod_builder_pop(b, &modifier_frame);
  298. }
  299. /* add size and framerate ranges */
  300. spa_pod_builder_add(b, SPA_FORMAT_VIDEO_size,
  301. SPA_POD_CHOICE_RANGE_Rectangle(&resolution, &min_resolution, &max_resolution),
  302. SPA_FORMAT_VIDEO_framerate,
  303. SPA_POD_CHOICE_RANGE_Fraction(&framerate, &min_framerate, &max_framerate), 0);
  304. return spa_pod_builder_pop(b, &format_frame);
  305. }
  306. static bool build_format_params(obs_pipewire_stream *obs_pw_stream, struct spa_pod_builder *pod_builder,
  307. const struct spa_pod ***param_list, uint32_t *n_params)
  308. {
  309. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  310. uint32_t params_count = 0;
  311. const struct spa_pod **params;
  312. if (!obs_pw_stream->format_info.num) {
  313. blog(LOG_ERROR, "[pipewire] No format found while building param pointers");
  314. return false;
  315. }
  316. params = bzalloc(2 * obs_pw_stream->format_info.num * sizeof(struct spa_pod *));
  317. if (!params) {
  318. blog(LOG_ERROR, "[pipewire] Failed to allocate memory for param pointers");
  319. return false;
  320. }
  321. if (!check_pw_version(&obs_pw->server_version, 0, 3, 33))
  322. goto build_shm;
  323. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  324. if (obs_pw_stream->format_info.array[i].modifiers.num == 0) {
  325. continue;
  326. }
  327. params[params_count++] = build_format(obs_pw_stream, pod_builder,
  328. obs_pw_stream->format_info.array[i].spa_format,
  329. obs_pw_stream->format_info.array[i].modifiers.array,
  330. obs_pw_stream->format_info.array[i].modifiers.num);
  331. }
  332. build_shm:
  333. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  334. params[params_count++] = build_format(obs_pw_stream, pod_builder,
  335. obs_pw_stream->format_info.array[i].spa_format, NULL, 0);
  336. }
  337. *param_list = params;
  338. *n_params = params_count;
  339. return true;
  340. }
  341. static bool drm_format_available(uint32_t drm_format, uint32_t *drm_formats, size_t n_drm_formats)
  342. {
  343. for (size_t j = 0; j < n_drm_formats; j++) {
  344. if (drm_format == drm_formats[j]) {
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. static void init_format_info_async(obs_pipewire_stream *obs_pw_stream)
  351. {
  352. da_init(obs_pw_stream->format_info);
  353. for (size_t i = 0; i < N_SUPPORTED_FORMATS_ASYNC; i++) {
  354. struct obs_pw_video_format obs_pw_video_format;
  355. struct format_info *info;
  356. if (!obs_pw_video_format_from_spa_format(supported_formats_async[i], &obs_pw_video_format))
  357. continue;
  358. info = da_push_back_new(obs_pw_stream->format_info);
  359. da_init(info->modifiers);
  360. info->spa_format = obs_pw_video_format.spa_format;
  361. info->drm_format = obs_pw_video_format.drm_format;
  362. }
  363. }
  364. static void init_format_info_sync(obs_pipewire_stream *obs_pw_stream)
  365. {
  366. da_init(obs_pw_stream->format_info);
  367. obs_enter_graphics();
  368. enum gs_dmabuf_flags dmabuf_flags;
  369. uint32_t *drm_formats = NULL;
  370. size_t n_drm_formats = 0;
  371. bool capabilities_queried = gs_query_dmabuf_capabilities(&dmabuf_flags, &drm_formats, &n_drm_formats);
  372. for (size_t i = 0; i < N_SUPPORTED_FORMATS_SYNC; i++) {
  373. struct obs_pw_video_format obs_pw_video_format;
  374. struct format_info *info;
  375. if (!obs_pw_video_format_from_spa_format(supported_formats_sync[i], &obs_pw_video_format) ||
  376. obs_pw_video_format.gs_format == GS_UNKNOWN)
  377. continue;
  378. info = da_push_back_new(obs_pw_stream->format_info);
  379. da_init(info->modifiers);
  380. info->spa_format = obs_pw_video_format.spa_format;
  381. info->drm_format = obs_pw_video_format.drm_format;
  382. if (!capabilities_queried ||
  383. !drm_format_available(obs_pw_video_format.drm_format, drm_formats, n_drm_formats))
  384. continue;
  385. size_t n_modifiers;
  386. uint64_t *modifiers = NULL;
  387. if (gs_query_dmabuf_modifiers_for_format(obs_pw_video_format.drm_format, &modifiers, &n_modifiers)) {
  388. da_push_back_array(info->modifiers, modifiers, n_modifiers);
  389. }
  390. bfree(modifiers);
  391. if (dmabuf_flags & GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED) {
  392. uint64_t modifier_implicit = DRM_FORMAT_MOD_INVALID;
  393. da_push_back(info->modifiers, &modifier_implicit);
  394. }
  395. }
  396. obs_leave_graphics();
  397. bfree(drm_formats);
  398. }
  399. static void init_format_info(obs_pipewire_stream *obs_pw_stream, const struct obs_pw_video_format *selected_format)
  400. {
  401. if (selected_format) {
  402. struct format_info *info;
  403. da_init(obs_pw_stream->format_info);
  404. info = da_push_back_new(obs_pw_stream->format_info);
  405. da_init(info->modifiers);
  406. info->spa_format = selected_format->spa_format;
  407. info->drm_format = selected_format->drm_format;
  408. } else {
  409. uint32_t output_flags;
  410. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  411. if (output_flags & OBS_SOURCE_VIDEO) {
  412. if (output_flags & OBS_SOURCE_ASYNC)
  413. init_format_info_async(obs_pw_stream);
  414. else
  415. init_format_info_sync(obs_pw_stream);
  416. }
  417. }
  418. }
  419. static void clear_format_info(obs_pipewire_stream *obs_pw_stream)
  420. {
  421. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  422. da_free(obs_pw_stream->format_info.array[i].modifiers);
  423. }
  424. da_free(obs_pw_stream->format_info);
  425. }
  426. static void remove_modifier_from_format(obs_pipewire_stream *obs_pw_stream, uint32_t spa_format, uint64_t modifier)
  427. {
  428. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  429. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  430. if (obs_pw_stream->format_info.array[i].spa_format != spa_format)
  431. continue;
  432. if (!check_pw_version(&obs_pw->server_version, 0, 3, 40)) {
  433. da_erase_range(obs_pw_stream->format_info.array[i].modifiers, 0,
  434. obs_pw_stream->format_info.array[i].modifiers.num - 1);
  435. continue;
  436. }
  437. int idx = da_find(obs_pw_stream->format_info.array[i].modifiers, &modifier, 0);
  438. while (idx != -1) {
  439. da_erase(obs_pw_stream->format_info.array[i].modifiers, idx);
  440. idx = da_find(obs_pw_stream->format_info.array[i].modifiers, &modifier, 0);
  441. }
  442. }
  443. }
  444. static void renegotiate_format(void *data, uint64_t expirations)
  445. {
  446. UNUSED_PARAMETER(expirations);
  447. obs_pipewire_stream *obs_pw_stream = (obs_pipewire_stream *)data;
  448. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  449. const struct spa_pod **params = NULL;
  450. blog(LOG_INFO, "[pipewire] Renegotiating stream");
  451. pw_thread_loop_lock(obs_pw->thread_loop);
  452. uint8_t params_buffer[4096];
  453. struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  454. uint32_t n_params;
  455. if (!build_format_params(obs_pw_stream, &pod_builder, &params, &n_params)) {
  456. teardown_pipewire(obs_pw);
  457. pw_thread_loop_unlock(obs_pw->thread_loop);
  458. return;
  459. }
  460. pw_stream_update_params(obs_pw_stream->stream, params, n_params);
  461. pw_thread_loop_unlock(obs_pw->thread_loop);
  462. bfree(params);
  463. }
  464. /* ------------------------------------------------- */
  465. static void return_unused_pw_buffer(struct pw_stream *stream, struct pw_buffer *b)
  466. {
  467. #if PW_CHECK_VERSION(1, 2, 0)
  468. struct spa_buffer *buffer = b->buffer;
  469. struct spa_data *last_data = &buffer->datas[buffer->n_datas - 1];
  470. struct spa_meta_sync_timeline *synctimeline =
  471. spa_buffer_find_meta_data(buffer, SPA_META_SyncTimeline, sizeof(struct spa_meta_sync_timeline));
  472. if (synctimeline && (last_data->type == SPA_DATA_SyncObj))
  473. gs_sync_signal_syncobj_timeline_point(last_data->fd, synctimeline->release_point);
  474. #endif
  475. pw_stream_queue_buffer(stream, b);
  476. }
  477. static inline struct pw_buffer *find_latest_buffer(struct pw_stream *stream)
  478. {
  479. struct pw_buffer *b;
  480. /* Find the most recent buffer */
  481. b = NULL;
  482. while (true) {
  483. struct pw_buffer *aux = pw_stream_dequeue_buffer(stream);
  484. if (!aux)
  485. break;
  486. if (b)
  487. return_unused_pw_buffer(stream, b);
  488. b = aux;
  489. }
  490. return b;
  491. }
  492. static uint32_t get_spa_buffer_plane_count(const struct spa_buffer *buffer)
  493. {
  494. uint32_t plane_count = 0;
  495. while (plane_count < buffer->n_datas) {
  496. if (buffer->datas[plane_count].type == SPA_DATA_DmaBuf)
  497. plane_count++;
  498. else
  499. break;
  500. }
  501. return plane_count;
  502. }
  503. static enum video_colorspace video_colorspace_from_spa_color_matrix(enum spa_video_color_matrix matrix)
  504. {
  505. switch (matrix) {
  506. case SPA_VIDEO_COLOR_MATRIX_RGB:
  507. return VIDEO_CS_DEFAULT;
  508. case SPA_VIDEO_COLOR_MATRIX_BT601:
  509. return VIDEO_CS_601;
  510. case SPA_VIDEO_COLOR_MATRIX_BT709:
  511. return VIDEO_CS_709;
  512. default:
  513. return VIDEO_CS_DEFAULT;
  514. }
  515. }
  516. static enum video_range_type video_color_range_from_spa_color_range(enum spa_video_color_range colorrange)
  517. {
  518. switch (colorrange) {
  519. case SPA_VIDEO_COLOR_RANGE_0_255:
  520. return VIDEO_RANGE_FULL;
  521. case SPA_VIDEO_COLOR_RANGE_16_235:
  522. return VIDEO_RANGE_PARTIAL;
  523. default:
  524. return VIDEO_RANGE_DEFAULT;
  525. }
  526. }
  527. static bool prepare_obs_frame(obs_pipewire_stream *obs_pw_stream, struct obs_source_frame *frame)
  528. {
  529. struct obs_pw_video_format obs_pw_video_format;
  530. frame->width = obs_pw_stream->format.info.raw.size.width;
  531. frame->height = obs_pw_stream->format.info.raw.size.height;
  532. video_format_get_parameters(video_colorspace_from_spa_color_matrix(obs_pw_stream->format.info.raw.color_matrix),
  533. video_color_range_from_spa_color_range(obs_pw_stream->format.info.raw.color_range),
  534. frame->color_matrix, frame->color_range_min, frame->color_range_max);
  535. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  536. obs_pw_video_format.video_format == VIDEO_FORMAT_NONE)
  537. return false;
  538. frame->format = obs_pw_video_format.video_format;
  539. frame->linesize[0] = SPA_ROUND_UP_N(frame->width * obs_pw_video_format.bpp, 4);
  540. return true;
  541. }
  542. static void process_video_async(obs_pipewire_stream *obs_pw_stream)
  543. {
  544. struct spa_buffer *buffer;
  545. struct pw_buffer *b;
  546. bool has_buffer;
  547. b = find_latest_buffer(obs_pw_stream->stream);
  548. if (!b) {
  549. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  550. return;
  551. }
  552. buffer = b->buffer;
  553. has_buffer = buffer->datas[0].chunk->size != 0;
  554. if (!has_buffer)
  555. goto done;
  556. #ifdef DEBUG_PIPEWIRE
  557. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  558. #endif
  559. struct obs_source_frame out = {0};
  560. if (!prepare_obs_frame(obs_pw_stream, &out)) {
  561. blog(LOG_ERROR, "[pipewire] Couldn't prepare frame");
  562. goto done;
  563. }
  564. for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
  565. out.data[i] = buffer->datas[i].data;
  566. if (out.data[i] == NULL) {
  567. blog(LOG_ERROR, "[pipewire] Failed to access data");
  568. goto done;
  569. }
  570. }
  571. #ifdef DEBUG_PIPEWIRE
  572. blog(LOG_DEBUG, "[pipewire] Frame info: Format: %s, Planes: %u", get_video_format_name(out.format),
  573. buffer->n_datas);
  574. for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
  575. blog(LOG_DEBUG, "[pipewire] Plane %u: Dataptr:%p, Linesize:%d", i, out.data[i], out.linesize[i]);
  576. }
  577. #endif
  578. obs_source_output_video(obs_pw_stream->source, &out);
  579. done:
  580. pw_stream_queue_buffer(obs_pw_stream->stream, b);
  581. }
  582. static void process_video_sync(obs_pipewire_stream *obs_pw_stream)
  583. {
  584. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  585. struct spa_meta_cursor *cursor;
  586. struct spa_meta_header *header;
  587. struct spa_meta_region *region;
  588. struct spa_meta_videotransform *video_transform;
  589. struct obs_pw_video_format obs_pw_video_format;
  590. struct spa_buffer *buffer;
  591. struct pw_buffer *b;
  592. bool has_buffer = true;
  593. b = find_latest_buffer(obs_pw_stream->stream);
  594. if (!b) {
  595. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  596. return;
  597. }
  598. buffer = b->buffer;
  599. header = spa_buffer_find_meta_data(buffer, SPA_META_Header, sizeof(*header));
  600. if (header && (header->flags & SPA_META_HEADER_FLAG_CORRUPTED) > 0) {
  601. blog(LOG_ERROR, "[pipewire] buffer is corrupt");
  602. return_unused_pw_buffer(obs_pw_stream->stream, b);
  603. return;
  604. }
  605. obs_enter_graphics();
  606. // Workaround for kwin behaviour pre 5.27.5
  607. // Workaround for mutter behaviour pre GNOME 43
  608. // Only check this if !SPA_META_Header, once supported platforms update.
  609. has_buffer = buffer->datas[0].chunk->size != 0;
  610. if (!has_buffer)
  611. goto read_metadata;
  612. if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
  613. uint32_t planes = get_spa_buffer_plane_count(buffer);
  614. uint32_t *offsets = alloca(sizeof(uint32_t) * planes);
  615. uint32_t *strides = alloca(sizeof(uint32_t) * planes);
  616. uint64_t *modifiers = alloca(sizeof(uint64_t) * planes);
  617. int *fds = alloca(sizeof(int) * planes);
  618. bool use_modifiers;
  619. bool corrupt = false;
  620. #if PW_CHECK_VERSION(1, 2, 0)
  621. struct spa_meta_sync_timeline *synctimeline =
  622. spa_buffer_find_meta_data(buffer, SPA_META_SyncTimeline, sizeof(struct spa_meta_sync_timeline));
  623. #endif
  624. #ifdef DEBUG_PIPEWIRE
  625. blog(LOG_DEBUG, "[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
  626. buffer->datas[0].fd, buffer->datas[0].chunk->stride, buffer->datas[0].chunk->offset,
  627. obs_pw_stream->format.info.raw.size.width, obs_pw_stream->format.info.raw.size.height);
  628. #endif
  629. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  630. obs_pw_video_format.gs_format == GS_UNKNOWN) {
  631. blog(LOG_ERROR, "[pipewire] unsupported DMA buffer format: %d",
  632. obs_pw_stream->format.info.raw.format);
  633. goto read_metadata;
  634. }
  635. for (uint32_t plane = 0; plane < planes; plane++) {
  636. fds[plane] = buffer->datas[plane].fd;
  637. offsets[plane] = buffer->datas[plane].chunk->offset;
  638. strides[plane] = buffer->datas[plane].chunk->stride;
  639. modifiers[plane] = obs_pw_stream->format.info.raw.modifier;
  640. corrupt |= (buffer->datas[plane].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) > 0;
  641. }
  642. #if PW_CHECK_VERSION(1, 2, 0)
  643. if (obs_pw_stream->sync.release_syncobj_fd != -1) {
  644. if (!obs_pw_stream->sync.release_point_will_signal) {
  645. gs_sync_signal_syncobj_timeline_point(obs_pw_stream->sync.release_syncobj_fd,
  646. obs_pw_stream->sync.release_point);
  647. obs_pw_stream->sync.release_point_will_signal = true;
  648. }
  649. }
  650. g_clear_fd(&obs_pw_stream->sync.acquire_syncobj_fd, NULL);
  651. g_clear_fd(&obs_pw_stream->sync.release_syncobj_fd, NULL);
  652. if (synctimeline && (buffer->n_datas == (planes + 2))) {
  653. assert(buffer->datas[planes].type == SPA_DATA_SyncObj);
  654. assert(buffer->datas[planes + 1].type == SPA_DATA_SyncObj);
  655. obs_pw_stream->sync.acquire_syncobj_fd = fcntl(buffer->datas[planes].fd, F_DUPFD_CLOEXEC, 5);
  656. obs_pw_stream->sync.acquire_point = synctimeline->acquire_point;
  657. obs_pw_stream->sync.release_syncobj_fd =
  658. fcntl(buffer->datas[planes + 1].fd, F_DUPFD_CLOEXEC, 5);
  659. obs_pw_stream->sync.release_point = synctimeline->release_point;
  660. obs_pw_stream->sync.release_point_will_signal = false;
  661. obs_pw_stream->sync.set = true;
  662. } else {
  663. obs_pw_stream->sync.set = false;
  664. }
  665. #else
  666. obs_pw_stream->sync.set = false;
  667. #endif
  668. if (corrupt) {
  669. blog(LOG_DEBUG, "[pipewire] buffer contains corrupted data");
  670. goto read_metadata;
  671. }
  672. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  673. use_modifiers = obs_pw_stream->format.info.raw.modifier != DRM_FORMAT_MOD_INVALID;
  674. obs_pw_stream->texture = gs_texture_create_from_dmabuf(obs_pw_stream->format.info.raw.size.width,
  675. obs_pw_stream->format.info.raw.size.height,
  676. obs_pw_video_format.drm_format, GS_BGRX, planes,
  677. fds, strides, offsets,
  678. use_modifiers ? modifiers : NULL);
  679. if (obs_pw_stream->texture == NULL) {
  680. remove_modifier_from_format(obs_pw_stream, obs_pw_stream->format.info.raw.format,
  681. obs_pw_stream->format.info.raw.modifier);
  682. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  683. goto read_metadata;
  684. }
  685. } else {
  686. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  687. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  688. obs_pw_video_format.gs_format == GS_UNKNOWN) {
  689. blog(LOG_ERROR, "[pipewire] unsupported buffer format: %d",
  690. obs_pw_stream->format.info.raw.format);
  691. goto read_metadata;
  692. }
  693. if ((buffer->datas[0].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) > 0) {
  694. blog(LOG_DEBUG, "[pipewire] buffer contains corrupted data");
  695. goto read_metadata;
  696. }
  697. if (buffer->datas[0].chunk->size == 0) {
  698. blog(LOG_DEBUG, "[pipewire] buffer contains empty data");
  699. goto read_metadata;
  700. }
  701. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  702. obs_pw_stream->texture = gs_texture_create(obs_pw_stream->format.info.raw.size.width,
  703. obs_pw_stream->format.info.raw.size.height,
  704. obs_pw_video_format.gs_format, 1,
  705. (const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
  706. }
  707. if (obs_pw_video_format.swap_red_blue)
  708. swap_texture_red_blue(obs_pw_stream->texture);
  709. /* Video Crop */
  710. region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop, sizeof(*region));
  711. if (region && spa_meta_region_is_valid(region)) {
  712. #ifdef DEBUG_PIPEWIRE
  713. blog(LOG_DEBUG, "[pipewire] Crop Region available (%dx%d+%d+%d)", region->region.position.x,
  714. region->region.position.y, region->region.size.width, region->region.size.height);
  715. #endif
  716. obs_pw_stream->crop.x = region->region.position.x;
  717. obs_pw_stream->crop.y = region->region.position.y;
  718. obs_pw_stream->crop.width = region->region.size.width;
  719. obs_pw_stream->crop.height = region->region.size.height;
  720. obs_pw_stream->crop.valid = true;
  721. } else {
  722. obs_pw_stream->crop.valid = false;
  723. }
  724. /* Video Transform */
  725. video_transform = spa_buffer_find_meta_data(buffer, SPA_META_VideoTransform, sizeof(*video_transform));
  726. if (video_transform)
  727. obs_pw_stream->transform = video_transform->transform;
  728. else
  729. obs_pw_stream->transform = SPA_META_TRANSFORMATION_None;
  730. read_metadata:
  731. /* Cursor */
  732. cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor, sizeof(*cursor));
  733. obs_pw_stream->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
  734. if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid) {
  735. struct spa_meta_bitmap *bitmap = NULL;
  736. if (cursor->bitmap_offset)
  737. bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset, struct spa_meta_bitmap);
  738. if (bitmap)
  739. g_clear_pointer(&obs_pw_stream->cursor.texture, gs_texture_destroy);
  740. if (bitmap && bitmap->size.width > 0 && bitmap->size.height > 0 &&
  741. obs_pw_video_format_from_spa_format(bitmap->format, &obs_pw_video_format) &&
  742. obs_pw_video_format.gs_format != GS_UNKNOWN) {
  743. const uint8_t *bitmap_data;
  744. bitmap_data = SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
  745. obs_pw_stream->cursor.hotspot_x = cursor->hotspot.x;
  746. obs_pw_stream->cursor.hotspot_y = cursor->hotspot.y;
  747. obs_pw_stream->cursor.width = bitmap->size.width;
  748. obs_pw_stream->cursor.height = bitmap->size.height;
  749. assert(obs_pw_stream->cursor.texture == NULL);
  750. obs_pw_stream->cursor.texture =
  751. gs_texture_create(obs_pw_stream->cursor.width, obs_pw_stream->cursor.height,
  752. obs_pw_video_format.gs_format, 1, &bitmap_data, GS_DYNAMIC);
  753. if (obs_pw_video_format.swap_red_blue)
  754. swap_texture_red_blue(obs_pw_stream->cursor.texture);
  755. }
  756. obs_pw_stream->cursor.x = cursor->position.x;
  757. obs_pw_stream->cursor.y = cursor->position.y;
  758. }
  759. pw_stream_queue_buffer(obs_pw_stream->stream, b);
  760. obs_leave_graphics();
  761. }
  762. static void on_process_cb(void *user_data)
  763. {
  764. obs_pipewire_stream *obs_pw_stream = user_data;
  765. uint32_t output_flags;
  766. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  767. if (output_flags & OBS_SOURCE_VIDEO) {
  768. if (output_flags & OBS_SOURCE_ASYNC)
  769. process_video_async(obs_pw_stream);
  770. else
  771. process_video_sync(obs_pw_stream);
  772. }
  773. }
  774. static void on_param_changed_cb(void *user_data, uint32_t id, const struct spa_pod *param)
  775. {
  776. obs_pipewire_stream *obs_pw_stream = user_data;
  777. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  778. struct spa_pod_builder pod_builder;
  779. const struct spa_pod *params[7];
  780. const char *format_name;
  781. uint32_t n_params = 0;
  782. uint32_t buffer_types;
  783. uint32_t output_flags;
  784. uint8_t params_buffer[1024];
  785. int result;
  786. #if PW_CHECK_VERSION(1, 2, 0)
  787. bool supports_explicit_sync = false;
  788. #endif
  789. if (!param || id != SPA_PARAM_Format)
  790. return;
  791. result = spa_format_parse(param, &obs_pw_stream->format.media_type, &obs_pw_stream->format.media_subtype);
  792. if (result < 0)
  793. return;
  794. if (obs_pw_stream->format.media_type != SPA_MEDIA_TYPE_video ||
  795. obs_pw_stream->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
  796. return;
  797. spa_format_video_raw_parse(param, &obs_pw_stream->format.info.raw);
  798. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  799. buffer_types = 1 << SPA_DATA_MemPtr;
  800. bool has_modifier = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL;
  801. if ((has_modifier || check_pw_version(&obs_pw->server_version, 0, 3, 24)) &&
  802. (output_flags & OBS_SOURCE_ASYNC_VIDEO) != OBS_SOURCE_ASYNC_VIDEO) {
  803. buffer_types |= 1 << SPA_DATA_DmaBuf;
  804. #if PW_CHECK_VERSION(1, 2, 0)
  805. obs_enter_graphics();
  806. supports_explicit_sync = gs_query_sync_capabilities();
  807. obs_leave_graphics();
  808. #endif
  809. }
  810. blog(LOG_INFO, "[pipewire] Negotiated format:");
  811. format_name = spa_debug_type_find_name(spa_type_video_format, obs_pw_stream->format.info.raw.format);
  812. blog(LOG_INFO, "[pipewire] Format: %d (%s)", obs_pw_stream->format.info.raw.format,
  813. format_name ? format_name : "unknown format");
  814. if (has_modifier) {
  815. blog(LOG_INFO, "[pipewire] Modifier: 0x%" PRIx64, obs_pw_stream->format.info.raw.modifier);
  816. }
  817. blog(LOG_INFO, "[pipewire] Size: %dx%d", obs_pw_stream->format.info.raw.size.width,
  818. obs_pw_stream->format.info.raw.size.height);
  819. blog(LOG_INFO, "[pipewire] Framerate: %d/%d", obs_pw_stream->format.info.raw.framerate.num,
  820. obs_pw_stream->format.info.raw.framerate.denom);
  821. /* Video crop */
  822. pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  823. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  824. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
  825. SPA_PARAM_META_size,
  826. SPA_POD_Int(sizeof(struct spa_meta_region)));
  827. /* Cursor */
  828. params[n_params++] =
  829. spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, SPA_PARAM_META_type,
  830. SPA_POD_Id(SPA_META_Cursor), SPA_PARAM_META_size,
  831. SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64), CURSOR_META_SIZE(1, 1),
  832. CURSOR_META_SIZE(1024, 1024)));
  833. /* Buffer options */
  834. #if PW_CHECK_VERSION(1, 2, 0)
  835. if (supports_explicit_sync) {
  836. struct spa_pod_frame dmabuf_explicit_sync_frame;
  837. spa_pod_builder_push_object(&pod_builder, &dmabuf_explicit_sync_frame, SPA_TYPE_OBJECT_ParamBuffers,
  838. SPA_PARAM_Buffers);
  839. spa_pod_builder_add(&pod_builder, SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(1 << SPA_DATA_DmaBuf), 0);
  840. spa_pod_builder_prop(&pod_builder, SPA_PARAM_BUFFERS_metaType, SPA_POD_PROP_FLAG_MANDATORY);
  841. spa_pod_builder_int(&pod_builder, 1 << SPA_META_SyncTimeline);
  842. params[n_params++] = spa_pod_builder_pop(&pod_builder, &dmabuf_explicit_sync_frame);
  843. }
  844. #endif
  845. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
  846. SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types));
  847. /* Sync timeline */
  848. #if PW_CHECK_VERSION(1, 2, 0)
  849. if (supports_explicit_sync) {
  850. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  851. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_SyncTimeline),
  852. SPA_PARAM_META_size,
  853. SPA_POD_Int(sizeof(struct spa_meta_sync_timeline)));
  854. }
  855. #endif
  856. /* Meta header */
  857. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  858. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
  859. SPA_PARAM_META_size,
  860. SPA_POD_Int(sizeof(struct spa_meta_header)));
  861. #if PW_CHECK_VERSION(0, 3, 62)
  862. if (check_pw_version(&obs_pw->server_version, 0, 3, 62)) {
  863. /* Video transformation */
  864. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  865. SPA_PARAM_META_type,
  866. SPA_POD_Id(SPA_META_VideoTransform),
  867. SPA_PARAM_META_size,
  868. SPA_POD_Int(sizeof(struct spa_meta_videotransform)));
  869. }
  870. #endif
  871. pw_stream_update_params(obs_pw_stream->stream, params, n_params);
  872. obs_pw_stream->negotiated = true;
  873. }
  874. static void on_state_changed_cb(void *user_data, enum pw_stream_state old, enum pw_stream_state state,
  875. const char *error)
  876. {
  877. UNUSED_PARAMETER(old);
  878. obs_pipewire_stream *obs_pw_stream = user_data;
  879. blog(LOG_INFO, "[pipewire] Stream %p state: \"%s\" (error: %s)", obs_pw_stream->stream,
  880. pw_stream_state_as_string(state), error ? error : "none");
  881. }
  882. static const struct pw_stream_events stream_events = {
  883. PW_VERSION_STREAM_EVENTS,
  884. .state_changed = on_state_changed_cb,
  885. .param_changed = on_param_changed_cb,
  886. .process = on_process_cb,
  887. };
  888. static void on_core_info_cb(void *user_data, const struct pw_core_info *info)
  889. {
  890. obs_pipewire *obs_pw = user_data;
  891. update_pw_versions(obs_pw, info->version);
  892. }
  893. static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res, const char *message)
  894. {
  895. obs_pipewire *obs_pw = user_data;
  896. blog(LOG_ERROR, "[pipewire] Error id:%u seq:%d res:%d (%s): %s", id, seq, res, spa_strerror(res), message);
  897. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  898. }
  899. static void on_core_done_cb(void *user_data, uint32_t id, int seq)
  900. {
  901. obs_pipewire *obs_pw = user_data;
  902. if (id == PW_ID_CORE && obs_pw->sync_id == seq)
  903. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  904. }
  905. static const struct pw_core_events core_events = {
  906. PW_VERSION_CORE_EVENTS,
  907. .info = on_core_info_cb,
  908. .done = on_core_done_cb,
  909. .error = on_core_error_cb,
  910. };
  911. /* obs_source_info methods */
  912. obs_pipewire *obs_pipewire_connect_fd(int pipewire_fd, const struct pw_registry_events *registry_events,
  913. void *user_data)
  914. {
  915. obs_pipewire *obs_pw;
  916. obs_pw = bzalloc(sizeof(obs_pipewire));
  917. obs_pw->pipewire_fd = pipewire_fd;
  918. obs_pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  919. obs_pw->context = pw_context_new(pw_thread_loop_get_loop(obs_pw->thread_loop), NULL, 0);
  920. if (pw_thread_loop_start(obs_pw->thread_loop) < 0) {
  921. blog(LOG_WARNING, "Error starting threaded mainloop");
  922. bfree(obs_pw);
  923. return NULL;
  924. }
  925. pw_thread_loop_lock(obs_pw->thread_loop);
  926. /* Core */
  927. obs_pw->core = pw_context_connect_fd(obs_pw->context, fcntl(obs_pw->pipewire_fd, F_DUPFD_CLOEXEC, 5), NULL, 0);
  928. if (!obs_pw->core) {
  929. blog(LOG_WARNING, "Error creating PipeWire core: %m");
  930. pw_thread_loop_unlock(obs_pw->thread_loop);
  931. bfree(obs_pw);
  932. return NULL;
  933. }
  934. pw_core_add_listener(obs_pw->core, &obs_pw->core_listener, &core_events, obs_pw);
  935. // Dispatch to receive the info core event
  936. obs_pw->sync_id = pw_core_sync(obs_pw->core, PW_ID_CORE, obs_pw->sync_id);
  937. pw_thread_loop_wait(obs_pw->thread_loop);
  938. /* Registry */
  939. if (registry_events) {
  940. obs_pw->registry = pw_core_get_registry(obs_pw->core, PW_VERSION_REGISTRY, 0);
  941. pw_registry_add_listener(obs_pw->registry, &obs_pw->registry_listener, registry_events, user_data);
  942. blog(LOG_INFO, "[pipewire] Created registry %p", obs_pw->registry);
  943. }
  944. pw_thread_loop_unlock(obs_pw->thread_loop);
  945. obs_pw->streams = g_ptr_array_new();
  946. return obs_pw;
  947. }
  948. struct pw_registry *obs_pipewire_get_registry(obs_pipewire *obs_pw)
  949. {
  950. return obs_pw->registry;
  951. }
  952. void obs_pipewire_roundtrip(obs_pipewire *obs_pw)
  953. {
  954. pw_thread_loop_lock(obs_pw->thread_loop);
  955. obs_pw->sync_id = pw_core_sync(obs_pw->core, PW_ID_CORE, obs_pw->sync_id);
  956. pw_thread_loop_wait(obs_pw->thread_loop);
  957. pw_thread_loop_unlock(obs_pw->thread_loop);
  958. }
  959. void obs_pipewire_destroy(obs_pipewire *obs_pw)
  960. {
  961. if (!obs_pw)
  962. return;
  963. while (obs_pw->streams->len > 0) {
  964. obs_pipewire_stream *obs_pw_stream = g_ptr_array_index(obs_pw->streams, 0);
  965. obs_pipewire_stream_destroy(obs_pw_stream);
  966. }
  967. g_clear_pointer(&obs_pw->streams, g_ptr_array_unref);
  968. teardown_pipewire(obs_pw);
  969. bfree(obs_pw);
  970. }
  971. obs_pipewire_stream *obs_pipewire_connect_stream(obs_pipewire *obs_pw, obs_source_t *source, int pipewire_node,
  972. const struct obs_pipewire_connect_stream_info *connect_info)
  973. {
  974. struct spa_pod_builder pod_builder;
  975. const struct spa_pod **params = NULL;
  976. obs_pipewire_stream *obs_pw_stream;
  977. uint32_t n_params;
  978. uint8_t params_buffer[4096];
  979. assert(connect_info != NULL);
  980. obs_pw_stream = bzalloc(sizeof(obs_pipewire_stream));
  981. obs_pw_stream->obs_pw = obs_pw;
  982. obs_pw_stream->source = source;
  983. obs_pw_stream->cursor.visible = connect_info->screencast.cursor_visible;
  984. obs_pw_stream->framerate.set = connect_info->video.framerate != NULL;
  985. obs_pw_stream->resolution.set = connect_info->video.resolution != NULL;
  986. obs_pw_stream->sync.acquire_syncobj_fd = -1;
  987. obs_pw_stream->sync.release_syncobj_fd = -1;
  988. if (obs_pw_stream->framerate.set)
  989. obs_pw_stream->framerate.fraction = *connect_info->video.framerate;
  990. if (obs_pw_stream->resolution.set)
  991. obs_pw_stream->resolution.rect = *connect_info->video.resolution;
  992. init_format_info(obs_pw_stream, connect_info->video.format);
  993. pw_thread_loop_lock(obs_pw->thread_loop);
  994. /* Signal to renegotiate */
  995. obs_pw_stream->reneg =
  996. pw_loop_add_event(pw_thread_loop_get_loop(obs_pw->thread_loop), renegotiate_format, obs_pw_stream);
  997. blog(LOG_DEBUG, "[pipewire] registered event %p", obs_pw_stream->reneg);
  998. /* Stream */
  999. obs_pw_stream->stream = pw_stream_new(obs_pw->core, connect_info->stream_name, connect_info->stream_properties);
  1000. pw_stream_add_listener(obs_pw_stream->stream, &obs_pw_stream->stream_listener, &stream_events, obs_pw_stream);
  1001. blog(LOG_INFO, "[pipewire] Created stream %p", obs_pw_stream->stream);
  1002. /* Stream parameters */
  1003. pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  1004. obs_get_video_info(&obs_pw_stream->video_info);
  1005. if (!build_format_params(obs_pw_stream, &pod_builder, &params, &n_params)) {
  1006. pw_thread_loop_unlock(obs_pw->thread_loop);
  1007. bfree(obs_pw_stream);
  1008. return NULL;
  1009. }
  1010. pw_stream_connect(obs_pw_stream->stream, PW_DIRECTION_INPUT, pipewire_node,
  1011. PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, n_params);
  1012. blog(LOG_INFO, "[pipewire] Playing stream %p", obs_pw_stream->stream);
  1013. pw_thread_loop_unlock(obs_pw->thread_loop);
  1014. bfree(params);
  1015. g_ptr_array_add(obs_pw->streams, obs_pw_stream);
  1016. return obs_pw_stream;
  1017. }
  1018. void obs_pipewire_stream_show(obs_pipewire_stream *obs_pw_stream)
  1019. {
  1020. if (obs_pw_stream->stream) {
  1021. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1022. pw_stream_set_active(obs_pw_stream->stream, true);
  1023. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1024. }
  1025. }
  1026. void obs_pipewire_stream_hide(obs_pipewire_stream *obs_pw_stream)
  1027. {
  1028. if (obs_pw_stream->stream) {
  1029. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1030. pw_stream_set_active(obs_pw_stream->stream, false);
  1031. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1032. }
  1033. }
  1034. uint32_t obs_pipewire_stream_get_width(obs_pipewire_stream *obs_pw_stream)
  1035. {
  1036. bool has_crop;
  1037. if (!obs_pw_stream->negotiated)
  1038. return 0;
  1039. has_crop = has_effective_crop(obs_pw_stream);
  1040. switch (obs_pw_stream->transform) {
  1041. case SPA_META_TRANSFORMATION_Flipped:
  1042. case SPA_META_TRANSFORMATION_None:
  1043. case SPA_META_TRANSFORMATION_Flipped180:
  1044. case SPA_META_TRANSFORMATION_180:
  1045. return has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  1046. case SPA_META_TRANSFORMATION_Flipped90:
  1047. case SPA_META_TRANSFORMATION_90:
  1048. case SPA_META_TRANSFORMATION_Flipped270:
  1049. case SPA_META_TRANSFORMATION_270:
  1050. return has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  1051. default:
  1052. return 0;
  1053. }
  1054. }
  1055. uint32_t obs_pipewire_stream_get_height(obs_pipewire_stream *obs_pw_stream)
  1056. {
  1057. bool has_crop;
  1058. if (!obs_pw_stream->negotiated)
  1059. return 0;
  1060. has_crop = has_effective_crop(obs_pw_stream);
  1061. switch (obs_pw_stream->transform) {
  1062. case SPA_META_TRANSFORMATION_Flipped:
  1063. case SPA_META_TRANSFORMATION_None:
  1064. case SPA_META_TRANSFORMATION_Flipped180:
  1065. case SPA_META_TRANSFORMATION_180:
  1066. return has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  1067. case SPA_META_TRANSFORMATION_Flipped90:
  1068. case SPA_META_TRANSFORMATION_90:
  1069. case SPA_META_TRANSFORMATION_Flipped270:
  1070. case SPA_META_TRANSFORMATION_270:
  1071. return has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  1072. default:
  1073. return 0;
  1074. }
  1075. }
  1076. void obs_pipewire_stream_video_render(obs_pipewire_stream *obs_pw_stream, gs_effect_t *effect)
  1077. {
  1078. bool rotated;
  1079. int flip = 0;
  1080. gs_eparam_t *image;
  1081. if (!obs_pw_stream->texture)
  1082. return;
  1083. if (obs_pw_stream->sync.set) {
  1084. gs_sync_t *acquire_sync = gs_sync_create_from_syncobj_timeline_point(
  1085. obs_pw_stream->sync.acquire_syncobj_fd, obs_pw_stream->sync.acquire_point);
  1086. gs_sync_wait(acquire_sync);
  1087. gs_sync_destroy(acquire_sync);
  1088. }
  1089. /* FIXME: Use obs_pw_stream->format.info.raw colorimetry info to handle
  1090. * textures in their corresponding color space */
  1091. image = gs_effect_get_param_by_name(effect, "image");
  1092. gs_effect_set_texture(image, obs_pw_stream->texture);
  1093. rotated = push_rotation(obs_pw_stream);
  1094. flip = get_buffer_flip(obs_pw_stream);
  1095. /* There is a SPA_VIDEO_FLAG_PREMULTIPLIED_ALPHA flag, but it does not
  1096. * seem to be fully implemented nor ever set. Just assume premultiplied
  1097. * always, which seems to be the default convention.
  1098. *
  1099. * See https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3126
  1100. *
  1101. * The cursor bitmap alpha mode is also not specified, and the
  1102. * convention there also seems to be premultiplied. */
  1103. gs_blend_state_push();
  1104. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1105. if (has_effective_crop(obs_pw_stream)) {
  1106. gs_draw_sprite_subregion(obs_pw_stream->texture, flip, obs_pw_stream->crop.x, obs_pw_stream->crop.y,
  1107. obs_pw_stream->crop.width, obs_pw_stream->crop.height);
  1108. } else {
  1109. gs_draw_sprite(obs_pw_stream->texture, flip, 0, 0);
  1110. }
  1111. if (rotated)
  1112. gs_matrix_pop();
  1113. if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid && obs_pw_stream->cursor.texture) {
  1114. float cursor_x = obs_pw_stream->cursor.x - obs_pw_stream->cursor.hotspot_x;
  1115. float cursor_y = obs_pw_stream->cursor.y - obs_pw_stream->cursor.hotspot_y;
  1116. gs_matrix_push();
  1117. gs_matrix_translate3f(cursor_x, cursor_y, 0.0f);
  1118. gs_effect_set_texture(image, obs_pw_stream->cursor.texture);
  1119. gs_draw_sprite(obs_pw_stream->texture, 0, obs_pw_stream->cursor.width, obs_pw_stream->cursor.height);
  1120. gs_matrix_pop();
  1121. }
  1122. gs_blend_state_pop();
  1123. if (obs_pw_stream->sync.set) {
  1124. gs_sync_t *release_sync = gs_sync_create();
  1125. gs_sync_export_syncobj_timeline_point(release_sync, obs_pw_stream->sync.release_syncobj_fd,
  1126. obs_pw_stream->sync.release_point);
  1127. gs_sync_destroy(release_sync);
  1128. obs_pw_stream->sync.release_point_will_signal = true;
  1129. }
  1130. }
  1131. void obs_pipewire_stream_set_cursor_visible(obs_pipewire_stream *obs_pw_stream, bool cursor_visible)
  1132. {
  1133. obs_pw_stream->cursor.visible = cursor_visible;
  1134. }
  1135. void obs_pipewire_stream_destroy(obs_pipewire_stream *obs_pw_stream)
  1136. {
  1137. uint32_t output_flags;
  1138. if (!obs_pw_stream)
  1139. return;
  1140. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  1141. if (output_flags & OBS_SOURCE_ASYNC_VIDEO)
  1142. obs_source_output_video(obs_pw_stream->source, NULL);
  1143. g_ptr_array_remove(obs_pw_stream->obs_pw->streams, obs_pw_stream);
  1144. obs_enter_graphics();
  1145. g_clear_pointer(&obs_pw_stream->cursor.texture, gs_texture_destroy);
  1146. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  1147. obs_leave_graphics();
  1148. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1149. if (obs_pw_stream->stream)
  1150. pw_stream_disconnect(obs_pw_stream->stream);
  1151. g_clear_pointer(&obs_pw_stream->stream, pw_stream_destroy);
  1152. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1153. g_clear_fd(&obs_pw_stream->sync.acquire_syncobj_fd, NULL);
  1154. g_clear_fd(&obs_pw_stream->sync.release_syncobj_fd, NULL);
  1155. clear_format_info(obs_pw_stream);
  1156. bfree(obs_pw_stream);
  1157. }
  1158. void obs_pipewire_stream_set_framerate(obs_pipewire_stream *obs_pw_stream, const struct spa_fraction *framerate)
  1159. {
  1160. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  1161. if ((!obs_pw_stream->framerate.set && !framerate) ||
  1162. (obs_pw_stream->framerate.set && framerate && obs_pw_stream->framerate.fraction.num == framerate->num &&
  1163. obs_pw_stream->framerate.fraction.denom == framerate->denom))
  1164. return;
  1165. if (framerate) {
  1166. obs_pw_stream->framerate.fraction = *framerate;
  1167. obs_pw_stream->framerate.set = true;
  1168. } else {
  1169. obs_pw_stream->framerate.fraction = SPA_FRACTION(0, 0);
  1170. obs_pw_stream->framerate.set = false;
  1171. }
  1172. /* Signal to renegotiate */
  1173. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  1174. }
  1175. void obs_pipewire_stream_set_resolution(obs_pipewire_stream *obs_pw_stream, const struct spa_rectangle *resolution)
  1176. {
  1177. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  1178. if ((!obs_pw_stream->resolution.set && !resolution) ||
  1179. (obs_pw_stream->resolution.set && resolution && obs_pw_stream->resolution.rect.width == resolution->width &&
  1180. obs_pw_stream->resolution.rect.height == resolution->height))
  1181. return;
  1182. if (resolution) {
  1183. obs_pw_stream->resolution.rect = *resolution;
  1184. obs_pw_stream->resolution.set = true;
  1185. } else {
  1186. obs_pw_stream->resolution.rect = SPA_RECTANGLE(0, 0);
  1187. obs_pw_stream->resolution.set = false;
  1188. }
  1189. /* Signal to renegotiate */
  1190. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  1191. }