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 using namespace std;
9 
10 #include <QString>
11 #include <QAtomicInt>
12 #include <QMutex>
13 #include <QMap>
14 
15 #include "streamhandler.h"
16 
17 class DTVSignalMonitor;
18 class ExternalChannel;
19 
20 class ExternIO
21 {
22  enum constants { kMaxErrorCnt = 20 };
23 
24  public:
25  ExternIO(const QString & app, const QStringList & args);
26  ~ExternIO(void);
27 
28  bool Ready(int fd, int timeout, const QString & what);
29  int Read(QByteArray & buffer, int maxlen, int timeout = 2500);
30  QString GetStatus(int timeout = 2500);
31  int Write(const QByteArray & buffer);
32  bool Run(void);
33  bool Error(void) const { return !m_error.isEmpty(); }
34  QString ErrorString(void) const { return m_error; }
35  void ClearError(void) { m_error.clear(); }
36 
37  static bool KillIfRunning(const QString & cmd);
38 
39  private:
40  void Fork(void);
41 
42  QFileInfo m_app;
43  QStringList m_args;
44  int m_appIn {-1};
45  int m_appOut {-1};
46  int m_appErr {-1};
47  pid_t m_pid {-1};
48  QString m_error;
49 
50  int m_bufSize {0};
51  char *m_buffer {nullptr};
52 
53  QString m_statusBuf;
54  QTextStream m_status;
55  int m_errCnt {0};
56 };
57 
58 // Note : This class always uses a TS reader.
59 
61 {
62  enum constants { MAX_API_VERSION = 2,
63  TS_PACKET_SIZE = 188,
64  PACKET_SIZE = TS_PACKET_SIZE * 8192,
65  TOO_FAST_SIZE = TS_PACKET_SIZE * 32768 };
66 
67  public:
68  static ExternalStreamHandler *Get(const QString &devname,
69  int inputid, int majorid);
70  static void Return(ExternalStreamHandler * & ref, int inputid);
71 
72  public:
73  explicit ExternalStreamHandler(const QString & path, int inputid,
74  int majorid);
75  ~ExternalStreamHandler(void) override { CloseApp(); }
76 
77  void run(void) override; // MThread
78  void PriorityEvent(int fd) override; // DeviceReaderCB
79 
80  QString GetDescription(void) { return m_loc; }
81  QString UpdateDescription(void);
82  bool IsAppOpen(void);
83  bool IsTSOpen(void);
84  bool HasTuner(void) const { return m_hasTuner; }
85  bool HasPictureAttributes(void) const { return m_hasPictureAttributes; }
86 
87  bool RestartStream(void);
88 
89  void LockReplay(void) { m_replayLock.lock(); }
90  void UnlockReplay(bool enable_replay = false)
91  { m_replay = enable_replay; m_replayLock.unlock(); }
92  void ReplayStream(void);
93  bool StartStreaming(void);
94  bool StopStreaming(void);
95 
96  bool CheckForError(void);
97 
98  void PurgeBuffer(void);
99 
100  bool ProcessCommand(const QString & cmd, QString & result,
101  int timeout = 4000 /* ms */,uint retry_cnt = 3);
102  bool ProcessVer1(const QString & cmd, QString & result,
103  int timeout /* ms */, uint retry_cnt);
104  bool ProcessVer2(const QString & command, QString & result,
105  int timeout /* ms */, 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};
124  uint m_serialNo {0};
125  bool m_hasTuner {false};
126  bool m_hasPictureAttributes {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
build_compdb.args
args
Definition: build_compdb.py:11
ExternalStreamHandler::m_ioLock
QMutex m_ioLock
Definition: ExternalStreamHandler.h:115
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
streamhandler.h
StreamHandler
Definition: streamhandler.h:55
ExternIO::constants
constants
Definition: ExternalStreamHandler.h:22
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:84
ExternIO::m_error
QString m_error
Definition: ExternalStreamHandler.h:48
ExternalStreamHandler::m_replayLock
QMutex m_replayLock
Definition: ExternalStreamHandler.h:139
ExternalStreamHandler::m_processLock
QMutex m_processLock
Definition: ExternalStreamHandler.h:140
ExternalStreamHandler::s_handlers
static QMap< int, ExternalStreamHandler * > s_handlers
Definition: ExternalStreamHandler.h:134
ExternIO::Error
bool Error(void) const
Definition: ExternalStreamHandler.h:33
ExternIO
Definition: ExternalStreamHandler.h:20
ExternalStreamHandler::s_handlersRefCnt
static QMap< int, uint > s_handlersRefCnt
Definition: ExternalStreamHandler.h:135
ExternalStreamHandler::~ExternalStreamHandler
~ExternalStreamHandler(void) override
Definition: ExternalStreamHandler.h:75
ExternalStreamHandler
Definition: ExternalStreamHandler.h:60
ExternalStreamHandler::LockReplay
void LockReplay(void)
Definition: ExternalStreamHandler.h:89
ExternalStreamHandler::m_replayBuffer
QByteArray m_replayBuffer
Definition: ExternalStreamHandler.h:128
ExternIO::ErrorString
QString ErrorString(void) const
Definition: ExternalStreamHandler.h:34
ExternIO::m_app
QFileInfo m_app
Definition: ExternalStreamHandler.h:42
ExternalStreamHandler::m_majorId
int m_majorId
Definition: ExternalStreamHandler.h:114
ExternalStreamHandler::s_handlersLock
static QMutex s_handlersLock
Definition: ExternalStreamHandler.h:133
uint
unsigned int uint
Definition: compat.h:140
ExternalChannel
Definition: ExternalChannel.h:18
ExternalStreamHandler::HasPictureAttributes
bool HasPictureAttributes(void) const
Definition: ExternalStreamHandler.h:85
ExternIO::m_statusBuf
QString m_statusBuf
Definition: ExternalStreamHandler.h:53
ExternalStreamHandler::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:117
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:15
ExternalStreamHandler::UnlockReplay
void UnlockReplay(bool enable_replay=false)
Definition: ExternalStreamHandler.h:90
ExternalStreamHandler::m_streamingCnt
QAtomicInt m_streamingCnt
Definition: ExternalStreamHandler.h:137
ExternalStreamHandler::m_loc
QString m_loc
Definition: ExternalStreamHandler.h:113
ExternIO::m_status
QTextStream m_status
Definition: ExternalStreamHandler.h:54
ExternalStreamHandler::m_streamLock
QMutex m_streamLock
Definition: ExternalStreamHandler.h:138
ExternIO::m_args
QStringList m_args
Definition: ExternalStreamHandler.h:43
ExternalStreamHandler::GetDescription
QString GetDescription(void)
Definition: ExternalStreamHandler.h:80
ExternalStreamHandler::constants
constants
Definition: ExternalStreamHandler.h:62
ExternIO::ClearError
void ClearError(void)
Definition: ExternalStreamHandler.h:35
ExternalStreamHandler::m_app
QString m_app
Definition: ExternalStreamHandler.h:118
PACKET_SIZE
#define PACKET_SIZE
Definition: lirc_client.cpp:1751