OSDN Git Service

import 0.9.3
[handbrake-jp/handbrake-jp.git] / macosx / InstantHandBrake / HBCore.m
1 /**
2  * @file
3  * Implementation of class HBCore.
4  */
5
6 #import "HBCore.h"
7 #include "hb.h"
8
9 // These constants specify the current state of HBCore.
10
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)
18
19
20 // These constants specify various status notifications sent by HBCore
21
22 /// Notification sent to update status while scanning. Matches HB_STATE_SCANNING constant in libhb.
23 NSString *HBCoreScanningNotification = @"HBCoreScanningNotification";
24
25 /// Notification sent after scanning is complete. Matches HB_STATE_SCANDONE constant in libhb.
26 NSString *HBCoreScanDoneNotification = @"HBCoreScanDoneNotification";
27
28 /// Notification sent to update status while encoding. Matches HB_STATE_WORKING constant in libhb.
29 NSString *HBCoreWorkingNotification = @"HBCoreWorkingNotification";
30
31 /// Notification sent when encoding is paused. Matches HB_STATE_PAUSED constant in libhb.
32 NSString *HBCorePausedNotification = @"HBCorePausedNotification";
33
34 /// Notification sent after encoding is complete. Matches HB_STATE_WORKDONE constant in libhb.
35 NSString *HBCoreWorkDoneNotification = @"HBCoreWorkDoneNotification";
36
37 /// Notification sent to update status while muxing. Matches HB_STATE_MUXING constant in libhb.
38 NSString *HBCoreMuxingNotification = @"HBCoreMuxingNotification";
39
40 /**
41  * Private methods of HBCore.
42  */
43 @interface HBCore (Private)
44 - (NSString *)stateAsString:(int)stateValue;
45 @end
46
47 @implementation HBCore
48
49 /**
50  * Initializes HBCore.
51  */
52 - (id)init
53 {
54     if (self = [super init])
55     {
56         state = HBStateIdle;    
57         hb_state = malloc(sizeof(struct hb_state_s));   
58     }
59     return self;
60 }
61
62 /**
63  * Releases resources.
64  */
65 - (void)dealloc
66 {
67     free(hb_state);    
68     [super dealloc];
69 }
70
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;
81 {
82     NSAssert(!hb_handle, @"[HBCore openInDebugMode:checkForUpdates:] libhb is already open");
83     if (hb_handle)
84         return NO;
85
86     state = HBStateIdle;    
87
88     hb_handle = hb_init(debugMode ? HB_DEBUG_ALL : HB_DEBUG_NONE, checkForUpdates);
89     if (!hb_handle)
90         return NO;
91
92     updateTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
93                                                     target:self
94                                                   selector:@selector(stateUpdateTimer:) 
95                                                   userInfo:NULL 
96                                                    repeats:YES] retain];
97
98     [[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSEventTrackingRunLoopMode];        
99     return YES;
100 }
101
102 /**
103  * Closes low level HandBrake library and releases resources.
104  *
105  * @return YES if libhb was closed successfully, NO if there was an error.
106  */
107 - (BOOL)close
108 {
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;
119 }
120
121 /**
122  * Removes all jobs from the queue.
123  */ 
124  
125 - (void) removeAllJobs
126 {
127     hb_job_t * job;
128     while( ( job = hb_job( hb_handle, 0 ) ) )
129         hb_rem( hb_handle, job );
130 }
131
132 /**
133  * Returns libhb handle used by this HBCore instance.
134  */ 
135 - (struct hb_handle_s *)hb_handle
136 {
137     return hb_handle;
138 }
139
140 /**
141  * Returns current state of HBCore.
142  *
143  * @return One of the HBState* string constants.
144  */
145 - (const NSString *)state
146 {
147     return state;
148 }
149
150 /**
151  * Returns latest hb_state_s information struct returned by libhb.
152  *
153  * @return Pointer to a hb_state_s struct containing state information of libhb.
154  */
155 - (const struct hb_state_s *)hb_state
156 {
157     return hb_state;
158 }
159
160 @end 
161
162 @implementation HBCore (Private)
163
164 /**
165  * Transforms a libhb state constant to a matching HBCore state constant.
166  */
167 - (const NSString *)stateAsString:(int)stateValue
168 {
169     switch (stateValue)
170     {
171         case HB_STATE_IDLE:
172             return HBStateIdle;        
173         case HB_STATE_SCANNING:
174             return HBStateScanning;
175         case HB_STATE_SCANDONE:
176             return HBStateScanDone;
177         case HB_STATE_WORKING:
178             return HBStateWorking;
179         case HB_STATE_PAUSED:
180             return HBStatePaused;
181         case HB_STATE_WORKDONE:
182             return HBStateWorkDone;
183         case HB_STATE_MUXING:
184             return HBStateMuxing;        
185         default:
186             NSAssert1(NO, @"[HBCore stateAsString:] unknown state %d", stateValue);
187             return nil;
188     }
189 }
190
191 /**
192  * This method polls libhb continuously for state changes and processes them.
193  * Additional processing for each state is performed in methods that start
194  * with 'handle' (e.g. handleHBStateScanning).
195  */
196 - (void)stateUpdateTimer:(NSTimer *)timer
197 {
198     if (!hb_handle)
199     {
200         // Libhb is not open so we cannot do anything.
201         return;
202     }
203
204     hb_get_state(hb_handle, hb_state);
205
206     if (hb_state->state == HB_STATE_IDLE)
207     {
208         // Libhb reported HB_STATE_IDLE, so nothing interesting has happened.
209         return;
210     }
211         
212     // Update HBCore state to reflect the current state of libhb
213     NSString *newState = [self stateAsString:hb_state->state];
214     if (newState != state)
215     {
216         [self willChangeValueForKey:@"state"];
217         state = newState;
218         [self didChangeValueForKey:@"state"];
219     }
220
221     // Determine name of the method that does further processing for this state
222     // and call it. 
223     SEL sel = NSSelectorFromString([NSString stringWithFormat:@"handle%@", state]);
224     if ([self respondsToSelector:sel])
225         [self performSelector:sel];
226 }
227
228 /**
229  * Processes HBStateScanning state information. Current implementation just
230  * sends HBCoreScanningNotification.
231  */
232 - (void)handleHBStateScanning
233 {
234     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreScanningNotification object:self];    
235 }
236
237 /**
238  * Processes HBStateScanDone state information. Current implementation just
239  * sends HBCoreScanDoneNotification.
240  */
241 - (void)handleHBStateScanDone
242 {
243     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreScanDoneNotification object:self];    
244 }
245
246 /**
247  * Processes HBStateWorking state information. Current implementation just
248  * sends HBCoreWorkingNotification.
249  */
250 - (void)handleHBStateWorking
251 {
252     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreWorkingNotification object:self];    
253 }
254
255 /**
256  * Processes HBStatePaused state information. Current implementation just
257  * sends HBCorePausedNotification.
258  */
259 - (void)handleHBStatePaused
260 {
261     [[NSNotificationCenter defaultCenter] postNotificationName:HBCorePausedNotification object:self];    
262 }
263
264 /**
265  * Processes HBStateWorkDone state information. Current implementation just
266  * sends HBCoreWorkDoneNotification.
267  */
268 - (void)handleHBStateWorkDone
269 {
270     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreWorkDoneNotification object:self];    
271 }
272
273 /**
274  * Processes HBStateMuxing state information. Current implementation just
275  * sends HBCoreMuxingNotification.
276  */
277 - (void)handleHBStateMuxing
278 {
279     [[NSNotificationCenter defaultCenter] postNotificationName:HBCoreMuxingNotification object:self];    
280 }
281
282 @end