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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/***
This file is part of snapcast
Copyright (C) 2014-2024 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 "meta_stream.hpp"
// local headers
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/utils/string_utils.hpp"
using namespace std;
namespace streamreader
{
static constexpr auto LOG_TAG = "MetaStream";
// static constexpr auto kResyncTolerance = 50ms;
MetaStream::MetaStream(PcmStream::Listener* pcmListener, const std::vector<std::shared_ptr<PcmStream>>& streams, boost::asio::io_context& ioc,
const ServerSettings& server_settings, const StreamUri& uri)
: PcmStream(pcmListener, ioc, server_settings, uri), first_read_(true)
{
auto path_components = utils::string::split(uri.path, '/');
for (const auto& component : path_components)
{
if (component.empty())
continue;
bool found = false;
for (const auto& stream : streams)
{
if (stream->getName() == component)
{
streams_.push_back(stream);
stream->addListener(this);
found = true;
break;
}
}
if (!found)
throw SnapException("Unknown stream: \"" + component + "\"");
}
if (streams_.empty())
throw SnapException("Meta stream '" + getName() + "' must contain at least one stream");
active_stream_ = streams_.front();
resampler_ = make_unique<Resampler>(active_stream_->getSampleFormat(), sampleFormat_);
}
MetaStream::~MetaStream()
{
stop();
}
void MetaStream::start()
{
LOG(DEBUG, LOG_TAG) << "Start, sampleformat: " << sampleFormat_.toString() << "\n";
PcmStream::start();
}
void MetaStream::stop()
{
active_ = false;
}
void MetaStream::onPropertiesChanged(const PcmStream* pcmStream, const Properties& properties)
{
LOG(DEBUG, LOG_TAG) << "onPropertiesChanged: " << pcmStream->getName() << "\n";
// std::lock_guard<std::recursive_mutex> lock(mutex_);
if (pcmStream != active_stream_.get())
return;
setProperties(properties);
}
void MetaStream::onStateChanged(const PcmStream* pcmStream, ReaderState state)
{
LOG(DEBUG, LOG_TAG) << "onStateChanged: " << pcmStream->getName() << ", state: " << state << "\n";
std::lock_guard<std::recursive_mutex> lock(active_mutex_);
// Should a pause keep the stream active? E.g. Spotify can only pause, so it would never get inactive
// if (active_stream_->getProperties().playback_status == PlaybackStatus::kPaused)
// return;
auto switch_stream = [this](std::shared_ptr<PcmStream> new_stream)
{
if (new_stream == active_stream_)
return;
LOG(INFO, LOG_TAG) << "Stream: " << name_ << ", switching active stream: " << (active_stream_ ? active_stream_->getName() : "<null>") << " => "
<< new_stream->getName() << "\n";
active_stream_ = new_stream;
setProperties(active_stream_->getProperties());
resampler_ = make_unique<Resampler>(active_stream_->getSampleFormat(), sampleFormat_);
};
for (const auto& stream : streams_)
{
if (stream->getState() == ReaderState::kPlaying)
{
if (state_ != ReaderState::kPlaying) // || (active_stream_ != stream))
first_read_ = true;
if (active_stream_ != stream)
{
switch_stream(stream);
}
setState(ReaderState::kPlaying);
return;
}
}
switch_stream(streams_.front());
setState(ReaderState::kIdle);
}
void MetaStream::onChunkRead(const PcmStream* pcmStream, const msg::PcmChunk& chunk)
{
// LOG(TRACE, LOG_TAG) << "onChunkRead: " << pcmStream->getName() << ", duration: " << chunk.durationMs() << "\n";
// std::lock_guard<std::recursive_mutex> lock(mutex_);
std::lock_guard<std::recursive_mutex> lock(active_mutex_);
if (pcmStream != active_stream_.get())
return;
// active_stream_->sampleFormat_
// sampleFormat_
if (first_read_)
{
first_read_ = false;
LOG(INFO, LOG_TAG) << "first read, updating timestamp\n";
tvEncodedChunk_ = std::chrono::steady_clock::now() - chunk.duration<std::chrono::nanoseconds>();
next_tick_ = std::chrono::steady_clock::now();
}
next_tick_ += chunk.duration<std::chrono::nanoseconds>();
auto currentTick = std::chrono::steady_clock::now();
auto next_read = next_tick_ - currentTick;
// Read took longer, wait for the buffer to fill up
if (next_read < 0ms)
{
// if (next_read >= -kResyncTolerance)
// {
// LOG(INFO, LOG_TAG) << "next read < 0 (" << getName() << "): " << std::chrono::duration_cast<std::chrono::microseconds>(next_read).count() / 1000.
// << " ms\n";
// }
// else
// {
resync(-next_read);
first_read_ = true;
// }
}
if (resampler_ && resampler_->resamplingNeeded())
{
auto resampled_chunk = resampler_->resample(chunk);
if (resampled_chunk)
chunkRead(*resampled_chunk);
}
else
chunkRead(chunk);
}
void MetaStream::onChunkEncoded(const PcmStream* pcmStream, std::shared_ptr<msg::PcmChunk> chunk, double duration)
{
std::ignore = pcmStream;
std::ignore = chunk;
std::ignore = duration;
// LOG(TRACE, LOG_TAG) << "onChunkEncoded: " << pcmStream->getName() << ", duration: " << duration << "\n";
// chunkEncoded(*encoder_, chunk, duration);
}
void MetaStream::onResync(const PcmStream* pcmStream, double ms)
{
LOG(DEBUG, LOG_TAG) << "onResync: " << pcmStream->getName() << ", duration: " << ms << " ms\n";
// std::lock_guard<std::recursive_mutex> lock(mutex_);
if (pcmStream != active_stream_.get())
return;
resync(std::chrono::nanoseconds(static_cast<int64_t>(ms * 1000000)));
}
// Setter for properties
void MetaStream::setShuffle(bool shuffle, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setShuffle(shuffle, std::move(handler));
}
void MetaStream::setLoopStatus(LoopStatus status, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setLoopStatus(status, std::move(handler));
}
void MetaStream::setVolume(uint16_t volume, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setVolume(volume, std::move(handler));
}
void MetaStream::setMute(bool mute, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setMute(mute, std::move(handler));
}
void MetaStream::setRate(float rate, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setRate(rate, std::move(handler));
}
// Control commands
void MetaStream::setPosition(std::chrono::milliseconds position, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->setPosition(position, std::move(handler));
}
void MetaStream::seek(std::chrono::milliseconds offset, ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->seek(offset, std::move(handler));
}
void MetaStream::next(ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->next(std::move(handler));
}
void MetaStream::previous(ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->previous(std::move(handler));
}
void MetaStream::pause(ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->pause(std::move(handler));
}
void MetaStream::playPause(ResultHandler handler)
{
LOG(DEBUG, LOG_TAG) << "PlayPause\n";
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (active_stream_->getState() == ReaderState::kIdle)
play(handler);
else
active_stream_->playPause(std::move(handler));
}
void MetaStream::stop(ResultHandler handler)
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
active_stream_->stop(std::move(handler));
}
void MetaStream::play(ResultHandler handler)
{
LOG(DEBUG, LOG_TAG) << "Play\n";
std::lock_guard<std::recursive_mutex> lock(mutex_);
if ((active_stream_->getProperties().can_play) && (active_stream_->getProperties().playback_status != PlaybackStatus::kPlaying))
return active_stream_->play(std::move(handler));
for (const auto& stream : streams_)
{
if ((stream->getState() == ReaderState::kIdle) && (stream->getProperties().can_play))
{
return stream->play(std::move(handler));
}
}
// call play on the active stream to get the handler called
active_stream_->play(std::move(handler));
}
} // namespace streamreader
|