MythTV  master
ExternalRecorder.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class ExternalRecorder
3  *
4  * Copyright (C) John Poet 2013
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 // Qt includes
22 #include <QString>
23 
24 // MythTV includes
25 #include "ExternalStreamHandler.h"
26 #include "ExternalRecorder.h"
27 #include "ExternalChannel.h"
28 #include "io/mythmediabuffer.h"
29 #include "tv_rec.h"
30 
31 #define LOC QString("ExternalRec[%1](%2): ") \
32  .arg(m_channel->GetInputID()) \
33  .arg(m_channel->GetDescription())
34 
36 {
37  // Make sure the first things in the file are a PAT & PMT
40 }
41 
42 
44 {
45  if (!Open())
46  {
47  m_error = "Failed to open device";
48  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
49  return;
50  }
51 
52  if (!m_streamData)
53  {
54  m_error = "MPEGStreamData pointer has not been set";
55  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
56  Close();
57  return;
58  }
59 
60  {
61  QMutexLocker locker(&m_pauseLock);
62  m_requestRecording = true;
63  m_recording = true;
64  m_recordingWait.wakeAll();
65  }
66 
68  {
73  m_streamData->HandleTables(pat->ProgramPID(0), *pmt);
74  LOG(VB_GENERAL, LOG_INFO, LOC + "PMT set");
75  }
76 
77  StartNewFile();
78 
80  m_seenSps = false;
81 
85 
87 
88  while (IsRecordingRequested() && !IsErrored())
89  {
90  if (PauseAndWait())
91  continue;
92 
93  if (!m_inputPmt)
94  {
95  LOG(VB_GENERAL, LOG_WARNING, LOC +
96  "Recording will not commence until a PMT is set.");
97  usleep(5000);
98  continue;
99  }
100 
101  if (!m_streamHandler->IsRunning())
102  {
103  m_error = "Stream handler died unexpectedly.";
104  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
105  }
106  }
107 
108  StopStreaming();
109 
113 
114  Close();
115 
116  FinishRecording();
117 
118  QMutexLocker locker(&m_pauseLock);
119  m_recording = false;
120  m_recordingWait.wakeAll();
121 }
122 
124 {
125  QString result;
126 
127  if (IsOpen())
128  {
129  LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
130  return true;
131  }
132 
133  ResetForNewFile();
134 
137  m_channel->GetMajorID());
138 
139  if (m_streamHandler)
140  {
141  if (m_streamHandler->IsAppOpen())
142  LOG(VB_RECORD, LOG_INFO, LOC + "Opened successfully");
143  else
144  {
146  (m_tvrec ? m_tvrec->GetInputId() : -1));
147 
148  return false;
149  }
150  return true;
151  }
152 
153  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
154  return false;
155 }
156 
158 {
159  QString result;
160 
161  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
162 
163  if (IsOpen())
165  (m_tvrec ? m_tvrec->GetInputId() : -1));
166 
167  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
168 }
169 
171 {
172  QMutexLocker locker(&m_pauseLock);
173  if (m_requestPause)
174  {
175  if (!IsPaused(true))
176  {
177  LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait pause");
178 
180  StopStreaming();
181 
182  m_paused = true;
183  m_pauseWait.wakeAll();
184 
185  if (m_tvrec)
187  }
188  }
189  else if (IsPaused(true))
190  {
191  LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait unpause");
192 
193  // The SignalMonitor will StartStreaming
194 
195  if (m_h2645Parser != nullptr)
196  m_h2645Parser->Reset();
197 
198  if (m_streamData)
200 
201  m_paused = false;
203  StartStreaming();
204  }
205 
206  // Always wait a little bit, unless woken up
208 
209  return IsPaused(true);
210 }
211 
213 {
214  LOG(VB_RECORD, LOG_INFO, LOC + "StartStreaming");
216 }
217 
219 {
221 }
DTVRecorder::HandleSingleProgramPMT
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
Definition: dtvrecorder.cpp:1334
ProgramAssociationTable::ProgramPID
uint ProgramPID(uint i) const
Definition: mpegtables.h:620
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
ExternalRecorder.h
DTVRecorder::m_inputPmt
ProgramMapTable * m_inputPmt
PMT on input side.
Definition: dtvrecorder.h:174
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:138
ExternalRecorder::StopStreaming
bool StopStreaming(void)
Definition: ExternalRecorder.cpp:218
ProgramMapTable
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:667
ExternalRecorder::run
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
Definition: ExternalRecorder.cpp:43
H2645Parser::Reset
virtual void Reset(void)
Definition: H2645Parser.cpp:100
ExternalRecorder::Close
void Close(void)
Definition: ExternalRecorder.cpp:157
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1168
RecorderBase::m_tvrec
TVRec * m_tvrec
Definition: recorderbase.h:314
DTVRecorder::IsErrored
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MPEG_PAT_PID
@ MPEG_PAT_PID
Definition: mpegtables.h:205
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:124
ExternalRecorder::m_channel
ExternalChannel * m_channel
Definition: ExternalRecorder.h:48
ExternalStreamHandler.h
DTVChannel::GetGeneratedPMT
const ProgramMapTable * GetGeneratedPMT(void) const
Definition: dtvchannel.h:140
MPEGStreamData::PMTSingleProgram
const ProgramMapTable * PMTSingleProgram(void) const
Definition: mpegstreamdata.h:262
ExternalRecorder::IsOpen
bool IsOpen(void) const
Definition: ExternalRecorder.h:37
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:78
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, QString output_file=QString())
Definition: streamhandler.cpp:33
RecorderBase::m_pauseLock
QMutex m_pauseLock
Definition: recorderbase.h:337
RecorderBase::m_requestPause
bool m_requestPause
Definition: recorderbase.h:338
ExternalRecorder::StartNewFile
void StartNewFile(void) override
Definition: ExternalRecorder.cpp:35
RecorderBase::m_recordingWait
QWaitCondition m_recordingWait
Definition: recorderbase.h:346
TVRec::RecorderPaused
void RecorderPaused(void)
This is a callback, called by the "recorder" instance when it has actually paused.
Definition: tv_rec.cpp:2947
ExternalChannel::GetDevice
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
Definition: ExternalChannel.h:42
TVRec::GetInputId
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:234
RecorderBase::m_paused
bool m_paused
Definition: recorderbase.h:339
MPEGStreamData::PATSingleProgram
const ProgramAssociationTable * PATSingleProgram(void) const
Definition: mpegstreamdata.h:260
DTVRecorder::HandleSingleProgramPAT
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
Definition: dtvrecorder.cpp:1314
ExternalRecorder::PauseAndWait
bool PauseAndWait(int timeout=100) override
If m_requestPause is true, sets pause and blocks up to timeout milliseconds or until unpaused,...
Definition: ExternalRecorder.cpp:170
ProgramAssociationTable::ProgramNumber
uint ProgramNumber(uint i) const
Definition: mpegtables.h:617
MPEGStreamData::HandleTables
virtual bool HandleTables(uint pid, const PSIPTable &psip)
Process PSIP packets.
Definition: mpegstreamdata.cpp:666
DTVChannel::GetGeneratedPAT
const ProgramAssociationTable * GetGeneratedPAT(void) const
Definition: dtvchannel.h:139
ExternalRecorder::StartStreaming
bool StartStreaming(void)
Definition: ExternalRecorder.cpp:212
MPEGStreamData::RemoveWritingListener
void RemoveWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1629
RecorderBase::IsRecordingRequested
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
Definition: recorderbase.cpp:252
DTVRecorder::m_h2645Parser
H2645Parser * m_h2645Parser
Definition: dtvrecorder.h:152
DTVRecorder::m_seenSps
bool m_seenSps
Definition: dtvrecorder.h:151
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:455
ExternalRecorder::Open
bool Open(void)
Definition: ExternalRecorder.cpp:123
ProgramAssociationTable
The Program Association Table lists all the programs in a stream, and is always found on PID 0.
Definition: mpegtables.h:590
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1120
mythmediabuffer.h
ExternalStreamHandler::IsAppOpen
bool IsAppOpen(void)
Definition: ExternalStreamHandler.cpp:995
RecorderBase::m_pauseWait
QWaitCondition m_pauseWait
Definition: recorderbase.h:340
MPEGStreamData::AddAVListener
void AddAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1643
DTVRecorder::m_waitForKeyframeOption
bool m_waitForKeyframeOption
Wait for the a GOP/SEQ-start before sending data.
Definition: dtvrecorder.h:155
DTVRecorder::m_streamData
MPEGStreamData * m_streamData
Definition: dtvrecorder.h:163
MPEGStreamData::AddWritingListener
void AddWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1618
MPEGStreamData::DesiredProgram
int DesiredProgram(void) const
Definition: mpegstreamdata.h:256
DTVRecorder::m_error
QString m_error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:161
ExternalChannel.h
RecorderBase::m_recording
bool m_recording
True while recording is actually being performed.
Definition: recorderbase.h:345
MPEGStreamData::Reset
virtual void Reset(void)
Definition: mpegstreamdata.h:94
tv_rec.h
ChannelBase::GetInputID
virtual int GetInputID(void) const
Definition: channelbase.h:67
LOC
#define LOC
Definition: ExternalRecorder.cpp:31
DTVChannel::HasGeneratedPAT
bool HasGeneratedPAT(void) const
Definition: dtvchannel.h:137
StreamHandler::IsRunning
bool IsRunning(void) const
Definition: streamhandler.cpp:161
RecorderBase::m_unpauseWait
QWaitCondition m_unpauseWait
Definition: recorderbase.h:341
ChannelBase::GetMajorID
int GetMajorID(void)
Definition: channelbase.cpp:857
MPEGStreamData::RemoveAVListener
void RemoveAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1654
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:484
RecorderBase::IsPaused
virtual bool IsPaused(bool holding_lock=false) const
Returns true iff recorder is paused.
Definition: recorderbase.cpp:284
RecorderBase::m_requestRecording
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:343
ExternalRecorder::m_streamHandler
ExternalStreamHandler * m_streamHandler
Definition: ExternalRecorder.h:49