diff options
-rw-r--r-- | gtk/src/hb-backend.c | 7 | ||||
-rw-r--r-- | gtk/src/preview.c | 2 | ||||
-rw-r--r-- | libhb/hb.c | 102 | ||||
-rw-r--r-- | libhb/hb.h | 8 | ||||
-rw-r--r-- | libhb/ports.c | 9 | ||||
-rw-r--r-- | libhb/ports.h | 4 | ||||
-rw-r--r-- | libhb/scan.c | 4 | ||||
-rw-r--r-- | test/test.c | 2 |
8 files changed, 91 insertions, 47 deletions
diff --git a/gtk/src/hb-backend.c b/gtk/src/hb-backend.c index 49962bd21..53b71fd2d 100644 --- a/gtk/src/hb-backend.c +++ b/gtk/src/hb-backend.c @@ -1095,14 +1095,14 @@ get_amix_value(gint val) static hb_handle_t * h_scan = NULL; static hb_handle_t * h_queue = NULL; -extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]); +extern void hb_get_temporary_directory(char path[512]); gchar* ghb_get_tmp_dir() { char dir[512]; - hb_get_tempory_directory(h_scan, dir); + hb_get_temporary_directory(dir); return g_strdup(dir); } @@ -1111,7 +1111,7 @@ ghb_hb_cleanup(gboolean partial) { char dir[512]; - hb_get_tempory_directory(h_scan, dir); + hb_get_temporary_directory(dir); del_tree(dir, !partial); } @@ -2948,6 +2948,7 @@ ghb_backend_close() { hb_close(&h_queue); hb_close(&h_scan); + hb_global_close(); } void ghb_backend_scan_stop() diff --git a/gtk/src/preview.c b/gtk/src/preview.c index 2171f8ac8..4ed717bb1 100644 --- a/gtk/src/preview.c +++ b/gtk/src/preview.c @@ -533,8 +533,6 @@ ghb_live_reset(signal_user_data_t *ud) ghb_set_preview_image(ud); } -extern void hb_get_tempory_directory(hb_handle_t *h, char path[512]); - G_MODULE_EXPORT void live_preview_start_cb(GtkWidget *xwidget, signal_user_data_t *ud) { diff --git a/libhb/hb.c b/libhb/hb.c index 0541b9fa7..6f1e1ff37 100644 --- a/libhb/hb.c +++ b/libhb/hb.c @@ -3,6 +3,8 @@ struct hb_handle_s { + int id; + /* The "Check for update" thread */ int build; char version[32]; @@ -49,6 +51,7 @@ struct hb_handle_s hb_lock_t *hb_avcodec_lock; hb_work_object_t * hb_objects = NULL; +int hb_instance_counter = 0; int hb_process_initialized = 0; static void thread_func( void * ); @@ -95,16 +98,16 @@ void hb_register( hb_work_object_t * w ) */ hb_handle_t * hb_init( int verbose, int update_check ) { - if (!hb_process_initialized) - { + if (!hb_process_initialized) + { #ifdef USE_PTHREAD #if defined( _WIN32 ) || defined( __MINGW32__ ) - pthread_win32_process_attach_np(); + pthread_win32_process_attach_np(); #endif #endif - hb_process_initialized =1; - } - + hb_process_initialized =1; + } + hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 ); uint64_t date; @@ -112,7 +115,9 @@ hb_handle_t * hb_init( int verbose, int update_check ) global_verbosity_level = verbose; if( verbose ) putenv( "HB_DEBUG=1" ); - + + h->id = hb_instance_counter++; + /* Check for an update on the website if asked to */ h->build = -1; @@ -194,7 +199,7 @@ hb_handle_t * hb_init( int verbose, int update_check ) #ifdef __APPLE__ hb_register( &hb_encca_aac ); #endif - + return h; } @@ -216,6 +221,8 @@ hb_handle_t * hb_init_dl( int verbose, int update_check ) putenv( "HB_DEBUG=1" ); } + h->id = hb_instance_counter++; + /* Check for an update on the website if asked to */ h->build = -1; @@ -356,7 +363,7 @@ void hb_remove_previews( hb_handle_t * h ) struct dirent * entry; memset( dirname, 0, 1024 ); - hb_get_tempory_directory( h, dirname ); + hb_get_temporary_directory( dirname ); dir = opendir( dirname ); if (dir == NULL) return; @@ -370,7 +377,7 @@ void hb_remove_previews( hb_handle_t * h ) for( i = 0; i < count; i++ ) { title = hb_list_item( h->list_title, i ); - len = snprintf( filename, 1024, "%" PRIxPTR, (intptr_t) title ); + len = snprintf( filename, 1024, "%d_%d", h->id, title->index ); if (strncmp(entry->d_name, filename, len) == 0) { snprintf( filename, 1024, "%s/%s", dirname, entry->d_name ); @@ -461,8 +468,8 @@ void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture, memset( filename, 0, 1024 ); - hb_get_tempory_filename( h, filename, "%" PRIxPTR "%d", - (intptr_t) title, picture ); + hb_get_tempory_filename( h, filename, "%d_%d_%d", + h->id, title->index, picture ); file = fopen( filename, "rb" ); if( !file ) @@ -1314,6 +1321,7 @@ void hb_close( hb_handle_t ** _h ) hb_title_t * title; h->die = 1; + hb_thread_close( &h->main_thread ); while( ( title = hb_list_item( h->list_title, 0 ) ) ) @@ -1331,9 +1339,41 @@ void hb_close( hb_handle_t ** _h ) hb_list_close( &h->jobs ); hb_lock_close( &h->state_lock ); hb_lock_close( &h->pause_lock ); + free( h ); *_h = NULL; +} + +/** + * Cleans up libhb at a process level. Call before the app closes. Removes preview directory. + */ +void hb_global_close() +{ + char dirname[1024]; + DIR * dir; + struct dirent * entry; + + /* Find and remove temp folder */ + memset( dirname, 0, 1024 ); + hb_get_temporary_directory( dirname ); + dir = opendir( dirname ); + if (dir) + { + while( ( entry = readdir( dir ) ) ) + { + char filename[1024]; + if( entry->d_name[0] == '.' ) + { + continue; + } + memset( filename, 0, 1024 ); + snprintf( filename, 1023, "%s/%s", dirname, entry->d_name ); + unlink( filename ); + } + closedir( dir ); + rmdir( dirname ); + } } /** @@ -1346,14 +1386,12 @@ static void thread_func( void * _h ) { hb_handle_t * h = (hb_handle_t *) _h; char dirname[1024]; - DIR * dir; - struct dirent * entry; h->pid = getpid(); /* Create folder for temporary files */ memset( dirname, 0, 1024 ); - hb_get_tempory_directory( h, dirname ); + hb_get_temporary_directory( dirname ); hb_mkdir( dirname ); @@ -1420,30 +1458,17 @@ static void thread_func( void * _h ) hb_snooze( 50 ); } + if( h->scan_thread ) + { + hb_scan_stop( h ); + hb_thread_close( &h->scan_thread ); + } if( h->work_thread ) { hb_stop( h ); hb_thread_close( &h->work_thread ); } - - /* Remove temp folder */ - dir = opendir( dirname ); - if (dir) - { - while( ( entry = readdir( dir ) ) ) - { - char filename[1024]; - if( entry->d_name[0] == '.' ) - { - continue; - } - memset( filename, 0, 1024 ); - snprintf( filename, 1023, "%s/%s", dirname, entry->d_name ); - unlink( filename ); - } - closedir( dir ); - rmdir( dirname ); - } + hb_remove_previews( h ); } /** @@ -1456,6 +1481,15 @@ int hb_get_pid( hb_handle_t * h ) } /** + * Returns the id for the given instance. + * @param h Handle to hb_handle_t + */ +int hb_get_instance_id( hb_handle_t * h ) +{ + return h->id; +} + +/** * Sets the current state. * @param h Handle to hb_handle_t * @param s Handle to new hb_state_t diff --git a/libhb/hb.h b/libhb/hb.h index 04d87b2be..036546b1d 100644 --- a/libhb/hb.h +++ b/libhb/hb.h @@ -99,6 +99,14 @@ int hb_get_scancount( hb_handle_t * ); Aborts all current jobs if any, frees memory. */ void hb_close( hb_handle_t ** ); +/* hb_global_close() + Performs final cleanup for the process. */ +void hb_global_close(); + +/* hb_get_instance_id() + Return the unique instance id of an libhb instance created by hb_init. */ +int hb_get_instance_id( hb_handle_t * h ); + #ifdef __cplusplus } #endif diff --git a/libhb/ports.c b/libhb/ports.c index 85e009a36..b29368e07 100644 --- a/libhb/ports.c +++ b/libhb/ports.c @@ -65,6 +65,7 @@ #endif #include <stddef.h> +#include <unistd.h> #include "hb.h" @@ -225,9 +226,9 @@ int hb_get_cpu_count() } /************************************************************************ - * Get a tempory directory for HB + * Get a temporary directory for HB ***********************************************************************/ -void hb_get_tempory_directory( hb_handle_t * h, char path[512] ) +void hb_get_temporary_directory( char path[512] ) { char base[512]; @@ -251,7 +252,7 @@ void hb_get_tempory_directory( hb_handle_t * h, char path[512] ) if( base[strlen(base)-1] == '/' ) base[strlen(base)-1] = '\0'; - snprintf( path, 512, "%s/hb.%d", base, hb_get_pid( h ) ); + snprintf( path, 512, "%s/hb.%d", base, getpid() ); } /************************************************************************ @@ -262,7 +263,7 @@ void hb_get_tempory_filename( hb_handle_t * h, char name[1024], { va_list args; - hb_get_tempory_directory( h, name ); + hb_get_temporary_directory( name ); strcat( name, "/" ); va_start( args, fmt ); diff --git a/libhb/ports.h b/libhb/ports.h index bc671b960..6f451b42b 100644 --- a/libhb/ports.h +++ b/libhb/ports.h @@ -30,9 +30,9 @@ int hb_get_cpu_count(); int hb_dvd_region(char *device, int *region_mask); /************************************************************************ - * Files utils + * File utils ***********************************************************************/ -void hb_get_tempory_directory( hb_handle_t * h, char path[512] ); +void hb_get_temporary_directory( char path[512] ); void hb_get_tempory_filename( hb_handle_t *, char name[1024], char * fmt, ... ); void hb_mkdir( char * name ); diff --git a/libhb/scan.c b/libhb/scan.c index 66afb2a21..e14e6712b 100644 --- a/libhb/scan.c +++ b/libhb/scan.c @@ -609,8 +609,8 @@ static int DecodePreviews( hb_scan_t * data, hb_title_t * title ) if( data->store_previews ) { - hb_get_tempory_filename( data->h, filename, "%" PRIxPTR "%d", - (intptr_t)title, i ); + hb_get_tempory_filename( data->h, filename, "%d_%d_%d", + hb_get_instance_id(data->h), title->index, i ); file_preview = fopen( filename, "wb" ); if( file_preview ) diff --git a/test/test.c b/test/test.c index bb2730182..015e13da4 100644 --- a/test/test.c +++ b/test/test.c @@ -213,6 +213,7 @@ int main( int argc, char ** argv ) "date.\n" ); } hb_close( &h ); + hb_global_close(); return 0; } @@ -324,6 +325,7 @@ int main( int argc, char ** argv ) /* Clean up */ hb_close( &h ); + hb_global_close(); if( input ) free( input ); if( output ) free( output ); if( format ) free( format ); |