1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* Copyright (C) 2005-2014 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "threads/SystemClock.h"
#include "CacheStrategy.h"
#include "IFile.h"
#include "Util.h"
#include "utils/log.h"
#include "SpecialProtocol.h"
#include "PlatformDefs.h" //for PRIdS, PRId64
#include "URL.h"
#if defined(TARGET_POSIX)
#include "posix/PosixFile.h"
#define CacheLocalFile CPosixFile
#elif defined(TARGET_WINDOWS)
#include "win32/Win32File.h"
#define CacheLocalFile CWin32File
#endif // TARGET_WINDOWS
#include <cassert>
#include <algorithm>
using namespace XFILE;
CCacheStrategy::CCacheStrategy() : m_bEndOfInput(false)
{
}
CCacheStrategy::~CCacheStrategy()
{
}
void CCacheStrategy::EndOfInput() {
m_bEndOfInput = true;
}
bool CCacheStrategy::IsEndOfInput()
{
return m_bEndOfInput;
}
void CCacheStrategy::ClearEndOfInput()
{
m_bEndOfInput = false;
}
CSimpleFileCache::CSimpleFileCache()
: m_cacheFileRead(new CacheLocalFile())
, m_cacheFileWrite(new CacheLocalFile())
, m_hDataAvailEvent(NULL)
, m_nStartPosition(0)
, m_nWritePosition(0)
, m_nReadPosition(0) {
}
CSimpleFileCache::~CSimpleFileCache()
{
Close();
delete m_cacheFileRead;
delete m_cacheFileWrite;
}
int CSimpleFileCache::Open()
{
Close();
m_hDataAvailEvent = new CEvent;
m_filename = CSpecialProtocol::TranslatePath(CUtil::GetNextFilename("special://temp/filecache%03d.cache", 999));
if (m_filename.empty())
{
CLog::Log(LOGERROR, "%s - Unable to generate a new filename", __FUNCTION__);
Close();
return CACHE_RC_ERROR;
}
CURL fileURL(m_filename);
if (!m_cacheFileWrite->OpenForWrite(fileURL, false))
{
CLog::LogF(LOGERROR, "failed to create file \"%s\" for writing", m_filename.c_str());
Close();
return CACHE_RC_ERROR;
}
if (!m_cacheFileRead->Open(fileURL))
{
CLog::LogF(LOGERROR, "failed to open file \"%s\" for reading", m_filename.c_str());
Close();
return CACHE_RC_ERROR;
}
return CACHE_RC_OK;
}
void CSimpleFileCache::Close()
{
if (m_hDataAvailEvent)
delete m_hDataAvailEvent;
m_hDataAvailEvent = NULL;
m_cacheFileWrite->Close();
m_cacheFileRead->Close();
if (!m_filename.empty() && !m_cacheFileRead->Delete(CURL(m_filename)))
CLog::LogF(LOGWARNING, "failed to delete temporary file \"%s\"", m_filename.c_str());
m_filename.clear();
}
size_t CSimpleFileCache::GetMaxWriteSize(const size_t& iRequestSize)
{
return iRequestSize; // Can always write since it's on disk
}
int CSimpleFileCache::WriteToCache(const char *pBuffer, size_t iSize)
{
size_t written = 0;
while (iSize > 0)
{
const ssize_t lastWritten = m_cacheFileWrite->Write(pBuffer, (iSize > SSIZE_MAX) ? SSIZE_MAX : iSize);
if (lastWritten <= 0)
{
CLog::LogF(LOGERROR, "failed to write to file");
return CACHE_RC_ERROR;
}
m_nWritePosition += lastWritten;
iSize -= lastWritten;
written += lastWritten;
}
// when reader waits for data it will wait on the event.
m_hDataAvailEvent->Set();
return written;
}
int64_t CSimpleFileCache::GetAvailableRead()
{
return m_nWritePosition - m_nReadPosition;
}
int CSimpleFileCache::ReadFromCache(char *pBuffer, size_t iMaxSize)
{
int64_t iAvailable = GetAvailableRead();
if ( iAvailable <= 0 )
return m_bEndOfInput? 0 : CACHE_RC_WOULD_BLOCK;
size_t toRead = ((int64_t)iMaxSize > iAvailable) ? (size_t)iAvailable : iMaxSize;
size_t readBytes = 0;
while (toRead > 0)
{
const ssize_t lastRead = m_cacheFileRead->Read(pBuffer, (toRead > SSIZE_MAX) ? SSIZE_MAX : toRead);
if (lastRead == 0)
break;
if (lastRead < 0)
{
CLog::LogF(LOGERROR, "failed to read from file");
return CACHE_RC_ERROR;
}
m_nReadPosition += lastRead;
toRead -= lastRead;
readBytes += lastRead;
}
if (readBytes > 0)
m_space.Set();
return readBytes;
}
int64_t CSimpleFileCache::WaitForData(unsigned int iMinAvail, unsigned int iMillis)
{
if( iMillis == 0 || IsEndOfInput() )
return GetAvailableRead();
XbmcThreads::EndTime endTime(iMillis);
while (!IsEndOfInput())
{
int64_t iAvail = GetAvailableRead();
if (iAvail >= iMinAvail)
return iAvail;
if (!m_hDataAvailEvent->WaitMSec(endTime.MillisLeft()))
return CACHE_RC_TIMEOUT;
}
return GetAvailableRead();
}
int64_t CSimpleFileCache::Seek(int64_t iFilePosition)
{
int64_t iTarget = iFilePosition - m_nStartPosition;
if (iTarget < 0)
{
CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek, request seek before start of cache.");
return CACHE_RC_ERROR;
}
int64_t nDiff = iTarget - m_nWritePosition;
if (nDiff > 500000 || (nDiff > 0 && WaitForData((unsigned int)(iTarget - m_nReadPosition), 5000) == CACHE_RC_TIMEOUT))
{
CLog::Log(LOGDEBUG,"CSimpleFileCache::Seek - Attempt to seek past read data");
return CACHE_RC_ERROR;
}
m_nReadPosition = m_cacheFileRead->Seek(iTarget, SEEK_SET);
if (m_nReadPosition != iTarget)
{
CLog::LogF(LOGERROR, "can't seek file");
return CACHE_RC_ERROR;
}
m_space.Set();
return iFilePosition;
}
bool CSimpleFileCache::Reset(int64_t iSourcePosition, bool clearAnyway)
{
if (!clearAnyway && IsCachedPosition(iSourcePosition))
{
m_nReadPosition = m_cacheFileRead->Seek(iSourcePosition - m_nStartPosition, SEEK_SET);
return false;
}
m_nStartPosition = iSourcePosition;
m_nWritePosition = m_cacheFileWrite->Seek(0, SEEK_SET);
m_nReadPosition = m_cacheFileRead->Seek(0, SEEK_SET);
return true;
}
void CSimpleFileCache::EndOfInput()
{
CCacheStrategy::EndOfInput();
m_hDataAvailEvent->Set();
}
int64_t CSimpleFileCache::CachedDataEndPosIfSeekTo(int64_t iFilePosition)
{
if (iFilePosition >= m_nStartPosition && iFilePosition <= m_nStartPosition + m_nWritePosition)
return m_nStartPosition + m_nWritePosition;
return iFilePosition;
}
int64_t CSimpleFileCache::CachedDataEndPos()
{
return m_nStartPosition + m_nWritePosition;
}
bool CSimpleFileCache::IsCachedPosition(int64_t iFilePosition)
{
return iFilePosition >= m_nStartPosition && iFilePosition <= m_nStartPosition + m_nWritePosition;
}
CCacheStrategy *CSimpleFileCache::CreateNew()
{
return new CSimpleFileCache();
}
CDoubleCache::CDoubleCache(CCacheStrategy *impl)
{
assert(NULL != impl);
m_pCache = impl;
m_pCacheOld = NULL;
}
CDoubleCache::~CDoubleCache()
{
delete m_pCache;
delete m_pCacheOld;
}
int CDoubleCache::Open()
{
return m_pCache->Open();
}
void CDoubleCache::Close()
{
m_pCache->Close();
if (m_pCacheOld)
{
delete m_pCacheOld;
m_pCacheOld = NULL;
}
}
size_t CDoubleCache::GetMaxWriteSize(const size_t& iRequestSize)
{
return m_pCache->GetMaxWriteSize(iRequestSize); // NOTE: Check the active cache only
}
int CDoubleCache::WriteToCache(const char *pBuffer, size_t iSize)
{
return m_pCache->WriteToCache(pBuffer, iSize);
}
int CDoubleCache::ReadFromCache(char *pBuffer, size_t iMaxSize)
{
return m_pCache->ReadFromCache(pBuffer, iMaxSize);
}
int64_t CDoubleCache::WaitForData(unsigned int iMinAvail, unsigned int iMillis)
{
return m_pCache->WaitForData(iMinAvail, iMillis);
}
int64_t CDoubleCache::Seek(int64_t iFilePosition)
{
/* Check whether position is NOT in our current cache but IS in our old cache.
* This is faster/more efficient than having to possibly wait for data in the
* Seek() call below
*/
if (!m_pCache->IsCachedPosition(iFilePosition) &&
m_pCacheOld && m_pCacheOld->IsCachedPosition(iFilePosition))
{
return CACHE_RC_ERROR; // Request seek event, so caches are swapped
}
return m_pCache->Seek(iFilePosition); // Normal seek
}
bool CDoubleCache::Reset(int64_t iSourcePosition, bool clearAnyway)
{
if (!clearAnyway && m_pCache->IsCachedPosition(iSourcePosition)
&& (!m_pCacheOld || !m_pCacheOld->IsCachedPosition(iSourcePosition)
|| m_pCache->CachedDataEndPos() >= m_pCacheOld->CachedDataEndPos()))
{
return m_pCache->Reset(iSourcePosition, clearAnyway);
}
if (!m_pCacheOld)
{
CCacheStrategy *pCacheNew = m_pCache->CreateNew();
if (pCacheNew->Open() != CACHE_RC_OK)
{
delete pCacheNew;
return m_pCache->Reset(iSourcePosition, clearAnyway);
}
bool bRes = pCacheNew->Reset(iSourcePosition, clearAnyway);
m_pCacheOld = m_pCache;
m_pCache = pCacheNew;
return bRes;
}
bool bRes = m_pCacheOld->Reset(iSourcePosition, clearAnyway);
CCacheStrategy *tmp = m_pCacheOld;
m_pCacheOld = m_pCache;
m_pCache = tmp;
return bRes;
}
void CDoubleCache::EndOfInput()
{
m_pCache->EndOfInput();
}
bool CDoubleCache::IsEndOfInput()
{
return m_pCache->IsEndOfInput();
}
void CDoubleCache::ClearEndOfInput()
{
m_pCache->ClearEndOfInput();
}
int64_t CDoubleCache::CachedDataEndPos()
{
return m_pCache->CachedDataEndPos();
}
int64_t CDoubleCache::CachedDataEndPosIfSeekTo(int64_t iFilePosition)
{
int64_t ret = m_pCache->CachedDataEndPosIfSeekTo(iFilePosition);
if (m_pCacheOld)
return std::max(ret, m_pCacheOld->CachedDataEndPosIfSeekTo(iFilePosition));
return ret;
}
bool CDoubleCache::IsCachedPosition(int64_t iFilePosition)
{
return m_pCache->IsCachedPosition(iFilePosition) || (m_pCacheOld && m_pCacheOld->IsCachedPosition(iFilePosition));
}
CCacheStrategy *CDoubleCache::CreateNew()
{
return new CDoubleCache(m_pCache->CreateNew());
}
|