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  if (IsOpen())
126  {
127  LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
128  return true;
129  }
130 
131  ResetForNewFile();
132 
135  m_channel->GetMajorID());
136 
137  if (m_streamHandler)
138  {
139  if (m_streamHandler->IsAppOpen())
140  LOG(VB_RECORD, LOG_INFO, LOC + "Opened successfully");
141  else
142  {
144  (m_tvrec ? m_tvrec->GetInputId() : -1));
145 
146  return false;
147  }
148  return true;
149  }
150 
151  Close();
152  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
153  return false;
154 }
155 
157 {
158  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
159 
160  if (IsOpen())
162  (m_tvrec ? m_tvrec->GetInputId() : -1));
163 
164  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
165 }
166 
168 {
169  QMutexLocker locker(&m_pauseLock);
170  if (m_requestPause)
171  {
172  if (!IsPaused(true))
173  {
174  LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait pause");
175 
177  StopStreaming();
178 
179  m_paused = true;
180  m_pauseWait.wakeAll();
181 
182  if (m_tvrec)
184  }
185  }
186  else if (IsPaused(true))
187  {
188  LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait unpause");
189 
190  // The SignalMonitor will StartStreaming
191 
192  if (m_h2645Parser != nullptr)
193  m_h2645Parser->Reset();
194 
195  if (m_streamData)
197 
198  m_paused = false;
200  StartStreaming();
201  }
202 
203  // Always wait a little bit, unless woken up
205 
206  return IsPaused(true);
207 }
208 
210 {
211  LOG(VB_RECORD, LOG_INFO, LOC + "StartStreaming");
213 }
214 
216 {
218 }
DTVRecorder::HandleSingleProgramPMT
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
Definition: dtvrecorder.cpp:1340
ProgramAssociationTable::ProgramPID
uint ProgramPID(uint i) const
Definition: mpegtables.h:646
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
ExternalRecorder.h
DTVRecorder::m_inputPmt
ProgramMapTable * m_inputPmt
PMT on input side.
Definition: dtvrecorder.h:172
PID::MPEG_PAT_PID
@ MPEG_PAT_PID
Definition: mpegtables.h:211
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:138
ExternalRecorder::StopStreaming
bool StopStreaming(void)
Definition: ExternalRecorder.cpp:215
ProgramMapTable
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:692
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:156
ExternalStreamHandler::StopStreaming
bool StopStreaming(void)
Definition: ExternalStreamHandler.cpp:1167
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:45
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
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:139
MPEGStreamData::PMTSingleProgram
const ProgramMapTable * PMTSingleProgram(void) const
Definition: mpegstreamdata.h:265
ExternalRecorder::IsOpen
bool IsOpen(void) const
Definition: ExternalRecorder.h:37
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:78
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:2950
ExternalChannel::GetDevice
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
Definition: ExternalChannel.h:41
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:263
DTVRecorder::HandleSingleProgramPAT
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
Definition: dtvrecorder.cpp:1320
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:167
ProgramAssociationTable::ProgramNumber
uint ProgramNumber(uint i) const
Definition: mpegtables.h:643
MPEGStreamData::HandleTables
virtual bool HandleTables(uint pid, const PSIPTable &psip)
Process PSIP packets.
Definition: mpegstreamdata.cpp:674
DTVChannel::GetGeneratedPAT
const ProgramAssociationTable * GetGeneratedPAT(void) const
Definition: dtvchannel.h:138
ExternalRecorder::StartStreaming
bool StartStreaming(void)
Definition: ExternalRecorder.cpp:209
MPEGStreamData::RemoveWritingListener
void RemoveWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1648
RecorderBase::IsRecordingRequested
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
Definition: recorderbase.cpp:251
DTVRecorder::m_h2645Parser
H2645Parser * m_h2645Parser
Definition: dtvrecorder.h:150
DTVRecorder::m_seenSps
bool m_seenSps
Definition: dtvrecorder.h:149
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:615
ExternalStreamHandler::StartStreaming
bool StartStreaming(void)
Definition: ExternalStreamHandler.cpp:1119
mythmediabuffer.h
ExternalStreamHandler::IsAppOpen
bool IsAppOpen(void)
Definition: ExternalStreamHandler.cpp:994
RecorderBase::m_pauseWait
QWaitCondition m_pauseWait
Definition: recorderbase.h:340
MPEGStreamData::AddAVListener
void AddAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1662
DTVRecorder::m_waitForKeyframeOption
bool m_waitForKeyframeOption
Wait for the a GOP/SEQ-start before sending data.
Definition: dtvrecorder.h:153
DTVRecorder::m_streamData
MPEGStreamData * m_streamData
Definition: dtvrecorder.h:161
MPEGStreamData::AddWritingListener
void AddWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1637
MPEGStreamData::DesiredProgram
int DesiredProgram(void) const
Definition: mpegstreamdata.h:259
DTVRecorder::m_error
QString m_error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:159
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:93
tv_rec.h
ChannelBase::GetInputID
virtual int GetInputID(void) const
Definition: channelbase.h:67
LOC
#define LOC
Definition: ExternalRecorder.cpp:31
StreamHandler::AddListener
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
Definition: streamhandler.cpp:33
DTVChannel::HasGeneratedPAT
bool HasGeneratedPAT(void) const
Definition: dtvchannel.h:136
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:851
MPEGStreamData::RemoveAVListener
void RemoveAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1673
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:283
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