1*218a8c03SAugustin Cavalier /* 2*218a8c03SAugustin Cavalier * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. 3*218a8c03SAugustin Cavalier * Distributed under the terms of the MIT License. 4*218a8c03SAugustin Cavalier */ 5*218a8c03SAugustin Cavalier #ifndef _CHUNK_CACHE_H 6*218a8c03SAugustin Cavalier #define _CHUNK_CACHE_H 7*218a8c03SAugustin Cavalier 8*218a8c03SAugustin Cavalier 9*218a8c03SAugustin Cavalier #include <Locker.h> 10*218a8c03SAugustin Cavalier #include <MediaDefs.h> 11*218a8c03SAugustin Cavalier #include <RealtimeAlloc.h> 12*218a8c03SAugustin Cavalier #include <queue> 13*218a8c03SAugustin Cavalier #include <deque> 14*218a8c03SAugustin Cavalier 15*218a8c03SAugustin Cavalier #include "ReaderPlugin.h" 16*218a8c03SAugustin Cavalier 17*218a8c03SAugustin Cavalier 18*218a8c03SAugustin Cavalier namespace BPrivate { 19*218a8c03SAugustin Cavalier namespace media { 20*218a8c03SAugustin Cavalier 21*218a8c03SAugustin Cavalier // Limit to 10 entries, we might want to instead limit to a length of time 22*218a8c03SAugustin Cavalier #define CACHE_MAX_ENTRIES 10 23*218a8c03SAugustin Cavalier 24*218a8c03SAugustin Cavalier struct chunk_buffer { 25*218a8c03SAugustin Cavalier void* buffer; 26*218a8c03SAugustin Cavalier size_t size; 27*218a8c03SAugustin Cavalier size_t capacity; 28*218a8c03SAugustin Cavalier media_header header; 29*218a8c03SAugustin Cavalier status_t status; 30*218a8c03SAugustin Cavalier }; 31*218a8c03SAugustin Cavalier 32*218a8c03SAugustin Cavalier typedef std::queue<chunk_buffer*> ChunkQueue; 33*218a8c03SAugustin Cavalier typedef std::deque<chunk_buffer*> ChunkList; 34*218a8c03SAugustin Cavalier 35*218a8c03SAugustin Cavalier class ChunkCache : public BLocker { 36*218a8c03SAugustin Cavalier public: 37*218a8c03SAugustin Cavalier ChunkCache(sem_id waitSem, size_t maxBytes); 38*218a8c03SAugustin Cavalier ~ChunkCache(); 39*218a8c03SAugustin Cavalier 40*218a8c03SAugustin Cavalier status_t InitCheck() const; 41*218a8c03SAugustin Cavalier 42*218a8c03SAugustin Cavalier void MakeEmpty(); 43*218a8c03SAugustin Cavalier bool SpaceLeft() const; 44*218a8c03SAugustin Cavalier 45*218a8c03SAugustin Cavalier chunk_buffer* NextChunk(Reader* reader, void* cookie); 46*218a8c03SAugustin Cavalier void RecycleChunk(chunk_buffer* chunk); 47*218a8c03SAugustin Cavalier bool ReadNextChunk(Reader* reader, void* cookie); 48*218a8c03SAugustin Cavalier 49*218a8c03SAugustin Cavalier private: 50*218a8c03SAugustin Cavalier rtm_pool* fRealTimePool; 51*218a8c03SAugustin Cavalier sem_id fWaitSem; 52*218a8c03SAugustin Cavalier size_t fMaxBytes; 53*218a8c03SAugustin Cavalier ChunkQueue fChunkCache; 54*218a8c03SAugustin Cavalier ChunkList fUnusedChunks; 55*218a8c03SAugustin Cavalier }; 56*218a8c03SAugustin Cavalier 57*218a8c03SAugustin Cavalier 58*218a8c03SAugustin Cavalier } // namespace media 59*218a8c03SAugustin Cavalier } // namespace BPrivate 60*218a8c03SAugustin Cavalier 61*218a8c03SAugustin Cavalier using namespace BPrivate::media; 62*218a8c03SAugustin Cavalier 63*218a8c03SAugustin Cavalier #endif // _CHUNK_CACHE_H 64