Fix timestamp for first chunk

This commit is contained in:
badaix 2019-12-29 21:07:04 +01:00
parent 3b9c2db79b
commit 8429c82091

View file

@ -19,6 +19,7 @@
#ifndef ASIO_STREAM_HPP #ifndef ASIO_STREAM_HPP
#define ASIO_STREAM_HPP #define ASIO_STREAM_HPP
#include "common/aixlog.hpp"
#include "pcm_stream.hpp" #include "pcm_stream.hpp"
#include <atomic> #include <atomic>
#include <boost/asio.hpp> #include <boost/asio.hpp>
@ -41,6 +42,7 @@ protected:
virtual void on_connect(); virtual void on_connect();
virtual void do_read(); virtual void do_read();
void check_state(); void check_state();
std::unique_ptr<msg::PcmChunk> chunk_; std::unique_ptr<msg::PcmChunk> chunk_;
timeval tv_chunk_; timeval tv_chunk_;
bool first_; bool first_;
@ -61,6 +63,7 @@ AsioStream<ReadStream>::AsioStream(PcmListener* pcmListener, boost::asio::io_con
chunk_ = std::make_unique<msg::PcmChunk>(sampleFormat_, chunk_ms_); chunk_ = std::make_unique<msg::PcmChunk>(sampleFormat_, chunk_ms_);
bytes_read_ = 0; bytes_read_ = 0;
buffer_ms_ = 50; buffer_ms_ = 50;
try try
{ {
buffer_ms_ = cpt::stoi(uri_.getQuery("buffer_ms", cpt::to_string(buffer_ms_))); buffer_ms_ = cpt::stoi(uri_.getQuery("buffer_ms", cpt::to_string(buffer_ms_)));
@ -115,6 +118,7 @@ template <typename ReadStream>
void AsioStream<ReadStream>::on_connect() void AsioStream<ReadStream>::on_connect()
{ {
first_ = true; first_ = true;
chronos::systemtimeofday(&tvEncodedChunk_);
do_read(); do_read();
} }
@ -139,6 +143,8 @@ void AsioStream<ReadStream>::do_read()
// the timestamp will be incremented after encoding, // the timestamp will be incremented after encoding,
// since we do not know how much the encoder actually encoded // since we do not know how much the encoder actually encoded
if (!first_)
{
timeval now; timeval now;
chronos::systemtimeofday(&now); chronos::systemtimeofday(&now);
auto stream2systime_diff = chronos::diff<std::chrono::milliseconds>(now, tvEncodedChunk_); auto stream2systime_diff = chronos::diff<std::chrono::milliseconds>(now, tvEncodedChunk_);
@ -147,12 +153,14 @@ void AsioStream<ReadStream>::do_read()
LOG(WARNING) << "Stream and system time out of sync: " << stream2systime_diff.count() << "ms, resetting stream time.\n"; LOG(WARNING) << "Stream and system time out of sync: " << stream2systime_diff.count() << "ms, resetting stream time.\n";
first_ = true; first_ = true;
} }
}
if (first_) if (first_)
{ {
first_ = false; first_ = false;
chronos::systemtimeofday(&tvEncodedChunk_); chronos::systemtimeofday(&tvEncodedChunk_);
nextTick_ = chronos::getTickCount() + buffer_ms_; nextTick_ = chronos::getTickCount() + buffer_ms_;
} }
encoder_->encode(chunk_.get()); encoder_->encode(chunk_.get());
nextTick_ += chunk_ms_; nextTick_ += chunk_ms_;
long currentTick = chronos::getTickCount(); long currentTick = chronos::getTickCount();