diff options
-rw-r--r-- | libhb/hb.c | 19 | ||||
-rw-r--r-- | libhb/hb.h | 4 | ||||
-rw-r--r-- | libhb/ports.c | 86 | ||||
-rw-r--r-- | libhb/ports.h | 8 | ||||
-rw-r--r-- | macosx/Controller.m | 18 | ||||
-rw-r--r-- | macosx/HBPreviewController.m | 6 | ||||
-rw-r--r-- | test/test.c | 10 |
7 files changed, 95 insertions, 56 deletions
diff --git a/libhb/hb.c b/libhb/hb.c index 56df9a9be..a7df7a220 100644 --- a/libhb/hb.c +++ b/libhb/hb.c @@ -64,9 +64,8 @@ struct hb_handle_s on multi-pass encodes where frames get dropped. */ hb_interjob_t * interjob; - // Power Management opaque pointer - // For OSX, it's an IOPMAssertionID* - void * hb_system_sleep_opaque; + // power management opaque pointer + void *system_sleep_opaque; } ; hb_work_object_t * hb_objects = NULL; @@ -411,7 +410,7 @@ hb_handle_t * hb_init( int verbose, int update_check ) h->build = -1; /* Initialize opaque for PowerManagement purposes */ - h->hb_system_sleep_opaque = hb_system_sleep_opaque_init(); + h->system_sleep_opaque = hb_system_sleep_opaque_init(); if( update_check ) { @@ -518,7 +517,7 @@ hb_handle_t * hb_init_dl( int verbose, int update_check ) h->build = -1; /* Initialize opaque for PowerManagement purposes */ - h->hb_system_sleep_opaque = hb_system_sleep_opaque_init(); + h->system_sleep_opaque = hb_system_sleep_opaque_init(); if( update_check ) { @@ -1679,6 +1678,8 @@ void hb_close( hb_handle_t ** _h ) hb_lock_close( &h->state_lock ); hb_lock_close( &h->pause_lock ); + hb_system_sleep_opaque_close(&h->system_sleep_opaque); + free( h->interjob ); free( h ); @@ -1886,14 +1887,14 @@ void hb_set_state( hb_handle_t * h, hb_state_t * s ) hb_unlock( h->pause_lock ); } -void hb_prevent_sleep( hb_handle_t * h ) +void hb_system_sleep_allow(hb_handle_t *h) { - hb_system_sleep_prevent( h->hb_system_sleep_opaque ); + hb_system_sleep_private_enable(h->system_sleep_opaque); } -void hb_allow_sleep( hb_handle_t * h ) +void hb_system_sleep_prevent(hb_handle_t *h) { - hb_system_sleep_allow( h->hb_system_sleep_opaque ); + hb_system_sleep_private_disable(h->system_sleep_opaque); } /* Passes a pointer to persistent data */ diff --git a/libhb/hb.h b/libhb/hb.h index 07100c946..e4c81a4e4 100644 --- a/libhb/hb.h +++ b/libhb/hb.h @@ -95,8 +95,8 @@ void hb_pause( hb_handle_t * ); void hb_resume( hb_handle_t * ); void hb_stop( hb_handle_t * ); -void hb_prevent_sleep( hb_handle_t * ); -void hb_allow_sleep( hb_handle_t * ); +void hb_system_sleep_allow(hb_handle_t*); +void hb_system_sleep_prevent(hb_handle_t*); /* Persistent data between jobs. */ typedef struct hb_interjob_s diff --git a/libhb/ports.c b/libhb/ports.c index 7097f2ee9..98945fec0 100644 --- a/libhb/ports.c +++ b/libhb/ports.c @@ -792,63 +792,101 @@ char *strtok_r(char *s, const char *delim, char **save_ptr) #ifdef __APPLE__ // 128 chars limit for IOPMAssertionCreateWithName -static CFStringRef reasonForActivity= CFSTR("HandBrake is currently scanning and/or encoding"); +static CFStringRef reasonForActivity = + CFSTR("HandBrake is currently scanning and/or encoding"); #endif void* hb_system_sleep_opaque_init() { - void* opaque; + void *opaque = NULL; #ifdef __APPLE__ - opaque = calloc( sizeof( IOPMAssertionID ), 1); - IOPMAssertionID * assertionID = (IOPMAssertionID *)opaque; + opaque = calloc(sizeof(IOPMAssertionID), 1); + if (opaque == NULL) + { + hb_error("hb_system_sleep: failed to allocate opaque"); + return NULL; + } + + IOPMAssertionID *assertionID = (IOPMAssertionID*)opaque; *assertionID = -1; #endif - return opaque; } -void hb_system_sleep_opaque_close(void **_opaque) +void hb_system_sleep_opaque_close(void **opaque) { + if (*opaque != NULL) + { + hb_system_sleep_private_enable(*opaque); + } #ifdef __APPLE__ - IOPMAssertionID * assertionID = (IOPMAssertionID *) *_opaque; - free( assertionID ); + if (*opaque != NULL) + { + IOPMAssertionID *assertionID = (IOPMAssertionID*)*opaque; + free(assertionID); + } #endif - *_opaque = NULL; + *opaque = NULL; } -void hb_system_sleep_allow(void *opaque) +void hb_system_sleep_private_enable(void *opaque) { #ifdef __APPLE__ - IOPMAssertionID * assertionID = (IOPMAssertionID *)opaque; + if (opaque == NULL) + { + hb_error("hb_system_sleep: opaque is NULL"); + } + IOPMAssertionID *assertionID = (IOPMAssertionID*)opaque; if (*assertionID == -1) + { + // nothing to do return; + } IOReturn success = IOPMAssertionRelease(*assertionID); - - if (success == kIOReturnSuccess) { - hb_deep_log( 3, "osxsleep: IOPM assertion %d successfully released, sleep allowed", *assertionID ); + if (success == kIOReturnSuccess) + { + hb_deep_log(3, + "hb_system_sleep: assertion %d released, sleep allowed", + *assertionID); *assertionID = -1; - } else { - hb_log( "osxsleep: error while trying to unset power management assertion" ); + } + else + { + hb_log("hb_system_sleep: failed to allow system sleep"); } #endif } -void hb_system_sleep_prevent(void *opaque) +void hb_system_sleep_private_disable(void *opaque) { #ifdef __APPLE__ - IOPMAssertionID * assertionID = (IOPMAssertionID *)opaque; - + if (opaque == NULL) + { + hb_error("hb_system_sleep: opaque is NULL"); + } + + IOPMAssertionID *assertionID = (IOPMAssertionID*)opaque; if (*assertionID != -1) + { + // nothing to do return; + } IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, - kIOPMAssertionLevelOn, reasonForActivity, assertionID); - if (success == kIOReturnSuccess) { - hb_deep_log( 3, "IOPM assertion %d successfully created, prevent sleep", *assertionID); - } else { - hb_log( "osxsleep: error while trying to set power management assertion" ); + kIOPMAssertionLevelOn, + reasonForActivity, + assertionID); + if (success == kIOReturnSuccess) + { + hb_deep_log(3, + "hb_system_sleep: assertion %d created, sleep prevented", + *assertionID); + } + else + { + hb_log("hb_system_sleep: failed to prevent system sleep"); } #endif } diff --git a/libhb/ports.h b/libhb/ports.h index 162781347..2c0aeb1ee 100644 --- a/libhb/ports.h +++ b/libhb/ports.h @@ -105,10 +105,10 @@ void hb_net_close( hb_net_t ** ); /************************************************************************ * OS Sleep Allow / Prevent ***********************************************************************/ -void * hb_system_sleep_opaque_init(); -void hb_system_sleep_opaque_close( void ** opaque ); -void hb_system_sleep_allow( void * opaque ); -void hb_system_sleep_prevent( void * opaque ); +void* hb_system_sleep_opaque_init(); +void hb_system_sleep_opaque_close(void **opaque); +void hb_system_sleep_private_enable(void *opaque); +void hb_system_sleep_private_disable(void *opaque); #endif /* __LIBHB__ */ diff --git a/macosx/Controller.m b/macosx/Controller.m index c8a064fb2..8635ea14f 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -2027,7 +2027,7 @@ static NSString * ChooseSourceIdentifier = @"Choose Source It [self writeToActivityLog: "scanning titles with a duration of %d seconds or more", min_title_duration_seconds]; } - hb_prevent_sleep(fHandle); + hb_system_sleep_prevent(fHandle); hb_scan(fHandle, [path UTF8String], scanTitleNum, hb_num_previews, 1 , min_title_duration_ticks); @@ -2042,7 +2042,7 @@ static NSString * ChooseSourceIdentifier = @"Choose Source It - (IBAction) cancelScanning:(id)sender { hb_scan_stop(fHandle); - hb_allow_sleep(fHandle); + hb_system_sleep_allow(fHandle); } - (IBAction) showNewScan:(id)sender @@ -2208,7 +2208,7 @@ static NSString * ChooseSourceIdentifier = @"Choose Source It } /* Done scanning, allow system sleep for the scan handle */ - hb_allow_sleep(fHandle); + hb_system_sleep_allow(fHandle); } @@ -2821,7 +2821,7 @@ fWorkingCount = 0; { [self writeToActivityLog: "incrementQueueItemDone there are no more pending encodes"]; /* Done encoding, allow system sleep for the encode handle */ - hb_allow_sleep(fQueueEncodeLibhb); + hb_system_sleep_allow(fQueueEncodeLibhb); /* * Since there are no more items to encode, go to queueCompletedAlerts * for user specified alerts after queue completed @@ -2869,7 +2869,7 @@ fWorkingCount = 0; * only useful for autocrop and static previews, which are already taken * care of at this point */ - hb_prevent_sleep(fQueueEncodeLibhb); + hb_system_sleep_prevent(fQueueEncodeLibhb); hb_scan(fQueueEncodeLibhb, [path UTF8String], scanTitleNum, 10, 0, 0); } } @@ -4541,7 +4541,7 @@ bool one_burned = FALSE; hb_stop(fQueueEncodeLibhb); - hb_allow_sleep(fQueueEncodeLibhb); + hb_system_sleep_allow(fQueueEncodeLibhb); // Delete all remaining jobs since libhb doesn't do this on its own. hb_job_t * job; @@ -4583,7 +4583,7 @@ bool one_burned = FALSE; - (void) doCancelCurrentJobAndStop { hb_stop(fQueueEncodeLibhb); - hb_allow_sleep(fQueueEncodeLibhb); + hb_system_sleep_allow(fQueueEncodeLibhb); // Delete all remaining jobs since libhb doesn't do this on its own. hb_job_t * job; @@ -4609,13 +4609,13 @@ bool one_burned = FALSE; if (s.state == HB_STATE_PAUSED) { - hb_prevent_sleep(fQueueEncodeLibhb); + hb_system_sleep_prevent(fQueueEncodeLibhb); hb_resume(fQueueEncodeLibhb); } else { hb_pause(fQueueEncodeLibhb); - hb_allow_sleep(fQueueEncodeLibhb); + hb_system_sleep_allow(fQueueEncodeLibhb); } } diff --git a/macosx/HBPreviewController.m b/macosx/HBPreviewController.m index 4ef2824e3..5214cf0ea 100644 --- a/macosx/HBPreviewController.m +++ b/macosx/HBPreviewController.m @@ -687,7 +687,7 @@ { fEncodeState = 2; hb_stop(fPreviewLibhb); - hb_allow_sleep(fPreviewLibhb); + hb_system_sleep_allow(fPreviewLibhb); [NSAnimationContext beginGrouping]; [[NSAnimationContext currentContext] setDuration:0.2]; [[fEncodingControlBox animator] setHidden:YES]; @@ -805,7 +805,7 @@ /* Let fPreviewLibhb do the job */ fEncodeState = 1; - hb_prevent_sleep(fPreviewLibhb); + hb_system_sleep_prevent(fPreviewLibhb); hb_start(fPreviewLibhb); } @@ -900,7 +900,7 @@ fEncodeState = 0; /* Done encoding, allow system sleep for the preview handle */ - hb_allow_sleep(fPreviewLibhb); + hb_system_sleep_allow(fPreviewLibhb); break; } } diff --git a/test/test.c b/test/test.c index fedff6c3d..8247f5699 100644 --- a/test/test.c +++ b/test/test.c @@ -254,7 +254,7 @@ int main( int argc, char ** argv ) titleindex = 0; } - hb_prevent_sleep(h); + hb_system_sleep_prevent(h); hb_scan(h, input, titleindex, preview_count, store_previews, min_title_duration * 90000LL); @@ -274,10 +274,10 @@ int main( int argc, char ** argv ) fprintf(stdout, "\nEncoding Paused by user command, 'r' to resume\n"); hb_pause(h); - hb_allow_sleep(h); + hb_system_sleep_allow(h); break; case 'r': - hb_prevent_sleep(h); + hb_system_sleep_prevent(h); hb_resume(h); break; case 'h': @@ -325,10 +325,10 @@ int main( int argc, char ** argv ) fprintf(stdout, "\nEncoding Paused by user command, 'r' to resume\n"); hb_pause(h); - hb_allow_sleep(h); + hb_system_sleep_allow(h); break; case 'r': - hb_prevent_sleep(h); + hb_system_sleep_prevent(h); hb_resume(h); break; case 'h': |