Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2019 Pengutronix, Michael Olbrich <m.olbrich@pengutronix.de> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
Marius Vlad | bb7ed37 | 2020-11-05 19:30:51 +0200 | [diff] [blame^] | 26 | #include "config.h" |
| 27 | |
Marius Vlad | c4076ef | 2020-08-28 16:59:36 +0300 | [diff] [blame] | 28 | #include <libweston/pipewire-plugin.h> |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 29 | #include "backend.h" |
| 30 | #include "libweston-internal.h" |
| 31 | #include "shared/timespec-util.h" |
| 32 | #include <libweston/backend-drm.h> |
| 33 | #include <libweston/weston-log.h> |
| 34 | |
| 35 | #include <sys/mman.h> |
| 36 | #include <errno.h> |
| 37 | #include <unistd.h> |
| 38 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 39 | #include <pipewire/pipewire.h> |
| 40 | |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 41 | #include <spa/param/format-utils.h> |
| 42 | #include <spa/param/video/format-utils.h> |
| 43 | #include <spa/utils/defs.h> |
| 44 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 45 | #if PW_CHECK_VERSION(0, 2, 90) |
| 46 | #include <spa/buffer/meta.h> |
| 47 | #include <spa/utils/result.h> |
| 48 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 49 | |
| 50 | #define PROP_RANGE(min, max) 2, (min), (max) |
| 51 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 52 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 53 | struct type { |
| 54 | struct spa_type_media_type media_type; |
| 55 | struct spa_type_media_subtype media_subtype; |
| 56 | struct spa_type_format_video format_video; |
| 57 | struct spa_type_video_format video_format; |
| 58 | }; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 59 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 60 | |
| 61 | struct weston_pipewire { |
| 62 | struct weston_compositor *compositor; |
| 63 | struct wl_list output_list; |
| 64 | struct wl_listener destroy_listener; |
| 65 | const struct weston_drm_virtual_output_api *virtual_output_api; |
| 66 | |
| 67 | struct weston_log_scope *debug; |
| 68 | |
| 69 | struct pw_loop *loop; |
| 70 | struct wl_event_source *loop_source; |
| 71 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 72 | #if PW_CHECK_VERSION(0, 2, 90) |
| 73 | struct pw_context *context; |
| 74 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 75 | struct pw_core *core; |
| 76 | struct pw_type *t; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 77 | #if PW_CHECK_VERSION(0, 2, 90) |
| 78 | struct spa_hook core_listener; |
| 79 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 80 | struct type type; |
| 81 | |
| 82 | struct pw_remote *remote; |
| 83 | struct spa_hook remote_listener; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 84 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct pipewire_output { |
| 88 | struct weston_output *output; |
| 89 | void (*saved_destroy)(struct weston_output *output); |
| 90 | int (*saved_enable)(struct weston_output *output); |
| 91 | int (*saved_disable)(struct weston_output *output); |
| 92 | int (*saved_start_repaint_loop)(struct weston_output *output); |
| 93 | |
| 94 | struct weston_head *head; |
| 95 | |
| 96 | struct weston_pipewire *pipewire; |
| 97 | |
| 98 | uint32_t seq; |
| 99 | struct pw_stream *stream; |
| 100 | struct spa_hook stream_listener; |
| 101 | |
| 102 | struct spa_video_info_raw video_format; |
| 103 | |
| 104 | struct wl_event_source *finish_frame_timer; |
| 105 | struct wl_list link; |
| 106 | bool submitted_frame; |
Michael Olbrich | a24a326 | 2020-08-19 08:43:15 +0200 | [diff] [blame] | 107 | enum dpms_enum dpms; |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct pipewire_frame_data { |
| 111 | struct pipewire_output *output; |
| 112 | int fd; |
| 113 | int stride; |
| 114 | struct drm_fb *drm_buffer; |
| 115 | int fence_sync_fd; |
| 116 | struct wl_event_source *fence_sync_event_source; |
| 117 | }; |
| 118 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 119 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 120 | static inline void init_type(struct type *type, struct spa_type_map *map) |
| 121 | { |
| 122 | spa_type_media_type_map(map, &type->media_type); |
| 123 | spa_type_media_subtype_map(map, &type->media_subtype); |
| 124 | spa_type_format_video_map(map, &type->format_video); |
| 125 | spa_type_video_format_map(map, &type->video_format); |
| 126 | } |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 127 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 128 | |
| 129 | static void |
| 130 | pipewire_debug_impl(struct weston_pipewire *pipewire, |
| 131 | struct pipewire_output *output, |
| 132 | const char *fmt, va_list ap) |
| 133 | { |
| 134 | FILE *fp; |
| 135 | char *logstr; |
| 136 | size_t logsize; |
| 137 | char timestr[128]; |
| 138 | |
| 139 | if (!weston_log_scope_is_enabled(pipewire->debug)) |
| 140 | return; |
| 141 | |
| 142 | fp = open_memstream(&logstr, &logsize); |
| 143 | if (!fp) |
| 144 | return; |
| 145 | |
| 146 | weston_log_scope_timestamp(pipewire->debug, timestr, sizeof timestr); |
| 147 | fprintf(fp, "%s", timestr); |
| 148 | |
| 149 | if (output) |
| 150 | fprintf(fp, "[%s]", output->output->name); |
| 151 | |
| 152 | fprintf(fp, " "); |
| 153 | vfprintf(fp, fmt, ap); |
| 154 | fprintf(fp, "\n"); |
| 155 | |
| 156 | if (fclose(fp) == 0) |
| 157 | weston_log_scope_write(pipewire->debug, logstr, logsize); |
| 158 | |
| 159 | free(logstr); |
| 160 | } |
| 161 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 162 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 163 | static void |
| 164 | pipewire_debug(struct weston_pipewire *pipewire, const char *fmt, ...) |
| 165 | { |
| 166 | va_list ap; |
| 167 | |
| 168 | va_start(ap, fmt); |
| 169 | pipewire_debug_impl(pipewire, NULL, fmt, ap); |
| 170 | va_end(ap); |
| 171 | } |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 172 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 173 | |
| 174 | static void |
| 175 | pipewire_output_debug(struct pipewire_output *output, const char *fmt, ...) |
| 176 | { |
| 177 | va_list ap; |
| 178 | |
| 179 | va_start(ap, fmt); |
| 180 | pipewire_debug_impl(output->pipewire, output, fmt, ap); |
| 181 | va_end(ap); |
| 182 | } |
| 183 | |
| 184 | static struct weston_pipewire * |
| 185 | weston_pipewire_get(struct weston_compositor *compositor); |
| 186 | |
| 187 | static struct pipewire_output * |
| 188 | lookup_pipewire_output(struct weston_output *base_output) |
| 189 | { |
| 190 | struct weston_compositor *c = base_output->compositor; |
| 191 | struct weston_pipewire *pipewire = weston_pipewire_get(c); |
| 192 | struct pipewire_output *output; |
| 193 | |
| 194 | wl_list_for_each(output, &pipewire->output_list, link) { |
| 195 | if (output->output == base_output) |
| 196 | return output; |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | pipewire_output_handle_frame(struct pipewire_output *output, int fd, |
| 203 | int stride, struct drm_fb *drm_buffer) |
| 204 | { |
| 205 | const struct weston_drm_virtual_output_api *api = |
| 206 | output->pipewire->virtual_output_api; |
| 207 | size_t size = output->output->height * stride; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 208 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 209 | struct pw_type *t = output->pipewire->t; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 210 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 211 | struct pw_buffer *buffer; |
| 212 | struct spa_buffer *spa_buffer; |
| 213 | struct spa_meta_header *h; |
| 214 | void *ptr; |
| 215 | |
| 216 | if (pw_stream_get_state(output->stream, NULL) != |
| 217 | PW_STREAM_STATE_STREAMING) |
| 218 | goto out; |
| 219 | |
| 220 | buffer = pw_stream_dequeue_buffer(output->stream); |
| 221 | if (!buffer) { |
| 222 | weston_log("Failed to dequeue a pipewire buffer\n"); |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | spa_buffer = buffer->buffer; |
| 227 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 228 | #if PW_CHECK_VERSION(0, 2, 90) |
| 229 | if ((h = spa_buffer_find_meta_data(spa_buffer, SPA_META_Header, |
| 230 | sizeof(struct spa_meta_header)))) { |
| 231 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 232 | if ((h = spa_buffer_find_meta(spa_buffer, t->meta.Header))) { |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 233 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 234 | h->pts = -1; |
| 235 | h->flags = 0; |
| 236 | h->seq = output->seq++; |
| 237 | h->dts_offset = 0; |
| 238 | } |
| 239 | |
| 240 | ptr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 241 | memcpy(spa_buffer->datas[0].data, ptr, size); |
| 242 | munmap(ptr, size); |
| 243 | |
| 244 | spa_buffer->datas[0].chunk->offset = 0; |
| 245 | spa_buffer->datas[0].chunk->stride = stride; |
| 246 | spa_buffer->datas[0].chunk->size = spa_buffer->datas[0].maxsize; |
| 247 | |
| 248 | pipewire_output_debug(output, "push frame"); |
| 249 | pw_stream_queue_buffer(output->stream, buffer); |
| 250 | |
| 251 | out: |
| 252 | close(fd); |
| 253 | output->submitted_frame = true; |
| 254 | api->buffer_released(drm_buffer); |
| 255 | } |
| 256 | |
| 257 | static int |
| 258 | pipewire_output_fence_sync_handler(int fd, uint32_t mask, void *data) |
| 259 | { |
| 260 | struct pipewire_frame_data *frame_data = data; |
| 261 | struct pipewire_output *output = frame_data->output; |
| 262 | |
| 263 | pipewire_output_handle_frame(output, frame_data->fd, frame_data->stride, |
| 264 | frame_data->drm_buffer); |
| 265 | |
| 266 | wl_event_source_remove(frame_data->fence_sync_event_source); |
| 267 | close(frame_data->fence_sync_fd); |
| 268 | free(frame_data); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int |
| 274 | pipewire_output_submit_frame(struct weston_output *base_output, int fd, |
| 275 | int stride, struct drm_fb *drm_buffer) |
| 276 | { |
| 277 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 278 | struct weston_pipewire *pipewire = output->pipewire; |
| 279 | const struct weston_drm_virtual_output_api *api = |
| 280 | pipewire->virtual_output_api; |
| 281 | struct wl_event_loop *loop; |
| 282 | struct pipewire_frame_data *frame_data; |
| 283 | int fence_sync_fd; |
| 284 | |
| 285 | pipewire_output_debug(output, "submit frame: fd = %d drm_fb = %p", |
| 286 | fd, drm_buffer); |
| 287 | |
| 288 | fence_sync_fd = api->get_fence_sync_fd(output->output); |
| 289 | if (fence_sync_fd == -1) { |
| 290 | pipewire_output_handle_frame(output, fd, stride, drm_buffer); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | frame_data = zalloc(sizeof *frame_data); |
| 295 | if (!frame_data) { |
| 296 | close(fence_sync_fd); |
| 297 | pipewire_output_handle_frame(output, fd, stride, drm_buffer); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | loop = wl_display_get_event_loop(pipewire->compositor->wl_display); |
| 302 | |
| 303 | frame_data->output = output; |
| 304 | frame_data->fd = fd; |
| 305 | frame_data->stride = stride; |
| 306 | frame_data->drm_buffer = drm_buffer; |
| 307 | frame_data->fence_sync_fd = fence_sync_fd; |
| 308 | frame_data->fence_sync_event_source = |
| 309 | wl_event_loop_add_fd(loop, frame_data->fence_sync_fd, |
| 310 | WL_EVENT_READABLE, |
| 311 | pipewire_output_fence_sync_handler, |
| 312 | frame_data); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | pipewire_output_timer_update(struct pipewire_output *output) |
| 319 | { |
| 320 | int64_t msec; |
| 321 | int32_t refresh; |
| 322 | |
| 323 | if (pw_stream_get_state(output->stream, NULL) == |
| 324 | PW_STREAM_STATE_STREAMING) |
| 325 | refresh = output->output->current_mode->refresh; |
| 326 | else |
| 327 | refresh = 1000; |
| 328 | |
| 329 | msec = millihz_to_nsec(refresh) / 1000000; |
| 330 | wl_event_source_timer_update(output->finish_frame_timer, msec); |
| 331 | } |
| 332 | |
| 333 | static int |
| 334 | pipewire_output_finish_frame_handler(void *data) |
| 335 | { |
| 336 | struct pipewire_output *output = data; |
| 337 | const struct weston_drm_virtual_output_api *api |
| 338 | = output->pipewire->virtual_output_api; |
| 339 | struct timespec now; |
| 340 | |
| 341 | if (output->submitted_frame) { |
| 342 | struct weston_compositor *c = output->pipewire->compositor; |
| 343 | output->submitted_frame = false; |
| 344 | weston_compositor_read_presentation_clock(c, &now); |
| 345 | api->finish_frame(output->output, &now, 0); |
| 346 | } |
| 347 | |
Michael Olbrich | a24a326 | 2020-08-19 08:43:15 +0200 | [diff] [blame] | 348 | if (output->dpms == WESTON_DPMS_ON) |
| 349 | pipewire_output_timer_update(output); |
| 350 | else |
| 351 | wl_event_source_timer_update(output->finish_frame_timer, 0); |
| 352 | |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void |
| 357 | pipewire_output_destroy(struct weston_output *base_output) |
| 358 | { |
| 359 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 360 | struct weston_mode *mode, *next; |
| 361 | |
| 362 | wl_list_for_each_safe(mode, next, &base_output->mode_list, link) { |
| 363 | wl_list_remove(&mode->link); |
| 364 | free(mode); |
| 365 | } |
| 366 | |
| 367 | output->saved_destroy(base_output); |
| 368 | |
| 369 | pw_stream_destroy(output->stream); |
| 370 | |
| 371 | wl_list_remove(&output->link); |
| 372 | weston_head_release(output->head); |
| 373 | free(output->head); |
| 374 | free(output); |
| 375 | } |
| 376 | |
| 377 | static int |
| 378 | pipewire_output_start_repaint_loop(struct weston_output *base_output) |
| 379 | { |
| 380 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 381 | |
| 382 | pipewire_output_debug(output, "start repaint loop"); |
| 383 | output->saved_start_repaint_loop(base_output); |
| 384 | |
| 385 | pipewire_output_timer_update(output); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Michael Olbrich | a24a326 | 2020-08-19 08:43:15 +0200 | [diff] [blame] | 390 | static void |
| 391 | pipewire_set_dpms(struct weston_output *base_output, enum dpms_enum level) |
| 392 | { |
| 393 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 394 | |
| 395 | if (output->dpms == level) |
| 396 | return; |
| 397 | |
| 398 | output->dpms = level; |
| 399 | pipewire_output_finish_frame_handler(output); |
| 400 | } |
| 401 | |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 402 | static int |
| 403 | pipewire_output_connect(struct pipewire_output *output) |
| 404 | { |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 405 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 406 | struct weston_pipewire *pipewire = output->pipewire; |
| 407 | struct type *type = &pipewire->type; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 408 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 409 | uint8_t buffer[1024]; |
| 410 | struct spa_pod_builder builder = |
| 411 | SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); |
| 412 | const struct spa_pod *params[1]; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 413 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 414 | struct pw_type *t = pipewire->t; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 415 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 416 | int frame_rate = output->output->current_mode->refresh / 1000; |
| 417 | int width = output->output->width; |
| 418 | int height = output->output->height; |
| 419 | int ret; |
| 420 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 421 | #if PW_CHECK_VERSION(0, 2, 90) |
| 422 | params[0] = spa_pod_builder_add_object(&builder, |
| 423 | SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat, |
| 424 | SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), |
| 425 | SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), |
| 426 | SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_BGRx), |
| 427 | SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&SPA_RECTANGLE(width, height)), |
| 428 | SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&SPA_FRACTION (0, 1)), |
| 429 | SPA_FORMAT_VIDEO_maxFramerate, |
| 430 | SPA_POD_CHOICE_RANGE_Fraction(&SPA_FRACTION(frame_rate, 1), |
| 431 | &SPA_FRACTION(1, 1), |
| 432 | &SPA_FRACTION(frame_rate, 1))); |
| 433 | |
| 434 | ret = pw_stream_connect(output->stream, PW_DIRECTION_OUTPUT, SPA_ID_INVALID, |
| 435 | (PW_STREAM_FLAG_DRIVER | |
| 436 | PW_STREAM_FLAG_MAP_BUFFERS), |
| 437 | params, 1); |
| 438 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 439 | params[0] = spa_pod_builder_object(&builder, |
| 440 | t->param.idEnumFormat, t->spa_format, |
| 441 | "I", type->media_type.video, |
| 442 | "I", type->media_subtype.raw, |
| 443 | ":", type->format_video.format, |
| 444 | "I", type->video_format.BGRx, |
| 445 | ":", type->format_video.size, |
| 446 | "R", &SPA_RECTANGLE(width, height), |
| 447 | ":", type->format_video.framerate, |
| 448 | "F", &SPA_FRACTION(0, 1), |
| 449 | ":", type->format_video.max_framerate, |
| 450 | "Fru", &SPA_FRACTION(frame_rate, 1), |
| 451 | PROP_RANGE(&SPA_FRACTION(1, 1), |
| 452 | &SPA_FRACTION(frame_rate, 1))); |
| 453 | |
| 454 | ret = pw_stream_connect(output->stream, PW_DIRECTION_OUTPUT, NULL, |
| 455 | (PW_STREAM_FLAG_DRIVER | |
| 456 | PW_STREAM_FLAG_MAP_BUFFERS), |
| 457 | params, 1); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 458 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 459 | if (ret != 0) { |
| 460 | weston_log("Failed to connect pipewire stream: %s", |
| 461 | spa_strerror(ret)); |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static int |
| 469 | pipewire_output_enable(struct weston_output *base_output) |
| 470 | { |
| 471 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 472 | struct weston_compositor *c = base_output->compositor; |
| 473 | const struct weston_drm_virtual_output_api *api |
| 474 | = output->pipewire->virtual_output_api; |
| 475 | struct wl_event_loop *loop; |
| 476 | int ret; |
| 477 | |
| 478 | api->set_submit_frame_cb(base_output, pipewire_output_submit_frame); |
| 479 | |
| 480 | ret = pipewire_output_connect(output); |
| 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | |
| 484 | ret = output->saved_enable(base_output); |
| 485 | if (ret < 0) |
| 486 | return ret; |
| 487 | |
| 488 | output->saved_start_repaint_loop = base_output->start_repaint_loop; |
| 489 | base_output->start_repaint_loop = pipewire_output_start_repaint_loop; |
Michael Olbrich | a24a326 | 2020-08-19 08:43:15 +0200 | [diff] [blame] | 490 | base_output->set_dpms = pipewire_set_dpms; |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 491 | |
| 492 | loop = wl_display_get_event_loop(c->wl_display); |
| 493 | output->finish_frame_timer = |
| 494 | wl_event_loop_add_timer(loop, |
| 495 | pipewire_output_finish_frame_handler, |
| 496 | output); |
Michael Olbrich | a24a326 | 2020-08-19 08:43:15 +0200 | [diff] [blame] | 497 | output->dpms = WESTON_DPMS_ON; |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int |
| 503 | pipewire_output_disable(struct weston_output *base_output) |
| 504 | { |
| 505 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 506 | |
| 507 | wl_event_source_remove(output->finish_frame_timer); |
| 508 | |
| 509 | pw_stream_disconnect(output->stream); |
| 510 | |
| 511 | return output->saved_disable(base_output); |
| 512 | } |
| 513 | |
| 514 | static void |
| 515 | pipewire_output_stream_state_changed(void *data, enum pw_stream_state old, |
| 516 | enum pw_stream_state state, |
| 517 | const char *error_message) |
| 518 | { |
| 519 | struct pipewire_output *output = data; |
| 520 | |
| 521 | pipewire_output_debug(output, "state changed %s -> %s", |
| 522 | pw_stream_state_as_string(old), |
| 523 | pw_stream_state_as_string(state)); |
| 524 | |
| 525 | switch (state) { |
| 526 | case PW_STREAM_STATE_STREAMING: |
| 527 | weston_output_schedule_repaint(output->output); |
| 528 | break; |
| 529 | default: |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | static void |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 535 | #if PW_CHECK_VERSION(0, 2, 90) |
| 536 | pipewire_output_stream_param_changed(void *data, uint32_t id, const struct spa_pod *format) |
| 537 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 538 | pipewire_output_stream_format_changed(void *data, const struct spa_pod *format) |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 539 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 540 | { |
| 541 | struct pipewire_output *output = data; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 542 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 543 | struct weston_pipewire *pipewire = output->pipewire; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 544 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 545 | uint8_t buffer[1024]; |
| 546 | struct spa_pod_builder builder = |
| 547 | SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); |
| 548 | const struct spa_pod *params[2]; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 549 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 550 | struct pw_type *t = pipewire->t; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 551 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 552 | int32_t width, height, stride, size; |
| 553 | const int bpp = 4; |
| 554 | |
| 555 | if (!format) { |
| 556 | pipewire_output_debug(output, "format = None"); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 557 | #if PW_CHECK_VERSION(0, 2, 90) |
| 558 | pw_stream_update_params(output->stream, NULL, 0); |
| 559 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 560 | pw_stream_finish_format(output->stream, 0, NULL, 0); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 561 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 565 | #if PW_CHECK_VERSION(0, 2, 90) |
| 566 | spa_format_video_raw_parse(format, &output->video_format); |
| 567 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 568 | spa_format_video_raw_parse(format, &output->video_format, |
| 569 | &pipewire->type.format_video); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 570 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 571 | |
| 572 | width = output->video_format.size.width; |
| 573 | height = output->video_format.size.height; |
| 574 | stride = SPA_ROUND_UP_N(width * bpp, 4); |
| 575 | size = height * stride; |
| 576 | |
| 577 | pipewire_output_debug(output, "format = %dx%d", width, height); |
| 578 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 579 | #if PW_CHECK_VERSION(0, 2, 90) |
| 580 | params[0] = spa_pod_builder_add_object(&builder, |
| 581 | SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers, |
| 582 | SPA_PARAM_BUFFERS_size, SPA_POD_Int(size), |
| 583 | SPA_PARAM_BUFFERS_stride, SPA_POD_Int(stride), |
| 584 | SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(4, 2, 8), |
| 585 | SPA_PARAM_BUFFERS_align, SPA_POD_Int(16)); |
| 586 | |
| 587 | params[1] = spa_pod_builder_add_object(&builder, |
| 588 | SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta, |
| 589 | SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header), |
| 590 | SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_header))); |
| 591 | |
| 592 | pw_stream_update_params(output->stream, params, 2); |
| 593 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 594 | params[0] = spa_pod_builder_object(&builder, |
| 595 | t->param.idBuffers, t->param_buffers.Buffers, |
| 596 | ":", t->param_buffers.size, |
| 597 | "i", size, |
| 598 | ":", t->param_buffers.stride, |
| 599 | "i", stride, |
| 600 | ":", t->param_buffers.buffers, |
| 601 | "iru", 4, PROP_RANGE(2, 8), |
| 602 | ":", t->param_buffers.align, |
| 603 | "i", 16); |
| 604 | |
| 605 | params[1] = spa_pod_builder_object(&builder, |
| 606 | t->param.idMeta, t->param_meta.Meta, |
| 607 | ":", t->param_meta.type, "I", t->meta.Header, |
| 608 | ":", t->param_meta.size, "i", sizeof(struct spa_meta_header)); |
| 609 | |
| 610 | pw_stream_finish_format(output->stream, 0, params, 2); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 611 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static const struct pw_stream_events stream_events = { |
| 615 | PW_VERSION_STREAM_EVENTS, |
| 616 | .state_changed = pipewire_output_stream_state_changed, |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 617 | #if PW_CHECK_VERSION(0, 2, 90) |
| 618 | .param_changed = pipewire_output_stream_param_changed, |
| 619 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 620 | .format_changed = pipewire_output_stream_format_changed, |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 621 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | static struct weston_output * |
| 625 | pipewire_output_create(struct weston_compositor *c, char *name) |
| 626 | { |
| 627 | struct weston_pipewire *pipewire = weston_pipewire_get(c); |
| 628 | struct pipewire_output *output; |
| 629 | struct weston_head *head; |
| 630 | const struct weston_drm_virtual_output_api *api; |
| 631 | const char *make = "Weston"; |
| 632 | const char *model = "Virtual Display"; |
| 633 | const char *serial_number = "unknown"; |
| 634 | const char *connector_name = "pipewire"; |
Marius Vlad | bb7ed37 | 2020-11-05 19:30:51 +0200 | [diff] [blame^] | 635 | char *remoting_name; |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 636 | |
| 637 | if (!name || !strlen(name)) |
| 638 | return NULL; |
| 639 | |
| 640 | api = pipewire->virtual_output_api; |
| 641 | |
| 642 | output = zalloc(sizeof *output); |
| 643 | if (!output) |
| 644 | return NULL; |
| 645 | |
| 646 | head = zalloc(sizeof *head); |
| 647 | if (!head) |
| 648 | goto err; |
| 649 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 650 | #if PW_CHECK_VERSION(0, 2, 90) |
| 651 | output->stream = pw_stream_new(pipewire->core, name, NULL); |
| 652 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 653 | output->stream = pw_stream_new(pipewire->remote, name, NULL); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 654 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 655 | if (!output->stream) { |
| 656 | weston_log("Cannot initialize pipewire stream\n"); |
| 657 | goto err; |
| 658 | } |
| 659 | |
| 660 | pw_stream_add_listener(output->stream, &output->stream_listener, |
| 661 | &stream_events, output); |
| 662 | |
| 663 | output->output = api->create_output(c, name); |
| 664 | if (!output->output) { |
| 665 | weston_log("Cannot create virtual output\n"); |
| 666 | goto err; |
| 667 | } |
| 668 | |
| 669 | output->saved_destroy = output->output->destroy; |
| 670 | output->output->destroy = pipewire_output_destroy; |
| 671 | output->saved_enable = output->output->enable; |
| 672 | output->output->enable = pipewire_output_enable; |
| 673 | output->saved_disable = output->output->disable; |
| 674 | output->output->disable = pipewire_output_disable; |
| 675 | output->pipewire = pipewire; |
| 676 | wl_list_insert(pipewire->output_list.prev, &output->link); |
| 677 | |
Marius Vlad | bb7ed37 | 2020-11-05 19:30:51 +0200 | [diff] [blame^] | 678 | asprintf(&remoting_name, "%s-%s", connector_name, name); |
| 679 | weston_head_init(head, remoting_name); |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 680 | weston_head_set_subpixel(head, WL_OUTPUT_SUBPIXEL_NONE); |
| 681 | weston_head_set_monitor_strings(head, make, model, serial_number); |
| 682 | head->compositor = c; |
| 683 | output->head = head; |
| 684 | |
| 685 | weston_output_attach_head(output->output, head); |
| 686 | |
Marius Vlad | bb7ed37 | 2020-11-05 19:30:51 +0200 | [diff] [blame^] | 687 | free(remoting_name); |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 688 | pipewire_output_debug(output, "created"); |
| 689 | |
| 690 | return output->output; |
| 691 | err: |
| 692 | if (output->stream) |
| 693 | pw_stream_destroy(output->stream); |
| 694 | if (head) |
| 695 | free(head); |
| 696 | free(output); |
| 697 | return NULL; |
| 698 | } |
| 699 | |
| 700 | static bool |
| 701 | pipewire_output_is_pipewire(struct weston_output *output) |
| 702 | { |
| 703 | return lookup_pipewire_output(output) != NULL; |
| 704 | } |
| 705 | |
| 706 | static int |
| 707 | pipewire_output_set_mode(struct weston_output *base_output, const char *modeline) |
| 708 | { |
| 709 | struct pipewire_output *output = lookup_pipewire_output(base_output); |
| 710 | const struct weston_drm_virtual_output_api *api = |
| 711 | output->pipewire->virtual_output_api; |
| 712 | struct weston_mode *mode; |
| 713 | int n, width, height, refresh = 0; |
| 714 | |
| 715 | if (output == NULL) { |
| 716 | weston_log("Output is not pipewire.\n"); |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | if (!modeline) |
| 721 | return -1; |
| 722 | |
| 723 | n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh); |
| 724 | if (n != 2 && n != 3) |
| 725 | return -1; |
| 726 | |
| 727 | if (pw_stream_get_state(output->stream, NULL) != |
| 728 | PW_STREAM_STATE_UNCONNECTED) { |
| 729 | return -1; |
| 730 | } |
| 731 | |
| 732 | mode = zalloc(sizeof *mode); |
| 733 | if (!mode) |
| 734 | return -1; |
| 735 | |
| 736 | pipewire_output_debug(output, "mode = %dx%d@%d", width, height, refresh); |
| 737 | |
| 738 | mode->flags = WL_OUTPUT_MODE_CURRENT; |
| 739 | mode->width = width; |
| 740 | mode->height = height; |
| 741 | mode->refresh = (refresh ? refresh : 60) * 1000LL; |
| 742 | |
| 743 | wl_list_insert(base_output->mode_list.prev, &mode->link); |
| 744 | |
| 745 | base_output->current_mode = mode; |
| 746 | |
| 747 | api->set_gbm_format(base_output, "XRGB8888"); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static void |
| 753 | pipewire_output_set_seat(struct weston_output *output, const char *seat) |
| 754 | { |
| 755 | } |
| 756 | |
| 757 | static void |
| 758 | weston_pipewire_destroy(struct wl_listener *l, void *data) |
| 759 | { |
| 760 | struct weston_pipewire *pipewire = |
| 761 | wl_container_of(l, pipewire, destroy_listener); |
| 762 | |
Leandro Ribeiro | f014964 | 2019-12-18 15:52:18 -0300 | [diff] [blame] | 763 | weston_log_scope_destroy(pipewire->debug); |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 764 | pipewire->debug = NULL; |
| 765 | |
| 766 | wl_event_source_remove(pipewire->loop_source); |
| 767 | pw_loop_leave(pipewire->loop); |
| 768 | pw_loop_destroy(pipewire->loop); |
| 769 | } |
| 770 | |
| 771 | static struct weston_pipewire * |
| 772 | weston_pipewire_get(struct weston_compositor *compositor) |
| 773 | { |
| 774 | struct wl_listener *listener; |
| 775 | struct weston_pipewire *pipewire; |
| 776 | |
| 777 | listener = wl_signal_get(&compositor->destroy_signal, |
| 778 | weston_pipewire_destroy); |
| 779 | if (!listener) |
| 780 | return NULL; |
| 781 | |
| 782 | pipewire = wl_container_of(listener, pipewire, destroy_listener); |
| 783 | return pipewire; |
| 784 | } |
| 785 | |
| 786 | static int |
| 787 | weston_pipewire_loop_handler(int fd, uint32_t mask, void *data) |
| 788 | { |
| 789 | struct weston_pipewire *pipewire = data; |
| 790 | int ret; |
| 791 | |
| 792 | ret = pw_loop_iterate(pipewire->loop, 0); |
| 793 | if (ret < 0) |
| 794 | weston_log("pipewire_loop_iterate failed: %s", |
| 795 | spa_strerror(ret)); |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 800 | #if PW_CHECK_VERSION(0, 2, 90) |
| 801 | static void |
| 802 | weston_pipewire_error(void *data, uint32_t id, int seq, int res, |
| 803 | const char *error) |
| 804 | { |
| 805 | weston_log("pipewire remote error: %s\n", error); |
| 806 | } |
| 807 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 808 | static void |
| 809 | weston_pipewire_state_changed(void *data, enum pw_remote_state old, |
| 810 | enum pw_remote_state state, const char *error) |
| 811 | { |
| 812 | struct weston_pipewire *pipewire = data; |
| 813 | |
| 814 | pipewire_debug(pipewire, "[remote] state changed %s -> %s", |
| 815 | pw_remote_state_as_string(old), |
| 816 | pw_remote_state_as_string(state)); |
| 817 | |
| 818 | switch (state) { |
| 819 | case PW_REMOTE_STATE_ERROR: |
| 820 | weston_log("pipewire remote error: %s\n", error); |
| 821 | break; |
| 822 | case PW_REMOTE_STATE_CONNECTED: |
| 823 | weston_log("connected to pipewire daemon\n"); |
| 824 | break; |
| 825 | default: |
| 826 | break; |
| 827 | } |
| 828 | } |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 829 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 830 | |
| 831 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 832 | #if PW_CHECK_VERSION(0, 2, 90) |
| 833 | static const struct pw_core_events core_events = { |
| 834 | PW_VERSION_CORE_EVENTS, |
| 835 | .error = weston_pipewire_error, |
| 836 | }; |
| 837 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 838 | static const struct pw_remote_events remote_events = { |
| 839 | PW_VERSION_REMOTE_EVENTS, |
| 840 | .state_changed = weston_pipewire_state_changed, |
| 841 | }; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 842 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 843 | |
| 844 | static int |
| 845 | weston_pipewire_init(struct weston_pipewire *pipewire) |
| 846 | { |
| 847 | struct wl_event_loop *loop; |
| 848 | |
| 849 | pw_init(NULL, NULL); |
| 850 | |
| 851 | pipewire->loop = pw_loop_new(NULL); |
| 852 | if (!pipewire->loop) |
| 853 | return -1; |
| 854 | |
| 855 | pw_loop_enter(pipewire->loop); |
| 856 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 857 | #if PW_CHECK_VERSION(0, 2, 90) |
| 858 | pipewire->context = pw_context_new(pipewire->loop, NULL, 0); |
| 859 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 860 | pipewire->core = pw_core_new(pipewire->loop, NULL); |
| 861 | pipewire->t = pw_core_get_type(pipewire->core); |
| 862 | init_type(&pipewire->type, pipewire->t->map); |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 863 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 864 | |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 865 | #if PW_CHECK_VERSION(0, 2, 90) |
| 866 | pw_core_add_listener(pipewire->core, |
| 867 | &pipewire->core_listener, |
| 868 | &core_events, pipewire); |
| 869 | #else |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 870 | pipewire->remote = pw_remote_new(pipewire->core, NULL, 0); |
| 871 | pw_remote_add_listener(pipewire->remote, |
| 872 | &pipewire->remote_listener, |
| 873 | &remote_events, pipewire); |
| 874 | |
| 875 | pw_remote_connect(pipewire->remote); |
| 876 | |
| 877 | while (true) { |
| 878 | enum pw_remote_state state; |
| 879 | const char *error = NULL; |
| 880 | int ret; |
| 881 | |
| 882 | state = pw_remote_get_state(pipewire->remote, &error); |
| 883 | if (state == PW_REMOTE_STATE_CONNECTED) |
| 884 | break; |
| 885 | |
| 886 | if (state == PW_REMOTE_STATE_ERROR) { |
| 887 | weston_log("pipewire error: %s\n", error); |
| 888 | goto err; |
| 889 | } |
| 890 | |
| 891 | ret = pw_loop_iterate(pipewire->loop, -1); |
| 892 | if (ret < 0) { |
| 893 | weston_log("pipewire_loop_iterate failed: %s", |
| 894 | spa_strerror(ret)); |
| 895 | goto err; |
| 896 | } |
| 897 | } |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 898 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 899 | |
| 900 | loop = wl_display_get_event_loop(pipewire->compositor->wl_display); |
| 901 | pipewire->loop_source = |
| 902 | wl_event_loop_add_fd(loop, pw_loop_get_fd(pipewire->loop), |
| 903 | WL_EVENT_READABLE, |
| 904 | weston_pipewire_loop_handler, |
| 905 | pipewire); |
| 906 | |
| 907 | return 0; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 908 | #if !PW_CHECK_VERSION(0, 2, 90) |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 909 | err: |
| 910 | if (pipewire->remote) |
| 911 | pw_remote_destroy(pipewire->remote); |
| 912 | pw_loop_leave(pipewire->loop); |
| 913 | pw_loop_destroy(pipewire->loop); |
| 914 | return -1; |
James Hilliard | 80b585f | 2020-07-06 00:58:02 -0600 | [diff] [blame] | 915 | #endif |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static const struct weston_pipewire_api pipewire_api = { |
| 919 | pipewire_output_create, |
| 920 | pipewire_output_is_pipewire, |
| 921 | pipewire_output_set_mode, |
| 922 | pipewire_output_set_seat, |
| 923 | }; |
| 924 | |
| 925 | WL_EXPORT int |
| 926 | weston_module_init(struct weston_compositor *compositor) |
| 927 | { |
| 928 | int ret; |
| 929 | struct weston_pipewire *pipewire; |
| 930 | const struct weston_drm_virtual_output_api *api = |
| 931 | weston_drm_virtual_output_get_api(compositor); |
| 932 | |
| 933 | if (!api) |
| 934 | return -1; |
| 935 | |
| 936 | pipewire = zalloc(sizeof *pipewire); |
| 937 | if (!pipewire) |
| 938 | return -1; |
| 939 | |
Pekka Paalanen | 6ffbba3 | 2019-11-06 12:59:32 +0200 | [diff] [blame] | 940 | if (!weston_compositor_add_destroy_listener_once(compositor, |
| 941 | &pipewire->destroy_listener, |
| 942 | weston_pipewire_destroy)) { |
| 943 | free(pipewire); |
| 944 | return 0; |
| 945 | } |
| 946 | |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 947 | pipewire->virtual_output_api = api; |
| 948 | pipewire->compositor = compositor; |
| 949 | wl_list_init(&pipewire->output_list); |
| 950 | |
| 951 | ret = weston_plugin_api_register(compositor, WESTON_PIPEWIRE_API_NAME, |
| 952 | &pipewire_api, sizeof(pipewire_api)); |
| 953 | |
| 954 | if (ret < 0) { |
| 955 | weston_log("Failed to register pipewire API.\n"); |
| 956 | goto failed; |
| 957 | } |
| 958 | |
| 959 | ret = weston_pipewire_init(pipewire); |
| 960 | if (ret < 0) { |
| 961 | weston_log("Failed to initialize pipewire.\n"); |
| 962 | goto failed; |
| 963 | } |
| 964 | |
Leandro Ribeiro | 5976dbb | 2019-12-18 15:21:03 -0300 | [diff] [blame] | 965 | pipewire->debug = |
Leandro Ribeiro | 2328935 | 2019-12-26 16:44:56 -0300 | [diff] [blame] | 966 | weston_compositor_add_log_scope(compositor, "pipewire", |
| 967 | "Debug messages from pipewire plugin\n", |
| 968 | NULL, NULL, NULL); |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 969 | |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 970 | return 0; |
| 971 | |
| 972 | failed: |
Pekka Paalanen | 6ffbba3 | 2019-11-06 12:59:32 +0200 | [diff] [blame] | 973 | wl_list_remove(&pipewire->destroy_listener.link); |
Michael Olbrich | d5d5aa9 | 2019-04-23 12:34:05 +0200 | [diff] [blame] | 974 | free(pipewire); |
| 975 | return -1; |
| 976 | } |