MythTV  master
importrecorder.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class ImportRecorder
3  *
4  * Copyright (C) Daniel Kristjansson 2009
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 // POSIX
22 #ifndef _WIN32
23 #include <sys/select.h>
24 #endif
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 
29 #include <chrono> // for milliseconds
30 #include <thread> // for sleep_for
31 
32 // Qt
33 #include <QDir>
34 
35 // MythTV
36 #include "libmythbase/mythdate.h"
37 #include "libmythbase/mythdirs.h"
38 
39 #include "importrecorder.h"
40 #include "mythcommflagplayer.h"
41 #include "tv_rec.h"
42 
43 #define TVREC_CARDNUM \
44  ((m_tvrec != nullptr) ? QString::number(m_tvrec->GetInputId()) : "NULL")
45 
46 #define LOC QString("ImportRec[%1](%2): ") \
47  .arg(TVREC_CARDNUM, m_videodevice)
48 
50  const QString &videodev,
51  [[maybe_unused]] const QString &audiodev,
52  [[maybe_unused]] const QString &vbidev)
53 {
54  QString testVideoDev = videodev;
55 
56  if (videodev.startsWith("file:", Qt::CaseInsensitive))
57  testVideoDev = videodev.mid(5);
58 
59  QFileInfo fi(testVideoDev);
60  if (fi.exists() && fi.isReadable() && fi.isFile() && fi.size() > 1560)
61  SetOption("videodevice", testVideoDev);
62  else
63  SetOption("videodevice", "unknown file");
64 
65  SetOption("tvformat", gCoreContext->GetSetting("TVFormat"));
66  SetOption("vbiformat", gCoreContext->GetSetting("VbiFormat"));
67 }
68 
69 void UpdateFS(int pc, void* ir);
70 void UpdateFS(int /*pc*/, void* ir)
71 {
72  if(ir)
73  static_cast<ImportRecorder*>(ir)->UpdateRecSize();
74 }
75 
77 {
79 
80  if(m_cfp)
82 }
83 
85 {
86  return m_nfc;
87 }
88 
90 {
91  LOG(VB_RECORD, LOG_INFO, LOC + "run -- begin");
92 
93  {
94  QMutexLocker locker(&m_pauseLock);
95  m_requestRecording = true;
96  m_recording = true;
97  m_recordingWait.wakeAll();
98  }
99 
100  LOG(VB_RECORD, LOG_INFO, LOC + "run -- " +
101  QString("attempting to open '%1'")
102  .arg(m_curRecording->GetPathname()));
103 
104  // retry opening the file until StopRecording() is called.
105  while (!Open() && IsRecordingRequested() && !IsErrored())
106  { // sleep 250 milliseconds unless StopRecording() or Unpause()
107  // is called, just to avoid running this loop too often.
108  QMutexLocker locker(&m_pauseLock);
109  if (m_requestRecording)
110  m_unpauseWait.wait(&m_pauseLock, 15000);
111  }
112 
114 
115  // build seek table
117  {
118  auto *ctx = new PlayerContext(kImportRecorderInUseID);
119  auto *cfp = new MythCommFlagPlayer(ctx, (PlayerFlags)(kAudioMuted | kVideoIsNull | kNoITV));
120  MythMediaBuffer *buffer = MythMediaBuffer::Create(m_ringBuffer->GetFilename(), false, true, 6s);
121  //This does update the status but does not set the ultimate
122  //recorded / failure status for the relevant recording
123  SetRecordingStatus(RecStatus::Recording, __FILE__, __LINE__);
124  ctx->SetPlayingInfo(m_curRecording);
125  ctx->SetRingBuffer(buffer);
126  ctx->SetPlayer(cfp);
127 
128  m_cfp=cfp;
130  cfp->RebuildSeekTable(false,UpdateFS,this);
132  m_cfp=nullptr;
133 
134  delete ctx;
135  }
136 
138 
139  // cleanup...
140  Close();
141 
142  FinishRecording();
143 
144  QMutexLocker locker(&m_pauseLock);
145  m_recording = false;
146  m_recordingWait.wakeAll();
147 
148  LOG(VB_RECORD, LOG_INFO, LOC + "run -- end");
149 }
150 
152 {
153  if (m_importFd >= 0) // already open
154  return true;
155 
156  if (!m_curRecording)
157  {
158  LOG(VB_RECORD, LOG_ERR, LOC + "no current recording!");
159  return false;
160  }
161 
162  ResetForNewFile();
163 
164  QString fn = m_curRecording->GetPathname();
165 
166  // Quick-and-dirty "copy" of sample prerecorded file.
167  // Sadly, won't work on Windows.
168  //
169  QFile preRecorded(m_videodevice);
170  QFile copy(fn);
171  if (preRecorded.exists() && (!copy.exists() || copy.size() == 0))
172  {
173  if (copy.exists()) // always created by RecorderBase?
174  {
175  QDir targetDir("."); // QDir::remove() needs an object
176  targetDir.remove(fn);
177  }
178 
179  LOG(VB_RECORD, LOG_INFO, LOC + QString("Trying to link %1 to %2")
180  .arg(m_videodevice, fn));
181 
182  if (preRecorded.link(fn))
183  LOG(VB_RECORD, LOG_DEBUG, LOC + "success!");
184  else
185  LOG(VB_RECORD, LOG_ERR, LOC + preRecorded.errorString());
186  }
187 
188  if (fn.startsWith("myth://", Qt::CaseInsensitive))
189  {
190  LOG(VB_RECORD, LOG_ERR, LOC + "Malformed recording ProgramInfo.");
191  return false;
192  }
193 
194  QFileInfo f(fn);
195  if (!f.exists())
196  {
197  LOG(VB_RECORD, LOG_INFO, LOC +
198  QString("'%1' does not exist yet").arg(fn));
199 
200  // Slow down run open loop when debugging -v record.
201  // This is just to make the debugging output less spammy.
202  if (VERBOSE_LEVEL_CHECK(VB_RECORD, LOG_ANY))
203  std::this_thread::sleep_for(250ms);
204 
205  return false;
206  }
207  if (!f.isReadable())
208  {
209  LOG(VB_GENERAL, LOG_ERR, LOC +
210  QString("'%1' is not readable").arg(fn));
211  return false;
212  }
213  if (!f.size())
214  {
215  LOG(VB_GENERAL, LOG_ERR, LOC +
216  QString("'%1' is empty").arg(fn));
217  return false;
218  }
219 
220  m_importFd = open(fn.toLocal8Bit().constData(), O_RDONLY);
221  if (m_importFd < 0)
222  {
223  LOG(VB_GENERAL, LOG_ERR, LOC +
224  QString("Couldn't open '%1'").arg(fn) + ENO);
225  }
226 
227  return m_importFd >= 0;
228 }
229 
231 {
232  if (m_importFd >= 0)
233  {
234  close(m_importFd);
235  m_importFd = -1;
236  }
237 }
ImportRecorder::m_cfp
MythCommFlagPlayer * m_cfp
Definition: importrecorder.h:49
ENO
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:73
ImportRecorder::Open
bool Open(void)
Definition: importrecorder.cpp:151
MythCoreContext::UnregisterFileForWrite
void UnregisterFileForWrite(const QString &file)
Definition: mythcorecontext.cpp:2140
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:140
mythcommflagplayer.h
MythCoreContext::RegisterFileForWrite
void RegisterFileForWrite(const QString &file, uint64_t size=0LL)
Definition: mythcorecontext.cpp:2122
VERBOSE_LEVEL_CHECK
static bool VERBOSE_LEVEL_CHECK(uint64_t mask, LogLevel_t level)
Definition: mythlogging.h:29
DTVRecorder::IsErrored
bool IsErrored(void) override
Tells us whether an unrecoverable error has been encountered.
Definition: dtvrecorder.h:45
MythMediaBuffer
Definition: mythmediabuffer.h:50
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
PlayerFlags
PlayerFlags
Definition: mythplayer.h:64
mythdirs.h
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
ImportRecorder::run
void run(void) override
run() starts the recording process, and does not exit until the recording is complete.
Definition: importrecorder.cpp:89
ProgramInfo::GetPathname
QString GetPathname(void) const
Definition: programinfo.h:343
RecorderBase::m_pauseLock
QMutex m_pauseLock
Definition: recorderbase.h:338
close
#define close
Definition: compat.h:43
RecorderBase::m_recordingWait
QWaitCondition m_recordingWait
Definition: recorderbase.h:347
RecorderBase::m_curRecording
RecordingInfo * m_curRecording
Definition: recorderbase.h:335
MythMediaBuffer::GetFilename
QString GetFilename(void) const
Definition: mythmediabuffer.cpp:1740
MythMediaBuffer::GetRealFileSize
long long GetRealFileSize(void) const
Definition: mythmediabuffer.cpp:462
mythdate.h
hardwareprofile.scan.profile
profile
Definition: scan.py:97
ImportRecorder::GetFramesWritten
long long GetFramesWritten(void) override
Returns number of frames written to disk.
Definition: importrecorder.cpp:84
MythFile::copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:263
LOC
#define LOC
Definition: importrecorder.cpp:46
RecordingInfo::SaveFilesize
void SaveFilesize(uint64_t fsize) override
Sets recording file size in database, and sets "filesize" field.
Definition: recordinginfo.cpp:1732
ImportRecorder::SetOptionsFromProfile
void SetOptionsFromProfile(RecordingProfile *profile, const QString &videodev, const QString &audiodev, const QString &vbidev) override
Sets basic recorder options.
Definition: importrecorder.cpp:49
importrecorder.h
RecorderBase::IsRecordingRequested
virtual bool IsRecordingRequested(void)
Tells us if StopRecording() has been called.
Definition: recorderbase.cpp:250
MythCommFlagPlayer
Definition: mythcommflagplayer.h:25
ImportRecorder::m_importFd
int m_importFd
Definition: importrecorder.h:48
DTVRecorder::SetOption
void SetOption(const QString &name, const QString &value) override
Set an specific option.
Definition: dtvrecorder.cpp:91
kNoITV
@ kNoITV
Definition: mythplayer.h:75
ImportRecorder::Close
void Close(void)
Definition: importrecorder.cpp:230
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
ImportRecorder::UpdateRecSize
void UpdateRecSize()
Definition: importrecorder.cpp:76
MythPlayer::GetDecoder
DecoderBase * GetDecoder(void)
Returns the stream decoder currently in use.
Definition: mythplayer.h:186
RecorderBase::m_videodevice
QString m_videodevice
Definition: recorderbase.h:323
RecorderBase::m_ringBuffer
MythMediaBuffer * m_ringBuffer
Definition: recorderbase.h:316
kVideoIsNull
@ kVideoIsNull
Definition: mythplayer.h:73
RecStatus::Recording
@ Recording
Definition: recordingstatus.h:29
RecorderBase::SetRecordingStatus
virtual void SetRecordingStatus(RecStatus::Type status, const QString &file, int line)
Definition: recorderbase.cpp:395
ImportRecorder::m_nfc
long long m_nfc
Definition: importrecorder.h:50
RecorderBase::m_recording
bool m_recording
True while recording is actually being performed.
Definition: recorderbase.h:346
ImportRecorder
ImportRecorder imports files, creating a seek map and other stuff that MythTV likes to have for recor...
Definition: importrecorder.h:23
tv_rec.h
kAudioMuted
@ kAudioMuted
Definition: mythplayer.h:74
PlayerContext
Definition: playercontext.h:49
MythMediaBuffer::Create
static MythMediaBuffer * Create(const QString &Filename, bool Write, bool UseReadAhead=true, std::chrono::milliseconds Timeout=kDefaultOpenTimeout, bool StreamOnly=false)
Creates a RingBuffer instance.
Definition: mythmediabuffer.cpp:98
RecordingProfile
Definition: recordingprofile.h:41
UpdateFS
void UpdateFS(int pc, void *ir)
Definition: importrecorder.cpp:70
RecorderBase::m_unpauseWait
QWaitCondition m_unpauseWait
Definition: recorderbase.h:342
DecoderBase::GetFramesRead
long long GetFramesRead(void) const
Definition: decoderbase.h:203
kImportRecorderInUseID
const QString kImportRecorderInUseID
Definition: programtypes.cpp:19
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:898
RecorderBase::m_requestRecording
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:344