aes67-daemon/daemon/mdns_client.cpp
Andrea Bondavalli 5deb6c1927 Added to the daemon the support for Multicast DNS (using Linux Avahi) to allow discovery of remote audio sources and of RTSP for SDP transfer.
Added to the WebUI the possibility to directly select a remote source SDP file for a Sink.

New files:
daemon/mdns_client.hpp,cpp -> mDNS client implementation using Avahi client library
daemon/rtsp_client.hpp,cpp -> RTSP client implementation used to transfer SDP file
daemon/utils.cpp -> used for common utility functions
.clang-format -> added clang-format configuration file

Modified files:
daemon/CMakeList.txt -> added support for Avahi and option WITH_AVAHI=[yes/no] to compile the daemon with or without Avahi mDNS support
daemon/config.hpp,cpp -> added configuration option mdns_enabled to enable or disable mDNS discovery at runtime
daemon/json.cpp -> extended JSON config with mdns_enabled option
daemon/browser.hpp,cpp -> added support for mDNS client to the browser
daemon/session_manager.cpp -> added support for RTSP protocol to Source URL field and fixed issue with SDP file parsing
webui/RemoteSources.js -> added visualization of mDNS remote sources
webui/SinkEdit.js -> added the possibility to directly select a remote source SDP file for a Sink
webui/SourceInfo.js -> added visualization of protocol source (SAP, mDNS or local) for a source
ubuntu-packages.sh -> added libavahi-client-dev to the list of required packages
build.sh -> added WITH_AVAHI=yes option when invoking CMake
README.md -> added notes about mDNS support via Avahi
daemon/README.md -> added notes about mDNS support via Avahi, support for RTSP protocol in source and new mdns_enabled config param

Additional minor changes to remaining files.
2020-03-29 19:41:56 +02:00

256 lines
9.4 KiB
C++

