From 78f735f4174c2a0fe275da41252e227226af5b3b Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 9 Mar 2011 13:39:07 -0800 Subject: audio: register arches with priorities, ex: prefer alsa over oss --- gr-audio/lib/gr_audio_registry.cc | 74 +++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 22 deletions(-) (limited to 'gr-audio/lib/gr_audio_registry.cc') diff --git a/gr-audio/lib/gr_audio_registry.cc b/gr-audio/lib/gr_audio_registry.cc index 7be652807..7aa9dc7a4 100644 --- a/gr-audio/lib/gr_audio_registry.cc +++ b/gr-audio/lib/gr_audio_registry.cc @@ -24,33 +24,55 @@ #include #include #include -#include #include /*********************************************************************** * Create registries **********************************************************************/ -typedef std::pair source_pair_t; -static std::vector &get_source_registry(void){ - static std::vector _registry; + +struct source_entry_t{ + reg_prio_type prio; + std::string arch; + source_factory_t source; +}; + +static std::vector &get_source_registry(void){ + static std::vector _registry; return _registry; } -typedef std::pair sink_pair_t; -static std::vector &get_sink_registry(void){ - static std::vector _registry; +struct sink_entry_t{ + reg_prio_type prio; + std::string arch; + sink_factory_t sink; +}; + +static std::vector &get_sink_registry(void){ + static std::vector _registry; return _registry; } /*********************************************************************** * Register functions **********************************************************************/ -void audio_register_source(const std::string &name, source_factory_t source){ - get_source_registry().push_back(std::make_pair(name, source)); +void audio_register_source( + reg_prio_type prio, const std::string &arch, source_factory_t source +){ + source_entry_t entry; + entry.prio = prio; + entry.arch = arch; + entry.source = source; + get_source_registry().push_back(entry); } -void audio_register_sink(const std::string &name, sink_factory_t sink){ - get_sink_registry().push_back(std::make_pair(name, sink)); +void audio_register_sink( + reg_prio_type prio, const std::string &arch, sink_factory_t sink +){ + sink_entry_t entry; + entry.prio = prio; + entry.arch = arch; + entry.sink = sink; + get_sink_registry().push_back(entry); } /*********************************************************************** @@ -61,6 +83,7 @@ static std::string default_arch_name(void){ } static void do_arch_warning(const std::string &arch){ + if (arch.empty()) return; //no warning when arch not specified std::cerr << "Could not find audio architecture \"" << arch << "\" in registry." << std::endl; std::cerr << " Defaulting to the first available architecture..." << std::endl; } @@ -73,14 +96,17 @@ audio_source::sptr audio_make_source( if (get_source_registry().empty()){ throw std::runtime_error("no available audio source factories"); } + std::string arch = default_arch_name(); - BOOST_FOREACH(const source_pair_t &e, get_source_registry()){ - if (arch.empty() || arch == e.first){ - return e.second(sampling_rate, device_name, ok_to_block); - } + source_entry_t entry = get_source_registry().front(); + + BOOST_FOREACH(const source_entry_t &e, get_source_registry()){ + if (e.prio > entry.prio) entry = e; //entry is highest prio + if (arch != e.arch) continue; //continue when no match + return e.source(sampling_rate, device_name, ok_to_block); } - do_arch_warning(arch); - return get_source_registry().front().second(sampling_rate, device_name, ok_to_block); + //std::cout << "Audio source arch: " << entry.name << std::endl; + return entry.source(sampling_rate, device_name, ok_to_block); } audio_sink::sptr audio_make_sink( @@ -91,14 +117,18 @@ audio_sink::sptr audio_make_sink( if (get_sink_registry().empty()){ throw std::runtime_error("no available audio sink factories"); } + std::string arch = default_arch_name(); - BOOST_FOREACH(const sink_pair_t &e, get_sink_registry()){ - if (arch.empty() || arch == e.first){ - return e.second(sampling_rate, device_name, ok_to_block); - } + sink_entry_t entry = get_sink_registry().front(); + + BOOST_FOREACH(const sink_entry_t &e, get_sink_registry()){ + if (e.prio > entry.prio) entry = e; //entry is highest prio + if (arch != e.arch) continue; //continue when no match + return e.sink(sampling_rate, device_name, ok_to_block); } do_arch_warning(arch); - return get_sink_registry().front().second(sampling_rate, device_name, ok_to_block); + //std::cout << "Audio sink arch: " << entry.name << std::endl; + return entry.sink(sampling_rate, device_name, ok_to_block); } /*********************************************************************** -- cgit