pipewire.c 31 KB

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