aes67-daemon/daemon/rtsp_server.hpp
Andrea Bondavalli 0dbfe78a10 - Added support for mDNS/RTSP sources advertisement compatible with Ravenna standard.
- mDNS advertisement for all local Sources is implemented by mdns_server.[cpp,hpp] and based on Linux Avahi.
  - RTSP server implementation supports DESCRIBE method to return SDP of local Sources and supports persistent connection but doesn't provide service updates via UPDATE method.
- Modified RTSP client to browse for _ravenna_session subtype of _rtsp._tcp services only.
- Modified SAP and mDNS discovery to avoid returning local Sources advertised by the daemon.
- Added "rtsp_port" and "node_id" config parameters.
  - rtsp_port is a read/write parameter that contains the port of the RTSP server.
  - node_id is a read only parameter that contains the unique daemon identifier used in mDNS and SAP sources announcements.
- Modified session manager to check that every Source and Sink created by the user has a unique name.
- Modified WebUI to visualize node_id and to visualize and edit rtsp_port parameters in Config tab.
- Extended regression test to verify proper behaviour of mDNS/RTSP sources advertisement and discovery.
- Modified REST API to browse remote sources to allow browsing of SAP, mDNS and all sources via HTTP GET /api/browse/sources/[all|mdns|sap].
- Amended daemon documentation.
2020-04-23 11:45:58 -07:00

118 lines
3.4 KiB
C++

// rtsp_server.hpp
//
// 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 <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <memory>
#include "session_manager.hpp"
#ifndef _RTSP_SERVER_HPP_
#define _RTSP_SERVER_HPP_
using namespace std::chrono;
using boost::asio::deadline_timer;
using boost::asio::ip::tcp;
using second_t = duration<double, std::ratio<1> >;
class RtspSession : public std::enable_shared_from_this<RtspSession> {
public:
constexpr static uint16_t max_length = 4096; // byte
constexpr static uint16_t session_tout_secs = 10; // sec
RtspSession(std::shared_ptr<SessionManager> session_manager,
tcp::socket socket)
: session_manager_(session_manager),
socket_(std::move(socket)),
length_{0},
cseq_{-1},
consumed_{0} {}
virtual ~RtspSession() {
BOOST_LOG_TRIVIAL(debug) << "rtsp_server:: session end";
}
void start();
void stop();
private:
bool process_request();
void build_response(const std::string& url);
void read_request();
void send_error(int status_code, const std::string& description);
void send_response(const std::string& response);
std::shared_ptr<SessionManager> session_manager_;
tcp::socket socket_;
char data_[max_length + 1];
std::string request_;
size_t length_{0};
int32_t cseq_{-1};
size_t consumed_{0};
};
class RtspServer {
public:
RtspServer() = delete;
RtspServer(std::shared_ptr<SessionManager> session_manager,
std::shared_ptr<Config> config)
: session_manager_(session_manager),
config_(config),
sessions_(SessionManager::stream_id_max),
sessions_start_point_(SessionManager::stream_id_max),
acceptor_(io_service_,
tcp::endpoint(boost::asio::ip::address::from_string(
config_->get_ip_addr_str()),
config_->get_rtsp_port())),
socket_(io_service_) {}
bool init() {
accept();
/* start rtsp server on a separate thread */
res_ = std::async(std::launch::async, &RtspServer::worker, this);
return true;
}
bool terminate() {
BOOST_LOG_TRIVIAL(info) << "rtsp_server: stopping ... ";
io_service_.stop();
res_.get();
return true;
}
void process();
private:
void worker();
void accept();
std::mutex mutex_;
boost::asio::io_service io_service_;
std::shared_ptr<SessionManager> session_manager_;
std::shared_ptr<Config> config_;
std::vector<std::weak_ptr<RtspSession> > sessions_;
std::vector<time_point<steady_clock> > sessions_start_point_;
tcp::acceptor acceptor_;
tcp::socket socket_{io_service_};
std::future<void> res_;
};
#endif