Add log tags

This commit is contained in:
badaix 2020-02-14 22:53:08 +01:00
parent 835c4864bd
commit 7119f0626c
3 changed files with 70 additions and 89 deletions

View file

@ -39,8 +39,8 @@ OboePlayer::OboePlayer(const PcmDevice& pcmDevice, std::shared_ptr<Stream> strea
if (env) if (env)
oboe::DefaultStreamValues::FramesPerBurst = cpt::stoi(env, oboe::DefaultStreamValues::FramesPerBurst); oboe::DefaultStreamValues::FramesPerBurst = cpt::stoi(env, oboe::DefaultStreamValues::FramesPerBurst);
LOG(INFO) << "DefaultStreamValues::SampleRate: " << oboe::DefaultStreamValues::SampleRate LOG(INFO, LOG_TAG) << "DefaultStreamValues::SampleRate: " << oboe::DefaultStreamValues::SampleRate
<< ", DefaultStreamValues::FramesPerBurst: " << oboe::DefaultStreamValues::FramesPerBurst << "\n"; << ", DefaultStreamValues::FramesPerBurst: " << oboe::DefaultStreamValues::FramesPerBurst << "\n";
// The builder set methods can be chained for convenience. // The builder set methods can be chained for convenience.
oboe::AudioStreamBuilder builder; oboe::AudioStreamBuilder builder;
@ -55,7 +55,7 @@ OboePlayer::OboePlayer(const PcmDevice& pcmDevice, std::shared_ptr<Stream> strea
//->setFramesPerCallback(2 * oboe::DefaultStreamValues::FramesPerBurst) //->setFramesPerCallback(2 * oboe::DefaultStreamValues::FramesPerBurst)
//->setFramesPerCallback(960) // 2*192) //->setFramesPerCallback(960) // 2*192)
->openManagedStream(out_stream_); ->openManagedStream(out_stream_);
LOG(INFO) << "BufferSizeInFrames: " << out_stream_->getBufferSizeInFrames() << ", FramesPerBurst: " << out_stream_->getFramesPerBurst() << "\n"; LOG(INFO, LOG_TAG) << "BufferSizeInFrames: " << out_stream_->getBufferSizeInFrames() << ", FramesPerBurst: " << out_stream_->getFramesPerBurst() << "\n";
if (result != oboe::Result::OK) if (result != oboe::Result::OK)
LOG(ERROR, LOG_TAG) << "Error building AudioStream: " << oboe::convertToText(result) << "\n"; LOG(ERROR, LOG_TAG) << "Error building AudioStream: " << oboe::convertToText(result) << "\n";
@ -77,6 +77,9 @@ OboePlayer::~OboePlayer()
auto result = out_stream_->stop(std::chrono::nanoseconds(100ms).count()); auto result = out_stream_->stop(std::chrono::nanoseconds(100ms).count());
if (result != oboe::Result::OK) if (result != oboe::Result::OK)
LOG(ERROR, LOG_TAG) << "Error in AudioStream::stop: " << oboe::convertToText(result) << "\n"; LOG(ERROR, LOG_TAG) << "Error in AudioStream::stop: " << oboe::convertToText(result) << "\n";
result = out_stream_->close();
if (result != oboe::Result::OK)
LOG(ERROR, LOG_TAG) << "Error in AudioStream::stop: " << oboe::convertToText(result) << "\n";
} }

View file

