diff --git a/Classes/SBQueue.h b/Classes/SBQueue.h index 6fc2b2b..5f4e330 100644 --- a/Classes/SBQueue.h +++ b/Classes/SBQueue.h @@ -11,8 +11,6 @@ #import "SBQueueItem.h" -@class MP42File; - typedef NS_ENUM(NSUInteger, SBQueueStatus) { SBQueueStatusUnknown = 0, SBQueueStatusWorking, diff --git a/Classes/SBQueue.m b/Classes/SBQueue.m index 855d71e..0fd6efa 100644 --- a/Classes/SBQueue.m +++ b/Classes/SBQueue.m @@ -8,19 +8,12 @@ #import "SBQueue.h" -#import "MetadataImporter.h" - -#import -#import -#import -#import - NSString *SBQueueWorkingNotification = @"SBQueueWorkingNotification"; NSString *SBQueueCompletedNotification = @"SBQueueCompletedNotification"; NSString *SBQueueFailedNotification = @"SBQueueFailedNotification"; NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; -@interface SBQueue () +@interface SBQueue () @property (atomic) SBQueueStatus status; @@ -199,9 +192,10 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; self.currentIndex = [self.items indexOfObject:self.currentItem]; self.currentItem.status = SBQueueItemStatusWorking; + self.currentItem.delegate = self; [self handleSBStatusWorking:0 index:self.currentIndex]; - noErr = [self processItem:self.currentItem optimize:self.optimize error:&outError]; + noErr = [self.currentItem processItem:self.optimize error:&outError]; // Check results if (self.cancelled) { @@ -218,6 +212,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; [self handleSBStatusFailed:outError]; } + self.currentItem.delegate = nil; self.currentItem = nil; } @@ -235,64 +230,6 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; }); } -/** - * Processes a SBQueueItem. - */ -- (BOOL)processItem:(SBQueueItem *)item optimize:(BOOL)optimize error:(NSError **)outError { - NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; - if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) - [attributes setObject:@YES forKey:MP42GenerateChaptersPreviewTrack]; - -#ifdef SB_SANDBOX - if([destination respondsToSelector:@selector(startAccessingSecurityScopedResource)]) - [destination startAccessingSecurityScopedResource]; -#endif - - BOOL noErr = YES; - MP42File *file = nil; - - // The file has been added directly to the queue - if (!item.mp4File && item.URL) { - [item prepareItem:outError]; - } - - file = item.mp4File; - file.delegate = self; - - NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[file.URL path] error:NULL]; - NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize]; - if (freeSpace && [file dataSize] > [freeSpace longLongValue]) { - NSLog(@"Not enough disk space"); - [self stop]; - } - - if (!self.cancelled) { - if ([file hasFileRepresentation]) { - // We have an existing mp4 file, update it - noErr = [file updateMP4FileWithAttributes:attributes error:outError]; - } else if (file && item.destURL) { - // Write the new file to disk - [attributes addEntriesFromDictionary:item.attributes]; - noErr = [file writeToUrl:item.destURL - withAttributes:attributes - error:outError]; - } - } - - if (noErr && optimize) { - [file optimize]; - } - -#ifdef SB_SANDBOX - if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)]) - [destination stopAccessingSecurityScopedResource]; -#endif - - [attributes release]; - - return noErr; -} - /** * Stops the queue and abort the current work. */ diff --git a/Classes/SBQueueItem.h b/Classes/SBQueueItem.h index eeebcc7..b4b9031 100755 --- a/Classes/SBQueueItem.h +++ b/Classes/SBQueueItem.h @@ -7,7 +7,6 @@ // #import - #import "SBQueueAction.h" @class MP42File; @@ -30,29 +29,33 @@ typedef enum SBQueueItemStatus : NSInteger { NSDictionary *_attributes; NSMutableArray *_actions; + BOOL _cancelled; + + id _delegate; + SBQueueItemStatus _status; } +@property (nonatomic, readwrite) SBQueueItemStatus status; + @property (nonatomic, readonly) MP42File *mp4File; -@property (nonatomic, readonly) NSDictionary *attributes; + @property (nonatomic, readonly) NSURL *URL; @property (nonatomic, copy) NSURL *destURL; @property (nonatomic, readonly) NSMutableArray *actions; +@property (nonatomic, readonly) NSDictionary *attributes; -@property (nonatomic, readwrite) SBQueueItemStatus status; +@property (nonatomic, assign) id delegate; -- (instancetype)initWithURL:(NSURL *)URL; + (instancetype)itemWithURL:(NSURL *)URL; -- (instancetype)initWithMP4:(MP42File *)MP4; -- (instancetype)initWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict; - + (instancetype)itemWithMP4:(MP42File *)MP4; + (instancetype)itemWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict; - (void)addAction:(id)action; - (BOOL)prepareItem:(NSError **)outError; +- (BOOL)processItem:(BOOL)optimize error:(NSError **)outError; @end diff --git a/Classes/SBQueueItem.m b/Classes/SBQueueItem.m index 3642773..c910368 100755 --- a/Classes/SBQueueItem.m +++ b/Classes/SBQueueItem.m @@ -13,10 +13,11 @@ #import #import -@interface SBQueueItem () +@interface SBQueueItem () @property (nonatomic, readwrite, retain) MP42File *mp4File; @property (nonatomic, readwrite) NSMutableArray *actions; +@property (atomic) BOOL cancelled; @end @@ -26,11 +27,14 @@ @synthesize attributes = _attributes; @synthesize actions = _actions; +@synthesize cancelled = _cancelled; @synthesize URL = _fileURL; @synthesize destURL = _destURL; @synthesize mp4File = _mp4File; +@synthesize delegate = _delegate; + - (instancetype)init { self = [super init]; @@ -164,6 +168,64 @@ return YES; } +- (BOOL)processItem:(BOOL)optimize error:(NSError **)outError { +#ifdef SB_SANDBOX + if([destination respondsToSelector:@selector(startAccessingSecurityScopedResource)]) + [destination startAccessingSecurityScopedResource]; +#endif + + BOOL noErr = YES; + + // The file has been added directly to the queue + if (!self.mp4File && self.URL) { + [self prepareItem:outError]; + } + + self.mp4File.delegate = self; + + NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[self.mp4File.URL path] error:NULL]; + NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize]; + if (freeSpace && [self.mp4File dataSize] > [freeSpace longLongValue]) { + NSLog(@"Not enough disk space"); + [self cancel]; + } + + if (!self.cancelled) { + if ([self.mp4File hasFileRepresentation]) { + // We have an existing mp4 file, update it + noErr = [self.mp4File updateMP4FileWithAttributes:self.attributes error:outError]; + } else if (self.mp4File && self.destURL) { + // Write the new file to disk + noErr = [self.mp4File writeToUrl:self.destURL + withAttributes:self.attributes + error:outError]; + } + } + + if (noErr && optimize) { + noErr = [self.mp4File optimize]; + } + +#ifdef SB_SANDBOX + if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)]) + [destination stopAccessingSecurityScopedResource]; +#endif + + self.mp4File = nil; + [self.actions removeAllObjects]; + + return noErr; +} + +- (void)cancel { + self.cancelled = YES; + [self.mp4File cancel]; +} + +- (void)progressStatus:(CGFloat)progress { + [self.delegate progressStatus:progress]; +} + - (void)dealloc { [_attributes release]; [_actions release];