forked from DragonMinded/serialdvdemu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo.cpp
More file actions
342 lines (300 loc) · 8.82 KB
/
Copy pathVideo.cpp
File metadata and controls
342 lines (300 loc) · 8.82 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <memory.h>
#include "Video.h"
#include "DVDEmu.h"
#define OPCODE_STOP 1
#define OPCODE_PLAY 2
#define OPCODE_PAUSE 3
#define OPCODE_UNPAUSE 4
/* Emulator state */
struct video_state
{
pthread_t thread;
pthread_mutex_t lock;
unsigned char queue[256];
unsigned int queue_pos;
unsigned int power_state;
unsigned int playing_state;
unsigned int paused_state;
unsigned int pause_delay;
unsigned int chapter;
unsigned int max_chapter;
char * dvd_path;
} video_state;
void *VideoThread( void *state )
{
/* Get state to read from */
struct video_state *private_state = (struct video_state *)state;
int loaded = 0;
while( true )
{
/* Process video commands, safe to read this because
* if it is non-zero we will try locking anyway. */
if( private_state->queue_pos > 0 )
{
pthread_mutex_lock(&private_state->lock);
unsigned int amount = 0;
unsigned int opcode = private_state->queue[0];
unsigned int arg = private_state->queue[1];
switch( opcode )
{
case OPCODE_STOP:
/* Stop */
amount = 1;
break;
case OPCODE_PAUSE:
/* Pause */
amount = 1;
break;
case OPCODE_UNPAUSE:
/* Pause */
amount = 1;
break;
case OPCODE_PLAY:
/* Seek */
amount = 2;
break;
}
/* Remove if needed */
if( amount < private_state->queue_pos )
{
memmove(&private_state->queue[0], &private_state->queue[amount], private_state->queue_pos - amount);
}
private_state->queue_pos -= amount;
pthread_mutex_unlock(&private_state->lock);
/* Perform operation */
switch( opcode )
{
case OPCODE_STOP:
/* Stop */
if( loaded )
{
exec_shell("./control.sh stop > /dev/null 2> /dev/null");
exec_shell("killall dbus-daemon > /dev/null 2> /dev/null");
exec_shell("killall omxplayer > /dev/null 2> /dev/null");
exec_shell("killall omxplayer.bin > /dev/null 2> /dev/null");
loaded = 0;
}
break;
case OPCODE_PAUSE:
/* Pause */
if( loaded )
{
exec_shell("./control.sh pause > /dev/null 2> /dev/null");
}
break;
case OPCODE_UNPAUSE:
/* Pause */
if( loaded )
{
if( private_state->pause_delay )
{
usleep( private_state->pause_delay * 1000 );
}
exec_shell("./control.sh pause > /dev/null 2> /dev/null");
}
break;
case OPCODE_PLAY:
/* Play */
if( !loaded )
{
/* TODO: Search for videos based on prepended zero or not.
* Also accept any extension that we support.
*/
char syscall[256];
sprintf(syscall, "omxplayer --loop -b --no-osd %s%02d.m4v > /dev/null 2> /dev/null &", private_state->dvd_path, arg);
exec_shell(syscall);
/* Wait for it to start */
while( 1 )
{
int exitcode = exec_shell("./control.sh status > /dev/null 2> /dev/null");
if( exitcode == 0 )
{
break;
}
}
loaded = 1;
}
break;
}
}
else
{
/* Sleep for a bit */
usleep( 10000 );
}
}
return NULL;
}
void VideoThreadAction( unsigned int opcode, unsigned int argument )
{
pthread_mutex_lock(&video_state.lock);
switch(opcode)
{
case OPCODE_STOP:
/* Stop */
video_state.queue[video_state.queue_pos] = OPCODE_STOP;
video_state.queue_pos++;
break;
case OPCODE_PAUSE:
/* Pause */
video_state.queue[video_state.queue_pos] = OPCODE_PAUSE;
video_state.queue_pos++;
break;
case OPCODE_UNPAUSE:
/* Pause */
video_state.queue[video_state.queue_pos] = OPCODE_UNPAUSE;
video_state.queue_pos++;
break;
case OPCODE_PLAY:
/* Seek */
video_state.queue[video_state.queue_pos] = OPCODE_PLAY;
video_state.queue_pos++;
video_state.queue[video_state.queue_pos] = argument;
video_state.queue_pos++;
break;
}
pthread_mutex_unlock(&video_state.lock);
}
void VideoInit( char * path, int chapters )
{
video_state.power_state = 0;
video_state.playing_state = 0;
video_state.paused_state = 0;
video_state.chapter = 1;
video_state.dvd_path = path;
video_state.max_chapter = chapters;
video_state.pause_delay = 0;
verbose_printf( "Disc has %d chapters.\n", video_state.max_chapter );
/* Start video playback thread */
memset( video_state.queue, 0, sizeof(video_state.queue) );
video_state.queue_pos = 0;
pthread_mutex_init(&video_state.lock, NULL);
pthread_create(&video_state.thread, NULL, VideoThread, &video_state);
}
void VideoSetPauseDelay( unsigned int milliseconds )
{
video_state.pause_delay = milliseconds;
}
void PowerOn()
{
if( !video_state.power_state )
{
verbose_printf( "* Exit standby mode.\n" );
video_state.power_state = 1;
Stop();
}
}
void PowerOff()
{
if( video_state.power_state )
{
Stop();
verbose_printf( "* Enter standby mode.\n" );
video_state.power_state = 0;
}
}
void Play()
{
if( !video_state.power_state )
{
PowerOn();
}
if( !video_state.playing_state )
{
verbose_printf( "* Enter playing state.\n" );
SeekToChapter(video_state.chapter);
video_state.playing_state = 1;
video_state.paused_state = 0;
}
else
{
/* Unpause only if we need to */
Unpause();
}
}
void Pause()
{
if( video_state.power_state &&
video_state.playing_state &&
!video_state.paused_state )
{
verbose_printf( "* Enter paused state.\n" );
VideoThreadAction( OPCODE_PAUSE, 0 );
video_state.playing_state = 1;
video_state.paused_state = 1;
}
}
void Unpause()
{
if( video_state.power_state &&
video_state.playing_state &&
video_state.paused_state )
{
verbose_printf( "* Exit paused state.\n" );
VideoThreadAction( OPCODE_UNPAUSE, 0 );
video_state.playing_state = 1;
video_state.paused_state = 0;
}
}
int SeekToChapter( unsigned int chapter )
{
if( video_state.power_state )
{
/* Is the chapter in bounds? */
if( chapter >= 1 && chapter <= video_state.max_chapter )
{
verbose_printf( "* Seek to chapter %d.\n", chapter );
/* Kill player */
VideoThreadAction( OPCODE_STOP, 0 );
/* Seek */
VideoThreadAction( OPCODE_PLAY, chapter );
video_state.playing_state = 1;
/* Repause if needed */
if( video_state.paused_state )
{
VideoThreadAction( OPCODE_PAUSE, 0 );
}
video_state.chapter = chapter;
return 1;
}
else
{
return 0;
}
}
else
{
/* Nothing seek-wise while off */
return 0;
}
}
void Stop()
{
if( video_state.power_state &&
( video_state.playing_state ||
video_state.paused_state ) )
{
/* If we are paused, exit the pause state */
Unpause();
verbose_printf( "* Enter idle state.\n" );
VideoThreadAction( OPCODE_STOP, 0 );
video_state.playing_state = 0;
video_state.paused_state = 0;
video_state.chapter = 1;
}
}
int GetChapter()
{
return video_state.chapter;
}
int IsPowered()
{
return video_state.power_state;
}
int IsPlaying()
{
return video_state.playing_state;
}