Skip to content

Commit

Permalink
Implement GetStreamTimes for ongoing recordings
Browse files Browse the repository at this point in the history
Note: This introduces a new bug if the ongoing recording has started in
the middle of a show. We don't know the actual start time of the
recording. Instead we use the start time of the show. As a result the
possible seek time is just wrong.
  • Loading branch information
manuelm committed Nov 18, 2018
1 parent d8b3f35 commit 2e94d0c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
7 changes: 4 additions & 3 deletions src/DvbData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,20 +651,21 @@ dvbviewer::RecordingReader *Dvb::OpenRecordedStream(const PVR_RECORDING &recinfo
break;
}

std::pair<std::time_t, std::time_t> startEndTimes(0, 0);
/* recording reopen only works in non-transcoding case */
std::time_t now = std::time(nullptr), end = 0;
if (m_settings.m_recordingTranscoding == Transcoding::OFF)
{
std::time_t now = std::time(nullptr);
const std::string channelName = recinfo.strChannelName;
auto timer = m_timers.GetTimer([&](const Timer &timer)
{
return timer.isRunning(&now, &channelName);
});
if (timer)
end = timer->end;
startEndTimes = std::make_pair(timer->start, timer->end);
}

return new RecordingReader(url, end);
return new RecordingReader(url, startEndTimes);
}

bool Dvb::GetRecordingEdl(const PVR_RECORDING &recinfo, PVR_EDL_ENTRY edl[],
Expand Down
30 changes: 23 additions & 7 deletions src/RecordingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
using namespace dvbviewer;
using namespace ADDON;

RecordingReader::RecordingReader(const std::string &streamURL, std::time_t end)
: m_streamURL(streamURL), m_end(end)
RecordingReader::RecordingReader(const std::string &streamURL,
const std::pair<std::time_t, std::time_t> &startEnd)
: m_streamURL(streamURL), m_timeStart(startEnd.first), m_timeEnd(startEnd.second)
{
m_readHandle = XBMC->CURLCreate(m_streamURL.c_str());
(void)XBMC->CURLOpen(m_readHandle, XFILE::READ_NO_CACHE | XFILE::READ_AUDIO_VIDEO);
m_len = XBMC->GetFileLength(m_readHandle);
m_nextReopen = std::chrono::steady_clock::now()
+ std::chrono::seconds(REOPEN_INTERVAL);
XBMC->Log(LOG_DEBUG, "RecordingReader: Started; url=%s, end=%u",
m_streamURL.c_str(), m_end);
m_timeRecorded = std::time(nullptr);
XBMC->Log(LOG_DEBUG, "RecordingReader: Started; url=%s, start=%u, end=%u",
m_streamURL.c_str(), m_timeStart, m_timeEnd);
}

RecordingReader::~RecordingReader(void)
Expand All @@ -39,7 +41,7 @@ bool RecordingReader::Start()
ssize_t RecordingReader::ReadData(unsigned char *buffer, unsigned int size)
{
/* check for playback of ongoing recording */
if (m_end)
if (m_timeEnd)
{
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
if (m_pos == m_len || now > m_nextReopen)
Expand All @@ -49,6 +51,7 @@ ssize_t RecordingReader::ReadData(unsigned char *buffer, unsigned int size)
(void)XBMC->CURLOpen(m_readHandle, XFILE::READ_REOPEN | XFILE::READ_NO_CACHE
| XFILE::READ_AUDIO_VIDEO);
m_len = XBMC->GetFileLength(m_readHandle);
m_timeRecorded = std::time(nullptr);
XBMC->SeekFile(m_readHandle, m_pos, SEEK_SET);

// random value (10 MiB) we choose to switch to fast reopen interval
Expand All @@ -57,8 +60,11 @@ ssize_t RecordingReader::ReadData(unsigned char *buffer, unsigned int size)
nearEnd ? REOPEN_INTERVAL_FAST : REOPEN_INTERVAL);

/* recording has finished */
if (std::time(nullptr) > m_end)
m_end = 0;
if (m_timeRecorded > m_timeEnd)
{
m_timeRecorded = m_timeEnd;
m_timeEnd = 0;
}
}
}

Expand Down Expand Up @@ -86,3 +92,13 @@ int64_t RecordingReader::Length()
{
return m_len;
}

std::time_t RecordingReader::TimeStart()
{
return m_timeStart;
}

std::time_t RecordingReader::TimeRecorded()
{
return m_timeRecorded;
}
13 changes: 10 additions & 3 deletions src/RecordingReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "libXBMC_addon.h"

#include <atomic>
#include <chrono>
#include <ctime>

Expand All @@ -11,20 +12,26 @@ namespace dvbviewer
class RecordingReader
{
public:
RecordingReader(const std::string &streamURL, std::time_t end);
RecordingReader(const std::string &streamURL,
const std::pair<std::time_t, std::time_t> &startEnd);
~RecordingReader(void);
bool Start();
ssize_t ReadData(unsigned char *buffer, unsigned int size);
int64_t Seek(long long position, int whence);
int64_t Position();
int64_t Length();

/* methods if the recording hasn't finished */
std::time_t TimeStart();
std::time_t TimeRecorded();

private:
std::string m_streamURL;
void *m_readHandle;

/*!< @brief end time of the recording in case this an ongoing recording */
std::time_t m_end;
std::time_t m_timeStart; /* start time. 0 if already finished on playback */
std::time_t m_timeEnd; /* end time. 0 as soon as recording is finished */
std::atomic<std::time_t> m_timeRecorded; /* maximum time recorded. changes every reopen */
std::chrono::steady_clock::time_point m_nextReopen;
uint64_t m_pos = { 0 };
uint64_t m_len;
Expand Down
28 changes: 21 additions & 7 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,30 @@ PVR_ERROR GetStreamTimes(PVR_STREAM_TIMES *times)
{
if (!times)
return PVR_ERROR_INVALID_PARAMETERS;

int64_t timeStart, timeEnd;
if (strReader)
{
times->startTime = strReader->TimeStart();
times->ptsStart = 0;
times->ptsBegin = 0;
times->ptsEnd = (!strReader->IsTimeshifting()) ? 0
: static_cast<int64_t>(strReader->TimeEnd() - strReader->TimeStart()) * DVD_TIME_BASE;
return PVR_ERROR_NO_ERROR;
timeStart = timeEnd = 0;
if (strReader->IsTimeshifting())
{
timeStart = strReader->TimeStart();
timeEnd = strReader->TimeEnd();
}
}
return PVR_ERROR_NOT_IMPLEMENTED;
else if (recReader && recReader->TimeStart() > 0)
{
timeStart = recReader->TimeStart();
timeEnd = recReader->TimeRecorded();
}
else
return PVR_ERROR_NOT_IMPLEMENTED;

times->startTime = timeStart;
times->ptsStart = 0;
times->ptsBegin = 0;
times->ptsEnd = (timeEnd - timeStart) * DVD_TIME_BASE;
return PVR_ERROR_NO_ERROR;
}

void PauseStream(bool paused)
Expand Down

0 comments on commit 2e94d0c

Please sign in to comment.