-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.cpp
378 lines (345 loc) · 10.7 KB
/
filter.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "filter.hpp"
#include <vlc_common.h>
// #include <vlc_threads.h>
#include <vlc_variables.h>
#include <vlc_playlist.h> // needed for var_GetTime(input_thread_t *, char *)
#include <vlc_aout_volume.h> // aout_ToggleMute
#include <list>
#include <algorithm> // for list::advance
#include <cstdio> // for file io
using namespace std;
#ifndef INT64_C
# define INT64_C(c) c ## LL
#endif
#define CALC_CLOCK_CYCLES(microseconds) (microseconds * CLOCK_FREQ / 1000000) // multiply microseconds by clock cycles per microsecond to get delay in terms of clock cycles
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
// temp
#ifdef MSDEBUG1
#include <iomanip>
#include <iostream>
#include "qt4/frames/gui_helpers.hpp"
using namespace std;
#endif
/* Non-member functions */
namespace Moviesoap
{
static char buffer[12], buffer3[12], buffer2[12];
/* These functions are used as callbacks for timers. Don't delete them. */
void tryModStart(void * p_data) { tryModStart( (Mod *) p_data, MoviesoapGetNow(p_input) ); }
void tryModStop(void * p_data) { tryModStop( (Mod *) p_data, MoviesoapGetNow(p_input) ); }
/* Remove mod from scheduledMods list (if present).
Schedule start or activate mod.
Add to scheduledMods list if timer was set.
If activated and has active effect, calls function to add to scheduledMods list and schedule deactivation.
Calls Filter::loadNextMod.
*/
void tryModStart(Mod * p_mod, mtime_t now)
{
// Remove from scheduled list
p_mod->removeFromScheduledList();
// Calculate delay until start in microseconds
mtime_t delay = p_mod->calcActivationDelay(now);
if (delay) {
// Reschedule start if start time is not reached
p_mod->schedule( delay, tryModStart );
// add to scheduled list
p_mod->addToScheduledList();
} else {
// Activate effect
p_mod->activate();
// Schedule deactivation if mod produces an active effect
if ( p_mod->producesActiveEffect() )
tryModStop(p_mod, now);
// Tell Filter to load next Mod
p_mod->p_filter->loadNextMod(now);
}
}
/* Remove mod from scheduledMods list (if present).
Schedule stop or deactivate mod.
Add to scheduledMods list if timer was set.
*/
void tryModStop(Mod * p_mod, mtime_t now)
{
// Remove from scheduled list
p_mod->removeFromScheduledList();
// Calculate delay until stop in microseconds
mtime_t delay = p_mod->calcDeactivationDelay(now);
if (delay) {
// Reschedule stop if stop time is not reached
p_mod->schedule( delay, tryModStop );
// add to scheduled list
p_mod->addToScheduledList();
} else {
// Deactivate effect
p_mod->deactivate();
}
}
inline void setMute( bool on_off ) { // todo: needs intf lock; otherwise, danger of race condition (which, unfortunately, leads to interface freeze)
#ifdef MSDEBUG3
msg_Info( p_obj, "Is muted? %d", playlist_MuteGet( Moviesoap::p_playlist ) );
#endif
if ( playlist_MuteGet( Moviesoap::p_playlist ) != on_off )
playlist_MuteSet( Moviesoap::p_playlist, on_off );
}
}
/* Filter functions */
namespace Moviesoap
{
/* Sort mods in modList by start time */
void Filter::sort() { modList.sort(); }
void Filter::nullify()
{
filepath.clear();
creator.clear();
title.clear();
imdb.clear();
year.clear();
modList.clear();
scheduledMods.clear();
queuedMod = modList.begin();
}
/* Returns iterator to mod for index */
list<Mod>::iterator Filter::getMod(int i)
{
if (i < 0 || i > modList.size())
return list<Mod>::iterator(NULL);
list<Mod>::iterator iter = modList.begin();
advance(iter, i);
return iter;
}
// ACTIVATING AND DEACTIVATING MODS
void Filter::Restart() { Restart(MoviesoapGetNow(p_input)); }
/* Set queuedMod to first mod. Call loadNextMod */
void Filter::Restart(mtime_t now) {
// Do nothing if there is no input thread.
if (!p_input)
return;
queuedMod = modList.begin();
#ifdef MSDEBUG2
cout << "\tFILTER RESTART (first mod: " << (*queuedMod).description << ")" << endl;
#endif
loadNextMod( now );
}
/* Destroy active timers. Undo any active effects (mute & blackout). Empty scheduledMods list. */
void Filter::StopAndCleanup(bool deactivate_effects)
{
#ifdef MSDEBUG2
cout << "FILTER STOP. " << scheduledMods.size() << " mods active." << endl;
#endif
// iterate through scheduled/active mods
list<Mod*>::iterator iter;
for ( iter = scheduledMods.begin(); iter != scheduledMods.end(); iter++ ) {
Mod * p_mod = *iter;
// remove pointer if NULL
if (p_mod == NULL) {
scheduledMods.erase( iter );
continue;
}
#ifdef MSDEBUG2
cout << "-- stop mod: " << p_mod->description << endl;
#endif
// End effect
if (deactivate_effects)
p_mod->deactivate();
#ifdef MSDEBUG2
cout << "-- stop mod: deactivated" << endl;
#endif
// Destroy active timer
vlc_timer_destroy( p_mod->timer );
#ifdef MSDEBUG2
cout << "-- stop mod: timer destroyed" << endl;
#endif
}
#ifdef MSDEBUG2
cout << "FILTER STOP: timers destroyed. " << endl;
#endif
scheduledMods.clear();
#ifdef MSDEBUG2
cout << "FILTER STOP COMPLETE. " << endl;
#endif
}
/* Destroy active timers. Undo any active effects (mute & blackout). Empty scheduledMods list. */
void Filter::Stop() { StopAndCleanup(true); }
/* Destroy timers, and but leave active effects (mute & blackout) as they are. */
void Filter::KillTimers() { StopAndCleanup(false); }
/* Kills timers on BLACKOUT mods in scheduledMods. (This is useful if a BLACKOUT mod is to be activated but another BLACKOUT is already running. When the first BLACKOUT expires, it would kill the effect even if the seond BLACKOUT weren't expired.) */
void Filter::KillBlackouts()
{
// iterate through scheduled/active mods
list<Mod*>::iterator iter;
for ( iter = scheduledMods.begin(); iter != scheduledMods.end(); iter++ ) {
Mod * p_mod = *iter;
// remove pointer if NULL
if (p_mod == NULL) {
scheduledMods.erase( iter );
continue;
}
if (p_mod->mod.mode == MOVIESOAP_BLACKOUT) {
// remove from scheduledMods
scheduledMods.erase( iter );
// destroy timer
vlc_timer_destroy( p_mod->timer );
// deactivate. just to be on the safe side
p_mod->deactivate();
}
}
}
/* Load queuedMod. Stop Filter if end reached. (Arg 'now' should be in microseconds) */
void Filter::loadNextMod(int64_t now)
{
#ifdef MSDEBUG1
strftime(now/MOVIESOAP_MOD_TIME_FACTOR, buffer);
cout << "-- loadNextMod -- now: " << buffer << endl;
#endif
// Find next mod whose stop time is not passed
for ( ; queuedMod != modList.end(); queuedMod++ ) {
#ifdef MSDEBUG2
strftime(queuedMod->mod.start, buffer);
strftime(queuedMod->mod.stop, buffer2);
cout << "ITER. MOD --" <<
" start: " << buffer <<
" stop: " << buffer2 << " " << (*queuedMod).description << endl;
#endif
if ( shouldEnqueueMod(&*queuedMod, now, MOVIESOAP_UNIVERSAL_TITLE, p_input) ) {
#ifdef MSDEBUG2
cout << "LOADING MOD..." << endl;
#endif
Mod * p_mod = &*queuedMod;
p_mod->p_filter = this;
queuedMod++;
tryModStart( p_mod, now );
return; // break
}
#ifdef MSDEBUG2
else {
cout << "NOT LOADED" << endl;
}
#endif
}
cout << "No further mods to load." << endl;
}
/* Create a filter with a few test mods for testing */
Filter * Filter::dummy()
{
Mod * p_mod;
Filter * p_filter = new Filter();
p_filter->filepath = "/home/markham/tmp/dummy-filter.cln";
p_filter->creator = "MJ";
p_filter->title = "Labour of Love";
p_filter->imdb = "1234567890";
p_filter->year = "1987";
// add mod
p_mod = new Mod( MOVIESOAP_SKIP, 500, 800, 0, 0 );
p_mod->description = "skip talking";
p_filter->modList.push_back( *p_mod );
// add mod
p_mod = new Mod( MOVIESOAP_BLACKOUT, 300, 1300, 5, 6, 5, 30, 400, 60 );
p_mod->description = "blackout (cover) something";
p_filter->modList.push_back( *p_mod );
// add mod
p_mod = new Mod( MOVIESOAP_MUTE, 1100, 2300, 0, 0 );
p_mod->description = "mute the word \"foo\"";
p_filter->modList.push_back( *p_mod );
// sort
p_filter->modList.sort();
return p_filter;
}
void Filter::toStdout() {
list<Mod>::iterator iter;
cout << "============================" << endl;
for (iter = modList.begin(); iter != modList.end(); iter++) {
printf("MOD: %s\n\t%d - %d (%d :: %d) [(%d, %d) (%d, %d)]\n",
iter->description.c_str(), iter->mod.start, iter->mod.stop,
iter->mod.category, iter->mod.severity,
iter->mod.x1, iter->mod.y1, iter->mod.x2, iter->mod.y2);
}
cout << "============================" << endl;
}
}
/* Mod functions */
namespace Moviesoap {
/* Constructor */
Mod::Mod(uint8_t mode, uint32_t start, uint32_t stop, uint8_t category, uint8_t severity, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
mod.mode = mode;
mod.start = start;
mod.stop = stop;
mod.category = category;
mod.severity = severity;
mod.x1 = x1;
mod.x2 = x2;
mod.y1 = y1;
mod.y2 = y2;
}
bool Mod::operator<(const Mod& otherMod) const
{
if (mod.title == otherMod.mod.title)
return mod.start < otherMod.mod.start;
else
return mod.title < otherMod.mod.title;
}
void Mod::activate()
{
#ifdef MSDEBUG2
strftime(MoviesoapGetNow(p_input) / MOVIESOAP_MOD_TIME_FACTOR, buffer);
cout << setw(18) << "ACTIVATE MOD: " <<
"now(" << buffer << ") " << description << endl;
#endif
// Implement effect
switch(mod.mode) {
case MOVIESOAP_SKIP:
var_SetTime( Moviesoap::p_input, "time", stopTime() );
break;
case MOVIESOAP_MUTE:
setMute( true );
break;
case MOVIESOAP_BLACKOUT:
// destroy 'stop' timer on any active blackout mod
if (blackout_config.b_active)
p_filter->KillBlackouts();
// set config
blackout_config.b_active = true;
blackout_config.i_x1 = mod.x1;
blackout_config.i_y1 = mod.y1;
blackout_config.i_x2 = mod.x2;
blackout_config.i_y2 = mod.y2;
break;
}
}
void Mod::deactivate()
{
#ifdef MSDEBUG2
strftime(MoviesoapGetNow(p_input) / MOVIESOAP_MOD_TIME_FACTOR, buffer);
cout << setw(18) << "DEACTIVATE MOD: " <<
"now(" << buffer << ") " << description << endl;
#endif
// Unimplement effect
switch(mod.mode) {
case MOVIESOAP_MUTE:
setMute( false );
break;
case MOVIESOAP_BLACKOUT:
blackout_config.b_active = false;
break;
}
}
void Mod::out(ostream & stream) { stream << "Mod: " << description << " (" << mod.start << "-" << mod.stop << ")" << endl; }
void Mod::nullify()
{
memset(&mod, 0, sizeof(mod));
description.clear();
memset(&timer, 0, sizeof(timer));
}
void Mod::schedule( mtime_t delay_in_us, void (*callback)(void *) )
{
// create timer
vlc_timer_create( &timer, callback, this );
// calc delay in terms of clock cycles
mtime_t delay = CALC_CLOCK_CYCLES(delay_in_us);
// schedule timer
vlc_timer_schedule( timer, false, delay, 0 );
}
}
#undef CALC_CLOCK_CYCLES