more cleanup for AudioInjector mem management

This commit is contained in:
Stephen Birarda 2015-02-12 10:56:40 -08:00
parent 7becf62ba9
commit ea52cea5dd
5 changed files with 33 additions and 13 deletions

View file

@ -55,7 +55,9 @@ var droneSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/drone.st
var currentDrone = null;
var latinSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/latin.stereo.raw")
var latinInjector = null;
var elevatorSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/elevator.stereo.raw")
var elevatorInjector = null;
var currentMuzakInjector = null;
var currentSound = null;
@ -140,7 +142,11 @@ function drawLobby() {
if (droneSound.downloaded) {
// start the drone sound
currentDrone = Audio.playSound(droneSound, { stereo: true, loop: true, localOnly: true, volume: DRONE_VOLUME });
if (!currentDrone) {
currentDrone = Audio.playSound(droneSound, { stereo: true, loop: true, localOnly: true, volume: DRONE_VOLUME });
} else {
currentDrone.restart();
}
}
// start one of our muzak sounds
@ -203,7 +209,24 @@ function playRandomMuzak() {
if (currentSound) {
// pick a random number of seconds from 0-10 to offset the muzak
var secondOffset = Math.random() * 10;
currentMuzakInjector = Audio.playSound(currentSound, { localOnly: true, secondOffset: secondOffset, volume: MUZAK_VOLUME });
if (currentSound == latinSound) {
if (!latinInjector) {
latinInjector = Audio.playSound(latinSound, { localOnly: true, secondOffset: secondOffset, volume: MUZAK_VOLUME });
} else {
latinInjector.restart();
}
currentMuzakInjector = latinInjector;
} else if (currentSound == elevatorSound) {
if (!elevatorInjector) {
elevatorInjector = Audio.playSound(elevatorSound, { localOnly: true, secondOffset: secondOffset, volume: MUZAK_VOLUME });
} else {
elevatorInjector.restart();
}
currentMuzakInjector = elevatorInjector;
}
} else {
currentMuzakInjector = null;
}
@ -228,9 +251,8 @@ function cleanupLobby() {
orbShell = false;
currentDrone.stop();
currentDrone = null;
currentMuzakInjector.stop();
currentMuzakInjector = null;
places = {};

View file

@ -974,14 +974,15 @@ bool AudioClient::outputLocalInjector(bool isStereo, qreal volume, AudioInjector
QAudioOutput* localOutput = new QAudioOutput(getNamedAudioDeviceForMode(QAudio::AudioOutput, _outputAudioDeviceName),
localFormat,
injector);
injector->getLocalBuffer());
localOutput->setVolume(volume);
// move the localOutput to the same thread as the local injector buffer
localOutput->moveToThread(injector->getLocalBuffer()->thread());
// have it be cleaned up when that injector is done
connect(injector, &AudioInjector::finished, localOutput, &QAudioOutput::stop);
// have it be stopped when that local buffer is about to close
connect(injector->getLocalBuffer(), &QIODevice::aboutToClose, localOutput, &QAudioOutput::stop);
qDebug() << "Starting QAudioOutput for local injector" << localOutput;

View file

@ -91,6 +91,7 @@ void AudioInjector::injectAudio() {
}
void AudioInjector::restart() {
qDebug() << "Restarting an AudioInjector by stopping and starting over.";
stop();
QMetaObject::invokeMethod(this, "injectAudio", Qt::QueuedConnection);
}
@ -101,6 +102,7 @@ void AudioInjector::injectLocally() {
if (_audioData.size() > 0) {
_localBuffer = new AudioInjectorLocalBuffer(_audioData, this);
_localBuffer->open(QIODevice::ReadOnly);
_localBuffer->setShouldLoop(_options.loop);

View file

@ -49,7 +49,6 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
optionsCopy.stereo = sound->isStereo();
QThread* injectorThread = new QThread();
qDebug() << "the injector thread is" << injectorThread;
injectorThread->setObjectName("Audio Injector Thread");
AudioInjector* injector = new AudioInjector(sound, optionsCopy);
@ -57,12 +56,9 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
injector->moveToThread(injectorThread);
qDebug() << "the injector is" << injector;
// start injecting when the injector thread starts
connect(injectorThread, &QThread::started, injector, &AudioInjector::injectAudio);
// connect the right slots and signals for AudioInjector and thread cleanup
connect(injector, &AudioInjector::destroyed, injectorThread, &QThread::quit);
connect(injectorThread, &QThread::finished, injectorThread, &QThread::deleteLater);

View file

@ -31,9 +31,8 @@ ScriptAudioInjector::ScriptAudioInjector(AudioInjector* injector) :
ScriptAudioInjector::~ScriptAudioInjector() {
if (!_injector.isNull()) {
// we've been asked to delete after finishing, trigger a queued deleteLater here
QMetaObject::invokeMethod(_injector.data(), "triggerDeleteAfterFinish", Qt::QueuedConnection);
_injector->triggerDeleteAfterFinish();
}
}
void ScriptAudioInjector::stopInjectorImmediately() {