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