mirror of
https://github.com/clangen/musikcube.git
synced 2025-03-14 13:21:13 +00:00
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:
parent
a996b95bc6
commit
34a07b018b
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user