diff --git a/client/controller.cpp b/client/controller.cpp index 8f67c4e5..4fca82e4 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -94,14 +94,18 @@ void Controller::onMessageReceived(ClientConnection* connection, const msg::Base headerChunk_->deserialize(baseMessage, buffer); logO << "Codec: " << headerChunk_->codec << "\n"; + decoder_.reset(nullptr); if (headerChunk_->codec == "pcm") decoder_.reset(new PcmDecoder()); #ifndef ANDROID - if (headerChunk_->codec == "ogg") + else if (headerChunk_->codec == "ogg") decoder_.reset(new OggDecoder()); #endif else if (headerChunk_->codec == "flac") decoder_.reset(new FlacDecoder()); + else + throw SnapException("codec not supported: \"" + headerChunk_->codec + "\""); + sampleFormat_ = decoder_->setHeader(headerChunk_.get()); logO << "sample rate: " << sampleFormat_.rate << "Hz\n"; logO << "bits/sample: " << sampleFormat_.bits << "\n"; diff --git a/client/decoder/decoder.h b/client/decoder/decoder.h index 8712198a..4a045950 100644 --- a/client/decoder/decoder.h +++ b/client/decoder/decoder.h @@ -18,6 +18,7 @@ #ifndef DECODER_H #define DECODER_H +#include #include "message/pcmChunk.h" #include "message/header.h" #include "message/sampleFormat.h" @@ -30,6 +31,9 @@ public: virtual ~Decoder() {}; virtual bool decode(msg::PcmChunk* chunk) = 0; virtual SampleFormat setHeader(msg::Header* chunk) = 0; + +protected: + std::mutex mutex_; }; diff --git a/client/decoder/flacDecoder.cpp b/client/decoder/flacDecoder.cpp index 169e36fd..0b872165 100644 --- a/client/decoder/flacDecoder.cpp +++ b/client/decoder/flacDecoder.cpp @@ -50,6 +50,7 @@ FlacDecoder::FlacDecoder() : Decoder() FlacDecoder::~FlacDecoder() { + std::lock_guard lock(mutex_); delete flacChunk; delete decoder; } @@ -57,6 +58,7 @@ FlacDecoder::~FlacDecoder() bool FlacDecoder::decode(msg::PcmChunk* chunk) { + std::lock_guard lock(mutex_); cacheInfo_.reset(); pcmChunk = chunk; flacChunk->payload = (char*)realloc(flacChunk->payload, chunk->payloadSize); diff --git a/client/decoder/flacDecoder.h b/client/decoder/flacDecoder.h index 0654325d..41a7d30f 100644 --- a/client/decoder/flacDecoder.h +++ b/client/decoder/flacDecoder.h @@ -21,6 +21,7 @@ #include "decoder.h" + struct CacheInfo { CacheInfo() : sampleRate_(0) diff --git a/client/decoder/oggDecoder.cpp b/client/decoder/oggDecoder.cpp index 7039f691..ab4808d4 100644 --- a/client/decoder/oggDecoder.cpp +++ b/client/decoder/oggDecoder.cpp @@ -39,6 +39,7 @@ OggDecoder::OggDecoder() : Decoder(), buffer(NULL), bytes(0) OggDecoder::~OggDecoder() { + std::lock_guard lock(mutex_); free(convbuffer); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); @@ -51,7 +52,7 @@ OggDecoder::~OggDecoder() bool OggDecoder::decode(msg::PcmChunk* chunk) { - + std::lock_guard lock(mutex_); /* grab some data at the head of the stream. We want the first page (which is guaranteed to be small and only contain the Vorbis stream initial header) We need the first page to get the stream