fixed crash when switching streams

This commit is contained in:
badaix 2016-02-06 12:51:20 +01:00
parent 060e52aeee
commit 3a5af32d98
5 changed files with 14 additions and 2 deletions

View file

@ -94,14 +94,18 @@ void Controller::onMessageReceived(ClientConnection* connection, const msg::Base
headerChunk_->deserialize(baseMessage, buffer); headerChunk_->deserialize(baseMessage, buffer);
logO << "Codec: " << headerChunk_->codec << "\n"; logO << "Codec: " << headerChunk_->codec << "\n";
decoder_.reset(nullptr);
if (headerChunk_->codec == "pcm") if (headerChunk_->codec == "pcm")
decoder_.reset(new PcmDecoder()); decoder_.reset(new PcmDecoder());
#ifndef ANDROID #ifndef ANDROID
if (headerChunk_->codec == "ogg") else if (headerChunk_->codec == "ogg")
decoder_.reset(new OggDecoder()); decoder_.reset(new OggDecoder());
#endif #endif
else if (headerChunk_->codec == "flac") else if (headerChunk_->codec == "flac")
decoder_.reset(new FlacDecoder()); decoder_.reset(new FlacDecoder());
else
throw SnapException("codec not supported: \"" + headerChunk_->codec + "\"");
sampleFormat_ = decoder_->setHeader(headerChunk_.get()); sampleFormat_ = decoder_->setHeader(headerChunk_.get());
logO << "sample rate: " << sampleFormat_.rate << "Hz\n"; logO << "sample rate: " << sampleFormat_.rate << "Hz\n";
logO << "bits/sample: " << sampleFormat_.bits << "\n"; logO << "bits/sample: " << sampleFormat_.bits << "\n";

View file

@ -18,6 +18,7 @@
#ifndef DECODER_H #ifndef DECODER_H
#define DECODER_H #define DECODER_H
#include <mutex>
#include "message/pcmChunk.h" #include "message/pcmChunk.h"
#include "message/header.h" #include "message/header.h"
#include "message/sampleFormat.h" #include "message/sampleFormat.h"
@ -30,6 +31,9 @@ public:
virtual ~Decoder() {}; virtual ~Decoder() {};
virtual bool decode(msg::PcmChunk* chunk) = 0; virtual bool decode(msg::PcmChunk* chunk) = 0;
virtual SampleFormat setHeader(msg::Header* chunk) = 0; virtual SampleFormat setHeader(msg::Header* chunk) = 0;
protected:
std::mutex mutex_;
}; };

View file

@ -50,6 +50,7 @@ FlacDecoder::FlacDecoder() : Decoder()
FlacDecoder::~FlacDecoder() FlacDecoder::~FlacDecoder()
{ {
std::lock_guard<std::mutex> lock(mutex_);
delete flacChunk; delete flacChunk;
delete decoder; delete decoder;
} }
@ -57,6 +58,7 @@ FlacDecoder::~FlacDecoder()
bool FlacDecoder::decode(msg::PcmChunk* chunk) bool FlacDecoder::decode(msg::PcmChunk* chunk)
{ {
std::lock_guard<std::mutex> lock(mutex_);
cacheInfo_.reset(); cacheInfo_.reset();
pcmChunk = chunk; pcmChunk = chunk;
flacChunk->payload = (char*)realloc(flacChunk->payload, chunk->payloadSize); flacChunk->payload = (char*)realloc(flacChunk->payload, chunk->payloadSize);

View file

@ -21,6 +21,7 @@
#include "decoder.h" #include "decoder.h"
struct CacheInfo struct CacheInfo
{ {
CacheInfo() : sampleRate_(0) CacheInfo() : sampleRate_(0)

View file

@ -39,6 +39,7 @@ OggDecoder::OggDecoder() : Decoder(), buffer(NULL), bytes(0)
OggDecoder::~OggDecoder() OggDecoder::~OggDecoder()
{ {
std::lock_guard<std::mutex> lock(mutex_);
free(convbuffer); free(convbuffer);
vorbis_block_clear(&vb); vorbis_block_clear(&vb);
vorbis_dsp_clear(&vd); vorbis_dsp_clear(&vd);
@ -51,7 +52,7 @@ OggDecoder::~OggDecoder()
bool OggDecoder::decode(msg::PcmChunk* chunk) bool OggDecoder::decode(msg::PcmChunk* chunk)
{ {
std::lock_guard<std::mutex> lock(mutex_);
/* grab some data at the head of the stream. We want the first page /* 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 (which is guaranteed to be small and only contain the Vorbis
stream initial header) We need the first page to get the stream stream initial header) We need the first page to get the stream