pipewire.c 46 KB

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