| 1 |
#ifndef AVIFILE_CACHE_H |
| 2 |
#define AVIFILE_CACHE_H |
| 3 |
|
| 4 |
#define _LARGEFILE64_SOURCE |
| 5 |
#ifdef AVIFILE_AVIFMT_H |
| 6 |
#error Cache.h has to be the first inlude file you use!!! |
| 7 |
#endif |
| 8 |
|
| 9 |
// These classes provide basic caching abilities for files |
| 10 |
// in AVI format. This is useful when files are played |
| 11 |
// over NFS or SMB. Relies heavily on AVI structure. |
| 12 |
|
| 13 |
#include "avifmt.h" |
| 14 |
#include "ReadHandlers.h" |
| 15 |
#include "avm_locker.h" |
| 16 |
#include "avm_stl.h" |
| 17 |
#include <sys/types.h> // size_t |
| 18 |
|
| 19 |
AVM_BEGIN_NAMESPACE; |
| 20 |
|
| 21 |
class Cache |
| 22 |
{ |
| 23 |
public: |
| 24 |
// FIXME: add methods for setting/getting size of cache |
| 25 |
Cache(size_t size = CACHE_SIZE); |
| 26 |
~Cache(); |
| 27 |
//int addStream(uint_t id, const avm::vector<AVIINDEXENTRY2>& table); |
| 28 |
int addStream(uint_t id, const avm::vector<uint32_t>& table); |
| 29 |
int clear(); |
| 30 |
int create(int fd); |
| 31 |
double getSize(); |
| 32 |
uint_t pickChunk(); |
| 33 |
StreamPacket* readPacket(uint_t id, framepos_t pos); |
| 34 |
|
| 35 |
protected: |
| 36 |
|
| 37 |
struct StreamEntry |
| 38 |
{ |
| 39 |
const avm::vector<uint32_t>* table; |
| 40 |
avm::qring<StreamPacket*> packets; |
| 41 |
uint_t position; // needed chunk |
| 42 |
|
| 43 |
// handled internaly |
| 44 |
size_t sum; // cached bytes in stream |
| 45 |
off_t offset; |
| 46 |
uint_t last; |
| 47 |
uint_t error; |
| 48 |
bool filling; |
| 49 |
static const uint_t OK = ~0U; |
| 50 |
//StreamEntry(const avm::vector<AVIINDEXENTRY2>* t = 0, |
| 51 |
StreamEntry(const avm::vector<uint32_t>* t = 0, |
| 52 |
uint_t pos = 0, size_t size = CACHE_SIZE) |
| 53 |
:table(t), packets(size), position(pos), sum(0), |
| 54 |
offset(0), last(OK), error(OK), filling(false) {} |
| 55 |
}; |
| 56 |
|
| 57 |
void* threadfunc(); // main thread for cache handling |
| 58 |
inline bool isCachable(const StreamEntry& stream, uint_t id) const; // new data could be readed |
| 59 |
|
| 60 |
static void* startThreadfunc(void* arg); |
| 61 |
|
| 62 |
static const uint_t WAIT = ~0U; |
| 63 |
static const size_t CACHE_SIZE = 300; |
| 64 |
|
| 65 |
size_t m_uiSize; // preallocated chunks for one stream |
| 66 |
avm::vector<StreamEntry> m_streams; |
| 67 |
|
| 68 |
PthreadTask* thread; |
| 69 |
PthreadMutex mutex; |
| 70 |
PthreadCond cond; |
| 71 |
StreamPacket* m_pPacket; |
| 72 |
int m_iFd; // cached file descritor |
| 73 |
uint_t m_uiId; // currently processed stream |
| 74 |
uint_t cache_access; // total amount of cache access |
| 75 |
uint_t cache_right; // accesses satisfied by cache |
| 76 |
uint_t cache_miss; // how many times we had to wait |
| 77 |
bool m_bQuit; // quit caching thread |
| 78 |
bool m_bThreaded; |
| 79 |
}; |
| 80 |
|
| 81 |
class InputStream |
| 82 |
{ |
| 83 |
public: |
| 84 |
InputStream(); |
| 85 |
~InputStream(); |
| 86 |
int open(const char* file); |
| 87 |
void close(); |
| 88 |
off_t seek(off_t offset); |
| 89 |
off_t seekCur(off_t offset); |
| 90 |
off_t pos() const; |
| 91 |
off_t len() const; |
| 92 |
bool eof() const { return m_bEof; } |
| 93 |
|
| 94 |
//enter asynchronous mode |
| 95 |
int async(); |
| 96 |
|
| 97 |
//int addStream(uint_t id, const avm::vector<AVIINDEXENTRY2>& table) |
| 98 |
int addStream(uint_t id, const avm::vector<uint32_t>& table); |
| 99 |
ssize_t read(void* buffer, size_t size); |
| 100 |
uint8_t readByte(); |
| 101 |
uint32_t readDword() { return readWord() | (readWord() << 16); } |
| 102 |
uint16_t readWord() { return (uint16_t) (readByte() | (readByte() << 8)); } |
| 103 |
|
| 104 |
//from cache: |
| 105 |
StreamPacket* readPacket(uint_t id, framepos_t fpos) |
| 106 |
{ |
| 107 |
return (m_pCache) ? m_pCache->readPacket(id, fpos) : 0; |
| 108 |
} |
| 109 |
|
| 110 |
int clear() { return (m_pCache) ? m_pCache->clear() : -1; } |
| 111 |
double cacheSize() const { return (m_pCache) ? m_pCache->getSize() : 0.0; } |
| 112 |
protected: |
| 113 |
static const uint_t BFRSIZE; |
| 114 |
int m_iFd; |
| 115 |
Cache* m_pCache; |
| 116 |
ssize_t m_iPos; |
| 117 |
ssize_t m_iBuffered; |
| 118 |
bool m_bEof; |
| 119 |
uint8_t* m_pBuffer; |
| 120 |
}; |
| 121 |
|
| 122 |
AVM_END_NAMESPACE; |
| 123 |
|
| 124 |
#endif // AVIFILE_CACHE_H |