diff --git a/src/kits/media/ChunkCache.cpp b/src/kits/media/ChunkCache.cpp index bf737e4f19..94d9c151ca 100644 --- a/src/kits/media/ChunkCache.cpp +++ b/src/kits/media/ChunkCache.cpp @@ -24,7 +24,7 @@ chunk_buffer::chunk_buffer() chunk_buffer::~chunk_buffer() { - free(buffer); + rtm_free(buffer); } @@ -34,23 +34,26 @@ chunk_buffer::~chunk_buffer() ChunkCache::ChunkCache(sem_id waitSem, size_t maxBytes) : BLocker("media chunk cache"), - fWaitSem(waitSem), - fMaxBytes(maxBytes), - fBytes(0) + fWaitSem(waitSem) { + rtm_create_pool(&fRealTimePool, maxBytes, "media chunk cache"); + fMaxBytes = rtm_available(fRealTimePool); } ChunkCache::~ChunkCache() { - while (chunk_buffer* chunk = fChunks.RemoveHead()) - delete chunk; + rtm_delete_pool(fRealTimePool); +} - while (chunk_buffer* chunk = fUnusedChunks.RemoveHead()) - delete chunk; - while (chunk_buffer* chunk = fInFlightChunks.RemoveHead()) - delete chunk; +status_t +ChunkCache::InitCheck() const +{ + if (fRealTimePool == NULL) + return B_NO_MEMORY; + + return B_OK; } @@ -60,7 +63,6 @@ ChunkCache::MakeEmpty() ASSERT(IsLocked()); fUnusedChunks.MoveFrom(&fChunks); - fBytes = 0; release_sem(fWaitSem); } @@ -71,7 +73,7 @@ ChunkCache::SpaceLeft() const { ASSERT(IsLocked()); - return fBytes < fMaxBytes; + return sizeof(chunk_buffer) + 2048 < rtm_available(fRealTimePool); } @@ -82,7 +84,6 @@ ChunkCache::NextChunk() chunk_buffer* chunk = fChunks.RemoveHead(); if (chunk != NULL) { - fBytes -= chunk->capacity; fInFlightChunks.Add(chunk); release_sem(fWaitSem); } @@ -113,10 +114,11 @@ ChunkCache::ReadNextChunk(Reader* reader, void* cookie) chunk_buffer* chunk = fUnusedChunks.RemoveHead(); if (chunk == NULL) { // allocate a new one - chunk = new(std::nothrow) chunk_buffer; + chunk = (chunk_buffer*)rtm_alloc(fRealTimePool, sizeof(chunk_buffer)); if (chunk == NULL) return false; + new(chunk) chunk_buffer; } const void* buffer; @@ -126,18 +128,17 @@ ChunkCache::ReadNextChunk(Reader* reader, void* cookie) if (chunk->status == B_OK) { if (chunk->capacity < bufferSize) { // adapt buffer size - free(chunk->buffer); + rtm_free(chunk->buffer); chunk->capacity = (bufferSize + 2047) & ~2047; - chunk->buffer = malloc(chunk->capacity); + chunk->buffer = rtm_alloc(fRealTimePool, chunk->capacity); if (chunk->buffer == NULL) { - delete chunk; + rtm_free(chunk); return false; } } memcpy(chunk->buffer, buffer, bufferSize); chunk->size = bufferSize; - fBytes += chunk->capacity; } fChunks.Add(chunk); diff --git a/src/kits/media/ChunkCache.h b/src/kits/media/ChunkCache.h index d533caa828..3b912c6076 100644 --- a/src/kits/media/ChunkCache.h +++ b/src/kits/media/ChunkCache.h @@ -8,10 +8,12 @@ #include #include -#include "ReaderPlugin.h" +#include #include +#include "ReaderPlugin.h" + namespace BPrivate { namespace media { @@ -37,6 +39,8 @@ public: ChunkCache(sem_id waitSem, size_t maxBytes); ~ChunkCache(); + status_t InitCheck() const; + void MakeEmpty(); bool SpaceLeft() const; @@ -45,9 +49,9 @@ public: bool ReadNextChunk(Reader* reader, void* cookie); private: + rtm_pool* fRealTimePool; sem_id fWaitSem; size_t fMaxBytes; - size_t fBytes; ChunkList fChunks; ChunkList fUnusedChunks; ChunkList fInFlightChunks; diff --git a/src/kits/media/Jamfile b/src/kits/media/Jamfile index 1df62491c6..fdc7ab82b7 100644 --- a/src/kits/media/Jamfile +++ b/src/kits/media/Jamfile @@ -73,7 +73,7 @@ SharedLibrary libmedia.so : OldMediaModule.cpp OldSoundFile.cpp OldSubscriber.cpp - + # Codec Plugin API ChunkCache.cpp DecoderPlugin.cpp diff --git a/src/kits/media/MediaExtractor.cpp b/src/kits/media/MediaExtractor.cpp index 0ece3736c5..84d35ccda1 100644 --- a/src/kits/media/MediaExtractor.cpp +++ b/src/kits/media/MediaExtractor.cpp @@ -90,6 +90,11 @@ MediaExtractor::MediaExtractor(BDataIO* source, int32 flags) fStreamInfo[i].lastChunk = NULL; memset(&fStreamInfo[i].encodedFormat, 0, sizeof(fStreamInfo[i].encodedFormat)); + + if (fStreamInfo[i].chunkCache->InitCheck() != B_OK) { + fInitStatus = B_NO_MEMORY; + return; + } } // create all stream cookies