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 <vector>
7 using namespace std;
8 
9 #include <QWaitCondition>
10 #include <QString>
11 #include <QMutex>
12 #include <QMap>
13 
14 #include "DeviceReadBuffer.h" // for ReaderPausedCB
15 #include "mpegstreamdata.h" // for PIDPriority
16 #include "mthread.h"
17 #include "mythdate.h"
18 
19 class ThreadedFileWriter;
20 
21 //#define DEBUG_PID_FILTERS
22 
23 class PIDInfo
24 {
25  public:
26  PIDInfo() :
27  _pid(0xffffffff), filter_fd(-1), streamType(0), pesType(-1) {;}
28  explicit PIDInfo(uint pid) :
29  _pid(pid), filter_fd(-1), streamType(0), pesType(-1) {;}
30  PIDInfo(uint pid, uint stream_type, int pes_type) :
31  _pid(pid), filter_fd(-1),
32  streamType(stream_type), 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 filter_fd >= 0; }
39 
41  int filter_fd;
43  int pesType;
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 typedef QMap<uint,PIDInfo*> PIDInfoMap;
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(const QString &device, int inputid)
73  : MThread("StreamHandler"), m_device(device), m_inputid(inputid) {}
74  ~StreamHandler();
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(unsigned char * buffer, uint len);
107  virtual void SetRunningDesired(bool desired);
108 
109  protected:
110  QString m_device;
112  bool m_needs_buffering {false};
113  bool m_allow_section_reader {false};
114 
116 
117  mutable QMutex m_start_stop_lock;
118  volatile bool m_running_desired {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_using_buffering {false};
127  bool m_using_section_reader {false};
128  QWaitCondition m_running_state_changed;
129 
130  mutable QMutex m_pid_lock {QMutex::Recursive};
131  vector<uint> m_eit_pids;
133  uint m_open_pid_filters {0};
135 
136  ThreadedFileWriter *m_mpts_tfw {nullptr};
137  QSet<QString> m_mpts_files;
139  QMutex m_mpts_lock;
140 
141  typedef QMap<MPEGStreamData*,QString> StreamDataList;
142  mutable QMutex m_listener_lock {QMutex::Recursive};
144 };
145 
146 #endif // _STREAM_HANDLER_H_
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
bool IsOpen(void) const
Definition: streamhandler.h:38
virtual bool Close(const QString &)
Definition: streamhandler.h:37
virtual PIDInfo * CreatePIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:98
virtual bool Open(const QString &, bool)
Definition: streamhandler.h:35
QString m_mpts_base_file
QSet< QString > m_mpts_files
QWaitCondition m_running_state_changed
QString m_device
void PriorityEvent(int fd) override
Definition: streamhandler.h:96
unsigned int uint
Definition: compat.h:140
int pesType
PESStreamID.
Definition: streamhandler.h:43
bool HasError(void) const
Definition: streamhandler.h:64
virtual ~PIDInfo()
Definition: streamhandler.h:33
virtual bool UpdateFilters(void)
Definition: streamhandler.h:89
uint streamType
StreamID.
Definition: streamhandler.h:42
PIDInfo(uint pid)
Definition: streamhandler.h:28
QMutex m_add_rm_lock
int filter_fd
Input filter file descriptor.
Definition: streamhandler.h:41
QMap< MPEGStreamData *, QString > StreamDataList
PIDInfoMap m_pid_info
PIDPriority
QMap< uint, PIDInfo * > PIDInfoMap
Definition: streamhandler.h:48
MythTimer m_cycle_timer
StreamDataList m_stream_data_list
virtual void CycleFiltersByPriority()
Definition: streamhandler.h:90
QMutex m_mpts_lock
This class supports the writing of recordings to disk.
QMutex m_start_stop_lock
void ReaderPaused(int fd) override
Definition: streamhandler.h:95
PIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:30
uint _pid
Definition: streamhandler.h:40
Encapsulates data about MPEG stream and emits events for each table.
vector< uint > m_eit_pids
StreamHandler(const QString &device, int inputid)
Definition: streamhandler.h:72