@ -26,6 +26,7 @@
using namespace std; using namespace std;
static constexpr auto LOG_TAG = "OpenSlPlayer";
static constexpr auto kPhaseInit = "Init"; static constexpr auto kPhaseInit = "Init";
static constexpr auto kPhaseStart = "Start"; static constexpr auto kPhaseStart = "Start";
@ -48,7 +49,6 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void* context)
} }
OpenslPlayer::OpenslPlayer(const PcmDevice& pcmDevice, std::shared_ptr<Stream> stream) OpenslPlayer::OpenslPlayer(const PcmDevice& pcmDevice, std::shared_ptr<Stream> stream)
: Player(pcmDevice, stream), engineObject(NULL), engineEngine(NULL), outputMixObject(NULL), bqPlayerObject(NULL), bqPlayerPlay(NULL), : Player(pcmDevice, stream), engineObject(NULL), engineEngine(NULL), outputMixObject(NULL), bqPlayerObject(NULL), bqPlayerPlay(NULL),
bqPlayerBufferQueue(NULL), bqPlayerVolume(NULL), curBuffer(0), ms_(50), buff_size(0), pubStream_(stream) bqPlayerBufferQueue(NULL), bqPlayerVolume(NULL), curBuffer(0), ms_(50), buff_size(0), pubStream_(stream)
@ -67,45 +67,17 @@ void OpenslPlayer::playerCallback(SLAndroidSimpleBufferQueueItf bq)
{ {
if (bq != bqPlayerBufferQueue) if (bq != bqPlayerBufferQueue)
{ {
LOG(ERROR) << "Wrong bq!\n"; LOG(ERROR, LOG_TAG) << "Wrong bq!\n";
return; return;
} }
/* static long lastTick = 0;
long now = chronos::getTickCount();
int diff = 0;
if (lastTick != 0)
{
diff = now - lastTick;
// LOG(ERROR) << "diff: " << diff << ", frames: " << player->frames_ / 44.1 << "\n";
// if (diff <= 50)
// {
// usleep(1000 * (50 - diff));
// diff = 50;
// }
}
lastTick = chronos::getTickCount();
*/
// size_t d = player->frames_ / 0.48d;
// LOG(ERROR) << "Delay: " << d << "\n";
// SLAndroidSimpleBufferQueueState state;
// (*bq)->GetState(bq, &state);
// cout << "bqPlayerCallback count: " << state.count << ", idx: " << state.index << "\n";
// diff = 0;
// chronos::usec delay((250 - diff) * 1000);
// while (active_ && !stream_->waitForChunk(100))
// LOG(INFO) << "Waiting for chunk\n";
if (!active_) if (!active_)
return; return;
chronos::usec delay(ms_ * 1000); chronos::usec delay(ms_ * 1000);
if (!pubStream_->getPlayerChunk(buffer[curBuffer], delay, frames_)) if (!pubStream_->getPlayerChunk(buffer[curBuffer], delay, frames_))
{ {
// LOG(INFO) << "Failed to get chunk. Playing silence.\n"; // LOG(INFO, LOG_TAG) << "Failed to get chunk. Playing silence.\n";
memset(buffer[curBuffer], 0, buff_size); memset(buffer[curBuffer], 0, buff_size);
} }
else else
@ -192,7 +164,7 @@ void OpenslPlayer::initOpensl()
frames_ = format.rate / (1000 / ms_); // * format.channels; // 1920; // 48000 * 2 / 50 // => 50ms frames_ = format.rate / (1000 / ms_); // * format.channels; // 1920; // 48000 * 2 / 50 // => 50ms
buff_size = frames_ * format.frameSize /* 2 -> sample size */; buff_size = frames_ * format.frameSize /* 2 -> sample size */;
LOG(INFO) << "frames: " << frames_ << ", channels: " << format.channels << ", rate: " << format.rate << ", buff: " << buff_size << "\n"; LOG(INFO, LOG_TAG) << "frames: " << frames_ << ", channels: " << format.channels << ", rate: " << format.rate << ", buff: " << buff_size << "\n";
SLresult result; SLresult result;
// create engine // create engine
@ -333,17 +305,17 @@ void OpenslPlayer::uninitOpensl()
// if (!active_) // if (!active_)
// return; // return;
LOG(INFO) << "uninitOpensl\n"; LOG(INFO, LOG_TAG) << "uninitOpensl\n";
SLresult result; SLresult result;
LOG(INFO) << "OpenSLWrap_Shutdown - stopping playback\n"; LOG(INFO, LOG_TAG) << "OpenSLWrap_Shutdown - stopping playback\n";
if (bqPlayerPlay != NULL) if (bqPlayerPlay != NULL)
{ {
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED); result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) if (SL_RESULT_SUCCESS != result)
LOG(ERROR) << "SetPlayState failed\n"; LOG(ERROR, LOG_TAG) << "SetPlayState failed\n";
} }
LOG(INFO) << "OpenSLWrap_Shutdown - deleting player object\n"; LOG(INFO, LOG_TAG) << "OpenSLWrap_Shutdown - deleting player object\n";
if (bqPlayerObject != NULL) if (bqPlayerObject != NULL)
{ {
@ -354,7 +326,7 @@ void OpenslPlayer::uninitOpensl()
bqPlayerVolume = NULL; bqPlayerVolume = NULL;
} }
LOG(INFO) << "OpenSLWrap_Shutdown - deleting mix object\n"; LOG(INFO, LOG_TAG) << "OpenSLWrap_Shutdown - deleting mix object\n";
if (outputMixObject != NULL) if (outputMixObject != NULL)
{ {
@ -362,7 +334,7 @@ void OpenslPlayer::uninitOpensl()
outputMixObject = NULL; outputMixObject = NULL;
} }
LOG(INFO) << "OpenSLWrap_Shutdown - deleting engine object\n"; LOG(INFO, LOG_TAG) << "OpenSLWrap_Shutdown - deleting engine object\n";
if (engineObject != NULL) if (engineObject != NULL)
{ {
@ -377,7 +349,7 @@ void OpenslPlayer::uninitOpensl()
delete[] buffer[1]; delete[] buffer[1];
buffer[1] = NULL; buffer[1] = NULL;
LOG(INFO) << "OpenSLWrap_Shutdown - finished\n"; LOG(INFO, LOG_TAG) << "OpenSLWrap_Shutdown - finished\n";
active_ = false; active_ = false;
} }

