22 #include "mythrecordingplayback.h" 23 #include "private/debug.h" 24 #include "private/os/threads/mutex.h" 25 #include "private/builtin.h" 37 RecordingPlayback::RecordingPlayback(
EventHandler& handler)
39 , m_eventHandler(handler)
40 , m_eventSubscriberId(0)
44 , m_chunk(MYTH_RECORDING_CHUNK_SIZE)
48 m_buffer.data =
new unsigned char[m_chunk];
49 m_eventSubscriberId = m_eventHandler.CreateSubscription(
this);
50 m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_UPDATE_FILE_SIZE);
54 RecordingPlayback::RecordingPlayback(
const std::string& server,
unsigned port)
56 , m_eventHandler(server, port)
57 , m_eventSubscriberId(0)
61 , m_chunk(MYTH_RECORDING_CHUNK_SIZE)
65 m_buffer.data =
new unsigned char[m_chunk];
67 m_eventSubscriberId = m_eventHandler.CreateSubscription(
this);
68 m_eventHandler.SubscribeForEvent(m_eventSubscriberId, EVENT_UPDATE_FILE_SIZE);
72 RecordingPlayback::~RecordingPlayback()
74 if (m_eventSubscriberId)
75 m_eventHandler.RevokeSubscription(m_eventSubscriberId);
77 delete[] m_buffer.data;
80 bool RecordingPlayback::Open()
83 OS::CLockGuard lock(*m_mutex);
84 if (ProtoPlayback::IsOpen())
86 if (ProtoPlayback::Open())
88 if (!m_eventHandler.IsRunning())
89 m_eventHandler.Start();
95 void RecordingPlayback::Close()
98 OS::CLockGuard lock(*m_mutex);
100 ProtoPlayback::Close();
103 bool RecordingPlayback::OpenTransfer(ProgramPtr recording)
106 OS::CLockGuard lock(*m_mutex);
107 if (!ProtoPlayback::IsOpen())
112 m_transfer.reset(
new ProtoTransfer(m_server, m_port, recording->fileName, recording->recording.storageGroup));
113 if (m_transfer->Open())
115 m_recording.swap(recording);
116 m_recording->fileSize = m_transfer->GetSize();
124 void RecordingPlayback::CloseTransfer()
127 OS::CLockGuard lock(*m_mutex);
131 TransferDone(*m_transfer);
137 bool RecordingPlayback::TransferIsOpen()
139 ProtoTransferPtr transfer(m_transfer);
141 return ProtoPlayback::TransferIsOpen(*transfer);
145 void RecordingPlayback::SetChunk(
unsigned size)
147 if (size < MYTH_RECORDING_CHUNK_MIN)
148 size = MYTH_RECORDING_CHUNK_MIN;
149 else if (size > MYTH_RECORDING_CHUNK_MAX)
150 size = MYTH_RECORDING_CHUNK_MAX;
152 m_buffer.pos = m_buffer.len = 0;
153 delete[] m_buffer.data;
154 m_buffer.data =
new unsigned char[size];
158 int64_t RecordingPlayback::GetSize()
const 160 ProtoTransferPtr transfer(m_transfer);
162 return transfer->GetSize();
166 int RecordingPlayback::Read(
void* buffer,
unsigned n)
173 if (m_buffer.len >= n)
175 memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, n);
182 if (m_buffer.len > 0)
184 memcpy(static_cast<unsigned char*>(buffer) + c, m_buffer.data + m_buffer.pos, m_buffer.len);
192 int r = _read(m_buffer.data, m_chunk);
201 int RecordingPlayback::_read(
void *buffer,
unsigned n)
203 ProtoTransferPtr transfer(m_transfer);
208 int64_t s = transfer->GetRemaining();
214 return TransferRequestBlock(*transfer, buffer, n);
221 return TransferRequestBlock(*transfer, buffer, n);
227 int64_t RecordingPlayback::Seek(int64_t offset, WHENCE_t whence)
229 if (whence == WHENCE_CUR)
233 int64_t p = _seek(offset, whence);
235 return (p >= m_buffer.len ? p - m_buffer.len : p);
238 offset -= m_buffer.len;
241 return _seek(offset, whence);
244 int64_t RecordingPlayback::_seek(int64_t offset, WHENCE_t whence)
246 ProtoTransferPtr transfer(m_transfer);
248 return TransferSeek(*transfer, offset, whence);
252 int64_t RecordingPlayback::GetPosition()
const 254 ProtoTransferPtr transfer(m_transfer);
258 return transfer->GetPosition() - m_buffer.len;
263 void RecordingPlayback::HandleBackendMessage(EventMessagePtr msg)
266 ProgramPtr recording(m_recording);
267 ProtoTransferPtr transfer(m_transfer);
270 case EVENT_UPDATE_FILE_SIZE:
271 if (msg->subject.size() >= 3 && recording && transfer)
275 if (msg->subject.size() >= 4)
279 if (string_to_uint32(msg->subject[1].c_str(), &chanid)
280 || string_to_time(msg->subject[2].c_str(), &startts)
281 || recording->channel.chanId != chanid
282 || recording->recording.startTs != startts
283 || string_to_int64(msg->subject[3].c_str(), &newsize))
290 if (string_to_uint32(msg->subject[1].c_str(), &recordedid)
291 || recording->recording.recordedId != recordedid
292 || string_to_int64(msg->subject[2].c_str(), &newsize))
297 transfer->SetSize(newsize);
298 recording->fileSize = newsize;
299 DBG(DBG_DEBUG,
"%s: (%d) %s %" PRIi64
"\n", __FUNCTION__,
300 msg->event, recording->fileName.c_str(), newsize);
This is the main namespace that encloses all public classes.