| 1 |
masaki |
2 |
// |
| 2 |
|
|
// XspfMMovieLoadRequest.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/01. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMMovieLoadRequest.h" |
| 10 |
|
|
|
| 11 |
|
|
#import <QTKit/QTKit.h> |
| 12 |
|
|
#import "XspfQTComponent.h" |
| 13 |
|
|
#import "XspfQTValueTransformers.h" |
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
@implementation XspfMMovieLoadRequest |
| 17 |
|
|
|
| 18 |
|
|
@synthesize object; |
| 19 |
|
|
@synthesize url; |
| 20 |
|
|
|
| 21 |
masaki |
71 |
+ (id)requestWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl; |
| 22 |
masaki |
2 |
{ |
| 23 |
|
|
return [[[self alloc] initWithObject:anObject url:anUrl] autorelease]; |
| 24 |
|
|
} |
| 25 |
masaki |
71 |
- (id)initWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl |
| 26 |
masaki |
2 |
{ |
| 27 |
|
|
self = [super init]; |
| 28 |
|
|
self.object = anObject; |
| 29 |
|
|
self.url = anUrl; |
| 30 |
|
|
|
| 31 |
|
|
return self; |
| 32 |
|
|
} |
| 33 |
masaki |
89 |
- (void)delloc |
| 34 |
|
|
{ |
| 35 |
|
|
self.object = nil; |
| 36 |
|
|
self.url = nil; |
| 37 |
|
|
|
| 38 |
|
|
[super dealloc]; |
| 39 |
|
|
} |
| 40 |
masaki |
2 |
|
| 41 |
|
|
static QTMovie *loadFromMovieURL(NSURL *url) |
| 42 |
|
|
{ |
| 43 |
masaki |
88 |
NSError *error = nil; |
| 44 |
masaki |
2 |
|
| 45 |
masaki |
88 |
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys: |
| 46 |
|
|
url, QTMovieURLAttribute, |
| 47 |
|
|
[NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute, |
| 48 |
|
|
nil]; |
| 49 |
|
|
QTMovie *result = [[QTMovie alloc] initWithAttributes:attrs error:&error]; |
| 50 |
|
|
if (result == nil) { |
| 51 |
|
|
if (error != nil) { |
| 52 |
|
|
NSLog(@"Couldn't load movie URL, error = %@", error); |
| 53 |
|
|
} |
| 54 |
|
|
} |
| 55 |
masaki |
2 |
|
| 56 |
masaki |
88 |
return result; |
| 57 |
masaki |
2 |
} |
| 58 |
|
|
|
| 59 |
|
|
static XspfQTComponent *componentForURL(NSURL *url) |
| 60 |
|
|
{ |
| 61 |
|
|
NSError *theErr = nil; |
| 62 |
|
|
|
| 63 |
|
|
NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url |
| 64 |
|
|
options:0 |
| 65 |
|
|
error:&theErr] autorelease]; |
| 66 |
|
|
if(!d) { |
| 67 |
|
|
if(theErr) { |
| 68 |
|
|
NSLog(@"%@", theErr); |
| 69 |
|
|
} |
| 70 |
|
|
return nil; |
| 71 |
|
|
} |
| 72 |
|
|
NSXMLElement *root = [d rootElement]; |
| 73 |
|
|
XspfQTComponent *pl = [XspfQTComponent xspfComponemtWithXMLElement:root]; |
| 74 |
|
|
if(!pl) { |
| 75 |
|
|
NSLog(@"Can not create XspfQTComponent."); |
| 76 |
|
|
return nil; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
return pl; |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
static inline NSSize maxSizeForFrame(NSSize size, CGSize frame) |
| 83 |
|
|
{ |
| 84 |
|
|
NSSize result = size; |
| 85 |
|
|
CGFloat aspectRetio = size.width / size.height; |
| 86 |
|
|
CGFloat frameAspectRetio = frame.width / frame.height; |
| 87 |
|
|
|
| 88 |
|
|
if(aspectRetio > frameAspectRetio) { |
| 89 |
|
|
result.width = frame.width; |
| 90 |
|
|
result.height = result.width / aspectRetio; |
| 91 |
|
|
} else { |
| 92 |
|
|
result.height = frame.height; |
| 93 |
|
|
result.width = result.height * aspectRetio; |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
return result; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
/** ��������������������������������������������������������������������������������������������������������������������������������������������������� |
| 100 |
|
|
** ������������������������������������������������������������ |
| 101 |
|
|
** ��������������������������������������������������������������������� |
| 102 |
|
|
** ������������������������������������������������������������ |
| 103 |
|
|
** ��������������������������������������������������������������������������� |
| 104 |
|
|
**/ |
| 105 |
|
|
static QTTime calcDefaultThumbnailQTTime(QTMovie *movie) |
| 106 |
|
|
{ |
| 107 |
|
|
XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease]; |
| 108 |
|
|
|
| 109 |
|
|
NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute]; |
| 110 |
|
|
id pV = [t transformedValue:pTimeValue]; |
| 111 |
|
|
if([pV longValue] == 0) { |
| 112 |
|
|
NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute]; |
| 113 |
|
|
id v = [t transformedValue:duration]; |
| 114 |
|
|
|
| 115 |
|
|
double newPosterTime = 0; |
| 116 |
|
|
double dDur = [v doubleValue]; |
| 117 |
|
|
if(dDur > 15 * 60) { |
| 118 |
|
|
newPosterTime = dDur / 100; |
| 119 |
|
|
} else if(dDur > 60) { |
| 120 |
|
|
newPosterTime = 1; |
| 121 |
|
|
} |
| 122 |
|
|
pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]]; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
return [pTimeValue QTTimeValue]; |
| 126 |
|
|
} |
| 127 |
|
|
static NSImage *thumbnailForTrackTime(XspfQTComponent *track, NSTimeInterval time, CGSize size) |
| 128 |
|
|
{ |
| 129 |
|
|
NSError *theErr = nil; |
| 130 |
|
|
QTMovie *movie = loadFromMovieURL([track movieLocation]); |
| 131 |
|
|
|
| 132 |
|
|
NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute]; |
| 133 |
|
|
NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size); |
| 134 |
|
|
|
| 135 |
|
|
QTTime t; |
| 136 |
|
|
if(time == DBL_MIN) { |
| 137 |
|
|
t = calcDefaultThumbnailQTTime(movie); |
| 138 |
|
|
} else { |
| 139 |
|
|
t = QTMakeTimeWithTimeInterval(time); |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys: |
| 143 |
|
|
QTMovieFrameImageTypeNSImage,QTMovieFrameImageType, |
| 144 |
|
|
[NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize, |
| 145 |
|
|
nil]; |
| 146 |
|
|
|
| 147 |
|
|
NSImage *theImage = (NSImage *)[movie frameImageAtTime:t |
| 148 |
|
|
withAttributes:imgProp |
| 149 |
|
|
error:&theErr]; |
| 150 |
|
|
if (theImage == nil) { |
| 151 |
|
|
if (theErr != nil) { |
| 152 |
|
|
NSLog(@"Couldn't create CGImageRef, error = %@", theErr); |
| 153 |
|
|
} |
| 154 |
|
|
return NULL; |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
return theImage; |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
static NSImage *thumbnailWithComponent(XspfQTComponent *component) |
| 161 |
|
|
{ |
| 162 |
|
|
XspfQTComponent *track = [component thumbnailTrack]; |
| 163 |
|
|
NSTimeInterval interval = [component thumbnailTimeInterval]; |
| 164 |
|
|
CGSize size = { 200, 200 }; |
| 165 |
|
|
|
| 166 |
|
|
if(!track) { |
| 167 |
|
|
XspfQTComponent *trackList = [component childAtIndex:0]; |
| 168 |
|
|
[trackList setSelectionIndex:0]; |
| 169 |
|
|
track = [trackList currentTrack ]; |
| 170 |
|
|
} |
| 171 |
|
|
|
| 172 |
|
|
NSImage *thumbnail = thumbnailForTrackTime(track, interval, size); |
| 173 |
|
|
|
| 174 |
|
|
return thumbnail; |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
masaki |
85 |
- (void)fire |
| 179 |
masaki |
2 |
{ |
| 180 |
|
|
id item = componentForURL(self.url); |
| 181 |
|
|
if(!item) return; |
| 182 |
masaki |
7 |
if([item childrenCount] == 0) return; |
| 183 |
masaki |
2 |
|
| 184 |
masaki |
7 |
id trackList = [item childAtIndex:0]; |
| 185 |
masaki |
71 |
self.object.movieNum = [NSNumber numberWithInt:[trackList childrenCount]]; |
| 186 |
|
|
self.object.thumbnail = thumbnailWithComponent(item); |
| 187 |
masaki |
2 |
} |
| 188 |
|
|
-(void)terminate |
| 189 |
|
|
{ |
| 190 |
|
|
[self doesNotRecognizeSelector:_cmd]; |
| 191 |
|
|
} |
| 192 |
|
|
@end |