MythTV  master
iptvrecorder.cpp
Go to the documentation of this file.
1 
9 #include <chrono> // for milliseconds
10 #include <thread> // for sleep_for
11 
12 // MythTV headers
13 #include "iptvchannel.h"
14 #include "iptvrecorder.h"
15 #include "mpeg/mpegstreamdata.h"
16 #include "tv_rec.h"
17 
18 #define LOC QString("IPTVRec: ")
19 
21  DTVRecorder(rec), m_channel(channel)
22 {
23 }
24 
26 {
27  StopRecording();
28  Close();
29 }
30 
32 {
33  if (IsOpen())
34  {
35  LOG(VB_GENERAL, LOG_INFO, LOC + "Stream already open");
36  return true;
37  }
38 
40 
41  if (!m_channel->Open())
42  LOG(VB_GENERAL, LOG_ERR, LOC + "Open: Open channel failed");
43 
44  LOG(VB_RECORD, LOG_INFO, LOC + "opened successfully");
45 
46  if (m_streamData)
48 
49  return m_streamData;
50 }
51 
52 bool IPTVRecorder::IsOpen(void) const
53 {
54  if (!m_channel)
55  return false;
56  return m_channel->IsOpen();
57 }
58 
60 {
61  LOG(VB_RECORD, LOG_INFO, LOC + "Close()");
62 
63  m_channel->Close();
64 }
65 
67 {
69  if (IsOpen() && !IsPaused())
71 }
72 
73 bool IPTVRecorder::PauseAndWait(std::chrono::milliseconds timeout)
74 {
75  QMutexLocker locker(&m_pauseLock);
76  if (m_requestPause)
77  {
78  if (!IsPaused(true))
79  {
80  m_channel->SetStreamData(nullptr);
81  m_paused = true;
82  m_pauseWait.wakeAll();
83  if (m_tvrec)
85  }
86 
87  m_unpauseWait.wait(&m_pauseLock, timeout.count());
88  }
89 
90  if (!m_requestPause && IsPaused(true))
91  {
93  m_paused = false;
94  m_unpauseWait.wakeAll();
95  }
96 
97  return IsPaused(true);
98 }
99 
101 {
102  // Make sure the first things in the file are a PAT & PMT
105 }
106 
108 {
109  LOG(VB_RECORD, LOG_INFO, LOC + "run -- begin");
110 
111  if (!Open())
112  {
113  m_error = "Failed to open IPTVRecorder device";
114  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
115  return;
116  }
117 
118  {
119  QMutexLocker locker(&m_pauseLock);
120  m_requestRecording = true;
121  m_recording = true;
122  m_recordingWait.wakeAll();
123  }
124 
125  StartNewFile();
126 
129 
130  while (IsRecordingRequested() && !IsErrored())
131  {
132  if (PauseAndWait())
133  continue;
134 
135  if (!IsRecordingRequested())
136  break;
137 
138  { // sleep 100 milliseconds unless StopRecording() or Unpause()
139  // is called, just to avoid running this too often.
140  QMutexLocker locker(&m_pauseLock);
142  continue;
143  m_unpauseWait.wait(&m_pauseLock, 100);
144  }
145 
146  if (!m_inputPmt)
147  {
148  LOG(VB_GENERAL, LOG_WARNING, LOC +
149  "Recording will not commence until a PMT is set.");
150  std::this_thread::sleep_for(5ms);
151  continue;
152  }
153  }
154 
155  LOG(VB_RECORD, LOG_INFO, LOC + "run -- ending...");
156 
159 
160  Close();
161 
162  FinishRecording();
163 
164  QMutexLocker locker(&m_pauseLock);
165  m_recording = false;
166  m_recordingWait.wakeAll();
167 
168  LOG(VB_RECORD, LOG_INFO, LOC + "run -- end");
169 }
170 
171 /* vim: set expandtab tabstop=4 shiftwidth=4: */
DTVRecorder::HandleSingleProgramPMT
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
Definition: dtvrecorder.cpp:1405
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
LOC
#define LOC
-*- Mode: c++ -*- IPTVRecorder Copyright (c) 2006-2009 Silicondust Engineering Ltd,...
Definition: iptvrecorder.cpp:18
DTVRecorder::m_inputPmt
ProgramMapTable * m_inputPmt
PMT on input side.
Definition: dtvrecorder.h:173
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:140
DTVRecorder::SetStreamData
virtual void SetStreamData(MPEGStreamData *data)
Definition: dtvrecorder.cpp:218
RecorderBase::m_tvrec
TVRec * m_tvrec
Definition: recorderbase.h:315
DTVRecorder::IsErrored
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:45
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
IPTVRecorder::~IPTVRecorder
~IPTVRecorder() override
Definition: iptvrecorder.cpp:25
DTVRecorder::FinishRecording
void FinishRecording(void) override
Flushes the ringbuffer, and if this is not a live LiveTV recording saves the position map and filesiz...
Definition: dtvrecorder.cpp:126
MPEGStreamData::PMTSingleProgram
const ProgramMapTable * PMTSingleProgram(void) const
Definition: mpegstreamdata.h:266
RecorderBase::m_pauseLock
QMutex m_pauseLock
Definition: recorderbase.h:338
RecorderBase::m_requestPause
bool m_requestPause
Definition: recorderbase.h:339
RecorderBase::m_recordingWait
QWaitCondition m_recordingWait
Definition: recorderbase.h:347
TVRec::RecorderPaused
void RecorderPaused(void)
This is a callback, called by the "recorder" instance when it has actually paused.
Definition: tv_rec.cpp:2984
RecorderBase::m_paused
bool m_paused
Definition: recorderbase.h:340
MPEGStreamData::PATSingleProgram
const ProgramAssociationTable * PATSingleProgram(void) const
Definition: mpegstreamdata.h:264
DTVRecorder::HandleSingleProgramPAT
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
Definition: dtvrecorder.cpp:1385
IPTVRecorder::Open
bool Open(void)
Definition: iptvrecorder.cpp:31
IPTVRecorder::Close
void Close(void)
Definition: iptvrecorder.cpp:59
IPTVRecorder::m_channel
IPTVChannel * m_channel
Definition: iptvrecorder.h:35
MPEGStreamData
Encapsulates data about MPEG stream and emits events for each table.
Definition: mpegstreamdata.h:85
IPTVRecorder::IsOpen
bool IsOpen(void) const
Definition: iptvrecorder.cpp:52
MPEGStreamData::RemoveWritingListener
void RemoveWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1660
RecorderBase::IsRecordingRequested
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
Definition: recorderbase.cpp:250
IPTVChannel
Definition: iptvchannel.h:24
IPTVRecorder::IPTVRecorder
IPTVRecorder(TVRec *rec, IPTVChannel *channel)
Definition: iptvrecorder.cpp:20
DTVRecorder
This is a specialization of RecorderBase used to handle MPEG-2, MPEG-4, MPEG-4 AVC,...
Definition: dtvrecorder.h:25
IPTVChannel::IsOpen
bool IsOpen(void) const override
Reports whether channel is already open.
Definition: iptvchannel.cpp:154
IPTVChannel::SetStreamData
void SetStreamData(MPEGStreamData *sd)
Definition: iptvchannel.cpp:61
mpegstreamdata.h
RecorderBase::StopRecording
virtual void StopRecording(void)
StopRecording() signals to the recorder that it should stop recording and exit cleanly.
Definition: recorderbase.cpp:224
iptvchannel.h
RecorderBase::m_pauseWait
QWaitCondition m_pauseWait
Definition: recorderbase.h:341
MPEGStreamData::AddAVListener
void AddAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1674
IPTVRecorder::PauseAndWait
bool PauseAndWait(std::chrono::milliseconds timeout=100ms) override
If m_requestPause is true, sets pause and blocks up to timeout milliseconds or until unpaused,...
Definition: iptvrecorder.cpp:73
IPTVChannel::Close
void Close(void) override
Closes the channel changing hardware to use.
Definition: iptvchannel.cpp:89
IPTVRecorder::SetStreamData
void SetStreamData(MPEGStreamData *data) override
Definition: iptvrecorder.cpp:66
DTVRecorder::m_streamData
MPEGStreamData * m_streamData
Definition: dtvrecorder.h:162
MPEGStreamData::AddWritingListener
void AddWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1649
DTVRecorder::m_error
QString m_error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:160
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:142
RecorderBase::m_recording
bool m_recording
True while recording is actually being performed.
Definition: recorderbase.h:346
tv_rec.h
IPTVRecorder::run
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
Definition: iptvrecorder.cpp:107
IPTVRecorder::StartNewFile
void StartNewFile(void) override
Definition: iptvrecorder.cpp:100
iptvrecorder.h
IPTVChannel::Open
bool Open(void) override
Opens the channel changing hardware for use.
Definition: iptvchannel.cpp:40
RecorderBase::m_unpauseWait
QWaitCondition m_unpauseWait
Definition: recorderbase.h:342
MPEGStreamData::RemoveAVListener
void RemoveAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1695
RecorderBase::IsPaused
virtual bool IsPaused(bool holding_lock=false) const
Returns true iff recorder is paused.
Definition: recorderbase.cpp:281
RecorderBase::m_requestRecording
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:344