summaryrefslogtreecommitdiff
path: root/mg_image_provider.c
blob: 5766d9ecfb1a5bcc6def944f9264a2ad84045cfd (plain)
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
//
#include "mg_image_provider.h"

#include "mg_item_gd.h"
#include "mg_tools.h"
#include "mg_setup.h"

#include <vdr/plugin.h>

#include <id3v2header.h>
#include <flacfile.h>
#include <vorbisfile.h>
#include <mpegfile.h>
#include <attachedpictureframe.h>

#include <cstring>
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <libgen.h>
#include <fileref.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fts.h>
#include <regex.h>
#include <errno.h>

using namespace std;

extern int alphasort();

const int FALSE = 0;
const int TRUE =  1;

int picture_select( struct dirent const *entry ) {
	if( (strcmp(entry->d_name, ".") == 0) ||
	(strcmp(entry->d_name, "..") == 0) ) {
		return (FALSE);
	}
	else {
		char *ext = rindex( entry->d_name, '.' );

		if( ext ) {
			if( !strcmp( ext, ".jpg" ) || !strcmp( ext, ".png" ) || !strcmp( ext, ".JPG" ) || !strcmp( ext, ".PNG" ) ) {
				return (TRUE);
			}
			else {
				return ( FALSE );
			}
		}
		else {
			return (FALSE);
		}
	}
}

#if 0
mgImage::mgImage() {
	bmp=0;
}

mgImage::~mgImage() {
	delete bmp;bmp=0;
}
#endif

string mgImageProvider::getImagePath( string &source ) {
	// returns converted image name, writes source image name into source argument reference
	string fname="";
	source = "";

	if (m_image_index>=m_image_list.size())
		m_image_index=0;
	if( m_image_index < m_image_list.size() ) {
		source = m_image_list[ m_image_index ];
	}
	m_image_index += 1;

	return fname;
}

mgMpgImageProvider::mgMpgImageProvider(tArea area) 
	: mgImageProvider(area) {
}	


mgImageProvider::mgImageProvider( string dir ) {
	// iterate all files in dir and put them into m_image_list
        currItem=0;
	m_mode = IM_PLAYLIST;
	m_image_index = 0;
	m_delete_imgs_from_tag = false;

	fillImageList( dir );
}

mgMpgImageProvider::mgMpgImageProvider( string dir ) 
	: mgImageProvider(dir) {
	Start();
}

mgImageProvider::mgImageProvider(tArea area ) {
	currItem=0;
	m_mode = IM_ITEM_DIR;
	m_image_index = 0;
	m_delete_imgs_from_tag = false;
	coverarea=area;
}

mgMpgImageProvider::~mgMpgImageProvider() {
	Lock();
	m_converted_images.clear();
	Unlock();
}

bool mgImageProvider::extractImagesFromDatabase() {
	/* GD entry looks like: img0x00004810-00.jpg
	   and may reside in $ToplevelDir/[0-9]{2}/

	   Then, all img0x00004810-??.jpg are possible matches, but not
	   files such as img0x00004810-00-s200.jpg which are optimized
	   for a different resoluition.

	   Thanks to viking (from vdrportal.de) for help with this
	*/
	string file = currItem->getImagePath();
	if( file == "" ) {
		return false;
	}

	// cut -00.jpg part from file
	int pos = file.find('-');
	string file_rex_s = file.substr( 0, pos+1 );
	file_rex_s += "[0-9]*.jpg";
	// now file_rex contains a regular expression we can test for to obtain valid images

	bool result = false;

	char *dir[2];
	dir[0] = the_setup.ToplevelDir;
	dir[1] = NULL;

	FTS *fts = fts_open( dir, FTS_LOGICAL, 0);
	if( fts ) {
		FTSENT *ftsent;
		regex_t path_rex, file_rex;

		if( regcomp( &path_rex, "[0-9][0-9]", REG_NOSUB ) ) {
			mgDebug( 1, "mgImageProvider::extractImagesFromDatabase: Error compiling dir regex. Not using GD images." );
			return false;
		}

		if( regcomp( &file_rex, file_rex_s.c_str(), REG_NOSUB ) ) {
			mgDebug( 1, "mgImageProvider::extractImagesFromDatabase: Error compiling file regex. Not using GD images." );
			return false;
		}

		while( (ftsent = fts_read(fts)) != NULL ) {
			switch( ftsent->fts_info ) {
				case FTS_DC:
				{
					mgDebug( 1, "Image import: Ignoring directory %s, would cycle already seen %s",
						ftsent->fts_path,ftsent->fts_cycle->fts_path );
				} break;
				case FTS_DNR:
				{
					mgDebug( 1, "Ignoring unreadable directory %s: error %d",
						ftsent->fts_path,ftsent->fts_errno);
				} break;
				case FTS_DOT:
				{
					mgDebug( 1, "Ignoring dot file %s:",
						ftsent->fts_path );
				} break;
				case FTS_SLNONE:
				{
					mgDebug(1,"Ignoring broken symbolic link %s",
						ftsent->fts_path);
				} break;
				case FTS_NSOK:	 // should never happen because we do not do FTS_NOSTAT
				case FTS_SL:	 // should never happen because we do FTS_LOGICAL
				case FTS_ERR:
				{
					mgDebug( 1, "Ignoring %s: error %d",
						ftsent->fts_path,ftsent->fts_errno );
				} break;
				case FTS_D:
				{
					if( !regexec( &path_rex, ftsent->fts_name, 0, NULL, 0 ) ) {
						fts_set( fts, ftsent, FTS_SKIP );
						mgDebug( 1, "Skipping directory %s", ftsent->fts_path );
					}
				} break;
				case FTS_DP:
					break;
				case FTS_F:
				{
					if( !ftsent->fts_path ) {
						mgDebug( 1, "internal error: fts_path is 0" );
					}
					else if( access( ftsent->fts_path, R_OK ) ) {
						mgDebug( 1, "Ignoring unreadable file %s: %s",
							ftsent->fts_path, strerror( errno ) );
					}
					else {
						if( regexec( &file_rex, ftsent->fts_name, 0, NULL, 0 ) ) {
								 // an image matching the GD database entry
							string img = string( ftsent->fts_path );
							m_image_list.push_back( img );
							mgDebug( 1, "Found image %s", ftsent->fts_path );
						}
					}
				} break;
				case FTS_NS:
				{
					mgDebug( 1, "Ignoring unstatable file %s: error %d",
						ftsent->fts_path,ftsent->fts_errno );
				} break;
				default:
				{
					mgDebug( 1, "Ignoring %s: unknown fts_info value %d",
						ftsent->fts_path,ftsent->fts_info );
				}
			}
		}
		fts_close(fts);
		regfree( &path_rex );
	}
	return result;
}

