pipewire.c 46 KB

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