edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits
| 1 | //! PipeWire playback, shipped as a `dlopen`-able plugin so the main binary |
| 2 | //! never gets a DT_NEEDED on libpipewire. Every entry point is `extern "C"`, |
| 3 | //! catches unwinds and reports failure as a null pointer or a negative code: |
| 4 | //! the caller's contract is "any failure means the app runs muted". |
| 5 | //! |
| 6 | //! corner-cut: that guarantee rests on `panic = "unwind"`. Building this crate with |
| 7 | //! `panic = "abort"` would turn a PipeWire bug into a killed app; the upgrade |
| 8 | //! path is running the output in a child process instead of in-process. |
| 9 | |
| 10 | use c_void; |
| 11 | use ; |
| 12 | use ; |
| 13 | use ; |
| 14 | use JoinHandle; |
| 15 | use ; |
| 16 | |
| 17 | use pipewire as pw; |
| 18 | use ; |
| 19 | use Pod; |
| 20 | |
| 21 | /// How long the loop thread waits for the daemon to accept the stream before |
| 22 | /// the open counts as failed. Negotiation is a couple of graph cycles when it |
| 23 | /// works at all, so this only ever fires on a broken setup. |
| 24 | const READY_TIMEOUT: Duration = from_secs; |
| 25 | /// Belt and braces around the whole open: no PipeWire call on this path is |
| 26 | /// supposed to block, but `ao_open` must never hang the caller. |
| 27 | const OPEN_TIMEOUT: Duration = from_secs; |
| 28 | /// Loop iteration timeout on the audio thread; also the worst-case latency of |
| 29 | /// an `ao_set_active` taking effect. |
| 30 | const POLL: Duration = from_millis; |
| 31 | /// Frames we hand over per callback, and the latency we ask the graph for. |
| 32 | /// Left to itself PipeWire gives us a buffer the size of the sink's (measured: |
| 33 | /// 12288 frames, 256 ms) and calls us once per buffer, so the position clock -- |
| 34 | /// which only moves in the callback -- would step 256 ms at a time and land |
| 35 | /// that coarseness straight in the A/V sync. 1024 frames is ~23 ms, comfortably |
| 36 | /// inside a video frame; measured granularity is then one graph quantum. |
| 37 | /// |
| 38 | /// corner-cut: a graph running a quantum longer than this would consume more per |
| 39 | /// cycle than we deliver and starve. The fix is the crate's `v0_3_49` feature, |
| 40 | /// whose `pw_buffer.requested` says exactly how many frames the graph wants -- |
| 41 | /// a Cargo.toml change, so not this slice. |
| 42 | const QUANTUM: u32 = 1024; |
| 43 | |
| 44 | /// Lock-free SPSC ring of f32 samples (interleaved, one producer: `ao_write`, |
| 45 | /// one consumer: the RT process callback). Samples live in atomics rather than |
| 46 | /// behind a lock so the RT thread never waits on the decoder thread. |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /// Multiplies the little-endian f32 samples in `dst` by `gain`, in place. |
| 95 | /// |
| 96 | /// This is how the editor's volume and mute are applied, rather than through |
| 97 | /// the stream's PipeWire volume control: the session manager persists every |
| 98 | /// change to a node's Props into its restore state (measured: WirePlumber |
| 99 | /// writes the bucket ~2 s after the change), so a mute keypress here would |
| 100 | /// become "edith is muted" in the system mixer tomorrow, and a quit while |
| 101 | /// muted would come back muted with nothing in the app to explain it. Scaling |
| 102 | /// the samples is invisible to the daemon, costs one multiply each, and -- |
| 103 | /// because the frames are still consumed and still handed over -- leaves the |
| 104 | /// device clock, and so the whole A/V sync, exactly where it was. |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | /// The consumer half of a flush: drops everything queued, then pops as usual. |
| 116 | /// Doing it here rather than in `ao_flush` keeps the ring single-consumer -- |
| 117 | /// only the RT thread ever moves `read` -- and means a flush asked for while |
| 118 | /// paused (no callbacks running) still lands before the next pop, so the stale |
| 119 | /// samples can never be heard. |
| 120 | /// The producer half of a flush: while the flag is pending, the ring still |
| 121 | /// holds pre-flush audio waiting to be discarded, and anything accepted now |
| 122 | /// would queue BEHIND that discard -- with no RT callbacks running (paused |
| 123 | /// seek) the next callback would then drain the new stream's head along with |
| 124 | /// the stale tail. Refuse instead; the writer's retry loop parks until the |
| 125 | /// RT thread consumes the flag. |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | /// Everything the RT thread and the caller's thread both touch. |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// Builds the stream, reports readiness through `ready`, then owns the PipeWire |
| 198 | /// loop until told to quit. Everything PipeWire lives in this one frame: the |
| 199 | /// smart pointers are `Rc`-flavoured and the borrow chain (listener -> stream -> |
| 200 | /// core -> context -> loop) is what enforces the destruction order. |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | /// Opens a playback stream for `sample_rate` Hz, `channels` channels of |
| 424 | /// interleaved f32. Returns null on any failure at all: no libpipewire, no |
| 425 | /// daemon, an unusable format. |
| 426 | |
| 427 | pub extern "C" *mut c_void |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | /// Queues up to `n` interleaved f32 samples, returning how many were accepted: |
| 483 | /// short counts mean the ring is full. Never blocks. -1 once the stream is dead. |
| 484 | /// |
| 485 | /// # Safety |
| 486 | /// `session` must come from [`ao_open`] and `samples` must point at `n` f32s. |
| 487 | |
| 488 | pub unsafe extern "C" |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | /// Samples per channel actually played at the device, or -1 while unknown. |
| 510 | /// |
| 511 | /// # Safety |
| 512 | /// `session` must come from [`ao_open`]. |
| 513 | |
| 514 | pub unsafe extern "C" |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | /// Says the caller has queued the last sample this stream will ever have. |
| 527 | /// What is still in the ring plays out untouched -- this is not a flush and not |
| 528 | /// a pause -- but the silence *after* it stops being counted as a late decoder: |
| 529 | /// there is no decoder left to be late ([`Shared::primed`]). The next accepted |
| 530 | /// write is a stream again and counts again. 0 on success. |
| 531 | /// |
| 532 | /// # Safety |
| 533 | /// `session` must come from [`ao_open`]. |
| 534 | |
| 535 | pub unsafe extern "C" |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | /// Starved callbacks since the stream opened -- one per quantum the decoder was |
| 549 | /// late for -- and, through `last_at`, the device position the newest of them |
| 550 | /// was seen at (`-1` if there has been none). The same number the drop print |
| 551 | /// reports, asked for while the session is still running: a caller measuring a |
| 552 | /// seek reads it before and after and the difference is that seek's cost. |
| 553 | /// |
| 554 | /// Two relaxed atomic loads, nothing else: the RT thread writes both and must |
| 555 | /// never wait on a reader. |
| 556 | /// |
| 557 | /// # Safety |
| 558 | /// `session` must come from [`ao_open`]; `last_at` is either null or writable. |
| 559 | |
| 560 | pub unsafe extern "C" |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | /// Pauses (0) or resumes (non-zero) playback. Paused freezes the position. |
| 577 | /// 0 on success, negative on failure. |
| 578 | /// |
| 579 | /// # Safety |
| 580 | /// `session` must come from [`ao_open`]. |
| 581 | |
| 582 | pub unsafe extern "C" |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | /// Sets the output gain: 0.0 is silence, 1.0 is the samples as written. One |
| 601 | /// knob for both the editor's volume and its mute -- mute is a gain of 0.0 |
| 602 | /// pushed down, and which of the two the user is looking at is the caller's |
| 603 | /// business. Takes effect on the next callback (~23 ms), and the position keeps |
| 604 | /// advancing throughout: this silences the output, it does not pause it. |
| 605 | /// 0 on success, negative on failure or a gain that is not in `0.0..=1.0`. |
| 606 | /// |
| 607 | /// # Safety |
| 608 | /// `session` must come from [`ao_open`]. |
| 609 | |
| 610 | pub unsafe extern "C" |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | /// Drops every queued sample, so playback resumes from whatever is written |
| 627 | /// next: what a seek needs. Takes effect on the next process callback (or the |
| 628 | /// first one after a resume), and leaves the played position alone -- the |
| 629 | /// device keeps counting, the caller re-bases against it. |
| 630 | /// 0 on success, negative on failure. |
| 631 | /// |
| 632 | /// # Safety |
| 633 | /// `session` must come from [`ao_open`]. |
| 634 | |
| 635 | pub unsafe extern "C" |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | /// Stops playback and releases the session. Safe to call with null. |
| 653 | /// |
| 654 | /// # Safety |
| 655 | /// `session` must come from [`ao_open`] and must not be used afterwards. |
| 656 | |
| 657 | pub unsafe extern "C" |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 |