Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
audio.cpp
Go to the documentation of this file.
1
5// standard includes
6#include <bitset>
7#include <map>
8#include <sstream>
9#include <thread>
10
11// lib includes
12#include <boost/regex.hpp>
13#include <pulse/error.h>
14#include <pulse/pulseaudio.h>
15#include <pulse/simple.h>
16
17// local includes
18#include "src/config.h"
19#include "src/logging.h"
20#include "src/platform/common.h"
22#include "src/thread_safe.h"
23
24namespace platf {
25 using namespace std::literals;
26
27 constexpr pa_channel_position_t position_mapping[] {
28 PA_CHANNEL_POSITION_FRONT_LEFT,
29 PA_CHANNEL_POSITION_FRONT_RIGHT,
30 PA_CHANNEL_POSITION_FRONT_CENTER,
31 PA_CHANNEL_POSITION_LFE,
32 PA_CHANNEL_POSITION_REAR_LEFT,
33 PA_CHANNEL_POSITION_REAR_RIGHT,
34 PA_CHANNEL_POSITION_SIDE_LEFT,
35 PA_CHANNEL_POSITION_SIDE_RIGHT,
36 };
37
38 std::string to_string(const char *name, const std::uint8_t *mapping, int channels) {
39 std::stringstream ss;
40
41 ss << "rate=48000 sink_name="sv << name << " format=float channels="sv << channels << " channel_map="sv;
42 std::for_each_n(mapping, channels - 1, [&ss](std::uint8_t pos) {
43 ss << pa_channel_position_to_string(position_mapping[pos]) << ',';
44 });
45
46 ss << pa_channel_position_to_string(position_mapping[mapping[channels - 1]]);
47
48 ss << " sink_properties=device.description="sv << name;
49 auto result = ss.str();
50
51 BOOST_LOG(debug) << "null-sink args: "sv << result;
52 return result;
53 }
54
55 struct mic_attr_t: public mic_t {
56 util::safe_ptr<pa_simple, pa_simple_free> mic;
57
58 capture_e sample(std::vector<float> &sample_buf) override {
59 auto sample_size = sample_buf.size();
60
61 auto buf = sample_buf.data();
62 int status;
63 if (pa_simple_read(mic.get(), buf, sample_size * sizeof(float), &status)) {
64 BOOST_LOG(error) << "pa_simple_read() failed: "sv << pa_strerror(status);
65
66 return capture_e::error;
67 }
68
69 return capture_e::ok;
70 }
71 };
72
73 std::unique_ptr<mic_t> microphone([[maybe_unused]] const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, std::string source_name, const char *server = nullptr) {
74 // Capture in the canonical layout, never in the order the Opus mapping
75 // describes: the multistream encoder applies that mapping and the client
76 // undoes it, so the two cancel. Handing it to PulseAudio as well made
77 // PulseAudio reorder the samples a second time, which nothing undoes. The
78 // built-in mappings are identities, so only a client's own surround-params
79 // (webOS Moonlight sends 0,1,4,5,2,3) came out permuted.
80 if (channels < 1 || channels > static_cast<int>(std::size(position_mapping))) {
81 BOOST_LOG(error) << "Unsupported audio channel count: "sv << channels;
82 return nullptr;
83 }
84
85 auto mic = std::make_unique<mic_attr_t>();
86
87 pa_sample_spec ss {PA_SAMPLE_FLOAT32, sample_rate, (std::uint8_t) channels};
88 pa_channel_map pa_map;
89 pa_channel_map_init(&pa_map);
90
91 pa_map.channels = channels;
92 std::copy_n(std::begin(position_mapping), channels, pa_map.map);
93
94 pa_buffer_attr pa_attr = {
95 .maxlength = uint32_t(-1),
96 .tlength = uint32_t(-1),
97 .prebuf = uint32_t(-1),
98 .minreq = uint32_t(-1),
99 .fragsize = uint32_t(frame_size * channels * sizeof(float))
100 };
101
102 int status;
103
104 mic->mic.reset(
105 pa_simple_new(server, "sunshine", pa_stream_direction_t::PA_STREAM_RECORD, source_name.c_str(), "sunshine-record", &ss, &pa_map, &pa_attr, &status)
106 );
107
108 if (!mic->mic) {
109 auto err_str = pa_strerror(status);
110 BOOST_LOG(error) << "pa_simple_new() failed: "sv << err_str;
111 return nullptr;
112 }
113
114 return mic;
115 }
116
117 std::unique_ptr<mic_t> session_audio::microphone(const std::string &pulse_server, const std::string &sink, const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size) {
118 return ::platf::microphone(mapping, channels, sample_rate, frame_size, sink + ".monitor", pulse_server.c_str());
119 }
120
121 namespace pa {
122 template<bool B, class T>
124
125 template<class T>
126 struct add_const_helper<true, T> {
127 using type = const std::remove_pointer_t<T> *;
128 };
129
130 template<class T>
131 struct add_const_helper<false, T> {
132 using type = const T *;
133 };
134
135 template<class T>
136 using add_const_t = typename add_const_helper<std::is_pointer_v<T>, T>::type;
137
138 template<class T>
139 void pa_free(T *p) {
140 pa_xfree(p);
141 }
142
143 using ctx_t = util::safe_ptr<pa_context, pa_context_unref>;
144 using loop_t = util::safe_ptr<pa_threaded_mainloop, pa_threaded_mainloop_free>;
145 using string_t = util::safe_ptr<char, pa_free<char>>;
146
147 template<class T>
148 using cb_simple_t = std::function<void(ctx_t::pointer, add_const_t<T> i)>;
149
150 template<class T>
151 void cb(ctx_t::pointer ctx, add_const_t<T> i, void *userdata) {
152 auto &f = *(cb_simple_t<T> *) userdata;
153
154 // Cannot similarly filter on eol here. Unless reported otherwise assume
155 // we have no need for special filtering like cb?
156 f(ctx, i);
157 }
158
159 template<class T>
160 using cb_t = std::function<void(ctx_t::pointer, add_const_t<T> i, int eol)>;
161
162 template<class T>
163 void cb(ctx_t::pointer ctx, add_const_t<T> i, int eol, void *userdata) {
164 auto &f = *(cb_t<T> *) userdata;
165
166 // For some reason, pulseaudio calls this callback after disconnecting
167 if (i && eol) {
168 return;
169 }
170
171 f(ctx, i, eol);
172 }
173
174 void cb_i(ctx_t::pointer ctx, std::uint32_t i, void *userdata) {
175 auto alarm = (safe::alarm_raw_t<int> *) userdata;
176
177 alarm->ring(i);
178 }
179
180 void ctx_state_cb(ctx_t::pointer ctx, void *userdata) {
181 auto &f = *(std::function<void(ctx_t::pointer)> *) userdata;
182
183 f(ctx);
184 }
185
186 void success_cb(ctx_t::pointer ctx, int status, void *userdata) {
187 assert(userdata != nullptr);
188
189 auto alarm = (safe::alarm_raw_t<int> *) userdata;
190 alarm->ring(status ? 0 : 1);
191 }
192
193 class server_t: public audio_control_t {
194 enum ctx_event_e : int {
195 ready,
196 terminated,
197 failed
198 };
199
200 public:
201 loop_t loop;
202 ctx_t ctx;
203 std::string requested_sink;
204
205 struct {
206 std::uint32_t stereo = PA_INVALID_INDEX;
207 std::uint32_t surround51 = PA_INVALID_INDEX;
208 std::uint32_t surround71 = PA_INVALID_INDEX;
209 } index;
210
211 std::unique_ptr<safe::event_t<ctx_event_e>> events;
212 std::unique_ptr<std::function<void(ctx_t::pointer)>> events_cb;
213
214 bool running = false;
215
221 class lock_t {
222 public:
223 explicit lock_t(pa_threaded_mainloop *loop):
224 loop {loop} {
225 pa_threaded_mainloop_lock(loop);
226 }
227
228 ~lock_t() {
229 pa_threaded_mainloop_unlock(loop);
230 }
231
232 lock_t(const lock_t &) = delete;
233 lock_t &operator=(const lock_t &) = delete;
234
235 private:
236 pa_threaded_mainloop *loop;
237 };
238
245 template<class Make>
246 bool issue(Make make) {
247 lock_t lock {loop.get()};
248 pa_operation *op = make();
249 if (!op) {
250 return false;
251 }
252 pa_operation_unref(op);
253 return true;
254 }
255
256 int context_errno() {
257 lock_t lock {loop.get()};
258 return pa_context_errno(ctx.get());
259 }
260
261 int init() {
262 events = std::make_unique<safe::event_t<ctx_event_e>>();
263 loop.reset(pa_threaded_mainloop_new());
264 ctx.reset(pa_context_new(pa_threaded_mainloop_get_api(loop.get()), "sunshine"));
265
266 events_cb = std::make_unique<std::function<void(ctx_t::pointer)>>([this](ctx_t::pointer ctx) {
267 switch (pa_context_get_state(ctx)) {
268 case PA_CONTEXT_READY:
269 events->raise(ready);
270 break;
271 case PA_CONTEXT_TERMINATED:
272 BOOST_LOG(debug) << "Pulseadio context terminated"sv;
273 events->raise(terminated);
274 break;
275 case PA_CONTEXT_FAILED:
276 BOOST_LOG(debug) << "Pulseadio context failed"sv;
277 events->raise(failed);
278 break;
279 case PA_CONTEXT_CONNECTING:
280 BOOST_LOG(debug) << "Connecting to pulseaudio"sv;
281 case PA_CONTEXT_UNCONNECTED:
282 case PA_CONTEXT_AUTHORIZING:
283 case PA_CONTEXT_SETTING_NAME:
284 break;
285 }
286 });
287
288 pa_context_set_state_callback(ctx.get(), ctx_state_cb, events_cb.get());
289
290 auto status = pa_context_connect(ctx.get(), nullptr, PA_CONTEXT_NOFLAGS, nullptr);
291 if (status) {
292 BOOST_LOG(error) << "Couldn't connect to pulseaudio: "sv << pa_strerror(status);
293 return -1;
294 }
295
296 if (pa_threaded_mainloop_start(loop.get()) < 0) {
297 BOOST_LOG(error) << "Couldn't run pulseaudio main loop"sv;
298 return -1;
299 }
300 running = true;
301
302 auto event = events->pop();
303 if (event == failed) {
304 return -1;
305 }
306
307 return 0;
308 }
309
310 int load_null(const char *name, const std::uint8_t *channel_mapping, int channels) {
311 auto alarm = safe::make_alarm<int>();
312
313 const bool op = issue([&] {
314 return pa_context_load_module(ctx.get(), "module-null-sink", to_string(name, channel_mapping, channels).c_str(), cb_i, alarm.get());
315 });
316 if (!op) {
317 BOOST_LOG(error) << "Couldn't create load-module operation: "sv << pa_strerror(context_errno());
318 return static_cast<int>(PA_INVALID_INDEX);
319 }
320
321 alarm->wait();
322 return *alarm->status();
323 }
324
325 int unload_null(std::uint32_t i) {
326 if (i == PA_INVALID_INDEX) {
327 return 0;
328 }
329
330 auto alarm = safe::make_alarm<int>();
331
332 const bool op = issue([&] {
333 return pa_context_unload_module(ctx.get(), i, success_cb, alarm.get());
334 });
335 if (!op) {
336 BOOST_LOG(error) << "Couldn't create unload-module operation: "sv << pa_strerror(context_errno());
337 return -1;
338 }
339
340 alarm->wait();
341
342 if (*alarm->status()) {
343 BOOST_LOG(error) << "Couldn't unload null-sink with index ["sv << i << "]: "sv << pa_strerror(context_errno());
344 return -1;
345 }
346
347 return 0;
348 }
349
358 std::uint32_t find_sink_module(const std::string &name) {
359 auto alarm = safe::make_alarm<int>();
360 std::uint32_t module_index = PA_INVALID_INDEX;
361
362 cb_t<pa_sink_info *> f = [&](ctx_t::pointer ctx, const pa_sink_info *sink_info, int eol) {
363 if (!sink_info) {
364 if (!eol) {
365 BOOST_LOG(error) << "Couldn't get pulseaudio sink info: "sv << pa_strerror(pa_context_errno(ctx));
366 alarm->ring(-1);
367 return;
368 }
369 alarm->ring(0);
370 return;
371 }
372
373 if (sink_info->name && name == sink_info->name) {
374 module_index = sink_info->owner_module;
375 }
376 };
377
378 const bool op = issue([&] {
379 return pa_context_get_sink_info_list(ctx.get(), cb<pa_sink_info *>, &f);
380 });
381 if (!op) {
382 BOOST_LOG(error) << "Couldn't create sink info operation: "sv << pa_strerror(context_errno());
383 return PA_INVALID_INDEX;
384 }
385
386 alarm->wait();
387 return *alarm->status() ? PA_INVALID_INDEX : module_index;
388 }
389
399 bool sink_exists(const std::string &name) {
400 auto alarm = safe::make_alarm<int>();
401 bool found = false;
402
403 cb_t<pa_sink_info *> f = [&](ctx_t::pointer ctx, const pa_sink_info *sink_info, int eol) {
404 if (!sink_info) {
405 if (!eol) {
406 BOOST_LOG(error) << "Couldn't get pulseaudio sink info: "sv << pa_strerror(pa_context_errno(ctx));
407 alarm->ring(-1);
408 return;
409 }
410 alarm->ring(0);
411 return;
412 }
413
414 if (sink_info->name && name == sink_info->name) {
415 found = true;
416 }
417 };
418
419 const bool op = issue([&] {
420 return pa_context_get_sink_info_list(ctx.get(), cb<pa_sink_info *>, &f);
421 });
422 if (!op) {
423 BOOST_LOG(error) << "Couldn't create sink info operation: "sv << pa_strerror(context_errno());
424 return false;
425 }
426
427 alarm->wait();
428 return *alarm->status() ? false : found;
429 }
430
431 bool create_session_sink(const std::string &name, const std::uint8_t *mapping, int channels) override {
432 // One that is already there counts as created. A client reconnecting to
433 // a session whose sink outlived it should be given that sink back
434 // rather than a second one under a name that is already taken.
435 if (find_sink_module(name) != PA_INVALID_INDEX) {
436 BOOST_LOG(debug) << "Reusing existing session sink ["sv << name << ']';
437 return true;
438 }
439
440 const auto module = static_cast<std::uint32_t>(load_null(name.c_str(), mapping, channels));
441 if (module == PA_INVALID_INDEX) {
442 BOOST_LOG(warning) << "Couldn't create session sink ["sv << name << "]: "sv
443 << pa_strerror(context_errno());
444 return false;
445 }
446
447 BOOST_LOG(info) << "Created session sink ["sv << name << "] with "sv << channels << " channel(s)"sv;
448 return true;
449 }
450
451 void remove_session_sink(const std::string &name) override {
452 const auto module = find_sink_module(name);
453 if (module == PA_INVALID_INDEX) {
454 return;
455 }
456 if (unload_null(module) == 0) {
457 BOOST_LOG(debug) << "Removed session sink ["sv << name << ']';
458 }
459 }
460
461 std::optional<sink_t> sink_info() override {
462 constexpr auto stereo = "sink-sunshine-stereo";
463 constexpr auto surround51 = "sink-sunshine-surround51";
464 constexpr auto surround71 = "sink-sunshine-surround71";
465
466 auto alarm = safe::make_alarm<int>();
467
468 sink_t sink;
469
470 // Count of all virtual sinks that are created by us
471 int nullcount = 0;
472
473 cb_t<pa_sink_info *> f = [&](ctx_t::pointer ctx, const pa_sink_info *sink_info, int eol) {
474 if (!sink_info) {
475 if (!eol) {
476 BOOST_LOG(error) << "Couldn't get pulseaudio sink info: "sv << pa_strerror(pa_context_errno(ctx));
477
478 alarm->ring(-1);
479 }
480
481 alarm->ring(0);
482 return;
483 }
484
485 // Ensure Sunshine won't create a sink that already exists.
486 if (!std::strcmp(sink_info->name, stereo)) {
487 index.stereo = sink_info->owner_module;
488
489 ++nullcount;
490 } else if (!std::strcmp(sink_info->name, surround51)) {
491 index.surround51 = sink_info->owner_module;
492
493 ++nullcount;
494 } else if (!std::strcmp(sink_info->name, surround71)) {
495 index.surround71 = sink_info->owner_module;
496
497 ++nullcount;
498 }
499 };
500
501 const bool op = issue([&] {
502 return pa_context_get_sink_info_list(ctx.get(), cb<pa_sink_info *>, &f);
503 });
504
505 if (!op) {
506 BOOST_LOG(error) << "Couldn't create card info operation: "sv << pa_strerror(context_errno());
507
508 return std::nullopt;
509 }
510
511 alarm->wait();
512
513 if (*alarm->status()) {
514 return std::nullopt;
515 }
516
517 auto sink_name = get_default_sink_name();
518 sink.host = sink_name;
519
520 if (index.stereo == PA_INVALID_INDEX) {
521 index.stereo = load_null(stereo, speaker::map_stereo, sizeof(speaker::map_stereo));
522 if (index.stereo == PA_INVALID_INDEX) {
523 BOOST_LOG(warning) << "Couldn't create virtual sink for stereo: "sv << pa_strerror(context_errno());
524 } else {
525 ++nullcount;
526 }
527 }
528
529 if (index.surround51 == PA_INVALID_INDEX) {
530 index.surround51 = load_null(surround51, speaker::map_surround51, sizeof(speaker::map_surround51));
531 if (index.surround51 == PA_INVALID_INDEX) {
532 BOOST_LOG(warning) << "Couldn't create virtual sink for surround-51: "sv << pa_strerror(context_errno());
533 } else {
534 ++nullcount;
535 }
536 }
537
538 if (index.surround71 == PA_INVALID_INDEX) {
539 index.surround71 = load_null(surround71, speaker::map_surround71, sizeof(speaker::map_surround71));
540 if (index.surround71 == PA_INVALID_INDEX) {
541 BOOST_LOG(warning) << "Couldn't create virtual sink for surround-71: "sv << pa_strerror(context_errno());
542 } else {
543 ++nullcount;
544 }
545 }
546
547 if (sink_name.empty()) {
548 BOOST_LOG(warning) << "Couldn't find an active default sink. Continuing with virtual audio only."sv;
549 }
550
551 if (nullcount == 3) {
552 sink.null = std::make_optional(sink_t::null_t {stereo, surround51, surround71});
553 }
554
555 return std::make_optional(std::move(sink));
556 }
557
558 std::string get_default_sink_name() {
559 std::string sink_name;
560 auto alarm = safe::make_alarm<int>();
561
562 cb_simple_t<pa_server_info *> server_f = [&](ctx_t::pointer ctx, const pa_server_info *server_info) {
563 if (!server_info) {
564 BOOST_LOG(error) << "Couldn't get pulseaudio server info: "sv << pa_strerror(pa_context_errno(ctx));
565 alarm->ring(-1);
566 }
567
568 if (server_info->default_sink_name) {
569 sink_name = server_info->default_sink_name;
570 }
571 alarm->ring(0);
572 };
573
574 const bool server_op = issue([&] {
575 return pa_context_get_server_info(ctx.get(), cb<pa_server_info *>, &server_f);
576 });
577 if (!server_op) {
578 return sink_name;
579 }
580 alarm->wait();
581 // No need to check status. If it failed just return default name.
582 return sink_name;
583 }
584
585 std::string get_monitor_name(const std::string &sink_name) {
586 std::string monitor_name;
587 auto alarm = safe::make_alarm<int>();
588
589 if (sink_name.empty()) {
590 return monitor_name;
591 }
592
593 cb_t<pa_sink_info *> sink_f = [&](ctx_t::pointer ctx, const pa_sink_info *sink_info, int eol) {
594 if (!sink_info) {
595 if (!eol) {
596 BOOST_LOG(error) << "Couldn't get pulseaudio sink info for ["sv << sink_name
597 << "]: "sv << pa_strerror(pa_context_errno(ctx));
598 alarm->ring(-1);
599 }
600
601 alarm->ring(0);
602 return;
603 }
604
605 monitor_name = sink_info->monitor_source_name;
606 };
607
608 const bool sink_op = issue([&] {
609 return pa_context_get_sink_info_by_name(ctx.get(), sink_name.c_str(), cb<pa_sink_info *>, &sink_f);
610 });
611 if (!sink_op) {
612 return monitor_name;
613 }
614
615 alarm->wait();
616 // No need to check status. If it failed just return default name.
617 BOOST_LOG(info) << "Found default monitor by name: "sv << monitor_name;
618 return monitor_name;
619 }
620
621 std::unique_ptr<mic_t> microphone(const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size) override {
622 // Sink choice priority:
623 // 1. Config sink
624 // 2. Last sink swapped to (Usually virtual in this case)
625 // 3. Default Sink
626 // An attempt was made to always use default to match the switching mechanic,
627 // but this happens right after the swap so the default returned by PA was not
628 // the new one just set!
629 auto sink_name = config::audio.sink;
630 if (sink_name.empty()) {
631 sink_name = requested_sink;
632 }
633 if (sink_name.empty()) {
634 sink_name = get_default_sink_name();
635 }
636
637 return ::platf::microphone(mapping, channels, sample_rate, frame_size, get_monitor_name(sink_name));
638 }
639
640 std::unique_ptr<mic_t> microphone_from(const std::string &sink, const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size) override {
641 // None of the priority above applies: the caller named the sink, and
642 // the configured sink, the last swap and the default are all the wrong
643 // answer for a session that owns one of its own.
644 return ::platf::microphone(mapping, channels, sample_rate, frame_size, get_monitor_name(sink));
645 }
646
647 bool is_sink_available(const std::string &sink) override {
648 // A monitor's audio device leaves with the monitor: disable the output
649 // and the sound server drops the sink, then builds a new one under the
650 // same name once the output is back. Callers use this to wait out that
651 // gap, so answering "yes" unconditionally is the one answer that makes
652 // the wait pointless.
653 return !sink.empty() && sink_exists(sink);
654 }
655
656 std::vector<std::string> streams_playing_elsewhere(const std::string &sink) override {
657 std::vector<std::string> elsewhere;
658 if (sink.empty()) {
659 return elsewhere;
660 }
661
662 std::map<std::uint32_t, std::string> sink_names;
663 {
664 auto alarm = safe::make_alarm<int>();
665 cb_t<pa_sink_info *> f = [&](ctx_t::pointer, const pa_sink_info *info, int) {
666 if (!info) {
667 alarm->ring(0);
668 return;
669 }
670 sink_names.emplace(info->index, info->name ? info->name : "");
671 };
672 const bool op = issue([&] {
673 return pa_context_get_sink_info_list(ctx.get(), cb<pa_sink_info *>, &f);
674 });
675 if (!op) {
676 return elsewhere;
677 }
678 alarm->wait();
679 }
680
681 auto alarm = safe::make_alarm<int>();
682 cb_t<pa_sink_input_info *> f = [&](ctx_t::pointer, const pa_sink_input_info *info, int) {
683 if (!info) {
684 alarm->ring(0);
685 return;
686 }
687 const auto target = sink_names.find(info->sink);
688 if (info->corked || target == sink_names.end() || target->second == sink) {
689 return;
690 }
691 const char *application = pa_proplist_gets(info->proplist, PA_PROP_APPLICATION_NAME);
692 elsewhere.push_back(std::string {application ? application : (info->name ? info->name : "?")} + " -> " + target->second);
693 };
694 const bool op = issue([&] {
695 return pa_context_get_sink_input_info_list(ctx.get(), cb<pa_sink_input_info *>, &f);
696 });
697 if (!op) {
698 return elsewhere;
699 }
700 alarm->wait();
701 return elsewhere;
702 }
703
704 int set_sink(const std::string &sink) override {
705 auto alarm = safe::make_alarm<int>();
706
707 BOOST_LOG(info) << "Setting default sink to: ["sv << sink << "]"sv;
708 const bool op = issue([&] {
709 return pa_context_set_default_sink(ctx.get(), sink.c_str(), success_cb, alarm.get());
710 });
711
712 if (!op) {
713 BOOST_LOG(error) << "Couldn't create set default-sink operation: "sv << pa_strerror(context_errno());
714 return -1;
715 }
716
717 alarm->wait();
718 if (*alarm->status()) {
719 BOOST_LOG(error) << "Couldn't set default-sink ["sv << sink << "]: "sv << pa_strerror(context_errno());
720
721 return -1;
722 }
723
724 requested_sink = sink;
725
726 return 0;
727 }
728
729 ~server_t() override {
730 unload_null(index.stereo);
731 unload_null(index.surround51);
732 unload_null(index.surround71);
733
734 if (running) {
735 {
736 lock_t lock {loop.get()};
737 pa_context_disconnect(ctx.get());
738 }
739
740 KITTY_WHILE_LOOP(auto event = events->pop(), event != terminated && event != failed, {
741 event = events->pop();
742 })
743
744 // Stopped without the lock: it joins the loop's thread. The context
745 // goes before the loop, as members are destroyed in reverse order.
746 pa_threaded_mainloop_stop(loop.get());
747 }
748 }
749 };
750 } // namespace pa
751
752 std::unique_ptr<audio_control_t> audio_control() {
753 auto audio = std::make_unique<pa::server_t>();
754
755 if (audio->init()) {
756 return nullptr;
757 }
758
759 return audio;
760 }
761} // namespace platf
std::vector< std::string > streams_playing_elsewhere(const std::string &sink) override
Applications playing to a sink other than sink.
Definition audio.cpp:656
bool create_session_sink(const std::string &name, const std::uint8_t *mapping, int channels) override
Create a sink that belongs to one isolated session alone.
Definition audio.cpp:431
bool is_sink_available(const std::string &sink) override
Check if the audio sink is available in the system.
Definition audio.cpp:647
void remove_session_sink(const std::string &name) override
Remove a sink created by create_session_sink(). Removing one that is already gone is not an error.
Definition audio.cpp:451
bool issue(Make make)
Definition audio.cpp:246
bool sink_exists(const std::string &name)
Whether a sink of this name is present right now.
Definition audio.cpp:399
std::unique_ptr< mic_t > microphone_from(const std::string &sink, const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size) override
Record a named sink's monitor, whatever the current sink is.
Definition audio.cpp:640
std::uint32_t find_sink_module(const std::string &name)
The module index behind a sink, or PA_INVALID_INDEX for none.
Definition audio.cpp:358
Declarations for common platform specific utilities.
capture_e
Definition common.h:435
@ ok
Success.
Definition common.h:436
@ error
Error.
Definition common.h:440
Declarations for the configuration of Sunshine.
Declarations for logging related functions.
An isolated session's own PipeWire server.
std::unique_ptr< mic_t > microphone(const std::string &pulse_server, const std::string &sink, const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size)
Record sink's monitor on the server at pulse_server.
Definition audio.cpp:117
Declarations for thread-safe data structures.