Commit d7e992d4 authored by Felix Paul Kühne's avatar Felix Paul Kühne

file parser queue: major rework to correctly support music albums wihout lucky...

file parser queue: major rework to correctly support music albums wihout lucky timing and guessing, parsing times and proper queue restarts
parent 05ee1d52
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
{ {
MLFile *_file; MLFile *_file;
VLCMedia *_media; VLCMedia *_media;
BOOL _parsing;
NSTimer *_timeOutTimer;
} }
@property (strong,readwrite) MLFile *file; @property (strong,readwrite) MLFile *file;
@end @end
...@@ -68,9 +70,12 @@ ...@@ -68,9 +70,12 @@
NSAssert(!_media, @"We are already parsing"); NSAssert(!_media, @"We are already parsing");
MLFile *file = self.file; MLFile *file = self.file;
APLog(@"Starting parsing %@", file); APLog(@"Starting PARSE %@", file);
[[MLCrashPreventer sharedPreventer] willParseFile:file]; [[MLCrashPreventer sharedPreventer] willParseFile:file];
_parsing = YES;
_timeOutTimer = [NSTimer scheduledTimerWithTimeInterval:3. target:self selector:@selector(cancelParsing:) userInfo:nil repeats:NO];
_media = [VLCMedia mediaWithURL:file.url]; _media = [VLCMedia mediaWithURL:file.url];
_media.delegate = self; _media.delegate = self;
[_media parse]; [_media parse];
...@@ -79,6 +84,17 @@ ...@@ -79,6 +84,17 @@
// Balanced in -mediaDidFinishParsing: // Balanced in -mediaDidFinishParsing:
} }
- (void)cancelParsing:(NSTimer *)timer
{
if (_parsing && _media) {
[self mediaDidFinishParsing:_media];
return;
}
_parsing = NO;
[self endParsing];
}
- (void)main - (void)main
{ {
[self performSelectorOnMainThread:@selector(parse) withObject:nil waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(parse) withObject:nil waitUntilDone:YES];
...@@ -86,6 +102,9 @@ ...@@ -86,6 +102,9 @@
- (void)mediaDidFinishParsing:(VLCMedia *)media - (void)mediaDidFinishParsing:(VLCMedia *)media
{ {
_parsing = NO;
[_timeOutTimer invalidate];
APLog(@"Parsed %@ - %lu tracks", media, [[_media tracksInformation] count]); APLog(@"Parsed %@ - %lu tracks", media, [[_media tracksInformation] count]);
if (_media.delegate != self) if (_media.delegate != self)
...@@ -125,16 +144,16 @@ ...@@ -125,16 +144,16 @@
} }
} }
@try {
[file setTracks:tracksSet]; [file setTracks:tracksSet];
if (mediaHasVideo && file.isMovie) {
if ([[_media length] intValue] < 600000) // 10min
file.type = kMLFileTypeClip;
}
[file setDuration:[[_media length] numberValue]]; [file setDuration:[[_media length] numberValue]];
}
@catch (NSException *exception) {
APLog(@"we failed to write some metadata because the file disappeared in front of us");
}
if ([file isAlbumTrack]) { if (!mediaHasVideo) {
NSDictionary *audioContentInfo = [_media metaDictionary]; NSDictionary *audioContentInfo = [_media metaDictionary];
if (audioContentInfo && audioContentInfo.count > 0) { if (audioContentInfo && audioContentInfo.count > 0) {
NSString *title = audioContentInfo[VLCMetaInformationTitle]; NSString *title = audioContentInfo[VLCMetaInformationTitle];
NSString *artist = audioContentInfo[VLCMetaInformationArtist]; NSString *artist = audioContentInfo[VLCMetaInformationArtist];
...@@ -146,7 +165,11 @@ ...@@ -146,7 +165,11 @@
MLAlbum *album = nil; MLAlbum *album = nil;
BOOL wasCreated = NO; BOOL wasCreated = NO;
MLAlbumTrack *track = [MLAlbumTrack trackWithAlbumName:albumName trackNumber:@([trackNumber integerValue]) createIfNeeded:YES wasCreated:&wasCreated]; MLAlbumTrack *track = [MLAlbumTrack trackWithAlbumName:albumName
trackNumber:@([trackNumber integerValue])
trackName:title
createIfNeeded:YES
wasCreated:&wasCreated];
if (track) { if (track) {
album = track.album; album = track.album;
track.title = title ? title : @""; track.title = title ? title : @"";
...@@ -161,9 +184,7 @@ ...@@ -161,9 +184,7 @@
file.albumTrack = track; file.albumTrack = track;
} }
} }
}
if (!mediaHasVideo) {
file.type = kMLFileTypeAudio; file.type = kMLFileTypeAudio;
APLog(@"'%@' is an audio file, fetching artwork", file.title); APLog(@"'%@' is an audio file, fetching artwork", file.title);
NSString *artist, *albumName, *title; NSString *artist, *albumName, *title;
...@@ -177,9 +198,16 @@ ...@@ -177,9 +198,16 @@
skipOperation = YES; skipOperation = YES;
} }
if (!skipOperation) {
title = file.title; title = file.title;
if (!title) {
NSDictionary *poorMansContentInfo = [MLTitleDecrapifier audioContentInfoFromFile:file];
if (poorMansContentInfo)
title = poorMansContentInfo[VLCMetaInformationTitle];
file.title = title;
}
if (!skipOperation) {
NSString *artworkPath = [self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumName]; NSString *artworkPath = [self artworkPathForMediaItemWithTitle:title Artist:artist andAlbumName:albumName];
if ([[NSFileManager defaultManager] fileExistsAtPath:artworkPath]) { if ([[NSFileManager defaultManager] fileExistsAtPath:artworkPath]) {
file.computedThumbnail = [UIImage scaleImage:[UIImage imageWithContentsOfFile:artworkPath] file.computedThumbnail = [UIImage scaleImage:[UIImage imageWithContentsOfFile:artworkPath]
...@@ -188,10 +216,26 @@ ...@@ -188,10 +216,26 @@
if (file.computedThumbnail == nil) if (file.computedThumbnail == nil)
file.albumTrack.containsArtwork = NO; file.albumTrack.containsArtwork = NO;
} }
} else {
MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
[sharedLibrary computeThumbnailForFile:file];
[sharedLibrary fetchMetaDataForFile:file];
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file.url.absoluteString error:nil];
NSNumber *size = attributes[NSFileSize]; // FIXME [result valueForAttribute:@"kMDItemFSSize"];
if ([size longLongValue] < 150000000) /* 150 MB */
file.type = kMLFileTypeClip;
else
file.type = kMLFileTypeMovie;
} }
file.hasFetchedInfo = @(YES);
[self endParsing];
}
- (void)endParsing
{
MLFileParserQueue *parserQueue = [MLFileParserQueue sharedFileParserQueue]; MLFileParserQueue *parserQueue = [MLFileParserQueue sharedFileParserQueue];
[[MLCrashPreventer sharedPreventer] didParseFile:file]; [[MLCrashPreventer sharedPreventer] didParseFile:self.file];
[parserQueue.queue setSuspended:NO]; [parserQueue.queue setSuspended:NO];
[parserQueue didFinishOperation:self]; [parserQueue didFinishOperation:self];
_media = nil; _media = nil;
...@@ -253,8 +297,6 @@ ...@@ -253,8 +297,6 @@
return self; return self;
} }
static inline NSString *hashFromFile(MLFile *file) static inline NSString *hashFromFile(MLFile *file)
{ {
return [NSString stringWithFormat:@"%p", [[file objectID] URIRepresentation]]; return [NSString stringWithFormat:@"%p", [[file objectID] URIRepresentation]];
...@@ -273,6 +315,10 @@ static inline NSString *hashFromFile(MLFile *file) ...@@ -273,6 +315,10 @@ static inline NSString *hashFromFile(MLFile *file)
APLog(@"%@ is unsafe and will crash, ignoring", file); APLog(@"%@ is unsafe and will crash, ignoring", file);
return; return;
} }
if (file.isBeingParsed) {
APLog(@"%@ is already being parsed, ignoring", file);
return;
}
MLParsingOperation *op = [[MLParsingOperation alloc] initWithFile:file]; MLParsingOperation *op = [[MLParsingOperation alloc] initWithFile:file];
[_fileDescriptionToOperation setValue:op forKey:hashFromFile(file)]; [_fileDescriptionToOperation setValue:op forKey:hashFromFile(file)];
[self.queue addOperation:op]; [self.queue addOperation:op];
...@@ -280,11 +326,15 @@ static inline NSString *hashFromFile(MLFile *file) ...@@ -280,11 +326,15 @@ static inline NSString *hashFromFile(MLFile *file)
- (void)stop - (void)stop
{ {
if (![[MLCrashPreventer sharedPreventer] fileParsingInProgress])
[_queue setSuspended:YES];
[_queue setMaxConcurrentOperationCount:0]; [_queue setMaxConcurrentOperationCount:0];
} }
- (void)resume - (void)resume
{ {
if (![[MLCrashPreventer sharedPreventer] fileParsingInProgress])
[_queue setSuspended:NO];
[_queue setMaxConcurrentOperationCount:1]; [_queue setMaxConcurrentOperationCount:1];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment