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