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