From 3d38302e2f3d5c939953b0e9e465b7c57b1bfd62 Mon Sep 17 00:00:00 2001 From: study-faraphel Date: Sat, 9 Nov 2024 14:19:26 +0100 Subject: [PATCH] fixed audio not being received properly. --- source/EventManager.cpp | 2 +- source/events/audio/AudioEvent.cpp | 6 ++++-- source/events/info/InfoEvent.cpp | 2 +- source/tasks/server/ServerTask.cpp | 22 ++++++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/source/EventManager.cpp b/source/EventManager.cpp index b8f096f..a1a3ce1 100644 --- a/source/EventManager.cpp +++ b/source/EventManager.cpp @@ -120,7 +120,7 @@ void EventManager::loopSender() { try { task = this->taskRegistry.at(this->context.me.status); } catch (const std::out_of_range& exception) { - std::cerr << "Unsupported status." << std::endl; + std::cerr << "[Sender] Unsupported status." << std::endl; continue; } diff --git a/source/events/audio/AudioEvent.cpp b/source/events/audio/AudioEvent.cpp index ed88304..e81f063 100644 --- a/source/events/audio/AudioEvent.cpp +++ b/source/events/audio/AudioEvent.cpp @@ -38,6 +38,7 @@ void AudioEvent::handle( // get the audio data in the content packet::AudioPacketData audioData; std::memcpy(&audioData, content.data.data(), sizeof(packet::AudioPacketData)); + // save it in the audio queue this->audioQueue.push(audioData); // notify that a new audio chunk is available @@ -119,10 +120,11 @@ void AudioEvent::loopPlay() { switch (error) { // success case paNoError: - // the output might be very slightly underflown, + break; + // the output might be very slightly underflowed, // causing a very small period where no noise will be played. case paOutputUnderflowed: - break; + std::cerr << "[Client] Underflowed!" << std::endl; default: std::cerr << "[Client] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl; diff --git a/source/events/info/InfoEvent.cpp b/source/events/info/InfoEvent.cpp index 2a4de06..7d81183 100644 --- a/source/events/info/InfoEvent.cpp +++ b/source/events/info/InfoEvent.cpp @@ -15,7 +15,7 @@ void InfoEvent::handle( ) { // get the peer information Peer peer; - std::memcpy(&peer, &content, sizeof(Peer)); + std::memcpy(&peer, content.data.data(), sizeof(Peer)); // check if the peer address is already in the map const auto iterator = context.remotePeers.find(peer.id); diff --git a/source/tasks/server/ServerTask.cpp b/source/tasks/server/ServerTask.cpp index 491a935..0e07ff3 100644 --- a/source/tasks/server/ServerTask.cpp +++ b/source/tasks/server/ServerTask.cpp @@ -10,7 +10,9 @@ #include #include +#include "../../events/types.hpp" #include "../../packets/audio/AudioPacketData.hpp" +#include "../../packets/base/GenericPacket.hpp" #include "../../utils/audio.hpp" @@ -56,9 +58,16 @@ void ServerTask::handle(Context& context) { // TODO(Faraphel): create a chrony server // read the file + packet::GenericPacket packet {}; + packet::GenericPacketContent packetContent {}; packet::AudioPacketData audioPacket; std::size_t done; + // create a packet + // TODO(Faraphel): should not be broadcast plaintext + packet.channel = 0; + packet.securityMode = static_cast(packet::SecurityMode::PLAIN); + if (mpg123_read( this->mpgHandle, &audioPacket.content, @@ -76,19 +85,24 @@ void ServerTask::handle(Context& context) { std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(5000); + // set the size of the content + audioPacket.contentSize = done; + // set the audio settings audioPacket.channels = this->channels; audioPacket.sampleFormat = util::encoding_mpg123_to_PulseAudio(this->encoding); audioPacket.sampleRate = this->sampleRate; - // set the size of the content - audioPacket.contentSize = done; + // wrap the audio packet in the content + packetContent.eventType = static_cast(event::EventType::AUDIO); + std::memcpy(packetContent.data.data(), &audioPacket, packetContent.data.size()); + packet.setContent(packetContent); // broadcast the audio data if (sendto( context.socket, - &audioPacket, - sizeof(audioPacket), + &packet, + sizeof(packet), 0, context.broadcastAddressInfo->ai_addr, context.broadcastAddressInfo->ai_addrlen