-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtasks.h
190 lines (146 loc) · 3.7 KB
/
tasks.h
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
#ifndef VDR_LIVE_TASKS_H
#define VDR_LIVE_TASKS_H
// STL headers need to be before VDR tools.h (included by <vdr/channels.h>)
#include <string>
#include <vector>
#if TNTVERSION >= 30000
#include <cxxtools/log.h> // must be loaded before any VDR include because of duplicate macros (LOG_ERROR, LOG_DEBUG, LOG_INFO)
#endif
#include <vdr/channels.h>
#include <vdr/thread.h>
#include "timers.h"
namespace vdrlive {
class Task;
class TaskManager: public cMutex
{
friend TaskManager& LiveTaskManager();
friend class StickyTask;
typedef std::vector<Task*> TaskList;
public:
bool Execute( Task& task );
// may only be called from Plugin::MainThreadHook
void DoScheduledTasks();
private:
TaskManager();
TaskManager( TaskManager const& );
void AddStickyTask( Task& task );
void RemoveStickyTask( Task& task );
TaskList m_taskQueue;
TaskList m_stickyTasks;
cCondVar m_scheduleWait;
};
class Task
{
friend void TaskManager::DoScheduledTasks();
public:
virtual ~Task() {}
bool Result() const { return m_result; }
std::string const& Error() const { return m_error; }
protected:
explicit Task()
: m_result( true )
{}
Task( Task const& );
void SetError( std::string const& error ) { m_result = false; m_error = error; }
private:
bool m_result;
std::string m_error;
virtual void Action() = 0;
};
class StickyTask: public Task
{
protected:
explicit StickyTask();
virtual ~StickyTask();
};
class SwitchChannelTask: public Task
{
public:
explicit SwitchChannelTask( tChannelID channel ): m_channel( channel ) {}
private:
tChannelID m_channel;
virtual void Action() override;
};
class RecordingTask: public Task
{
protected:
explicit RecordingTask(std::string const& recording)
: m_recording(recording)
{}
std::string m_recording;
};
class PlayRecordingTask: public RecordingTask
{
public:
explicit PlayRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
};
class PauseRecordingTask: public RecordingTask
{
public:
explicit PauseRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
};
class StopRecordingTask: public RecordingTask
{
public:
explicit StopRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
};
class ForwardRecordingTask: public RecordingTask
{
public:
explicit ForwardRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
};
class BackwardRecordingTask: public RecordingTask
{
public:
explicit BackwardRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
};
class RemoveRecordingTask: public RecordingTask
{
public:
explicit RemoveRecordingTask( std::string const& recording )
: RecordingTask(recording)
{}
virtual void Action() override;
std::string const & RecName() const { return m_recName; }
private:
std::string m_recName;
};
TaskManager& LiveTaskManager();
class cLiveWorker: public cThread {
private:
cMutex m_mutex;
cCondVar m_waitCondition;
public:
cLiveWorker() {}
virtual ~cLiveWorker() {}
void Stop() {
m_waitCondition.Broadcast(); // wakeup the thread
Cancel(10); // wait up to 10 seconds for thread was stopping
}
virtual void Action() {
m_mutex.Lock();
int loopSleep = 50; // do this every 50 milli seconds
while (Running()) {
m_waitCondition.TimedWait(m_mutex, loopSleep);
LiveTimerManager().DoPendingWork();
LiveTaskManager().DoScheduledTasks();
}
}
};
} // namespace vdrlive
#endif // VDR_LIVE_TASKS_H