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