//
// mdns_client.cpp
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "mdns_client.hpp"
#include <boost/asio.hpp>
#include "config.hpp"
#include "log.hpp"
#include "rtsp_client.hpp"
#ifdef _USE_AVAHI_
void MDNSClient::resolve_callback(AvahiServiceResolver* r,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char* name,
const char* type,
const char* domain,
const char* host_name,
const AvahiAddress* address,
uint16_t port,
AvahiStringList* txt,
AvahiLookupResultFlags flags,
void* userdata) {
MDNSClient& mdns = *(reinterpret_cast<MDNSClient*>(userdata));
/* Called whenever a service has been resolved successfully or timed out */
switch (event) {
case AVAHI_RESOLVER_FAILURE:
BOOST_LOG_TRIVIAL(error) << "avahi_client:: (Resolver) failed to resolve "
<< "service " << name << " of type " << type
<< " in domain " << domain << " : "
<< avahi_strerror(avahi_client_errno(
avahi_service_resolver_get_client(r)));
break;
case AVAHI_RESOLVER_FOUND:
BOOST_LOG_TRIVIAL(debug) << "avahi_client:: (Resolver) "
<< "service " << name << " of type " << type
<< " in domain " << domain;
char addr[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(addr, sizeof(addr), address);
char info[256];
snprintf(info, sizeof(info),
"%s:%u (%s), "
"local: %i, "
"our_own: %i, "
"wide_area: %i, "
"multicast: %i, "
"cached: %i",
host_name, port, addr, !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
!!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
!!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
!!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
!!(flags & AVAHI_LOOKUP_RESULT_CACHED));
BOOST_LOG_TRIVIAL(debug) << "avahi_client:: (Resolver) " << info;
boost::system::error_code ec;
boost::asio::ip::address_v4::from_string(addr, ec);
if (!ec) {
/* if valid IPv4 address retrieve source data via RTSP */
std::lock_guard<std::mutex> lock(mdns.sources_res_mutex_);
/* have fun ;-) */
mdns.sources_res_.emplace_back(std::async(
std::launch::async,
[&mdns, name_ = std::forward<std::string>(name),
domain_ = std::forward<std::string>(domain),
addr_ = std::forward<std::string>(addr),
port_ = std::forward<std::string>(std::to_string(port))] {
auto res = RTSPClient::describe(std::string("/by-name/") + name_,
addr_, port_);
if (res.first) {
mdns.on_new_rtsp_source(name_, domain_, res.second);
}
}));
}
break;
}
avahi_service_resolver_free(r);
}
void MDNSClient::browse_callback(AvahiServiceBrowser* b,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
const char* type,
const char* domain,
AvahiLookupResultFlags flags,
void* userdata) {
MDNSClient& mdns = *(reinterpret_cast<MDNSClient*>(userdata));
/* Called whenever a new services becomes available on the LAN or is removed
* from the LAN */
switch (event) {
case AVAHI_BROWSER_FAILURE:
BOOST_LOG_TRIVIAL(fatal) << "avahi_client:: (Browser) "
<< avahi_strerror(avahi_client_errno(
avahi_service_browser_get_client(b)));
avahi_threaded_poll_quit(mdns.poll_.get());
return;
case AVAHI_BROWSER_NEW:
BOOST_LOG_TRIVIAL(info) << "avahi_client:: (Browser) NEW: "
<< "service " << name << " of type " << type
<< " in domain " << domain;
/* We ignore the returned resolver object. In the callback
function we free it. If the server is terminated before
the callback function is called the server will free
the resolver for us. */
if (!(avahi_service_resolver_new(mdns.client_.get(), interface, protocol,
name, type, domain, AVAHI_PROTO_UNSPEC,
AVAHI_LOOKUP_NO_TXT, resolve_callback,
&mdns))) {
BOOST_LOG_TRIVIAL(error)
<< "avahi_client:: "
<< "Failed to resolve service " << name << " : "
<< avahi_strerror(avahi_client_errno(mdns.client_.get()));
}
break;
case AVAHI_BROWSER_REMOVE:
BOOST_LOG_TRIVIAL(info) << "avahi_client:: (Browser) REMOVE: "
<< "service " << name << " of type " << type
<< " in domain " << domain;
mdns.on_remove_rtsp_source(name, domain);
break;
case AVAHI_BROWSER_ALL_FOR_NOW:
BOOST_LOG_TRIVIAL(debug) << "avahi_client:: (Browser) ALL_FOR_NOW";
break;
case AVAHI_BROWSER_CACHE_EXHAUSTED:
BOOST_LOG_TRIVIAL(debug) << "avahi_client:: (Browser) CACHE_EXHAUSTED";
break;
}
}
void MDNSClient::client_callback(AvahiClient* c,
AvahiClientState state,
void* userdata) {
MDNSClient& mdns = *(reinterpret_cast<MDNSClient*>(userdata));
/* Called whenever the client or server state changes */
if (state == AVAHI_CLIENT_FAILURE) {
BOOST_LOG_TRIVIAL(fatal) << "avahi_client:: server connection failure: "
<< avahi_strerror(avahi_client_errno(c));
avahi_threaded_poll_quit(mdns.poll_.get());
}
}
#endif
bool MDNSClient::init() {
if (running_) {
return true;
}
#ifdef _USE_AVAHI_
/* allocate poll loop object */
poll_.reset(avahi_threaded_poll_new());
if (poll_ == nullptr) {
BOOST_LOG_TRIVIAL(fatal)
<< "avahi_client:: failed to create threaded poll object";
return false;
}
/* allocate a new client */
int error;
client_.reset(avahi_client_new(avahi_threaded_poll_get(poll_.get()),
AVAHI_CLIENT_NO_FAIL, client_callback, this,
&error));
if (client_ == nullptr) {
BOOST_LOG_TRIVIAL(fatal)
<< "avahi_client:: failed to create client: " << avahi_strerror(error);
return false;
}
/* Create the service browser */
sb_.reset(avahi_service_browser_new(client_.get(), AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC, "_rtsp._tcp", nullptr,
{}, browse_callback, this));
if (sb_ == nullptr) {
BOOST_LOG_TRIVIAL(fatal)
<< "avahi_client:: failed to create service browser: "
<< avahi_strerror(avahi_client_errno(client_.get()));
return false;
}
(void)avahi_threaded_poll_start(poll_.get());
#endif
running_ = true;
return true;
}
void MDNSClient::process_results() {
#ifdef _USE_AVAHI_
std::lock_guard<std::mutex> lock(sources_res_mutex_);
/* remove all completed results and populate remote sources list */
sources_res_.remove_if([](auto& result) {
if (!result.valid()) {
/* if invalid future remove from the list */
return true;
}
auto status = result.wait_for(std::chrono::milliseconds(0));
if (status == std::future_status::ready) {
result.get();
/* if completed remove from the list */
return true;
}
/* if not completed leave in the list */
return false;
});
#endif
}
bool MDNSClient::terminate() {
if (running_) {
running_ = false;
#ifdef _USE_AVAHI_
/* remove all completed results and populate remote sources list */
/* wait for all pending results and remove from list */
std::lock_guard<std::mutex> lock(sources_res_mutex_);
BOOST_LOG_TRIVIAL(fatal) << "avahi_client:: waiting for "
<< sources_res_.size() << " RTSP clients";
sources_res_.remove_if([](auto& result) {
if (result.valid()) {
result.wait();
}
return true;
});
avahi_threaded_poll_stop(poll_.get());
#endif
}
return true;
}