/
dvbrecorder.cpp
190 lines (155 loc) · 4.9 KB
/
dvbrecorder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Class DVBRecorder
*
* Copyright (C) Daniel Thor Kristjansson 2010
*
* This class glues the DVBStreamHandler which handles the DVB devices
* to the DTVRecorder that handles recordings in MythTV.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// MythTV includes
#include "dvbstreamhandler.h"
#include "mpegstreamdata.h"
#include "dvbrecorder.h"
#include "dvbchannel.h"
#include "tv_rec.h"
#include "mythlogging.h"
#define LOC QString("DVBRec[%1](%2): ") \
.arg(tvrec ? tvrec->GetCaptureCardNum() : -1).arg(videodevice)
DVBRecorder::DVBRecorder(TVRec *rec, DVBChannel *channel)
: DTVRecorder(rec), _channel(channel), _stream_handler(NULL)
{
videodevice = QString::null;
}
bool DVBRecorder::Open(void)
{
if (IsOpen())
{
LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
return true;
}
if (videodevice.isEmpty())
return false;
ResetForNewFile();
_stream_handler = DVBStreamHandler::Get(videodevice);
LOG(VB_RECORD, LOG_INFO, LOC + "Card opened successfully");
return true;
}
bool DVBRecorder::IsOpen(void) const
{
return (NULL != _stream_handler);
}
void DVBRecorder::Close(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
DVBStreamHandler::Return(_stream_handler);
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
}
void DVBRecorder::run(void)
{
if (!Open())
{
_error = "Failed to open DVB device";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
return;
}
{
QMutexLocker locker(&pauseLock);
request_recording = true;
recording = true;
recordingWait.wakeAll();
}
// Listen for time table on DVB standard streams
if (_channel && (_channel->GetSIStandard() == "dvb"))
_stream_data->AddListeningPID(DVB_TDT_PID);
// Make sure the first things in the file are a PAT & PMT
bool tmp = _wait_for_keyframe_option;
_wait_for_keyframe_option = false;
HandleSingleProgramPAT(_stream_data->PATSingleProgram());
HandleSingleProgramPMT(_stream_data->PMTSingleProgram());
_wait_for_keyframe_option = tmp;
_stream_data->AddAVListener(this);
_stream_data->AddWritingListener(this);
_stream_handler->AddListener(_stream_data, false, true);
while (IsRecordingRequested() && !IsErrored())
{
if (PauseAndWait())
continue;
{ // sleep 100 milliseconds unless StopRecording() or Unpause()
// is called, just to avoid running this too often.
QMutexLocker locker(&pauseLock);
if (!request_recording || request_pause)
continue;
unpauseWait.wait(&pauseLock, 100);
}
if (!_input_pmt)
{
LOG(VB_GENERAL, LOG_WARNING, LOC +
"Recording will not commence until a PMT is set.");
usleep(5000);
continue;
}
if (!_stream_handler->IsRunning())
{
_error = "Stream handler died unexpectedly.";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
}
}
_stream_handler->RemoveListener(_stream_data);
_stream_data->RemoveWritingListener(this);
_stream_data->RemoveAVListener(this);
Close();
FinishRecording();
QMutexLocker locker(&pauseLock);
recording = false;
recordingWait.wakeAll();
}
bool DVBRecorder::PauseAndWait(int timeout)
{
QMutexLocker locker(&pauseLock);
if (request_pause)
{
if (!IsPaused(true))
{
_stream_handler->RemoveListener(_stream_data);
paused = true;
pauseWait.wakeAll();
if (tvrec)
tvrec->RecorderPaused();
}
unpauseWait.wait(&pauseLock, timeout);
}
if (!request_pause && IsPaused(true))
{
paused = false;
_stream_handler->AddListener(_stream_data, false, true);
unpauseWait.wakeAll();
}
return IsPaused(true);
}
QString DVBRecorder::GetSIStandard(void) const
{
return _channel->GetSIStandard();
}
void DVBRecorder::SetCAMPMT(const ProgramMapTable *pmt)
{
_channel->SetPMT(pmt);
}
void DVBRecorder::UpdateCAMTimeOffset(void)
{
_channel->SetTimeOffset(GetStreamData()->TimeOffset());
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */