-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecoder.cpp
708 lines (530 loc) · 14.2 KB
/
decoder.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* IvySync - Video SyncStarter
*
* (c) Copyright 2004 - 2006 Denis Rojo <[email protected]>
* Nederlands Instituut voor Mediakunst
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <decoder.h>
#include <utils.h>
#ifdef WITH_GUI
#include <gui.h>
#endif
Decoder::Decoder()
: Thread(), Entry() {
fd = 0;
playmode = SINGLE;
position = -1;
playing = false;
stopped = false;
dummy = false;
#ifdef WITH_GUI
gui = false;
#endif
quit = true;
filesize = 0L;
filepos = 0L;
newfilepos = 0L;
playlist_fd = 0;
buffo = NULL;
}
Decoder::~Decoder() {
close();
free(buffo);
quit = true;
}
bool Decoder::init(const char *dev) {
int len;
if(dummy) {
N("running in dummy test run - no device opened");
} else {
fd = ::open(dev, O_WRONLY|O_NDELAY,S_IWUSR|S_IWGRP|S_IWOTH);
if(fd<0) {
D("error opening device %s: %s",dev,strerror(errno));
return false;
}
}
// save the path to the device
strncpy(device, dev, MAXPATH);
// parse the last 2 cyphers of the device path
len = strlen(dev);
// last two chars of the device name are the number
device_num = atoi(&dev[len-2]);
return true;
}
bool Decoder::setup(bool *sync, int bufsize) {
// save the syncstarter flag
syncstart = sync;
if(buffo) free(buffo);
buffo = (uint8_t*) calloc( bufsize+1, 1024); // +1 safety bound
if(!buffo) {
E("fatal error: can't allocate %uKB of memory for decoder", bufsize);
return(false);
}
buffo_size = bufsize*1024;
quit = false;
return(true);
}
void Decoder::close() {
playing = false;
quit = true;
if(running) {
D("thread was running, waiting to join...");
join();
}
if(fd) ::close(fd);
position = -1;
// delete playlist entries
empty();
}
void Decoder::update() {
if(position<0) { // first time we play from the playlist
position = 1;
} else {
switch(playmode) {
case PLAY: // next
position++;
if( position > playlist.len() ) {
// stop();
position = 1;
}
break;
case CONT: // next or first if at the end
if( position >= (int)playlist.len() ) position = 1;
else position++;
break;
case LOOP:
// play the same again
break;
case RAND:
// play a random one
break;
case SINGLE:
stop();
break;
default:
stop();
// just stop
break;
}
}
// current movie is now in playlist[position];
if( ! playlist.len() ) D("playlist empty");
else {
D("current playlist at position %u:", position);
int c;
for(c=1; c<=playlist.len(); c++) {
D("%s[%u] %s",
(c==position)?"->":" ",
c, playlist[c]->name);
}
}
#ifdef WITH_GUI
// refresh the GUI if present
if(gui) gui->refresh();
#endif
}
void Decoder::run() {
int in, written, writing;
uint8_t *buf;
Entry *movie;
if(!fd && !dummy) {
E("thread %u falling down: no device opened",pthread_self());
return;
}
running = true;
D("thread %u launched",pthread_self());
// set max realtime priority
// if( set_rtpriority(true) )
// A("thread %u running on max realtime priority",pthread_self());
while(!quit) {
update();
if(quit) break;
if(stopped) {
stopped = false;
playing = false;
}
// if is not playing, sleep
while(!playing && !quit) jsleep(0,100);
///////////////////////////
// check it out from the playlist
movie = playlist[position];
if(!movie) {
E("no movie at position %i", position);
playing = false;
continue;
}
// just to be sure
if(playlist_fd) fclose(playlist_fd);
// STAT METHOD FOR FILESIZE
// // check if it exists and ackowledge filesize
// if( stat(movie->name, &moviestat) < 0 ) {
// E("error %u on movie %s: %s", errno, movie->name, strerror(errno));
// if(errno == 75) // value too large for defined datatype
// // we just deactivate seek and position handling
// filesize = 0x0;
// else
// continue;
// } else
// filesize = moviestat.st_size;
playlist_fd = fopen64( movie->name, "rb" );
if(!playlist_fd) {
E("can't open %s: %s (%i)", movie->name, strerror(errno), errno);
if(errno==27) { // EOVERFLOW - file too large on Linux/Glibc
int tmpfd;
tmpfd = open( movie->name, O_RDONLY|O_LARGEFILE);
if(!tmpfd) {
E("failed opening with largefile support: %s",strerror(errno));
continue;
} else playlist_fd = fdopen( tmpfd , "rb" );
} else {
continue;
}
}
N("now playing %s",movie->name);
// read the total length of movie file
fseeko64(playlist_fd, 0L, SEEK_END);
filesize = ftello64(playlist_fd);
// set position at the beginning
filepos = 0L;
fseeko64(playlist_fd, filepos, SEEK_SET);
//////////////////////////////////////
// ??? or should we use:
// fgetpos(playlist_fd, &filesize);
// fsetpos(playlist_fd, &filepos);
// none seem to work with files > 2GB
if(filesize)
A("movie length: %lu KB",filesize/1024);
else
A("movie is too large to be seekable");
do { // inner reading loop
#ifdef WITH_GUI
// update the GUI
if(gui) gui->refresh();
#endif
// process asynchronous flags
if(quit || stopped) break;
while(!playing && !quit) jsleep(0,100);
//////////////////////////////
// read in the data
in = fread(buffo, 1, buffo_size, playlist_fd);
if( feof(playlist_fd) || in<1 ) { // EOF
D("end of file %s",movie->name);
break;
}
written = 0;
writing = in;
buf = buffo;
if(!*syncstart) {
// unlock();
while(!*syncstart) jsleep(0,1); // check every nanosecond
}
while(writing) { // writing loop
buf += written;
// if(quit || stopped) break;
if(dummy) // emulation mode with no device (for devel)
written = writing;
else
written = ::write(fd, buf, writing);
if(written<0) // error on write
continue;
else
filepos += written;
writing -= written;
flush();
}
} while(in>0 && !quit); // read/write inner loop
clear();
// close the file playing
A("end of movie %s", movie->name);
if(playlist_fd) {
fclose(playlist_fd);
playlist_fd = 0;
}
} // run() thread loop
clear();
// close the file playing
if(playlist_fd) {
fclose(playlist_fd);
playlist_fd = 0;
}
A("quitting decoder for %s",device);
D("thread %u finished", pthread_self());
return;
}
void Decoder::flush() {
struct pollfd fdled;
fdled.fd = fd;
fdled.events=POLLOUT;
while( poll(&fdled,1,1000) < 1 ) { // wait infinite until ready
if(fdled.revents & POLLOUT) return;
else {
W("device %i still not ready for writing",fd);
if(quit) return;
}
}
// if there is a seek to do, do it now
if((newfilepos > 0L) && playlist_fd) {
D("seeking to new position %lu", newfilepos);
fseeko64(playlist_fd, newfilepos, SEEK_SET);
filepos = newfilepos;
newfilepos = 0;
}
}
bool Decoder::play() {
playing = true;
stopped = false;
return true;
}
bool Decoder::stop() {
// newfilepos = 1;
// clear();
// playing = false;
stopped = true;
return true;
}
bool Decoder::pause() {
playing = false;
return true;
}
bool Decoder::clear() {
if(!fd) return false;
// close the device
flush();
::close(fd); // *BLANK*
// reopen the device again
fd = ::open(device, O_WRONLY|O_NDELAY,S_IWUSR|S_IWGRP|S_IWOTH);
if(fd<0) {
D("error opening device %s: %s",device,strerror(errno));
return false;
}
return true;
}
int Decoder::getpos() {
// filesize : 100 = filepos : x
// filesize : filepos = 100 : x
int percent;
if(!playlist_fd) return 0;
if(!filesize) return 0;
percent = (int) ( (filepos * 100) / filesize );
D("movie %s at position~ %u %% (%lu byte)",
playlist[position]->name, percent, filepos);
return percent;
}
void Decoder::setpos(int pos) {
// filesize : 100 = x : pos
if(!playlist_fd) return;
if(!filesize) return;
newfilepos = (filesize * pos) / 100;
D("Decoder::setpos(%u) : newfilepos = %lu",
pos, newfilepos);
}
off64_t Decoder::getoffset() {
return filepos;
}
void Decoder::setoffset(off64_t pos) {
(pos < filesize) ? newfilepos = pos : newfilepos = filesize;
}
bool Decoder::prepend(char *file) {
Entry *ent = new Entry();
ent->set_name(file);
playlist.prepend( ent );
return true;
}
bool Decoder::append(char *file) {
Entry *ent = new Entry();
ent->set_name(file);
playlist.append( ent );
return true;
}
bool Decoder::insert(char *file, int pos) {
Entry *ent = new Entry();
ent->set_name(file);
playlist.insert( ent, pos );
return true;
}
/*
bool Decoder::remove(char *file) {
A("TODO: Decoder::remove(char *file)");
return true;
}
*/
bool Decoder::remove(int pos) {
Entry *ent;
ent = playlist[pos];
if(!ent) return false;
delete ent;
return true;
}
bool Decoder::empty() {
Entry *ent = playlist.begin();
while(ent) {
ent->rem();
delete ent;
ent = playlist.begin();
}
// playlist.clear()
position = 1;
return true;
}
static time_t now_epoch;
static struct tm now;
int playlist_selector(const struct dirent *dir) {
char today_str[32];
strftime(today_str,31,"%d%b%y",&now);
if( strstr(dir->d_name, today_str) )
return 1;
if( strstr(dir->d_name, "video") )
return 1;
return 0;
}
int Decoder::load() {
// load the playlist from the .ivysync/ directory
// renders a date string of today in the format of DDMMMYY-HHMM (12Aug)
// if the playlist .ivysync/DDMMM*-videoNN is there load that one
// otherwise fallback on the .ivysync/videoNN playlist
// if that is not even there then we don't have a playlist.
FILE *fd;
char path[512];
char line[1024];
int c = 0;
struct stat st;
struct tm pltime;
struct tm plseltime;
struct dirent **filelist;
int found;
char ThePlaylist[512];
char videodev[64];
char *home = getenv("HOME");
snprintf(path,511,"%s/.ivysync",home);
if( stat(path, &st) != 0) {
D("no saved playlists in %s: %s",path,strerror(errno));
return -1;
}
// when we are now
now_epoch = time(NULL);
localtime_r( &now_epoch, &now );
snprintf(videodev,63,"video%u",device_num);
// use the default playlist
snprintf(ThePlaylist,511,"video%u",device_num);
// scan the directory for scheduled playlists starting with date
found = scandir(path, &filelist, playlist_selector, alphasort);
if(found < 0) {
E("playlist scandir: %s",strerror(errno));
return -1;
}
// setup time selection of the latest playlist of today
memcpy(&plseltime, &now, sizeof(struct tm));
plseltime.tm_hour = 0;
plseltime.tm_min = 0;
// in filelist[] we have all playlists of today
// now need to sort out the ones that are not from this device
// and the ones that are in the future (hour and minutes)
while(found--) {
D("checking playlist for today %s", filelist[found]->d_name);
// eliminate the ones that are not for this device
if( ! strstr( filelist[found]->d_name, videodev ) ) continue;
// read and check the exact time on the filename
// in case the playlist filename doesn't starts with video*
if ( filelist[found]->d_name[0] == 'v') {
snprintf(ThePlaylist,511,"%s",filelist[found]->d_name);
} else {
get_time( filelist[found]->d_name, &pltime );
// skip if we already have a more recent playlist
if(plseltime.tm_hour > pltime.tm_hour) continue;
else if(plseltime.tm_hour == pltime.tm_hour)
if(plseltime.tm_min >= pltime.tm_min) continue;
if(now.tm_hour > pltime.tm_hour) {
D("this playlist is actual, we're going to use this");
snprintf(ThePlaylist,511,"%s",filelist[found]->d_name);
memcpy(&plseltime,&pltime,sizeof(struct tm));
} else if(now.tm_hour == pltime.tm_hour) {
// same hour, let's check the minutes
if(now.tm_min >= pltime.tm_min) {
D("this playlist is scheduled right now");
snprintf(ThePlaylist,511,"%s",filelist[found]->d_name);
memcpy(&plseltime,&pltime,sizeof(struct tm));
}
} else D("this playlist will be activated later");
}
}
snprintf(path,511,"%s/.ivysync/%s",home,ThePlaylist);
fd = fopen(path,"r");
if(!fd) {
E("can't load playlist %s: %s", path, strerror(errno));
return -1;
}
A("reading from playlist file %s",path);
while( fgets(line,1023,fd) ) {
if( feof(fd) ) break;
chomp(line);
if( append(line) ) {
c++;
D("%u+ %s",c,line);
}
}
fclose(fd);
return c;
}
#ifdef WITH_GUI
int Decoder::save() {
FILE *fd;
char *home = getenv("HOME");
char path[512];
int c;
struct stat st;
vector<string>::iterator pl_iter;
string pl;
// create the configuration directory if doesn't exist
snprintf(path,511,"%s/.ivysync",home);
if( stat(path, &st) != 0) {
if(errno==ENOENT) mkdir(path,0744);
else {
E("error saving in %s: %s",path, strerror(errno));
return -1;
}
}
snprintf(path,511,"%s/.ivysync/video%u",home,device_num);
fd = fopen(path,"w+");
if(!fd) {
E("can't save to %s: %s", path, strerror(errno));
return -1;
}
D("saving to configuration file %s",path);
Entry *ent;
ent = playlist.begin();
c = 0;
while(ent) {
fputs(ent->name, fd);
fputs("\n",fd);
D("%u - %s", c, ent->name);
ent = ent->next;
c++;
}
fflush(fd);
fclose(fd);
return c;
}
#endif