improved latency before playing an audio

prevented playing packets that are too old to avoid offset
This commit is contained in:
study-faraphel 2024-11-14 13:24:49 +01:00
parent 9646c2d09b
commit 11e4861082
8 changed files with 57 additions and 12 deletions

View file

@ -57,6 +57,8 @@ add_executable(M2-PT-DRP
source/packets/base/SecurityMode.hpp source/packets/base/SecurityMode.hpp
source/packets/base/PacketData.hpp source/packets/base/PacketData.hpp
source/packets/info/InfoPacketData.hpp source/packets/info/InfoPacketData.hpp
source/utils/time/Chrony.cpp
source/utils/time/Chrony.hpp
) )
target_include_directories(M2-PT-DRP PRIVATE target_include_directories(M2-PT-DRP PRIVATE
source source

View file

@ -2,6 +2,9 @@
Debian Debian
```bash ```bash
# Pre-requires
sudo apt install chrony
# Download # Download
sudo apt install -y git sudo apt install -y git
git clone https://git.faraphel.fr/study-faraphel/M2-PT-DRP git clone https://git.faraphel.fr/study-faraphel/M2-PT-DRP
@ -17,6 +20,6 @@ cmake --build build
cd build cd build
# Run # Run
./M2-PT-DRP --host ff02::1 --ipv6 sudo ./M2-PT-DRP --host ff02::1 --ipv6
``` ```

View file

@ -5,6 +5,7 @@
#include "packets/audio/AudioPacketData.hpp" #include "packets/audio/AudioPacketData.hpp"
#include "packets/base/PacketData.hpp" #include "packets/base/PacketData.hpp"
#include "utils/audio.hpp"
namespace drp::event { namespace drp::event {
@ -90,6 +91,12 @@ void AudioEvent::loopPlay() {
// get the most recent audio chunk // get the most recent audio chunk
const auto audioPacket = this->audioQueue.top(); const auto audioPacket = this->audioQueue.top();
// if the packet should have started playing before, skip it
if (audioPacket.timePlay < std::chrono::high_resolution_clock::now()) {
this->audioQueue.pop();
continue;
}
// update the stream with the new audio settings // update the stream with the new audio settings
this->updateAudioStream( this->updateAudioStream(
audioPacket.channels, audioPacket.channels,

View file

@ -57,10 +57,8 @@ ServerTask::~ServerTask() {
void ServerTask::handle(Context& context) { void ServerTask::handle(Context& context) {
// TODO(Faraphel): create a chrony server // TODO(Faraphel): create a chrony server
// allow anybody to connect as a chrony client // get the time of the start of the processing
FILE* chronyProcess = popen("chronyc allow all 2>&1", "r"); const auto startProcessingTime = std::chrono::high_resolution_clock::now();
if (pclose(chronyProcess) == -1)
std::cerr << "Could not allow chrony connection." << std::endl;
// prepare the packet structure // prepare the packet structure
packet::base::Packet packet {}; packet::base::Packet packet {};
@ -116,12 +114,13 @@ void ServerTask::handle(Context& context) {
std::cout << "[Task - Server] Sent: " << done << " bytes" << std::endl; std::cout << "[Task - Server] Sent: " << done << " bytes" << std::endl;
// wait for the duration of the audio chunk // wait for the duration of the audio chunk, since the start of the processing
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<std::uint64_t>( std::this_thread::sleep_until(startProcessingTime + util::get_audio_chunk_duration(
(1 / static_cast<double>(this->sampleRate * this->channels * mpg123_encsize(this->encoding))) * this->sampleRate,
1000 * this->channels,
static_cast<double>(done) mpg123_encsize(this->encoding),
))); done
));
} }

View file

@ -1,5 +1,6 @@
#include "audio.hpp" #include "audio.hpp"
#include <chrono>
#include <stdexcept> #include <stdexcept>
#include <fmt123.h> #include <fmt123.h>
@ -30,4 +31,17 @@ std::uint32_t encoding_mpg123_to_PulseAudio(const int encoding_mpg123) {
} }
std::chrono::milliseconds get_audio_chunk_duration(
const long sampleRate,
const int channels,
const int encodingSize,
const std::size_t length
) {
return std::chrono::milliseconds(static_cast<std::uint64_t>(
(1 / static_cast<double>(sampleRate * channels * encodingSize)) *
1000 * static_cast<double>(length)
));
}
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <chrono>
#include <cstdint> #include <cstdint>
@ -8,5 +9,11 @@ namespace drp::util {
std::uint32_t encoding_mpg123_to_PulseAudio(int encoding_mpg123); std::uint32_t encoding_mpg123_to_PulseAudio(int encoding_mpg123);
std::chrono::milliseconds get_audio_chunk_duration(
long sampleRate,
int channels,
int encodingSize,
std::size_t length
);
} }

View file

@ -0,0 +1 @@
#include "Chrony.hpp"

View file

@ -0,0 +1,12 @@
#pragma once
namespace drp::util::time {
class Chrony {
};
}