From e7cfaa1d99ef37200ca750583f7c732d72818f9f Mon Sep 17 00:00:00 2001 From: Andrea Bondavalli Date: Sun, 14 Jun 2020 05:30:58 -0700 Subject: [PATCH] Minor changes to code indentation --- daemon/rtsp_client.cpp | 121 ++++++++++++++++++++--------------------- daemon/rtsp_client.hpp | 37 ++++++------- 2 files changed, 76 insertions(+), 82 deletions(-) mode change 100755 => 100644 daemon/rtsp_client.cpp diff --git a/daemon/rtsp_client.cpp b/daemon/rtsp_client.cpp old mode 100755 new mode 100644 index bfb7fdc..e220063 --- a/daemon/rtsp_client.cpp +++ b/daemon/rtsp_client.cpp @@ -21,13 +21,13 @@ #include #include +#include #include #include #include +#include #include #include -#include -#include #include "log.hpp" #include "utils.hpp" @@ -37,7 +37,6 @@ using namespace boost::asio; using namespace boost::asio::ip; using namespace boost::algorithm; - struct RtspResponse { int32_t cseq{-1}; std::string content_type; @@ -88,7 +87,7 @@ RtspResponse read_response(tcp::iostream& s, uint16_t max_length) { << "cannot perform number conversion"; } - BOOST_LOG_TRIVIAL(debug) << "rtsp_client:: reading body length " + BOOST_LOG_TRIVIAL(debug) << "rtsp_client:: reading body length " << res.content_length; // read up to max_length if (res.content_length > 0 && res.content_length < max_length) { @@ -101,14 +100,13 @@ RtspResponse read_response(tcp::iostream& s, uint16_t max_length) { return res; } -std::pair RtspClient::process( - RtspClient::Observer callback, - const std::string& name, - const std::string& domain, - const std::string& path, - const std::string& address, - const std::string& port, - bool wait_for_updates) { +std::pair RtspClient::process(RtspClient::Observer callback, + const std::string& name, + const std::string& domain, + const std::string& path, + const std::string& address, + const std::string& port, + bool wait_for_updates) { RtspSource rtsp_source; ip::tcp::iostream s; try { @@ -160,88 +158,89 @@ std::pair RtspClient::process( auto res = read_response(s, max_body_length); if (is_describe && res.cseq != cseq) { BOOST_LOG_TRIVIAL(error) - << "rtsp_client:: invalid response sequence " << res.cseq + << "rtsp_client:: invalid response sequence " << res.cseq << " from rtsp://" << address << ":" << port << path; return {false, rtsp_source}; } - if (!res.content_type.empty() && - res.content_type.rfind("application/sdp", 0) == std::string::npos) { + if (!res.content_type.empty() && + res.content_type.rfind("application/sdp", 0) == std::string::npos) { BOOST_LOG_TRIVIAL(error) << "rtsp_client:: unsupported content-type " << res.content_type << " from " << "rtsp://" << address << ":" << port << path; - if (is_describe) { + if (is_describe) { return {false, rtsp_source}; - } + } } else { std::stringstream ss; ss << "rtsp:" << std::hex << crc16(reinterpret_cast(res.body.c_str()), res.body.length()); - /*<< std::hex << ip::address_v4::from_string(address.c_str()).to_ulong();*/ + /*<< std::hex << + * ip::address_v4::from_string(address.c_str()).to_ulong();*/ rtsp_source.id = ss.str(); rtsp_source.source = "mDNS"; rtsp_source.address = address; rtsp_source.sdp = std::move(res.body); - BOOST_LOG_TRIVIAL(info) << "rtsp_client:: completed " + BOOST_LOG_TRIVIAL(info) << "rtsp_client:: completed " << "rtsp://" << address << ":" << port << path; if (is_announce || is_describe) { if (is_announce && announced_name.empty()) { /* if no name from URL we try from SDP file */ announced_name = sdp_get_subject(rtsp_source.sdp); - } - callback(announced_name.empty() ? name : announced_name, domain, + } + callback(announced_name.empty() ? name : announced_name, domain, rtsp_source); } - if (is_announce) { + if (is_announce) { s << "RTSP/1.0 200 OK\r\n"; s << "CSeq: " << res.cseq << "\r\n"; s << "\r\n"; - } else if (!is_describe) { + } else if (!is_describe) { s << "RTSP/1.0 405 Method Not Allowed\r\n"; s << "CSeq: " << res.cseq << "\r\n"; s << "\r\n"; - } + } } if (wait_for_updates) { - g_mutex.lock(); - g_active_clients[{name, domain}] = &s; - g_mutex.unlock(); + g_mutex.lock(); + g_active_clients[{name, domain}] = &s; + g_mutex.unlock(); /* we start waiting for updates */ - do { + do { std::getline(s, request); - } while (request.empty() && !s.error()); - if (s.error()) { - BOOST_LOG_TRIVIAL(info) << "rtsp_client:: end: " - << s.error().message(); + } while (request.empty() && !s.error()); + if (s.error()) { + BOOST_LOG_TRIVIAL(info) + << "rtsp_client:: end: " << s.error().message(); break; - } - BOOST_LOG_TRIVIAL(info) << "rtsp_client:: received " << request; - boost::trim(request); - is_describe = is_announce = false; - announced_name = ""; - std::vector fields; - split(fields, request, boost::is_any_of(" ")); - if (fields.size() >= 2 && fields[0] == "ANNOUNCE") { - auto const res = parse_url(fields[1]); - if (std::get<0>(res)) { - /* if we find a valid announced source name we use it - * otherwise we try from SDP file or we use the mDNS name */ - auto path = std::get<4>(res); - if (path.rfind("/by-name/") != std::string::npos) { - announced_name = path.substr(9); - BOOST_LOG_TRIVIAL(debug) << "rtsp_client:: found announced name " - << announced_name; - } - } - is_announce = true; + } + BOOST_LOG_TRIVIAL(info) << "rtsp_client:: received " << request; + boost::trim(request); + is_describe = is_announce = false; + announced_name = ""; + std::vector fields; + split(fields, request, boost::is_any_of(" ")); + if (fields.size() >= 2 && fields[0] == "ANNOUNCE") { + auto const res = parse_url(fields[1]); + if (std::get<0>(res)) { + /* if we find a valid announced source name we use it + * otherwise we try from SDP file or we use the mDNS name */ + auto path = std::get<4>(res); + if (path.rfind("/by-name/") != std::string::npos) { + announced_name = path.substr(9); + BOOST_LOG_TRIVIAL(debug) + << "rtsp_client:: found announced name " << announced_name; + } + } + is_announce = true; + } } - } - } while (wait_for_updates); + } while (wait_for_updates); } catch (std::exception& e) { BOOST_LOG_TRIVIAL(warning) << "rtsp_client:: error with " @@ -259,7 +258,6 @@ std::pair RtspClient::process( return {true, rtsp_source}; } - void RtspClient::stop(const std::string& name, const std::string& domain) { std::lock_guard lock(g_mutex); auto it = g_active_clients.find({name, domain}); @@ -279,9 +277,8 @@ void RtspClient::stop_all() { std::lock_guard lock(g_mutex); auto it = g_active_clients.begin(); while (it != g_active_clients.end()) { - BOOST_LOG_TRIVIAL(info) - << "rtsp_client:: stopping client " - << it->first.first << " " << it->first.second; + BOOST_LOG_TRIVIAL(info) << "rtsp_client:: stopping client " + << it->first.first << " " << it->first.second; #if BOOST_VERSION < 106600 it->second->close(); #else @@ -291,10 +288,8 @@ void RtspClient::stop_all() { } } -std::pair RtspClient::describe( - const std::string& path, - const std::string& address, - const std::string& port) { +std::pair RtspClient::describe(const std::string& path, + const std::string& address, + const std::string& port) { return RtspClient::process({}, {}, {}, path, address, port, false); } - diff --git a/daemon/rtsp_client.hpp b/daemon/rtsp_client.hpp index 3da888b..ff36d67 100644 --- a/daemon/rtsp_client.hpp +++ b/daemon/rtsp_client.hpp @@ -20,8 +20,8 @@ #ifndef _RTSP_CLIENT_HPP_ #define _RTSP_CLIENT_HPP_ -#include #include +#include struct RtspSource { std::string id; @@ -36,33 +36,32 @@ class RtspClient { constexpr static uint16_t client_timeout = 10; // sec constexpr static const char dft_port[] = "554"; - using Observer = std::function; + using Observer = std::function; - static std::pair process( - Observer callback, - const std::string& name, - const std::string& domain, - const std::string& path, - const std::string& address, - const std::string& port = dft_port, - bool wait_for_updates = true); + static std::pair process(Observer callback, + const std::string& name, + const std::string& domain, + const std::string& path, + const std::string& address, + const std::string& port = dft_port, + bool wait_for_updates = true); static void stop(const std::string& name, const std::string& domain); static void stop_all(); static std::pair describe( - const std::string& path, - const std::string& address, - const std::string& port = dft_port); + const std::string& path, + const std::string& address, + const std::string& port = dft_port); inline static std::atomic g_seq_number{0}; - inline static std::map, - boost::asio::ip::tcp::iostream* /*stream*/> g_active_clients; + inline static std::map< + std::pair, + boost::asio::ip::tcp::iostream* /*stream*/> + g_active_clients; inline static std::mutex g_mutex; - }; #endif