Made a couple small tweaks to Stream and Player to allow closing the data

stream from a background thread which should allow interrupting blocking reads.
This commit is contained in:
Casey Langen 2017-03-18 11:20:00 -07:00
parent a996b95bc6
commit 34a07b018b
8 changed files with 38 additions and 9 deletions

View File

@ -81,8 +81,11 @@ NomadDecoder::~NomadDecoder() {
}
void NomadDecoder::Destroy() {
nomad_close(this->nomadContext);
this->nomadContext = nullptr;
if (this->nomadContext) {
nomad_close(this->nomadContext);
this->nomadContext = nullptr;
}
delete this;
}
@ -132,6 +135,9 @@ bool NomadDecoder::Open(IDataStream *stream) {
this->duration = nomad_info(this->nomadContext)->duration;
}
}
else {
this->nomadContext = nullptr;
}
return result ? false : true;
}

View File

@ -68,7 +68,8 @@ bool NomadDecoderFactory::CanHandle(const char* type) const {
if (endsWith(str, ".mp3") ||
str.find("audio/mpeg3") != std::string::npos ||
str.find("audio/x-mpeg-3") != std::string::npos ||
str.find("audio/mp3") != std::string::npos)
str.find("audio/mp3") != std::string::npos ||
str.find("audio/mpeg") != std::string::npos)
{
return true;
}

View File

@ -57,6 +57,7 @@ namespace musik { namespace core { namespace audio {
virtual double SetPosition(double seconds) = 0;
virtual double GetDuration() = 0;
virtual bool OpenStream(std::string uri) = 0;
virtual void Close() = 0;
};
typedef std::shared_ptr<IStream> IStreamPtr;

View File

@ -169,6 +169,7 @@ void Player::Destroy() {
return; /* already terminated (or terminating) */
}
this->stream->Close();
this->state = Player::Quit;
this->writeToOutputCondition.notify_all();
this->thread->detach();

View File

@ -136,6 +136,12 @@ bool Stream::OpenStream(std::string uri) {
return false;
}
void Stream::Close() {
if (this->dataStream) {
this->dataStream->Close();
}
}
void Stream::OnBufferProcessedByPlayer(Buffer* buffer) {
this->recycledBuffers.push_back(buffer);
}

View File

@ -67,6 +67,7 @@ namespace musik { namespace core { namespace audio {
virtual double SetPosition(double seconds);
virtual double GetDuration();
virtual bool OpenStream(std::string uri);
virtual void Close();
private:
bool GetNextBufferFromDecoder();

View File

@ -48,7 +48,7 @@ using namespace musik::core::io;
using namespace musik::core::sdk;
LocalFileStream::LocalFileStream()
: file(NULL)
: file(nullptr)
, filesize(-1) {
}
@ -104,9 +104,9 @@ bool LocalFileStream::Open(const char *filename, unsigned int options) {
}
bool LocalFileStream::Close() {
if (this->file) {
if (fclose(this->file) == 0) {
this->file = NULL;
auto file = this->file.exchange(nullptr);
if (file) {
if (fclose(file) == 0) {
return true;
}
}
@ -119,19 +119,31 @@ void LocalFileStream::Destroy() {
}
PositionType LocalFileStream::Read(void* buffer, PositionType readBytes) {
if (!this->file) {
return 0;
}
return (PositionType) fread(buffer, 1, readBytes, this->file);
}
bool LocalFileStream::SetPosition(PositionType position) {
if (!this->file) {
return false;
}
return fseek(this->file, position, SEEK_SET) == 0;
}
PositionType LocalFileStream::Position() {
if (!this->file) {
return -1;
}
return ftell(this->file);
}
bool LocalFileStream::Eof() {
return feof(this->file) != 0;
return !this->file || feof(this->file) != 0;
}
long LocalFileStream::Length() {

View File

@ -36,6 +36,7 @@
#include <core/config.h>
#include <core/sdk/IDataStream.h>
#include <atomic>
namespace musik { namespace core { namespace io {
@ -61,7 +62,7 @@ namespace musik { namespace core { namespace io {
private:
std::string extension;
std::string uri;
FILE *file;
std::atomic<FILE*> file;
long filesize;
};