void mgImageProvider::updateFromItemDirectory() {
	// no images in tags, find images in the directory of the file itself
	string dir = dirname( (char *) (currItem->getSourceFile().c_str()) );

	// do something, when there are no images here, either:
	// simply go up one step in the directory hierarchy, until we reach top level directory
	bool toplevel_reached = false;
	while( !m_image_list.size() && !toplevel_reached ) {
		if( samedir( dir.c_str(), the_setup.ToplevelDir ) ) {
			toplevel_reached = true;
		}

		fillImageList( dir );

		if( !m_image_list.size() ) {
			// nothing found, turn up one directory level
			dir = dirname( (char *)dir.c_str() );
		}
	}
}

bool mgImageProvider::updateItem( mgItemGd *newitem ) {
	// clean up stuff from previous item ?

	if (newitem==currItem)
		return false;
	currItem=newitem;
	return CollectImages();
}

string
mgImageProvider::getCachedMPGFile(mgItemGd *item,string f) {
	// determine the filename of a (to be) cached .mpg file
	string bname = basename( (char *)f.c_str() );
	unsigned dotpos = bname.rfind('.');
	if (dotpos != string::npos)
		bname.replace(dotpos,9999,".mpg");
	else
		bname = bname + ".mpg";
	string result=item->getCachedFilename("covers/",false) + bname;
	mkdir_p(result.c_str());
	return result;
}

bool mgImageProvider::CollectImages() {
	string filename = currItem->getSourceFile();
	m_image_list.clear();


	if( m_mode == IM_ITEM_DIR ) {
								 // do not try to acquire new images when we are playing back a separate directory
		// check whether the item has cover images defined in the database ?
		bool has_db_images = extractImagesFromDatabase();

		if( !has_db_images ) {
			// no. check whether the item has cover images defined in tags ?
			string dir = extractImagesFromTag( filename );

			if( dir == "" ) {
				// no. use anything from the directory
				updateFromItemDirectory();
			}
			else {
				fillImageList( dir );
			}
		}

		m_image_index = 0;
	}
	bool result = m_image_list.size() > 0;
	return result;
}

bool mgMpgImageProvider::updateItem( mgItemGd *newitem ) {
	// clean up stuff from previous item ?
	if (newitem==currItem)
		return false;
	Lock();
	currItem=newitem;
	m_converted_images.clear();
	m_need_conversion.clear();
	Unlock();
	CollectImages();
	for( unsigned i=0; i < m_image_list.size(); i++ ) {
		FILE *fp;
		string filename = m_image_list[i];

		if( (fp = fopen( filename.c_str(), "rb" )) ) {
			// filename can be opened
			fclose (fp);
			string tmpFile = getCachedMPGFile(currItem,filename);
			struct stat stsource;
			struct stat sttmp;
			if (stat(filename.c_str(),&stsource))
				stsource.st_mtime=0L;
			if (stat(tmpFile.c_str(),&sttmp))
				sttmp.st_mtime=0L;
			Lock();
			if (sttmp.st_mtime==0L || sttmp.st_mtime<stsource.st_mtime)
				m_need_conversion.push_back(filename);
			else if( !access( tmpFile.c_str(), R_OK ))
				m_converted_images.push_back( tmpFile );
			Unlock();
		}
	}
	if( m_mode == IM_ITEM_DIR )
		Start();
	Lock();
	bool result = m_converted_images.size() > 0;
	Unlock();

	return result;
}

void mgMpgImageProvider::Action() {
	// convert all imges
	Lock();
	vector<string> images( m_need_conversion );
	mgItemGd *cnvItem = currItem;
	Unlock();
	for( unsigned i=0; i < images.size(); i++ ) {
		if (i>0)
			// sleep 1 sec: we don't want to start all image conversions simultaneously
			usleep(1000*1000);	
		string cachedFile = getCachedMPGFile(cnvItem,images[i]);
		char *cmd;
		msprintf( &cmd, "%s/scripts/muggle-image-convert \"%s\" \"%s\" %d %d %d %d &",
			the_setup.ConfigDirectory.c_str(),
		images[i].c_str(), cachedFile.c_str(),
		coverarea.x1,coverarea.y1,coverarea.Width(),coverarea.Height() );
		mgDebug(5,"%d %s",system( (const char*) cmd ),cmd);
		free(cmd);
	}
	int waitcount=0;
	while (images.size() && waitcount<20) { // wait up to 20 seconds for all conversions
		for( unsigned i=0; i < images.size(); i++ ) {
			Lock();
			mgItemGd *expectItem = currItem;
			Unlock();
			if (expectItem!=cnvItem) {
				images.clear();
				break;
			}
			string cachedFile = getCachedMPGFile(cnvItem,images[i]);
			// if file can be read by process, add to the list of converted images
			if( !access( cachedFile.c_str(), R_OK ) ) {
				Lock();
				m_converted_images.push_back( cachedFile );
				Unlock();
				images.erase(images.begin()+i);
			}
		}
		// wait 1/2 second
		usleep(1000*500);
		waitcount++;
	}
}

void mgImageProvider::fillImageList( string dir ) {
	// obtain all .png, .jpg in dir and gather them in m_image_list

	struct dirent **files;
	int count = scandir( dir.c_str(), &files, picture_select, alphasort );

	if( count ) {
		for ( int i=0; i < count; i++ ) {
			string fname = dir + "/" + string( files[i]->d_name );
			m_image_list.push_back( fname );
			free( files[i] );
		}
		free( files );
	}
}

void mgImageProvider::writeImage( TagLib::ByteVector &image, int num, string &image_cache ) {
	char* image_data = image.data();
	int len = image.size();

	// save image_data to temporary file
	char *buf;
	msprintf( &buf, "%s/image-%d.jpg", image_cache.c_str(), num );

	FILE *f = fopen( buf, "w+" );
	fwrite( image_data, sizeof(char), len, f );
	fclose( f );
	free( buf );
}

string mgImageProvider::treatFrameList( TagLib::ID3v2::FrameList &l, string &image_cache ) {
	string result;

	if( !l.isEmpty() ) {
		m_delete_imgs_from_tag = true;
		TagLib::ID3v2::FrameList::ConstIterator it = l.begin();

		int num = 0;
		while( !(it == l.end()) ) {
			TagLib::ID3v2::AttachedPictureFrame *picframe = (TagLib::ID3v2::AttachedPictureFrame*) (*it);

			TagLib::ByteVector image = picframe->picture();
			writeImage( image, num, image_cache );

			it++;
			num++;
		}
		result = image_cache;
	}
	else {
		result = "";
	}

	return result;
}

string mgImageProvider::extractImagesFromTag( string f ) {
	TagLib::ID3v2::FrameList l;
	const char *filename = f.c_str();
	string image_cache = string( the_setup.CacheDir );
	string dir = "";

	if( !strcasecmp(extension(filename), "flac") ) {
		TagLib::FLAC::File f(filename);
		if (f.ID3v2Tag()) {
			l = f.ID3v2Tag()->frameListMap()["APIC"];
			dir = treatFrameList( l, image_cache );
		}
	}
	else if( !strcasecmp(extension(filename), "mp3") ) {
		TagLib::MPEG::File f(filename);
		if (f.ID3v2Tag()) {
			l = f.ID3v2Tag()->frameListMap()["APIC"];
			dir = treatFrameList( l, image_cache );
		}
	}
	else if( !strcasecmp(extension(filename), "ogg") ) {
		// what to do here?

		TagLib::Vorbis::File f(filename);
		/*
		if (f.ID3v2Tag())
		  {
			l = f.ID3v2Tag()->frameListMap()["APIC"];
			dir = treatFrameList( l, image_cache );
		  }
		*/
	}

	// returns empty if no images were found in tags
	return dir;
}

string mgMpgImageProvider::getImagePath( string &source ) {
	// returns converted image name, writes source image name into source argument reference

	string fname="";
	source = "";

	// check, how many images are converted at all
	Lock();
	if (m_image_index>=m_converted_images.size())
		m_image_index=0;
	if( m_image_index < m_converted_images.size() ) {
		fname = m_converted_images[ m_image_index ];
		// here we could mangle the original name to add an extension such as -s200
		source = m_image_list[ m_image_index ];
	}
	m_image_index += 1;
	Unlock();

	return fname;
}