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 <QStringList>
19 #include <QTextStream>
20 
21 #include "libmythbase/mythchrono.h"
22 
23 #include "streamhandler.h"
24 
25 class DTVSignalMonitor;
26 class ExternalChannel;
27 
28 class ExternIO
29 {
30  enum constants { kMaxErrorCnt = 20 };
31 
32  public:
33  ExternIO(const QString & app, const QStringList & args);
34  ~ExternIO(void);
35 
36  bool Ready(int fd, std::chrono::milliseconds timeout, const QString & what);
37  int Read(QByteArray & buffer, int maxlen, std::chrono::milliseconds timeout = 2500ms);
38  QString GetStatus(std::chrono::milliseconds timeout = 2500ms);
39  int Write(const QByteArray & buffer);
40  bool Run(void);
41  bool Error(void) const { return !m_error.isEmpty(); }
42  QString ErrorString(void) const { return m_error; }
43  void ClearError(void) { m_error.clear(); }
44 
45  static bool KillIfRunning(const QString & cmd);
46 
47  private:
48  void Fork(void);
49 
50  QFileInfo m_app;
51  QStringList m_args;
52  int m_appIn {-1};
53  int m_appOut {-1};
54  int m_appErr {-1};
55  pid_t m_pid {-1};
56  QString m_error;
57 
58  int m_bufSize {0};
59  char *m_buffer {nullptr};
60 
61  QString m_statusBuf;
62  QTextStream m_status;
63  int m_errCnt {0};
64 };
65 
66 // Note : This class always uses a TS reader.
67 
69 {
74 
75  public:
76  static ExternalStreamHandler *Get(const QString &devname,
77  int inputid, int majorid);
78  static void Return(ExternalStreamHandler * & ref, int inputid);
79 
80  public:
81  explicit ExternalStreamHandler(const QString & path, int inputid,
82  int majorid);
83  ~ExternalStreamHandler(void) override { CloseApp(); }
84 
85  void run(void) override; // MThread
86  void PriorityEvent(int fd) override; // DeviceReaderCB
87 
88  QString GetDescription(void) { return m_loc; }
89  QString UpdateDescription(void);
90  bool IsAppOpen(void);
91  bool IsTSOpen(void);
92  bool HasTuner(void) const { return m_hasTuner; }
93  bool HasPictureAttributes(void) const { return m_hasPictureAttributes; }
94 
95  bool RestartStream(void);
96 
97  void LockReplay(void) { m_replayLock.lock(); }
98  void UnlockReplay(bool enable_replay = false)
99  { m_replay = enable_replay; m_replayLock.unlock(); }
100  void ReplayStream(void);
101  bool StartStreaming(void);
102  bool StopStreaming(void);
103 
104  bool CheckForError(void);
105 
106  void PurgeBuffer(void);
107 
108  bool ProcessCommand(const QString & cmd, QString & result,
109  std::chrono::milliseconds timeout = 4s,
110  uint retry_cnt = 3);
111  bool ProcessVer1(const QString & cmd, QString & result,
112  std::chrono::milliseconds timeout, uint retry_cnt);
113  bool ProcessVer2(const QString & command, QString & result,
114  std::chrono::milliseconds timeout, uint retry_cnt);
115 
116  private:
117  int StreamingCount(void) const;
118  bool SetAPIVersion(void);
119  bool OpenApp(void);
120  void CloseApp(void);
121 
122  QString m_loc;
124  QMutex m_ioLock;
125  ExternIO *m_io {nullptr};
126  QStringList m_args;
127  QString m_app;
128  bool m_tsOpen {false};
129  int m_ioErrCnt {0};
130  bool m_pollMode {false};
131 
132  int m_apiVersion {1};
134  bool m_hasTuner {false};
136 
137  QByteArray m_replayBuffer;
138  bool m_replay {false};
139  bool m_xon {false};
140 
141  // for implementing Get & Return
142  static QMutex s_handlersLock;
143  static QMap<int, ExternalStreamHandler*> s_handlers;
144  static QMap<int, uint> s_handlersRefCnt;
145 
146  QAtomicInt m_streamingCnt;
147  QMutex m_streamLock;
148  QMutex m_replayLock;
150 };
151 
152 #endif // EXTERNAL_STREAMHANDLER_H
ExternalStreamHandler::ProcessVer1
bool ProcessVer1(const QString &cmd, QString &result, std::chrono::milliseconds timeout, uint retry_cnt)
Definition: ExternalStreamHandler.cpp:1253
ExternalStreamHandler::ExternalStreamHandler
ExternalStreamHandler(const QString &path, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:540
build_compdb.args
args
Definition: build_compdb.py:11
ExternalStreamHandler::m_pollMode
bool m_pollMode
Definition: ExternalStreamHandler.h:130
ExternalStreamHandler::IsTSOpen
bool IsTSOpen(void)
Definition: ExternalStreamHandler.cpp:1017
ExternalStreamHandler::m_ioLock
QMutex m_ioLock
Definition: ExternalStreamHandler.h:124
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
ExternalStreamHandler::m_apiVersion
int m_apiVersion
Definition: ExternalStreamHandler.h:132
ExternalStreamHandler::m_serialNo
uint m_serialNo
Definition: ExternalStreamHandler.h:133
streamhandler.h
ExternalStreamHandler::PriorityEvent
void PriorityEvent(int fd) override
Definition: ExternalStreamHandler.cpp:1564
StreamHandler
Definition: streamhandler.h:58
ExternalStreamHandler::m_hasPictureAttributes
bool m_hasPictureAttributes
Definition: ExternalStreamHandler.h:135
ExternalStreamHandler::StreamingCount
int StreamingCount(void) const
Definition: ExternalStreamHandler.cpp:574
ExternIO::constants
constants
Definition: ExternalStreamHandler.h:30
ExternIO::~ExternIO
~ExternIO(void)
Definition: ExternalStreamHandler.cpp:65
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
ExternIO::GetStatus
QString GetStatus(std::chrono::milliseconds timeout=2500ms)
Definition: ExternalStreamHandler.cpp:172
ExternalStreamHandler::m_io
ExternIO * m_io
Definition: ExternalStreamHandler.h:125
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1177
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:92
ExternIO::m_error
QString m_error
Definition: ExternalStreamHandler.h:56
ExternalStreamHandler::m_xon
bool m_xon
Definition: ExternalStreamHandler.h:139
ExternalStreamHandler::m_replayLock
QMutex m_replayLock
Definition: ExternalStreamHandler.h:148
ExternalStreamHandler::m_processLock
QMutex m_processLock
Definition: ExternalStreamHandler.h:149
ExternIO::ExternIO
ExternIO(const QString &app, const QStringList &args)
Definition: ExternalStreamHandler.cpp:36
ExternalStreamHandler::s_handlers
static QMap< int, ExternalStreamHandler * > s_handlers
Definition: ExternalStreamHandler.h:143
ExternIO::m_bufSize
int m_bufSize
Definition: ExternalStreamHandler.h:58
ExternIO::Error
bool Error(void) const
Definition: ExternalStreamHandler.h:41
ExternIO::m_buffer
char * m_buffer
Definition: ExternalStreamHandler.h:59
ExternIO
Definition: ExternalStreamHandler.h:28
ExternalStreamHandler::s_handlersRefCnt
static QMap< int, uint > s_handlersRefCnt
Definition: ExternalStreamHandler.h:144
ExternalStreamHandler::TOO_FAST_SIZE
@ TOO_FAST_SIZE
Definition: ExternalStreamHandler.h:73
ExternalStreamHandler::~ExternalStreamHandler
~ExternalStreamHandler(void) override
Definition: ExternalStreamHandler.h:83
ExternalStreamHandler
Definition: ExternalStreamHandler.h:68
ExternalStreamHandler::m_hasTuner
bool m_hasTuner
Definition: ExternalStreamHandler.h:134
ExternIO::KillIfRunning
static bool KillIfRunning(const QString &cmd)
Definition: ExternalStreamHandler.cpp:245
ExternalStreamHandler::ReplayStream
void ReplayStream(void)
Definition: ExternalStreamHandler.cpp:1086
ExternalStreamHandler::m_tsOpen
bool m_tsOpen
Definition: ExternalStreamHandler.h:128
ExternalStreamHandler::LockReplay
void LockReplay(void)
Definition: ExternalStreamHandler.h:97
ExternIO::Fork
void Fork(void)
Definition: ExternalStreamHandler.cpp:300
ExternalStreamHandler::m_replayBuffer
QByteArray m_replayBuffer
Definition: ExternalStreamHandler.h:137
ExternalStreamHandler::CloseApp
void CloseApp(void)
Definition: ExternalStreamHandler.cpp:1031
ExternIO::m_appErr
int m_appErr
Definition: ExternalStreamHandler.h:54
ExternIO::Ready
bool Ready(int fd, std::chrono::milliseconds timeout, const QString &what)
Definition: ExternalStreamHandler.cpp:75
ExternIO::m_pid
pid_t m_pid
Definition: ExternalStreamHandler.h:55
ExternIO::ErrorString
QString ErrorString(void) const
Definition: ExternalStreamHandler.h:42
ExternalStreamHandler::PurgeBuffer
void PurgeBuffer(void)
Definition: ExternalStreamHandler.cpp:1554
ExternIO::m_app
QFileInfo m_app
Definition: ExternalStreamHandler.h:50
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:460
ExternIO::Run
bool Run(void)
Definition: ExternalStreamHandler.cpp:234
ExternalStreamHandler::RestartStream
bool RestartStream(void)
Definition: ExternalStreamHandler.cpp:1069
ExternalStreamHandler::m_majorId
int m_majorId
Definition: ExternalStreamHandler.h:123
ExternalStreamHandler::SetAPIVersion
bool SetAPIVersion(void)
Definition: ExternalStreamHandler.cpp:866
ExternalStreamHandler::s_handlersLock
static QMutex s_handlersLock
Definition: ExternalStreamHandler.h:142
uint
unsigned int uint
Definition: compat.h:79
ExternalChannel
-*- Mode: c++ -*-
Definition: ExternalChannel.h:20
ExternIO::m_appOut
int m_appOut
Definition: ExternalStreamHandler.h:53
ExternIO::m_appIn
int m_appIn
Definition: ExternalStreamHandler.h:52
ExternalStreamHandler::TS_PACKET_SIZE
@ TS_PACKET_SIZE
Definition: ExternalStreamHandler.h:71
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1129
ExternalStreamHandler::UpdateDescription
QString UpdateDescription(void)
Definition: ExternalStreamHandler.cpp:896
ExternalStreamHandler::IsAppOpen
bool IsAppOpen(void)
Definition: ExternalStreamHandler.cpp:1004
ExternalStreamHandler::CheckForError
bool CheckForError(void)
Definition: ExternalStreamHandler.cpp:1505
ExternalStreamHandler::ProcessVer2
bool ProcessVer2(const QString &command, QString &result, std::chrono::milliseconds timeout, uint retry_cnt)
Definition: ExternalStreamHandler.cpp:1359
ExternalStreamHandler::m_ioErrCnt
int m_ioErrCnt
Definition: ExternalStreamHandler.h:129
ExternalStreamHandler::m_replay
bool m_replay
Definition: ExternalStreamHandler.h:138
ExternalStreamHandler::HasPictureAttributes
bool HasPictureAttributes(void) const
Definition: ExternalStreamHandler.h:93
ExternalStreamHandler::MAX_API_VERSION
@ MAX_API_VERSION
Definition: ExternalStreamHandler.h:70
ExternalStreamHandler::PACKET_SIZE
@ PACKET_SIZE
Definition: ExternalStreamHandler.h:72
ExternIO::Write
int Write(const QByteArray &buffer)
Definition: ExternalStreamHandler.cpp:201
ExternIO::m_statusBuf
QString m_statusBuf
Definition: ExternalStreamHandler.h:61
ExternalStreamHandler::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:126
ExternIO::kMaxErrorCnt
@ kMaxErrorCnt
Definition: ExternalStreamHandler.h:30
mythchrono.h
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1236
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:98
ExternalStreamHandler::m_streamingCnt
QAtomicInt m_streamingCnt
Definition: ExternalStreamHandler.h:146
ExternalStreamHandler::m_loc
QString m_loc
Definition: ExternalStreamHandler.h:122
ExternIO::Read
int Read(QByteArray &buffer, int maxlen, std::chrono::milliseconds timeout=2500ms)
Definition: ExternalStreamHandler.cpp:111
ExternIO::m_status
QTextStream m_status
Definition: ExternalStreamHandler.h:62
ExternalStreamHandler::m_streamLock
QMutex m_streamLock
Definition: ExternalStreamHandler.h:147
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:491
ExternalStreamHandler::OpenApp
bool OpenApp(void)
Definition: ExternalStreamHandler.cpp:911
ExternIO::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:51
ExternIO::m_errCnt
int m_errCnt
Definition: ExternalStreamHandler.h:63
ExternalStreamHandler::GetDescription
QString GetDescription(void)
Definition: ExternalStreamHandler.h:88
ExternalStreamHandler::constants
constants
Definition: ExternalStreamHandler.h:70
ExternIO::ClearError
void ClearError(void)
Definition: ExternalStreamHandler.h:43
ExternalStreamHandler::m_app
QString m_app
Definition: ExternalStreamHandler.h:127