| 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 |
|
| 30 |
#ifdef LIVE555 |
| 31 |
|
| 32 |
#include "os-dependent.h" |
| 33 |
#include "MemoryBuffer.h" |
| 34 |
#include <kodi/General.h> //for kodi::Log |
| 35 |
#include "TSDebug.h" |
| 36 |
|
| 37 |
#include <thread> |
| 38 |
|
| 39 |
#define MAX_MEMORY_BUFFER_SIZE (1024L*1024L*12L) |
| 40 |
|
| 41 |
CMemoryBuffer::CMemoryBuffer(void) |
| 42 |
{ |
| 43 |
m_bRunning = true; |
| 44 |
m_BytesInBuffer = 0; |
| 45 |
} |
| 46 |
|
| 47 |
CMemoryBuffer::~CMemoryBuffer() |
| 48 |
{ |
| 49 |
Clear(); |
| 50 |
} |
| 51 |
|
| 52 |
bool CMemoryBuffer::IsRunning() |
| 53 |
{ |
| 54 |
return m_bRunning; |
| 55 |
} |
| 56 |
|
| 57 |
void CMemoryBuffer::Clear() |
| 58 |
{ |
| 59 |
std::lock_guard<std::mutex> BufferLock(m_BufferLock); |
| 60 |
|
| 61 |
for (auto& item : m_Array) |
| 62 |
{ |
| 63 |
if (item) |
| 64 |
{ |
| 65 |
if (item->data) |
| 66 |
delete[] item->data; |
| 67 |
delete item; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
m_Array.clear(); |
| 72 |
m_BytesInBuffer = 0; |
| 73 |
} |
| 74 |
|
| 75 |
size_t CMemoryBuffer::Size() |
| 76 |
{ |
| 77 |
return m_BytesInBuffer; |
| 78 |
} |
| 79 |
|
| 80 |
void CMemoryBuffer::Run(bool onOff) |
| 81 |
{ |
| 82 |
TSDEBUG(ADDON_LOG_DEBUG, "memorybuffer: run:%d %d", onOff, m_bRunning); |
| 83 |
|
| 84 |
if (m_bRunning != onOff) |
| 85 |
{ |
| 86 |
m_bRunning = onOff; |
| 87 |
|
| 88 |
if (m_bRunning == false) |
| 89 |
{ |
| 90 |
Clear(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
TSDEBUG(ADDON_LOG_DEBUG, "memorybuffer: running:%d", onOff); |
| 95 |
} |
| 96 |
|
| 97 |
size_t CMemoryBuffer::ReadFromBuffer(unsigned char *pbData, size_t lDataLength) |
| 98 |
{ |
| 99 |
if (pbData == NULL || lDataLength <= 0 || !m_bRunning) |
| 100 |
return 0; |
| 101 |
|
| 102 |
while (m_BytesInBuffer < lDataLength) |
| 103 |
{ |
| 104 |
if (!m_bRunning) |
| 105 |
return 0; |
| 106 |
|
| 107 |
std::unique_lock<std::mutex> lock(m_BufferLock); |
| 108 |
m_condition.wait_for(lock, std::chrono::milliseconds(5000)); |
| 109 |
|
| 110 |
if (!m_bRunning) |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
// kodi::Log(ADDON_LOG_DEBUG, "get..%d/%d", lDataLength, m_BytesInBuffer); |
| 115 |
size_t bytesWritten = 0; |
| 116 |
std::lock_guard<std::mutex> BufferLock(m_BufferLock); |
| 117 |
|
| 118 |
while (bytesWritten < lDataLength) |
| 119 |
{ |
| 120 |
if (m_Array.empty()) |
| 121 |
{ |
| 122 |
kodi::Log(ADDON_LOG_DEBUG, "memorybuffer: read:empty buffer\n"); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
BufferItem *item = m_Array.at(0); |
| 126 |
|
| 127 |
if (NULL == item) |
| 128 |
{ |
| 129 |
kodi::Log(ADDON_LOG_DEBUG, "memorybuffer: item==NULL\n"); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
size_t copyLength; |
| 134 |
if ( (item->nDataLength - item->nOffset) < (lDataLength - bytesWritten) ) |
| 135 |
{ |
| 136 |
copyLength = item->nDataLength - item->nOffset; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
copyLength = lDataLength - bytesWritten; |
| 141 |
} |
| 142 |
|
| 143 |
if (NULL == item->data) |
| 144 |
{ |
| 145 |
kodi::Log(ADDON_LOG_DEBUG, "memorybuffer: item->data==NULL\n"); |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
memcpy(&pbData[bytesWritten], &item->data[item->nOffset], copyLength); |
| 150 |
|
| 151 |
bytesWritten += copyLength; |
| 152 |
item->nOffset += copyLength; |
| 153 |
m_BytesInBuffer -= copyLength; |
| 154 |
|
| 155 |
if (item->nOffset >= item->nDataLength) |
| 156 |
{ |
| 157 |
m_Array.erase(m_Array.begin()); |
| 158 |
if (item->data) |
| 159 |
delete[] item->data; |
| 160 |
delete item; |
| 161 |
} |
| 162 |
} |
| 163 |
return bytesWritten; |
| 164 |
} |
| 165 |
|
| 166 |
long CMemoryBuffer::PutBuffer(unsigned char *pbData, size_t lDataLength) |
| 167 |
{ |
| 168 |
if (lDataLength == 0 || pbData == NULL) return E_FAIL; |
| 169 |
|
| 170 |
BufferItem* item = new BufferItem(); |
| 171 |
item->nOffset = 0; |
| 172 |
item->nDataLength = lDataLength; |
| 173 |
item->data = new unsigned char[lDataLength]; |
| 174 |
memcpy(item->data, pbData, lDataLength); |
| 175 |
bool sleep = false; |
| 176 |
{ |
| 177 |
std::lock_guard<std::mutex> BufferLock(m_BufferLock); |
| 178 |
m_Array.push_back(item); |
| 179 |
m_BytesInBuffer += lDataLength; |
| 180 |
|
| 181 |
//kodi::Log(ADDON_LOG_DEBUG, "add..%d/%d",lDataLength,m_BytesInBuffer); |
| 182 |
while (m_BytesInBuffer > MAX_MEMORY_BUFFER_SIZE) |
| 183 |
{ |
| 184 |
sleep = true; |
| 185 |
kodi::Log(ADDON_LOG_DEBUG, "memorybuffer:put full buffer (%d)", (unsigned long) m_BytesInBuffer); |
| 186 |
BufferItem *item2 = m_Array.at(0); |
| 187 |
size_t copyLength = item2->nDataLength - item2->nOffset; |
| 188 |
|
| 189 |
m_BytesInBuffer -= copyLength; |
| 190 |
m_Array.erase(m_Array.begin()); |
| 191 |
if (item2->data) |
| 192 |
delete[] item2->data; |
| 193 |
delete item2; |
| 194 |
} |
| 195 |
if (m_BytesInBuffer > 0) |
| 196 |
{ |
| 197 |
m_condition.notify_one(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if (sleep) |
| 202 |
{ |
| 203 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 204 |
} |
| 205 |
return S_OK; |
| 206 |
} |
| 207 |
|
| 208 |
#endif //LIVE555 |