MythTV  master
ExternalStreamHandler.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef EXTERNAL_STREAMHANDLER_H
4 #define EXTERNAL_STREAMHANDLER_H
5 
6 // POSIX
7 #include <sys/types.h> // for pid_t
8 
9 #include <cstdint>
10 #include <vector>
11 
12 #include <QString>
13 #include <QAtomicInt>
14 #include <QByteArray>
15 #include <QFileInfo>
16 #include <QMutex>
17 #include <QMap>
18 #include <QVariantMap>
19 #include <QStringList>
20 #include <QTextStream>
21 
22 #include "libmythbase/mythchrono.h"
23 
24 #include "streamhandler.h"
25 
26 class DTVSignalMonitor;
27 class ExternalChannel;
28 
29 class ExternIO
30 {
31  static constexpr uint8_t kMaxErrorCnt { 20 };
32 
33  public:
34  ExternIO(const QString & app, const QStringList & args);
35  ~ExternIO(void);
36 
37  bool Ready(int fd, std::chrono::milliseconds timeout, const QString & what);
38  int Read(QByteArray & buffer, int maxlen, std::chrono::milliseconds timeout = 2500ms);
39  QByteArray GetStatus(std::chrono::milliseconds timeout = 2500ms);
40  int Write(const QByteArray & buffer);
41  bool Run(void);
42  bool Error(void) const { return !m_error.isEmpty(); }
43  QString ErrorString(void) const { return m_error; }
44  void ClearError(void) { m_error.clear(); }
45 
46  static bool KillIfRunning(const QString & cmd);
47 
48  private:
49  void Fork(void);
50 
51  QFileInfo m_app;
52  QStringList m_args;
53  int m_appIn {-1};
54  int m_appOut {-1};
55  int m_appErr {-1};
56  pid_t m_pid {-1};
57  QString m_error;
58 
59  int m_bufSize {0};
60  char *m_buffer {nullptr};
61 
62  QString m_statusBuf;
63  QTextStream m_status;
64  int m_errCnt {0};
65 };
66 
67 // Note : This class always uses a TS reader.
68 
70 {
75 
76  public:
77  static ExternalStreamHandler *Get(const QString &devname,
78  int inputid, int majorid);
79  static void Return(ExternalStreamHandler * & ref, int inputid);
80 
81  public:
82  explicit ExternalStreamHandler(const QString & path, int inputid,
83  int majorid);
84  ~ExternalStreamHandler(void) override { CloseApp(); }
85 
86  void run(void) override; // MThread
87  void PriorityEvent(int fd) override; // DeviceReaderCB
88 
89  QString GetDescription(void) { return m_loc; }
90  QString UpdateDescription(void);
91  bool IsDamaged(void) const { return m_damaged; }
92  void ClearDamaged(void) { m_damaged = false; }
93  bool IsAppOpen(void);
94  bool IsTSOpen(void);
95  bool HasTuner(void) const { return m_hasTuner; }
96  bool HasPictureAttributes(void) const { return m_hasPictureAttributes; }
97 
98  bool RestartStream(void);
99 
100  void LockReplay(void) { m_replayLock.lock(); }
101  void UnlockReplay(bool enable_replay = false)
102  { m_replay = enable_replay; m_replayLock.unlock(); }
103  void ReplayStream(void);
104  bool StartStreaming(void);
105  bool StopStreaming(void);
106 
107  bool CheckForError(void);
108 
109  void PurgeBuffer(void);
110 
111  bool ProcessCommand(const QString & cmd, QString & result,
112  std::chrono::milliseconds timeout = 4s,
113  uint retry_cnt = 3);
114  bool ProcessVer1(const QString & cmd, QString & result,
115  std::chrono::milliseconds timeout, uint retry_cnt);
116  bool ProcessVer2(const QString & command, QString & result,
117  std::chrono::milliseconds timeout, uint retry_cnt);
118  bool ProcessJson(const QVariantMap & vmsg,
119  QVariantMap & elements,
120  QByteArray & response,
121  std::chrono::milliseconds timeout = 4s,
122  uint retry_cnt = 3);
123  int APIVersion(void) const { return m_apiVersion; }
124 
125  private:
126  int StreamingCount(void) const;
127  bool SetAPIVersion(void);
128  bool OpenApp(void);
129  void CloseApp(void);
130 
131  QString m_loc;
133  QMutex m_ioLock;
134  ExternIO *m_io {nullptr};
135  QStringList m_args;
136  QString m_app;
137  bool m_tsOpen {false};
138  int m_ioErrCnt {0};
139  bool m_pollMode {false};
140 
141  int m_apiVersion {1};
143  bool m_hasTuner {false};
145 
146  QByteArray m_replayBuffer;
147  bool m_replay {false};
148  bool m_xon {false};
149  bool m_damaged {false};
150 
151  // for implementing Get & Return
152  static QMutex s_handlersLock;
153  static QMap<int, ExternalStreamHandler*> s_handlers;
154  static QMap<int, uint> s_handlersRefCnt;
155 
156  QAtomicInt m_streamingCnt;
157  QMutex m_streamLock;
158  QMutex m_replayLock;
160 };
161 
162 #endif // EXTERNAL_STREAMHANDLER_H
ExternalStreamHandler::ProcessVer1
bool ProcessVer1(const QString &cmd, QString &result, std::chrono::milliseconds timeout, uint retry_cnt)
Definition: ExternalStreamHandler.cpp:1277
ExternalStreamHandler::ExternalStreamHandler
ExternalStreamHandler(const QString &path, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:544
build_compdb.args
args
Definition: build_compdb.py:11
ExternalStreamHandler::m_pollMode
bool m_pollMode
Definition: ExternalStreamHandler.h:139
ExternalStreamHandler::IsTSOpen
bool IsTSOpen(void)
Definition: ExternalStreamHandler.cpp:1020
ExternalStreamHandler::m_ioLock
QMutex m_ioLock
Definition: ExternalStreamHandler.h:133
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
ExternalStreamHandler::m_apiVersion
int m_apiVersion
Definition: ExternalStreamHandler.h:141
ExternalStreamHandler::m_serialNo
uint m_serialNo
Definition: ExternalStreamHandler.h:142
streamhandler.h
ExternalStreamHandler::ClearDamaged
void ClearDamaged(void)
Definition: ExternalStreamHandler.h:92
ExternalStreamHandler::PriorityEvent
void PriorityEvent(int fd) override
Definition: ExternalStreamHandler.cpp:1787
StreamHandler
Definition: streamhandler.h:56
ExternalStreamHandler::m_hasPictureAttributes
bool m_hasPictureAttributes
Definition: ExternalStreamHandler.h:144
ExternalStreamHandler::StreamingCount
int StreamingCount(void) const
Definition: ExternalStreamHandler.cpp:574
ExternIO::~ExternIO
~ExternIO(void)
Definition: ExternalStreamHandler.cpp:68
ExternalStreamHandler::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: ExternalStreamHandler.cpp:579
ExternalStreamHandler::m_io
ExternIO * m_io
Definition: ExternalStreamHandler.h:134
ExternalStreamHandler::IsDamaged
bool IsDamaged(void) const
Definition: ExternalStreamHandler.h:91
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1183
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:95
ExternIO::m_error
QString m_error
Definition: ExternalStreamHandler.h:57
ExternalStreamHandler::m_xon
bool m_xon
Definition: ExternalStreamHandler.h:148
ExternalStreamHandler::m_replayLock
QMutex m_replayLock
Definition: ExternalStreamHandler.h:158
ExternalStreamHandler::m_processLock
QMutex m_processLock
Definition: ExternalStreamHandler.h:159
ExternIO::ExternIO
ExternIO(const QString &app, const QStringList &args)
Definition: ExternalStreamHandler.cpp:39
ExternalStreamHandler::s_handlers
static QMap< int, ExternalStreamHandler * > s_handlers
Definition: ExternalStreamHandler.h:153
ExternIO::m_bufSize
int m_bufSize
Definition: ExternalStreamHandler.h:59
ExternIO::Error
bool Error(void) const
Definition: ExternalStreamHandler.h:42
ExternIO::m_buffer
char * m_buffer
Definition: ExternalStreamHandler.h:60
ExternIO
Definition: ExternalStreamHandler.h:29
ExternalStreamHandler::s_handlersRefCnt
static QMap< int, uint > s_handlersRefCnt
Definition: ExternalStreamHandler.h:154
ExternalStreamHandler::TOO_FAST_SIZE
@ TOO_FAST_SIZE
Definition: ExternalStreamHandler.h:74
ExternalStreamHandler::~ExternalStreamHandler
~ExternalStreamHandler(void) override
Definition: ExternalStreamHandler.h:84
ExternalStreamHandler
Definition: ExternalStreamHandler.h:69
ExternalStreamHandler::m_hasTuner
bool m_hasTuner
Definition: ExternalStreamHandler.h:143
ExternIO::KillIfRunning
static bool KillIfRunning(const QString &cmd)
Definition: ExternalStreamHandler.cpp:248
ExternIO::kMaxErrorCnt
static constexpr uint8_t kMaxErrorCnt
Definition: ExternalStreamHandler.h:31
ExternalStreamHandler::ReplayStream
void ReplayStream(void)
Definition: ExternalStreamHandler.cpp:1090
ExternalStreamHandler::m_tsOpen
bool m_tsOpen
Definition: ExternalStreamHandler.h:137
ExternalStreamHandler::LockReplay
void LockReplay(void)
Definition: ExternalStreamHandler.h:100
ExternIO::Fork
void Fork(void)
Definition: ExternalStreamHandler.cpp:300
ExternalStreamHandler::m_replayBuffer
QByteArray m_replayBuffer
Definition: ExternalStreamHandler.h:146
ExternalStreamHandler::CloseApp
void CloseApp(void)
Definition: ExternalStreamHandler.cpp:1034
ExternIO::m_appErr
int m_appErr
Definition: ExternalStreamHandler.h:55
ExternIO::Ready
bool Ready(int fd, std::chrono::milliseconds timeout, const QString &what)
Definition: ExternalStreamHandler.cpp:78
ExternIO::m_pid
pid_t m_pid
Definition: ExternalStreamHandler.h:56
ExternIO::ErrorString
QString ErrorString(void) const
Definition: ExternalStreamHandler.h:43
ExternalStreamHandler::PurgeBuffer
void PurgeBuffer(void)
Definition: ExternalStreamHandler.cpp:1777
ExternalStreamHandler::APIVersion
int APIVersion(void) const
Definition: ExternalStreamHandler.h:123
ExternIO::m_app
QFileInfo m_app
Definition: ExternalStreamHandler.h:51
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:464
ExternIO::Run
bool Run(void)
Definition: ExternalStreamHandler.cpp:237
ExternalStreamHandler::RestartStream
bool RestartStream(void)
Definition: ExternalStreamHandler.cpp:1072
ExternalStreamHandler::m_majorId
int m_majorId
Definition: ExternalStreamHandler.h:132
ExternalStreamHandler::SetAPIVersion
bool SetAPIVersion(void)
Definition: ExternalStreamHandler.cpp:874
ExternalStreamHandler::s_handlersLock
static QMutex s_handlersLock
Definition: ExternalStreamHandler.h:152
uint
unsigned int uint
Definition: compat.h:81
ExternalChannel
-*- Mode: c++ -*-
Definition: ExternalChannel.h:20
ExternIO::m_appOut
int m_appOut
Definition: ExternalStreamHandler.h:54
ExternIO::m_appIn
int m_appIn
Definition: ExternalStreamHandler.h:53
ExternalStreamHandler::ProcessJson
bool ProcessJson(const QVariantMap &vmsg, QVariantMap &elements, QByteArray &response, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1525
ExternalStreamHandler::TS_PACKET_SIZE
@ TS_PACKET_SIZE
Definition: ExternalStreamHandler.h:72
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1133
ExternalStreamHandler::UpdateDescription
QString UpdateDescription(void)
Definition: ExternalStreamHandler.cpp:899
ExternalStreamHandler::IsAppOpen
bool IsAppOpen(void)
Definition: ExternalStreamHandler.cpp:1007
ExternalStreamHandler::CheckForError
bool CheckForError(void)
Definition: ExternalStreamHandler.cpp:1675
ExternalStreamHandler::ProcessVer2
bool ProcessVer2(const QString &command, QString &result, std::chrono::milliseconds timeout, uint retry_cnt)
Definition: ExternalStreamHandler.cpp:1383
ExternalStreamHandler::m_damaged
bool m_damaged
Definition: ExternalStreamHandler.h:149
ExternalStreamHandler::m_ioErrCnt
int m_ioErrCnt
Definition: ExternalStreamHandler.h:138
ExternalStreamHandler::m_replay
bool m_replay
Definition: ExternalStreamHandler.h:147
ExternalStreamHandler::HasPictureAttributes
bool HasPictureAttributes(void) const
Definition: ExternalStreamHandler.h:96
ExternalStreamHandler::MAX_API_VERSION
@ MAX_API_VERSION
Definition: ExternalStreamHandler.h:71
ExternalStreamHandler::PACKET_SIZE
@ PACKET_SIZE
Definition: ExternalStreamHandler.h:73
ExternIO::Write
int Write(const QByteArray &buffer)
Definition: ExternalStreamHandler.cpp:204
ExternIO::m_statusBuf
QString m_statusBuf
Definition: ExternalStreamHandler.h:62
ExternalStreamHandler::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:135
ExternIO::GetStatus
QByteArray GetStatus(std::chrono::milliseconds timeout=2500ms)
Definition: ExternalStreamHandler.cpp:175
mythchrono.h
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1242
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:14
ExternalStreamHandler::UnlockReplay
void UnlockReplay(bool enable_replay=false)
Definition: ExternalStreamHandler.h:101
ExternalStreamHandler::m_streamingCnt
QAtomicInt m_streamingCnt
Definition: ExternalStreamHandler.h:156
ExternalStreamHandler::m_loc
QString m_loc
Definition: ExternalStreamHandler.h:131
ExternIO::Read
int Read(QByteArray &buffer, int maxlen, std::chrono::milliseconds timeout=2500ms)
Definition: ExternalStreamHandler.cpp:112
ExternIO::m_status
QTextStream m_status
Definition: ExternalStreamHandler.h:63
ExternalStreamHandler::m_streamLock
QMutex m_streamLock
Definition: ExternalStreamHandler.h:157
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:495
ExternalStreamHandler::OpenApp
bool OpenApp(void)
Definition: ExternalStreamHandler.cpp:914
ExternIO::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:52
ExternIO::m_errCnt
int m_errCnt
Definition: ExternalStreamHandler.h:64
ExternalStreamHandler::GetDescription
QString GetDescription(void)
Definition: ExternalStreamHandler.h:89
ExternalStreamHandler::constants
constants
Definition: ExternalStreamHandler.h:71
ExternIO::ClearError
void ClearError(void)
Definition: ExternalStreamHandler.h:44
ExternalStreamHandler::m_app
QString m_app
Definition: ExternalStreamHandler.h:136