MLThumbnailerQueue.m 6.92 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * MLThumbnailerQueue.m
 * MobileMediaLibraryKit
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
6
 * Copyright (C) 2010-2015 VLC authors and VideoLAN
7 8 9 10 11
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Pierre's avatar
Pierre committed
26 27 28

#import "MLThumbnailerQueue.h"
#import "MLFile.h"
29
#import "MLCrashPreventer.h"
30
#import "MLMediaLibrary.h"
Pierre's avatar
Pierre committed
31 32 33 34

@interface ThumbnailOperation : NSOperation <VLCMediaThumbnailerDelegate>
{
    MLFile *_file;
35
    VLCMedia *_media;
36
    VLCLibrary *_internalLibrary;
Pierre's avatar
Pierre committed
37
}
38
@property (strong,readwrite) MLFile *file;
Pierre's avatar
Pierre committed
39 40
@end

41
@interface MLThumbnailerQueue ()
42 43
{
    VLCLibrary *_internalLibrary;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
44 45
    NSDictionary *_fileDescriptionToOperation;
    NSOperationQueue *_queue;
46
}
47 48 49
- (void)didFinishOperation:(ThumbnailOperation *)op;
@end

Pierre's avatar
Pierre committed
50 51
@implementation ThumbnailOperation
@synthesize file=_file;
52
- (id)initWithFile:(MLFile *)file andVLCLibrary:(VLCLibrary *)library;
Pierre's avatar
Pierre committed
53 54 55 56
{
    if (!(self = [super init]))
        return nil;
    self.file = file;
57
    _internalLibrary = library;
Pierre's avatar
Pierre committed
58 59 60 61 62
    return self;
}

- (void)fetchThumbnail
{
63
    APLog(@"Starting THUMB %@", self.file);
64 65 66

    [[MLCrashPreventer sharedPreventer] willParseFile:self.file];

67
    _media = [VLCMedia mediaWithURL:[NSURL URLWithString:self.file.url]];
68
    VLCMediaThumbnailer *thumbnailer = [VLCMediaThumbnailer thumbnailerWithMedia:_media delegate:self andVLCLibrary:_internalLibrary];
69 70 71 72 73
    MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];

    CGSize thumbSize = [thumbnailerQueue preferredThumbnailSizeForDevice];
    thumbnailer.thumbnailWidth = thumbSize.width;
    thumbnailer.thumbnailHeight = thumbSize.height;
Pierre's avatar
Pierre committed
74
    [thumbnailer fetchThumbnail];
75
    [thumbnailerQueue.queue setSuspended:YES]; // Balanced in -mediaThumbnailer:didFinishThumbnail
76
     // Balanced in -mediaThumbnailer:didFinishThumbnail:
Pierre's avatar
Pierre committed
77 78 79 80 81 82
}
- (void)main
{
    [self performSelectorOnMainThread:@selector(fetchThumbnail) withObject:nil waitUntilDone:YES];
}

Pierre's avatar
Pierre committed
83
- (void)endThumbnailing
Pierre's avatar
Pierre committed
84
{
85
    [[MLCrashPreventer sharedPreventer] didParseFile:self.file];
86 87 88
    MLThumbnailerQueue *thumbnailerQueue = [MLThumbnailerQueue sharedThumbnailerQueue];
    [thumbnailerQueue.queue setSuspended:NO];
    [thumbnailerQueue didFinishOperation:self];
Pierre's avatar
Pierre committed
89
}
Pierre's avatar
Pierre committed
90 91 92
- (void)mediaThumbnailer:(VLCMediaThumbnailer *)mediaThumbnailer didFinishThumbnail:(CGImageRef)thumbnail
{
    mediaThumbnailer.delegate = nil;
93
    APLog(@"Finished thumbnail for %@", self.file.title);
94
    self.file.computedThumbnail = [UIImage imageWithCGImage:thumbnail];
Pierre's avatar
Pierre committed
95 96 97 98 99 100 101 102 103

    [self endThumbnailing];
}

- (void)mediaThumbnailerDidTimeOut:(VLCMediaThumbnailer *)mediaThumbnailer
{
    self.file.thumbnailTimeouted = YES;
    [self endThumbnailing];
}
Pierre's avatar
Pierre committed
104 105 106
@end

