MythTV  master
streamhandler.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef STREAM_HANDLER_H
4 #define STREAM_HANDLER_H
5 
6 #include <utility>
7 #include <vector>
8 
9 // Qt headers
10 #include <QWaitCondition>
11 #include <QString>
12 #include <QMutex>
13 #include <QMap>
14 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
15 #include <QRecursiveMutex>
16 #endif
17 
18 // MythTV headers
19 #include "libmythbase/mthread.h"
20 #include "libmythbase/mythdate.h"
21 
22 #include "DeviceReadBuffer.h" // for ReaderPausedCB
23 #include "mpeg/mpegstreamdata.h" // for PIDPriority
24 
25 class ThreadedFileWriter;
26 
27 //#define DEBUG_PID_FILTERS
28 
29 class PIDInfo
30 {
31  public:
32  PIDInfo() = default;
33  explicit PIDInfo(uint pid) : m_pid(pid) {}
34  PIDInfo(uint pid, uint stream_type, int pes_type) :
35  m_pid(pid), m_streamType(stream_type), m_pesType(pes_type) {;}
36  virtual ~PIDInfo() {;}
37 
38  virtual bool Open(const QString &/*dev*/, bool /*use_section_reader*/)
39  { return false; }
40  virtual bool Close(const QString &/*dev*/) { return false; }
41  bool IsOpen(void) const { return m_filterFd >= 0; }
42 
43  uint m_pid { UINT_MAX };
44  int m_filterFd { -1 };
46  int m_pesType { -1 };
47 };
48 // Please do not change this to hash or other unordered container.
49 // HDHRStreamHandler::UpdateFilters() relies on the forward
50 // iterator returning these in order of ascending pid number.
51 using PIDInfoMap = QMap<uint,PIDInfo*>;
52 
53 // locking order
54 // _pid_lock -> _listener_lock
55 // _add_rm_lock -> _listener_lock
56 // -> _start_stop_lock
57 
58 class StreamHandler : protected MThread, public DeviceReaderCB
59 {
60  public:
61  virtual void AddListener(MPEGStreamData *data,
62  bool allow_section_reader = false,
63  bool needs_buffering = false,
64  const QString& output_file= QString());
65  virtual void RemoveListener(MPEGStreamData *data);
66  bool IsRunning(void) const;
67  bool HasError(void) const { return m_bError; }
68 
70  virtual bool AddNamedOutputFile(const QString &filename);
72  virtual void RemoveNamedOutputFile(const QString &filename);
73 
74  protected:
75  explicit StreamHandler(QString device, int inputid)
76  : MThread("StreamHandler"), m_device(std::move(device)), m_inputId(inputid) {}
77  ~StreamHandler() override;
78 
79  void Start(void);
80  void Stop(void);
81 
82  void SetRunning(bool running,
83  bool using_buffering,
84  bool using_section_reader);
85 
86  bool AddPIDFilter(PIDInfo *info);
87  bool RemovePIDFilter(uint pid);
88  bool RemoveAllPIDFilters(void);
89 
90  void UpdateListeningForEIT(void);
91  bool UpdateFiltersFromStreamData(void);
92  virtual bool UpdateFilters(void) { return true; }
93  virtual void CycleFiltersByPriority() {}
94 
95  PIDPriority GetPIDPriority(uint pid) const;
96 
97  // DeviceReaderCB
98  void ReaderPaused(int fd) override { (void) fd; } // DeviceReaderCB
99  void PriorityEvent(int fd) override { (void) fd; } // DeviceReaderCB
100 
101  virtual PIDInfo *CreatePIDInfo(uint pid, uint stream_type, int pes_type)
102  { return new PIDInfo(pid, stream_type, pes_type); }
103 
104  protected:
106  void WriteMPTS(const unsigned char * buffer, uint len);
110  virtual void SetRunningDesired(bool desired);
111 
112  protected:
113  QString m_device;
115  bool m_needsBuffering {false};
116  bool m_allowSectionReader {false};
117 
118  QMutex m_addRmLock;
119 
120  mutable QMutex m_startStopLock;
121  volatile bool m_runningDesired {false};
122 
123  // not to be confused with the other four header files that define
124  // m_error to be a QString. Somehow v4l2encstreamhandler.cpp
125  // blends these into a single class.
126  volatile bool m_bError {false};
127  bool m_running {false};
128  bool m_restarting {false};
129  bool m_usingBuffering {false};
130  bool m_usingSectionReader {false};
131  QWaitCondition m_runningStateChanged;
132 
133 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
134  mutable QMutex m_pidLock {QMutex::Recursive};
135 #else
136  mutable QRecursiveMutex m_pidLock;
137 #endif
138  std::vector<uint> m_eitPids;
141  bool m_filtersChanged {false};
143 
145  QSet<QString> m_mptsFiles;
146  QString m_mptsBaseFile;
147  QMutex m_mptsLock;
148 
149  using StreamDataList = QHash<MPEGStreamData*,QString>;
150 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
151  mutable QMutex m_listenerLock {QMutex::Recursive};
152 #else
153  mutable QRecursiveMutex m_listenerLock;
154 #endif
156 };
157 
158 #endif // STREAM_HANDLER_H
StreamHandler::StreamDataList
QHash< MPEGStreamData *, QString > StreamDataList
Definition: streamhandler.h:149
PIDInfo::Close
virtual bool Close(const QString &)
Definition: streamhandler.h:40
StreamHandler::RemoveAllPIDFilters
bool RemoveAllPIDFilters(void)
Definition: streamhandler.cpp:240
StreamHandler::SetRunning
void SetRunning(bool running, bool using_buffering, bool using_section_reader)
Definition: streamhandler.cpp:173
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
StreamHandler::WriteMPTS
void WriteMPTS(const unsigned char *buffer, uint len)
Write out a copy of the raw MPTS.
Definition: streamhandler.cpp:355
StreamHandler::Stop
void Stop(void)
Definition: streamhandler.cpp:155
StreamHandler::CycleFiltersByPriority
virtual void CycleFiltersByPriority()
Definition: streamhandler.h:93
StreamHandler
Definition: streamhandler.h:58
StreamHandler::m_mptsTfw
ThreadedFileWriter * m_mptsTfw
Definition: streamhandler.h:144
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:67
PIDInfo
Definition: streamhandler.h:29
PIDInfo::PIDInfo
PIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:34
StreamHandler::AddNamedOutputFile
virtual bool AddNamedOutputFile(const QString &filename)
Called with _listener_lock locked just after adding new output file.
Definition: streamhandler.cpp:362
StreamHandler::StreamHandler
StreamHandler(QString device, int inputid)
Definition: streamhandler.h:75
StreamHandler::m_startStopLock
QMutex m_startStopLock
Definition: streamhandler.h:120
StreamHandler::m_filtersChanged
bool m_filtersChanged
Definition: streamhandler.h:141
StreamHandler::GetPIDPriority
PIDPriority GetPIDPriority(uint pid) const
Definition: streamhandler.cpp:343
DeviceReaderCB
Definition: DeviceReadBuffer.h:19
PIDInfo::IsOpen
bool IsOpen(void) const
Definition: streamhandler.h:41
PIDInfo::m_streamType
uint m_streamType
StreamID.
Definition: streamhandler.h:45
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:80
StreamHandler::m_inputId
int m_inputId
Definition: streamhandler.h:114
StreamHandler::PriorityEvent
void PriorityEvent(int fd) override
Definition: streamhandler.h:99
StreamHandler::m_openPidFilters
uint m_openPidFilters
Definition: streamhandler.h:140
StreamHandler::m_usingSectionReader
bool m_usingSectionReader
Definition: streamhandler.h:130
StreamHandler::m_needsBuffering
bool m_needsBuffering
Definition: streamhandler.h:115
StreamHandler::ReaderPaused
void ReaderPaused(int fd) override
Definition: streamhandler.h:98
StreamHandler::m_listenerLock
QRecursiveMutex m_listenerLock
Definition: streamhandler.h:153
mythdate.h
StreamHandler::m_running
bool m_running
Definition: streamhandler.h:127
StreamHandler::m_pidInfo
PIDInfoMap m_pidInfo
Definition: streamhandler.h:139
StreamHandler::m_pidLock
QRecursiveMutex m_pidLock
Definition: streamhandler.h:136
StreamHandler::SetRunningDesired
virtual void SetRunningDesired(bool desired)
At minimum this sets _running_desired, this may also send signals to anything that might be blocking ...
Definition: streamhandler.cpp:184
StreamHandler::m_mptsLock
QMutex m_mptsLock
Definition: streamhandler.h:147
StreamHandler::m_usingBuffering
bool m_usingBuffering
Definition: streamhandler.h:129
StreamHandler::UpdateFiltersFromStreamData
bool UpdateFiltersFromStreamData(void)
Definition: streamhandler.cpp:290
MPEGStreamData
Encapsulates data about MPEG stream and emits events for each table.
Definition: mpegstreamdata.h:85
StreamHandler::RemoveNamedOutputFile
virtual void RemoveNamedOutputFile(const QString &filename)
Called with _listener_lock locked just before removing old output file.
Definition: streamhandler.cpp:406
PIDInfo::PIDInfo
PIDInfo()=default
uint
unsigned int uint
Definition: compat.h:79
StreamHandler::CreatePIDInfo
virtual PIDInfo * CreatePIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:101
StreamHandler::~StreamHandler
~StreamHandler() override
Definition: streamhandler.cpp:17
mpegstreamdata.h
StreamHandler::UpdateListeningForEIT
void UpdateListeningForEIT(void)
Definition: streamhandler.cpp:259
StreamHandler::AddPIDFilter
bool AddPIDFilter(PIDInfo *info)
Definition: streamhandler.cpp:191
ThreadedFileWriter
This class supports the writing of recordings to disk.
Definition: threadedfilewriter.h:42
StreamHandler::m_mptsFiles
QSet< QString > m_mptsFiles
Definition: streamhandler.h:145
PIDInfo::~PIDInfo
virtual ~PIDInfo()
Definition: streamhandler.h:36
StreamHandler::UpdateFilters
virtual bool UpdateFilters(void)
Definition: streamhandler.h:92
StreamHandler::m_cycleTimer
MythTimer m_cycleTimer
Definition: streamhandler.h:142
PIDInfo::m_pid
uint m_pid
Definition: streamhandler.h:43
PIDInfo::m_pesType
int m_pesType
PESStreamID.
Definition: streamhandler.h:46
StreamHandler::m_runningDesired
volatile bool m_runningDesired
Definition: streamhandler.h:121
PIDInfoMap
QMap< uint, PIDInfo * > PIDInfoMap
Definition: streamhandler.h:51
PIDInfo::m_filterFd
int m_filterFd
Input filter file descriptor.
Definition: streamhandler.h:44
StreamHandler::m_runningStateChanged
QWaitCondition m_runningStateChanged
Definition: streamhandler.h:131
std
Definition: mythchrono.h:23
PIDInfo::Open
virtual bool Open(const QString &, bool)
Definition: streamhandler.h:38
StreamHandler::m_mptsBaseFile
QString m_mptsBaseFile
Definition: streamhandler.h:146
StreamHandler::m_streamDataList
StreamDataList m_streamDataList
Definition: streamhandler.h:155
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
StreamHandler::RemovePIDFilter
bool RemovePIDFilter(uint pid)
Definition: streamhandler.cpp:208
mthread.h
StreamHandler::m_bError
volatile bool m_bError
Definition: streamhandler.h:126
StreamHandler::m_eitPids
std::vector< uint > m_eitPids
Definition: streamhandler.h:138
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
Definition: streamhandler.cpp:35
PIDInfo::PIDInfo
PIDInfo(uint pid)
Definition: streamhandler.h:33
StreamHandler::m_addRmLock
QMutex m_addRmLock
Definition: streamhandler.h:118
StreamHandler::m_restarting
bool m_restarting
Definition: streamhandler.h:128
StreamHandler::IsRunning
bool IsRunning(void) const
Definition: streamhandler.cpp:163
DeviceReadBuffer.h
StreamHandler::m_device
QString m_device
Definition: streamhandler.h:113
build_compdb.filename
filename
Definition: build_compdb.py:21
StreamHandler::Start
void Start(void)
Definition: streamhandler.cpp:117
PIDPriority
PIDPriority
Definition: mpegstreamdata.h:76
StreamHandler::m_allowSectionReader
bool m_allowSectionReader
Definition: streamhandler.h:116