59 lines
1.2 KiB
C++
59 lines
1.2 KiB
C++
#pragma once
|
|
|
|
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <portaudio.h>
|
|
#include <queue>
|
|
#include <thread>
|
|
|
|
#include "packets/AudioPacket.hpp"
|
|
|
|
|
|
// TODO(Faraphel): should be moved somewhere else
|
|
struct AudioPacketsComparator {
|
|
bool operator() (const AudioPacket &a, const AudioPacket &b) const {
|
|
return a.timePlay > b.timePlay;
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* the audio Client.
|
|
* Receive audio packets and play them at a specific time.
|
|
*/
|
|
class Client {
|
|
public:
|
|
/**
|
|
* :param channels: the number of channel in the audio
|
|
* :param rate: the rate of the audio
|
|
*/
|
|
explicit Client(int channels, double rate);
|
|
~Client();
|
|
|
|
/**
|
|
* Indefinitely receive and play audio data.
|
|
*/
|
|
void loop();
|
|
|
|
private:
|
|
/**
|
|
* Indefinitely receive audio data.
|
|
*/
|
|
void loopReceiver();
|
|
|
|
/**
|
|
* Indefinitely play audio data.
|
|
*/
|
|
void loopPlayer();
|
|
|
|
PaStream* stream;
|
|
int channels;
|
|
std::priority_queue<AudioPacket, std::vector<AudioPacket>, AudioPacketsComparator> audioQueue;
|
|
|
|
std::mutex audioMutex;
|
|
std::unique_lock<std::mutex> audioLock;
|
|
std::condition_variable audioCondition;
|
|
std::thread receiverThread;
|
|
std::thread playerThread;
|
|
};
|