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.toLower().startsWith("file:"))
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  {
122  MythMediaBuffer *buffer = MythMediaBuffer::Create(m_ringBuffer->GetFilename(), false, true, 6000);
123  //This does update the status but does not set the ultimate
124  //recorded / failure status for the relevant recording
125  SetRecordingStatus(RecStatus::Recording, __FILE__, __LINE__);
126 
127  auto *ctx = new PlayerContext(kImportRecorderInUseID);
128  ctx->SetPlayingInfo(m_curRecording);
129  ctx->SetRingBuffer(buffer);
130  ctx->SetPlayer(cfp);
131  cfp->SetPlayerInfo(nullptr, nullptr, ctx);
132 
133  m_cfp=cfp;
135  cfp->RebuildSeekTable(false,UpdateFS,this);
137  m_cfp=nullptr;
138 
139  delete ctx;
140  }
141 
143 
144  // cleanup...
145  Close();
146 
147  FinishRecording();
148 
149  QMutexLocker locker(&m_pauseLock);
150  m_recording = false;
151  m_recordingWait.wakeAll();
152 
153  LOG(VB_RECORD, LOG_INFO, LOC + "run -- end");
154 }
155 
157 {
158  if (m_importFd >= 0) // already open
159  return true;
160 
161  if (!m_curRecording)
162  {
163  LOG(VB_RECORD, LOG_ERR, LOC + "no current recording!");
164  return false;
165  }
166 
167  ResetForNewFile();
168 
169  QString fn = m_curRecording->GetPathname();
170 
171  // Quick-and-dirty "copy" of sample prerecorded file.
172  // Sadly, won't work on Windows.
173  //
174  QFile preRecorded(m_videodevice);
175  QFile copy(fn);
176  if (preRecorded.exists() && (!copy.exists() || copy.size() == 0))
177  {
178  if (copy.exists()) // always created by RecorderBase?
179  {
180  QDir targetDir("."); // QDir::remove() needs an object
181  targetDir.remove(fn);
182  }
183 
184  LOG(VB_RECORD, LOG_INFO, LOC + QString("Trying to link %1 to %2")
185  .arg(m_videodevice).arg(fn));
186 
187  if (preRecorded.link(fn))
188  LOG(VB_RECORD, LOG_DEBUG, LOC + "success!");
189  else
190  LOG(VB_RECORD, LOG_ERR, LOC + preRecorded.errorString());
191  }
192 
193  if (fn.toLower().startsWith("myth://"))
194  {
195  LOG(VB_RECORD, LOG_ERR, LOC + "Malformed recording ProgramInfo.");
196  return false;
197  }
198 
199  QFileInfo f(fn);
200  if (!f.exists())
201  {
202  LOG(VB_RECORD, LOG_INFO, LOC +
203  QString("'%1' does not exist yet").arg(fn));
204 
205  // Slow down run open loop when debugging -v record.
206  // This is just to make the debugging output less spammy.
207  if (VERBOSE_LEVEL_CHECK(VB_RECORD, LOG_ANY))
208  std::this_thread::sleep_for(std::chrono::milliseconds(250));
209 
210  return false;
211  }
212  if (!f.isReadable())
213  {
214  LOG(VB_GENERAL, LOG_ERR, LOC +
215  QString("'%1' is not readable").arg(fn));
216  return false;
217  }
218  if (!f.size())
219  {
220  LOG(VB_GENERAL, LOG_ERR, LOC +
221  QString("'%1' is empty").arg(fn));
222  return false;
223  }
224 
225  m_importFd = open(fn.toLocal8Bit().constData(), O_RDONLY);
226  if (m_importFd < 0)
227  {
228  LOG(VB_GENERAL, LOG_ERR, LOC +
229  QString("Couldn't open '%1'").arg(fn) + ENO);
230  }
231 
232  return m_importFd >= 0;
233 }
234 
236 {
237  if (m_importFd >= 0)
238  {
239  close(m_importFd);
240  m_importFd = -1;
241  }
242 }
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:308
ImportRecorder::Open
bool Open(void)
Definition: importrecorder.cpp:156
MythCoreContext::UnregisterFileForWrite
void UnregisterFileForWrite(const QString &file)
Definition: mythcorecontext.cpp:2107
DTVRecorder::ResetForNewFile
void ResetForNewFile(void) override
Definition: dtvrecorder.cpp:137
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:104
MythCoreContext::RegisterFileForWrite
void RegisterFileForWrite(const QString &file, uint64_t size=0LL)
Definition: mythcorecontext.cpp:2089
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:47
MythMediaBuffer
Definition: mythmediabuffer.h:49
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
PlayerFlags
PlayerFlags
Definition: mythplayer.h:87
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:123
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:338
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:1748
MythMediaBuffer::GetRealFileSize
long long GetRealFileSize(void) const
Definition: mythmediabuffer.cpp:485
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:250
MythCommFlagPlayer
Definition: mythcommflagplayer.h:6
f
QTextStream t & f
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:600
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:88
kNoITV
@ kNoITV
Definition: mythplayer.h:100
ImportRecorder::Close
void Close(void)
Definition: importrecorder.cpp:235
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
ImportRecorder::UpdateRecSize
void UpdateRecSize()
Definition: importrecorder.cpp:79
MythPlayer::GetDecoder
DecoderBase * GetDecoder(void)
Returns the stream decoder currently in use.
Definition: mythplayer.h:313
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:98
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: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:345
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:99
PlayerContext
Definition: playercontext.h:48
RecordingProfile
Definition: recordingprofile.h:39
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:203
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:901
RecorderBase::m_requestRecording
bool m_requestRecording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:343