-
Notifications
You must be signed in to change notification settings - Fork 344
/
ExternalRecorder.cpp
220 lines (178 loc) · 5.54 KB
/
ExternalRecorder.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: c++ -*-
* Class ExternalRecorder
*
* Copyright (C) John Poet 2013
*
* 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
*/
// Qt includes
#include <QString>
// MythTV includes
#include "ExternalStreamHandler.h"
#include "ExternalRecorder.h"
#include "ExternalChannel.h"
#include "ringbuffer.h"
#include "tv_rec.h"
#define LOC QString("ExternalRec[%1](%2): ") \
.arg(tvrec ? tvrec->GetInputId() : -1) \
.arg(m_channel->GetDevice())
ExternalRecorder::ExternalRecorder(TVRec *rec, ExternalChannel *channel) :
DTVRecorder(rec), m_channel(channel), m_stream_handler(NULL)
{
}
void ExternalRecorder::StartNewFile(void)
{
// Make sure the first things in the file are a PAT & PMT
HandleSingleProgramPAT(_stream_data->PATSingleProgram(), true);
HandleSingleProgramPMT(_stream_data->PMTSingleProgram(), true);
}
void ExternalRecorder::run(void)
{
if (!Open())
{
_error = "Failed to open device";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
return;
}
if (!_stream_data)
{
_error = "MPEGStreamData pointer has not been set";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
Close();
return;
}
{
QMutexLocker locker(&pauseLock);
request_recording = true;
recording = true;
recordingWait.wakeAll();
}
if (m_channel->HasGeneratedPAT())
{
const ProgramAssociationTable *pat = m_channel->GetGeneratedPAT();
const ProgramMapTable *pmt = m_channel->GetGeneratedPMT();
_stream_data->Reset(pat->ProgramNumber(0));
_stream_data->HandleTables(MPEG_PAT_PID, *pat);
_stream_data->HandleTables(pat->ProgramPID(0), *pmt);
LOG(VB_GENERAL, LOG_INFO, LOC + "PMT set");
}
StartNewFile();
_stream_data->AddAVListener(this);
_stream_data->AddWritingListener(this);
m_stream_handler->AddListener(_stream_data, false, true);
StartStreaming();
while (IsRecordingRequested() && !IsErrored())
{
if (PauseAndWait())
continue;
if (!_input_pmt)
{
LOG(VB_GENERAL, LOG_WARNING, LOC +
"Recording will not commence until a PMT is set.");
usleep(5000);
continue;
}
if (!m_stream_handler->IsRunning())
{
_error = "Stream handler died unexpectedly.";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
}
}
StopStreaming();
m_stream_handler->RemoveListener(_stream_data);
_stream_data->RemoveWritingListener(this);
_stream_data->RemoveAVListener(this);
Close();
FinishRecording();
QMutexLocker locker(&pauseLock);
recording = false;
recordingWait.wakeAll();
}
bool ExternalRecorder::Open(void)
{
QString result;
if (IsOpen())
{
LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
return true;
}
ResetForNewFile();
m_stream_handler = ExternalStreamHandler::Get(m_channel->GetDevice());
if (m_stream_handler)
{
if (m_stream_handler->IsAppOpen())
LOG(VB_RECORD, LOG_INFO, LOC + "Opened successfully");
else
{
LOG(VB_RECORD, LOG_ERR, LOC +
QString("Failed to open recorder: %1")
.arg(m_stream_handler->ErrorString()));
ExternalStreamHandler::Return(m_stream_handler);
return false;
}
return true;
}
LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
return false;
}
void ExternalRecorder::Close(void)
{
QString result;
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
if (IsOpen())
ExternalStreamHandler::Return(m_stream_handler);
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
}
bool ExternalRecorder::PauseAndWait(int timeout)
{
QMutexLocker locker(&pauseLock);
if (request_pause)
{
if (!IsPaused(true))
{
LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait pause");
StopStreaming();
paused = true;
pauseWait.wakeAll();
if (tvrec)
tvrec->RecorderPaused();
}
}
else if (IsPaused(true))
{
LOG(VB_RECORD, LOG_INFO, LOC + "PauseAndWait unpause");
StartStreaming();
if (_stream_data)
_stream_data->Reset(_stream_data->DesiredProgram());
paused = false;
}
// Always wait a little bit, unless woken up
unpauseWait.wait(&pauseLock, timeout);
return IsPaused(true);
}
bool ExternalRecorder::StartStreaming(void)
{
m_h264_parser.Reset();
_wait_for_keyframe_option = true;
_seen_sps = false;
LOG(VB_RECORD, LOG_INFO, LOC + "StartStreaming");
if (m_stream_handler && m_stream_handler->StartStreaming(true))
return true;
return false;
}
bool ExternalRecorder::StopStreaming(void)
{
return (m_stream_handler && m_stream_handler->StopStreaming());
}