pipewire.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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)
  400. {
  401. uint32_t output_flags;
  402. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  403. if (output_flags & OBS_SOURCE_VIDEO) {
  404. if (output_flags & OBS_SOURCE_ASYNC)
  405. init_format_info_async(obs_pw_stream);
  406. else
  407. init_format_info_sync(obs_pw_stream);
  408. }
  409. }
  410. static void clear_format_info(obs_pipewire_stream *obs_pw_stream)
  411. {
  412. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  413. da_free(obs_pw_stream->format_info.array[i].modifiers);
  414. }
  415. da_free(obs_pw_stream->format_info);
  416. }
  417. static void remove_modifier_from_format(obs_pipewire_stream *obs_pw_stream, uint32_t spa_format, uint64_t modifier)
  418. {
  419. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  420. for (size_t i = 0; i < obs_pw_stream->format_info.num; i++) {
  421. if (obs_pw_stream->format_info.array[i].spa_format != spa_format)
  422. continue;
  423. if (!check_pw_version(&obs_pw->server_version, 0, 3, 40)) {
  424. da_erase_range(obs_pw_stream->format_info.array[i].modifiers, 0,
  425. obs_pw_stream->format_info.array[i].modifiers.num - 1);
  426. continue;
  427. }
  428. int idx = da_find(obs_pw_stream->format_info.array[i].modifiers, &modifier, 0);
  429. while (idx != -1) {
  430. da_erase(obs_pw_stream->format_info.array[i].modifiers, idx);
  431. idx = da_find(obs_pw_stream->format_info.array[i].modifiers, &modifier, 0);
  432. }
  433. }
  434. }
  435. static void renegotiate_format(void *data, uint64_t expirations)
  436. {
  437. UNUSED_PARAMETER(expirations);
  438. obs_pipewire_stream *obs_pw_stream = (obs_pipewire_stream *)data;
  439. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  440. const struct spa_pod **params = NULL;
  441. blog(LOG_INFO, "[pipewire] Renegotiating stream");
  442. pw_thread_loop_lock(obs_pw->thread_loop);
  443. uint8_t params_buffer[4096];
  444. struct spa_pod_builder pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  445. uint32_t n_params;
  446. if (!build_format_params(obs_pw_stream, &pod_builder, &params, &n_params)) {
  447. teardown_pipewire(obs_pw);
  448. pw_thread_loop_unlock(obs_pw->thread_loop);
  449. return;
  450. }
  451. pw_stream_update_params(obs_pw_stream->stream, params, n_params);
  452. pw_thread_loop_unlock(obs_pw->thread_loop);
  453. bfree(params);
  454. }
  455. /* ------------------------------------------------- */
  456. static void return_unused_pw_buffer(struct pw_stream *stream, struct pw_buffer *b)
  457. {
  458. #if PW_CHECK_VERSION(1, 2, 0)
  459. struct spa_buffer *buffer = b->buffer;
  460. struct spa_data *last_data = &buffer->datas[buffer->n_datas - 1];
  461. struct spa_meta_sync_timeline *synctimeline =
  462. spa_buffer_find_meta_data(buffer, SPA_META_SyncTimeline, sizeof(struct spa_meta_sync_timeline));
  463. if (synctimeline && (last_data->type == SPA_DATA_SyncObj))
  464. gs_sync_signal_syncobj_timeline_point(last_data->fd, synctimeline->release_point);
  465. #endif
  466. pw_stream_queue_buffer(stream, b);
  467. }
  468. static inline struct pw_buffer *find_latest_buffer(struct pw_stream *stream)
  469. {
  470. struct pw_buffer *b;
  471. /* Find the most recent buffer */
  472. b = NULL;
  473. while (true) {
  474. struct pw_buffer *aux = pw_stream_dequeue_buffer(stream);
  475. if (!aux)
  476. break;
  477. if (b)
  478. return_unused_pw_buffer(stream, b);
  479. b = aux;
  480. }
  481. return b;
  482. }
  483. static uint32_t get_spa_buffer_plane_count(const struct spa_buffer *buffer)
  484. {
  485. uint32_t plane_count = 0;
  486. while (plane_count < buffer->n_datas) {
  487. if (buffer->datas[plane_count].type == SPA_DATA_DmaBuf)
  488. plane_count++;
  489. else
  490. break;
  491. }
  492. return plane_count;
  493. }
  494. static enum video_colorspace video_colorspace_from_spa_color_matrix(enum spa_video_color_matrix matrix)
  495. {
  496. switch (matrix) {
  497. case SPA_VIDEO_COLOR_MATRIX_RGB:
  498. return VIDEO_CS_DEFAULT;
  499. case SPA_VIDEO_COLOR_MATRIX_BT601:
  500. return VIDEO_CS_601;
  501. case SPA_VIDEO_COLOR_MATRIX_BT709:
  502. return VIDEO_CS_709;
  503. default:
  504. return VIDEO_CS_DEFAULT;
  505. }
  506. }
  507. static enum video_range_type video_color_range_from_spa_color_range(enum spa_video_color_range colorrange)
  508. {
  509. switch (colorrange) {
  510. case SPA_VIDEO_COLOR_RANGE_0_255:
  511. return VIDEO_RANGE_FULL;
  512. case SPA_VIDEO_COLOR_RANGE_16_235:
  513. return VIDEO_RANGE_PARTIAL;
  514. default:
  515. return VIDEO_RANGE_DEFAULT;
  516. }
  517. }
  518. static bool prepare_obs_frame(obs_pipewire_stream *obs_pw_stream, struct obs_source_frame *frame)
  519. {
  520. struct obs_pw_video_format obs_pw_video_format;
  521. frame->width = obs_pw_stream->format.info.raw.size.width;
  522. frame->height = obs_pw_stream->format.info.raw.size.height;
  523. video_format_get_parameters(video_colorspace_from_spa_color_matrix(obs_pw_stream->format.info.raw.color_matrix),
  524. video_color_range_from_spa_color_range(obs_pw_stream->format.info.raw.color_range),
  525. frame->color_matrix, frame->color_range_min, frame->color_range_max);
  526. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  527. obs_pw_video_format.video_format == VIDEO_FORMAT_NONE)
  528. return false;
  529. frame->format = obs_pw_video_format.video_format;
  530. frame->linesize[0] = SPA_ROUND_UP_N(frame->width * obs_pw_video_format.bpp, 4);
  531. return true;
  532. }
  533. static void process_video_async(obs_pipewire_stream *obs_pw_stream)
  534. {
  535. struct spa_buffer *buffer;
  536. struct pw_buffer *b;
  537. bool has_buffer;
  538. b = find_latest_buffer(obs_pw_stream->stream);
  539. if (!b) {
  540. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  541. return;
  542. }
  543. buffer = b->buffer;
  544. has_buffer = buffer->datas[0].chunk->size != 0;
  545. if (!has_buffer)
  546. goto done;
  547. #ifdef DEBUG_PIPEWIRE
  548. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  549. #endif
  550. struct obs_source_frame out = {0};
  551. if (!prepare_obs_frame(obs_pw_stream, &out)) {
  552. blog(LOG_ERROR, "[pipewire] Couldn't prepare frame");
  553. goto done;
  554. }
  555. for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
  556. out.data[i] = buffer->datas[i].data;
  557. if (out.data[i] == NULL) {
  558. blog(LOG_ERROR, "[pipewire] Failed to access data");
  559. goto done;
  560. }
  561. }
  562. #ifdef DEBUG_PIPEWIRE
  563. blog(LOG_DEBUG, "[pipewire] Frame info: Format: %s, Planes: %u", get_video_format_name(out.format),
  564. buffer->n_datas);
  565. for (uint32_t i = 0; i < buffer->n_datas && i < MAX_AV_PLANES; i++) {
  566. blog(LOG_DEBUG, "[pipewire] Plane %u: Dataptr:%p, Linesize:%d", i, out.data[i], out.linesize[i]);
  567. }
  568. #endif
  569. obs_source_output_video(obs_pw_stream->source, &out);
  570. done:
  571. pw_stream_queue_buffer(obs_pw_stream->stream, b);
  572. }
  573. static void process_video_sync(obs_pipewire_stream *obs_pw_stream)
  574. {
  575. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  576. struct spa_meta_cursor *cursor;
  577. struct spa_meta_header *header;
  578. struct spa_meta_region *region;
  579. struct spa_meta_videotransform *video_transform;
  580. struct obs_pw_video_format obs_pw_video_format;
  581. struct spa_buffer *buffer;
  582. struct pw_buffer *b;
  583. bool has_buffer = true;
  584. b = find_latest_buffer(obs_pw_stream->stream);
  585. if (!b) {
  586. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  587. return;
  588. }
  589. buffer = b->buffer;
  590. header = spa_buffer_find_meta_data(buffer, SPA_META_Header, sizeof(*header));
  591. if (header && (header->flags & SPA_META_HEADER_FLAG_CORRUPTED) > 0) {
  592. blog(LOG_ERROR, "[pipewire] buffer is corrupt");
  593. return_unused_pw_buffer(obs_pw_stream->stream, b);
  594. return;
  595. }
  596. obs_enter_graphics();
  597. // Workaround for kwin behaviour pre 5.27.5
  598. // Workaround for mutter behaviour pre GNOME 43
  599. // Only check this if !SPA_META_Header, once supported platforms update.
  600. has_buffer = buffer->datas[0].chunk->size != 0;
  601. if (!has_buffer)
  602. goto read_metadata;
  603. if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
  604. uint32_t planes = get_spa_buffer_plane_count(buffer);
  605. uint32_t *offsets = alloca(sizeof(uint32_t) * planes);
  606. uint32_t *strides = alloca(sizeof(uint32_t) * planes);
  607. uint64_t *modifiers = alloca(sizeof(uint64_t) * planes);
  608. int *fds = alloca(sizeof(int) * planes);
  609. bool use_modifiers;
  610. bool corrupt = false;
  611. #if PW_CHECK_VERSION(1, 2, 0)
  612. struct spa_meta_sync_timeline *synctimeline =
  613. spa_buffer_find_meta_data(buffer, SPA_META_SyncTimeline, sizeof(struct spa_meta_sync_timeline));
  614. #endif
  615. #ifdef DEBUG_PIPEWIRE
  616. blog(LOG_DEBUG, "[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
  617. buffer->datas[0].fd, buffer->datas[0].chunk->stride, buffer->datas[0].chunk->offset,
  618. obs_pw_stream->format.info.raw.size.width, obs_pw_stream->format.info.raw.size.height);
  619. #endif
  620. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  621. obs_pw_video_format.gs_format == GS_UNKNOWN) {
  622. blog(LOG_ERROR, "[pipewire] unsupported DMA buffer format: %d",
  623. obs_pw_stream->format.info.raw.format);
  624. goto read_metadata;
  625. }
  626. for (uint32_t plane = 0; plane < planes; plane++) {
  627. fds[plane] = buffer->datas[plane].fd;
  628. offsets[plane] = buffer->datas[plane].chunk->offset;
  629. strides[plane] = buffer->datas[plane].chunk->stride;
  630. modifiers[plane] = obs_pw_stream->format.info.raw.modifier;
  631. corrupt |= (buffer->datas[plane].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) > 0;
  632. }
  633. #if PW_CHECK_VERSION(1, 2, 0)
  634. if (obs_pw_stream->sync.release_syncobj_fd != -1) {
  635. if (!obs_pw_stream->sync.release_point_will_signal) {
  636. gs_sync_signal_syncobj_timeline_point(obs_pw_stream->sync.release_syncobj_fd,
  637. obs_pw_stream->sync.release_point);
  638. obs_pw_stream->sync.release_point_will_signal = true;
  639. }
  640. }
  641. g_clear_fd(&obs_pw_stream->sync.acquire_syncobj_fd, NULL);
  642. g_clear_fd(&obs_pw_stream->sync.release_syncobj_fd, NULL);
  643. if (synctimeline && (buffer->n_datas == (planes + 2))) {
  644. assert(buffer->datas[planes].type == SPA_DATA_SyncObj);
  645. assert(buffer->datas[planes + 1].type == SPA_DATA_SyncObj);
  646. obs_pw_stream->sync.acquire_syncobj_fd = fcntl(buffer->datas[planes].fd, F_DUPFD_CLOEXEC, 5);
  647. obs_pw_stream->sync.acquire_point = synctimeline->acquire_point;
  648. obs_pw_stream->sync.release_syncobj_fd =
  649. fcntl(buffer->datas[planes + 1].fd, F_DUPFD_CLOEXEC, 5);
  650. obs_pw_stream->sync.release_point = synctimeline->release_point;
  651. obs_pw_stream->sync.release_point_will_signal = false;
  652. obs_pw_stream->sync.set = true;
  653. } else {
  654. obs_pw_stream->sync.set = false;
  655. }
  656. #else
  657. obs_pw_stream->sync.set = false;
  658. #endif
  659. if (corrupt) {
  660. blog(LOG_DEBUG, "[pipewire] buffer contains corrupted data");
  661. goto read_metadata;
  662. }
  663. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  664. use_modifiers = obs_pw_stream->format.info.raw.modifier != DRM_FORMAT_MOD_INVALID;
  665. obs_pw_stream->texture = gs_texture_create_from_dmabuf(
  666. obs_pw_stream->format.info.raw.size.width, obs_pw_stream->format.info.raw.size.height,
  667. obs_pw_video_format.drm_format, obs_pw_video_format.gs_format, planes, fds, strides, offsets,
  668. use_modifiers ? modifiers : NULL);
  669. if (obs_pw_stream->texture == NULL) {
  670. remove_modifier_from_format(obs_pw_stream, obs_pw_stream->format.info.raw.format,
  671. obs_pw_stream->format.info.raw.modifier);
  672. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  673. goto read_metadata;
  674. }
  675. } else {
  676. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  677. if (!obs_pw_video_format_from_spa_format(obs_pw_stream->format.info.raw.format, &obs_pw_video_format) ||
  678. obs_pw_video_format.gs_format == GS_UNKNOWN) {
  679. blog(LOG_ERROR, "[pipewire] unsupported buffer format: %d",
  680. obs_pw_stream->format.info.raw.format);
  681. goto read_metadata;
  682. }
  683. if ((buffer->datas[0].chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) > 0) {
  684. blog(LOG_DEBUG, "[pipewire] buffer contains corrupted data");
  685. goto read_metadata;
  686. }
  687. if (buffer->datas[0].chunk->size == 0) {
  688. blog(LOG_DEBUG, "[pipewire] buffer contains empty data");
  689. goto read_metadata;
  690. }
  691. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  692. obs_pw_stream->texture = gs_texture_create(obs_pw_stream->format.info.raw.size.width,
  693. obs_pw_stream->format.info.raw.size.height,
  694. obs_pw_video_format.gs_format, 1,
  695. (const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
  696. }
  697. if (obs_pw_video_format.swap_red_blue)
  698. swap_texture_red_blue(obs_pw_stream->texture);
  699. /* Video Crop */
  700. region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop, sizeof(*region));
  701. if (region && spa_meta_region_is_valid(region)) {
  702. #ifdef DEBUG_PIPEWIRE
  703. blog(LOG_DEBUG, "[pipewire] Crop Region available (%dx%d+%d+%d)", region->region.position.x,
  704. region->region.position.y, region->region.size.width, region->region.size.height);
  705. #endif
  706. obs_pw_stream->crop.x = region->region.position.x;
  707. obs_pw_stream->crop.y = region->region.position.y;
  708. obs_pw_stream->crop.width = region->region.size.width;
  709. obs_pw_stream->crop.height = region->region.size.height;
  710. obs_pw_stream->crop.valid = true;
  711. } else {
  712. obs_pw_stream->crop.valid = false;
  713. }
  714. /* Video Transform */
  715. video_transform = spa_buffer_find_meta_data(buffer, SPA_META_VideoTransform, sizeof(*video_transform));
  716. if (video_transform)
  717. obs_pw_stream->transform = video_transform->transform;
  718. else
  719. obs_pw_stream->transform = SPA_META_TRANSFORMATION_None;
  720. read_metadata:
  721. /* Cursor */
  722. cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor, sizeof(*cursor));
  723. obs_pw_stream->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
  724. if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid) {
  725. struct spa_meta_bitmap *bitmap = NULL;
  726. if (cursor->bitmap_offset)
  727. bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset, struct spa_meta_bitmap);
  728. if (bitmap)
  729. g_clear_pointer(&obs_pw_stream->cursor.texture, gs_texture_destroy);
  730. if (bitmap && bitmap->size.width > 0 && bitmap->size.height > 0 &&
  731. obs_pw_video_format_from_spa_format(bitmap->format, &obs_pw_video_format) &&
  732. obs_pw_video_format.gs_format != GS_UNKNOWN) {
  733. const uint8_t *bitmap_data;
  734. bitmap_data = SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
  735. obs_pw_stream->cursor.hotspot_x = cursor->hotspot.x;
  736. obs_pw_stream->cursor.hotspot_y = cursor->hotspot.y;
  737. obs_pw_stream->cursor.width = bitmap->size.width;
  738. obs_pw_stream->cursor.height = bitmap->size.height;
  739. assert(obs_pw_stream->cursor.texture == NULL);
  740. obs_pw_stream->cursor.texture =
  741. gs_texture_create(obs_pw_stream->cursor.width, obs_pw_stream->cursor.height,
  742. obs_pw_video_format.gs_format, 1, &bitmap_data, GS_DYNAMIC);
  743. if (obs_pw_video_format.swap_red_blue)
  744. swap_texture_red_blue(obs_pw_stream->cursor.texture);
  745. }
  746. obs_pw_stream->cursor.x = cursor->position.x;
  747. obs_pw_stream->cursor.y = cursor->position.y;
  748. }
  749. pw_stream_queue_buffer(obs_pw_stream->stream, b);
  750. obs_leave_graphics();
  751. }
  752. static void on_process_cb(void *user_data)
  753. {
  754. obs_pipewire_stream *obs_pw_stream = user_data;
  755. uint32_t output_flags;
  756. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  757. if (output_flags & OBS_SOURCE_VIDEO) {
  758. if (output_flags & OBS_SOURCE_ASYNC)
  759. process_video_async(obs_pw_stream);
  760. else
  761. process_video_sync(obs_pw_stream);
  762. }
  763. }
  764. static void on_param_changed_cb(void *user_data, uint32_t id, const struct spa_pod *param)
  765. {
  766. obs_pipewire_stream *obs_pw_stream = user_data;
  767. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  768. struct spa_pod_builder pod_builder;
  769. const struct spa_pod *params[7];
  770. const char *format_name;
  771. uint32_t n_params = 0;
  772. uint32_t buffer_types;
  773. uint32_t output_flags;
  774. uint8_t params_buffer[1024];
  775. int result;
  776. #if PW_CHECK_VERSION(1, 2, 0)
  777. bool supports_explicit_sync = false;
  778. #endif
  779. if (!param || id != SPA_PARAM_Format)
  780. return;
  781. result = spa_format_parse(param, &obs_pw_stream->format.media_type, &obs_pw_stream->format.media_subtype);
  782. if (result < 0)
  783. return;
  784. if (obs_pw_stream->format.media_type != SPA_MEDIA_TYPE_video ||
  785. obs_pw_stream->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
  786. return;
  787. spa_format_video_raw_parse(param, &obs_pw_stream->format.info.raw);
  788. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  789. buffer_types = 1 << SPA_DATA_MemPtr;
  790. bool has_modifier = spa_pod_find_prop(param, NULL, SPA_FORMAT_VIDEO_modifier) != NULL;
  791. if ((has_modifier || check_pw_version(&obs_pw->server_version, 0, 3, 24)) &&
  792. (output_flags & OBS_SOURCE_ASYNC_VIDEO) != OBS_SOURCE_ASYNC_VIDEO) {
  793. buffer_types |= 1 << SPA_DATA_DmaBuf;
  794. #if PW_CHECK_VERSION(1, 2, 0)
  795. obs_enter_graphics();
  796. supports_explicit_sync = gs_query_sync_capabilities();
  797. obs_leave_graphics();
  798. #endif
  799. }
  800. blog(LOG_INFO, "[pipewire] Negotiated format:");
  801. format_name = spa_debug_type_find_name(spa_type_video_format, obs_pw_stream->format.info.raw.format);
  802. blog(LOG_INFO, "[pipewire] Format: %d (%s)", obs_pw_stream->format.info.raw.format,
  803. format_name ? format_name : "unknown format");
  804. if (has_modifier) {
  805. blog(LOG_INFO, "[pipewire] Modifier: 0x%" PRIx64, obs_pw_stream->format.info.raw.modifier);
  806. }
  807. blog(LOG_INFO, "[pipewire] Size: %dx%d", obs_pw_stream->format.info.raw.size.width,
  808. obs_pw_stream->format.info.raw.size.height);
  809. blog(LOG_INFO, "[pipewire] Framerate: %d/%d", obs_pw_stream->format.info.raw.framerate.num,
  810. obs_pw_stream->format.info.raw.framerate.denom);
  811. /* Video crop */
  812. pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  813. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  814. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
  815. SPA_PARAM_META_size,
  816. SPA_POD_Int(sizeof(struct spa_meta_region)));
  817. /* Cursor */
  818. params[n_params++] =
  819. spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, SPA_PARAM_META_type,
  820. SPA_POD_Id(SPA_META_Cursor), SPA_PARAM_META_size,
  821. SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64), CURSOR_META_SIZE(1, 1),
  822. CURSOR_META_SIZE(1024, 1024)));
  823. /* Buffer options */
  824. #if PW_CHECK_VERSION(1, 2, 0)
  825. if (supports_explicit_sync) {
  826. struct spa_pod_frame dmabuf_explicit_sync_frame;
  827. spa_pod_builder_push_object(&pod_builder, &dmabuf_explicit_sync_frame, SPA_TYPE_OBJECT_ParamBuffers,
  828. SPA_PARAM_Buffers);
  829. spa_pod_builder_add(&pod_builder, SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(1 << SPA_DATA_DmaBuf), 0);
  830. spa_pod_builder_prop(&pod_builder, SPA_PARAM_BUFFERS_metaType, SPA_POD_PROP_FLAG_MANDATORY);
  831. spa_pod_builder_int(&pod_builder, 1 << SPA_META_SyncTimeline);
  832. params[n_params++] = spa_pod_builder_pop(&pod_builder, &dmabuf_explicit_sync_frame);
  833. }
  834. #endif
  835. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
  836. SPA_PARAM_BUFFERS_dataType, SPA_POD_Int(buffer_types));
  837. /* Sync timeline */
  838. #if PW_CHECK_VERSION(1, 2, 0)
  839. if (supports_explicit_sync) {
  840. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  841. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_SyncTimeline),
  842. SPA_PARAM_META_size,
  843. SPA_POD_Int(sizeof(struct spa_meta_sync_timeline)));
  844. }
  845. #endif
  846. /* Meta header */
  847. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  848. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
  849. SPA_PARAM_META_size,
  850. SPA_POD_Int(sizeof(struct spa_meta_header)));
  851. #if PW_CHECK_VERSION(0, 3, 62)
  852. if (check_pw_version(&obs_pw->server_version, 0, 3, 62)) {
  853. /* Video transformation */
  854. params[n_params++] = spa_pod_builder_add_object(&pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  855. SPA_PARAM_META_type,
  856. SPA_POD_Id(SPA_META_VideoTransform),
  857. SPA_PARAM_META_size,
  858. SPA_POD_Int(sizeof(struct spa_meta_videotransform)));
  859. }
  860. #endif
  861. pw_stream_update_params(obs_pw_stream->stream, params, n_params);
  862. obs_pw_stream->negotiated = true;
  863. }
  864. static void on_state_changed_cb(void *user_data, enum pw_stream_state old, enum pw_stream_state state,
  865. const char *error)
  866. {
  867. UNUSED_PARAMETER(old);
  868. obs_pipewire_stream *obs_pw_stream = user_data;
  869. blog(LOG_INFO, "[pipewire] Stream %p state: \"%s\" (error: %s)", obs_pw_stream->stream,
  870. pw_stream_state_as_string(state), error ? error : "none");
  871. }
  872. static const struct pw_stream_events stream_events = {
  873. PW_VERSION_STREAM_EVENTS,
  874. .state_changed = on_state_changed_cb,
  875. .param_changed = on_param_changed_cb,
  876. .process = on_process_cb,
  877. };
  878. static void on_core_info_cb(void *user_data, const struct pw_core_info *info)
  879. {
  880. obs_pipewire *obs_pw = user_data;
  881. update_pw_versions(obs_pw, info->version);
  882. }
  883. static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res, const char *message)
  884. {
  885. obs_pipewire *obs_pw = user_data;
  886. blog(LOG_ERROR, "[pipewire] Error id:%u seq:%d res:%d (%s): %s", id, seq, res, spa_strerror(res), message);
  887. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  888. }
  889. static void on_core_done_cb(void *user_data, uint32_t id, int seq)
  890. {
  891. obs_pipewire *obs_pw = user_data;
  892. if (id == PW_ID_CORE && obs_pw->sync_id == seq)
  893. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  894. }
  895. static const struct pw_core_events core_events = {
  896. PW_VERSION_CORE_EVENTS,
  897. .info = on_core_info_cb,
  898. .done = on_core_done_cb,
  899. .error = on_core_error_cb,
  900. };
  901. /* obs_source_info methods */
  902. obs_pipewire *obs_pipewire_connect_fd(int pipewire_fd, const struct pw_registry_events *registry_events,
  903. void *user_data)
  904. {
  905. obs_pipewire *obs_pw;
  906. obs_pw = bzalloc(sizeof(obs_pipewire));
  907. obs_pw->pipewire_fd = pipewire_fd;
  908. obs_pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  909. obs_pw->context = pw_context_new(pw_thread_loop_get_loop(obs_pw->thread_loop), NULL, 0);
  910. if (pw_thread_loop_start(obs_pw->thread_loop) < 0) {
  911. blog(LOG_WARNING, "Error starting threaded mainloop");
  912. bfree(obs_pw);
  913. return NULL;
  914. }
  915. pw_thread_loop_lock(obs_pw->thread_loop);
  916. /* Core */
  917. obs_pw->core = pw_context_connect_fd(obs_pw->context, fcntl(obs_pw->pipewire_fd, F_DUPFD_CLOEXEC, 5), NULL, 0);
  918. if (!obs_pw->core) {
  919. blog(LOG_WARNING, "Error creating PipeWire core: %m");
  920. pw_thread_loop_unlock(obs_pw->thread_loop);
  921. bfree(obs_pw);
  922. return NULL;
  923. }
  924. pw_core_add_listener(obs_pw->core, &obs_pw->core_listener, &core_events, obs_pw);
  925. // Dispatch to receive the info core event
  926. obs_pw->sync_id = pw_core_sync(obs_pw->core, PW_ID_CORE, obs_pw->sync_id);
  927. pw_thread_loop_wait(obs_pw->thread_loop);
  928. /* Registry */
  929. if (registry_events) {
  930. obs_pw->registry = pw_core_get_registry(obs_pw->core, PW_VERSION_REGISTRY, 0);
  931. pw_registry_add_listener(obs_pw->registry, &obs_pw->registry_listener, registry_events, user_data);
  932. blog(LOG_INFO, "[pipewire] Created registry %p", obs_pw->registry);
  933. }
  934. pw_thread_loop_unlock(obs_pw->thread_loop);
  935. obs_pw->streams = g_ptr_array_new();
  936. return obs_pw;
  937. }
  938. struct pw_registry *obs_pipewire_get_registry(obs_pipewire *obs_pw)
  939. {
  940. return obs_pw->registry;
  941. }
  942. void obs_pipewire_roundtrip(obs_pipewire *obs_pw)
  943. {
  944. pw_thread_loop_lock(obs_pw->thread_loop);
  945. obs_pw->sync_id = pw_core_sync(obs_pw->core, PW_ID_CORE, obs_pw->sync_id);
  946. pw_thread_loop_wait(obs_pw->thread_loop);
  947. pw_thread_loop_unlock(obs_pw->thread_loop);
  948. }
  949. void obs_pipewire_destroy(obs_pipewire *obs_pw)
  950. {
  951. if (!obs_pw)
  952. return;
  953. while (obs_pw->streams->len > 0) {
  954. obs_pipewire_stream *obs_pw_stream = g_ptr_array_index(obs_pw->streams, 0);
  955. obs_pipewire_stream_destroy(obs_pw_stream);
  956. }
  957. g_clear_pointer(&obs_pw->streams, g_ptr_array_unref);
  958. teardown_pipewire(obs_pw);
  959. bfree(obs_pw);
  960. }
  961. obs_pipewire_stream *obs_pipewire_connect_stream(obs_pipewire *obs_pw, obs_source_t *source, int pipewire_node,
  962. const struct obs_pipwire_connect_stream_info *connect_info)
  963. {
  964. struct spa_pod_builder pod_builder;
  965. const struct spa_pod **params = NULL;
  966. obs_pipewire_stream *obs_pw_stream;
  967. uint32_t n_params;
  968. uint8_t params_buffer[4096];
  969. assert(connect_info != NULL);
  970. obs_pw_stream = bzalloc(sizeof(obs_pipewire_stream));
  971. obs_pw_stream->obs_pw = obs_pw;
  972. obs_pw_stream->source = source;
  973. obs_pw_stream->cursor.visible = connect_info->screencast.cursor_visible;
  974. obs_pw_stream->framerate.set = connect_info->video.framerate != NULL;
  975. obs_pw_stream->resolution.set = connect_info->video.resolution != NULL;
  976. obs_pw_stream->sync.acquire_syncobj_fd = -1;
  977. obs_pw_stream->sync.release_syncobj_fd = -1;
  978. if (obs_pw_stream->framerate.set)
  979. obs_pw_stream->framerate.fraction = *connect_info->video.framerate;
  980. if (obs_pw_stream->resolution.set)
  981. obs_pw_stream->resolution.rect = *connect_info->video.resolution;
  982. init_format_info(obs_pw_stream);
  983. pw_thread_loop_lock(obs_pw->thread_loop);
  984. /* Signal to renegotiate */
  985. obs_pw_stream->reneg =
  986. pw_loop_add_event(pw_thread_loop_get_loop(obs_pw->thread_loop), renegotiate_format, obs_pw_stream);
  987. blog(LOG_DEBUG, "[pipewire] registered event %p", obs_pw_stream->reneg);
  988. /* Stream */
  989. obs_pw_stream->stream = pw_stream_new(obs_pw->core, connect_info->stream_name, connect_info->stream_properties);
  990. pw_stream_add_listener(obs_pw_stream->stream, &obs_pw_stream->stream_listener, &stream_events, obs_pw_stream);
  991. blog(LOG_INFO, "[pipewire] Created stream %p", obs_pw_stream->stream);
  992. /* Stream parameters */
  993. pod_builder = SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  994. obs_get_video_info(&obs_pw_stream->video_info);
  995. if (!build_format_params(obs_pw_stream, &pod_builder, &params, &n_params)) {
  996. pw_thread_loop_unlock(obs_pw->thread_loop);
  997. bfree(obs_pw_stream);
  998. return NULL;
  999. }
  1000. pw_stream_connect(obs_pw_stream->stream, PW_DIRECTION_INPUT, pipewire_node,
  1001. PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, n_params);
  1002. blog(LOG_INFO, "[pipewire] Playing stream %p", obs_pw_stream->stream);
  1003. pw_thread_loop_unlock(obs_pw->thread_loop);
  1004. bfree(params);
  1005. g_ptr_array_add(obs_pw->streams, obs_pw_stream);
  1006. return obs_pw_stream;
  1007. }
  1008. void obs_pipewire_stream_show(obs_pipewire_stream *obs_pw_stream)
  1009. {
  1010. if (obs_pw_stream->stream) {
  1011. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1012. pw_stream_set_active(obs_pw_stream->stream, true);
  1013. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1014. }
  1015. }
  1016. void obs_pipewire_stream_hide(obs_pipewire_stream *obs_pw_stream)
  1017. {
  1018. if (obs_pw_stream->stream) {
  1019. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1020. pw_stream_set_active(obs_pw_stream->stream, false);
  1021. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1022. }
  1023. }
  1024. uint32_t obs_pipewire_stream_get_width(obs_pipewire_stream *obs_pw_stream)
  1025. {
  1026. bool has_crop;
  1027. if (!obs_pw_stream->negotiated)
  1028. return 0;
  1029. has_crop = has_effective_crop(obs_pw_stream);
  1030. switch (obs_pw_stream->transform) {
  1031. case SPA_META_TRANSFORMATION_Flipped:
  1032. case SPA_META_TRANSFORMATION_None:
  1033. case SPA_META_TRANSFORMATION_Flipped180:
  1034. case SPA_META_TRANSFORMATION_180:
  1035. return has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  1036. case SPA_META_TRANSFORMATION_Flipped90:
  1037. case SPA_META_TRANSFORMATION_90:
  1038. case SPA_META_TRANSFORMATION_Flipped270:
  1039. case SPA_META_TRANSFORMATION_270:
  1040. return has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  1041. default:
  1042. return 0;
  1043. }
  1044. }
  1045. uint32_t obs_pipewire_stream_get_height(obs_pipewire_stream *obs_pw_stream)
  1046. {
  1047. bool has_crop;
  1048. if (!obs_pw_stream->negotiated)
  1049. return 0;
  1050. has_crop = has_effective_crop(obs_pw_stream);
  1051. switch (obs_pw_stream->transform) {
  1052. case SPA_META_TRANSFORMATION_Flipped:
  1053. case SPA_META_TRANSFORMATION_None:
  1054. case SPA_META_TRANSFORMATION_Flipped180:
  1055. case SPA_META_TRANSFORMATION_180:
  1056. return has_crop ? obs_pw_stream->crop.height : obs_pw_stream->format.info.raw.size.height;
  1057. case SPA_META_TRANSFORMATION_Flipped90:
  1058. case SPA_META_TRANSFORMATION_90:
  1059. case SPA_META_TRANSFORMATION_Flipped270:
  1060. case SPA_META_TRANSFORMATION_270:
  1061. return has_crop ? obs_pw_stream->crop.width : obs_pw_stream->format.info.raw.size.width;
  1062. default:
  1063. return 0;
  1064. }
  1065. }
  1066. void obs_pipewire_stream_video_render(obs_pipewire_stream *obs_pw_stream, gs_effect_t *effect)
  1067. {
  1068. bool rotated;
  1069. int flip = 0;
  1070. gs_eparam_t *image;
  1071. if (!obs_pw_stream->texture)
  1072. return;
  1073. if (obs_pw_stream->sync.set) {
  1074. gs_sync_t *acquire_sync = gs_sync_create_from_syncobj_timeline_point(
  1075. obs_pw_stream->sync.acquire_syncobj_fd, obs_pw_stream->sync.acquire_point);
  1076. gs_sync_wait(acquire_sync);
  1077. gs_sync_destroy(acquire_sync);
  1078. }
  1079. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1080. gs_technique_t *tech = gs_effect_get_technique(effect, "DrawSrgbDecompress");
  1081. gs_technique_begin(tech);
  1082. gs_technique_begin_pass(tech, 0);
  1083. const bool linear_srgb = gs_get_linear_srgb();
  1084. const bool previous = gs_framebuffer_srgb_enabled();
  1085. gs_enable_framebuffer_srgb(linear_srgb);
  1086. image = gs_effect_get_param_by_name(effect, "image");
  1087. if (linear_srgb)
  1088. gs_effect_set_texture_srgb(image, obs_pw_stream->texture);
  1089. else
  1090. gs_effect_set_texture(image, obs_pw_stream->texture);
  1091. rotated = push_rotation(obs_pw_stream);
  1092. flip = get_buffer_flip(obs_pw_stream);
  1093. /* There is a SPA_VIDEO_FLAG_PREMULTIPLIED_ALPHA flag, but it does not
  1094. * seem to be fully implemented nor ever set. Just assume premultiplied
  1095. * always, which seems to be the default convention.
  1096. *
  1097. * See https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3126
  1098. *
  1099. * The cursor bitmap alpha mode is also not specified, and the
  1100. * convention there also seems to be premultiplied. */
  1101. gs_blend_state_push();
  1102. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1103. if (has_effective_crop(obs_pw_stream)) {
  1104. gs_draw_sprite_subregion(obs_pw_stream->texture, flip, obs_pw_stream->crop.x, obs_pw_stream->crop.y,
  1105. obs_pw_stream->crop.width, obs_pw_stream->crop.height);
  1106. } else {
  1107. gs_draw_sprite(obs_pw_stream->texture, flip, 0, 0);
  1108. }
  1109. if (rotated)
  1110. gs_matrix_pop();
  1111. if (obs_pw_stream->cursor.visible && obs_pw_stream->cursor.valid && obs_pw_stream->cursor.texture) {
  1112. float cursor_x = obs_pw_stream->cursor.x - obs_pw_stream->cursor.hotspot_x;
  1113. float cursor_y = obs_pw_stream->cursor.y - obs_pw_stream->cursor.hotspot_y;
  1114. gs_matrix_push();
  1115. gs_matrix_translate3f(cursor_x, cursor_y, 0.0f);
  1116. if (linear_srgb)
  1117. gs_effect_set_texture_srgb(image, obs_pw_stream->cursor.texture);
  1118. else
  1119. gs_effect_set_texture(image, obs_pw_stream->cursor.texture);
  1120. gs_draw_sprite(obs_pw_stream->texture, 0, obs_pw_stream->cursor.width, obs_pw_stream->cursor.height);
  1121. gs_matrix_pop();
  1122. }
  1123. gs_blend_state_pop();
  1124. gs_enable_framebuffer_srgb(previous);
  1125. gs_technique_end_pass(tech);
  1126. gs_technique_end(tech);
  1127. if (obs_pw_stream->sync.set) {
  1128. gs_sync_t *release_sync = gs_sync_create();
  1129. gs_sync_export_syncobj_timeline_point(release_sync, obs_pw_stream->sync.release_syncobj_fd,
  1130. obs_pw_stream->sync.release_point);
  1131. gs_sync_destroy(release_sync);
  1132. obs_pw_stream->sync.release_point_will_signal = true;
  1133. }
  1134. }
  1135. void obs_pipewire_stream_set_cursor_visible(obs_pipewire_stream *obs_pw_stream, bool cursor_visible)
  1136. {
  1137. obs_pw_stream->cursor.visible = cursor_visible;
  1138. }
  1139. void obs_pipewire_stream_destroy(obs_pipewire_stream *obs_pw_stream)
  1140. {
  1141. uint32_t output_flags;
  1142. if (!obs_pw_stream)
  1143. return;
  1144. output_flags = obs_source_get_output_flags(obs_pw_stream->source);
  1145. if (output_flags & OBS_SOURCE_ASYNC_VIDEO)
  1146. obs_source_output_video(obs_pw_stream->source, NULL);
  1147. g_ptr_array_remove(obs_pw_stream->obs_pw->streams, obs_pw_stream);
  1148. obs_enter_graphics();
  1149. g_clear_pointer(&obs_pw_stream->cursor.texture, gs_texture_destroy);
  1150. g_clear_pointer(&obs_pw_stream->texture, gs_texture_destroy);
  1151. obs_leave_graphics();
  1152. pw_thread_loop_lock(obs_pw_stream->obs_pw->thread_loop);
  1153. if (obs_pw_stream->stream)
  1154. pw_stream_disconnect(obs_pw_stream->stream);
  1155. g_clear_pointer(&obs_pw_stream->stream, pw_stream_destroy);
  1156. pw_thread_loop_unlock(obs_pw_stream->obs_pw->thread_loop);
  1157. g_clear_fd(&obs_pw_stream->sync.acquire_syncobj_fd, NULL);
  1158. g_clear_fd(&obs_pw_stream->sync.release_syncobj_fd, NULL);
  1159. clear_format_info(obs_pw_stream);
  1160. bfree(obs_pw_stream);
  1161. }
  1162. void obs_pipewire_stream_set_framerate(obs_pipewire_stream *obs_pw_stream, const struct spa_fraction *framerate)
  1163. {
  1164. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  1165. if ((!obs_pw_stream->framerate.set && !framerate) ||
  1166. (obs_pw_stream->framerate.set && framerate && obs_pw_stream->framerate.fraction.num == framerate->num &&
  1167. obs_pw_stream->framerate.fraction.denom == framerate->denom))
  1168. return;
  1169. if (framerate) {
  1170. obs_pw_stream->framerate.fraction = *framerate;
  1171. obs_pw_stream->framerate.set = true;
  1172. } else {
  1173. obs_pw_stream->framerate.fraction = SPA_FRACTION(0, 0);
  1174. obs_pw_stream->framerate.set = false;
  1175. }
  1176. /* Signal to renegotiate */
  1177. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  1178. }
  1179. void obs_pipewire_stream_set_resolution(obs_pipewire_stream *obs_pw_stream, const struct spa_rectangle *resolution)
  1180. {
  1181. obs_pipewire *obs_pw = obs_pw_stream->obs_pw;
  1182. if ((!obs_pw_stream->resolution.set && !resolution) ||
  1183. (obs_pw_stream->resolution.set && resolution && obs_pw_stream->resolution.rect.width == resolution->width &&
  1184. obs_pw_stream->resolution.rect.height == resolution->height))
  1185. return;
  1186. if (resolution) {
  1187. obs_pw_stream->resolution.rect = *resolution;
  1188. obs_pw_stream->resolution.set = true;
  1189. } else {
  1190. obs_pw_stream->resolution.rect = SPA_RECTANGLE(0, 0);
  1191. obs_pw_stream->resolution.set = false;
  1192. }
  1193. /* Signal to renegotiate */
  1194. pw_loop_signal_event(pw_thread_loop_get_loop(obs_pw->thread_loop), obs_pw_stream->reneg);
  1195. }