11 #include <QCoreApplication>
20 #include "libmythbase/mythversion.h"
27 static constexpr
const char*
VERSION {
"1.0.0" };
28 #define LOC QString("File(%1): ").arg(m_fileName)
31 int data_rate,
bool loopinput) :
32 m_parent(parent), m_fileName(
std::move(fname)),
33 m_loop(loopinput), m_dataRate(data_rate)
35 setObjectName(
"Streamer");
42 LOG(VB_RECORD, LOG_INFO,
LOC +
"Streamer::dtor -- begin");
44 LOG(VB_RECORD, LOG_INFO,
LOC +
"Streamer::dtor -- end");
52 LOG(VB_RECORD, LOG_ERR,
LOC + QString(
"Failed to open '%1' - ")
59 LOG(VB_RECORD, LOG_INFO,
LOC +
"Streamer::Close -- begin");
67 LOG(VB_RECORD, LOG_INFO,
LOC +
"Streamer::Close -- end");
75 LOG(VB_RECORD, LOG_DEBUG,
LOC +
"SendBytes -- start");
79 LOG(VB_GENERAL, LOG_ERR,
LOC +
"SendBytes -- file not open");
99 read_sz = std::min(rate, read_sz);
100 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
108 QByteArray buffer =
m_file->read(read_sz);
110 pkt_size = buffer.size();
117 LOG(VB_RECORD, LOG_WARNING,
LOC +
118 QString(
"SendBytes -- Overflow: %1 > %2, "
119 "dropping first %3 bytes.")
130 LOG(VB_RECORD, LOG_DEBUG,
LOC +
"SendBytes -- Buffer is empty.");
135 LOG(VB_RECORD, LOG_DEBUG,
LOC +
136 QString(
"SendBytes -- Read %1 from file. %2 bytes buffered")
137 .arg(pkt_size).arg(buf_size));
140 if (write_len > buf_size)
141 write_len = buf_size;
142 LOG(VB_RECORD, LOG_DEBUG,
LOC +
143 QString(
"SendBytes -- writing %1 bytes").arg(write_len));
147 LOG(VB_RECORD, LOG_DEBUG,
LOC +
148 QString(
"SendBytes -- wrote %1 bytes").arg(wrote));
150 if (wrote < buf_size)
153 LOG(VB_RECORD, LOG_WARNING,
LOC +
154 QString(
"%1 bytes unwritten").arg(
m_buffer.size()));
159 LOG(VB_RECORD, LOG_DEBUG,
LOC +
"SendBytes -- end");
165 setObjectName(
"Command");
170 QByteArray buf = status.toLatin1() +
'\n';
171 int len =
write(2, buf.constData(), buf.size());
172 if (len != buf.size())
174 LOG(VB_RECORD, LOG_ERR,
LOC +
175 QString(
"Status -- Wrote %1 of %2 bytes of status '%3'")
176 .arg(len).arg(status.size()).arg(status));
179 LOG(VB_RECORD, LOG_DEBUG,
"Status: " + status);
185 LOG(VB_RECORD, LOG_DEBUG,
LOC + cmd);
187 if (cmd.startsWith(
"Version?"))
192 if (cmd.startsWith(
"APIVersion?"))
197 if (cmd.startsWith(
"APIVersion:1"))
199 QString reply = (
API_VERSION == 1) ?
"OK:Yes":
"OK:No";
203 if (cmd.startsWith(
"HasLock?"))
208 if (cmd.startsWith(
"SignalStrengthPercent"))
213 if (cmd.startsWith(
"LockTimeout"))
218 if (cmd.startsWith(
"HasTuner?"))
223 if (cmd.startsWith(
"HasPictureAttributes?"))
232 LOG(VB_RECORD, LOG_ERR,
LOC + QString(
"%1 failed - not initialized!")
237 if (cmd.startsWith(
"SendBytes"))
242 LOG(VB_RECORD, LOG_ERR,
LOC +
"SendBytes - file not open.");
246 if (
m_eof.loadAcquire() != 0)
256 else if (cmd.startsWith(
"XON"))
260 else if (cmd.startsWith(
"XOFF"))
264 else if (cmd.startsWith(
"TuneChannel"))
273 else if (cmd.startsWith(
"IsOpen?"))
281 else if (cmd.startsWith(
"CloseRecorder"))
287 else if (cmd.startsWith(
"FlowControl?"))
291 else if (cmd.startsWith(
"BlockSize"))
296 else if (cmd.startsWith(
"StartStreaming"))
300 else if (cmd.startsWith(
"StopStreaming"))
309 send_status(QString(
"ERR:Unknown command '%1'").arg(cmd));
310 LOG(VB_RECORD, LOG_ERR,
LOC + QString(
"Unknown command '%1'")
322 std::array<struct pollfd,2> polls {};
325 polls[0].events = POLLIN | POLLPRI;
326 polls[0].revents = 0;
331 auto *streamThread =
new QThread(
this);
334 connect(streamThread, &QThread::finished,
340 streamThread->start();
343 input.open(stdin, QIODevice::ReadOnly);
344 QTextStream qtin(&input);
346 LOG(VB_RECORD, LOG_INFO,
LOC +
"Listening for commands");
350 int ret = poll(polls.data(), poll_cnt,
m_timeout);
352 if (polls[0].revents & POLLHUP)
354 LOG(VB_RECORD, LOG_ERR,
LOC +
"poll eof (POLLHUP)");
357 if (polls[0].revents & POLLNVAL)
359 LOG(VB_RECORD, LOG_ERR,
LOC +
"poll error");
363 if (polls[0].revents & POLLIN)
367 cmd = qtin.readLine();
370 streamThread->quit();
371 streamThread->wait();
373 streamThread =
nullptr;
380 if ((EOVERFLOW == errno))
382 LOG(VB_RECORD, LOG_ERR,
LOC +
"command overflow.");
386 if ((EAGAIN == errno) || (EINTR == errno))
388 LOG(VB_RECORD, LOG_ERR,
LOC +
"retry command read.");
392 LOG(VB_RECORD, LOG_ERR,
LOC +
"unknown error reading command.");
401 int main(
int argc,
char *argv[])
426 QCoreApplication a(argc, argv);
427 QCoreApplication::setApplicationName(
"mythfilerecorder");