fixed audio not being received properly.

This commit is contained in:
study-faraphel 2024-11-09 14:19:26 +01:00
parent 724cad320f
commit 3d38302e2f
4 changed files with 24 additions and 8 deletions

View file

@ -120,7 +120,7 @@ void EventManager::loopSender() {
try { try {
task = this->taskRegistry.at(this->context.me.status); task = this->taskRegistry.at(this->context.me.status);
} catch (const std::out_of_range& exception) { } catch (const std::out_of_range& exception) {
std::cerr << "Unsupported status." << std::endl; std::cerr << "[Sender] Unsupported status." << std::endl;
continue; continue;
} }

View file

@ -38,6 +38,7 @@ void AudioEvent::handle(
// get the audio data in the content // get the audio data in the content
packet::AudioPacketData audioData; packet::AudioPacketData audioData;
std::memcpy(&audioData, content.data.data(), sizeof(packet::AudioPacketData)); std::memcpy(&audioData, content.data.data(), sizeof(packet::AudioPacketData));
// save it in the audio queue // save it in the audio queue
this->audioQueue.push(audioData); this->audioQueue.push(audioData);
// notify that a new audio chunk is available // notify that a new audio chunk is available
@ -119,10 +120,11 @@ void AudioEvent::loopPlay() {
switch (error) { switch (error) {
// success // success
case paNoError: 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. // causing a very small period where no noise will be played.
case paOutputUnderflowed: case paOutputUnderflowed:
break; std::cerr << "[Client] Underflowed!" << std::endl;
default: default:
std::cerr << "[Client] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl; std::cerr << "[Client] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl;

View file

@ -15,7 +15,7 @@ void InfoEvent::handle(
) { ) {
// get the peer information // get the peer information
Peer peer; 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 // check if the peer address is already in the map
const auto iterator = context.remotePeers.find(peer.id); const auto iterator = context.remotePeers.find(peer.id);

View file

@ -10,7 +10,9 @@
#include <thread> #include <thread>
#include <sys/socket.h> #include <sys/socket.h>
#include "../../events/types.hpp"
#include "../../packets/audio/AudioPacketData.hpp" #include "../../packets/audio/AudioPacketData.hpp"
#include "../../packets/base/GenericPacket.hpp"
#include "../../utils/audio.hpp" #include "../../utils/audio.hpp"
@ -56,9 +58,16 @@ void ServerTask::handle(Context& context) {
// TODO(Faraphel): create a chrony server // TODO(Faraphel): create a chrony server
// read the file // read the file
packet::GenericPacket packet {};
packet::GenericPacketContent packetContent {};
packet::AudioPacketData audioPacket; packet::AudioPacketData audioPacket;
std::size_t done; std::size_t done;
// create a packet
// TODO(Faraphel): should not be broadcast plaintext
packet.channel = 0;
packet.securityMode = static_cast<std::uint8_t>(packet::SecurityMode::PLAIN);
if (mpg123_read( if (mpg123_read(
this->mpgHandle, this->mpgHandle,
&audioPacket.content, &audioPacket.content,
@ -76,19 +85,24 @@ void ServerTask::handle(Context& context) {
std::chrono::high_resolution_clock::now() + std::chrono::high_resolution_clock::now() +
std::chrono::milliseconds(5000); std::chrono::milliseconds(5000);
// set the size of the content
audioPacket.contentSize = done;
// set the audio settings // set the audio settings
audioPacket.channels = this->channels; audioPacket.channels = this->channels;
audioPacket.sampleFormat = util::encoding_mpg123_to_PulseAudio(this->encoding); audioPacket.sampleFormat = util::encoding_mpg123_to_PulseAudio(this->encoding);
audioPacket.sampleRate = this->sampleRate; audioPacket.sampleRate = this->sampleRate;
// set the size of the content // wrap the audio packet in the content
audioPacket.contentSize = done; packetContent.eventType = static_cast<std::uint8_t>(event::EventType::AUDIO);
std::memcpy(packetContent.data.data(), &audioPacket, packetContent.data.size());
packet.setContent(packetContent);
// broadcast the audio data // broadcast the audio data
if (sendto( if (sendto(
context.socket, context.socket,
&audioPacket, &packet,
sizeof(audioPacket), sizeof(packet),
0, 0,
context.broadcastAddressInfo->ai_addr, context.broadcastAddressInfo->ai_addr,
context.broadcastAddressInfo->ai_addrlen context.broadcastAddressInfo->ai_addrlen