More work on the queue.

This commit is contained in:
Damiano Galassi committed 2014-03-16 13:15:04 +01:00
1 parent 9fc0aa520c
commit ed1e386e90
2 files changed
+109 -72

No files matched your search

+3 -2
View File
@@ -27,10 +27,11 @@ extern NSString *SBQueueCancelledNotification;
@interface SBQueue : NSObject { @interface SBQueue : NSObject {
@private @private
dispatch_queue_t _queue; dispatch_queue_t _workQueue;
dispatch_queue_t _itemsQueue;
SBQueueStatus _status; SBQueueStatus _status;
MP42File *_currentMP4; SBQueueItem *_currentItem;
NSUInteger _currentIndex; NSUInteger _currentIndex;
NSMutableArray *_items; NSMutableArray *_items;
NSURL *_URL; NSURL *_URL;
+106 -70
View File
@@ -28,10 +28,12 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@property (nonatomic, copy) NSURL *URL; @property (nonatomic, copy) NSURL *URL;
@property (nonatomic, retain) NSMutableArray *items; @property (nonatomic, retain) NSMutableArray *items;
@property (atomic, retain) MP42File *currentMP4; @property (atomic, retain) SBQueueItem *currentItem;
@property (nonatomic) NSUInteger currentIndex; @property (atomic) NSUInteger currentIndex;
@property (atomic) BOOL cancelled;
@property (nonatomic) dispatch_queue_t queue; @property (nonatomic) dispatch_queue_t workQueue;
@property (nonatomic) dispatch_queue_t itemsQueue;
@end @end
@@ -39,13 +41,15 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@synthesize status = _status; @synthesize status = _status;
@synthesize optimize = _optimize; @synthesize optimize = _optimize;
@synthesize currentMP4 = _currentMP4, currentIndex = _currentIndex; @synthesize currentItem = _currentItem, currentIndex = _currentIndex;
@synthesize items = _items, URL = _URL, queue = _queue; @synthesize cancelled = _cancelled;
@synthesize items = _items, itemsQueue = _itemsQueue, URL = _URL, workQueue = _workQueue;
- (instancetype)initWithURL:(NSURL *)queueURL { - (instancetype)initWithURL:(NSURL *)queueURL {
self = [super init]; self = [super init];
if (self) { if (self) {
_queue = dispatch_queue_create("org.subler.Queue", NULL); _workQueue = dispatch_queue_create("org.subler.WorkQueue", NULL);
_itemsQueue = dispatch_queue_create("org.subler.ItemsQueue", NULL);
_URL = [queueURL copy]; _URL = [queueURL copy];
if ([[NSFileManager defaultManager] fileExistsAtPath:[queueURL path]]) { if ([[NSFileManager defaultManager] fileExistsAtPath:[queueURL path]]) {
@@ -71,70 +75,106 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
} }
- (BOOL)saveQueueToDisk { - (BOOL)saveQueueToDisk {
return [NSKeyedArchiver archiveRootObject:self.items toFile:[self.URL path]]; __block BOOL noErr = YES;
dispatch_sync(self.itemsQueue, ^{
noErr = [NSKeyedArchiver archiveRootObject:self.items toFile:[self.URL path]];
});
return noErr;
} }
- (SBQueueItem *)firstItemInQueue - (SBQueueItem *)firstItemInQueue
{ {
for (SBQueueItem *item in self.items) __block SBQueueItem *firstItem = nil;
if ((item.status != SBQueueItemStatusCompleted) && (item.status != SBQueueItemStatusFailed)) dispatch_sync(self.itemsQueue, ^{
return item; for (SBQueueItem *item in self.items) {
if ((item.status != SBQueueItemStatusCompleted) && (item.status != SBQueueItemStatusFailed)) {
return nil; firstItem = [item retain];
break;
}
}
});
return [firstItem autorelease];
} }
#pragma mark - item management #pragma mark - item management
- (void)addItem:(SBQueueItem *)item { - (void)addItem:(SBQueueItem *)item {
[self.items addObject:item]; dispatch_sync(self.itemsQueue, ^{
[self.items addObject:item];
});
} }
- (NSUInteger)count { - (NSUInteger)count {
return [self.items count]; __block NSUInteger count = 0;
dispatch_sync(self.itemsQueue, ^{
count = [self.items count];
});
return count;
} }
- (NSUInteger)readyCount { - (NSUInteger)readyCount {
NSUInteger count = 0; __block NSUInteger count = 0;
for (SBQueueItem *item in self.items) dispatch_sync(self.itemsQueue, ^{
if ([item status] != SBQueueItemStatusCompleted) for (SBQueueItem *item in self.items)
count++; if ([item status] != SBQueueItemStatusCompleted)
count++;
});
return count; return count;
} }
- (SBQueueItem *)itemAtIndex:(NSUInteger)index { - (SBQueueItem *)itemAtIndex:(NSUInteger)index {
return [self.items objectAtIndex:index]; __block SBQueueItem *item = nil;
dispatch_sync(self.itemsQueue, ^{
item = [self.items objectAtIndex:index];
});
return item;
} }
- (NSArray *)itemsAtIndexes:(NSIndexSet *)indexes { - (NSArray *)itemsAtIndexes:(NSIndexSet *)indexes {
return [self.items objectsAtIndexes:indexes]; __block NSArray *items = nil;
dispatch_sync(self.itemsQueue, ^{
items = [self.items objectsAtIndexes:indexes];
});
return items;
} }
- (NSUInteger)indexOfItem:(SBQueueItem *)item { - (NSUInteger)indexOfItem:(SBQueueItem *)item {
return [self.items indexOfObject:item]; __block NSUInteger index = NSNotFound;
dispatch_sync(self.itemsQueue, ^{
index = [self.items indexOfObject:item];
});
return index;
} }
- (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index { - (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index {
[self.items insertObject:anItem atIndex:index]; dispatch_sync(self.itemsQueue, ^{
[self.items insertObject:anItem atIndex:index];
});
} }
- (void)removeItemsAtIndexes:(NSIndexSet *)indexes { - (void)removeItemsAtIndexes:(NSIndexSet *)indexes {
[self.items removeObjectsAtIndexes:indexes]; dispatch_sync(self.itemsQueue, ^{
[self.items removeObjectsAtIndexes:indexes];
});
} }
- (void)removeItem:(SBQueueItem *)item { - (void)removeItem:(SBQueueItem *)item {
[self.items removeObject:item]; dispatch_sync(self.itemsQueue, ^{
[self.items removeObject:item];
});
} }
- (NSIndexSet *)removeCompletedItems { - (NSIndexSet *)removeCompletedItems {
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
for (SBQueueItem *item in self.items) dispatch_sync(self.itemsQueue, ^{
if ([item status] == SBQueueItemStatusCompleted) for (SBQueueItem *item in self.items)
[indexes addIndex:[self.items indexOfObject:item]]; if ([item status] == SBQueueItemStatusCompleted)
[indexes addIndex:[self.items indexOfObject:item]];
[self.items removeObjectsAtIndexes:indexes]; [self.items removeObjectsAtIndexes:indexes];
});
return [indexes autorelease]; return [indexes autorelease];
} }
@@ -166,52 +206,44 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
// Enable sleep assertion // Enable sleep assertion
[self disableSleep]; [self disableSleep];
dispatch_async(self.queue, ^{ dispatch_async(self.workQueue, ^{
NSError *outError = nil; NSError *outError = nil;
BOOL noErr = NO; BOOL noErr = NO;
for (;;) { for (;;) {
@autoreleasepool { @autoreleasepool {
__block SBQueueItem *item = nil;
// Save the queue // Save the queue
dispatch_async(dispatch_get_main_queue(), ^{ [self saveQueueToDisk];
[self saveQueueToDisk];
});
// Get the first item available in the queue // Get the first item available in the queue
dispatch_sync(dispatch_get_main_queue(), ^{ self.currentItem = [[self firstItemInQueue] retain];
item = [[self firstItemInQueue] retain];
item.status = SBQueueItemStatusWorking;
});
if (item == nil) { if (!self.currentItem) {
break; break;
} }
[self handleSBStatusWorking:0]; self.currentIndex = [self.items indexOfObject:self.currentItem];
noErr = [self processItem:item error:&outError]; self.currentItem.status = SBQueueItemStatusWorking;
[self handleSBStatusWorking:self.currentIndex];
noErr = [self processItem:self.currentItem optimize:self.optimize error:&outError];
// Check results // Check results
if (_cancelled) { if (self.cancelled) {
item.status = SBQueueItemStatusCancelled; self.currentItem.status = SBQueueItemStatusCancelled;
[item release]; [self.currentItem release];
[self handleSBStatusCancelled]; [self handleSBStatusCancelled];
break; break;
} else if (noErr) {
if (self.optimize) {
noErr = [self.currentMP4 optimize];
}
} }
if (noErr) { if (noErr) {
item.status = SBQueueItemStatusCompleted; self.currentItem.status = SBQueueItemStatusCompleted;
} else { } else {
item.status = SBQueueItemStatusFailed; self.currentItem.status = SBQueueItemStatusFailed;
[self handleSBStatusFailed:outError]; [self handleSBStatusFailed:outError];
} }
[item release]; [self.currentItem release];
} }
if (self.status == SBQueueStatusCancelled) { if (self.status == SBQueueStatusCancelled) {
@@ -231,7 +263,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
/** /**
* Processes a SBQueueItem. * Processes a SBQueueItem.
*/ */
- (BOOL)processItem:(SBQueueItem *)item error:(NSError **)outError { - (BOOL)processItem:(SBQueueItem *)item optimize:(BOOL)optimize error:(NSError **)outError {
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue])
[attributes setObject:@YES forKey:MP42GenerateChaptersPreviewTrack]; [attributes setObject:@YES forKey:MP42GenerateChaptersPreviewTrack];
@@ -242,37 +274,40 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
#endif #endif
BOOL noErr = YES; BOOL noErr = YES;
MP42File *file = nil;
self.currentMP4 = [item mp4File];
self.currentIndex = [self.items indexOfObject:item];
// The file has been added directly to the queue // The file has been added directly to the queue
if (!_currentMP4 && item.URL) { if (!item.mp4File && item.URL) {
[item prepareItem:outError]; [item prepareItem:outError];
self.currentMP4 = item.mp4File; file = item.mp4File;
} }
self.currentMP4.delegate = self; file.delegate = self;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[_currentMP4.URL path] error:NULL]; NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[file.URL path] error:NULL];
NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize]; NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize];
if (freeSpace && [_currentMP4 dataSize] > [freeSpace longLongValue]) { if (freeSpace && [file dataSize] > [freeSpace longLongValue]) {
NSLog(@"Not enough disk space"); NSLog(@"Not enough disk space");
[self stop];
} }
if (!_cancelled) { if (!self.cancelled) {
if ([self.currentMP4 hasFileRepresentation]) { if ([file hasFileRepresentation]) {
// We have an existing mp4 file, update it // We have an existing mp4 file, update it
noErr = [self.currentMP4 updateMP4FileWithAttributes:attributes error:outError]; noErr = [file updateMP4FileWithAttributes:attributes error:outError];
} else if (self.currentMP4 && item.destURL) { } else if (file && item.destURL) {
// Write the new file to disk // Write the new file to disk
[attributes addEntriesFromDictionary:item.attributes]; [attributes addEntriesFromDictionary:item.attributes];
noErr = [self.currentMP4 writeToUrl:item.destURL noErr = [file writeToUrl:item.destURL
withAttributes:attributes withAttributes:attributes
error:outError]; error:outError];
} }
} }
if (noErr && optimize) {
[file optimize];
}
#ifdef SB_SANDBOX #ifdef SB_SANDBOX
if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)]) if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)])
[destination stopAccessingSecurityScopedResource]; [destination stopAccessingSecurityScopedResource];
@@ -286,8 +321,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
* Stops the queue and abort the current work. * Stops the queue and abort the current work.
*/ */
- (void)stop { - (void)stop {
_cancelled = YES; self.cancelled = YES;
[self.currentMP4 cancel]; [self.currentItem.mp4File cancel];
} }
- (void)progressStatus:(CGFloat)progress { - (void)progressStatus:(CGFloat)progress {
@@ -319,7 +354,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
*/ */
- (void)handleSBStatusFailed:(NSError *)error { - (void)handleSBStatusFailed:(NSError *)error {
self.status = SBQueueStatusFailed; self.status = SBQueueStatusFailed;
[[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self userInfo:@{@"Error" : error}]; [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self userInfo:@{@"Error" : error != nil ? error : [NSNull null]}];
} }
/** /**
@@ -332,7 +367,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
} }
- (void)dealloc { - (void)dealloc {
dispatch_release(_queue); dispatch_release(_workQueue);
dispatch_release(_itemsQueue);
[_items release]; [_items release];
[_URL release]; [_URL release];