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 Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21// Qt includes
22#include <QString>
23
24// MythTV includes
26
28#include "ExternalRecorder.h"
29#include "ExternalChannel.h"
30#include "io/mythmediabuffer.h"
31#include "tv_rec.h"
32
33#define LOC QString("ExternalRec[%1](%2): ") \
34 .arg(m_channel->GetInputID()) \
35 .arg(m_channel->GetDescription())
36
38{
39 // Make sure the first things in the file are a PAT & PMT
42}
43
44
46{
47 if (!Open())
48 {
49 m_error = "Failed to open device";
50 LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
51 return;
52 }
53
54 if (!m_streamData)
55 {
56 m_error = "MPEGStreamData pointer has not been set";
57 LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
58 Close();
59 return;
60 }
61
62 {
63 QMutexLocker locker(&m_pauseLock);
64 m_requestRecording = true;
65 m_recording = true;
66 m_recordingWait.wakeAll();
67 }
68
70 {
75 m_streamData->HandleTables(pat->ProgramPID(0), *pmt);
76 LOG(VB_GENERAL, LOG_INFO, LOC + "PMT set");
77 }
78
80
82 m_seenSps = false;
83
87
89
90 while (IsRecordingRequested() && !IsErrored())
91 {
92 if (PauseAndWait())
93 continue;
94
95 if (!m_inputPmt)
96 {
97 LOG(VB_GENERAL, LOG_WARNING, LOC +
98 "Recording will not commence until a PMT is set.");
99 usleep(5000);
100 continue;
101 }
102
104 {
105 m_error = "Stream handler died unexpectedly.";
106 LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
107 }
108
110 {
111 LOG(VB_GENERAL, LOG_WARNING, LOC +
112 QString("Recording is damaged. Setting status to %1")
114 SetRecordingStatus(RecStatus::Failing, __FILE__, __LINE__);
116 }
117 }
118
120
124
125 Close();
126
128
129 QMutexLocker locker(&m_pauseLock);
130 m_recording = false;
131 m_recordingWait.wakeAll();
132}
133
135{
136 if (IsOpen())
137 {
138 LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
139 return true;
140 }
141
143
147
148 if (m_streamHandler)
149 {
151 LOG(VB_RECORD, LOG_INFO, LOC + "Opened successfully");
152 else
153 {
155 (m_tvrec ? m_tvrec->GetInputId() : -1));
156
157 return false;
158 }
159 return true;
160 }
161
162 Close();
163 LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
164 return false;
165}
166
168{
169 LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
170
171 if (IsOpen())
173 (m_tvrec ? m_tvrec->GetInputId() : -1));
174
175 LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
176}
177
178bool ExternalRecorder::PauseAndWait(std::chrono::milliseconds timeout)
179{
180 QMutexLocker locker(&m_pauseLock);
181 if (m_requestPause)
182 {
183 if (!IsPaused(true))
184 {
185 LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait pause");
186
189
190 m_paused = true;
191 m_pauseWait.wakeAll();
192
193 if (m_tvrec)
195 }
196 }
197 else if (IsPaused(true))
198 {
199 LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait unpause");
200
201 // The SignalMonitor will StartStreaming
202
203 if (m_h2645Parser != nullptr)
205
206 if (m_streamData)
208
209 m_paused = false;
212 }
213
214 // Always wait a little bit, unless woken up
215 m_unpauseWait.wait(&m_pauseLock, timeout.count());
216
217 return IsPaused(true);
218}
219
221{
222 LOG(VB_RECORD, LOG_INFO, LOC + "StartStreaming");
224}
225
227{
229}
#define LOC
virtual int GetInputID(void) const
Definition: channelbase.h:67
int GetMajorID(void)
bool HasGeneratedPAT(void) const
Definition: dtvchannel.h:135
const ProgramMapTable * GetGeneratedPMT(void) const
Definition: dtvchannel.h:138
const ProgramAssociationTable * GetGeneratedPAT(void) const
Definition: dtvchannel.h:137
bool m_seenSps
Definition: dtvrecorder.h:151
QString m_error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:161
void FinishRecording(void) override
Flushes the ringbuffer, and if this is not a live LiveTV recording saves the position map and filesiz...
ProgramMapTable * m_inputPmt
PMT on input side.
Definition: dtvrecorder.h:174
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:46
void ResetForNewFile(void) override
H2645Parser * m_h2645Parser
Definition: dtvrecorder.h:152
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
bool m_waitForKeyframeOption
Wait for the a GOP/SEQ-start before sending data.
Definition: dtvrecorder.h:155
MPEGStreamData * m_streamData
Definition: dtvrecorder.h:163
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
ExternalChannel * m_channel
ExternalStreamHandler * m_streamHandler
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
bool IsOpen(void) const
bool PauseAndWait(std::chrono::milliseconds timeout=100ms) override
If m_requestPause is true, sets pause and blocks up to timeout milliseconds or until unpaused,...
void StartNewFile(void) override
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
static void Return(ExternalStreamHandler *&ref, int inputid)
virtual void Reset(void)
Definition: H2645Parser.cpp:92
const ProgramMapTable * PMTSingleProgram(void) const
void AddWritingListener(TSPacketListener *val)
int DesiredProgram(void) const
void RemoveWritingListener(TSPacketListener *val)
const ProgramAssociationTable * PATSingleProgram(void) const
virtual void Reset(void)
void RemoveAVListener(TSPacketListenerAV *val)
virtual bool HandleTables(uint pid, const PSIPTable &psip)
Process PSIP packets.
void AddAVListener(TSPacketListenerAV *val)
@ MPEG_PAT_PID
Definition: mpegtables.h:211
The Program Association Table lists all the programs in a stream, and is always found on PID 0.
Definition: mpegtables.h:599
uint ProgramNumber(uint i) const
Definition: mpegtables.h:626
uint ProgramPID(uint i) const
Definition: mpegtables.h:629
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:676
static QString toString(RecStatus::Type recstatus, uint id)
Converts "recstatus" into a short (unreadable) string.
QMutex m_pauseLock
Definition: recorderbase.h:313
bool m_requestPause
Definition: recorderbase.h:314
TVRec * m_tvrec
Definition: recorderbase.h:290
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
bool m_recording
True while recording is actually being performed.
Definition: recorderbase.h:321
virtual void SetRecordingStatus(RecStatus::Type status, const QString &file, int line)
QWaitCondition m_pauseWait
Definition: recorderbase.h:316
virtual bool IsPaused(bool holding_lock=false) const
Returns true iff recorder is paused.
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:319
QWaitCondition m_unpauseWait
Definition: recorderbase.h:317
QWaitCondition m_recordingWait
Definition: recorderbase.h:322
bool IsRunning(void) const
virtual void RemoveListener(MPEGStreamData *data)
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
void RecorderPaused(void)
This is a callback, called by the "recorder" instance when it has actually paused.
Definition: tv_rec.cpp:2996
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:234
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
@ kSingleRecord