@implementation MLThumbnailerQueue
107
@synthesize queue=_queue;
Pierre's avatar
Pierre committed
108 109 110 111 112 113 114 115 116
+ (MLThumbnailerQueue *)sharedThumbnailerQueue
{
    static MLThumbnailerQueue *shared = nil;
    if (!shared) {
        shared = [[MLThumbnailerQueue alloc] init];
    }
    return shared;
}

117 118 119 120
- (id)init
{
    self = [super init];
    if (self != nil) {
121
        int speedCategory = [[MLMediaLibrary sharedMediaLibrary] deviceSpeedCategory];
122 123 124 125
        APLog(@"running on a category %i device", speedCategory);
        if (speedCategory < 2)
            _internalLibrary = [VLCLibrary sharedLibrary];
        else
126
            _internalLibrary = [[VLCLibrary alloc] initWithOptions:@[@"--avcodec-threads=1", @"--avcodec-skip-idct=4", @"--deinterlace=-1", @"--avcodec-skiploopfilter=3", @"--no-interact", @"--avi-index=3"]];
127
        _fileDescriptionToOperation = [[NSMutableDictionary alloc] init];
128 129
        _queue = [[NSOperationQueue alloc] init];
        [_queue setMaxConcurrentOperationCount:1];
130 131 132 133 134 135 136 137 138
    }
    return self;
}

static inline NSString *hashFromFile(MLFile *file)
{
    return [NSString stringWithFormat:@"%p", [[file objectID] URIRepresentation]];
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
- (CGSize)preferredThumbnailSizeForDevice
{
    CGFloat thumbnailWidth, thumbnailHeight;
    /* optimize thumbnails for the device */
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if ([UIScreen mainScreen].scale==2.0) {
            thumbnailWidth = 540.;
            thumbnailHeight = 405.;
        } else {
            thumbnailWidth = 272.;
            thumbnailHeight = 204.;
        }
    } else {
        if (SYSTEM_RUNS_IOS7) {
            if ([UIScreen mainScreen].scale==2.0) {
                thumbnailWidth = 480.;
                thumbnailHeight = 270.;
            } else {
                thumbnailWidth = 720.;
                thumbnailHeight = 405.;
            }
        } else {
            if ([UIScreen mainScreen].scale==2.0) {
                thumbnailWidth = 480.;
                thumbnailHeight = 270.;
            } else {
                thumbnailWidth = 240.;
                thumbnailHeight = 135.;
            }
        }
    }
    return CGSizeMake(thumbnailWidth, thumbnailHeight);
}

173 174 175 176 177
- (void)didFinishOperation:(ThumbnailOperation *)op
{
    [_fileDescriptionToOperation setValue:nil forKey:hashFromFile(op.file)];
}

Pierre's avatar
Pierre committed
178 179
- (void)addFile:(MLFile *)file
{
180
    if (_fileDescriptionToOperation[hashFromFile(file)])
181 182
        return;
    if (![[MLCrashPreventer sharedPreventer] isFileSafe:file]) {
183
        APLog(@"'%@' is unsafe and will crash, ignoring", file.title);
184 185
        return;
    }
186 187 188 189
    if ([file isAlbumTrack]) {
        APLog(@"'%@' is an audio file, ignoring", file.title);
        return;
    }
190
    ThumbnailOperation *op = [[ThumbnailOperation alloc] initWithFile:file andVLCLibrary:_internalLibrary];
191
    [_fileDescriptionToOperation setValue:op forKey:hashFromFile(file)];
192
    [self.queue addOperation:op];
Pierre's avatar
Pierre committed
193 194 195 196
}

- (void)stop
{
197
    [_queue setMaxConcurrentOperationCount:0];
Pierre's avatar
Pierre committed
198 199 200 201
}

- (void)resume
{
202
    [_queue setMaxConcurrentOperationCount:1];
Pierre's avatar
Pierre committed
203
}
204 205 206

- (void)setHighPriorityForFile:(MLFile *)file
{
207
    ThumbnailOperation *op = _fileDescriptionToOperation[hashFromFile(file)];
208 209 210 211 212
    [op setQueuePriority:NSOperationQueuePriorityHigh];
}

- (void)setDefaultPriorityForFile:(MLFile *)file
{
213
    ThumbnailOperation *op = _fileDescriptionToOperation[hashFromFile(file)];
214 215
    [op setQueuePriority:NSOperationQueuePriorityNormal];
}
216

Pierre's avatar
Pierre committed
217
@end