| 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 "FileReader.h" |
| 36 |
#include <kodi/General.h> //for kodi::Log |
| 37 |
#include "TSDebug.h" |
| 38 |
#include "os-dependent.h" |
| 39 |
#include <algorithm> //std::min, std::max |
| 40 |
#include "utils.h" |
| 41 |
#include <errno.h> |
| 42 |
|
| 43 |
#include <thread> |
| 44 |
|
| 45 |
/* indicate that caller can handle truncated reads, where function returns before entire buffer has been filled */ |
| 46 |
#define READ_TRUNCATED 0x01 |
| 47 |
|
| 48 |
/* indicate that that caller support read in the minimum defined chunk size, this disables internal cache then */ |
| 49 |
#define READ_CHUNKED 0x02 |
| 50 |
|
| 51 |
/* use cache to access this file */ |
| 52 |
#define READ_CACHED 0x04 |
| 53 |
|
| 54 |
/* open without caching. regardless to file type. */ |
| 55 |
#define READ_NO_CACHE 0x08 |
| 56 |
|
| 57 |
/* calcuate bitrate for file while reading */ |
| 58 |
#define READ_BITRATE 0x10 |
| 59 |
|
| 60 |
/* indicate that caller want open a file without intermediate buffer regardless to file type */ |
| 61 |
#define READ_NO_BUFFER 0x200 |
| 62 |
|
| 63 |
template<typename T> void SafeDelete(T*& p) |
| 64 |
{ |
| 65 |
if (p) |
| 66 |
{ |
| 67 |
delete p; |
| 68 |
p = nullptr; |
| 69 |
} |
| 70 |
} |
| 71 |
namespace MPTV |
| 72 |
{ |
| 73 |
FileReader::FileReader() : |
| 74 |
m_fileSize(0), |
| 75 |
m_fileName(""), |
| 76 |
m_flags(READ_NO_CACHE | READ_TRUNCATED | READ_BITRATE) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
FileReader::~FileReader() |
| 81 |
{ |
| 82 |
CloseFile(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
long FileReader::GetFileName(std::string& fileName) |
| 87 |
{ |
| 88 |
fileName = m_fileName; |
| 89 |
return S_OK; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
long FileReader::SetFileName(const std::string& fileName) |
| 94 |
{ |
| 95 |
m_fileName = ToKodiPath(fileName); |
| 96 |
return S_OK; |
| 97 |
} |
| 98 |
|
| 99 |
// |
| 100 |
// OpenFile |
| 101 |
// |
| 102 |
// Opens the file ready for streaming |
| 103 |
// |
| 104 |
long FileReader::OpenFile(const std::string& fileName) |
| 105 |
{ |
| 106 |
SetFileName(fileName); |
| 107 |
return OpenFile(); |
| 108 |
} |
| 109 |
|
| 110 |
long FileReader::OpenFile() |
| 111 |
{ |
| 112 |
int Tmo = 25; //5 in MediaPortal |
| 113 |
|
| 114 |
|
| 115 |
// Is the file already opened |
| 116 |
if (!IsFileInvalid()) |
| 117 |
{ |
| 118 |
kodi::Log(ADDON_LOG_INFO, "FileReader::OpenFile() file already open"); |
| 119 |
return S_OK; |
| 120 |
} |
| 121 |
|
| 122 |
// Has a filename been set yet |
| 123 |
if (m_fileName.empty()) |
| 124 |
{ |
| 125 |
kodi::Log(ADDON_LOG_ERROR, "FileReader::OpenFile() no filename"); |
| 126 |
return ERROR_INVALID_NAME; |
| 127 |
} |
| 128 |
|
| 129 |
do |
| 130 |
{ |
| 131 |
kodi::Log(ADDON_LOG_INFO, "FileReader::OpenFile() %s.", m_fileName.c_str()); |
| 132 |
if (m_hFile.OpenFile(m_fileName, m_flags)) |
| 133 |
{ |
| 134 |
break; |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
kodi::vfs::FileStatus buffer; |
| 139 |
bool statResult = kodi::vfs::StatFile(m_fileName, buffer); |
| 140 |
|
| 141 |
if (!statResult) |
| 142 |
{ |
| 143 |
if (errno == EACCES) |
| 144 |
{ |
| 145 |
kodi::Log(ADDON_LOG_ERROR, "Permission denied. Check the file or share access rights for '%s'", m_fileName.c_str()); |
| 146 |
kodi::QueueNotification(QUEUE_ERROR, "", "Permission denied"); |
| 147 |
Tmo = 0; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 153 |
|
| 154 |
} while (--Tmo); |
| 155 |
|
| 156 |
if (Tmo) |
| 157 |
{ |
| 158 |
if (Tmo < 4) // 1 failed + 1 succeded is quasi-normal, more is a bit suspicious ( disk drive too slow or problem ? ) |
| 159 |
kodi::Log(ADDON_LOG_DEBUG, "FileReader::OpenFile(), %d tries to succeed opening %ws.", 6 - Tmo, m_fileName.c_str()); |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
kodi::Log(ADDON_LOG_ERROR, "FileReader::OpenFile(), open file %s failed.", m_fileName.c_str()); |
| 164 |
return S_FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
kodi::Log(ADDON_LOG_DEBUG, "%s: OpenFile(%s) succeeded.", __FUNCTION__, m_fileName.c_str()); |
| 168 |
|
| 169 |
SetFilePointer(0, SEEK_SET); |
| 170 |
|
| 171 |
return S_OK; |
| 172 |
|
| 173 |
} // Open |
| 174 |
|
| 175 |
// |
| 176 |
// CloseFile |
| 177 |
// |
| 178 |
// Closes any dump file we have opened |
| 179 |
// |
| 180 |
long FileReader::CloseFile() |
| 181 |
{ |
| 182 |
if (m_hFile.IsOpen()) |
| 183 |
{ |
| 184 |
m_hFile.Close(); |
| 185 |
} |
| 186 |
|
| 187 |
return S_OK; |
| 188 |
} // CloseFile |
| 189 |
|
| 190 |
|
| 191 |
inline bool FileReader::IsFileInvalid() |
| 192 |
{ |
| 193 |
return !m_hFile.IsOpen(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
int64_t FileReader::SetFilePointer(int64_t llDistanceToMove, unsigned long dwMoveMethod) |
| 198 |
{ |
| 199 |
TSDEBUG(ADDON_LOG_DEBUG, "%s: distance %d method %d.", __FUNCTION__, llDistanceToMove, dwMoveMethod); |
| 200 |
int64_t rc = m_hFile.Seek(llDistanceToMove, dwMoveMethod); |
| 201 |
TSDEBUG(ADDON_LOG_DEBUG, "%s: distance %d method %d returns %d.", __FUNCTION__, llDistanceToMove, dwMoveMethod, rc); |
| 202 |
return rc; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
int64_t FileReader::GetFilePointer() |
| 207 |
{ |
| 208 |
return m_hFile.GetPosition(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
void FileReader::SetNoBuffer(void) |
| 213 |
{ |
| 214 |
m_flags |= READ_NO_BUFFER; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
long FileReader::Read(unsigned char* pbData, size_t lDataLength, size_t *dwReadBytes) |
| 219 |
{ |
| 220 |
ssize_t read_bytes = m_hFile.Read((void*)pbData, lDataLength);//Read file data into buffer |
| 221 |
if (read_bytes < 0) |
| 222 |
{ |
| 223 |
TSDEBUG(ADDON_LOG_DEBUG, "%s: ReadFile function failed.", __FUNCTION__); |
| 224 |
*dwReadBytes = 0; |
| 225 |
return S_FALSE; |
| 226 |
} |
| 227 |
*dwReadBytes = static_cast<size_t>(read_bytes); |
| 228 |
TSDEBUG(ADDON_LOG_DEBUG, "%s: requested read length %d actually read %d.", __FUNCTION__, lDataLength, *dwReadBytes); |
| 229 |
|
| 230 |
if (*dwReadBytes < lDataLength) |
| 231 |
{ |
| 232 |
kodi::Log(ADDON_LOG_INFO, "%s: requested %d bytes, read only %d bytes.", __FUNCTION__, lDataLength, *dwReadBytes); |
| 233 |
return S_FALSE; |
| 234 |
} |
| 235 |
return S_OK; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
int64_t FileReader::GetFileSize() |
| 240 |
{ |
| 241 |
return m_hFile.GetLength(); |
| 242 |
} |
| 243 |
|
| 244 |
int64_t FileReader::OnChannelChange(void) |
| 245 |
{ |
| 246 |
return m_hFile.GetPosition(); |
| 247 |
} |
| 248 |
} |