pipewire.c 42 KB

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