Skip to content

Commit

Permalink
sync upstream cppmyth 2.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
janbar committed Feb 25, 2018
1 parent cfb9fdb commit 73d61bd
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cppmyth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif ()

###############################################################################
# set lib version here
set (CPPMYTH_LIB_VERSION "2.9.6")
set (CPPMYTH_LIB_VERSION "2.10.0")
set (CPPMYTH_LIB_SOVERSION "2")

###############################################################################
Expand Down
77 changes: 76 additions & 1 deletion lib/cppmyth/src/mythlivetvplayback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ LiveTVPlayback::LiveTVPlayback(EventHandler& handler)
, m_recorder()
, m_signal()
, m_chain()
, m_chunk(MYTH_LIVETV_CHUNK_SIZE)
{
m_buffer.pos = 0;
m_buffer.len = 0;
m_buffer.data = new unsigned char[m_chunk];
m_eventSubscriberId = m_eventHandler.CreateSubscription(this);
m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_SIGNAL);
m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_LIVETV_CHAIN);
Expand All @@ -70,7 +74,11 @@ LiveTVPlayback::LiveTVPlayback(const std::string& server, unsigned port)
, m_recorder()
, m_signal()
, m_chain()
, m_chunk(MYTH_LIVETV_CHUNK_SIZE)
{
m_buffer.pos = 0;
m_buffer.len = 0;
m_buffer.data = new unsigned char[m_chunk];
// Private handler will be stopped and closed by destructor.
m_eventSubscriberId = m_eventHandler.CreateSubscription(this);
m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_SIGNAL);
Expand All @@ -86,6 +94,7 @@ LiveTVPlayback::~LiveTVPlayback()
if (m_eventSubscriberId)
m_eventHandler.RevokeSubscription(m_eventSubscriberId);
Close();
delete[] m_buffer.data;
}

bool LiveTVPlayback::Open()
Expand Down Expand Up @@ -464,6 +473,19 @@ void LiveTVPlayback::HandleBackendMessage(EventMessagePtr msg)
}
}

void LiveTVPlayback::SetChunk(unsigned size)
{
if (size < MYTH_LIVETV_CHUNK_MIN)
size = MYTH_LIVETV_CHUNK_MIN;
else if (size > MYTH_LIVETV_CHUNK_MAX)
size = MYTH_LIVETV_CHUNK_MAX;

m_buffer.pos = m_buffer.len = 0;
delete[] m_buffer.data;
m_buffer.data = new unsigned char[size];
m_chunk = size;
}

int64_t LiveTVPlayback::GetSize() const
{
int64_t size = 0;
Expand All @@ -474,6 +496,41 @@ int64_t LiveTVPlayback::GetSize() const
}

int LiveTVPlayback::Read(void* buffer, unsigned n)
{
int c = 0;
bool refill = true;
for (;;)
{
// all requested data are in the buffer
if (m_buffer.len >= n)
{
memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, n);
c += n;
m_buffer.pos += n;
m_buffer.len -= n;
return c;
}
// fill with the rest of data before read a new chunk
if (m_buffer.len > 0)
{
memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, m_buffer.len);
c += m_buffer.len;
n -= m_buffer.len;
m_buffer.len = 0;
}
if (!refill)
break;
m_buffer.pos = 0;
int r = _read(m_buffer.data, m_chunk);
if (r < 0)
return -1;
m_buffer.len += r;
refill = false; // won't read again
}
return c;
}

int LiveTVPlayback::_read(void* buffer, unsigned n)
{
int r = 0;
bool retry;
Expand Down Expand Up @@ -540,6 +597,23 @@ int LiveTVPlayback::Read(void* buffer, unsigned n)
}

int64_t LiveTVPlayback::Seek(int64_t offset, WHENCE_t whence)
{
if (whence == WHENCE_CUR)
{
if (offset == 0)
{
int64_t p = _seek(offset, whence);
// it returns the current position of the first byte in buffer
return (p >= m_buffer.len ? p - m_buffer.len : p);
}
// rebase to the first position in the buffer
offset -= m_buffer.len;
}
m_buffer.len = 0; // clear data in buffer
return _seek(offset, whence);
}

int64_t LiveTVPlayback::_seek(int64_t offset, WHENCE_t whence)
{
OS::CLockGuard lock(*m_mutex); // Lock chain
if (!m_recorder || !m_chain.currentSequence)
Expand Down Expand Up @@ -625,7 +699,8 @@ int64_t LiveTVPlayback::GetPosition() const
pos += m_chain.chained[i].first->GetSize();
pos += m_chain.currentTransfer->GetPosition();
}
return pos;
// it returns the current position of first byte in buffer
return pos - m_buffer.len;
}

bool LiveTVPlayback::IsPlaying() const
Expand Down
12 changes: 12 additions & 0 deletions lib/cppmyth/src/mythlivetvplayback.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

#include <vector>

#define MYTH_LIVETV_CHUNK_SIZE 64000
#define MYTH_LIVETV_CHUNK_MIN 8000
#define MYTH_LIVETV_CHUNK_MAX 128000

