MythTV  master
asirecorder.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class ASIRecorder
3  *
4  * Copyright (C) Daniel Kristjansson 2010
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 // Qt includes
22 #include <QString>
23 
24 // MythTV includes
25 #include "asichannel.h"
26 #include "asirecorder.h"
27 #include "asistreamhandler.h"
28 #include "io/mythmediabuffer.h"
29 #include "mpeg/tsstreamdata.h"
30 #include "tv_rec.h"
31 
32 #define LOC QString("ASIRec[%1](%2): ") \
33  .arg(m_tvrec ? m_tvrec->GetInputId() : -1) \
34  .arg(m_channel->GetDevice())
35 
37  DTVRecorder(rec), m_channel(channel)
38 {
39  if (channel->GetFormat().compare("MPTS") == 0)
40  {
41  // MPTS only. Use TSStreamData to write out unfiltered data
42  LOG(VB_RECORD, LOG_INFO, LOC + "Using TSStreamData");
44  m_recordMptsOnly = true;
45  m_recordMpts = false;
46  }
47  else
48  {
49  LOG(VB_RECORD, LOG_INFO, LOC + "Using MPEGStreamData");
50  SetStreamData(new MPEGStreamData(-1, rec ? rec->GetInputId() : -1,
51  false));
52  if (channel->GetProgramNumber() < 0 || !channel->GetMinorChannel())
54  }
55 }
56 
58  const QString &videodev,
59  const QString &/*audiodev*/,
60  const QString &/*vbidev*/)
61 {
62  // We don't want to call DTVRecorder::SetOptionsFromProfile() since
63  // we do not have a "recordingtype" in our profile.
64  DTVRecorder::SetOption("videodevice", videodev);
65  DTVRecorder::SetOption("tvformat", gCoreContext->GetSetting("TVFormat"));
66  SetIntOption(profile, "recordmpts");
67 }
68 
70 {
71  if (!m_recordMptsOnly)
72  {
73  if (m_recordMpts)
75 
76  // Make sure the first things in the file are a PAT & PMT
79  }
80 }
81 
82 
83 void ASIRecorder::run(void)
84 {
85  if (!Open())
86  {
87  m_error = "Failed to open device";
88  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
89  return;
90  }
91 
92  if (!m_streamData)
93  {
94  m_error = "MPEGStreamData pointer has not been set";
95  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
96  Close();
97  return;
98  }
99 
100  {
101  QMutexLocker locker(&m_pauseLock);
102  m_requestRecording = true;
103  m_recording = true;
104  m_recordingWait.wakeAll();
105  }
106 
107  if (m_channel->HasGeneratedPAT())
108  {
110  const ProgramMapTable *pmt = m_channel->GetGeneratedPMT();
111  m_streamData->Reset(pat->ProgramNumber(0));
113  m_streamData->HandleTables(pat->ProgramPID(0), *pmt);
114  }
115 
116  // Listen for time table on DVB standard streams
117  if (m_channel && (m_channel->GetSIStandard() == "dvb"))
119 
120  StartNewFile();
121 
125  m_streamData, false, true,
126  (m_recordMpts) ? m_ringBuffer->GetFilename() : QString());
127 
128  while (IsRecordingRequested() && !IsErrored())
129  {
130  if (PauseAndWait())
131  continue;
132 
133  { // sleep 100 milliseconds unless StopRecording() or Unpause()
134  // is called, just to avoid running this too often.
135  QMutexLocker locker(&m_pauseLock);
137  continue;
138  m_unpauseWait.wait(&m_pauseLock, 100);
139  }
140 
141  if (!m_inputPmt && !m_recordMptsOnly)
142  {
143  LOG(VB_GENERAL, LOG_WARNING, LOC +
144  "Recording will not commence until a PMT is set.");
145  usleep(5000);
146  continue;
147  }
148 
149  if (!m_streamHandler->IsRunning())
150  {
151  m_error = "Stream handler died unexpectedly.";
152  LOG(VB_GENERAL, LOG_ERR, LOC + m_error);
153  }
154  }
155 
159 
160  Close();
161 
162  FinishRecording();
163 
164  QMutexLocker locker(&m_pauseLock);
165  m_recording = false;
166  m_recordingWait.wakeAll();
167 }
168 
170 {
171  if (IsOpen())
172  {
173  LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
174  return true;
175  }
176 
177  ResetForNewFile();
178 
180  m_tvrec ? m_tvrec->GetInputId() : -1);
181 
182 
183  LOG(VB_RECORD, LOG_INFO, LOC + "Opened successfully");
184 
185  return true;
186 }
187 
188 bool ASIRecorder::IsOpen(void) const
189 {
190  return m_streamHandler;
191 }
192 
194 {
195  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
196 
197  if (IsOpen())
199  m_tvrec ? m_tvrec->GetInputId() : -1);
200 
201  LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
202 }
DTVRecorder::HandleSingleProgramPMT
void HandleSingleProgramPMT(ProgramMapTable *pmt, bool insert) override
Definition: dtvrecorder.cpp:1408
ProgramAssociationTable::ProgramPID
uint ProgramPID(uint i) const
Definition: mpegtables.h:648
PID::MPEG_PAT_PID
@ MPEG_PAT_PID
Definition: mpegtables.h:211
DTVRecorder::m_inputPmt
ProgramMapTable * m_inputPmt
PMT on input side.
Definition: dtvrecorder.h:174
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:140
ASIRecorder::SetOptionsFromProfile
void SetOptionsFromProfile(RecordingProfile *profile, const QString &videodev, const QString &audiodev, const QString &vbidev) override
Sets basic recorder options.
Definition: asirecorder.cpp:57
ProgramMapTable
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:694
DTVRecorder::SetStreamData
virtual void SetStreamData(MPEGStreamData *data)
Definition: dtvrecorder.cpp:218
DTVChannel::GetMinorChannel
uint GetMinorChannel(void) const
Returns minor channel, 0 if unknown.
Definition: dtvchannel.h:97
RecorderBase::m_tvrec
TVRec * m_tvrec
Definition: recorderbase.h:309
DTVRecorder::IsErrored
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:46
DTVChannel::GetFormat
QString GetFormat(void)
Definition: dtvchannel.h:46
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
StreamHandler::AddNamedOutputFile
virtual bool AddNamedOutputFile(const QString &filename)
Called with _listener_lock locked just after adding new output file.
Definition: streamhandler.cpp:362
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
DTVChannel::GetGeneratedPMT
const ProgramMapTable * GetGeneratedPMT(void) const
Definition: dtvchannel.h:138
MPEGStreamData::PMTSingleProgram
const ProgramMapTable * PMTSingleProgram(void) const
Definition: mpegstreamdata.h:266
StreamHandler::RemoveListener
virtual void RemoveListener(MPEGStreamData *data)
Definition: streamhandler.cpp:80
RecorderBase::m_pauseLock
QMutex m_pauseLock
Definition: recorderbase.h:332
ASIRecorder::run
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
Definition: asirecorder.cpp:83
RecorderBase::m_requestPause
bool m_requestPause
Definition: recorderbase.h:333
ASIRecorder::Open
bool Open(void)
Definition: asirecorder.cpp:169
asistreamhandler.h
RecorderBase::m_recordingWait
QWaitCondition m_recordingWait
Definition: recorderbase.h:341
asirecorder.h
MythMediaBuffer::GetFilename
QString GetFilename(void) const
Definition: mythmediabuffer.cpp:1745
ASIRecorder::IsOpen
bool IsOpen(void) const
Definition: asirecorder.cpp:188
TVRec::GetInputId
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:234
MPEGStreamData::PATSingleProgram
const ProgramAssociationTable * PATSingleProgram(void) const
Definition: mpegstreamdata.h:264
DTVRecorder::HandleSingleProgramPAT
void HandleSingleProgramPAT(ProgramAssociationTable *pat, bool insert) override
Definition: dtvrecorder.cpp:1388
ASIStreamHandler::Get
static ASIStreamHandler * Get(const QString &devname, int inputid)
Definition: asistreamhandler.cpp:33
ProgramAssociationTable::ProgramNumber
uint ProgramNumber(uint i) const
Definition: mpegtables.h:645
hardwareprofile.scan.profile
profile
Definition: scan.py:97
DTVRecorder::m_recordMptsOnly
bool m_recordMptsOnly
Definition: dtvrecorder.h:179
ASIRecorder::m_channel
ASIChannel * m_channel
Definition: asirecorder.h:73
MPEGStreamData::HandleTables
virtual bool HandleTables(uint pid, const PSIPTable &psip)
Process PSIP packets.
Definition: mpegstreamdata.cpp:668
ASIRecorder::ASIRecorder
ASIRecorder(TVRec *rec, ASIChannel *channel)
Definition: asirecorder.cpp:36
DTVChannel::GetGeneratedPAT
const ProgramAssociationTable * GetGeneratedPAT(void) const
Definition: dtvchannel.h:137
MPEGStreamData
Encapsulates data about MPEG stream and emits events for each table.
Definition: mpegstreamdata.h:85
MPEGStreamData::RemoveWritingListener
void RemoveWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1659
RecorderBase::IsRecordingRequested
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
Definition: recorderbase.cpp:256
ASIStreamHandler::Return
static void Return(ASIStreamHandler *&ref, int inputid)
Definition: asistreamhandler.cpp:66
ASIChannel::GetDevice
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
Definition: asichannel.h:34
ASIStreamHandler::AddListener
void AddListener(MPEGStreamData *data, bool=false, bool=false, const QString &output_file=QString()) override
Definition: asistreamhandler.h:49
DTVRecorder::SetOption
void SetOption(const QString &name, const QString &value) override
Set an specific option.
Definition: dtvrecorder.cpp:91
LOC
#define LOC
Definition: asirecorder.cpp:32
DTVRecorder
This is a specialization of RecorderBase used to handle MPEG-2, MPEG-4, MPEG-4 AVC,...
Definition: dtvrecorder.h:26
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
ProgramAssociationTable
The Program Association Table lists all the programs in a stream, and is always found on PID 0.
Definition: mpegtables.h:617
ASIRecorder::Close
void Close(void)
Definition: asirecorder.cpp:193
asichannel.h
mythmediabuffer.h
DTVChannel::GetSIStandard
QString GetSIStandard(void) const
Returns PSIP table standard: MPEG, DVB, ATSC, or OpenCable.
Definition: dtvchannel.cpp:45
RecorderBase::m_ringBuffer
MythMediaBuffer * m_ringBuffer
Definition: recorderbase.h:310
MPEGStreamData::AddAVListener
void AddAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1673
ASIRecorder::m_streamHandler
ASIStreamHandler * m_streamHandler
Definition: asirecorder.h:74
ASIChannel
-*- Mode: c++ -*-
Definition: asichannel.h:14
PID::DVB_TDT_PID
@ DVB_TDT_PID
Definition: mpegtables.h:219
DTVRecorder::m_streamData
MPEGStreamData * m_streamData
Definition: dtvrecorder.h:163
MPEGStreamData::AddWritingListener
void AddWritingListener(TSPacketListener *val)
Definition: mpegstreamdata.cpp:1648
DTVRecorder::m_error
QString m_error
non-empty iff irrecoverable recording error detected
Definition: dtvrecorder.h:161
DTVRecorder::m_recordMpts
bool m_recordMpts
Definition: dtvrecorder.h:178
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:340
DTVChannel::GetProgramNumber
int GetProgramNumber(void) const
Returns program number in PAT, -1 if unknown.
Definition: dtvchannel.h:89
MPEGStreamData::Reset
virtual void Reset(void)
Definition: mpegstreamdata.h:94
tv_rec.h
ASIRecorder::StartNewFile
void StartNewFile(void) override
Definition: asirecorder.cpp:69
MPEGStreamData::AddListeningPID
virtual void AddListeningPID(uint pid, PIDPriority priority=kPIDPriorityNormal)
Definition: mpegstreamdata.h:120
DTVChannel::HasGeneratedPAT
bool HasGeneratedPAT(void) const
Definition: dtvchannel.h:135
RecordingProfile
Definition: recordingprofile.h:41
StreamHandler::IsRunning
bool IsRunning(void) const
Definition: streamhandler.cpp:163
RecorderBase::m_unpauseWait
QWaitCondition m_unpauseWait
Definition: recorderbase.h:336
TSStreamData
Specialized version of MPEGStreamData which is used to 'blindly' record the entire MPTS transport fro...
Definition: tsstreamdata.h:11
MPEGStreamData::RemoveAVListener
void RemoveAVListener(TSPacketListenerAV *val)
Definition: mpegstreamdata.cpp:1694
MPEGStreamData::SetListeningDisabled
void SetListeningDisabled(bool lt)
Definition: mpegstreamdata.h:92
RecorderBase::PauseAndWait
virtual bool PauseAndWait(std::chrono::milliseconds timeout=100ms)
If m_requestPause is true, sets pause and blocks up to timeout milliseconds or until unpaused,...
Definition: recorderbase.cpp:332
tsstreamdata.h
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:902
RecorderBase::m_requestRecording
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:338
RecorderBase::SetIntOption
void SetIntOption(RecordingProfile *profile, const QString &name)
Convenience function used to set integer options from a profile.
Definition: recorderbase.cpp:205