View file

@ -24,9 +24,11 @@
#include <string.h> #include <string.h>
using namespace std; using namespace std;
// using namespace chronos;
namespace cs = chronos; namespace cs = chronos;
static constexpr auto LOG_TAG = "Stream";
static constexpr auto kCorrectionBegin = 100us;
Stream::Stream(const SampleFormat& in_format, const SampleFormat& out_format) Stream::Stream(const SampleFormat& in_format, const SampleFormat& out_format)
: in_format_(in_format), sleep_(0), median_(0), shortMedian_(0), lastUpdate_(0), playedFrames_(0), bufferMs_(cs::msec(500)), soxr_(nullptr), frame_delta_(0) : in_format_(in_format), sleep_(0), median_(0), shortMedian_(0), lastUpdate_(0), playedFrames_(0), bufferMs_(cs::msec(500)), soxr_(nullptr), frame_delta_(0)
@ -34,7 +36,6 @@ Stream::Stream(const SampleFormat& in_format, const SampleFormat& out_format)
buffer_.setSize(500); buffer_.setSize(500);
shortBuffer_.setSize(100); shortBuffer_.setSize(100);
miniBuffer_.setSize(20); miniBuffer_.setSize(20);
// cardBuffer_.setSize(50);
if (out_format.rate != 0) if (out_format.rate != 0)
format_ = out_format; format_ = out_format;
@ -52,7 +53,7 @@ Stream::Stream(const SampleFormat& in_format, const SampleFormat& out_format)
if ((format_.rate != in_format_.rate) || (format_.bits != in_format_.bits)) if ((format_.rate != in_format_.rate) || (format_.bits != in_format_.bits))
{ {
LOG(INFO) << "Resampling from " << in_format_.getFormat() << " to " << format_.getFormat() << "\n"; LOG(INFO, LOG_TAG) << "Resampling from " << in_format_.getFormat() << " to " << format_.getFormat() << "\n";
soxr_error_t error; soxr_error_t error;
soxr_datatype_t in_type = SOXR_INT16_I; soxr_datatype_t in_type = SOXR_INT16_I;
@ -67,7 +68,7 @@ Stream::Stream(const SampleFormat& in_format, const SampleFormat& out_format)
soxr_ = soxr_create(static_cast<double>(in_format_.rate), static_cast<double>(format_.rate), format_.channels, &error, &iospec, &q_spec, NULL); soxr_ = soxr_create(static_cast<double>(in_format_.rate), static_cast<double>(format_.rate), format_.channels, &error, &iospec, &q_spec, NULL);
if (error) if (error)
{ {
LOG(ERROR) << "Error soxr_create: " << error << "\n"; LOG(ERROR, LOG_TAG) << "Error soxr_create: " << error << "\n";
} }
// initialize the buffer with 20ms (~latency of the reampler) // initialize the buffer with 20ms (~latency of the reampler)
resample_buffer_.resize(format_.frameSize * ceil(format_.msRate()) * 20); resample_buffer_.resize(format_.frameSize * ceil(format_.msRate()) * 20);
@ -88,7 +89,7 @@ void Stream::setRealSampleRate(double sampleRate)
correctAfterXFrames_ = 0; correctAfterXFrames_ = 0;
else else
correctAfterXFrames_ = round((format_.rate / sampleRate) / (format_.rate / sampleRate - 1.)); correctAfterXFrames_ = round((format_.rate / sampleRate) / (format_.rate / sampleRate - 1.));
// LOG(DEBUG) << "Correct after X: " << correctAfterXFrames_ << " (Real rate: " << sampleRate << ", rate: " << format_.rate << ")\n"; // LOG(DEBUG, LOG_TAG) << "Correct after X: " << correctAfterXFrames_ << " (Real rate: " << sampleRate << ", rate: " << format_.rate << ")\n";
} }
@ -112,7 +113,7 @@ void Stream::addChunk(unique_ptr<msg::PcmChunk> chunk)
chunks_.pop(); chunks_.pop();
// chunks_.push(move(chunk)); // chunks_.push(move(chunk));
// LOG(DEBUG) << "new chunk: " << chunk->duration<cs::msec>().count() << ", Chunks: " << chunks_.size() << "\n"; // LOG(DEBUG, LOG_TAG) << "new chunk: " << chunk->duration<cs::msec>().count() << ", Chunks: " << chunks_.size() << "\n";
if (soxr_ == nullptr) if (soxr_ == nullptr)
{ {
@ -120,9 +121,6 @@ void Stream::addChunk(unique_ptr<msg::PcmChunk> chunk)
} }
else else
{ {
size_t idone;
size_t odone;
if (in_format_.bits == 24) if (in_format_.bits == 24)
{ {
// sox expects 32 bit input, shift 8 bits left // sox expects 32 bit input, shift 8 bits left
@ -131,17 +129,19 @@ void Stream::addChunk(unique_ptr<msg::PcmChunk> chunk)
frames[n] = frames[n] << 8; frames[n] = frames[n] << 8;
} }
size_t idone;
size_t odone;
auto resample_buffer_framesize = resample_buffer_.size() / format_.frameSize; auto resample_buffer_framesize = resample_buffer_.size() / format_.frameSize;
auto error = soxr_process(soxr_, chunk->payload, chunk->getFrameCount(), &idone, resample_buffer_.data(), resample_buffer_framesize, &odone); auto error = soxr_process(soxr_, chunk->payload, chunk->getFrameCount(), &idone, resample_buffer_.data(), resample_buffer_framesize, &odone);
if (error) if (error)
{ {
LOG(ERROR) << "Error soxr_process: " << error << "\n"; LOG(ERROR, LOG_TAG) << "Error soxr_process: " << error << "\n";
// delete out; // delete out;
} }
else else
{ {
LOG(TRACE) << "Resample idone: " << idone << "/" << chunk->getFrameCount() << ", odone: " << odone << "/" LOG(TRACE, LOG_TAG) << "Resample idone: " << idone << "/" << chunk->getFrameCount() << ", odone: " << odone << "/"
<< resample_buffer_.size() / format_.frameSize << ", delay: " << soxr_delay(soxr_) << "\n"; << resample_buffer_.size() / format_.frameSize << ", delay: " << soxr_delay(soxr_) << "\n";
// some data has been resampled (odone frames) and some is still in the pipe (soxr_delay frames) // some data has been resampled (odone frames) and some is still in the pipe (soxr_delay frames)
if (odone > 0) if (odone > 0)
@ -180,12 +180,13 @@ void Stream::addChunk(unique_ptr<msg::PcmChunk> chunk)
{ {
// buffer for resampled data too small, add space for 5ms // buffer for resampled data too small, add space for 5ms
resample_buffer_.resize(resample_buffer_.size() + format_.frameSize * ceil(format_.msRate()) * 5); resample_buffer_.resize(resample_buffer_.size() + format_.frameSize * ceil(format_.msRate()) * 5);
LOG(INFO) << "Resample buffer completely filled, adding space for 5ms; new buffer size: " << resample_buffer_.size() << " bytes\n"; LOG(INFO, LOG_TAG) << "Resample buffer completely filled, adding space for 5ms; new buffer size: " << resample_buffer_.size() << " bytes\n";
} }
// //LOG(TRACE) << "ts: " << out->timestamp.sec << "s, " << out->timestamp.usec/1000.f << " ms, duration: " << odone / format_.msRate() << "\n"; // //LOG(TRACE, LOG_TAG) << "ts: " << out->timestamp.sec << "s, " << out->timestamp.usec/1000.f << " ms, duration: " << odone / format_.msRate()
// << "\n";
// int64_t next_us = us + static_cast<int64_t>(odone / format_.msRate() * 1000); // int64_t next_us = us + static_cast<int64_t>(odone / format_.msRate() * 1000);
// LOG(TRACE) << "ts: " << us << ", next: " << next_us << ", diff: " << next_us_ - us << "\n"; // LOG(TRACE, LOG_TAG) << "ts: " << us << ", next: " << next_us << ", diff: " << next_us_ - us << "\n";
// next_us_ = next_us; // next_us_ = next_us;
} }
} }
@ -199,7 +200,6 @@ bool Stream::waitForChunk(size_t ms) const
} }
cs::time_point_clk Stream::getSilentPlayerChunk(void* outputBuffer, unsigned long frames) cs::time_point_clk Stream::getSilentPlayerChunk(void* outputBuffer, unsigned long frames)
{ {
if (!chunk_) if (!chunk_)
@ -219,8 +219,8 @@ time_point_ms Stream::seekTo(const time_point_ms& to)
while (to > chunk_->timePoint())// + std::chrono::milliseconds((long int)chunk_->getDuration()))// while (to > chunk_->timePoint())// + std::chrono::milliseconds((long int)chunk_->getDuration()))//
{ {
chunk_ = chunks_.pop(); chunk_ = chunks_.pop();
LOG(DEBUG) << "\nto: " << Chunk::getAge(to) << "\t chunk: " << Chunk::getAge(chunk_->timePoint()) << "\n"; LOG(DEBUG, LOG_TAG) << "\nto: " << Chunk::getAge(to) << "\t chunk: " << Chunk::getAge(chunk_->timePoint()) << "\n";
LOG(DEBUG) << "diff: " << std::chrono::duration_cast<std::chrono::milliseconds>((to - chunk_->timePoint())).count() << "\n"; LOG(DEBUG, LOG_TAG) << "diff: " << std::chrono::duration_cast<std::chrono::milliseconds>((to - chunk_->timePoint())).count() << "\n";
} }
chunk_->seek(std::chrono::duration_cast<std::chrono::milliseconds>(to - chunk_->timePoint()).count() * format_.msRate()); chunk_->seek(std::chrono::duration_cast<std::chrono::milliseconds>(to - chunk_->timePoint()).count() * format_.msRate());
return chunk_->timePoint(); return chunk_->timePoint();
@ -296,7 +296,7 @@ cs::time_point_clk Stream::getNextPlayerChunk(void* outputBuffer, const cs::usec
// Size of each slice. The last slice may be bigger. // Size of each slice. The last slice may be bigger.
int size = max / slices; int size = max / slices;
// LOG(TRACE) << "getNextPlayerChunk, frames: " << frames << ", correction: " << framesCorrection << " (" << toRead << "), slices: " << slices // LOG(TRACE, LOG_TAG) << "getNextPlayerChunk, frames: " << frames << ", correction: " << framesCorrection << " (" << toRead << "), slices: " << slices
// << "\n"; // << "\n";
size_t pos = 0; size_t pos = 0;
@ -309,14 +309,16 @@ cs::time_point_clk Stream::getNextPlayerChunk(void* outputBuffer, const cs::usec
if (framesCorrection < 0) if (framesCorrection < 0)
{ {
// Read one frame less per slice from the input, but write a duplicated frame per slice to the output // Read one frame less per slice from the input, but write a duplicated frame per slice to the output
// LOG(TRACE) << "duplicate - requested: " << frames << ", read: " << toRead << ", slice: " << n << ", size: " << size << ", out pos: " << pos << ", // LOG(TRACE, LOG_TAG) << "duplicate - requested: " << frames << ", read: " << toRead << ", slice: " << n << ", size: " << size << ", out pos: " <<
// pos << ",
// source pos: " << pos - n << "\n"; // source pos: " << pos - n << "\n";
memcpy(static_cast<char*>(outputBuffer) + pos * format_.frameSize, buffer + (pos - n) * format_.frameSize, size * format_.frameSize); memcpy(static_cast<char*>(outputBuffer) + pos * format_.frameSize, buffer + (pos - n) * format_.frameSize, size * format_.frameSize);
} }
else else
{ {
// Read all input frames, but skip a frame per slice when writing to the output. // Read all input frames, but skip a frame per slice when writing to the output.
// LOG(TRACE) << "remove - requested: " << frames << ", read: " << toRead << ", slice: " << n << ", size: " << size << ", out pos: " << pos - n << // LOG(TRACE, LOG_TAG) << "remove - requested: " << frames << ", read: " << toRead << ", slice: " << n << ", size: " << size << ", out pos: " << pos
// - n <<
// ", source pos: " << pos << "\n"; // ", source pos: " << pos << "\n";
memcpy(static_cast<char*>(outputBuffer) + (pos - n) * format_.frameSize, buffer + pos * format_.frameSize, size * format_.frameSize); memcpy(static_cast<char*>(outputBuffer) + (pos - n) * format_.frameSize, buffer + pos * format_.frameSize, size * format_.frameSize);
} }
@ -376,14 +378,15 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
{ {
if (outputBufferDacTime > bufferMs_) if (outputBufferDacTime > bufferMs_)
{ {
LOG(INFO) << "outputBufferDacTime > bufferMs: " << cs::duration<cs::msec>(outputBufferDacTime) << " > " << cs::duration<cs::msec>(bufferMs_) << "\n"; LOG(INFO, LOG_TAG) << "outputBufferDacTime > bufferMs: " << cs::duration<cs::msec>(outputBufferDacTime) << " > " << cs::duration<cs::msec>(bufferMs_)
<< "\n";
sleep_ = cs::usec(0); sleep_ = cs::usec(0);
return false; return false;
} }
if (!chunk_ && !chunks_.try_pop(chunk_, outputBufferDacTime)) if (!chunk_ && !chunks_.try_pop(chunk_, outputBufferDacTime))
{ {
// LOG(INFO) << "no chunks available\n"; // LOG(INFO, LOG_TAG) << "no chunks available\n";
sleep_ = cs::usec(0); sleep_ = cs::usec(0);
return false; return false;
} }
@ -396,19 +399,19 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
/// age < 0 => play in -age /// age < 0 => play in -age
/// age > 0 => too old /// age > 0 => too old
cs::usec age = std::chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - chunk_->start()) - bufferMs_ + outputBufferDacTime; cs::usec age = std::chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - chunk_->start()) - bufferMs_ + outputBufferDacTime;
// LOG(INFO) << "age: " << age.count() / 1000 << "\n"; // LOG(INFO, LOG_TAG) << "age: " << age.count() / 1000 << "\n";
if ((sleep_.count() == 0) && (cs::abs(age) > cs::msec(200))) if ((sleep_.count() == 0) && (cs::abs(age) > cs::msec(200)))
{ {
LOG(INFO) << "age > 200: " << cs::duration<cs::msec>(age) << "\n"; LOG(INFO, LOG_TAG) << "age > 200: " << cs::duration<cs::msec>(age) << "\n";
sleep_ = age; sleep_ = age;
} }
try try
{ {
// LOG(DEBUG) << "frames: " << frames << "\tms: " << frames*2 / PLAYER_CHUNK_MS_SIZE << "\t" << PLAYER_CHUNK_SIZE << "\n"; // LOG(DEBUG, LOG_TAG) << "frames: " << frames << "\tms: " << frames*2 / PLAYER_CHUNK_MS_SIZE << "\t" << PLAYER_CHUNK_SIZE << "\n";
cs::nsec bufferDuration = cs::nsec(static_cast<cs::nsec::rep>(frames / format_.nsRate())); cs::nsec bufferDuration = cs::nsec(static_cast<cs::nsec::rep>(frames / format_.nsRate()));
// LOG(DEBUG) << "buffer duration: " << bufferDuration.count() << "\n"; // LOG(DEBUG, LOG_TAG) << "buffer duration: " << bufferDuration.count() << "\n";
cs::usec correction = cs::usec(0); cs::usec correction = cs::usec(0);
if (sleep_.count() != 0) if (sleep_.count() != 0)
@ -416,27 +419,29 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
resetBuffers(); resetBuffers();
if (sleep_ < -bufferDuration / 2) if (sleep_ < -bufferDuration / 2)
{ {
LOG(INFO) << "sleep < -bufferDuration/2: " << cs::duration<cs::msec>(sleep_) << " < " << -cs::duration<cs::msec>(bufferDuration) / 2 << ", "; LOG(INFO, LOG_TAG) << "sleep < -bufferDuration/2: " << cs::duration<cs::msec>(sleep_) << " < " << -cs::duration<cs::msec>(bufferDuration) / 2
<< ", ";
// We're early: not enough chunks_. play silence. Reference chunk_ is the oldest (front) one // We're early: not enough chunks_. play silence. Reference chunk_ is the oldest (front) one
sleep_ = sleep_ =
chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - getSilentPlayerChunk(outputBuffer, frames) - bufferMs_ + outputBufferDacTime); chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - getSilentPlayerChunk(outputBuffer, frames) - bufferMs_ + outputBufferDacTime);
LOG(INFO) << "sleep: " << cs::duration<cs::msec>(sleep_) << "\n"; LOG(INFO, LOG_TAG) << "sleep: " << cs::duration<cs::msec>(sleep_) << "\n";
if (sleep_ < -bufferDuration / 2) if (sleep_ < -bufferDuration / 2)
return true; return true;
} }
else if (sleep_ > bufferDuration / 2) else if (sleep_ > bufferDuration / 2)
{ {
LOG(INFO) << "sleep > bufferDuration/2: " << cs::duration<cs::msec>(sleep_) << " > " << cs::duration<cs::msec>(bufferDuration) / 2 << "\n"; LOG(INFO, LOG_TAG) << "sleep > bufferDuration/2: " << cs::duration<cs::msec>(sleep_) << " > " << cs::duration<cs::msec>(bufferDuration) / 2
<< "\n";
// We're late: discard oldest chunks // We're late: discard oldest chunks
while (sleep_ > chunk_->duration<cs::usec>()) while (sleep_ > chunk_->duration<cs::usec>())
{ {
LOG(INFO) << "sleep > chunkDuration: " << cs::duration<cs::msec>(sleep_) << " > " << chunk_->duration<cs::msec>().count() LOG(INFO, LOG_TAG) << "sleep > chunkDuration: " << cs::duration<cs::msec>(sleep_) << " > " << chunk_->duration<cs::msec>().count()
<< ", chunks: " << chunks_.size() << ", out: " << cs::duration<cs::msec>(outputBufferDacTime) << ", chunks: " << chunks_.size() << ", out: " << cs::duration<cs::msec>(outputBufferDacTime)
<< ", needed: " << cs::duration<cs::msec>(bufferDuration) << "\n"; << ", needed: " << cs::duration<cs::msec>(bufferDuration) << "\n";
sleep_ = std::chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - chunk_->start() - bufferMs_ + outputBufferDacTime); sleep_ = std::chrono::duration_cast<cs::usec>(TimeProvider::serverNow() - chunk_->start() - bufferMs_ + outputBufferDacTime);
if (!chunks_.try_pop(chunk_, outputBufferDacTime)) if (!chunks_.try_pop(chunk_, outputBufferDacTime))
{ {
LOG(INFO) << "no chunks available\n"; LOG(INFO, LOG_TAG) << "no chunks available\n";
chunk_ = nullptr; chunk_ = nullptr;
sleep_ = cs::usec(0); sleep_ = cs::usec(0);
return false; return false;
@ -457,7 +462,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
} }
else else
{ {
LOG(INFO) << "Sleep " << cs::duration<cs::msec>(sleep_) << "\n"; LOG(INFO, LOG_TAG) << "Sleep " << cs::duration<cs::msec>(sleep_) << "\n";
correction = sleep_; correction = sleep_;
sleep_ = cs::usec(0); sleep_ = cs::usec(0);
} }
@ -483,7 +488,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
{ {
if (cs::usec(abs(median_)) > cs::msec(2)) if (cs::usec(abs(median_)) > cs::msec(2))
{ {
LOG(INFO) << "pBuffer->full() && (abs(median_) > 2): " << median_ << "\n"; LOG(INFO, LOG_TAG) << "pBuffer->full() && (abs(median_) > 2): " << median_ << "\n";
sleep_ = cs::usec(median_); sleep_ = cs::usec(median_);
} }
// else if (cs::usec(median_) > cs::usec(300)) // else if (cs::usec(median_) > cs::usec(300))
@ -499,7 +504,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
{ {
if (cs::usec(abs(shortMedian_)) > cs::msec(5)) if (cs::usec(abs(shortMedian_)) > cs::msec(5))
{ {
LOG(INFO) << "pShortBuffer->full() && (abs(shortMedian_) > 5): " << shortMedian_ << "\n"; LOG(INFO, LOG_TAG) << "pShortBuffer->full() && (abs(shortMedian_) > 5): " << shortMedian_ << "\n";
sleep_ = cs::usec(shortMedian_); sleep_ = cs::usec(shortMedian_);
} }
// else // else
@ -509,7 +514,7 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
} }
else if (miniBuffer_.full() && (cs::usec(abs(miniBuffer_.median())) > cs::msec(50))) else if (miniBuffer_.full() && (cs::usec(abs(miniBuffer_.median())) > cs::msec(50)))
{ {
LOG(INFO) << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << miniBuffer_.median() << "\n"; LOG(INFO, LOG_TAG) << "pMiniBuffer->full() && (abs(pMiniBuffer->mean()) > 50): " << miniBuffer_.median() << "\n";
sleep_ = cs::usec(static_cast<cs::msec::rep>(miniBuffer_.mean())); sleep_ = cs::usec(static_cast<cs::msec::rep>(miniBuffer_.mean()));
} }
} }
@ -521,25 +526,25 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
if (lastAge != msAge) if (lastAge != msAge)
{ {
lastAge = msAge; lastAge = msAge;
LOG(INFO) << "Sleep " << cs::duration<cs::msec>(sleep_) << ", age: " << msAge << ", bufferDuration: " << cs::duration<cs::msec>(bufferDuration) LOG(INFO, LOG_TAG) << "Sleep " << cs::duration<cs::msec>(sleep_) << ", age: " << msAge
<< "\n"; << ", bufferDuration: " << cs::duration<cs::msec>(bufferDuration) << "\n";
} }
} }
else if (shortBuffer_.full()) else if (shortBuffer_.full())
{ {
auto miniMedian = miniBuffer_.median(); auto miniMedian = miniBuffer_.median();
if ((cs::usec(shortMedian_) > cs::usec(100)) && (cs::usec(miniMedian) > cs::usec(50)) && (cs::usec(age) > cs::usec(50))) if ((cs::usec(shortMedian_) > kCorrectionBegin) && (cs::usec(miniMedian) > cs::usec(50)) && (cs::usec(age) > cs::usec(50)))
{ {
double rate = (shortMedian_ / 100) * 0.00005; double rate = (shortMedian_ / 100) * 0.00005;
rate = 1.0 - std::min(rate, 0.0005); rate = 1.0 - std::min(rate, 0.0005);
// LOG(INFO) << "Rate: " << rate << "\n"; // LOG(INFO, LOG_TAG) << "Rate: " << rate << "\n";
setRealSampleRate(format_.rate * rate); // 0.9999); setRealSampleRate(format_.rate * rate); // 0.9999);
} }
else if ((cs::usec(shortMedian_) < -cs::usec(100)) && (cs::usec(miniMedian) < -cs::usec(50)) && (cs::usec(age) < -cs::usec(50))) else if ((cs::usec(shortMedian_) < -kCorrectionBegin) && (cs::usec(miniMedian) < -cs::usec(50)) && (cs::usec(age) < -cs::usec(50)))
{ {
double rate = (-shortMedian_ / 100) * 0.00005; double rate = (-shortMedian_ / 100) * 0.00005;
rate = 1.0 + std::min(rate, 0.0005); rate = 1.0 + std::min(rate, 0.0005);
// LOG(INFO) << "Rate: " << rate << "\n"; // LOG(INFO, LOG_TAG) << "Rate: " << rate << "\n";
setRealSampleRate(format_.rate * rate); // 1.0001); setRealSampleRate(format_.rate * rate); // 1.0001);
} }
} }
@ -553,9 +558,10 @@ bool Stream::getPlayerChunk(void* outputBuffer, const cs::usec& outputBufferDacT
lastUpdate_ = now; lastUpdate_ = now;
median_ = buffer_.median(); median_ = buffer_.median();
shortMedian_ = shortBuffer_.median(); shortMedian_ = shortBuffer_.median();
LOG(INFO) << "Chunk: " << age.count() / 100 << "\t" << miniBuffer_.median() / 100 << "\t" << shortMedian_ / 100 << "\t" << median_ / 100 << "\t" LOG(INFO, LOG_TAG) << "Chunk: " << age.count() / 100 << "\t" << miniBuffer_.median() / 100 << "\t" << shortMedian_ / 100 << "\t" << median_ / 100
<< buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\t" << frame_delta_ << "\n"; << "\t" << buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\t" << frame_delta_ << "\n";
// LOG(INFO) << "Chunk: " << age.count()/1000 << "\t" << miniBuffer_.median()/1000 << "\t" << shortMedian_/1000 << "\t" << median_/1000 << "\t" << // LOG(INFO, LOG_TAG) << "Chunk: " << age.count()/1000 << "\t" << miniBuffer_.median()/1000 << "\t" << shortMedian_/1000 << "\t" << median_/1000 <<
// "\t" <<
// buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n"; // buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n";
frame_delta_ = 0; frame_delta_ = 0;
} }