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 // _ExternalSTREAMHANDLER_H_
static QMap< int, ExternalStreamHandler * > s_handlers
void ClearError(void)
static QMap< int, uint > s_handlersRefCnt
QString ErrorString(void) const
~ExternalStreamHandler(void) override
QStringList m_args
void UnlockReplay(bool enable_replay=false)
bool HasPictureAttributes(void) const
unsigned int uint
Definition: compat.h:140
bool Error(void) const
This class is intended to detect the presence of needed tables.
#define PACKET_SIZE
Definition: lirc_client.c:1751
QTextStream m_status