12 #ifndef hifi_AudioMixerSlavePool_h
13 #define hifi_AudioMixerSlavePool_h
15 #include <condition_variable>
20 #include <shared/QtHelpers.h>
21 #include <TBBHelpers.h>
23 #include "AudioMixerSlave.h"
25 class AudioMixerSlavePool;
27 class AudioMixerSlaveThread :
public QThread,
public AudioMixerSlave {
29 using ConstIter = NodeList::const_iterator;
30 using Mutex = std::mutex;
31 using Lock = std::unique_lock<Mutex>;
34 AudioMixerSlaveThread(AudioMixerSlavePool& pool, AudioMixerSlave::SharedData& sharedData)
35 : AudioMixerSlave(sharedData), _pool(pool) {}
37 void run() override final;
40 friend class AudioMixerSlavePool;
43 void notify(
bool stopping);
44 bool try_pop(SharedNodePointer& node);
46 AudioMixerSlavePool& _pool;
47 void (AudioMixerSlave::*_function)(const SharedNodePointer& node) {
nullptr };
53 class AudioMixerSlavePool {
54 using Queue = tbb::concurrent_queue<SharedNodePointer>;
55 using Mutex = std::mutex;
56 using Lock = std::unique_lock<Mutex>;
57 using ConditionVariable = std::condition_variable;
60 using ConstIter = NodeList::const_iterator;
62 AudioMixerSlavePool(AudioMixerSlave::SharedData& sharedData,
int numThreads = QThread::idealThreadCount())
63 : _workerSharedData(sharedData) { setNumThreads(numThreads); }
64 ~AudioMixerSlavePool() { resize(0); }
67 void processPackets(ConstIter begin, ConstIter end);
70 void mix(ConstIter begin, ConstIter end,
unsigned int frame,
int numToRetain);
73 void each(std::function<
void(AudioMixerSlave& slave)> functor);
75 #ifdef DEBUG_EVENT_QUEUE
76 void queueStats(QJsonObject& stats);
79 void setNumThreads(
int numThreads);
80 int numThreads() {
return _numThreads; }
83 void run(ConstIter begin, ConstIter end);
84 void resize(
int numThreads);
86 std::vector<std::unique_ptr<AudioMixerSlaveThread>> _slaves;
88 friend void AudioMixerSlaveThread::wait();
89 friend void AudioMixerSlaveThread::notify(
bool stopping);
90 friend bool AudioMixerSlaveThread::try_pop(SharedNodePointer& node);
94 ConditionVariable _slaveCondition;
95 ConditionVariable _poolCondition;
96 void (AudioMixerSlave::*_function)(
const SharedNodePointer& node);
97 std::function<void(AudioMixerSlave&)> _configure;
98 int _numThreads { 0 };
99 int _numStarted { 0 };
100 int _numFinished { 0 };
101 int _numStopped { 0 };
108 AudioMixerSlave::SharedData& _workerSharedData;