Properly implemented play/pause semantics in output plugins (only WaveOut

was doing it properly!)
This commit is contained in:
casey langen 2016-12-23 12:53:57 -08:00
parent a6372b3c92
commit 5cd3a8de10
7 changed files with 28 additions and 1 deletions

View File

@ -79,6 +79,7 @@ AlsaOut::AlsaOut()
, rate(44100) , rate(44100)
, volume(1.0) , volume(1.0)
, quit(false) , quit(false)
, paused(false)
, initialized(false) { , initialized(false) {
std::cerr << "AlsaOut::AlsaOut() called" << std::endl; std::cerr << "AlsaOut::AlsaOut() called" << std::endl;
this->writeThread.reset(new boost::thread(boost::bind(&AlsaOut::WriteLoop, this))); this->writeThread.reset(new boost::thread(boost::bind(&AlsaOut::WriteLoop, this)));
@ -198,6 +199,7 @@ void AlsaOut::Pause() {
if (this->pcmHandle) { if (this->pcmHandle) {
snd_pcm_pause(this->pcmHandle, 1); snd_pcm_pause(this->pcmHandle, 1);
this->paused = true;
} }
} }
@ -206,6 +208,7 @@ void AlsaOut::Resume() {
if (this->pcmHandle) { if (this->pcmHandle) {
snd_pcm_pause(this->pcmHandle, 0); snd_pcm_pause(this->pcmHandle, 0);
this->paused = false;
NOTIFY(); NOTIFY();
} }
} }
@ -282,6 +285,10 @@ bool AlsaOut::Play(IBuffer *buffer, IBufferProvider* provider) {
{ {
LOCK("play"); LOCK("play");
if (this->paused) {
return false;
}
if (this->CountBuffersWithProvider(provider) >= BUFFER_COUNT) { if (this->CountBuffersWithProvider(provider) >= BUFFER_COUNT) {
return false; return false;
} }

View File

@ -87,7 +87,7 @@ class AlsaOut : public musik::core::sdk::IOutput {
size_t channels; size_t channels;
size_t rate; size_t rate;
double volume; double volume;
volatile bool quit, initialized; volatile bool quit, paused, initialized;
std::unique_ptr<boost::thread> writeThread; std::unique_ptr<boost::thread> writeThread;
boost::recursive_mutex stateMutex; boost::recursive_mutex stateMutex;

View File

@ -91,6 +91,7 @@ void CoreAudioOut::NotifyBufferCompleted(BufferContext *context) {
CoreAudioOut::CoreAudioOut() { CoreAudioOut::CoreAudioOut() {
this->quit = false; this->quit = false;
this->paused = false;
this->volume = 1.0f; this->volume = 1.0f;
this->audioFormat = (AudioStreamBasicDescription) { 0 }; this->audioFormat = (AudioStreamBasicDescription) { 0 };
@ -113,6 +114,10 @@ CoreAudioOut::CoreAudioOut() {
bool CoreAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) { bool CoreAudioOut::Play(IBuffer *buffer, IBufferProvider *provider) {
boost::recursive_mutex::scoped_lock lock(this->mutex); boost::recursive_mutex::scoped_lock lock(this->mutex);
if (this->paused) {
return false;
}
if (countBuffersWithProvider(this->buffers, provider) >= BUFFER_COUNT) { if (countBuffersWithProvider(this->buffers, provider) >= BUFFER_COUNT) {
/* enough buffers are already in the queue. bail, we'll notify the /* enough buffers are already in the queue. bail, we'll notify the
caller when there's more data available */ caller when there's more data available */
@ -206,6 +211,7 @@ void CoreAudioOut::Pause() {
if (this->audioQueue) { if (this->audioQueue) {
AudioQueuePause(this->audioQueue); AudioQueuePause(this->audioQueue);
this->paused = true;
} }
} }
@ -214,6 +220,7 @@ void CoreAudioOut::Resume() {
if (this->audioQueue) { if (this->audioQueue) {
AudioQueueStart(this->audioQueue, NULL); AudioQueueStart(this->audioQueue, NULL);
this->paused = false;
} }
} }

View File

@ -81,4 +81,5 @@ class CoreAudioOut : public musik::core::sdk::IOutput {
boost::thread thread; boost::thread thread;
boost::recursive_mutex mutex; boost::recursive_mutex mutex;
bool quit; bool quit;
bool paused;
}; };

View File

@ -155,6 +155,10 @@ bool DirectSoundOut::Play(IBuffer *buffer, IBufferProvider *provider) {
{ {
Lock lock(this->stateMutex); Lock lock(this->stateMutex);
if (this->state == StatePaused) {
return false;
}
if (!this->Configure(buffer)) { if (!this->Configure(buffer)) {
this->Reset(); this->Reset();
return false; return false;

View File

@ -144,6 +144,10 @@ bool PulseOut::Play(IBuffer *buffer, IBufferProvider* provider) {
{ {
Lock lock(this->stateMutex); Lock lock(this->stateMutex);
if (this->state == StatePaused) {
return false;
}
this->OpenDevice(buffer); this->OpenDevice(buffer);
if (!this->audioConnection || this->state != StatePlaying) { if (!this->audioConnection || this->state != StatePlaying) {

View File

@ -122,6 +122,10 @@ bool WasapiOut::Play(IBuffer *buffer, IBufferProvider *provider) {
{ {
Lock lock(this->stateMutex); Lock lock(this->stateMutex);
if (this->state == StatePaused) {
return false;
}
if (!this->Configure(buffer)) { if (!this->Configure(buffer)) {
this->Reset(); this->Reset();
return false; return false;