summaryrefslogtreecommitdiff
path: root/mg_image_provider.c
blob: 94a723000857422cf8e73d23eb5acc05b1134593 (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

#include "mg_image_provider.h"

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

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

#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);
	}
    }
}

string mgImageProvider::getImagePath( string &source )
{
  string fname="";
  source = "";

  // check, how many images are converted at all
  Lock();
  if( m_image_index < m_converted_images.size() )
    {
      fname = m_converted_images[ m_image_index ];
    }
  source = m_image_list[ m_image_index ];
  
  // wrap to beginning of list when all images are displayed
  m_image_index += 1;
  if( m_image_index >= m_converted_images.size() )
    {
      m_image_index = 0;
    }
  Unlock();

  return fname;
}

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

  fillImageList( dir );
  Start();
}

mgImageProvider::mgImageProvider( )
{
  m_mode = IM_ITEM_DIR;
  m_image_index = 0;
  m_delete_imgs_from_tag = false;
}

mgImageProvider::~mgImageProvider()
{
  deleteTemporaryImages();
}

void mgImageProvider::deleteTemporaryImages()
{
  if( m_delete_imgs_from_tag )
    {
      for( vector<string>::iterator iter = m_image_list.begin(); iter != m_image_list.end(); iter ++ )
	{
	  // remove( (*iter).c_str() );
	  // cout << "Removing " << *iter << endl;
	}
      m_delete_imgs_from_tag = false;
    }
  m_image_list.clear();

  for( vector<string>::iterator iter = m_converted_images.begin(); iter != m_converted_images.end(); iter ++ )
    {
      // remove( (*iter).c_str() );
      // cout << "Removing " << *iter << endl;
    }  
  m_converted_images.clear();
}

mgImageProvider* mgImageProvider::Create( string dir )
{
  return new mgImageProvider( dir );
}

mgImageProvider* mgImageProvider::Create( )
{
  return new mgImageProvider();
}

bool mgImageProvider::extractImagesFromDatabase( mgItemGd *item )
{
  /* 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 = item->getImagePath();
  if( file == "" )
    {
      return false;
    }

  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;
      
      if( regcomp( &path_rex, "[0-9][0-9]", REG_NOSUB ) )
	{
	  mgDebug( 1, "mgImageProvider::extractImagesFromDatabase: Error compiling dir 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
		  {
		    m_image_list.push_back( string( ftsent->fts_path ) );
		    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( mgItemGd *item )
{
  // no images in tags, find images in the directory of the file itself
  string dir = dirname( (char *) (item->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 *item )
{
  // clean up stuff from previous item ?

  string filename = item->getSourceFile();

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

      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( item );
	    }
	  else
	    {
	      fillImageList( dir );
	    }
	}		

      // start a thread to convert all images in 'dir' into .mpg format in the background
      Start();
      
      m_image_index = 0;
    }
  // else: nothing todo when changing the item currently being played

  Lock();
  bool result = m_image_list.size() > 0;
  Unlock();

  return result;
}

void mgImageProvider::Action()
{
  // convert all images
  Lock();
  vector<string> images( m_image_list );
  Unlock();

  for( unsigned i=0; i < images.size(); i++ )
    {
      FILE *fp;
      string filename = images[i];
      
      if( (fp = fopen( filename.c_str(), "rb" )) )
	{
	  // filename can be opened
	  fclose (fp);
	  
	  // determine the filename of a (to be) cached .mpg file
	  string bname = basename( (char *)filename.c_str() );
	  unsigned dotpos = bname.rfind( ".", filename.length() );
	  
	  // assemble path from relative paths
	  string tmpFile = string( the_setup.ImageCacheDir ) + string( "/" ) + bname.substr( 0, dotpos ) + string( ".mpg" );


	  char *tmp;
	  asprintf( &tmp, "/usr/local/bin/image_convert.sh \"%s\" \"%s\"", filename.c_str(), tmpFile.c_str() );
	  system( (const char*) tmp );
	  free(tmp);
	 
	  // if file can be read by process, add to the list of converted images
	  if( !access( tmpFile.c_str(), R_OK ) )
	    {
	      Lock();
	      m_converted_images.push_back( tmpFile );
	      Unlock();
	    }
	}

      // Check whether we need to continue this!? Next song may be playing already...
    }
  // cout << "Image conversion thread ending." << endl << flush;
}

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;
  asprintf( &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.ImageCacheDir );
  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;
}