-
Notifications
You must be signed in to change notification settings - Fork 13
/
tasks.cpp
266 lines (222 loc) · 6.58 KB
/
tasks.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "tasks.h"
#include "stdext.h"
#include "recman.h"
// STL headers need to be before VDR tools.h (included by <vdr/menu.h>)
#include <algorithm>
#include <functional>
#include <vdr/menu.h>
namespace vdrlive {
const char* NowReplaying()
{
return cReplayControl::NowReplaying();
}
StickyTask::StickyTask()
{
LiveTaskManager().AddStickyTask( *this );
}
StickyTask::~StickyTask()
{
LiveTaskManager().RemoveStickyTask( *this );
}
void SwitchChannelTask::Action()
{
LOCK_CHANNELS_READ;
cChannel* channel = (cChannel *)Channels->GetByChannelID( m_channel );
if ( channel == 0 ) {
SetError( tr("Couldn't find channel or no channels available.") );
return;
}
if ( !Channels->SwitchTo( channel->Number() ) )
SetError( tr("Couldn't switch to channel.") );
}
void PlayRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const* recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError( tr("Couldn't find recording or no recordings available.") );
return;
}
const char *current = NowReplaying();
if (!current || (0 != strcmp(current, recording->FileName()))) {
cReplayControl::SetRecording( 0 );
cControl::Shutdown();
cReplayControl::SetRecording( recording->FileName() );
cControl::Launch( new cReplayControl );
cControl::Attach();
}
else {
#if APIVERSNUM >= 20402
cMutexLock mutexLock;
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control(mutexLock));
#else
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control());
#endif
if (! replayControl) {
SetError(tr("Cannot control playback!"));
return;
}
replayControl->Play();
}
}
void PauseRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const* recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError( tr("Couldn't find recording or no recordings available.") );
return;
}
const char *current = NowReplaying();
if (!current) {
SetError(tr("Not playing a recording."));
return;
}
if (0 != strcmp(current, recording->FileName())) {
// not replaying same recording like in request
SetError(tr("Not playing the same recording as from request."));
return;
}
#if APIVERSNUM >= 20402
cMutexLock mutexLock;
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control(mutexLock));
#else
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control());
#endif
if (! replayControl) {
SetError(tr("Cannot control playback!"));
return;
}
replayControl->Pause();
}
void StopRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const* recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError( tr("Couldn't find recording or no recordings available.") );
return;
}
const char *current = NowReplaying();
if (!current) {
SetError(tr("Not playing a recording."));
return;
}
cReplayControl::SetRecording( 0 );
cControl::Shutdown();
}
void ForwardRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const* recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError( tr("Couldn't find recording or no recordings available.") );
return;
}
const char *current = NowReplaying();
if (!current) {
SetError(tr("Not playing a recording."));
return;
}
if (0 != strcmp(current, recording->FileName())) {
// not replaying same recording like in request
SetError(tr("Not playing the same recording as from request."));
return;
}
#if APIVERSNUM >= 20402
cMutexLock mutexLock;
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control(mutexLock));
#else
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control());
#endif
if (! replayControl) {
SetError(tr("Cannot control playback!"));
return;
}
replayControl->Forward();
}
void BackwardRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const* recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError(tr("Couldn't find recording or no recordings available."));
return;
}
const char *current = NowReplaying();
if (!current) {
SetError(tr("Not playing a recording."));
return;
}
if (0 != strcmp(current, recording->FileName())) {
// not replaying same recording like in request
SetError(tr("Not playing the same recording as from request."));
return;
}
#if APIVERSNUM >= 20402
cMutexLock mutexLock;
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control(mutexLock));
#else
cReplayControl* replayControl = reinterpret_cast<cReplayControl*>(cControl::Control());
#endif
if (! replayControl) {
SetError(tr("Cannot control playback!"));
return;
}
replayControl->Backward();
}
void RemoveRecordingTask::Action()
{
RecordingsManagerPtr recordings = LiveRecordingsManager();
cRecording const * recording = recordings->GetByMd5Hash( m_recording );
if ( recording == 0 ) {
SetError( tr("Couldn't find recording or no recordings available.") );
return;
}
m_recName = recording->Name();
const char *current = NowReplaying();
if (current && (0 == strcmp(current, recording->FileName()))) {
SetError(tr("Attempt to delete recording currently in playback."));
return;
}
recordings->DeleteRecording(recording);
}
TaskManager::TaskManager()
{
}
void TaskManager::AddStickyTask( Task& task )
{
cMutexLock lock( this );
m_stickyTasks.push_back( &task );
}
void TaskManager::RemoveStickyTask( Task& task )
{
cMutexLock lock( this );
m_stickyTasks.erase( find( m_stickyTasks.begin(), m_stickyTasks.end(), &task ) );
}
bool TaskManager::Execute( Task& task )
{
cMutexLock lock( this );
m_taskQueue.push_back( &task );
m_scheduleWait.Wait( *this );
return task.Result();
}
void TaskManager::DoScheduledTasks()
{
if ( m_taskQueue.empty() && m_stickyTasks.empty() )
return;
using std::for_each;
using std::bind;
using namespace std::placeholders;
cMutexLock lock( this );
for_each( m_taskQueue.begin(), m_taskQueue.end(), bind( &Task::Action, _1 ) );
for_each( m_stickyTasks.begin(), m_stickyTasks.end(), bind( &Task::Action, _1 ) );
m_taskQueue.clear();
m_scheduleWait.Broadcast();
}
TaskManager& LiveTaskManager()
{
static TaskManager instance;
return instance;
}
} // namespace vdrlive