1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/***
This file is part of snapcast
Copyright (C) 2014-2025 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/>.
***/
// prototype/interface header file
#include "file_player.hpp"
// local headers
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/str_compat.hpp"
#include "common/utils/string_utils.hpp"
// 3rd party headers
// standard headers
using namespace std;
namespace player
{
static constexpr auto LOG_TAG = "FilePlayer";
static constexpr auto kDefaultBuffer = 50ms;
static constexpr auto kDescription = "Raw PCM file output";
std::vector<PcmDevice> FilePlayer::pcm_list(const std::string& parameter)
{
auto params = utils::string::split_pairs(parameter, ',', '=');
string filename;
if (params.find("filename") != params.end())
filename = params["filename"];
if (filename.empty())
filename = "stdout";
return {PcmDevice{0, filename, kDescription}};
}
FilePlayer::FilePlayer(boost::asio::io_context& io_context, const ClientSettings::Player& settings, std::shared_ptr<Stream> stream)
: Player(io_context, settings, std::move(stream)), timer_(io_context), file_(nullptr)
{
auto params = utils::string::split_pairs(settings.parameter, ',', '=');
string filename;
if (params.find("filename") != params.end())
filename = params["filename"];
if (filename.empty() || (filename == "stdout"))
{
file_.reset(stdout, [](auto p) { std::ignore = p; });
}
else if (filename == "stderr")
{
file_.reset(stderr, [](auto p) { std::ignore = p; });
}
else if (filename != "null")
{
std::string mode = "w";
if (params.find("mode") != params.end())
mode = params["mode"];
if ((mode != "w") && (mode != "a"))
throw SnapException("Mode must be w (write) or a (append)");
mode += "b";
file_.reset(fopen(filename.c_str(), mode.c_str()), [](auto p) { fclose(p); });
if (!file_)
throw SnapException("Error opening file: '" + filename + "', error: " + cpt::to_string(errno));
}
}
FilePlayer::~FilePlayer()
{
LOG(DEBUG, LOG_TAG) << "Destructor\n";
stop(); // NOLINT
}
bool FilePlayer::needsThread() const
{
return false;
}
void FilePlayer::requestAudio()
{
auto numFrames = static_cast<uint32_t>(stream_->getFormat().msRate() * kDefaultBuffer.count());
auto needed = numFrames * stream_->getFormat().frameSize();
if (buffer_.size() < needed)
buffer_.resize(needed);
if (!stream_->getPlayerChunkOrSilence(buffer_.data(), 10ms, numFrames))
{
// LOG(INFO, LOG_TAG) << "Failed to get chunk. Playing silence.\n";
}
else
{
adjustVolume(static_cast<char*>(buffer_.data()), numFrames);
}
if (file_)
{
fwrite(buffer_.data(), 1, needed, file_.get());
fflush(file_.get());
}
loop();
}
void FilePlayer::loop()
{
next_request_ += kDefaultBuffer;
auto now = std::chrono::steady_clock::now();
if (next_request_ < now)
next_request_ = now + 1ms;
timer_.expires_at(next_request_);
timer_.async_wait([this](boost::system::error_code ec)
{
if (ec)
return;
requestAudio();
});
}
void FilePlayer::start()
{
next_request_ = std::chrono::steady_clock::now();
loop();
}
void FilePlayer::stop()
{
LOG(INFO, LOG_TAG) << "Stop\n";
timer_.cancel();
}
} // namespace player
|