use a priority queue to ensure ordering of injectors

This commit is contained in:
Stephen Birarda 2015-11-19 17:42:14 -08:00
parent adf41fce19
commit 5f9c3eea76
2 changed files with 18 additions and 6 deletions

View file

@ -26,7 +26,7 @@ AudioInjectorManager::~AudioInjectorManager() {
// make sure any still living injectors are stopped and deleted // make sure any still living injectors are stopped and deleted
while (!_injectors.empty()) { while (!_injectors.empty()) {
// grab the injector at the front // grab the injector at the front
auto& timePointerPair = _injectors.front(); auto& timePointerPair = _injectors.top();
// ask it to stop and be deleted // ask it to stop and be deleted
timePointerPair.second->stopAndDeleteLater(); timePointerPair.second->stopAndDeleteLater();
@ -67,7 +67,7 @@ void AudioInjectorManager::run() {
// when does the next injector need to send a frame? // when does the next injector need to send a frame?
// do we get to wait or should we just go for it now? // do we get to wait or should we just go for it now?
auto timeInjectorPair = _injectors.front(); auto timeInjectorPair = _injectors.top();
auto nextTimestamp = timeInjectorPair.first; auto nextTimestamp = timeInjectorPair.first;
int64_t difference = int64_t(nextTimestamp - usecTimestampNow()); int64_t difference = int64_t(nextTimestamp - usecTimestampNow());
@ -78,7 +78,7 @@ void AudioInjectorManager::run() {
if (_injectors.size() > 0) { if (_injectors.size() > 0) {
// loop through the injectors in the map and send whatever frames need to go out // loop through the injectors in the map and send whatever frames need to go out
auto front = _injectors.front(); auto front = _injectors.top();
while (_injectors.size() > 0 && front.first <= usecTimestampNow()) { while (_injectors.size() > 0 && front.first <= usecTimestampNow()) {
// either way we're popping this injector off - get a copy first // either way we're popping this injector off - get a copy first
auto injector = front.second; auto injector = front.second;
@ -90,11 +90,11 @@ void AudioInjectorManager::run() {
if (nextCallDelta > 0 && !injector->isFinished()) { if (nextCallDelta > 0 && !injector->isFinished()) {
// re-enqueue the injector with the correct timing // re-enqueue the injector with the correct timing
_injectors.push({ usecTimestampNow() + nextCallDelta, injector }); _injectors.emplace(usecTimestampNow() + nextCallDelta, injector);
} }
} }
front = _injectors.front(); front = _injectors.top();
} }
} }

View file

@ -33,9 +33,19 @@ public:
private slots: private slots:
void run(); void run();
private: private:
using InjectorQPointer = QPointer<AudioInjector>; using InjectorQPointer = QPointer<AudioInjector>;
using TimeInjectorPointerPair = std::pair<uint64_t, InjectorQPointer>; using TimeInjectorPointerPair = std::pair<uint64_t, InjectorQPointer>;
using InjectorQueue = std::queue<TimeInjectorPointerPair>;
struct greaterTime {
bool operator() (const TimeInjectorPointerPair& x, const TimeInjectorPointerPair& y) const {
return x.first > y.first;
}
};
using InjectorQueue = std::priority_queue<TimeInjectorPointerPair,
std::deque<TimeInjectorPointerPair>,
greaterTime>;
using Mutex = std::mutex; using Mutex = std::mutex;
using Lock = std::unique_lock<Mutex>; using Lock = std::unique_lock<Mutex>;
@ -58,4 +68,6 @@ private:
friend class AudioInjector; friend class AudioInjector;
}; };
#endif // hifi_AudioInjectorManager_h #endif // hifi_AudioInjectorManager_h