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 "DeviceReadBuffer.h" // for ReaderPausedCB
20 #include "mpegstreamdata.h" // for PIDPriority
21 #include "mthread.h"
22 #include "mythdate.h"
23 
24 class ThreadedFileWriter;
25 
26 //#define DEBUG_PID_FILTERS
27 
28 class PIDInfo
29 {
30  public:
31  PIDInfo() = default;
32  explicit PIDInfo(uint pid) : m_pid(pid) {}
33  PIDInfo(uint pid, uint stream_type, int pes_type) :
34  m_pid(pid), m_streamType(stream_type), m_pesType(pes_type) {;}
35  virtual ~PIDInfo() {;}
36 
37  virtual bool Open(const QString &/*dev*/, bool /*use_section_reader*/)
38  { return false; }
39  virtual bool Close(const QString &/*dev*/) { return false; }
40  bool IsOpen(void) const { return m_filterFd >= 0; }
41 
42  uint m_pid { UINT_MAX };
43  int m_filterFd { -1 };
45  int m_pesType { -1 };
46 };
47 // Please do not change this to hash or other unordered container.
48 // HDHRStreamHandler::UpdateFilters() relies on the forward
49 // iterator returning these in order of ascending pid number.
50 using PIDInfoMap = QMap<uint,PIDInfo*>;
51 
52 // locking order
53 // _pid_lock -> _listener_lock
54 // _add_rm_lock -> _listener_lock
55 // -> _start_stop_lock
56 
57 class StreamHandler : protected MThread, public DeviceReaderCB
58 {
59  public:
60  virtual void AddListener(MPEGStreamData *data,
61  bool allow_section_reader = false,
62  bool needs_buffering = false,
63  const QString& output_file= QString());
64  virtual void RemoveListener(MPEGStreamData *data);
65  bool IsRunning(void) const;
66  bool HasError(void) const { return m_bError; }
67 
69  virtual bool AddNamedOutputFile(const QString &filename);
71  virtual void RemoveNamedOutputFile(const QString &filename);
72 
73  protected:
74  explicit StreamHandler(QString device, int inputid)
75  : MThread("StreamHandler"), m_device(std::move(device)), m_inputId(inputid) {}
76  ~StreamHandler() override;
77 
78  void Start(void);
79  void Stop(void);
80 
81  void SetRunning(bool running,
82  bool using_buffering,
83  bool using_section_reader);
84 
85  bool AddPIDFilter(PIDInfo *info);
86  bool RemovePIDFilter(uint pid);
87  bool RemoveAllPIDFilters(void);
88 
89  void UpdateListeningForEIT(void);
90  bool UpdateFiltersFromStreamData(void);
91  virtual bool UpdateFilters(void) { return true; }
92  virtual void CycleFiltersByPriority() {}
93 
94  PIDPriority GetPIDPriority(uint pid) const;
95 
96  // DeviceReaderCB
97  void ReaderPaused(int fd) override { (void) fd; } // DeviceReaderCB
98  void PriorityEvent(int fd) override { (void) fd; } // DeviceReaderCB
99 
100  virtual PIDInfo *CreatePIDInfo(uint pid, uint stream_type, int pes_type)
101  { return new PIDInfo(pid, stream_type, pes_type); }
102 
103  protected:
105  void WriteMPTS(const unsigned char * buffer, uint len);
109  virtual void SetRunningDesired(bool desired);
110 
111  protected:
112  QString m_device;
114  bool m_needsBuffering {false};
115  bool m_allowSectionReader {false};
116 
117  QMutex m_addRmLock;
118 
119  mutable QMutex m_startStopLock;
120  volatile bool m_runningDesired {false};
121 
122  // not to be confused with the other four header files that define
123  // m_error to be a QString. Somehow v4l2encstreamhandler.cpp
124  // blends these into a single class.
125  volatile bool m_bError {false};
126  bool m_running {false};
127  bool m_restarting {false};
128  bool m_usingBuffering {false};
129  bool m_usingSectionReader {false};
130  QWaitCondition m_runningStateChanged;
131 
132 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
133  mutable QMutex m_pidLock {QMutex::Recursive};
134 #else
135  mutable QRecursiveMutex m_pidLock;
136 #endif
137  std::vector<uint> m_eitPids;
141 
143  QSet<QString> m_mptsFiles;
144  QString m_mptsBaseFile;
145  QMutex m_mptsLock;
146 
147  using StreamDataList = QHash<MPEGStreamData*,QString>;
148 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
149  mutable QMutex m_listenerLock {QMutex::Recursive};
150 #else
151  mutable QRecursiveMutex m_listenerLock;
152 #endif
154 };
155 
156 #endif // STREAM_HANDLER_H
StreamHandler::StreamDataList
QHash< MPEGStreamData *, QString > StreamDataList
Definition: streamhandler.h:147
PIDInfo::Close
virtual bool Close(const QString &)
Definition: streamhandler.h:39
StreamHandler::RemoveAllPIDFilters
bool RemoveAllPIDFilters(void)
Definition: streamhandler.cpp:234
StreamHandler::SetRunning
void SetRunning(bool running, bool using_buffering, bool using_section_reader)
Definition: streamhandler.cpp:171
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:349
StreamHandler::Stop
void Stop(void)
Definition: streamhandler.cpp:153
StreamHandler::CycleFiltersByPriority
virtual void CycleFiltersByPriority()
Definition: streamhandler.h:92
StreamHandler
Definition: streamhandler.h:57
StreamHandler::m_mptsTfw
ThreadedFileWriter * m_mptsTfw
Definition: streamhandler.h:142
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:66
PIDInfo
Definition: streamhandler.h:28
PIDInfo::PIDInfo
PIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:33
StreamHandler::AddNamedOutputFile
virtual bool AddNamedOutputFile(const QString &filename)
Called with _listener_lock locked just after adding new output file.
Definition: streamhandler.cpp:356
StreamHandler::StreamHandler
StreamHandler(QString device, int inputid)
Definition: streamhandler.h:74
StreamHandler::m_startStopLock
QMutex m_startStopLock
Definition: streamhandler.h:119
StreamHandler::GetPIDPriority
PIDPriority GetPIDPriority(uint pid) const
Definition: streamhandler.cpp:337
DeviceReaderCB
Definition: DeviceReadBuffer.h:18
PIDInfo::IsOpen
bool IsOpen(void) const
Definition: streamhandler.h:40
PIDInfo::m_streamType
uint m_streamType
StreamID.
Definition: streamhandler.h:44
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:78
StreamHandler::m_inputId
int m_inputId
Definition: streamhandler.h:113
StreamHandler::PriorityEvent
void PriorityEvent(int fd) override
Definition: streamhandler.h:98
StreamHandler::m_openPidFilters
uint m_openPidFilters
Definition: streamhandler.h:139
StreamHandler::m_usingSectionReader
bool m_usingSectionReader
Definition: streamhandler.h:129
StreamHandler::m_needsBuffering
bool m_needsBuffering
Definition: streamhandler.h:114
StreamHandler::ReaderPaused
void ReaderPaused(int fd) override
Definition: streamhandler.h:97
StreamHandler::m_listenerLock
QRecursiveMutex m_listenerLock
Definition: streamhandler.h:151
mythdate.h
StreamHandler::m_running
bool m_running
Definition: streamhandler.h:126
StreamHandler::m_pidInfo
PIDInfoMap m_pidInfo
Definition: streamhandler.h:138
StreamHandler::m_pidLock
QRecursiveMutex m_pidLock
Definition: streamhandler.h:135
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:182
StreamHandler::m_mptsLock
QMutex m_mptsLock
Definition: streamhandler.h:145
StreamHandler::m_usingBuffering
bool m_usingBuffering
Definition: streamhandler.h:128
StreamHandler::UpdateFiltersFromStreamData
bool UpdateFiltersFromStreamData(void)
Definition: streamhandler.cpp:284
MPEGStreamData
Encapsulates data about MPEG stream and emits events for each table.
Definition: mpegstreamdata.h:84
StreamHandler::RemoveNamedOutputFile
virtual void RemoveNamedOutputFile(const QString &filename)
Called with _listener_lock locked just before removing old output file.
Definition: streamhandler.cpp:399
PIDInfo::PIDInfo
PIDInfo()=default
uint
unsigned int uint
Definition: compat.h:140
StreamHandler::CreatePIDInfo
virtual PIDInfo * CreatePIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:100
StreamHandler::~StreamHandler
~StreamHandler() override
Definition: streamhandler.cpp:15
mpegstreamdata.h
StreamHandler::UpdateListeningForEIT
void UpdateListeningForEIT(void)
Definition: streamhandler.cpp:253
StreamHandler::AddPIDFilter
bool AddPIDFilter(PIDInfo *info)
Definition: streamhandler.cpp:189
ThreadedFileWriter
This class supports the writing of recordings to disk.
Definition: threadedfilewriter.h:42
StreamHandler::m_mptsFiles
QSet< QString > m_mptsFiles
Definition: streamhandler.h:143
PIDInfo::~PIDInfo
virtual ~PIDInfo()
Definition: streamhandler.h:35
StreamHandler::UpdateFilters
virtual bool UpdateFilters(void)
Definition: streamhandler.h:91
StreamHandler::m_cycleTimer
MythTimer m_cycleTimer
Definition: streamhandler.h:140
PIDInfo::m_pid
uint m_pid
Definition: streamhandler.h:42
PIDInfo::m_pesType
int m_pesType
PESStreamID.
Definition: streamhandler.h:45
StreamHandler::m_runningDesired
volatile bool m_runningDesired
Definition: streamhandler.h:120
PIDInfoMap
QMap< uint, PIDInfo * > PIDInfoMap
Definition: streamhandler.h:50
PIDInfo::m_filterFd
int m_filterFd
Input filter file descriptor.
Definition: streamhandler.h:43
StreamHandler::m_runningStateChanged
QWaitCondition m_runningStateChanged
Definition: streamhandler.h:130
std
Definition: mythchrono.h:23
PIDInfo::Open
virtual bool Open(const QString &, bool)
Definition: streamhandler.h:37
StreamHandler::m_mptsBaseFile
QString m_mptsBaseFile
Definition: streamhandler.h:144
StreamHandler::m_streamDataList
StreamDataList m_streamDataList
Definition: streamhandler.h:153
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:204
mthread.h
StreamHandler::m_bError
volatile bool m_bError
Definition: streamhandler.h:125
StreamHandler::m_eitPids
std::vector< uint > m_eitPids
Definition: streamhandler.h:137
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
Definition: streamhandler.cpp:33
PIDInfo::PIDInfo
PIDInfo(uint pid)
Definition: streamhandler.h:32
StreamHandler::m_addRmLock
QMutex m_addRmLock
Definition: streamhandler.h:117
StreamHandler::m_restarting
bool m_restarting
Definition: streamhandler.h:127
StreamHandler::IsRunning
bool IsRunning(void) const
Definition: streamhandler.cpp:161
DeviceReadBuffer.h
StreamHandler::m_device
QString m_device
Definition: streamhandler.h:112
build_compdb.filename
filename
Definition: build_compdb.py:21
StreamHandler::Start
void Start(void)
Definition: streamhandler.cpp:115
PIDPriority
PIDPriority
Definition: mpegstreamdata.h:75
StreamHandler::m_allowSectionReader
bool m_allowSectionReader
Definition: streamhandler.h:115