Moves the filters' logging info to work.c, adds parameter info. I also changed the...
[HandBrake.git] / macosx / HBCore.m
blob358e11c9eef936d4a8c625018d1eef6ee4c8a568
1 /**
2  * @file
3  * Implementation of class HBCore.
4  */
6 #import "HBCore.h"
7 #include "hb.h"
9 // These constants specify the current state of HBCore.
11 const NSString *HBStateIdle = @"HBStateIdle";           ///< HB is doing nothing (HB_STATE_IDLE)
12 const NSString *HBStateScanning = @"HBStateScanning";   ///< HB is scanning (HB_STATE_SCANNING)
13 const NSString *HBStateScanDone = @"HBStateScanDone";   ///< Scanning has been completed (HB_STATE_SCANDONE)
14 const NSString *HBStateWorking = @"HBStateWorking";     ///< HB is encoding (HB_STATE_WORKING)
15 const NSString *HBStatePaused = @"HBStatePaused";       ///< Encoding is paused (HB_STATE_PAUSED)
16 const NSString *HBStateWorkDone = @"HBStateWorkDone";   ///< Encoding has been completed (HB_STATE_WORKDONE)
17 const NSString *HBStateMuxing = @"HBStateMuxing";       ///< HB is muxing (HB_STATE_MUXING)
20 // These constants specify various status notifications sent by HBCore
22 /// Notification sent to update status while scanning. Matches HB_STATE_SCANNING constant in libhb.
23 NSString *HBCoreScanningNotification = @"HBCoreScanningNotification";
25 /// Notification sent after scanning is complete. Matches HB_STATE_SCANDONE constant in libhb.
26 NSString *HBCoreScanDoneNotification = @"HBCoreScanDoneNotification";
28 /// Notification sent to update status while encoding. Matches HB_STATE_WORKING constant in libhb.
29 NSString *HBCoreWorkingNotification = @"HBCoreWorkingNotification";
31 /// Notification sent when encoding is paused. Matches HB_STATE_PAUSED constant in libhb.
32 NSString *HBCorePausedNotification = @"HBCorePausedNotification";
34 /// Notification sent after encoding is complete. Matches HB_STATE_WORKDONE constant in libhb.
35 NSString *HBCoreWorkDoneNotification = @"HBCoreWorkDoneNotification";
37 /// Notification sent to update status while muxing. Matches HB_STATE_MUXING constant in libhb.
38 NSString *HBCoreMuxingNotification = @"HBCoreMuxingNotification";
40 /**
41  * Private methods of HBCore.
42  */
43 @interface HBCore (Private)
44 - (NSString *)stateAsString:(int)stateValue;
45 @end
47 @implementation HBCore
49 /**
50  * Initializes HBCore.
51  */
52 - (id)init
54     if (self = [super init])
55     {
56         state = HBStateIdle;    
57         hb_state = malloc(sizeof(struct hb_state_s));   
58     }
59     return self;
62 /**
63  * Releases resources.
64  */
65 - (void)dealloc
67     free(hb_state);    
68     [super dealloc];
71 /**
72  * Opens low level HandBrake library. This should be called once before other
73  * functions HBCore are used.
74  *
75  * @param debugMode         If set to YES, libhb will print verbose debug output.
76  * @param checkForUpdates   If set to YES, libhb checks for updated versions.
77  *
78  * @return YES if libhb was opened, NO if there was an error.
79  */
80 - (BOOL)openInDebugMode:(BOOL)debugMode checkForUpdates:(BOOL)checkForUpdates;
82     NSAssert(!hb_handle, @"[HBCore openInDebugMode:checkForUpdates:] libhb is already open");
83     if (hb_handle)
84         return NO;
86     state = HBStateIdle;    
88     hb_handle = hb_init(debugMode ? HB_DEBUG_ALL : HB_DEBUG_NONE, checkForUpdates);
89     if (!hb_handle)
90         return NO;
92     updateTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2
93                                                     target:self
94                                                   selector:@selector(stateUpdateTimer:) 
95                                                   userInfo:NULL 
96                                                    repeats:YES] retain];
98     [[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSModalPanelRunLoopMode];        
99     return YES;
103  * Closes low level HandBrake library and releases resources.
105  * @return YES if libhb was closed successfully, NO if there was an error.
106  */
107 - (BOOL)close
109     NSAssert(hb_handle, @"[HBCore close] libhb is not open");
110     if (!hb_handle)
111         return NO;
112         
113     [updateTimer invalidate];
114     [updateTimer release];
115     updateTimer = nil;
116     hb_close(&hb_handle);
117     hb_handle = NULL;
118     return YES;
122  * Returns libhb handle used by this HBCore instance.
123  */ 
124 - (struct hb_handle_s *)hb_handle
126     return hb_handle;
130  * Returns current state of HBCore.
132  * @return One of the HBState* string constants.
133  */
134 - (const NSString *)state
136     return state;
140  * Returns latest hb_state_s information struct returned by libhb.
142  * @return Pointer to a hb_state_s struct containing state information of libhb.
143  */
144 - (const struct hb_state_s *)hb_state
146     return hb_state;
149 @end 
151 @implementation HBCore (Private)
154  * Transforms a libhb state constant to a matching HBCore state constant.
155  */
156 - (const NSString *)stateAsString:(int)stateValue
158     switch (stateValue)
159     {
160         case HB_STATE_IDLE:
161             return HBStateIdle;        
162         case HB_STATE_SCANNING:
163             return HBStateScanning;
164         case HB_STATE_SCANDONE:
165             return HBStateScanDone;
166         case HB_STATE_WORKING:
167             return HBStateWorking;
168         case HB_STATE_PAUSED:
169             return HBStatePaused;
170         case HB_STATE_WORKDONE:
171             return HBStateWorkDone;
172         case HB_STATE_MUXING:
173             return HBStateMuxing;        
174         default:
175             NSAssert1(NO, @"[HBCore stateAsString:] unknown state %d", stateValue);
176             return nil;
177     }
181  * This method polls libhb continuously for state changes and processes them.
182  * Additional processing for each state is performed in methods that start
183  * with 'handle' (e.g. handleHBStateScanning).
184  */
185 - (void)stateUpdateTimer:(NSTimer *)timer
187     if (!hb_handle)
188     {
189         // Libhb is not open so we cannot do anything.
190         return;
191     }
193     hb_get_state(hb_handle, hb_state);
195     if (hb_state->state == HB_STATE_IDLE)
196     {
197         // Libhb reported HB_STATE_IDLE, so nothing interesting has happened.
198         return;
199     }
200         
201     // Update HBCore state to reflect the current state of libhb
202     NSString *newState = [self stateAsString:hb_state->state];
203     if (newState != state)
204     {
205         [self willChangeValueForKey:@"state"];
206         state = newState;
207         [self didChangeValueForKey:@"state"];
208     }
210     // Determine name of the method that does further processing for this state
211     // and call it. 
212     SEL sel = NSSelectorFromString([NSString stringWithFormat:@"handle%@", state]);
213     if ([self respondsToSelector:sel])
214         [self performSelector:sel];
218  * Processes HBStateScanning state information. Current implementation just
219  * sends HBCoreScanningNotification.
220  */
221 - (void)handleHBStateScanning
223     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreScanningNotification object:self];    
227  * Processes HBStateScanDone state information. Current implementation just
228  * sends HBCoreScanDoneNotification.
229  */
230 - (void)handleHBStateScanDone
232     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreScanDoneNotification object:self];    
236  * Processes HBStateWorking state information. Current implementation just
237  * sends HBCoreWorkingNotification.
238  */
239 - (void)handleHBStateWorking
241     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreWorkingNotification object:self];    
245  * Processes HBStatePaused state information. Current implementation just
246  * sends HBCorePausedNotification.
247  */
248 - (void)handleHBStatePaused
250     [[NSNotificationCenter defaultCenter] postNotificationName:HBCorePausedNotification object:self];    
254  * Processes HBStateWorkDone state information. Current implementation just
255  * sends HBCoreWorkDoneNotification.
256  */
257 - (void)handleHBStateWorkDone
259     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreWorkDoneNotification object:self];    
263  * Processes HBStateMuxing state information. Current implementation just
264  * sends HBCoreMuxingNotification.
265  */
266 - (void)handleHBStateMuxing
268     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreMuxingNotification object:self];    
271 @end