namespace Myth
{

Expand All @@ -50,6 +54,8 @@ namespace Myth
bool SpawnLiveTV(const ChannelPtr& thisChannel);
void StopLiveTV();

void SetChunk(unsigned size); // to change the size of read chunk

// Implement Stream
int64_t GetSize() const;
int Read(void *buffer, unsigned n);
Expand Down Expand Up @@ -98,6 +104,12 @@ namespace Myth

typedef std::multimap<unsigned, std::pair<CardInputPtr, ChannelPtr> > preferredCards_t;
preferredCards_t FindTunableCardIds(const std::string& chanNum, const ChannelList& channels);

int _read(void *buffer, unsigned n);
int64_t _seek(int64_t offset, WHENCE_t whence);
// data buffer
unsigned m_chunk; // the size of block to read
struct { unsigned pos; unsigned len; unsigned char * data; } m_buffer;
};

}
Expand Down
81 changes: 79 additions & 2 deletions lib/cppmyth/src/mythrecordingplayback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ RecordingPlayback::RecordingPlayback(EventHandler& handler)
, m_transfer(NULL)
, m_recording(NULL)
, m_readAhead(false)
, m_chunk(MYTH_RECORDING_CHUNK_SIZE)
{
m_buffer.pos = 0;
m_buffer.len = 0;
m_buffer.data = new unsigned char[m_chunk];
m_eventSubscriberId = m_eventHandler.CreateSubscription(this);
m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_UPDATE_FILE_SIZE);
Open();
Expand All @@ -54,7 +58,11 @@ RecordingPlayback::RecordingPlayback(const std::string& server, unsigned port)
, m_transfer(NULL)
, m_recording(NULL)
, m_readAhead(false)
, m_chunk(MYTH_RECORDING_CHUNK_SIZE)
{
m_buffer.pos = 0;
m_buffer.len = 0;
m_buffer.data = new unsigned char[m_chunk];
// Private handler will be stopped and closed by destructor.
m_eventSubscriberId = m_eventHandler.CreateSubscription(this);
m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_UPDATE_FILE_SIZE);
Expand All @@ -66,6 +74,7 @@ RecordingPlayback::~RecordingPlayback()
if (m_eventSubscriberId)
m_eventHandler.RevokeSubscription(m_eventSubscriberId);
Close();
delete[] m_buffer.data;
}

bool RecordingPlayback::Open()
Expand Down Expand Up @@ -133,6 +142,19 @@ bool RecordingPlayback::TransferIsOpen()
return false;
}

void RecordingPlayback::SetChunk(unsigned size)
{
if (size < MYTH_RECORDING_CHUNK_MIN)
size = MYTH_RECORDING_CHUNK_MIN;
else if (size > MYTH_RECORDING_CHUNK_MAX)
size = MYTH_RECORDING_CHUNK_MAX;

m_buffer.pos = m_buffer.len = 0;
delete[] m_buffer.data;
m_buffer.data = new unsigned char[size];
m_chunk = size;
}

int64_t RecordingPlayback::GetSize() const
{
ProtoTransferPtr transfer(m_transfer);
Expand All @@ -141,7 +163,42 @@ int64_t RecordingPlayback::GetSize() const
return 0;
}

int RecordingPlayback::Read(void *buffer, unsigned n)
int RecordingPlayback::Read(void* buffer, unsigned n)
{
int c = 0;
bool refill = true;
for (;;)
{
// all requested data are in the buffer
if (m_buffer.len >= n)
{
memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, n);
c += n;
m_buffer.pos += n;
m_buffer.len -= n;
return c;
}
// fill with the rest of data before read a new chunk
if (m_buffer.len > 0)
{
memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, m_buffer.len);
c += m_buffer.len;
n -= m_buffer.len;
m_buffer.len = 0;
}
if (!refill)
break;
m_buffer.pos = 0;
int r = _read(m_buffer.data, m_chunk);
if (r < 0)
return -1;
m_buffer.len += r;
refill = false; // won't read again
}
return c;
}

int RecordingPlayback::_read(void *buffer, unsigned n)
{
ProtoTransferPtr transfer(m_transfer);
if (transfer)
Expand All @@ -168,6 +225,23 @@ int RecordingPlayback::Read(void *buffer, unsigned n)
}

int64_t RecordingPlayback::Seek(int64_t offset, WHENCE_t whence)
{
if (whence == WHENCE_CUR)
{
if (offset == 0)
{
int64_t p = _seek(offset, whence);
// it returns the current position of the first byte in buffer
return (p >= m_buffer.len ? p - m_buffer.len : p);
}
// rebase to the first position in the buffer
offset -= m_buffer.len;
}
m_buffer.len = 0; // clear data in buffer
return _seek(offset, whence);
}

int64_t RecordingPlayback::_seek(int64_t offset, WHENCE_t whence)
{
ProtoTransferPtr transfer(m_transfer);
if (transfer)
Expand All @@ -179,7 +253,10 @@ int64_t RecordingPlayback::GetPosition() const
{
ProtoTransferPtr transfer(m_transfer);
if (transfer)
return transfer->GetPosition();
{
// it returns the current position of first byte in buffer
return transfer->GetPosition() - m_buffer.len;
}
return 0;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/cppmyth/src/mythrecordingplayback.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "mythstream.h"
#include "mytheventhandler.h"

#define MYTH_RECORDING_CHUNK_SIZE 64000
#define MYTH_RECORDING_CHUNK_MIN 8000
#define MYTH_RECORDING_CHUNK_MAX 128000

namespace Myth
{

Expand All @@ -44,6 +48,8 @@ namespace Myth
void CloseTransfer();
bool TransferIsOpen();

void SetChunk(unsigned size); // to change the size of read chunk

// Implement Stream
int64_t GetSize() const;
int Read(void *buffer, unsigned n);
Expand All @@ -59,6 +65,12 @@ namespace Myth
ProtoTransferPtr m_transfer;
ProgramPtr m_recording;
volatile bool m_readAhead;

int _read(void *buffer, unsigned n);
int64_t _seek(int64_t offset, WHENCE_t whence);
// data buffer
unsigned m_chunk; // the size of block to read
struct { unsigned pos; unsigned len; unsigned char * data; } m_buffer;
};

}
Expand Down

0 comments on commit 73d61bd

Please sign in to comment.