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