MythTV  master
dvbrecorder.cpp
Go to the documentation of this file.
1 /*
2  * Class DVBRecorder
3  *
4  * Copyright (C) Daniel Thor Kristjansson 2010
5  *
6  * This class glues the DVBStreamHandler which handles the DVB devices
7  * to the DTVRecorder that handles recordings in MythTV.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 // MythTV includes
25 #include "dvbstreamhandler.h"
26 #include "mpegstreamdata.h"
27 #include "tsstreamdata.h"
28 #include "dvbrecorder.h"
29 #include "dvbchannel.h"
30 #include "ringbuffer.h"
31 #include "tv_rec.h"
32 #include "mythlogging.h"
33 
34 #define LOC QString("DVBRec[%1](%2): ") \
35  .arg(tvrec ? tvrec->GetInputId() : -1).arg(videodevice)
36 
38  : DTVRecorder(rec), _channel(channel), _stream_handler(nullptr)
39 {
40  videodevice.clear();
41 }
42 
44 {
45  if (IsOpen())
46  {
47  LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
48  return true;
49  }
50 
51  if (videodevice.isEmpty())
52  return false;
53 
55 
56  if (_channel->GetFormat().compare("MPTS") == 0)
57  {
58  // MPTS only. Use TSStreamData to write out unfiltered data
59  LOG(VB_RECORD, LOG_INFO, LOC + "Using TSStreamData");
61  _record_mpts_only = true;
62  _record_mpts = false;
63  }
64 
66  tvrec ? tvrec->GetInputId() : -1);
67 
68  LOG(VB_RECORD, LOG_INFO, LOC + "Card opened successfully");
69 
70  return true;
71 }
72 
73 bool DVBRecorder::IsOpen(void) const
74 {
75  return (nullptr != _stream_handler);
76 }
77 
79 {
80  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
81 
83 
84  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
85 }
86 
88 {
89  if (!_record_mpts_only)
90  {
91  if (_record_mpts)
93 
94  // Make sure the first things in the file are a PAT & PMT
97  }
98 }
99 
101 {
102  if (!Open())
103  {
104  _error = "Failed to open DVB device";
105  LOG(VB_GENERAL, LOG_ERR, LOC + _error);
106  return;
107  }
108 
109  {
110  QMutexLocker locker(&pauseLock);
111  request_recording = true;
112  recording = true;
113  recordingWait.wakeAll();
114  }
115 
116  // Listen for time table on DVB standard streams
117  if (_channel && (_channel->GetSIStandard() == "dvb"))
119  if (_record_mpts_only)
120  _stream_data->AddListeningPID(0x2000);
121 
122  StartNewFile();
123 
127  (_record_mpts) ? ringBuffer->GetFilename() : QString());
128 
129  while (IsRecordingRequested() && !IsErrored())
130  {
131  if (PauseAndWait())
132  continue;
133 
134  { // sleep 100 milliseconds unless StopRecording() or Unpause()
135  // is called, just to avoid running this too often.
136  QMutexLocker locker(&pauseLock);
138  continue;
139  unpauseWait.wait(&pauseLock, 100);
140  }
141 
142  if (!_input_pmt && !_record_mpts_only)
143  {
144  LOG(VB_GENERAL, LOG_WARNING, LOC +
145  "Recording will not commence until a PMT is set.");
146  usleep(5000);
147  continue;
148  }
149 
150  if (!_stream_handler->IsRunning())
151  {
152  _error = "Stream handler died unexpectedly.";
153  LOG(VB_GENERAL, LOG_ERR, LOC + _error);
154  }
155  }
156 
160 
161  Close();
162 
163  FinishRecording();
164 
165  QMutexLocker locker(&pauseLock);
166  recording = false;
167  recordingWait.wakeAll();
168 }
169 
171 {
172  QMutexLocker locker(&pauseLock);
173  if (request_pause)
174  {
175  if (!IsPaused(true))
176  {
178 
179  paused = true;
180  pauseWait.wakeAll();
181  if (tvrec)
183  }
184 
185  unpauseWait.wait(&pauseLock, timeout);
186  }
187 
188  if (!request_pause && IsPaused(true))
189  {
190  paused = false;
192  unpauseWait.wakeAll();
193  }
194 
195  return IsPaused(true);
196 }
197 
198 QString DVBRecorder::GetSIStandard(void) const
199 {
200  return _channel->GetSIStandard();
201 }
202 
204 {
205  if (pmt->IsEncrypted(_channel->GetSIStandard()))
206  _channel->SetPMT(pmt);
207 }
208 
210 {
212 }
213 
214 /* vim: set expandtab tabstop=4 shiftwidth=4: */
QString _error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:162
bool PauseAndWait(int timeout=100) override
If request_pause is true, sets pause and blocks up to timeout milliseconds or until unpaused,...
int _record_mpts
Definition: dtvrecorder.h:177
const ProgramAssociationTable * PATSingleProgram(void) const
RingBuffer * ringBuffer
Definition: recorderbase.h:297
This is a specialization of RecorderBase used to handle MPEG-2, MPEG-4, MPEG-4 AVC,...
Definition: dtvrecorder.h:28
QWaitCondition unpauseWait
Definition: recorderbase.h:323
uint GetInputId(void)
Returns the inputid.
Definition: tv_rec.h:241
QMutex pauseLock
Definition: recorderbase.h:319
DVBRecorder(TVRec *, DVBChannel *)
Definition: dvbrecorder.cpp:37
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
void SetCAMPMT(const ProgramMapTable *) override
void ResetForNewFile(void) override
QWaitCondition pauseWait
Definition: recorderbase.h:322
void SetPMT(const ProgramMapTable *)
Tells the Conditional Access Module which streams we wish to decode.
Definition: dvbchannel.cpp:557
virtual void RemoveListener(MPEGStreamData *data)
QString GetFilename(void) const
Returns name of file used by this RingBuffer.
QWaitCondition recordingWait
Definition: recorderbase.h:328
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
Provides interface to the tuning hardware when using DVB drivers.
Definition: dvbchannel.h:29
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:48
MPEGStreamData * _stream_data
Definition: dtvrecorder.h:164
virtual bool IsPaused(bool holding_lock=false) const
Returns true iff recorder is paused.
static void Return(DVBStreamHandler *&ref, int inputid)
void SetTimeOffset(double offset)
Tells the Conditional Access Module the offset from the computers utc time to dvb time.
Definition: dvbchannel.cpp:569
void FinishRecording(void) override
Flushes the ringbuffer, and if this is not a live LiveTV recording saves the position map and filesiz...
TVRec * tvrec
Definition: recorderbase.h:296
bool recording
True while recording is actually being performed.
Definition: recorderbase.h:327
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:150
bool request_recording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:325
QString GetFormat(void)
Definition: dtvchannel.h:47
void RemoveWritingListener(TSPacketListener *)
void RecorderPaused(void)
This is a callback, called by the "recorder" instance when it has actually paused.
Definition: tv_rec.cpp:2966
void AddAVListener(TSPacketListenerAV *)
bool Open(void)
Definition: dvbrecorder.cpp:43
MPEGStreamData * GetStreamData(void) const
Definition: dtvrecorder.h:60
virtual void AddListeningPID(uint pid, PIDPriority priority=kPIDPriorityNormal)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
const ProgramMapTable * PMTSingleProgram(void) const
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_drb=false, QString output_file=QString())
static DVBStreamHandler * Get(const QString &dvb_device, int inputid)
void AddWritingListener(TSPacketListener *)
bool _record_mpts_only
Definition: dtvrecorder.h:178
bool IsEncrypted(QString sistandard) const
Returns true iff PMT contains CA descriptor for a vid/aud stream.
Definition: mpegtables.cpp:552
ProgramMapTable * _input_pmt
PMT on input side.
Definition: dtvrecorder.h:173
QString GetSIStandard(void) const override
#define LOC
Definition: dvbrecorder.cpp:34
Specialized version of MPEGStreamData which is used to 'blindly' record the entire MPTS transport fro...
Definition: tsstreamdata.h:11
virtual void SetStreamData(MPEGStreamData *sd)
QString GetSIStandard(void) const
Returns PSIP table standard: MPEG, DVB, ATSC, or OpenCable.
Definition: dtvchannel.cpp:55
void RemoveAVListener(TSPacketListenerAV *)
void StartNewFile(void) override
Definition: dvbrecorder.cpp:87
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:656
QString videodevice
Definition: recorderbase.h:304
void Close(void)
Definition: dvbrecorder.cpp:78
virtual bool AddNamedOutputFile(const QString &filename)
Called with _listener_lock locked just after adding new output file.
bool IsRunning(void) const
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
DVBStreamHandler * _stream_handler
Definition: dvbrecorder.h:42
DVBChannel * _channel
Definition: dvbrecorder.h:41
void UpdateCAMTimeOffset(void) override
bool request_pause
Definition: recorderbase.h:320
bool IsOpen(void) const
Definition: dvbrecorder.cpp:73
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.