/
cetonrecorder.cpp
161 lines (127 loc) · 3.88 KB
/
cetonrecorder.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
/** -*- Mode: c++ -*-
* CetonRecorder
* Copyright (c) 2011 Ronald Frazier
* Copyright (c) 2006-2009 by Silicondust Engineering Ltd.
* Distributed as part of MythTV under GPL v2 and later.
*/
// MythTV includes
#include "cetonstreamhandler.h"
#include "cetonrecorder.h"
#include "cetonchannel.h"
#include "mythlogging.h"
#include "tv_rec.h"
#define LOC QString("CetonRec[%1]: ") \
.arg(tvrec ? tvrec->GetCaptureCardNum() : -1)
CetonRecorder::CetonRecorder(TVRec *rec, CetonChannel *channel) :
DTVRecorder(rec), _channel(channel), _stream_handler(NULL)
{
}
bool CetonRecorder::Open(void)
{
if (IsOpen())
{
LOG(VB_GENERAL, LOG_WARNING, LOC + "Card already open");
return true;
}
ResetForNewFile();
_stream_handler = CetonStreamHandler::Get(_channel->GetDevice());
LOG(VB_RECORD, LOG_INFO, LOC + "Ceton opened successfully");
return true;
}
void CetonRecorder::Close(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- begin");
if (IsOpen())
CetonStreamHandler::Return(_stream_handler);
LOG(VB_RECORD, LOG_INFO, LOC + "Close() -- end");
}
void CetonRecorder::run(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "run -- begin");
/* Create video socket. */
if (!Open())
{
_error = "Failed to open CetonRecorder device";
LOG(VB_GENERAL, LOG_ERR, LOC + _error);
return;
}
{
QMutexLocker locker(&pauseLock);
request_recording = true;
recording = true;
recordingWait.wakeAll();
}
// 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);
while (IsRecordingRequested() && !IsErrored())
{
if (PauseAndWait())
continue;
if (!IsRecordingRequested())
break;
{ // 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);
}
}
LOG(VB_RECORD, LOG_INFO, LOC + "run -- ending...");
_stream_handler->RemoveListener(_stream_data);
_stream_data->RemoveWritingListener(this);
_stream_data->RemoveAVListener(this);
Close();
FinishRecording();
QMutexLocker locker(&pauseLock);
recording = false;
recordingWait.wakeAll();
LOG(VB_RECORD, LOG_INFO, LOC + "run -- end");
}
bool CetonRecorder::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);
unpauseWait.wakeAll();
}
return IsPaused(true);
}
QString CetonRecorder::GetSIStandard(void) const
{
return _channel->GetSIStandard();
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */