pipewire.c 30 KB

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