pipewire.c 39 KB

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