mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-21 13:06:15 +02:00
move aixlog and popl to common
This commit is contained in:
parent
ba977ac123
commit
7cb595db28
43 changed files with 3157 additions and 59 deletions
|
@ -136,27 +136,6 @@ endif()
|
|||
|
||||
include_directories(${INCLUDE_DIRS})
|
||||
|
||||
cmake_push_check_state()
|
||||
set (CMAKE_REQUIRED_INCLUDES "${CMAKE_SOURCE_DIR}/externals/asio/asio/include")
|
||||
check_include_file_cxx(asio.hpp HAS_ASIO_HPP "-pthread -DASIO_STANDALONE -DASIO_DISABLE_STD_FUTURE -std=c++11")
|
||||
cmake_pop_check_state()
|
||||
if(HAS_ASIO_HPP)
|
||||
add_definitions("-DHAS_ASIO_HPP -DASIO_STANDALONE")
|
||||
else()
|
||||
message(FATAL_ERROR "Need to have asio installed")
|
||||
endif()
|
||||
|
||||
list(APPEND CMAKE_REQUIRED_INCLUDES "${INCLUDE_DIRS}")
|
||||
|
||||
check_include_file_cxx(popl.hpp HAS_POPL_HPP -std=c++11)
|
||||
if(NOT HAS_POPL_HPP)
|
||||
message(FATAL_ERROR "Need to have popl installed")
|
||||
endif()
|
||||
|
||||
check_include_file_cxx(aixlog.hpp HAS_AIXLOG_HPP -std=c++11)
|
||||
if(NOT HAS_AIXLOG_HPP)
|
||||
message(FATAL_ERROR "Need to have aixlog installed")
|
||||
endif()
|
||||
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/CheckCXX11StringSupport.cmake)
|
||||
CHECK_CXX11_STRING_SUPPORT(HAS_CXX11_STRING_SUPPORT)
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
***/
|
||||
|
||||
#include "browseAvahi.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
|
||||
using namespace std;
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "clientConnection.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "message/hello.h"
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#if defined(HAS_FLAC)
|
||||
#include "decoder/flacDecoder.h"
|
||||
#endif
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "message/hello.h"
|
||||
#include "message/time.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "flacDecoder.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/endian.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include <cmath>
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/endian.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "oggDecoder.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "pcmDecoder.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/endian.hpp"
|
||||
#include "common/snapException.h"
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "alsaPlayer.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "openslPlayer.h"
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "player.h"
|
||||
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/endian.hpp"
|
||||
#include "pcmDevice.h"
|
||||
#include "stream.h"
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
#include <sys/resource.h>
|
||||
|
||||
#include "browseZeroConf/browsemDNS.h"
|
||||
#include "common/popl.hpp"
|
||||
#include "controller.h"
|
||||
#include "popl.hpp"
|
||||
|
||||
#ifdef HAS_ALSA
|
||||
#include "player/alsaPlayer.h"
|
||||
|
@ -29,7 +29,7 @@
|
|||
#ifdef HAS_DAEMON
|
||||
#include "common/daemon.h"
|
||||
#endif
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/signalHandler.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "stream.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "timeProvider.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "timeProvider.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
|
||||
|
||||
TimeProvider::TimeProvider() : diffToServer_(0)
|
||||
|
|
1114
common/aixlog.hpp
Normal file
1114
common/aixlog.hpp
Normal file
File diff suppressed because it is too large
Load diff
1313
common/popl.hpp
Normal file
1313
common/popl.hpp
Normal file
File diff suppressed because it is too large
Load diff
|
@ -20,7 +20,7 @@
|
|||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "config.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils/file_utils.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "control_server.hpp"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils.h"
|
||||
#include "config.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "control_session_http.hpp"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "message/pcmChunk.h"
|
||||
#include <boost/beast/http/file_body.hpp>
|
||||
#include <iostream>
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "control_session_tcp.hpp"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "message/pcmChunk.h"
|
||||
|
||||
using namespace std;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
#if defined(HAS_FLAC)
|
||||
#include "flacEncoder.h"
|
||||
#endif
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "flacEncoder.h"
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils.h"
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
***/
|
||||
|
||||
#include "publishAvahi.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include <cstdlib>
|
||||
#include <thread>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "publishBonjour.h"
|
||||
|
||||
typedef union {
|
||||
|
|
|
@ -38,7 +38,7 @@ doc_root = /home/johannes/Develop/snapcast/control
|
|||
#
|
||||
[tcp]
|
||||
# enable TCP Json RPC
|
||||
#enabled = true
|
||||
enabled = true
|
||||
|
||||
# address to listen on
|
||||
# TODO: not implemented yet
|
||||
|
@ -90,6 +90,7 @@ stream = pipe:///tmp/snapfifo?name=default
|
|||
|
||||
# enable debug logging
|
||||
#debug = false
|
||||
debug = true
|
||||
|
||||
# log file name for the debug logs (debug must be enabled)
|
||||
#debug_logfile =
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include <memory>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include "popl.hpp"
|
||||
#include "common/popl.hpp"
|
||||
#ifdef HAS_DAEMON
|
||||
#include "common/daemon.h"
|
||||
#endif
|
||||
|
@ -36,7 +36,7 @@
|
|||
#if defined(HAS_AVAHI) || defined(HAS_BONJOUR)
|
||||
#include "publishZeroConf/publishmDNS.h"
|
||||
#endif
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "config.h"
|
||||
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "stream_server.hpp"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "config.h"
|
||||
#include "message/hello.h"
|
||||
#include "message/streamTags.h"
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
#include "stream_session.hpp"
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "message/pcmChunk.h"
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
|
240
server/stream_session_1.cpp
Normal file
240
server/stream_session_1.cpp
Normal file
|
@ -0,0 +1,240 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
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
|
||||
(at your option) 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 "stream_session.hpp"
|
||||
|
||||
#include "common/aixlog.hpp"
|
||||
#include "message/pcmChunk.h"
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
StreamSession::StreamSession(MessageReceiver* receiver, tcp::socket&& socket)
|
||||
: buffer_pos_(0), socket_(std::move(socket)), messageReceiver_(receiver), pcmStream_(nullptr)
|
||||
{
|
||||
base_msg_size_ = baseMessage_.getSize();
|
||||
buffer_.resize(base_msg_size_);
|
||||
}
|
||||
|
||||
|
||||
StreamSession::~StreamSession()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::read_message()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
socket_.async_read_some(boost::asio::buffer(&buffer_[buffer_pos_], baseMessage_.size - buffer_pos_),
|
||||
[this, self](boost::system::error_code ec, std::size_t length) {
|
||||
if (!ec)
|
||||
{
|
||||
buffer_pos_ += length;
|
||||
if (buffer_pos_ + 1 < baseMessage_.size)
|
||||
read_message();
|
||||
else
|
||||
{
|
||||
tv t;
|
||||
baseMessage_.received = t;
|
||||
if (messageReceiver_ != nullptr)
|
||||
messageReceiver_->onMessageReceived(this, baseMessage_, &buffer_[0]);
|
||||
buffer_pos_ = 0;
|
||||
read_header();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void StreamSession::read_header()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
socket_.async_read_some(boost::asio::buffer(&buffer_[buffer_pos_], base_msg_size_ - buffer_pos_),
|
||||
[this, self](boost::system::error_code ec, std::size_t length) {
|
||||
if (!ec)
|
||||
{
|
||||
buffer_pos_ += length;
|
||||
if (buffer_pos_ + 1 < base_msg_size_)
|
||||
read_header();
|
||||
else
|
||||
{
|
||||
baseMessage_.deserialize(&buffer_[0]);
|
||||
|
||||
if ((baseMessage_.type > message_type::kLast) || (baseMessage_.type < message_type::kFirst))
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "unknown message type received: " << baseMessage_.type << ", size: " << baseMessage_.size;
|
||||
throw std::runtime_error(ss.str().c_str());
|
||||
}
|
||||
else if (baseMessage_.size > msg::max_size)
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "received message of type " << baseMessage_.type << " to large: " << baseMessage_.size;
|
||||
throw std::runtime_error(ss.str().c_str());
|
||||
}
|
||||
|
||||
// LOG(INFO) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " <<
|
||||
// baseMessage.id << ", refers: " <<
|
||||
// baseMessage.refersTo << "\n";
|
||||
if (baseMessage_.size > buffer_.size())
|
||||
buffer_.resize(baseMessage_.size);
|
||||
buffer_pos_ = 0;
|
||||
read_message();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::setPcmStream(PcmStreamPtr pcmStream)
|
||||
{
|
||||
pcmStream_ = pcmStream;
|
||||
}
|
||||
|
||||
|
||||
const PcmStreamPtr StreamSession::pcmStream() const
|
||||
{
|
||||
return pcmStream_;
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::start()
|
||||
{
|
||||
read_header();
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::stop()
|
||||
{
|
||||
LOG(DEBUG) << "StreamSession::stop\n";
|
||||
boost::system::error_code ec;
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
||||
socket_.close(ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
||||
LOG(DEBUG) << "StreamSession stopped\n";
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::sendAsync(msg::message_ptr message, bool sendNow)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!message)
|
||||
return;
|
||||
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream stream(&streambuf);
|
||||
tv t;
|
||||
message->sent = t;
|
||||
message->serialize(stream);
|
||||
|
||||
auto self(shared_from_this());
|
||||
boost::asio::async_write(socket_, streambuf, [this, self](boost::system::error_code ec, std::size_t length) {
|
||||
if (!ec)
|
||||
{
|
||||
LOG(DEBUG) << "StreamSession wrote " << length << " bytes\n";
|
||||
// do_read();
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "StreamSession write error: " << ec.message() << "\n";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::setBufferMs(size_t bufferMs)
|
||||
{
|
||||
bufferMs_ = bufferMs;
|
||||
}
|
||||
|
||||
|
||||
bool StreamSession::send(msg::message_ptr message)
|
||||
{
|
||||
sendAsync(message);
|
||||
// // TODO on exception: set active = false
|
||||
// // LOG(INFO) << "send: " << message->type << ", size: " << message->getSize() << ", id: " << message->id << ", refers: " << message->refersTo <<
|
||||
// "\n"; boost::asio::streambuf streambuf; std::ostream stream(&streambuf); tv t; message->sent = t; message->serialize(stream); boost::asio::write(socket_,
|
||||
// streambuf);
|
||||
// // LOG(INFO) << "done: " << message->type << ", size: " << message->size << ", id: " << message->id << ", refers: " << message->refersTo << "\n";
|
||||
// return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// void StreamSession::reader()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// while (active_)
|
||||
// {
|
||||
// getNextMessage();
|
||||
// }
|
||||
// }
|
||||
// catch (const std::exception& e)
|
||||
// {
|
||||
// SLOG(ERROR) << "Exception in StreamSession::reader(): " << e.what() << endl;
|
||||
// }
|
||||
|
||||
// if (active_ && (messageReceiver_ != nullptr))
|
||||
// messageReceiver_->onDisconnect(this);
|
||||
// }
|
||||
|
||||
|
||||
// void StreamSession::writer()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// boost::asio::streambuf streambuf;
|
||||
// std::ostream stream(&streambuf);
|
||||
// shared_ptr<msg::BaseMessage> message;
|
||||
// while (active_)
|
||||
// {
|
||||
// if (messages_.try_pop(message, std::chrono::milliseconds(500)))
|
||||
// {
|
||||
// if (bufferMs_ > 0)
|
||||
// {
|
||||
// const msg::WireChunk* wireChunk = dynamic_cast<const msg::WireChunk*>(message.get());
|
||||
// if (wireChunk != nullptr)
|
||||
// {
|
||||
// chronos::time_point_clk now = chronos::clk::now();
|
||||
// size_t age = 0;
|
||||
// if (now > wireChunk->start())
|
||||
// age = std::chrono::duration_cast<chronos::msec>(now - wireChunk->start()).count();
|
||||
// // LOG(DEBUG) << "PCM chunk. Age: " << age << ", buffer: " << bufferMs_ << ", age > buffer: " << (age > bufferMs_) << "\n";
|
||||
// if (age > bufferMs_)
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
// send(message);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// catch (const std::exception& e)
|
||||
// {
|
||||
// SLOG(ERROR) << "Exception in StreamSession::writer(): " << e.what() << endl;
|
||||
// }
|
||||
|
||||
// if (active_ && (messageReceiver_ != nullptr))
|
||||
// messageReceiver_->onDisconnect(this);
|
||||
// }
|
102
server/stream_session_1.hpp
Normal file
102
server/stream_session_1.hpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
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
|
||||
(at your option) 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/>.
|
||||
***/
|
||||
|
||||
#ifndef STREAM_SESSION_H
|
||||
#define STREAM_SESSION_H
|
||||
|
||||
#include "common/queue.h"
|
||||
#include "message/message.h"
|
||||
#include "streamreader/streamManager.h"
|
||||
#include <atomic>
|
||||
#include <boost/asio.hpp>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
|
||||
class StreamSession;
|
||||
|
||||
|
||||
/// Interface: callback for a received message.
|
||||
class MessageReceiver
|
||||
{
|
||||
public:
|
||||
virtual void onMessageReceived(StreamSession* connection, const msg::BaseMessage& baseMessage, char* buffer) = 0;
|
||||
virtual void onDisconnect(StreamSession* connection) = 0;
|
||||
};
|
||||
|
||||
|
||||
/// Endpoint for a connected client.
|
||||
/**
|
||||
* Endpoint for a connected client.
|
||||
* Messages are sent to the client with the "send" method.
|
||||
* Received messages from the client are passed to the MessageReceiver callback
|
||||
*/
|
||||
class StreamSession : public std::enable_shared_from_this<StreamSession>
|
||||
{
|
||||
public:
|
||||
/// ctor. Received message from the client are passed to MessageReceiver
|
||||
StreamSession(MessageReceiver* receiver, tcp::socket&& socket);
|
||||
~StreamSession();
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
/// Sends a message to the client (synchronous)
|
||||
bool send(msg::message_ptr message);
|
||||
|
||||
/// Sends a message to the client (asynchronous)
|
||||
void sendAsync(msg::message_ptr message, bool sendNow = false);
|
||||
|
||||
/// Max playout latency. No need to send PCM data that is older than bufferMs
|
||||
void setBufferMs(size_t bufferMs);
|
||||
|
||||
std::string clientId;
|
||||
|
||||
std::string getIP()
|
||||
{
|
||||
return socket_.remote_endpoint().address().to_string();
|
||||
}
|
||||
|
||||
void setPcmStream(PcmStreamPtr pcmStream);
|
||||
const PcmStreamPtr pcmStream() const;
|
||||
|
||||
protected:
|
||||
void read_header();
|
||||
void read_message();
|
||||
|
||||
msg::BaseMessage baseMessage_;
|
||||
std::vector<char> buffer_;
|
||||
size_t buffer_pos_;
|
||||
size_t base_msg_size_;
|
||||
tcp::socket socket_;
|
||||
std::mutex mutex_;
|
||||
MessageReceiver* messageReceiver_;
|
||||
size_t bufferMs_;
|
||||
PcmStreamPtr pcmStream_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
242
server/stream_session_2.cpp
Normal file
242
server/stream_session_2.cpp
Normal file
|
@ -0,0 +1,242 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
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
|
||||
(at your option) 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 "stream_session.hpp"
|
||||
|
||||
#include "common/aixlog.hpp"
|
||||
#include "message/pcmChunk.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
StreamSession::StreamSession(MessageReceiver* receiver, tcp::socket&& socket)
|
||||
: active_(false), writerThread_(nullptr), socket_(std::move(socket)), messageReceiver_(receiver), pcmStream_(nullptr)
|
||||
{
|
||||
base_msg_size_ = baseMessage_.getSize();
|
||||
buffer_.resize(base_msg_size_);
|
||||
}
|
||||
|
||||
|
||||
StreamSession::~StreamSession()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::setPcmStream(PcmStreamPtr pcmStream)
|
||||
{
|
||||
pcmStream_ = pcmStream;
|
||||
}
|
||||
|
||||
|
||||
const PcmStreamPtr StreamSession::pcmStream() const
|
||||
{
|
||||
return pcmStream_;
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::read_message()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
socket_.async_read_some(boost::asio::buffer(&buffer_[buffer_pos_], baseMessage_.size - buffer_pos_),
|
||||
[this, self](boost::system::error_code ec, std::size_t length) {
|
||||
if (!ec)
|
||||
{
|
||||
buffer_pos_ += length;
|
||||
if (buffer_pos_ + 1 < baseMessage_.size)
|
||||
read_message();
|
||||
else
|
||||
{
|
||||
tv t;
|
||||
baseMessage_.received = t;
|
||||
if (messageReceiver_ != nullptr)
|
||||
messageReceiver_->onMessageReceived(this, baseMessage_, &buffer_[0]);
|
||||
buffer_pos_ = 0;
|
||||
read_header();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void StreamSession::read_header()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
socket_.async_read_some(boost::asio::buffer(&buffer_[buffer_pos_], base_msg_size_ - buffer_pos_),
|
||||
[this, self](boost::system::error_code ec, std::size_t length) {
|
||||
if (!ec)
|
||||
{
|
||||
buffer_pos_ += length;
|
||||
if (buffer_pos_ + 1 < base_msg_size_)
|
||||
read_header();
|
||||
else
|
||||
{
|
||||
baseMessage_.deserialize(&buffer_[0]);
|
||||
|
||||
if ((baseMessage_.type > message_type::kLast) || (baseMessage_.type < message_type::kFirst))
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "unknown message type received: " << baseMessage_.type << ", size: " << baseMessage_.size;
|
||||
throw std::runtime_error(ss.str().c_str());
|
||||
}
|
||||
else if (baseMessage_.size > msg::max_size)
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "received message of type " << baseMessage_.type << " to large: " << baseMessage_.size;
|
||||
throw std::runtime_error(ss.str().c_str());
|
||||
}
|
||||
|
||||
// LOG(INFO) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " <<
|
||||
// baseMessage.id << ", refers: " <<
|
||||
// baseMessage.refersTo << "\n";
|
||||
if (baseMessage_.size > buffer_.size())
|
||||
buffer_.resize(baseMessage_.size);
|
||||
buffer_pos_ = 0;
|
||||
read_message();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::start()
|
||||
{
|
||||
active_ = true;
|
||||
// readerThread_.reset(new thread(&StreamSession::reader, this));
|
||||
read_header();
|
||||
writerThread_.reset(new thread(&StreamSession::writer, this));
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::stop()
|
||||
{
|
||||
if (!active_)
|
||||
return;
|
||||
|
||||
active_ = false;
|
||||
|
||||
try
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
||||
socket_.close(ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
||||
if (writerThread_ && writerThread_->joinable())
|
||||
{
|
||||
LOG(DEBUG) << "StreamSession joining writerThread\n";
|
||||
messages_.abort_wait();
|
||||
writerThread_->join();
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
writerThread_ = nullptr;
|
||||
LOG(DEBUG) << "StreamSession stopped\n";
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::sendAsync(const msg::message_ptr& message, bool sendNow)
|
||||
{
|
||||
if (!message)
|
||||
return;
|
||||
|
||||
// the writer will take care about old messages
|
||||
while (messages_.size() > 2000) // chunk->getDuration() > 10000)
|
||||
messages_.pop();
|
||||
|
||||
if (sendNow)
|
||||
messages_.push_front(message);
|
||||
else
|
||||
messages_.push(message);
|
||||
}
|
||||
|
||||
|
||||
bool StreamSession::active() const
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
||||
|
||||
void StreamSession::setBufferMs(size_t bufferMs)
|
||||
{
|
||||
bufferMs_ = bufferMs;
|
||||
}
|
||||
|
||||
|
||||
bool StreamSession::send(const msg::message_ptr& message)
|
||||
{
|
||||
// TODO on exception: set active = false
|
||||
// LOG(INFO) << "send: " << message->type << ", size: " << message->getSize() << ", id: " << message->id << ", refers: " << message->refersTo << "\n";
|
||||
if (!active_)
|
||||
return false;
|
||||
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream stream(&streambuf);
|
||||
tv t;
|
||||
message->sent = t;
|
||||
message->serialize(stream);
|
||||
boost::asio::write(socket_, streambuf);
|
||||
// LOG(INFO) << "done: " << message->type << ", size: " << message->size << ", id: " << message->id << ", refers: " << message->refersTo << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void StreamSession::writer()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream stream(&streambuf);
|
||||
shared_ptr<msg::BaseMessage> message;
|
||||
while (active_)
|
||||
{
|
||||
if (messages_.try_pop(message, std::chrono::milliseconds(500)))
|
||||
{
|
||||
if (bufferMs_ > 0)
|
||||
{
|
||||
const msg::WireChunk* wireChunk = dynamic_cast<const msg::WireChunk*>(message.get());
|
||||
if (wireChunk != nullptr)
|
||||
{
|
||||
chronos::time_point_clk now = chronos::clk::now();
|
||||
size_t age = 0;
|
||||
if (now > wireChunk->start())
|
||||
age = std::chrono::duration_cast<chronos::msec>(now - wireChunk->start()).count();
|
||||
// LOG(DEBUG) << "PCM chunk. Age: " << age << ", buffer: " << bufferMs_ << ", age > buffer: " << (age > bufferMs_) << "\n";
|
||||
if (age > bufferMs_)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
SLOG(ERROR) << "Exception in StreamSession::writer(): " << e.what() << endl;
|
||||
}
|
||||
|
||||
if (active_ && (messageReceiver_ != nullptr))
|
||||
messageReceiver_->onDisconnect(this);
|
||||
}
|
107
server/stream_session_2.hpp
Normal file
107
server/stream_session_2.hpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2019 Johannes Pohl
|
||||
|
||||
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
|
||||
(at your option) 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/>.
|
||||
***/
|
||||
|
||||
#ifndef STREAM_SESSION_H
|
||||
#define STREAM_SESSION_H
|
||||
|
||||
#include "common/queue.h"
|
||||
#include "message/message.h"
|
||||
#include "streamreader/streamManager.h"
|
||||
#include <atomic>
|
||||
#include <boost/asio.hpp>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
|
||||
class StreamSession;
|
||||
|
||||
|
||||
/// Interface: callback for a received message.
|
||||
class MessageReceiver
|
||||
{
|
||||
public:
|
||||
virtual void onMessageReceived(StreamSession* connection, const msg::BaseMessage& baseMessage, char* buffer) = 0;
|
||||
virtual void onDisconnect(StreamSession* connection) = 0;
|
||||
};
|
||||
|
||||
|
||||
/// Endpoint for a connected client.
|
||||
/**
|
||||
* Endpoint for a connected client.
|
||||
* Messages are sent to the client with the "send" method.
|
||||
* Received messages from the client are passed to the MessageReceiver callback
|
||||
*/
|
||||
class StreamSession : public std::enable_shared_from_this<StreamSession>
|
||||
{
|
||||
public:
|
||||
/// ctor. Received message from the client are passed to MessageReceiver
|
||||
StreamSession(MessageReceiver* receiver, tcp::socket&& socket);
|
||||
~StreamSession();
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
/// Sends a message to the client (synchronous)
|
||||
bool send(const msg::message_ptr& message);
|
||||
|
||||
/// Sends a message to the client (asynchronous)
|
||||
void sendAsync(const msg::message_ptr& message, bool sendNow = false);
|
||||
|
||||
bool active() const;
|
||||
|
||||
/// Max playout latency. No need to send PCM data that is older than bufferMs
|
||||
void setBufferMs(size_t bufferMs);
|
||||
|
||||
std::string clientId;
|
||||
|
||||
std::string getIP()
|
||||
{
|
||||
return socket_.remote_endpoint().address().to_string();
|
||||
}
|
||||
|
||||
void setPcmStream(PcmStreamPtr pcmStream);
|
||||
const PcmStreamPtr pcmStream() const;
|
||||
|
||||
protected:
|
||||
void read_header();
|
||||
void read_message();
|
||||
void writer();
|
||||
|
||||
std::atomic<bool> active_;
|
||||
|
||||
msg::BaseMessage baseMessage_;
|
||||
std::vector<char> buffer_;
|
||||
size_t buffer_pos_;
|
||||
size_t base_msg_size_;
|
||||
|
||||
std::unique_ptr<std::thread> writerThread_;
|
||||
tcp::socket socket_;
|
||||
MessageReceiver* messageReceiver_;
|
||||
Queue<std::shared_ptr<msg::BaseMessage>> messages_;
|
||||
size_t bufferMs_;
|
||||
PcmStreamPtr pcmStream_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -17,8 +17,8 @@
|
|||
***/
|
||||
|
||||
#include "airplayStream.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "base64.h"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include <memory>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "encoder/encoderFactory.h"
|
||||
#include "fileStream.h"
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
#include <memory>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "encoder/encoderFactory.h"
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "encoder/encoderFactory.h"
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
|
||||
#include "processStream.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "spotifyStream.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/utils.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
#include "streamManager.h"
|
||||
#include "airplayStream.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/snapException.h"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
***/
|
||||
|
||||
#include "streamUri.h"
|
||||
#include "aixlog.hpp"
|
||||
#include "common/aixlog.hpp"
|
||||
#include "common/strCompat.h"
|
||||
#include "common/utils/string_utils.h"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue