pipewire.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 "portal.h"
  22. #include <util/dstr.h>
  23. #include <gio/gio.h>
  24. #include <gio/gunixfdlist.h>
  25. #include <fcntl.h>
  26. #include <glad/glad.h>
  27. #include <linux/dma-buf.h>
  28. #include <spa/param/video/format-utils.h>
  29. #include <spa/debug/format.h>
  30. #include <spa/debug/types.h>
  31. #include <spa/param/video/type-info.h>
  32. #define REQUEST_PATH "/org/freedesktop/portal/desktop/request/%s/obs%u"
  33. #define SESSION_PATH "/org/freedesktop/portal/desktop/session/%s/obs%u"
  34. #define CURSOR_META_SIZE(width, height) \
  35. (sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + \
  36. width * height * 4)
  37. #define fourcc_code(a, b, c, d) \
  38. ((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | \
  39. ((__u32)(d) << 24))
  40. #define DRM_FORMAT_XRGB8888 \
  41. fourcc_code('X', 'R', '2', \
  42. '4') /* [31:0] x:R:G:B 8:8:8:8 little endian */
  43. #define DRM_FORMAT_XBGR8888 \
  44. fourcc_code('X', 'B', '2', \
  45. '4') /* [31:0] x:B:G:R 8:8:8:8 little endian */
  46. #define DRM_FORMAT_ARGB8888 \
  47. fourcc_code('A', 'R', '2', \
  48. '4') /* [31:0] A:R:G:B 8:8:8:8 little endian */
  49. #define DRM_FORMAT_ABGR8888 \
  50. fourcc_code('A', 'B', '2', \
  51. '4') /* [31:0] A:B:G:R 8:8:8:8 little endian */
  52. struct _obs_pipewire_data {
  53. GCancellable *cancellable;
  54. char *sender_name;
  55. char *session_handle;
  56. uint32_t pipewire_node;
  57. int pipewire_fd;
  58. uint32_t available_cursor_modes;
  59. obs_source_t *source;
  60. obs_data_t *settings;
  61. gs_texture_t *texture;
  62. struct pw_thread_loop *thread_loop;
  63. struct pw_context *context;
  64. struct pw_core *core;
  65. struct spa_hook core_listener;
  66. struct pw_stream *stream;
  67. struct spa_hook stream_listener;
  68. struct spa_video_info format;
  69. struct {
  70. bool valid;
  71. int x, y;
  72. uint32_t width, height;
  73. } crop;
  74. struct {
  75. bool visible;
  76. bool valid;
  77. int x, y;
  78. int hotspot_x, hotspot_y;
  79. int width, height;
  80. gs_texture_t *texture;
  81. } cursor;
  82. enum obs_pw_capture_type capture_type;
  83. struct obs_video_info video_info;
  84. bool negotiated;
  85. };
  86. struct dbus_call_data {
  87. obs_pipewire_data *obs_pw;
  88. char *request_path;
  89. guint signal_id;
  90. gulong cancelled_id;
  91. };
  92. /* auxiliary methods */
  93. static const char *capture_type_to_string(enum obs_pw_capture_type capture_type)
  94. {
  95. switch (capture_type) {
  96. case DESKTOP_CAPTURE:
  97. return "desktop";
  98. case WINDOW_CAPTURE:
  99. return "window";
  100. }
  101. return "unknown";
  102. }
  103. static void new_request_path(obs_pipewire_data *data, char **out_path,
  104. char **out_token)
  105. {
  106. static uint32_t request_token_count = 0;
  107. request_token_count++;
  108. if (out_token) {
  109. struct dstr str;
  110. dstr_init(&str);
  111. dstr_printf(&str, "obs%u", request_token_count);
  112. *out_token = str.array;
  113. }
  114. if (out_path) {
  115. struct dstr str;
  116. dstr_init(&str);
  117. dstr_printf(&str, REQUEST_PATH, data->sender_name,
  118. request_token_count);
  119. *out_path = str.array;
  120. }
  121. }
  122. static void new_session_path(obs_pipewire_data *data, char **out_path,
  123. char **out_token)
  124. {
  125. static uint32_t session_token_count = 0;
  126. session_token_count++;
  127. if (out_token) {
  128. struct dstr str;
  129. dstr_init(&str);
  130. dstr_printf(&str, "obs%u", session_token_count);
  131. *out_token = str.array;
  132. }
  133. if (out_path) {
  134. struct dstr str;
  135. dstr_init(&str);
  136. dstr_printf(&str, SESSION_PATH, data->sender_name,
  137. session_token_count);
  138. *out_path = str.array;
  139. }
  140. }
  141. static void on_cancelled_cb(GCancellable *cancellable, void *data)
  142. {
  143. UNUSED_PARAMETER(cancellable);
  144. struct dbus_call_data *call = data;
  145. blog(LOG_INFO, "[pipewire] screencast session cancelled");
  146. g_dbus_connection_call(
  147. portal_get_dbus_connection(), "org.freedesktop.portal.Desktop",
  148. call->request_path, "org.freedesktop.portal.Request", "Close",
  149. NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  150. }
  151. static struct dbus_call_data *subscribe_to_signal(obs_pipewire_data *obs_pw,
  152. const char *path,
  153. GDBusSignalCallback callback)
  154. {
  155. struct dbus_call_data *call;
  156. call = bzalloc(sizeof(struct dbus_call_data));
  157. call->obs_pw = obs_pw;
  158. call->request_path = bstrdup(path);
  159. call->cancelled_id = g_signal_connect(obs_pw->cancellable, "cancelled",
  160. G_CALLBACK(on_cancelled_cb),
  161. call);
  162. call->signal_id = g_dbus_connection_signal_subscribe(
  163. portal_get_dbus_connection(), "org.freedesktop.portal.Desktop",
  164. "org.freedesktop.portal.Request", "Response",
  165. call->request_path, NULL, G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
  166. callback, call, NULL);
  167. return call;
  168. }
  169. static void dbus_call_data_free(struct dbus_call_data *call)
  170. {
  171. if (!call)
  172. return;
  173. if (call->signal_id)
  174. g_dbus_connection_signal_unsubscribe(
  175. portal_get_dbus_connection(), call->signal_id);
  176. if (call->cancelled_id > 0)
  177. g_signal_handler_disconnect(call->obs_pw->cancellable,
  178. call->cancelled_id);
  179. g_clear_pointer(&call->request_path, bfree);
  180. bfree(call);
  181. }
  182. static void teardown_pipewire(obs_pipewire_data *obs_pw)
  183. {
  184. if (obs_pw->thread_loop) {
  185. pw_thread_loop_wait(obs_pw->thread_loop);
  186. pw_thread_loop_stop(obs_pw->thread_loop);
  187. }
  188. if (obs_pw->stream)
  189. pw_stream_disconnect(obs_pw->stream);
  190. g_clear_pointer(&obs_pw->stream, pw_stream_destroy);
  191. g_clear_pointer(&obs_pw->context, pw_context_destroy);
  192. g_clear_pointer(&obs_pw->thread_loop, pw_thread_loop_destroy);
  193. if (obs_pw->pipewire_fd > 0) {
  194. close(obs_pw->pipewire_fd);
  195. obs_pw->pipewire_fd = 0;
  196. }
  197. obs_pw->negotiated = false;
  198. }
  199. static void destroy_session(obs_pipewire_data *obs_pw)
  200. {
  201. if (obs_pw->session_handle) {
  202. g_dbus_connection_call(portal_get_dbus_connection(),
  203. "org.freedesktop.portal.Desktop",
  204. obs_pw->session_handle,
  205. "org.freedesktop.portal.Session",
  206. "Close", NULL, NULL,
  207. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL,
  208. NULL);
  209. g_clear_pointer(&obs_pw->session_handle, g_free);
  210. }
  211. g_clear_pointer(&obs_pw->sender_name, bfree);
  212. g_clear_pointer(&obs_pw->cursor.texture, gs_texture_destroy);
  213. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  214. g_cancellable_cancel(obs_pw->cancellable);
  215. g_clear_object(&obs_pw->cancellable);
  216. }
  217. static inline bool has_effective_crop(obs_pipewire_data *obs_pw)
  218. {
  219. return obs_pw->crop.valid &&
  220. (obs_pw->crop.x != 0 || obs_pw->crop.y != 0 ||
  221. obs_pw->crop.width < obs_pw->format.info.raw.size.width ||
  222. obs_pw->crop.height < obs_pw->format.info.raw.size.height);
  223. }
  224. static bool spa_pixel_format_to_drm_format(uint32_t spa_format,
  225. uint32_t *out_format)
  226. {
  227. switch (spa_format) {
  228. case SPA_VIDEO_FORMAT_RGBA:
  229. *out_format = DRM_FORMAT_ABGR8888;
  230. break;
  231. case SPA_VIDEO_FORMAT_RGBx:
  232. *out_format = DRM_FORMAT_XBGR8888;
  233. break;
  234. case SPA_VIDEO_FORMAT_BGRA:
  235. *out_format = DRM_FORMAT_ARGB8888;
  236. break;
  237. case SPA_VIDEO_FORMAT_BGRx:
  238. *out_format = DRM_FORMAT_XRGB8888;
  239. break;
  240. default:
  241. return false;
  242. }
  243. return true;
  244. }
  245. static bool spa_pixel_format_to_obs_format(uint32_t spa_format,
  246. enum gs_color_format *out_format,
  247. bool *swap_red_blue)
  248. {
  249. switch (spa_format) {
  250. case SPA_VIDEO_FORMAT_RGBA:
  251. *out_format = GS_RGBA;
  252. *swap_red_blue = false;
  253. break;
  254. case SPA_VIDEO_FORMAT_RGBx:
  255. *out_format = GS_BGRX;
  256. *swap_red_blue = true;
  257. break;
  258. case SPA_VIDEO_FORMAT_BGRA:
  259. *out_format = GS_BGRA;
  260. *swap_red_blue = false;
  261. break;
  262. case SPA_VIDEO_FORMAT_BGRx:
  263. *out_format = GS_BGRX;
  264. *swap_red_blue = false;
  265. break;
  266. default:
  267. return false;
  268. }
  269. return true;
  270. }
  271. static void swap_texture_red_blue(gs_texture_t *texture)
  272. {
  273. GLuint gl_texure = *(GLuint *)gs_texture_get_obj(texture);
  274. glBindTexture(GL_TEXTURE_2D, gl_texure);
  275. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  276. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  277. glBindTexture(GL_TEXTURE_2D, 0);
  278. }
  279. /* ------------------------------------------------- */
  280. static void on_process_cb(void *user_data)
  281. {
  282. obs_pipewire_data *obs_pw = user_data;
  283. struct spa_meta_cursor *cursor;
  284. uint32_t drm_format;
  285. struct spa_meta_region *region;
  286. struct spa_buffer *buffer;
  287. struct pw_buffer *b;
  288. bool swap_red_blue = false;
  289. bool has_buffer;
  290. /* Find the most recent buffer */
  291. b = NULL;
  292. while (true) {
  293. struct pw_buffer *aux =
  294. pw_stream_dequeue_buffer(obs_pw->stream);
  295. if (!aux)
  296. break;
  297. if (b)
  298. pw_stream_queue_buffer(obs_pw->stream, b);
  299. b = aux;
  300. }
  301. if (!b) {
  302. blog(LOG_DEBUG, "[pipewire] Out of buffers!");
  303. return;
  304. }
  305. buffer = b->buffer;
  306. has_buffer = buffer->datas[0].chunk->size != 0;
  307. obs_enter_graphics();
  308. if (!has_buffer)
  309. goto read_metadata;
  310. if (buffer->datas[0].type == SPA_DATA_DmaBuf) {
  311. uint32_t offsets[1];
  312. uint32_t strides[1];
  313. uint64_t modifiers[1];
  314. int fds[1];
  315. blog(LOG_DEBUG,
  316. "[pipewire] DMA-BUF info: fd:%ld, stride:%d, offset:%u, size:%dx%d",
  317. buffer->datas[0].fd, buffer->datas[0].chunk->stride,
  318. buffer->datas[0].chunk->offset,
  319. obs_pw->format.info.raw.size.width,
  320. obs_pw->format.info.raw.size.height);
  321. if (!spa_pixel_format_to_drm_format(
  322. obs_pw->format.info.raw.format, &drm_format)) {
  323. blog(LOG_ERROR,
  324. "[pipewire] unsupported DMA buffer format: %d",
  325. obs_pw->format.info.raw.format);
  326. goto read_metadata;
  327. }
  328. fds[0] = buffer->datas[0].fd;
  329. offsets[0] = buffer->datas[0].chunk->offset;
  330. strides[0] = buffer->datas[0].chunk->stride;
  331. modifiers[0] = obs_pw->format.info.raw.modifier;
  332. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  333. obs_pw->texture = gs_texture_create_from_dmabuf(
  334. obs_pw->format.info.raw.size.width,
  335. obs_pw->format.info.raw.size.height, drm_format,
  336. GS_BGRX, 1, fds, strides, offsets, modifiers);
  337. } else {
  338. blog(LOG_DEBUG, "[pipewire] Buffer has memory texture");
  339. enum gs_color_format obs_format;
  340. if (!spa_pixel_format_to_obs_format(
  341. obs_pw->format.info.raw.format, &obs_format,
  342. &swap_red_blue)) {
  343. blog(LOG_ERROR,
  344. "[pipewire] unsupported DMA buffer format: %d",
  345. obs_pw->format.info.raw.format);
  346. goto read_metadata;
  347. }
  348. g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
  349. obs_pw->texture = gs_texture_create(
  350. obs_pw->format.info.raw.size.width,
  351. obs_pw->format.info.raw.size.height, obs_format, 1,
  352. (const uint8_t **)&buffer->datas[0].data, GS_DYNAMIC);
  353. }
  354. if (swap_red_blue)
  355. swap_texture_red_blue(obs_pw->texture);
  356. /* Video Crop */
  357. region = spa_buffer_find_meta_data(buffer, SPA_META_VideoCrop,
  358. sizeof(*region));
  359. if (region && spa_meta_region_is_valid(region)) {
  360. blog(LOG_DEBUG,
  361. "[pipewire] Crop Region available (%dx%d+%d+%d)",
  362. region->region.position.x, region->region.position.y,
  363. region->region.size.width, region->region.size.height);
  364. obs_pw->crop.x = region->region.position.x;
  365. obs_pw->crop.y = region->region.position.y;
  366. obs_pw->crop.width = region->region.size.width;
  367. obs_pw->crop.height = region->region.size.height;
  368. obs_pw->crop.valid = true;
  369. } else {
  370. obs_pw->crop.valid = false;
  371. }
  372. read_metadata:
  373. /* Cursor */
  374. cursor = spa_buffer_find_meta_data(buffer, SPA_META_Cursor,
  375. sizeof(*cursor));
  376. obs_pw->cursor.valid = cursor && spa_meta_cursor_is_valid(cursor);
  377. if (obs_pw->cursor.visible && obs_pw->cursor.valid) {
  378. struct spa_meta_bitmap *bitmap = NULL;
  379. enum gs_color_format format;
  380. if (cursor->bitmap_offset)
  381. bitmap = SPA_MEMBER(cursor, cursor->bitmap_offset,
  382. struct spa_meta_bitmap);
  383. if (bitmap && bitmap->size.width > 0 &&
  384. bitmap->size.height > 0 &&
  385. spa_pixel_format_to_obs_format(bitmap->format, &format,
  386. &swap_red_blue)) {
  387. const uint8_t *bitmap_data;
  388. bitmap_data =
  389. SPA_MEMBER(bitmap, bitmap->offset, uint8_t);
  390. obs_pw->cursor.hotspot_x = cursor->hotspot.x;
  391. obs_pw->cursor.hotspot_y = cursor->hotspot.y;
  392. obs_pw->cursor.width = bitmap->size.width;
  393. obs_pw->cursor.height = bitmap->size.height;
  394. g_clear_pointer(&obs_pw->cursor.texture,
  395. gs_texture_destroy);
  396. obs_pw->cursor.texture = gs_texture_create(
  397. obs_pw->cursor.width, obs_pw->cursor.height,
  398. format, 1, &bitmap_data, GS_DYNAMIC);
  399. if (swap_red_blue)
  400. swap_texture_red_blue(obs_pw->cursor.texture);
  401. }
  402. obs_pw->cursor.x = cursor->position.x;
  403. obs_pw->cursor.y = cursor->position.y;
  404. }
  405. pw_stream_queue_buffer(obs_pw->stream, b);
  406. obs_leave_graphics();
  407. }
  408. static void on_param_changed_cb(void *user_data, uint32_t id,
  409. const struct spa_pod *param)
  410. {
  411. obs_pipewire_data *obs_pw = user_data;
  412. struct spa_pod_builder pod_builder;
  413. const struct spa_pod *params[3];
  414. uint8_t params_buffer[1024];
  415. int result;
  416. if (!param || id != SPA_PARAM_Format)
  417. return;
  418. result = spa_format_parse(param, &obs_pw->format.media_type,
  419. &obs_pw->format.media_subtype);
  420. if (result < 0)
  421. return;
  422. if (obs_pw->format.media_type != SPA_MEDIA_TYPE_video ||
  423. obs_pw->format.media_subtype != SPA_MEDIA_SUBTYPE_raw)
  424. return;
  425. spa_format_video_raw_parse(param, &obs_pw->format.info.raw);
  426. blog(LOG_DEBUG, "[pipewire] Negotiated format:");
  427. blog(LOG_DEBUG, "[pipewire] Format: %d (%s)",
  428. obs_pw->format.info.raw.format,
  429. spa_debug_type_find_name(spa_type_video_format,
  430. obs_pw->format.info.raw.format));
  431. blog(LOG_DEBUG, "[pipewire] Size: %dx%d",
  432. obs_pw->format.info.raw.size.width,
  433. obs_pw->format.info.raw.size.height);
  434. blog(LOG_DEBUG, "[pipewire] Framerate: %d/%d",
  435. obs_pw->format.info.raw.framerate.num,
  436. obs_pw->format.info.raw.framerate.denom);
  437. /* Video crop */
  438. pod_builder =
  439. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  440. params[0] = spa_pod_builder_add_object(
  441. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  442. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
  443. SPA_PARAM_META_size,
  444. SPA_POD_Int(sizeof(struct spa_meta_region)));
  445. /* Cursor */
  446. params[1] = spa_pod_builder_add_object(
  447. &pod_builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
  448. SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Cursor),
  449. SPA_PARAM_META_size,
  450. SPA_POD_CHOICE_RANGE_Int(CURSOR_META_SIZE(64, 64),
  451. CURSOR_META_SIZE(1, 1),
  452. CURSOR_META_SIZE(1024, 1024)));
  453. /* Buffer options */
  454. params[2] = spa_pod_builder_add_object(
  455. &pod_builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
  456. SPA_PARAM_BUFFERS_dataType,
  457. SPA_POD_Int((1 << SPA_DATA_MemPtr) | (1 << SPA_DATA_DmaBuf)));
  458. pw_stream_update_params(obs_pw->stream, params, 3);
  459. obs_pw->negotiated = true;
  460. }
  461. static void on_state_changed_cb(void *user_data, enum pw_stream_state old,
  462. enum pw_stream_state state, const char *error)
  463. {
  464. UNUSED_PARAMETER(old);
  465. UNUSED_PARAMETER(error);
  466. obs_pipewire_data *obs_pw = user_data;
  467. blog(LOG_DEBUG, "[pipewire] stream %p state: \"%s\" (error: %s)",
  468. obs_pw->stream, pw_stream_state_as_string(state),
  469. error ? error : "none");
  470. }
  471. static const struct pw_stream_events stream_events = {
  472. PW_VERSION_STREAM_EVENTS,
  473. .state_changed = on_state_changed_cb,
  474. .param_changed = on_param_changed_cb,
  475. .process = on_process_cb,
  476. };
  477. static void on_core_error_cb(void *user_data, uint32_t id, int seq, int res,
  478. const char *message)
  479. {
  480. UNUSED_PARAMETER(seq);
  481. obs_pipewire_data *obs_pw = user_data;
  482. blog(LOG_ERROR, "[pipewire] Error id:%u seq:%d res:%d (%s): %s", id,
  483. seq, res, g_strerror(res), message);
  484. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  485. }
  486. static void on_core_done_cb(void *user_data, uint32_t id, int seq)
  487. {
  488. UNUSED_PARAMETER(seq);
  489. obs_pipewire_data *obs_pw = user_data;
  490. if (id == PW_ID_CORE)
  491. pw_thread_loop_signal(obs_pw->thread_loop, FALSE);
  492. }
  493. static const struct pw_core_events core_events = {
  494. PW_VERSION_CORE_EVENTS,
  495. .done = on_core_done_cb,
  496. .error = on_core_error_cb,
  497. };
  498. static void play_pipewire_stream(obs_pipewire_data *obs_pw)
  499. {
  500. struct spa_pod_builder pod_builder;
  501. const struct spa_pod *params[1];
  502. uint8_t params_buffer[1024];
  503. struct obs_video_info ovi;
  504. obs_pw->thread_loop = pw_thread_loop_new("PipeWire thread loop", NULL);
  505. obs_pw->context = pw_context_new(
  506. pw_thread_loop_get_loop(obs_pw->thread_loop), NULL, 0);
  507. if (pw_thread_loop_start(obs_pw->thread_loop) < 0) {
  508. blog(LOG_WARNING, "Error starting threaded mainloop");
  509. return;
  510. }
  511. pw_thread_loop_lock(obs_pw->thread_loop);
  512. /* Core */
  513. obs_pw->core = pw_context_connect_fd(
  514. obs_pw->context, fcntl(obs_pw->pipewire_fd, F_DUPFD_CLOEXEC, 5),
  515. NULL, 0);
  516. if (!obs_pw->core) {
  517. blog(LOG_WARNING, "Error creating PipeWire core: %m");
  518. pw_thread_loop_unlock(obs_pw->thread_loop);
  519. return;
  520. }
  521. pw_core_add_listener(obs_pw->core, &obs_pw->core_listener, &core_events,
  522. obs_pw);
  523. /* Stream */
  524. obs_pw->stream = pw_stream_new(
  525. obs_pw->core, "OBS Studio",
  526. pw_properties_new(PW_KEY_MEDIA_TYPE, "Video",
  527. PW_KEY_MEDIA_CATEGORY, "Capture",
  528. PW_KEY_MEDIA_ROLE, "Screen", NULL));
  529. pw_stream_add_listener(obs_pw->stream, &obs_pw->stream_listener,
  530. &stream_events, obs_pw);
  531. blog(LOG_INFO, "[pipewire] created stream %p", obs_pw->stream);
  532. /* Stream parameters */
  533. pod_builder =
  534. SPA_POD_BUILDER_INIT(params_buffer, sizeof(params_buffer));
  535. obs_get_video_info(&ovi);
  536. params[0] = spa_pod_builder_add_object(
  537. &pod_builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
  538. SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
  539. SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
  540. SPA_FORMAT_VIDEO_format,
  541. SPA_POD_CHOICE_ENUM_Id(
  542. 4, SPA_VIDEO_FORMAT_BGRA, SPA_VIDEO_FORMAT_RGBA,
  543. SPA_VIDEO_FORMAT_BGRx, SPA_VIDEO_FORMAT_RGBx),
  544. SPA_FORMAT_VIDEO_size,
  545. SPA_POD_CHOICE_RANGE_Rectangle(
  546. &SPA_RECTANGLE(320, 240), // Arbitrary
  547. &SPA_RECTANGLE(1, 1), &SPA_RECTANGLE(8192, 4320)),
  548. SPA_FORMAT_VIDEO_framerate,
  549. SPA_POD_CHOICE_RANGE_Fraction(
  550. &SPA_FRACTION(ovi.fps_num, ovi.fps_den),
  551. &SPA_FRACTION(0, 1), &SPA_FRACTION(360, 1)));
  552. obs_pw->video_info = ovi;
  553. pw_stream_connect(
  554. obs_pw->stream, PW_DIRECTION_INPUT, obs_pw->pipewire_node,
  555. PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params,
  556. 1);
  557. blog(LOG_INFO, "[pipewire] playing stream…");
  558. pw_thread_loop_unlock(obs_pw->thread_loop);
  559. }
  560. /* ------------------------------------------------- */
  561. static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res,
  562. void *user_data)
  563. {
  564. g_autoptr(GUnixFDList) fd_list = NULL;
  565. g_autoptr(GVariant) result = NULL;
  566. g_autoptr(GError) error = NULL;
  567. obs_pipewire_data *obs_pw = user_data;
  568. int fd_index;
  569. result = g_dbus_proxy_call_with_unix_fd_list_finish(
  570. G_DBUS_PROXY(source), &fd_list, res, &error);
  571. if (error) {
  572. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  573. blog(LOG_ERROR,
  574. "[pipewire] Error retrieving pipewire fd: %s",
  575. error->message);
  576. return;
  577. }
  578. g_variant_get(result, "(h)", &fd_index, &error);
  579. obs_pw->pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
  580. if (error) {
  581. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  582. blog(LOG_ERROR,
  583. "[pipewire] Error retrieving pipewire fd: %s",
  584. error->message);
  585. return;
  586. }
  587. play_pipewire_stream(obs_pw);
  588. }
  589. static void open_pipewire_remote(obs_pipewire_data *obs_pw)
  590. {
  591. GVariantBuilder builder;
  592. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  593. g_dbus_proxy_call_with_unix_fd_list(
  594. portal_get_dbus_proxy(), "OpenPipeWireRemote",
  595. g_variant_new("(oa{sv})", obs_pw->session_handle, &builder),
  596. G_DBUS_CALL_FLAGS_NONE, -1, NULL, obs_pw->cancellable,
  597. on_pipewire_remote_opened_cb, obs_pw);
  598. }
  599. /* ------------------------------------------------- */
  600. static void on_start_response_received_cb(GDBusConnection *connection,
  601. const char *sender_name,
  602. const char *object_path,
  603. const char *interface_name,
  604. const char *signal_name,
  605. GVariant *parameters, void *user_data)
  606. {
  607. UNUSED_PARAMETER(connection);
  608. UNUSED_PARAMETER(sender_name);
  609. UNUSED_PARAMETER(object_path);
  610. UNUSED_PARAMETER(interface_name);
  611. UNUSED_PARAMETER(signal_name);
  612. g_autoptr(GVariant) stream_properties = NULL;
  613. g_autoptr(GVariant) streams = NULL;
  614. g_autoptr(GVariant) result = NULL;
  615. struct dbus_call_data *call = user_data;
  616. obs_pipewire_data *obs_pw = call->obs_pw;
  617. GVariantIter iter;
  618. uint32_t response;
  619. size_t n_streams;
  620. g_clear_pointer(&call, dbus_call_data_free);
  621. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  622. if (response != 0) {
  623. blog(LOG_WARNING,
  624. "[pipewire] Failed to start screencast, denied or cancelled by user");
  625. return;
  626. }
  627. streams =
  628. g_variant_lookup_value(result, "streams", G_VARIANT_TYPE_ARRAY);
  629. g_variant_iter_init(&iter, streams);
  630. n_streams = g_variant_iter_n_children(&iter);
  631. if (n_streams != 1) {
  632. blog(LOG_WARNING,
  633. "[pipewire] Received more than one stream when only one was expected. "
  634. "This is probably a bug in the desktop portal implementation you are "
  635. "using.");
  636. // The KDE Desktop portal implementation sometimes sends an invalid
  637. // response where more than one stream is attached, and only the
  638. // last one is the one we're looking for. This is the only known
  639. // buggy implementation, so let's at least try to make it work here.
  640. for (size_t i = 0; i < n_streams - 1; i++) {
  641. g_autoptr(GVariant) throwaway_properties = NULL;
  642. uint32_t throwaway_pipewire_node;
  643. g_variant_iter_loop(&iter, "(u@a{sv})",
  644. &throwaway_pipewire_node,
  645. &throwaway_properties);
  646. }
  647. }
  648. g_variant_iter_loop(&iter, "(u@a{sv})", &obs_pw->pipewire_node,
  649. &stream_properties);
  650. blog(LOG_INFO, "[pipewire] %s selected, setting up screencast",
  651. capture_type_to_string(obs_pw->capture_type));
  652. open_pipewire_remote(obs_pw);
  653. }
  654. static void on_started_cb(GObject *source, GAsyncResult *res, void *user_data)
  655. {
  656. UNUSED_PARAMETER(user_data);
  657. g_autoptr(GVariant) result = NULL;
  658. g_autoptr(GError) error = NULL;
  659. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  660. if (error) {
  661. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  662. blog(LOG_ERROR,
  663. "[pipewire] Error selecting screencast source: %s",
  664. error->message);
  665. return;
  666. }
  667. }
  668. static void start(obs_pipewire_data *obs_pw)
  669. {
  670. GVariantBuilder builder;
  671. struct dbus_call_data *call;
  672. char *request_token;
  673. char *request_path;
  674. new_request_path(obs_pw, &request_path, &request_token);
  675. blog(LOG_INFO, "[pipewire] asking for %s…",
  676. capture_type_to_string(obs_pw->capture_type));
  677. call = subscribe_to_signal(obs_pw, request_path,
  678. on_start_response_received_cb);
  679. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  680. g_variant_builder_add(&builder, "{sv}", "handle_token",
  681. g_variant_new_string(request_token));
  682. g_dbus_proxy_call(portal_get_dbus_proxy(), "Start",
  683. g_variant_new("(osa{sv})", obs_pw->session_handle, "",
  684. &builder),
  685. G_DBUS_CALL_FLAGS_NONE, -1, obs_pw->cancellable,
  686. on_started_cb, call);
  687. bfree(request_token);
  688. bfree(request_path);
  689. }
  690. /* ------------------------------------------------- */
  691. static void on_select_source_response_received_cb(
  692. GDBusConnection *connection, const char *sender_name,
  693. const char *object_path, const char *interface_name,
  694. const char *signal_name, GVariant *parameters, void *user_data)
  695. {
  696. UNUSED_PARAMETER(connection);
  697. UNUSED_PARAMETER(sender_name);
  698. UNUSED_PARAMETER(object_path);
  699. UNUSED_PARAMETER(interface_name);
  700. UNUSED_PARAMETER(signal_name);
  701. g_autoptr(GVariant) ret = NULL;
  702. struct dbus_call_data *call = user_data;
  703. obs_pipewire_data *obs_pw = call->obs_pw;
  704. uint32_t response;
  705. blog(LOG_DEBUG, "[pipewire] Response to select source received");
  706. g_clear_pointer(&call, dbus_call_data_free);
  707. g_variant_get(parameters, "(u@a{sv})", &response, &ret);
  708. if (response != 0) {
  709. blog(LOG_WARNING,
  710. "[pipewire] Failed to select source, denied or cancelled by user");
  711. return;
  712. }
  713. start(obs_pw);
  714. }
  715. static void on_source_selected_cb(GObject *source, GAsyncResult *res,
  716. void *user_data)
  717. {
  718. UNUSED_PARAMETER(user_data);
  719. g_autoptr(GVariant) result = NULL;
  720. g_autoptr(GError) error = NULL;
  721. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  722. if (error) {
  723. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  724. blog(LOG_ERROR,
  725. "[pipewire] Error selecting screencast source: %s",
  726. error->message);
  727. return;
  728. }
  729. }
  730. static void select_source(obs_pipewire_data *obs_pw)
  731. {
  732. struct dbus_call_data *call;
  733. GVariantBuilder builder;
  734. char *request_token;
  735. char *request_path;
  736. new_request_path(obs_pw, &request_path, &request_token);
  737. call = subscribe_to_signal(obs_pw, request_path,
  738. on_select_source_response_received_cb);
  739. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  740. g_variant_builder_add(&builder, "{sv}", "types",
  741. g_variant_new_uint32(obs_pw->capture_type));
  742. g_variant_builder_add(&builder, "{sv}", "multiple",
  743. g_variant_new_boolean(FALSE));
  744. g_variant_builder_add(&builder, "{sv}", "handle_token",
  745. g_variant_new_string(request_token));
  746. if (obs_pw->available_cursor_modes & 4)
  747. g_variant_builder_add(&builder, "{sv}", "cursor_mode",
  748. g_variant_new_uint32(4));
  749. else if ((obs_pw->available_cursor_modes & 2) && obs_pw->cursor.visible)
  750. g_variant_builder_add(&builder, "{sv}", "cursor_mode",
  751. g_variant_new_uint32(2));
  752. else
  753. g_variant_builder_add(&builder, "{sv}", "cursor_mode",
  754. g_variant_new_uint32(1));
  755. g_dbus_proxy_call(portal_get_dbus_proxy(), "SelectSources",
  756. g_variant_new("(oa{sv})", obs_pw->session_handle,
  757. &builder),
  758. G_DBUS_CALL_FLAGS_NONE, -1, obs_pw->cancellable,
  759. on_source_selected_cb, call);
  760. bfree(request_token);
  761. bfree(request_path);
  762. }
  763. /* ------------------------------------------------- */
  764. static void on_create_session_response_received_cb(
  765. GDBusConnection *connection, const char *sender_name,
  766. const char *object_path, const char *interface_name,
  767. const char *signal_name, GVariant *parameters, void *user_data)
  768. {
  769. UNUSED_PARAMETER(connection);
  770. UNUSED_PARAMETER(sender_name);
  771. UNUSED_PARAMETER(object_path);
  772. UNUSED_PARAMETER(interface_name);
  773. UNUSED_PARAMETER(signal_name);
  774. g_autoptr(GVariant) result = NULL;
  775. struct dbus_call_data *call = user_data;
  776. obs_pipewire_data *obs_pw = call->obs_pw;
  777. uint32_t response;
  778. g_clear_pointer(&call, dbus_call_data_free);
  779. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  780. if (response != 0) {
  781. blog(LOG_WARNING,
  782. "[pipewire] Failed to create session, denied or cancelled by user");
  783. return;
  784. }
  785. blog(LOG_INFO, "[pipewire] screencast session created");
  786. g_variant_lookup(result, "session_handle", "s",
  787. &obs_pw->session_handle);
  788. select_source(obs_pw);
  789. }
  790. static void on_session_created_cb(GObject *source, GAsyncResult *res,
  791. void *user_data)
  792. {
  793. UNUSED_PARAMETER(user_data);
  794. g_autoptr(GVariant) result = NULL;
  795. g_autoptr(GError) error = NULL;
  796. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  797. if (error) {
  798. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  799. blog(LOG_ERROR,
  800. "[pipewire] Error creating screencast session: %s",
  801. error->message);
  802. return;
  803. }
  804. }
  805. static void create_session(obs_pipewire_data *obs_pw)
  806. {
  807. struct dbus_call_data *call;
  808. GVariantBuilder builder;
  809. char *session_token;
  810. char *request_token;
  811. char *request_path;
  812. new_request_path(obs_pw, &request_path, &request_token);
  813. new_session_path(obs_pw, NULL, &session_token);
  814. call = subscribe_to_signal(obs_pw, request_path,
  815. on_create_session_response_received_cb);
  816. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  817. g_variant_builder_add(&builder, "{sv}", "handle_token",
  818. g_variant_new_string(request_token));
  819. g_variant_builder_add(&builder, "{sv}", "session_handle_token",
  820. g_variant_new_string(session_token));
  821. g_dbus_proxy_call(portal_get_dbus_proxy(), "CreateSession",
  822. g_variant_new("(a{sv})", &builder),
  823. G_DBUS_CALL_FLAGS_NONE, -1, obs_pw->cancellable,
  824. on_session_created_cb, call);
  825. bfree(session_token);
  826. bfree(request_token);
  827. bfree(request_path);
  828. }
  829. /* ------------------------------------------------- */
  830. static void update_available_cursor_modes(obs_pipewire_data *obs_pw,
  831. GDBusProxy *proxy)
  832. {
  833. g_autoptr(GVariant) cached_cursor_modes = NULL;
  834. uint32_t available_cursor_modes;
  835. cached_cursor_modes =
  836. g_dbus_proxy_get_cached_property(proxy, "AvailableCursorModes");
  837. available_cursor_modes =
  838. cached_cursor_modes ? g_variant_get_uint32(cached_cursor_modes)
  839. : 0;
  840. obs_pw->available_cursor_modes = available_cursor_modes;
  841. blog(LOG_INFO, "[pipewire] available cursor modes:");
  842. if (available_cursor_modes & 4)
  843. blog(LOG_INFO, "[pipewire] - Metadata");
  844. if (available_cursor_modes & 2)
  845. blog(LOG_INFO, "[pipewire] - Always visible");
  846. if (available_cursor_modes & 1)
  847. blog(LOG_INFO, "[pipewire] - Hidden");
  848. }
  849. /* ------------------------------------------------- */
  850. static gboolean init_obs_pipewire(obs_pipewire_data *obs_pw)
  851. {
  852. GDBusConnection *connection;
  853. GDBusProxy *proxy;
  854. char *aux;
  855. obs_pw->cancellable = g_cancellable_new();
  856. connection = portal_get_dbus_connection();
  857. if (!connection)
  858. return FALSE;
  859. proxy = portal_get_dbus_proxy();
  860. if (!proxy)
  861. return FALSE;
  862. update_available_cursor_modes(obs_pw, proxy);
  863. obs_pw->sender_name =
  864. bstrdup(g_dbus_connection_get_unique_name(connection) + 1);
  865. /* Replace dots by underscores */
  866. while ((aux = strstr(obs_pw->sender_name, ".")) != NULL)
  867. *aux = '_';
  868. blog(LOG_INFO, "PipeWire initialized (sender name: %s)",
  869. obs_pw->sender_name);
  870. create_session(obs_pw);
  871. return TRUE;
  872. }
  873. static bool reload_session_cb(obs_properties_t *properties,
  874. obs_property_t *property, void *data)
  875. {
  876. UNUSED_PARAMETER(properties);
  877. UNUSED_PARAMETER(property);
  878. obs_pipewire_data *obs_pw = data;
  879. teardown_pipewire(obs_pw);
  880. destroy_session(obs_pw);
  881. init_obs_pipewire(obs_pw);
  882. return false;
  883. }
  884. /* obs_source_info methods */
  885. void *obs_pipewire_create(enum obs_pw_capture_type capture_type,
  886. obs_data_t *settings, obs_source_t *source)
  887. {
  888. obs_pipewire_data *obs_pw = bzalloc(sizeof(obs_pipewire_data));
  889. obs_pw->source = source;
  890. obs_pw->settings = settings;
  891. obs_pw->capture_type = capture_type;
  892. obs_pw->cursor.visible = obs_data_get_bool(settings, "ShowCursor");
  893. if (!init_obs_pipewire(obs_pw))
  894. g_clear_pointer(&obs_pw, bfree);
  895. return obs_pw;
  896. }
  897. void obs_pipewire_destroy(obs_pipewire_data *obs_pw)
  898. {
  899. if (!obs_pw)
  900. return;
  901. teardown_pipewire(obs_pw);
  902. destroy_session(obs_pw);
  903. bfree(obs_pw);
  904. }
  905. void obs_pipewire_get_defaults(obs_data_t *settings)
  906. {
  907. obs_data_set_default_bool(settings, "ShowCursor", true);
  908. }
  909. obs_properties_t *obs_pipewire_get_properties(obs_pipewire_data *obs_pw,
  910. const char *reload_string_id)
  911. {
  912. obs_properties_t *properties;
  913. properties = obs_properties_create();
  914. obs_properties_add_button2(properties, "Reload",
  915. obs_module_text(reload_string_id),
  916. reload_session_cb, obs_pw);
  917. obs_properties_add_bool(properties, "ShowCursor",
  918. obs_module_text("ShowCursor"));
  919. return properties;
  920. }
  921. void obs_pipewire_update(obs_pipewire_data *obs_pw, obs_data_t *settings)
  922. {
  923. obs_pw->cursor.visible = obs_data_get_bool(settings, "ShowCursor");
  924. }
  925. void obs_pipewire_show(obs_pipewire_data *obs_pw)
  926. {
  927. if (obs_pw->stream)
  928. pw_stream_set_active(obs_pw->stream, true);
  929. }
  930. void obs_pipewire_hide(obs_pipewire_data *obs_pw)
  931. {
  932. if (obs_pw->stream)
  933. pw_stream_set_active(obs_pw->stream, false);
  934. }
  935. uint32_t obs_pipewire_get_width(obs_pipewire_data *obs_pw)
  936. {
  937. if (!obs_pw->negotiated)
  938. return 0;
  939. if (obs_pw->crop.valid)
  940. return obs_pw->crop.width;
  941. else
  942. return obs_pw->format.info.raw.size.width;
  943. }
  944. uint32_t obs_pipewire_get_height(obs_pipewire_data *obs_pw)
  945. {
  946. if (!obs_pw->negotiated)
  947. return 0;
  948. if (obs_pw->crop.valid)
  949. return obs_pw->crop.height;
  950. else
  951. return obs_pw->format.info.raw.size.height;
  952. }
  953. void obs_pipewire_video_render(obs_pipewire_data *obs_pw, gs_effect_t *effect)
  954. {
  955. gs_eparam_t *image;
  956. if (!obs_pw->texture)
  957. return;
  958. image = gs_effect_get_param_by_name(effect, "image");
  959. gs_effect_set_texture(image, obs_pw->texture);
  960. if (has_effective_crop(obs_pw)) {
  961. gs_draw_sprite_subregion(obs_pw->texture, 0, obs_pw->crop.x,
  962. obs_pw->crop.y, obs_pw->crop.width,
  963. obs_pw->crop.height);
  964. } else {
  965. gs_draw_sprite(obs_pw->texture, 0, 0, 0);
  966. }
  967. if (obs_pw->cursor.visible && obs_pw->cursor.valid &&
  968. obs_pw->cursor.texture) {
  969. float cursor_x = obs_pw->cursor.x - obs_pw->cursor.hotspot_x;
  970. float cursor_y = obs_pw->cursor.y - obs_pw->cursor.hotspot_y;
  971. gs_matrix_push();
  972. gs_matrix_translate3f(cursor_x, cursor_y, 0.0f);
  973. gs_effect_set_texture(image, obs_pw->cursor.texture);
  974. gs_draw_sprite(obs_pw->texture, 0, obs_pw->cursor.width,
  975. obs_pw->cursor.height);
  976. gs_matrix_pop();
  977. }
  978. }
  979. enum obs_pw_capture_type
  980. obs_pipewire_get_capture_type(obs_pipewire_data *obs_pw)
  981. {
  982. return obs_pw->capture_type;
  983. }