2#include <ossia/detail/config.hpp>
4#if defined(OSSIA_ENABLE_PIPEWIRE)
5#if __has_include(<pipewire/pipewire.h>) && __has_include(<spa/param/latency-utils.h>)
6#define OSSIA_AUDIO_PIPEWIRE 1
7#include <ossia/audio/audio_engine.hpp>
8#include <ossia/detail/dylib_loader.hpp>
9#include <ossia/detail/hash_map.hpp>
11#include <ossia/detail/thread.hpp>
13#include <pipewire/core.h>
14#include <pipewire/filter.h>
15#include <pipewire/pipewire.h>
16#include <spa/pod/builder.h>
17#include <spa/utils/result.h>
26#include <spa/param/latency-utils.h>
33 decltype(&::pw_init) init{};
34 decltype(&::pw_deinit) deinit{};
36 decltype(&::pw_context_new) context_new{};
37 decltype(&::pw_context_connect) context_connect{};
38 decltype(&::pw_context_destroy) context_destroy{};
40 decltype(&::pw_core_disconnect) core_disconnect{};
42 decltype(&::pw_proxy_add_listener) proxy_add_listener{};
43 decltype(&::pw_proxy_destroy) proxy_destroy{};
45 decltype(&::pw_main_loop_new) main_loop_new{};
46 decltype(&::pw_main_loop_destroy) main_loop_destroy{};
47 decltype(&::pw_main_loop_quit) main_loop_quit{};
48 decltype(&::pw_main_loop_run) main_loop_run{};
49 decltype(&::pw_main_loop_get_loop) main_loop_get_loop{};
51 decltype(&::pw_properties_new) properties_new{};
52 decltype(&::pw_properties_free) properties_free{};
53 decltype(&::pw_properties_get) properties_get{};
55 decltype(&::pw_filter_new_simple) filter_new_simple{};
56 decltype(&::pw_filter_get_node_id) filter_get_node_id{};
57 decltype(&::pw_filter_get_properties) filter_get_properties{};
58 decltype(&::pw_filter_add_port) filter_add_port{};
59 decltype(&::pw_filter_destroy) filter_destroy{};
60 decltype(&::pw_filter_connect) filter_connect{};
61 decltype(&::pw_filter_get_dsp_buffer) filter_get_dsp_buffer{};
63 static const libpipewire& instance()
65 static const libpipewire self;
73 : library(
"libpipewire-0.3.so.0")
78 init = library.symbol<
decltype(&::pw_init)>(
"pw_init");
79 deinit = library.symbol<
decltype(&::pw_deinit)>(
"pw_deinit");
81 context_new = library.symbol<
decltype(&::pw_context_new)>(
"pw_context_new");
83 = library.symbol<
decltype(&::pw_context_connect)>(
"pw_context_connect");
85 = library.symbol<
decltype(&::pw_context_destroy)>(
"pw_context_destroy");
88 = library.symbol<
decltype(&::pw_core_disconnect)>(
"pw_core_disconnect");
91 = library.symbol<
decltype(&::pw_proxy_add_listener)>(
"pw_proxy_add_listener");
92 proxy_destroy = library.symbol<
decltype(&::pw_proxy_destroy)>(
"pw_proxy_destroy");
94 main_loop_new = library.symbol<
decltype(&::pw_main_loop_new)>(
"pw_main_loop_new");
96 = library.symbol<
decltype(&::pw_main_loop_destroy)>(
"pw_main_loop_destroy");
97 main_loop_quit = library.symbol<
decltype(&::pw_main_loop_quit)>(
"pw_main_loop_quit");
98 main_loop_run = library.symbol<
decltype(&::pw_main_loop_run)>(
"pw_main_loop_run");
100 = library.symbol<
decltype(&::pw_main_loop_get_loop)>(
"pw_main_loop_get_loop");
102 properties_new = library.symbol<
decltype(&::pw_properties_new)>(
"pw_properties_new");
104 = library.symbol<
decltype(&::pw_properties_free)>(
"pw_properties_free");
105 properties_get = library.symbol<
decltype(&::pw_properties_get)>(
"pw_properties_get");
108 = library.symbol<
decltype(&::pw_filter_new_simple)>(
"pw_filter_new_simple");
110 = library.symbol<
decltype(&::pw_filter_get_node_id)>(
"pw_filter_get_node_id");
111 filter_get_properties = library.symbol<
decltype(&::pw_filter_get_properties)>(
112 "pw_filter_get_properties");
114 = library.symbol<
decltype(&::pw_filter_add_port)>(
"pw_filter_add_port");
115 filter_destroy = library.symbol<
decltype(&::pw_filter_destroy)>(
"pw_filter_destroy");
116 filter_connect = library.symbol<
decltype(&::pw_filter_connect)>(
"pw_filter_connect");
117 filter_get_dsp_buffer = library.symbol<
decltype(&::pw_filter_get_dsp_buffer)>(
118 "pw_filter_get_dsp_buffer");
124 assert(context_connect);
125 assert(context_destroy);
127 assert(core_disconnect);
129 assert(proxy_destroy);
131 assert(main_loop_new);
132 assert(main_loop_destroy);
133 assert(main_loop_quit);
134 assert(main_loop_run);
135 assert(main_loop_get_loop);
137 assert(properties_new);
138 assert(properties_free);
139 assert(properties_get);
141 assert(filter_new_simple);
142 assert(filter_get_node_id);
143 assert(filter_get_properties);
144 assert(filter_add_port);
145 assert(filter_destroy);
146 assert(filter_connect);
147 assert(filter_get_dsp_buffer);
151struct pipewire_context
153 pw_main_loop* main_loop{};
156 pw_context* context{};
159 pw_registry* registry{};
160 spa_hook registry_listener{};
166 std::unique_ptr<spa_hook> listener;
168 std::vector<listened_port> port_listener{};
175 std::string port_name;
176 std::string port_alias;
177 std::string object_path;
184 pw_direction direction{};
189 std::vector<port_info> inputs;
190 std::vector<port_info> outputs;
195 ossia::hash_map<uint32_t, node> physical_audio;
196 ossia::hash_map<uint32_t, node> physical_midi;
197 ossia::hash_map<uint32_t, node> software_audio;
198 ossia::hash_map<uint32_t, node> software_midi;
200 void for_each_port(
auto func)
202 for(
auto& map : {physical_audio, physical_midi, software_audio, software_midi})
204 for(
auto& [
id, node] : map)
206 for(
auto& port : node.inputs)
208 for(
auto& port : node.outputs)
214 void remove_port(uint32_t
id)
216 for(
auto map : {&physical_audio, &physical_midi, &software_audio, &software_midi})
218 for(
auto& [_, node] : *map)
220 ossia::remove_erase_if(
221 node.inputs, [
id](
const port_info& p) { return p.id == id; });
222 ossia::remove_erase_if(
223 node.outputs, [
id](
const port_info& p) { return p.id == id; });
231 const libpipewire& pw = libpipewire::instance();
232 explicit pipewire_context()
236 char* argv[] = {NULL};
240 this->main_loop = pw.main_loop_new(
nullptr);
247 this->lp = pw.main_loop_get_loop(this->main_loop);
250 ossia::logger().error(
"PipeWire: main_loop_get_loop failed!");
254 this->context = pw.context_new(lp,
nullptr, 0);
261 this->core = pw.context_connect(this->context,
nullptr, 0);
268 this->registry = pw_core_get_registry(this->core, PW_VERSION_REGISTRY, 0);
271 ossia::logger().error(
"PipeWire: core_get_registry failed!");
276 spa_zero(registry_listener);
277 static constexpr const struct pw_port_events port_events
278 = {.version = PW_VERSION_PORT_EVENTS,
280 [](
void* object,
const pw_port_info* info) {
281 ((pipewire_context*)
object)->register_port(info);
285 static constexpr const struct pw_registry_events registry_events = {
286 .version = PW_VERSION_REGISTRY_EVENTS,
288 [](
void* object, uint32_t id, uint32_t ,
const char* type,
289 uint32_t ,
const struct spa_dict* ) {
290 pipewire_context& self = *(pipewire_context*)
object;
293 if(strcmp(type, PW_TYPE_INTERFACE_Port) == 0)
296 = (pw_port*)pw_registry_bind(self.registry,
id, type, PW_VERSION_PORT, 0);
297 self.port_listener.push_back({id, port, std::make_unique<spa_hook>()});
298 auto& l = self.port_listener.back();
300 pw_port_add_listener(l.port, l.listener.get(), &port_events, &self);
304 [](
void* object, uint32_t id) {
305 pipewire_context& self = *(pipewire_context*)
object;
309 self.current_graph.remove_port(
id);
312 auto it = ossia::find_if(
313 self.port_listener, [&](
const listened_port& l) { return l.id == id; });
314 if(it != self.port_listener.end())
316 libpipewire::instance().proxy_destroy((pw_proxy*)it->port);
317 self.port_listener.erase(it);
323 pw_registry_add_listener(
324 this->registry, &this->registry_listener, ®istry_events,
this);
339 spa_hook core_listener;
341 static constexpr struct pw_core_events core_events = {
342 .version = PW_VERSION_CORE_EVENTS,
345 [](
void* object, uint32_t id,
int seq) {
346 auto& self = *(pipewire_context*)
object;
347 if(
id == PW_ID_CORE && seq == self.pending)
350 libpipewire::instance().main_loop_quit(self.main_loop);
359#if defined(PW_CORE_EVENT_BOUND_PROPS)
364 spa_zero(core_listener);
365 pw_core_add_listener(core, &core_listener, &core_events,
this);
367 pending = pw_core_sync(core, PW_ID_CORE, 0);
370 pw.main_loop_run(this->main_loop);
372 spa_hook_remove(&core_listener);
375 pw_proxy* link_ports(uint32_t out_port, uint32_t in_port)
377 auto props = pw.properties_new(
378 PW_KEY_LINK_OUTPUT_PORT, std::to_string(out_port).c_str(),
379 PW_KEY_LINK_INPUT_PORT, std::to_string(in_port).c_str(),
nullptr);
381 auto proxy = (pw_proxy*)pw_core_create_object(
382 this->core,
"link-factory", PW_TYPE_INTERFACE_Link, PW_VERSION_LINK,
388 pw.properties_free(props);
393 pw.properties_free(props);
397 void register_port(
const pw_port_info* info)
399 const spa_dict_item* item{};
404 spa_dict_for_each(item, info->props)
406 std::string_view k{item->key}, v{item->value};
407 if(k ==
"format.dsp")
409 else if(k ==
"port.name")
411 else if(k ==
"port.alias")
413 else if(k ==
"object.path")
415 else if(k ==
"port.id")
417 else if(k ==
"node.id")
419 else if(k ==
"port.physical" && v ==
"true")
421 else if(k ==
"port.terminal" && v ==
"true")
423 else if(k ==
"port.monitor" && v ==
"true")
425 else if(k ==
"port.direction")
429 p.direction = pw_direction::SPA_DIRECTION_OUTPUT;
433 p.direction = pw_direction::SPA_DIRECTION_INPUT;
438 if(p.node_id.empty())
441 const auto nid = std::stoul(p.node_id);
444 if(p.format.find(
"audio") != p.format.npos)
446 if(p.direction == pw_direction::SPA_DIRECTION_OUTPUT)
447 this->current_graph.physical_audio[nid].outputs.push_back(std::move(p));
449 this->current_graph.physical_audio[nid].inputs.push_back(std::move(p));
451 else if(p.format.find(
"midi") != p.format.npos)
453 if(p.direction == pw_direction::SPA_DIRECTION_OUTPUT)
454 this->current_graph.physical_midi[nid].outputs.push_back(std::move(p));
456 this->current_graph.physical_midi[nid].inputs.push_back(std::move(p));
465 if(p.format.find(
"audio") != p.format.npos)
467 if(p.direction == pw_direction::SPA_DIRECTION_OUTPUT)
468 this->current_graph.software_audio[nid].outputs.push_back(std::move(p));
470 this->current_graph.software_audio[nid].inputs.push_back(std::move(p));
472 else if(p.format.find(
"midi") != p.format.npos)
474 if(p.direction == pw_direction::SPA_DIRECTION_OUTPUT)
475 this->current_graph.software_midi[nid].outputs.push_back(std::move(p));
477 this->current_graph.software_midi[nid].inputs.push_back(std::move(p));
486 int get_fd() const noexcept
491 auto spa_callbacks = this->lp->control->iface.cb;
492 auto spa_loop_methods = (
const spa_loop_control_methods*)spa_callbacks.funcs;
493 if(spa_loop_methods->get_fd)
494 return spa_loop_methods->get_fd(spa_callbacks.data);
502 pw.proxy_destroy((pw_proxy*)this->registry);
503 for(
auto& [
id, p, l] : this->port_listener)
505 pw.proxy_destroy((pw_proxy*)p);
507 pw.core_disconnect(this->core);
509 pw.context_destroy(this->context);
511 pw.main_loop_destroy(this->main_loop);
521 std::string card_out;
523 std::vector<std::string> inputs;
524 std::vector<std::string> outputs;
530class pipewire_audio_protocol :
public audio_engine
537 std::shared_ptr<pipewire_context> loop{};
539 std::vector<pw_proxy*> links{};
541 explicit pipewire_audio_protocol(
542 std::shared_ptr<pipewire_context> loop,
const audio_setup& setup)
544 auto& pw = libpipewire::instance();
546 static constexpr const struct pw_filter_events filter_events = {
547 .version = PW_VERSION_FILTER_EVENTS,
554 .process = on_process,
556#if PW_VERSION_CORE > 3
569 PW_KEY_MEDIA_TYPE,
"Audio",
570 PW_KEY_MEDIA_CATEGORY,
"Duplex",
571 PW_KEY_MEDIA_ROLE,
"DSP",
572 PW_KEY_MEDIA_NAME,
"ossia",
573 PW_KEY_NODE_LATENCY, fmt::format(
"{}/{}", setup.buffer_size, setup.rate).c_str(),
574 PW_KEY_NODE_FORCE_QUANTUM, fmt::format(
"{}", setup.buffer_size).c_str(),
575 PW_KEY_NODE_LOCK_QUANTUM,
"true",
576 PW_KEY_NODE_RATE, fmt::format(
"{}/{}", 1, setup.rate).c_str(),
577 PW_KEY_NODE_FORCE_RATE, fmt::format(
"{}/{}", 1, setup.rate).c_str(),
578 PW_KEY_NODE_LOCK_RATE,
"true",
580 PW_KEY_NODE_PAUSE_ON_IDLE,
"false",
581 PW_KEY_NODE_SUSPEND_ON_IDLE,
"false",
585 this->filter = pw.filter_new_simple(
586 lp, setup.name.c_str(), filter_props, &filter_events,
this);
589 throw std::runtime_error(
"PipeWire: could not create filter instance");
593 for(std::size_t i = 0; i < setup.inputs.size(); i++)
595 auto p = (port*)pw.filter_add_port(
596 this->filter, PW_DIRECTION_INPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
599 PW_KEY_FORMAT_DSP,
"32 bit float mono audio", PW_KEY_PORT_NAME,
600 setup.inputs[i].c_str(), NULL),
602 input_ports.push_back(p);
605 for(std::size_t i = 0; i < setup.outputs.size(); i++)
607 auto p = (port*)pw.filter_add_port(
608 this->filter, PW_DIRECTION_OUTPUT, PW_FILTER_PORT_FLAG_MAP_BUFFERS,
611 PW_KEY_FORMAT_DSP,
"32 bit float mono audio", PW_KEY_PORT_NAME,
612 setup.outputs[i].c_str(), NULL),
614 output_ports.push_back(p);
617 if(pw.filter_connect(this->filter, PW_FILTER_FLAG_RT_PROCESS,
nullptr, 0) < 0)
619 throw std::runtime_error(
"PipeWire: cannot connect");
623 this->loop->synchronize();
626 auto node_id = filter_node_id();
627 while(node_id == 4294967295)
629 this->loop->synchronize();
630 node_id = filter_node_id();
638 const auto num_local_ins = this->input_ports.size();
639 const auto num_local_outs = this->output_ports.size();
640 auto& this_node = this->loop->current_graph.software_audio[node_id];
641 while(this_node.inputs.size() < num_local_ins
642 || this_node.outputs.size() < num_local_outs)
644 this->loop->synchronize();
651 this->effective_buffer_size = setup.buffer_size;
652 this->effective_sample_rate = setup.rate;
653 this->effective_inputs = setup.inputs.size();
654 this->effective_outputs = setup.outputs.size();
657 uint32_t filter_node_id() {
return this->loop->pw.filter_get_node_id(this->filter); }
661 auto node_id = filter_node_id();
663 std::vector<std::pair<std::optional<uint32_t>, std::optional<uint32_t>>>
665 std::vector<std::pair<std::optional<uint32_t>, std::optional<uint32_t>>>
669 for(
auto& [node, ports] : loop->current_graph.physical_audio)
671 auto& [out, in] = ports;
676 phys_in_to_ossia.emplace_back(port.id, std::nullopt);
680 for(
auto& port : out)
682 ossia_to_phys_out.emplace_back(std::nullopt, port.id);
687 for(
auto& [node, ports] : loop->current_graph.software_audio)
691 auto& [in, out] = ports;
694 for(std::size_t i = 0; i < in.size(); i++)
696 if(i >= phys_in_to_ossia.size())
699 phys_in_to_ossia[i].second = in[i].id;
703 for(std::size_t i = 0; i < out.size(); i++)
705 if(i >= ossia_to_phys_out.size())
708 ossia_to_phys_out[i].first = out[i].id;
715 for(
auto [phys, self] : phys_in_to_ossia)
719 if(
auto link = this->loop->link_ports(*phys, *self))
720 this->links.push_back(link);
728 for(
auto [self, phys] : ossia_to_phys_out)
732 if(
auto link = this->loop->link_ports(*self, *phys))
733 this->links.push_back(link);
742 void wait(
int ms)
override
747 using namespace std::chrono;
748 using clk = high_resolution_clock;
750 auto t0 = clk::now();
751 auto t1 = clk::now();
752 while(duration_cast<milliseconds>(t1 - t0).count() < ms)
754 pw_loop_iterate(loop->lp, ms);
759 bool running()
const override
766 ~pipewire_audio_protocol()
768 auto& pw = libpipewire::instance();
770 for(
auto link : this->links)
771 pw.proxy_destroy(link);
773 pw.filter_destroy(this->filter);
779 clear_buffers(pipewire_audio_protocol& self, uint32_t nframes, std::size_t outputs)
781 auto& pw = libpipewire::instance();
782 for(std::size_t i = 0; i < outputs; i++)
784 auto chan = (
float*)pw.filter_get_dsp_buffer(self.output_ports[i], nframes);
786 for(std::size_t j = 0; j < nframes; j++)
793 static void on_process(
void* userdata,
struct spa_io_position* position)
796 static const thread_local auto _
798 ossia::set_thread_name(
"ossia audio 0");
799 ossia::set_thread_pinned(thread_type::Audio, 0);
806 const auto& pw = libpipewire::instance();
807 auto& self = *(pipewire_audio_protocol*)userdata;
808 const uint32_t nframes = position->clock.duration;
812 const auto inputs = self.input_ports.size();
813 const auto outputs = self.output_ports.size();
814 if(self.stop_processing)
817 clear_buffers(self, nframes, outputs);
821 auto dummy = (
float*)alloca(
sizeof(
float) * nframes);
822 memset(dummy, 0,
sizeof(
float) * nframes);
824 auto float_input = (
float**)alloca(
sizeof(
float*) * inputs);
825 auto float_output = (
float**)alloca(
sizeof(
float*) * outputs);
826 for(std::size_t i = 0; i < inputs; i++)
828 float_input[i] = (
float*)pw.filter_get_dsp_buffer(self.input_ports[i], nframes);
829 if(float_input[i] ==
nullptr)
830 float_input[i] = dummy;
832 for(std::size_t i = 0; i < outputs; i++)
834 float_output[i] = (
float*)pw.filter_get_dsp_buffer(self.output_ports[i], nframes);
835 if(float_output[i] ==
nullptr)
836 float_output[i] = dummy;
840 ossia::audio_tick_state ts{float_input, float_output, (int)inputs,
841 (
int)outputs, nframes, position->clock.nsec * 1e-9};
846 std::vector<port*> input_ports;
847 std::vector<port*> output_ports;
spdlog::logger & logger() noexcept
Where the errors will be logged. Default is stderr.
Definition context.cpp:118