handle blocked AudioInjectorManager for restart

This commit is contained in:
Stephen Birarda 2015-11-17 16:31:18 -08:00
parent d764ff2e87
commit ab5c8e072f
5 changed files with 22 additions and 16 deletions

View file

@ -9,7 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
var soundURL = "atp:44a83a788ccfd2924e35c902c34808b24dbd0309d000299ce01a355f91cf8115.wav"; var soundURL = "http://hifi-public.s3.amazonaws.com/birarda/medium-crowd.wav";
var audioOptions = { var audioOptions = {
position: MyAvatar.position, position: MyAvatar.position,
volume: 0.5 volume: 0.5

View file

@ -85,8 +85,17 @@ void AudioInjector::setupInjection() {
} }
void AudioInjector::restart() { void AudioInjector::restart() {
// grab the AudioInjectorManager
auto injectorManager = DependencyManager::get<AudioInjectorManager>();
if (thread() != QThread::currentThread()) { if (thread() != QThread::currentThread()) {
QMetaObject::invokeMethod(this, "restart"); QMetaObject::invokeMethod(this, "restart");
if (!_options.localOnly) {
// notify the AudioInjectorManager to wake up in case it's waiting for new injectors
injectorManager->notifyInjectorReadyCondition();
}
return; return;
} }
@ -100,18 +109,16 @@ void AudioInjector::restart() {
_shouldStop = false; _shouldStop = false;
_state = State::NotFinished; _state = State::NotFinished;
qDebug() << "Emitting restartedWhileFinished to inject audio again to restart an injector";
// call inject audio to start injection over again // call inject audio to start injection over again
setupInjection(); setupInjection();
// if we're a local injector call inject locally to start injecting again // if we're a local injector call inject locally to start injecting again
if (_options.localOnly) { if (_options.localOnly) {
injectLocally(); injectLocally();
} else {
// wake the AudioInjectorManager back up if it's stuck waiting
injectorManager->restartFinishedInjector(this);
} }
// emit our restarted signal, for network injectors this allows the AudioInjectorManager to start considering us again
emit restartedWhileFinished();
} }
} }

View file

@ -76,15 +76,16 @@ public slots:
signals: signals:
void finished(); void finished();
void restartedWhileFinished(); void restarting();
private slots:
void finish();
private: private:
void setupInjection(); void setupInjection();
uint64_t injectNextFrame(); uint64_t injectNextFrame();
bool injectLocally(); bool injectLocally();
void finish();
QByteArray _audioData; QByteArray _audioData;
AudioInjectorOptions _options; AudioInjectorOptions _options;
std::atomic<State> _state { State::NotFinished }; std::atomic<State> _state { State::NotFinished };

View file

@ -117,7 +117,6 @@ bool AudioInjectorManager::threadInjector(AudioInjector* injector) {
injector->moveToThread(_thread); injector->moveToThread(_thread);
// handle a restart once the injector has finished // handle a restart once the injector has finished
connect(injector, &AudioInjector::restartedWhileFinished, this, &AudioInjectorManager::restartFinishedInjector);
// add the injector to the queue with a send timestamp of now // add the injector to the queue with a send timestamp of now
_injectors.emplace(usecTimestampNow(), InjectorQPointer { injector }); _injectors.emplace(usecTimestampNow(), InjectorQPointer { injector });
@ -134,9 +133,7 @@ bool AudioInjectorManager::threadInjector(AudioInjector* injector) {
} }
} }
void AudioInjectorManager::restartFinishedInjector() { void AudioInjectorManager::restartFinishedInjector(AudioInjector* injector) {
auto injector = qobject_cast<AudioInjector*>(sender());
// guard the injectors vector with a mutex // guard the injectors vector with a mutex
std::unique_lock<std::mutex> lock(_injectorsMutex); std::unique_lock<std::mutex> lock(_injectorsMutex);

View file

@ -36,7 +36,8 @@ private slots:
void run(); void run();
private: private:
bool threadInjector(AudioInjector* injector); bool threadInjector(AudioInjector* injector);
void restartFinishedInjector(); void restartFinishedInjector(AudioInjector* injector);
void notifyInjectorReadyCondition() { _injectorReady.notify_one(); }
AudioInjectorManager() {}; AudioInjectorManager() {};
AudioInjectorManager(const AudioInjectorManager&) = delete; AudioInjectorManager(const AudioInjectorManager&) = delete;