openal fix for windows to hybernate

This commit is contained in:
John Preston 2015-01-10 16:08:30 +03:00
parent ed44b818fc
commit 94b0a83f0b
4 changed files with 3831 additions and 4 deletions

View File

@ -51,6 +51,7 @@ bool _checkALError() {
} }
void audioInit() { void audioInit() {
uint64 ms = getms();
if (audioDevice) return; if (audioDevice) return;
audioDevice = alcOpenDevice(NULL); audioDevice = alcOpenDevice(NULL);
@ -148,6 +149,8 @@ void audioInit() {
if (!_checkALError()) return audioFinish(); if (!_checkALError()) return audioFinish();
voicemsgs = new VoiceMessages(); voicemsgs = new VoiceMessages();
alcSuspendContext(audioContext);
LOG(("Audio init time: %1").arg(getms() - ms));
} }
bool audioWorks() { bool audioWorks() {
@ -157,7 +160,9 @@ bool audioWorks() {
void audioPlayNotify() { void audioPlayNotify() {
if (!audioWorks()) return; if (!audioWorks()) return;
audioVoice()->processContext();
alSourcePlay(notifySource); alSourcePlay(notifySource);
emit audioVoice()->faderOnTimer();
} }
void audioFinish() { void audioFinish() {
@ -307,6 +312,7 @@ void VoiceMessages::pauseresume() {
updateCurrentStarted(); updateCurrentStarted();
} }
_data[_current].state = VoiceMessageResuming; _data[_current].state = VoiceMessageResuming;
processContext();
alSourcePlay(_data[_current].source); alSourcePlay(_data[_current].source);
break; break;
case VoiceMessageStarting: case VoiceMessageStarting:
@ -328,15 +334,25 @@ void VoiceMessages::currentState(AudioData **audio, VoiceMessageState *state, in
if (duration) *duration = _data[_current].duration; if (duration) *duration = _data[_current].duration;
} }
void VoiceMessages::processContext() {
_fader->processContext();
}
VoiceMessages *audioVoice() { VoiceMessages *audioVoice() {
return voicemsgs; return voicemsgs;
} }
VoiceMessagesFader::VoiceMessagesFader(QThread *thread) : _timer(this) { VoiceMessagesFader::VoiceMessagesFader(QThread *thread) : _timer(this), _suspendFlag(false) {
moveToThread(thread); moveToThread(thread);
_timer.moveToThread(thread); _timer.moveToThread(thread);
_suspendTimer.moveToThread(thread);
_timer.setSingleShot(true); _timer.setSingleShot(true);
connect(&_timer, SIGNAL(timeout()), this, SLOT(onTimer())); connect(&_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
_suspendTimer.setSingleShot(true);
connect(&_suspendTimer, SIGNAL(timeout()), this, SLOT(onSuspendTimer()));
connect(this, SIGNAL(stopSuspend()), this, SLOT(onSuspendTimerStop()), Qt::QueuedConnection);
} }
void VoiceMessagesFader::onInit() { void VoiceMessagesFader::onInit() {
@ -403,7 +419,6 @@ void VoiceMessagesFader::onTimer() {
b = a; b = a;
} }
alSourcef(m.source, AL_GAIN, newGain); alSourcef(m.source, AL_GAIN, newGain);
LOG(("Now volume is: %1").arg(newGain));
} }
} else if (playing && (state == AL_PLAYING || !m.loading)) { } else if (playing && (state == AL_PLAYING || !m.loading)) {
if (state != AL_PLAYING) { if (state != AL_PLAYING) {
@ -428,13 +443,44 @@ void VoiceMessagesFader::onTimer() {
if (fading) hasFading = true; if (fading) hasFading = true;
} }
} }
if (!hasPlaying) {
ALint state = AL_INITIAL;
alGetSourcei(notifySource, AL_SOURCE_STATE, &state);
if (_checkALError() && state == AL_PLAYING) {
hasPlaying = true;
}
}
if (hasFading) { if (hasFading) {
_timer.start(AudioFadeTimeout); _timer.start(AudioFadeTimeout);
processContext();
} else if (hasPlaying) { } else if (hasPlaying) {
_timer.start(AudioCheckPositionTimeout); _timer.start(AudioCheckPositionTimeout);
processContext();
} else {
QMutexLocker lock(&_suspendMutex);
_suspendFlag = true;
_suspendTimer.start(AudioSuspendTimeout);
} }
} }
void VoiceMessagesFader::onSuspendTimer() {
QMutexLocker lock(&_suspendMutex);
if (_suspendFlag) {
alcSuspendContext(audioContext);
}
}
void VoiceMessagesFader::onSuspendTimerStop() {
if (_suspendTimer.isActive()) _suspendTimer.stop();
}
void VoiceMessagesFader::processContext() {
QMutexLocker lock(&_suspendMutex);
_suspendFlag = false;
emit stopSuspend();
alcProcessContext(audioContext);
}
struct VoiceMessagesLoader::Loader { struct VoiceMessagesLoader::Loader {
QString fname; QString fname;
QByteArray data; QByteArray data;
@ -681,6 +727,7 @@ void VoiceMessagesLoader::onLoad(AudioData *audio) {
alGetSourcei(m.source, AL_SOURCE_STATE, &state); alGetSourcei(m.source, AL_SOURCE_STATE, &state);
if (_checkALError()) { if (_checkALError()) {
if (state != AL_PLAYING) { if (state != AL_PLAYING) {
voice->processContext();
alSourcePlay(m.source); alSourcePlay(m.source);
emit needToCheck(); emit needToCheck();
} }

View File

@ -48,6 +48,7 @@ public:
void pauseresume(); void pauseresume();
void currentState(AudioData **audio, VoiceMessageState *state = 0, int64 *position = 0, int64 *duration = 0); void currentState(AudioData **audio, VoiceMessageState *state = 0, int64 *position = 0, int64 *duration = 0);
void processContext();
~VoiceMessages(); ~VoiceMessages();
@ -112,6 +113,7 @@ class VoiceMessagesFader : public QObject {
public: public:
VoiceMessagesFader(QThread *thread); VoiceMessagesFader(QThread *thread);
void processContext();
signals: signals:
@ -120,14 +122,20 @@ signals:
void audioStopped(AudioData *audio); void audioStopped(AudioData *audio);
void needToPreload(AudioData *audio); void needToPreload(AudioData *audio);
void stopSuspend();
public slots: public slots:
void onInit(); void onInit();
void onTimer(); void onTimer();
void onSuspendTimer();
void onSuspendTimerStop();
private: private:
QTimer _timer; QTimer _timer, _suspendTimer;
QMutex _suspendMutex;
bool _suspendFlag;
}; };

View File

@ -86,9 +86,10 @@ enum {
AudioVoiceMsgChannels = 2, // stereo AudioVoiceMsgChannels = 2, // stereo
AudioVoiceMsgBufferSize = 1024 * 1024, // 1 Mb buffers AudioVoiceMsgBufferSize = 1024 * 1024, // 1 Mb buffers
AudioVoiceMsgInMemory = 1024 * 1024, // 1 Mb audio is hold in memory and auto loaded AudioVoiceMsgInMemory = 1024 * 1024, // 1 Mb audio is hold in memory and auto loaded
AudioSuspendTimeout = 3000, // suspend in 3 secs after playing is over
StickerInMemory = 256 * 1024, // 128 Kb stickers hold in memory, auto loaded and displayed inline StickerInMemory = 256 * 1024, // 128 Kb stickers hold in memory, auto loaded and displayed inline
StickerMaxSize = 1280, // 1024x1024 is a max image size for sticker StickerMaxSize = 2048, // 2048x2048 is a max image size for sticker
MediaViewImageSizeLimit = 100 * 1024 * 1024, // show up to 100mb jpg/png/gif docs in app MediaViewImageSizeLimit = 100 * 1024 * 1024, // show up to 100mb jpg/png/gif docs in app
MaxZoomLevel = 7, // x8 MaxZoomLevel = 7, // x8

File diff suppressed because it is too large Load Diff