pipewire.c 42 KB

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