| 1 |
/* |
| 2 |
* Copyright (C) 2010-2021 Team Kodi (https://kodi.tv) |
| 3 |
* Copyright (C) 2010 Alwin Esch (Team Kodi) |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSE.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "InputstreamRecording.h" |
| 10 |
|
| 11 |
#include "RequestPacket.h" |
| 12 |
#include "ResponsePacket.h" |
| 13 |
#include "Settings.h" |
| 14 |
#include "vnsicommand.h" |
| 15 |
|
| 16 |
#include <limits.h> |
| 17 |
|
| 18 |
#define SEEK_POSSIBLE 0x10 // flag used to check if protocol allows seeks |
| 19 |
|
| 20 |
cVNSIRecording::cVNSIRecording(kodi::addon::CInstancePVRClient& instance) |
| 21 |
: cVNSISession(instance), m_instance(instance) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
cVNSIRecording::~cVNSIRecording() |
| 26 |
{ |
| 27 |
Close(); |
| 28 |
} |
| 29 |
|
| 30 |
bool cVNSIRecording::OpenRecording(const kodi::addon::PVRRecording& recinfo) |
| 31 |
{ |
| 32 |
m_recinfo = recinfo; |
| 33 |
|
| 34 |
if (!cVNSISession::Open(CVNSISettings::Get().GetHostname(), CVNSISettings::Get().GetPort(), |
| 35 |
"XBMC RecordingStream Receiver")) |
| 36 |
return false; |
| 37 |
|
| 38 |
if (!cVNSISession::Login()) |
| 39 |
return false; |
| 40 |
|
| 41 |
cRequestPacket vrp; |
| 42 |
vrp.init(VNSI_RECSTREAM_OPEN); |
| 43 |
vrp.add_U32(std::stoi(recinfo.GetRecordingId())); |
| 44 |
|
| 45 |
auto vresp = ReadResult(&vrp); |
| 46 |
if (!vresp) |
| 47 |
return false; |
| 48 |
|
| 49 |
uint32_t returnCode = vresp->extract_U32(); |
| 50 |
if (returnCode == VNSI_RET_OK) |
| 51 |
{ |
| 52 |
m_currentPlayingRecordFrames = vresp->extract_U32(); |
| 53 |
m_currentPlayingRecordBytes = vresp->extract_U64(); |
| 54 |
m_currentPlayingRecordPosition = 0; |
| 55 |
} |
| 56 |
else |
| 57 |
kodi::Log(ADDON_LOG_ERROR, "%s - Can't open recording '%s'", __func__, |
| 58 |
recinfo.GetTitle().c_str()); |
| 59 |
|
| 60 |
return (returnCode == VNSI_RET_OK); |
| 61 |
} |
| 62 |
|
| 63 |
void cVNSIRecording::Close() |
| 64 |
{ |
| 65 |
if (IsOpen()) |
| 66 |
{ |
| 67 |
try |
| 68 |
{ |
| 69 |
cRequestPacket vrp; |
| 70 |
vrp.init(VNSI_RECSTREAM_CLOSE); |
| 71 |
ReadSuccess(&vrp); |
| 72 |
} |
| 73 |
catch (std::exception e) |
| 74 |
{ |
| 75 |
kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what()); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
cVNSISession::Close(); |
| 80 |
} |
| 81 |
|
| 82 |
int cVNSIRecording::Read(unsigned char* buf, uint32_t buf_size) |
| 83 |
{ |
| 84 |
if (m_connectionLost && TryReconnect() != cVNSISession::CONN_ESABLISHED) |
| 85 |
{ |
| 86 |
*buf = 0; |
| 87 |
SleepMs(100); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
if (m_currentPlayingRecordPosition >= m_currentPlayingRecordBytes) |
| 92 |
{ |
| 93 |
GetLength(); |
| 94 |
if (m_currentPlayingRecordPosition >= m_currentPlayingRecordBytes) |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
cRequestPacket vrp; |
| 99 |
vrp.init(VNSI_RECSTREAM_GETBLOCK); |
| 100 |
vrp.add_U64(m_currentPlayingRecordPosition); |
| 101 |
vrp.add_U32(buf_size); |
| 102 |
|
| 103 |
auto vresp = ReadResult(&vrp); |
| 104 |
if (!vresp) |
| 105 |
return -1; |
| 106 |
|
| 107 |
uint32_t length = vresp->getUserDataLength(); |
| 108 |
uint8_t* data = vresp->getUserData(); |
| 109 |
if (length > buf_size) |
| 110 |
{ |
| 111 |
kodi::Log(ADDON_LOG_ERROR, "%s: PANIC - Received more bytes as requested", __func__); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
memcpy(buf, data, length); |
| 116 |
m_currentPlayingRecordPosition += length; |
| 117 |
return length; |
| 118 |
} |
| 119 |
|
| 120 |
bool cVNSIRecording::GetStreamTimes(kodi::addon::PVRStreamTimes& times) |
| 121 |
{ |
| 122 |
GetLength(); |
| 123 |
if (m_currentPlayingRecordLengthMSec == 0) |
| 124 |
return false; |
| 125 |
|
| 126 |
times.SetStartTime(0); |
| 127 |
times.SetPTSStart(0); |
| 128 |
times.SetPTSBegin(0); |
| 129 |
times.SetPTSEnd(m_currentPlayingRecordLengthMSec * 1000); |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
long long cVNSIRecording::Seek(long long pos, uint32_t whence) |
| 135 |
{ |
| 136 |
uint64_t nextPos = m_currentPlayingRecordPosition; |
| 137 |
|
| 138 |
switch (whence) |
| 139 |
{ |
| 140 |
case SEEK_SET: |
| 141 |
nextPos = pos; |
| 142 |
break; |
| 143 |
|
| 144 |
case SEEK_CUR: |
| 145 |
nextPos += pos; |
| 146 |
break; |
| 147 |
|
| 148 |
case SEEK_END: |
| 149 |
if (m_currentPlayingRecordBytes) |
| 150 |
nextPos = m_currentPlayingRecordBytes - pos; |
| 151 |
else |
| 152 |
return -1; |
| 153 |
|
| 154 |
break; |
| 155 |
|
| 156 |
case SEEK_POSSIBLE: |
| 157 |
return 1; |
| 158 |
|
| 159 |
default: |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
if (nextPos >= m_currentPlayingRecordBytes) |
| 164 |
{ |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
m_currentPlayingRecordPosition = nextPos; |
| 169 |
|
| 170 |
return m_currentPlayingRecordPosition; |
| 171 |
} |
| 172 |
|
| 173 |
long long cVNSIRecording::Length(void) |
| 174 |
{ |
| 175 |
return m_currentPlayingRecordBytes; |
| 176 |
} |
| 177 |
|
| 178 |
void cVNSIRecording::OnReconnect() |
| 179 |
{ |
| 180 |
OpenRecording(m_recinfo); |
| 181 |
} |
| 182 |
|
| 183 |
void cVNSIRecording::GetLength() |
| 184 |
{ |
| 185 |
cRequestPacket vrp; |
| 186 |
vrp.init(VNSI_RECSTREAM_GETLENGTH); |
| 187 |
|
| 188 |
auto vresp = ReadResult(&vrp); |
| 189 |
if (!vresp) |
| 190 |
return; |
| 191 |
|
| 192 |
m_currentPlayingRecordBytes = vresp->extract_U64(); |
| 193 |
|
| 194 |
if (GetProtocol() >= 12) |
| 195 |
m_currentPlayingRecordLengthMSec = vresp->extract_U64(); |
| 196 |
} |