forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstatemachine.cpp
More file actions
323 lines (299 loc) · 9.51 KB
/
Copy pathstatemachine.cpp
File metadata and controls
323 lines (299 loc) · 9.51 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file statemachine.cpp
* Device State Machine and Event Handler
*
* This file defines cStateMachine which is the implementation
* of a state machine for cSoftHdDevice and the event handler which
* controls the state machine.
*
* @copyright 2025 - 2026 by Andreas Baierl. All Rights Reserved.
*
* @license{AGPL-3.0-or-later}
*/
#include <atomic>
#include <mutex>
#include <variant>
#include <vector>
#include <vdr/thread.h>
#include "logger.h"
#include "softhddevice.h"
#include "statemachine.h"
/**
* Create the state machine
*
* @param device pointer to cSoftHdDevice object
*/
cStateMachine::cStateMachine(cSoftHdDevice *device)
: m_pDevice(device)
{
}
/**
* Sets the new state and triggers actions, which are necessary
* when leaving the old and entering the new state.
*
* @param newState target state
*/
void cStateMachine::ChangeState(State newState)
{
if (m_state == newState)
return;
LOGDEBUG("STATE MACHINE: Preparing to leave state %s", StateToString(m_state));
m_pDevice->LeaveState(m_state);
LOGDEBUG("STATE MACHINE: Changing state %s -> %s", StateToString(m_state), StateToString(newState));
m_state = newState;
m_pDevice->EnterState(m_state);
LOGDEBUG("STATE MACHINE: State changed to %s", StateToString(m_state));
}
/**
* Event handler for playback state transitions
*
* Processes events (Play, Pause, Stop, TrickSpeed, StillPicture) and performs
* the appropriate state transitions based on the current state. The method halts
* both display and decoding threads before processing the event and resumes them
* afterwards to ensure thread-safe state transitions.
*
* @param event The event to process (variant type containing specific event data)
*/
void cStateMachine::OnEventReceived(const Event& event)
{
uint64_t startStateChange = cTimeMs::Now();
LOGDEBUG("STATE MACHINE: received %s", EventToString(event));
bool needsResume = false;
#ifdef USE_GLES
// Lock the GL thread before the state machine lock, because cmdCopyBufferToOutputFb() calls
// cSoftHdDevice::OsdDrawARGB(), which itself locks the state machine mutex and we can end
// up in a deadlock then.
// We can safely unlock the thread again after the state change, because cSoftHdDevice::OsdDrawARGB()
// always tests if we are in detached mode and this new state is probably set then.
bool needsOglResume = false;
if (m_state != DETACHED)
needsOglResume = m_pDevice->HaltOpenGlThread();
#endif
{ // locked state machine context
std::lock_guard<std::mutex> lock(m_mutex);
if (m_state != DETACHED) {
m_pDevice->HaltVideoThreads();
needsResume = true;
}
auto invalid = [this, &event]() {
LOGWARNING("STATE MACHINE: Invalid event '%s' in state '%s' received", EventToString(event), StateToString(m_state));
};
switch (m_state) {
case State::DETACHED:
std::visit(overload{
[&invalid](const PlayEvent&) { invalid(); },
[&invalid](const PauseEvent&) { invalid(); },
[&invalid](const StopEvent&) { invalid(); },
[&invalid](const TrickSpeedEvent&) { invalid(); },
[&invalid](const StillPictureEvent&) { invalid(); },
[](const DetachEvent&) { /* ignore */ },
[this](const AttachEvent&) {
if (!m_pDevice->IsDetachForced())
ChangeState(STOP);
},
[&invalid](const BufferUnderrunEvent&) { invalid(); },
[&invalid](const BufferingThresholdReachedEvent&) { invalid(); },
[&invalid](const ScheduleResyncAtPtsMsEvent&) { invalid(); },
[&invalid](const ResyncEvent&) { invalid(); },
[&invalid](const DisplayChangeEvent&) { invalid(); },
}, event);
needsResume = false;
break;
case State::STOP:
std::visit(overload{
[this, &needsResume](const PlayEvent&) {
if (m_pDevice->InitAudio(true)) {
LOGERROR("device: audio init failed, change to detached mode");
m_pDevice->SetDetachForced();
ChangeState(DETACHED);
needsResume = false;
} else {
ChangeState(BUFFERING);
m_pDevice->ResetVideoFilter();
}
},
[&invalid](const PauseEvent&) { invalid(); },
[&invalid](const StopEvent&) { invalid(); },
[&invalid](const TrickSpeedEvent&) { invalid(); },
[this](const StillPictureEvent& s) {
m_pDevice->HandleStillPicture(s.data, s.size);
},
[this, &needsResume](const DetachEvent&) {
ChangeState(DETACHED);
needsResume = false;
},
[&invalid](const AttachEvent&) { invalid(); },
[&invalid](const BufferUnderrunEvent&) { invalid(); },
[&invalid](const BufferingThresholdReachedEvent&) { invalid(); },
[&invalid](const ScheduleResyncAtPtsMsEvent&) { invalid(); },
[&invalid](const ResyncEvent&) { invalid(); },
[this](const DisplayChangeEvent& d) {
m_pDevice->HandleDisplayModeChange(d.mode);
},
}, event);
break;
case State::BUFFERING:
std::visit(overload{
[this](const PlayEvent&) { /* ignore */ },
[this](const PauseEvent&) { /* ignore */ },
[this](const StopEvent&) {
ChangeState(STOP);
},
[this](const TrickSpeedEvent& t) {
// abort buffering and proceed with trick speed immediately, because trick speed shall be as fast and as demanded as possible
ChangeState(PLAY);
m_pDevice->SetTrickSpeed(t.speed, t.active, t.forward);
ChangeState(TRICK_SPEED);
},
[this](const StillPictureEvent& s) {
m_pDevice->HandleStillPicture(s.data, s.size);
},
[this, &needsResume](const DetachEvent&) {
ChangeState(DETACHED);
needsResume = false;
},
[&invalid](const AttachEvent&) { invalid(); },
[&invalid](const BufferUnderrunEvent&) { invalid(); },
[this](const BufferingThresholdReachedEvent&) {
if (m_pDevice->SchedulePlaybackStart())
ChangeState(PLAY);
},
[this](const ScheduleResyncAtPtsMsEvent& s) {
ChangeState(PLAY);
m_pDevice->ScheduleResyncAtPtsMs(s.pts);
},
[&invalid](const ResyncEvent&) { invalid(); },
[this](const DisplayChangeEvent& d) {
m_pDevice->HandleDisplayModeChange(d.mode);
},
}, event);
break;
case State::PLAY:
std::visit(overload{
[this](const PlayEvent&) {
m_pDevice->ResumeFromPause();
},
[this](const PauseEvent&) {
m_pDevice->PausePlayback(true);
},
[this](const StopEvent&) {
ChangeState(STOP);
},
[this](const TrickSpeedEvent& t) {
m_pDevice->SetTrickSpeed(t.speed, t.active, t.forward);
ChangeState(TRICK_SPEED);
},
[this](const StillPictureEvent& s) {
m_pDevice->HandleStillPicture(s.data, s.size);
},
[this, &needsResume](const DetachEvent&) {
ChangeState(DETACHED);
needsResume = false;
},
[&invalid](const AttachEvent&) { invalid(); },
[this](const BufferUnderrunEvent&) {
ChangeState(BUFFERING);
},
[&invalid](const BufferingThresholdReachedEvent&) { /* ignore */ },
[this](const ScheduleResyncAtPtsMsEvent& s) {
m_pDevice->ScheduleResyncAtPtsMs(s.pts);
},
[this](const ResyncEvent&) {
ChangeState(BUFFERING);
},
[this](const DisplayChangeEvent& d) {
m_pDevice->HandleDisplayModeChange(d.mode);
},
}, event);
break;
case State::TRICK_SPEED:
std::visit(overload{
[this](const PlayEvent&) {
ChangeState(PLAY);
},
[this](const PauseEvent&) {
m_pDevice->PausePlayback(false);
},
[this](const StopEvent&) {
ChangeState(STOP);
},
[this](const TrickSpeedEvent& t) {
// resume from pause, or change trick speed direction/speed
m_pDevice->SetTrickSpeed(t.speed, t.active, t.forward);
m_pDevice->ResumePlayback();
},
[this](const StillPictureEvent& s) {
m_pDevice->HandleStillPicture(s.data, s.size);
},
[this, &needsResume](const DetachEvent&) {
ChangeState(DETACHED);
needsResume = false;
},
[&invalid](const AttachEvent&) { invalid(); },
[this](const BufferUnderrunEvent&) { /* ignore during trick speed. Fast forward/reverse as fast and as demanded as possible */ },
[&invalid](const BufferingThresholdReachedEvent&) { invalid(); },
[&invalid](const ScheduleResyncAtPtsMsEvent&) { invalid(); },
[&invalid](const ResyncEvent&) { invalid(); },
[this](const DisplayChangeEvent& d) {
m_pDevice->HandleDisplayModeChange(d.mode);
},
}, event);
break;
}
if (needsResume)
m_pDevice->ResumeVideoThreads();
} // end of locked state machine context
#ifdef USE_GLES
if (needsOglResume)
m_pDevice->ResumeOpenGlThread();
#endif
uint64_t stopStateChange = cTimeMs::Now();
LOGDEBUG("STATE MACHINE: state change done in %d ms", (int)(stopStateChange - startStateChange));
}
/**
* Create and start the event handler thread
*
* @param statemachine pointer to the cStateMachine object as the final event receiver
*/
cEventHandler::cEventHandler(cStateMachine *statemachine)
: cThread("event handler"),
m_pStateMachine(statemachine)
{
Start();
}
/**
* Stop and delete the event handler thread
*/
cEventHandler::~cEventHandler(void)
{
Cancel(2);
}
/**
* Add an event to the queue
*
* @param event event, which should be added to the queue
*/
void cEventHandler::AddEvent(Event event)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_eventQueue.push_back(event);
}
/**
* Periodically send events in the queue to the final event receiver/handler
*/
void cEventHandler::Action(void)
{
LOGDEBUG("device: event queue handler thread started");
while (Running()) {
std::vector<Event> local;
{
std::lock_guard<std::mutex> lock(m_mutex);
local.swap(m_eventQueue);
}
for (auto &event : local)
m_pStateMachine->OnEventReceived(event);
usleep(10000);
}
LOGDEBUG("device: event queue handler thread stopped");
}