diff --git a/src/plugins/httpdatastream/HttpDataStream.cpp b/src/plugins/httpdatastream/HttpDataStream.cpp index 65a4b6851..8a07f3624 100755 --- a/src/plugins/httpdatastream/HttpDataStream.cpp +++ b/src/plugins/httpdatastream/HttpDataStream.cpp @@ -38,6 +38,8 @@ #include "LruDiskCache.h" #include +#include +#include #include #include @@ -62,14 +64,19 @@ using namespace musik::core::sdk; namespace al = boost::algorithm; -static const int MAX_CACHE_FILES = 35; -static const int NOTIFY_INTERVAL_BYTES = 131072; /* 2^17 */ -static const int PRECACHE_BYTES = 524288; /*2^19 */ - static std::mutex globalMutex; static IEnvironment* environment; static LruDiskCache diskCache; static std::string cachePath; +static IPreferences* prefs; + +static const int kDefaultMaxCacheFiles = 35; +static const int kDefaultPreCacheSizeBytes = 524288; /*2^19 */ +static const int kDefaultChunkSizeBytes = 131072; /* 2^17 */ + +static const std::string kMaxCacheFiles = "max_cache_files"; +static const std::string kPreCacheBufferSizeBytesKey = "precache_buffer_size_bytes"; +static const std::string kChunkSizeBytesKey = "chunk_size_bytes"; const std::string HttpDataStream::kRemoteTrackHost = "musikcore://remote-track/"; @@ -89,6 +96,18 @@ extern "C" DLLEXPORT void SetEnvironment(IEnvironment* environment) { } } +extern "C" DLLEXPORT void SetPreferences(IPreferences * prefs) { + ::prefs = prefs; +} + +extern "C" DLLEXPORT musik::core::sdk::ISchema * GetSchema() { + auto schema = new TSchema<>(); + schema->AddInt(kMaxCacheFiles, kDefaultMaxCacheFiles); + schema->AddInt(kPreCacheBufferSizeBytesKey, kDefaultPreCacheSizeBytes, 32768); + schema->AddInt(kChunkSizeBytesKey, kDefaultChunkSizeBytes, 32768); + return schema; +} + static bool parseHeader(std::string raw, std::string& key, std::string& value) { al::replace_all(raw, "\r\n", ""); @@ -241,9 +260,13 @@ bool HttpDataStream::Open(const char *rawUri, OpenFlags flags) { return false; } + this->precacheSizeBytes = prefs->GetInt(kPreCacheBufferSizeBytesKey.c_str(), kDefaultPreCacheSizeBytes); + this->chunkSizeBytes = prefs->GetInt(kChunkSizeBytesKey.c_str(), kDefaultChunkSizeBytes); + this->maxCacheFiles = prefs->GetInt(kMaxCacheFiles.c_str(), kDefaultMaxCacheFiles); + std::unique_lock lock(this->stateMutex); - diskCache.Init(cachePath, MAX_CACHE_FILES); + diskCache.Init(cachePath, this->maxCacheFiles); this->httpUri = rawUri; @@ -441,7 +464,7 @@ size_t HttpDataStream::CurlWriteCallback(char *ptr, size_t size, size_t nmemb, v size_t result = fwrite(ptr, size, nmemb, stream->writeFile); stream->written += result; - if (stream->written >= NOTIFY_INTERVAL_BYTES) { + if (stream->written >= stream->chunkSizeBytes) { fflush(stream->writeFile); stream->reader->Add(stream->written); stream->written = 0; @@ -449,7 +472,7 @@ size_t HttpDataStream::CurlWriteCallback(char *ptr, size_t size, size_t nmemb, v if (stream->totalWritten > -1) { stream->totalWritten += result; - if (stream->totalWritten >= PRECACHE_BYTES) { + if (stream->totalWritten >= stream->precacheSizeBytes) { stream->startedContition.notify_all(); stream->totalWritten = -1; } diff --git a/src/plugins/httpdatastream/HttpDataStream.h b/src/plugins/httpdatastream/HttpDataStream.h index 39eb0337f..c3be34db1 100755 --- a/src/plugins/httpdatastream/HttpDataStream.h +++ b/src/plugins/httpdatastream/HttpDataStream.h @@ -101,4 +101,5 @@ class HttpDataStream : public IDataStream { std::condition_variable startedContition; std::shared_ptr downloadThread; std::shared_ptr reader; + int precacheSizeBytes, chunkSizeBytes, maxCacheFiles; }; \ No newline at end of file