| 1 |
/* |
| 2 |
* Copyright (C) 2005-2012 Team Kodi |
| 3 |
* https://kodi.tv |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
************************************************************************* |
| 19 |
* This file is a modified version from Team MediaPortal's |
| 20 |
* TsReader DirectShow filter |
| 21 |
* MediaPortal is a GPL'ed HTPC-Application |
| 22 |
* Copyright (C) 2005-2012 Team MediaPortal |
| 23 |
* http://www.team-mediaportal.com |
| 24 |
* |
| 25 |
* Changes compared to Team MediaPortal's version: |
| 26 |
* - Code cleanup for PVR addon usage |
| 27 |
* - Code refactoring for cross platform usage |
| 28 |
************************************************************************* |
| 29 |
* This file originates from TSFileSource, a GPL directshow push |
| 30 |
* source filter that provides an MPEG transport stream output. |
| 31 |
* Copyright (C) 2005-2006 nate, bear |
| 32 |
* http://forums.dvbowners.com/ |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "MultiFileReader.h" |
| 36 |
#include <kodi/General.h> //for kodi::Log |
| 37 |
#include <kodi/Filesystem.h> |
| 38 |
#include <kodi/tools/EndTime.h> |
| 39 |
#include "TSDebug.h" |
| 40 |
#include <string> |
| 41 |
#include "utils.h" |
| 42 |
#include <algorithm> |
| 43 |
#include <inttypes.h> |
| 44 |
#include "os-dependent.h" |
| 45 |
|
| 46 |
#include <thread> |
| 47 |
|
| 48 |
//Maximum time in msec to wait for the buffer file to become available - Needed for DVB radio (this sometimes takes some time) |
| 49 |
#define MAX_BUFFER_TIMEOUT 1500 |
| 50 |
|
| 51 |
template<typename T> void SafeDelete(T*& p) |
| 52 |
{ |
| 53 |
if (p) |
| 54 |
{ |
| 55 |
delete p; |
| 56 |
p = nullptr; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
namespace MPTV |
| 61 |
{ |
| 62 |
MultiFileReader::MultiFileReader() : |
| 63 |
m_TSBufferFile(), |
| 64 |
m_TSFile(), |
| 65 |
m_startPosition(0), |
| 66 |
m_currentFileStartOffset(0), |
| 67 |
m_endPosition(0), |
| 68 |
m_currentPosition(0), |
| 69 |
m_lastZapPosition(0), |
| 70 |
m_filesAdded(0), |
| 71 |
m_filesRemoved(0), |
| 72 |
m_TSFileId(0), |
| 73 |
m_bDelay(0) |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
MultiFileReader::~MultiFileReader() |
| 78 |
{ |
| 79 |
//CloseFile called by ~FileReader |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
long MultiFileReader::SetFileName(const std::string& fileName) |
| 84 |
{ |
| 85 |
return m_TSBufferFile.SetFileName(fileName); |
| 86 |
} |
| 87 |
|
| 88 |
long MultiFileReader::OpenFile(const std::string& fileName) |
| 89 |
{ |
| 90 |
SetFileName(fileName); |
| 91 |
return OpenFile(); |
| 92 |
} |
| 93 |
|
| 94 |
// |
| 95 |
// OpenFile |
| 96 |
// |
| 97 |
long MultiFileReader::OpenFile() |
| 98 |
{ |
| 99 |
// The underlying CFileStreamBuffer class used by kodi::vfs::CFile.Open() appears |
| 100 |
// to be incompatible with the TS buffer files created by the TvServer, so disable |
| 101 |
// the buffering. |
| 102 |
m_TSBufferFile.SetNoBuffer(); |
| 103 |
|
| 104 |
long hResult = m_TSBufferFile.OpenFile(); |
| 105 |
kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: buffer file opened return code %d.", hResult); |
| 106 |
|
| 107 |
if (hResult != S_OK) |
| 108 |
return hResult; |
| 109 |
|
| 110 |
m_lastZapPosition = 0; |
| 111 |
m_currentFileStartOffset = 0; |
| 112 |
|
| 113 |
int retryCount = 0; |
| 114 |
|
| 115 |
while ((m_TSBufferFile.GetFileSize() == 0) && (retryCount < 50)) |
| 116 |
{ |
| 117 |
retryCount++; |
| 118 |
kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: buffer file has zero length, closing, waiting 100 ms and re-opening. Attempt: %d.", retryCount); |
| 119 |
m_TSBufferFile.CloseFile(); |
| 120 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 121 |
hResult = m_TSBufferFile.OpenFile(); |
| 122 |
kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: buffer file opened return code %d.", hResult); |
| 123 |
} |
| 124 |
|
| 125 |
if (RefreshTSBufferFile() == S_FALSE) |
| 126 |
{ |
| 127 |
// For radio the buffer sometimes needs some time to become available, so wait and try it more than once |
| 128 |
kodi::tools::CEndTime timeout(MAX_BUFFER_TIMEOUT); |
| 129 |
|
| 130 |
do |
| 131 |
{ |
| 132 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 133 |
if (timeout.MillisLeft() == 0) |
| 134 |
{ |
| 135 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader: timed out while waiting for buffer file to become available"); |
| 136 |
kodi::QueueNotification(QUEUE_ERROR, "", "Time out while waiting for buffer file"); |
| 137 |
return S_FALSE; |
| 138 |
} |
| 139 |
} while (RefreshTSBufferFile() == S_FALSE); |
| 140 |
} |
| 141 |
|
| 142 |
m_currentPosition = 0; |
| 143 |
|
| 144 |
return hResult; |
| 145 |
} |
| 146 |
|
| 147 |
// |
| 148 |
// CloseFile |
| 149 |
// |
| 150 |
long MultiFileReader::CloseFile() |
| 151 |
{ |
| 152 |
long hr; |
| 153 |
std::vector<MultiFileReaderFile *>::iterator it; |
| 154 |
|
| 155 |
m_TSBufferFile.CloseFile(); |
| 156 |
hr = m_TSFile.CloseFile(); |
| 157 |
|
| 158 |
for (it = m_tsFiles.begin(); it < m_tsFiles.end(); ++it) |
| 159 |
{ |
| 160 |
delete (*it); |
| 161 |
} |
| 162 |
m_tsFiles.clear(); |
| 163 |
|
| 164 |
m_TSFileId = 0; |
| 165 |
return hr; |
| 166 |
} |
| 167 |
|
| 168 |
bool MultiFileReader::IsFileInvalid() |
| 169 |
{ |
| 170 |
return m_TSBufferFile.IsFileInvalid(); |
| 171 |
} |
| 172 |
|
| 173 |
int64_t MultiFileReader::SetFilePointer(int64_t llDistanceToMove, unsigned long dwMoveMethod) |
| 174 |
{ |
| 175 |
RefreshTSBufferFile(); |
| 176 |
|
| 177 |
if (dwMoveMethod == FILE_END) |
| 178 |
{ |
| 179 |
m_currentPosition = m_endPosition + llDistanceToMove; |
| 180 |
} |
| 181 |
else if (dwMoveMethod == FILE_CURRENT) |
| 182 |
{ |
| 183 |
m_currentPosition += llDistanceToMove; |
| 184 |
} |
| 185 |
else // if (dwMoveMethod == FILE_BEGIN) |
| 186 |
{ |
| 187 |
m_currentPosition = m_startPosition + llDistanceToMove; |
| 188 |
} |
| 189 |
|
| 190 |
if (m_currentPosition < m_startPosition) |
| 191 |
m_currentPosition = m_startPosition; |
| 192 |
|
| 193 |
if (m_currentPosition > m_endPosition) |
| 194 |
{ |
| 195 |
kodi::Log(ADDON_LOG_ERROR, "Seeking beyond the end position: %I64d > %I64d", m_currentPosition, m_endPosition); |
| 196 |
m_currentPosition = m_endPosition; |
| 197 |
} |
| 198 |
|
| 199 |
return m_currentPosition; |
| 200 |
} |
| 201 |
|
| 202 |
int64_t MultiFileReader::SetCurrentFilePointer(int64_t timeShiftBufferFilePos, long timeshiftBufferFileID) |
| 203 |
{ |
| 204 |
RefreshTSBufferFile(); |
| 205 |
|
| 206 |
if (m_TSFileId != timeshiftBufferFileID) |
| 207 |
{ |
| 208 |
// We have to switch to a different buffer file |
| 209 |
TSDEBUG(ADDON_LOG_DEBUG, "Change buffer file from %i to %i", m_TSFileId, timeshiftBufferFileID); |
| 210 |
|
| 211 |
MultiFileReaderFile *file = NULL; |
| 212 |
std::vector<MultiFileReaderFile *>::iterator it = m_tsFiles.begin(); |
| 213 |
for (; it < m_tsFiles.end(); ++it) |
| 214 |
{ |
| 215 |
file = *it; |
| 216 |
if (file->filePositionId == timeshiftBufferFileID) |
| 217 |
break; |
| 218 |
}; |
| 219 |
|
| 220 |
if (!file) |
| 221 |
{ |
| 222 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader::no buffer file with id=%i", timeshiftBufferFileID); |
| 223 |
kodi::QueueNotification(QUEUE_ERROR, "", "No buffer file"); |
| 224 |
return m_currentPosition; |
| 225 |
} |
| 226 |
|
| 227 |
if (m_currentPosition < (file->startPosition + timeShiftBufferFilePos)) |
| 228 |
{ |
| 229 |
m_TSFile.CloseFile(); |
| 230 |
m_TSFile.SetFileName(file->filename.c_str()); |
| 231 |
m_TSFile.OpenFile(); |
| 232 |
|
| 233 |
m_TSFileId = file->filePositionId; |
| 234 |
m_currentFileStartOffset = file->startPosition; |
| 235 |
|
| 236 |
TSDEBUG(ADDON_LOG_DEBUG, "MultiFileReader::Read() Current File Changed to %s TS file id=%i\n", file->filename.c_str(), m_TSFileId); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// Reposition the read pointer within the current timeshift buffer file |
| 241 |
TSDEBUG(ADDON_LOG_DEBUG, "Move read pointer within buffer file %i; %I64d -> %i64d", m_TSFileId, m_currentPosition, m_currentFileStartOffset + timeShiftBufferFilePos); |
| 242 |
m_currentPosition = m_currentFileStartOffset + timeShiftBufferFilePos; |
| 243 |
|
| 244 |
if (m_currentPosition > m_endPosition) |
| 245 |
{ |
| 246 |
kodi::Log(ADDON_LOG_ERROR, "Seeking beyond the end position: %I64d > %I64d", m_currentPosition, m_endPosition); |
| 247 |
m_currentPosition = m_endPosition; |
| 248 |
} |
| 249 |
|
| 250 |
return m_currentPosition; |
| 251 |
} |
| 252 |
|
| 253 |
int64_t MultiFileReader::GetFilePointer() |
| 254 |
{ |
| 255 |
return m_currentPosition; |
| 256 |
} |
| 257 |
|
| 258 |
long MultiFileReader::Read(unsigned char* pbData, size_t lDataLength, size_t *dwReadBytes) |
| 259 |
{ |
| 260 |
// If the file has already been closed, don't continue |
| 261 |
if (m_TSBufferFile.IsFileInvalid()) |
| 262 |
return S_FALSE; |
| 263 |
|
| 264 |
RefreshTSBufferFile(); |
| 265 |
|
| 266 |
if (m_currentPosition < m_startPosition) |
| 267 |
{ |
| 268 |
kodi::Log(ADDON_LOG_INFO, "%s: current position adjusted from %%I64dd to %%I64dd.", __FUNCTION__, m_currentPosition, m_startPosition); |
| 269 |
m_currentPosition = m_startPosition; |
| 270 |
} |
| 271 |
|
| 272 |
// Find out which file the currentPosition is in. |
| 273 |
MultiFileReaderFile *file = NULL; |
| 274 |
std::vector<MultiFileReaderFile *>::iterator it = m_tsFiles.begin(); |
| 275 |
for (; it < m_tsFiles.end(); ++it) |
| 276 |
{ |
| 277 |
file = *it; |
| 278 |
if (m_currentPosition < (file->startPosition + file->length)) |
| 279 |
break; |
| 280 |
}; |
| 281 |
|
| 282 |
// kodi::Log(ADDON_LOG_DEBUG, "%s: reading %ld bytes. File %s, start %d, current %d, end %d.", __FUNCTION__, lDataLength, file->filename.c_str(), m_startPosition, m_currentPosition, m_endPosition); |
| 283 |
|
| 284 |
if (!file) |
| 285 |
{ |
| 286 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader::no file"); |
| 287 |
kodi::QueueNotification(QUEUE_ERROR, "", "No buffer file"); |
| 288 |
return S_FALSE; |
| 289 |
} |
| 290 |
if (m_currentPosition < (file->startPosition + file->length)) |
| 291 |
{ |
| 292 |
if (m_TSFileId != file->filePositionId) |
| 293 |
{ |
| 294 |
m_TSFile.CloseFile(); |
| 295 |
m_TSFile.SetFileName(file->filename.c_str()); |
| 296 |
if (m_TSFile.OpenFile() != S_OK) |
| 297 |
{ |
| 298 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader: can't open %s\n", file->filename.c_str()); |
| 299 |
return S_FALSE; |
| 300 |
} |
| 301 |
|
| 302 |
m_TSFileId = file->filePositionId; |
| 303 |
m_currentFileStartOffset = file->startPosition; |
| 304 |
|
| 305 |
TSDEBUG(ADDON_LOG_DEBUG, "MultiFileReader::Read() Current File Changed to %s TS file id=%i\n", file->filename.c_str(), m_TSFileId); |
| 306 |
} |
| 307 |
|
| 308 |
int64_t seekPosition = m_currentPosition - file->startPosition; |
| 309 |
|
| 310 |
m_TSFile.SetFilePointer(seekPosition, FILE_BEGIN); |
| 311 |
int64_t posSeeked = m_TSFile.GetFilePointer(); |
| 312 |
if (posSeeked != seekPosition) |
| 313 |
{ |
| 314 |
m_TSFile.SetFilePointer(seekPosition, FILE_BEGIN); |
| 315 |
posSeeked = m_TSFile.GetFilePointer(); |
| 316 |
if (posSeeked != seekPosition) |
| 317 |
{ |
| 318 |
kodi::Log(ADDON_LOG_ERROR, "SEEK FAILED"); |
| 319 |
return S_FALSE; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
size_t bytesRead = 0; |
| 324 |
long hr; |
| 325 |
|
| 326 |
int64_t bytesToRead = file->length - seekPosition; |
| 327 |
if ((int64_t)lDataLength > bytesToRead) |
| 328 |
{ |
| 329 |
// kodi::Log(ADDON_LOG_DEBUG, "%s: datalength %lu bytesToRead %lli.", __FUNCTION__, lDataLength, bytesToRead); |
| 330 |
hr = m_TSFile.Read(pbData, (size_t)bytesToRead, &bytesRead); |
| 331 |
if (FAILED(hr)) |
| 332 |
{ |
| 333 |
kodi::Log(ADDON_LOG_ERROR, "READ FAILED1"); |
| 334 |
return S_FALSE; |
| 335 |
} |
| 336 |
m_currentPosition += bytesToRead; |
| 337 |
|
| 338 |
hr = this->Read(pbData + bytesToRead, lDataLength - (size_t)bytesToRead, dwReadBytes); |
| 339 |
if (FAILED(hr)) |
| 340 |
{ |
| 341 |
kodi::Log(ADDON_LOG_ERROR, "READ FAILED2"); |
| 342 |
} |
| 343 |
*dwReadBytes += bytesRead; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
hr = m_TSFile.Read(pbData, lDataLength, dwReadBytes); |
| 348 |
if (FAILED(hr)) |
| 349 |
{ |
| 350 |
kodi::Log(ADDON_LOG_ERROR, "READ FAILED3"); |
| 351 |
} |
| 352 |
m_currentPosition += lDataLength; |
| 353 |
} |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
// The current position is past the end of the last file |
| 358 |
*dwReadBytes = 0; |
| 359 |
} |
| 360 |
|
| 361 |
// kodi::Log(ADDON_LOG_DEBUG, "%s: read %lu bytes. start %lli, current %lli, end %lli.", __FUNCTION__, *dwReadBytes, m_startPosition, m_currentPosition, m_endPosition); |
| 362 |
return S_OK; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
long MultiFileReader::RefreshTSBufferFile() |
| 367 |
{ |
| 368 |
if (m_TSBufferFile.IsFileInvalid()) |
| 369 |
{ |
| 370 |
kodi::Log(ADDON_LOG_ERROR, "%s: buffer file is invalid.", __FUNCTION__); |
| 371 |
return S_FALSE; |
| 372 |
} |
| 373 |
|
| 374 |
size_t bytesRead; |
| 375 |
MultiFileReaderFile *file; |
| 376 |
|
| 377 |
int64_t currentPosition; |
| 378 |
int32_t filesAdded, filesRemoved; |
| 379 |
int32_t filesAdded2, filesRemoved2; |
| 380 |
long Error = 0; |
| 381 |
long Loop = 10; |
| 382 |
|
| 383 |
Wchar_t* pBuffer = NULL; |
| 384 |
do |
| 385 |
{ |
| 386 |
Error = 0; |
| 387 |
currentPosition = -1; |
| 388 |
filesAdded = -1; |
| 389 |
filesRemoved = -1; |
| 390 |
filesAdded2 = -2; |
| 391 |
filesRemoved2 = -2; |
| 392 |
|
| 393 |
int64_t fileLength = m_TSBufferFile.GetFileSize(); |
| 394 |
|
| 395 |
// Min file length is Header ( int64_t + int32_t + int32_t ) + filelist ( > 0 ) + Footer ( int32_t + int32_t ) |
| 396 |
int64_t minimumlength = (int64_t)(sizeof(currentPosition) + sizeof(filesAdded) + sizeof(filesRemoved) + sizeof(Wchar_t) + sizeof(filesAdded2) + sizeof(filesRemoved2)); |
| 397 |
if (fileLength <= minimumlength) |
| 398 |
{ |
| 399 |
kodi::Log(ADDON_LOG_ERROR, "%s: TSBufferFile too short. Minimum length %ld, current length %ld", __FUNCTION__, minimumlength, fileLength); |
| 400 |
return S_FALSE; |
| 401 |
} |
| 402 |
|
| 403 |
m_TSBufferFile.SetFilePointer(0, FILE_BEGIN); |
| 404 |
|
| 405 |
size_t readLength = sizeof(currentPosition) + sizeof(filesAdded) + sizeof(filesRemoved); |
| 406 |
unsigned char* readBuffer = new unsigned char[readLength]; |
| 407 |
|
| 408 |
long result = m_TSBufferFile.Read(readBuffer, readLength, &bytesRead); |
| 409 |
|
| 410 |
if (!SUCCEEDED(result) || bytesRead != readLength) |
| 411 |
Error |= 0x02; |
| 412 |
|
| 413 |
if (Error == 0) |
| 414 |
{ |
| 415 |
currentPosition = *((int64_t*)(readBuffer + 0)); |
| 416 |
filesAdded = *((int32_t*)(readBuffer + sizeof(currentPosition))); |
| 417 |
filesRemoved = *((int32_t*)(readBuffer + sizeof(currentPosition) + sizeof(filesAdded))); |
| 418 |
} |
| 419 |
|
| 420 |
delete[] readBuffer; |
| 421 |
|
| 422 |
// If no files added or removed, break the loop ! |
| 423 |
if ((m_filesAdded == filesAdded) && (m_filesRemoved == filesRemoved)) |
| 424 |
break; |
| 425 |
|
| 426 |
int64_t remainingLength = fileLength - sizeof(currentPosition) - sizeof(filesAdded) - sizeof(filesRemoved) - sizeof(filesAdded2) - sizeof(filesRemoved2); |
| 427 |
|
| 428 |
// Above 100kb seems stupid and figure out a problem !!! |
| 429 |
if (remainingLength > 100000) |
| 430 |
Error |= 0x10; |
| 431 |
|
| 432 |
pBuffer = (Wchar_t*) new char[(unsigned int)remainingLength]; |
| 433 |
|
| 434 |
result = m_TSBufferFile.Read((unsigned char*)pBuffer, (uint32_t)remainingLength, &bytesRead); |
| 435 |
if (!SUCCEEDED(result) || (int64_t)bytesRead != remainingLength) |
| 436 |
Error |= 0x20; |
| 437 |
|
| 438 |
readLength = sizeof(filesAdded) + sizeof(filesRemoved); |
| 439 |
|
| 440 |
readBuffer = new unsigned char[readLength]; |
| 441 |
|
| 442 |
result = m_TSBufferFile.Read(readBuffer, readLength, &bytesRead); |
| 443 |
|
| 444 |
if (!SUCCEEDED(result) || bytesRead != readLength) |
| 445 |
Error |= 0x40; |
| 446 |
|
| 447 |
if (Error == 0) |
| 448 |
{ |
| 449 |
filesAdded2 = *((int32_t*)(readBuffer + 0)); |
| 450 |
filesRemoved2 = *((int32_t*)(readBuffer + sizeof(filesAdded2))); |
| 451 |
} |
| 452 |
|
| 453 |
delete[] readBuffer; |
| 454 |
|
| 455 |
if ((filesAdded2 != filesAdded) || (filesRemoved2 != filesRemoved)) |
| 456 |
{ |
| 457 |
Error |= 0x80; |
| 458 |
|
| 459 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader has error 0x%x in Loop %d. Try to clear SMB Cache.", Error, 10 - Loop); |
| 460 |
kodi::Log(ADDON_LOG_DEBUG, "%s: filesAdded %d, filesAdded2 %d, filesRemoved %d, filesRemoved2 %d.", __FUNCTION__, filesAdded, filesAdded2, filesRemoved, filesRemoved2); |
| 461 |
|
| 462 |
// try to clear local / remote SMB file cache. This should happen when we close the filehandle |
| 463 |
m_TSBufferFile.CloseFile(); |
| 464 |
m_TSBufferFile.OpenFile(); |
| 465 |
std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 466 |
} |
| 467 |
|
| 468 |
if (Error) |
| 469 |
delete[] pBuffer; |
| 470 |
|
| 471 |
Loop--; |
| 472 |
} while (Error && Loop); // If Error is set, try again...until Loop reaches 0. |
| 473 |
|
| 474 |
if (Loop < 8) |
| 475 |
{ |
| 476 |
kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader has waited %d times for TSbuffer integrity.", 10 - Loop); |
| 477 |
|
| 478 |
if (Error) |
| 479 |
{ |
| 480 |
kodi::Log(ADDON_LOG_ERROR, "MultiFileReader has failed for TSbuffer integrity. Error : %x", Error); |
| 481 |
return E_FAIL; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
if ((m_filesAdded != filesAdded) || (m_filesRemoved != filesRemoved)) |
| 486 |
{ |
| 487 |
int32_t filesToRemove = filesRemoved - m_filesRemoved; |
| 488 |
int32_t filesToAdd = filesAdded - m_filesAdded; |
| 489 |
int32_t fileID = filesRemoved; |
| 490 |
int64_t nextStartPosition = 0; |
| 491 |
|
| 492 |
TSDEBUG(ADDON_LOG_DEBUG, "MultiFileReader: Files Added %i, Removed %i\n", filesToAdd, filesToRemove); |
| 493 |
|
| 494 |
// Removed files that aren't present anymore. |
| 495 |
while ((filesToRemove > 0) && (!m_tsFiles.empty())) |
| 496 |
{ |
| 497 |
file = m_tsFiles.at(0); |
| 498 |
|
| 499 |
TSDEBUG(ADDON_LOG_DEBUG, "MultiFileReader: Removing file %s\n", file->filename.c_str()); |
| 500 |
|
| 501 |
SafeDelete(file); |
| 502 |
m_tsFiles.erase(m_tsFiles.begin()); |
| 503 |
|
| 504 |
filesToRemove--; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
// Figure out what the start position of the next new file will be |
| 509 |
if (!m_tsFiles.empty()) |
| 510 |
{ |
| 511 |
file = m_tsFiles.back(); |
| 512 |
|
| 513 |
if (filesToAdd > 0) |
| 514 |
{ |
| 515 |
// If we're adding files the changes are the one at the back has a partial length |
| 516 |
// so we need update it. |
| 517 |
GetFileLength(file->filename.c_str(), file->length); |
| 518 |
} |
| 519 |
|
| 520 |
nextStartPosition = file->startPosition + file->length; |
| 521 |
} |
| 522 |
|
| 523 |
// Get the real path of the buffer file |
| 524 |
std::string sFilename; |
| 525 |
std::string path; |
| 526 |
|
| 527 |
m_TSBufferFile.GetFileName(sFilename); |
| 528 |
//size_t pos = sFilename.find_last_of(PATH_SEPARATOR_CHAR); |
| 529 |
size_t pos = sFilename.find_last_of('/'); |
| 530 |
path = sFilename.substr(0, pos + 1); |
| 531 |
|
| 532 |
// Create a list of files in the .tsbuffer file. |
| 533 |
std::vector<std::string> filenames; |
| 534 |
|
| 535 |
Wchar_t* pwCurrFile = pBuffer; //Get a pointer to the first wchar filename string in pBuffer |
| 536 |
size_t length = WcsLen(pwCurrFile); |
| 537 |
|
| 538 |
//kodi::Log(ADDON_LOG_DEBUG, "%s: WcsLen(%d), sizeof(Wchar_t) == %d sizeof(wchar_t) == %d.", __FUNCTION__, length, sizeof(Wchar_t), sizeof(wchar_t)); |
| 539 |
|
| 540 |
while (length > 0) |
| 541 |
{ |
| 542 |
// Convert the current filename (wchar to normal char) |
| 543 |
char* wide2normal = new char[length + 1]; |
| 544 |
WcsToMbs(wide2normal, pwCurrFile, length); |
| 545 |
wide2normal[length] = '\0'; |
| 546 |
std::string sCurrFile = wide2normal; |
| 547 |
//kodi::Log(ADDON_LOG_DEBUG, "%s: filename %s (%s).", __FUNCTION__, wide2normal, sCurrFile.c_str()); |
| 548 |
delete[] wide2normal; |
| 549 |
|
| 550 |
// Modify filename path here to include the real (local) path |
| 551 |
pos = sCurrFile.find_last_of(92); |
| 552 |
std::string name = sCurrFile.substr(pos + 1); |
| 553 |
if (path.length() > 0 && name.length() > 0) |
| 554 |
{ |
| 555 |
// Replace the original path with our local path |
| 556 |
filenames.push_back(path + name); |
| 557 |
} |
| 558 |
else |
| 559 |
{ |
| 560 |
// Keep existing path |
| 561 |
filenames.push_back(sCurrFile); |
| 562 |
} |
| 563 |
|
| 564 |
// Move the wchar buffer pointer to the next wchar string |
| 565 |
pwCurrFile += (length + 1); |
| 566 |
length = WcsLen(pwCurrFile); |
| 567 |
} |
| 568 |
|
| 569 |
// Go through files |
| 570 |
std::vector<MultiFileReaderFile *>::iterator itFiles = m_tsFiles.begin(); |
| 571 |
std::vector<std::string>::iterator itFilenames = filenames.begin(); |
| 572 |
|
| 573 |
while (itFiles < m_tsFiles.end()) |
| 574 |
{ |
| 575 |
file = *itFiles; |
| 576 |
|
| 577 |
++itFiles; |
| 578 |
fileID++; |
| 579 |
|
| 580 |
if (itFilenames < filenames.end()) |
| 581 |
{ |
| 582 |
// TODO: Check that the filenames match. ( Ambass : With buffer integrity check, probably no need to do this !) |
| 583 |
++itFilenames; |
| 584 |
} |
| 585 |
else |
| 586 |
{ |
| 587 |
kodi::Log(ADDON_LOG_DEBUG, "MultiFileReader: Missing files!!\n"); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
while (itFilenames < filenames.end()) |
| 592 |
{ |
| 593 |
std::string pFilename = *itFilenames; |
| 594 |
|
| 595 |
TSDEBUG(ADDON_LOG_DEBUG, "%s: Adding file %s (%" PRId64 ")\n", __FUNCTION__, pFilename.c_str(), nextStartPosition); |
| 596 |
|
| 597 |
file = new MultiFileReaderFile(); |
| 598 |
file->filename = pFilename; |
| 599 |
file->startPosition = nextStartPosition; |
| 600 |
|
| 601 |
fileID++; |
| 602 |
file->filePositionId = fileID; |
| 603 |
|
| 604 |
GetFileLength(file->filename.c_str(), file->length); |
| 605 |
|
| 606 |
m_tsFiles.push_back(file); |
| 607 |
|
| 608 |
nextStartPosition = file->startPosition + file->length; |
| 609 |
|
| 610 |
++itFilenames; |
| 611 |
} |
| 612 |
|
| 613 |
m_filesAdded = filesAdded; |
| 614 |
m_filesRemoved = filesRemoved; |
| 615 |
|
| 616 |
delete[] pBuffer; |
| 617 |
} |
| 618 |
|
| 619 |
if (!m_tsFiles.empty()) |
| 620 |
{ |
| 621 |
file = m_tsFiles.front(); |
| 622 |
m_startPosition = file->startPosition; |
| 623 |
// Since the buffer file may be re-used when a channel is changed, we |
| 624 |
// want the start position to reflect the position in the file after the last |
| 625 |
// channel change, or the real start position, whichever is larger |
| 626 |
if (m_lastZapPosition > m_startPosition) |
| 627 |
{ |
| 628 |
m_startPosition = m_lastZapPosition; |
| 629 |
} |
| 630 |
|
| 631 |
file = m_tsFiles.back(); |
| 632 |
file->length = currentPosition; |
| 633 |
m_endPosition = file->startPosition + currentPosition; |
| 634 |
|
| 635 |
TSDEBUG(ADDON_LOG_DEBUG, "StartPosition %lli, EndPosition %lli, CurrentPosition %lli\n", m_startPosition, m_endPosition, m_currentPosition); |
| 636 |
} |
| 637 |
else |
| 638 |
{ |
| 639 |
m_startPosition = 0; |
| 640 |
m_endPosition = 0; |
| 641 |
} |
| 642 |
|
| 643 |
return S_OK; |
| 644 |
} |
| 645 |
|
| 646 |
long MultiFileReader::GetFileLength(const char* pFilename, int64_t &length) |
| 647 |
{ |
| 648 |
//USES_CONVERSION; |
| 649 |
|
| 650 |
length = 0; |
| 651 |
|
| 652 |
// Try to open the file |
| 653 |
kodi::vfs::CFile hFile; |
| 654 |
if (hFile.OpenFile(pFilename, 0)) |
| 655 |
{ |
| 656 |
length = hFile.GetLength(); |
| 657 |
} |
| 658 |
else |
| 659 |
{ |
| 660 |
kodi::Log(ADDON_LOG_ERROR, "Failed to open file %s : 0x%x(%s)\n", pFilename, errno, strerror(errno)); |
| 661 |
kodi::QueueFormattedNotification(QUEUE_ERROR, "Failed to open file %s", pFilename); |
| 662 |
return S_FALSE; |
| 663 |
} |
| 664 |
return S_OK; |
| 665 |
} |
| 666 |
|
| 667 |
int64_t MultiFileReader::GetFileSize() |
| 668 |
{ |
| 669 |
RefreshTSBufferFile(); |
| 670 |
return m_endPosition - m_startPosition; |
| 671 |
} |
| 672 |
|
| 673 |
int64_t MultiFileReader::OnChannelChange(void) |
| 674 |
{ |
| 675 |
m_lastZapPosition = m_currentPosition; |
| 676 |
return m_currentPosition; |
| 677 |
} |
| 678 |
} |
| 679 |
|