improved latency before playing an audio
prevented playing packets that are too old to avoid offset
This commit is contained in:
parent
9646c2d09b
commit
11e4861082
8 changed files with 57 additions and 12 deletions
|
@ -57,6 +57,8 @@ add_executable(M2-PT-DRP
|
|||
source/packets/base/SecurityMode.hpp
|
||||
source/packets/base/PacketData.hpp
|
||||
source/packets/info/InfoPacketData.hpp
|
||||
source/utils/time/Chrony.cpp
|
||||
source/utils/time/Chrony.hpp
|
||||
)
|
||||
target_include_directories(M2-PT-DRP PRIVATE
|
||||
source
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
Debian
|
||||
```bash
|
||||
# Pre-requires
|
||||
sudo apt install chrony
|
||||
|
||||
# Download
|
||||
sudo apt install -y git
|
||||
git clone https://git.faraphel.fr/study-faraphel/M2-PT-DRP
|
||||
|
@ -17,6 +20,6 @@ cmake --build build
|
|||
cd build
|
||||
|
||||
# Run
|
||||
./M2-PT-DRP --host ff02::1 --ipv6
|
||||
sudo ./M2-PT-DRP --host ff02::1 --ipv6
|
||||
```
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "packets/audio/AudioPacketData.hpp"
|
||||
#include "packets/base/PacketData.hpp"
|
||||
#include "utils/audio.hpp"
|
||||
|
||||
|
||||
namespace drp::event {
|
||||
|
@ -90,6 +91,12 @@ void AudioEvent::loopPlay() {
|
|||
// get the most recent audio chunk
|
||||
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
|
||||
this->updateAudioStream(
|
||||
audioPacket.channels,
|
||||
|
@ -137,4 +144,4 @@ void AudioEvent::loopPlay() {
|
|||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,10 +57,8 @@ ServerTask::~ServerTask() {
|
|||
void ServerTask::handle(Context& context) {
|
||||
// TODO(Faraphel): create a chrony server
|
||||
|
||||
// allow anybody to connect as a chrony client
|
||||
FILE* chronyProcess = popen("chronyc allow all 2>&1", "r");
|
||||
if (pclose(chronyProcess) == -1)
|
||||
std::cerr << "Could not allow chrony connection." << std::endl;
|
||||
// get the time of the start of the processing
|
||||
const auto startProcessingTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// prepare the packet structure
|
||||
packet::base::Packet packet {};
|
||||
|
@ -116,12 +114,13 @@ void ServerTask::handle(Context& context) {
|
|||
|
||||
std::cout << "[Task - Server] Sent: " << done << " bytes" << std::endl;
|
||||
|
||||
// wait for the duration of the audio chunk
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<std::uint64_t>(
|
||||
(1 / static_cast<double>(this->sampleRate * this->channels * mpg123_encsize(this->encoding))) *
|
||||
1000 *
|
||||
static_cast<double>(done)
|
||||
)));
|
||||
// wait for the duration of the audio chunk, since the start of the processing
|
||||
std::this_thread::sleep_until(startProcessingTime + util::get_audio_chunk_duration(
|
||||
this->sampleRate,
|
||||
this->channels,
|
||||
mpg123_encsize(this->encoding),
|
||||
done
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "audio.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <stdexcept>
|
||||
|
||||
#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)
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
|
@ -8,5 +9,11 @@ namespace drp::util {
|
|||
|
||||
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
|
||||
);
|
||||
|
||||
}
|
1
source/utils/time/Chrony.cpp
Normal file
1
source/utils/time/Chrony.cpp
Normal file
|
@ -0,0 +1 @@
|
|||
#include "Chrony.hpp"
|
12
source/utils/time/Chrony.hpp
Normal file
12
source/utils/time/Chrony.hpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
namespace drp::util::time {
|
||||
|
||||
|
||||
class Chrony {
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue