| 1 |
// |
| 2 |
// XspfMMovieLoadRequest.m |
| 3 |
// XspfManager |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 09/11/01. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2009-2010, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2009-2010, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
1, Redistributions of source code must retain the above copyright |
| 40 |
notice, this list of conditions and the following disclaimer. |
| 41 |
2, Redistributions in binary form must reproduce the above copyright |
| 42 |
notice, this list of conditions and the following disclaimer in |
| 43 |
the documentation and/or other materials provided with the |
| 44 |
distribution. |
| 45 |
3, The names of its contributors may be used to endorse or promote |
| 46 |
products derived from this software without specific prior |
| 47 |
written permission. |
| 48 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 51 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 52 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 53 |
INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 54 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 55 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 56 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 58 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 59 |
POSSIBILITY OF SUCH DAMAGE. |
| 60 |
*/ |
| 61 |
|
| 62 |
#import "XspfMMovieLoadRequest.h" |
| 63 |
|
| 64 |
#import <QTKit/QTKit.h> |
| 65 |
#import "HMXSPFComponent.h" |
| 66 |
#import "XspfQTValueTransformers.h" |
| 67 |
#import "HMUtilities.h" |
| 68 |
|
| 69 |
|
| 70 |
@implementation XspfMMovieLoadRequest |
| 71 |
|
| 72 |
@synthesize object; |
| 73 |
|
| 74 |
+ (id)requestWithObject:(XspfMXspfObject *)anObject |
| 75 |
{ |
| 76 |
return [[[self alloc] initWithObject:anObject] autorelease]; |
| 77 |
} |
| 78 |
- (id)initWithObject:(XspfMXspfObject *)anObject |
| 79 |
{ |
| 80 |
self = [super init]; |
| 81 |
self.object = anObject; |
| 82 |
|
| 83 |
return self; |
| 84 |
} |
| 85 |
- (void)delloc |
| 86 |
{ |
| 87 |
self.object = nil; |
| 88 |
|
| 89 |
[super dealloc]; |
| 90 |
} |
| 91 |
- (NSTimeInterval)sleepTime |
| 92 |
{ |
| 93 |
return 1.0; |
| 94 |
} |
| 95 |
|
| 96 |
static QTMovie *loadFromMovieURL(NSURL *url) |
| 97 |
{ |
| 98 |
NSError *error = nil; |
| 99 |
|
| 100 |
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys: |
| 101 |
url, QTMovieURLAttribute, |
| 102 |
[NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute, |
| 103 |
nil]; |
| 104 |
QTMovie *result = [[QTMovie alloc] initWithAttributes:attrs error:&error]; |
| 105 |
if (result == nil) { |
| 106 |
if (error != nil) { |
| 107 |
HMLog(HMLogLevelError, @"Couldn't load movie URL, error = %@", error); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return result; |
| 112 |
} |
| 113 |
|
| 114 |
static HMXSPFComponent *componentForURL(NSURL *url) |
| 115 |
{ |
| 116 |
NSError *theErr = nil; |
| 117 |
|
| 118 |
NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url |
| 119 |
options:0 |
| 120 |
error:&theErr] autorelease]; |
| 121 |
if(!d) { |
| 122 |
HMLog(HMLogLevelError, @"Could not load XML."); |
| 123 |
if(theErr) { |
| 124 |
HMLog(HMLogLevelError, @"%@", theErr); |
| 125 |
} |
| 126 |
return nil; |
| 127 |
} |
| 128 |
NSXMLElement *root = [d rootElement]; |
| 129 |
HMXSPFComponent *pl = [HMXSPFComponent xspfComponentWithXMLElement:root]; |
| 130 |
if(!pl) { |
| 131 |
HMLog(HMLogLevelError, @"Can not create XspfQTComponent."); |
| 132 |
return nil; |
| 133 |
} |
| 134 |
|
| 135 |
return pl; |
| 136 |
} |
| 137 |
|
| 138 |
static inline NSSize maxSizeForFrame(NSSize size, CGSize frame) |
| 139 |
{ |
| 140 |
NSSize result = size; |
| 141 |
CGFloat aspectRetio = size.width / size.height; |
| 142 |
CGFloat frameAspectRetio = frame.width / frame.height; |
| 143 |
|
| 144 |
if(aspectRetio > frameAspectRetio) { |
| 145 |
result.width = frame.width; |
| 146 |
result.height = result.width / aspectRetio; |
| 147 |
} else { |
| 148 |
result.height = frame.height; |
| 149 |
result.width = result.height * aspectRetio; |
| 150 |
} |
| 151 |
|
| 152 |
return result; |
| 153 |
} |
| 154 |
|
| 155 |
/** はじめのフレームは真っ黒、あるいは真っ白である場合が多い。そのため以下の秒数のフレームを使用する。 |
| 156 |
** 0、ポスターフレームがあればそれを使用。 |
| 157 |
** 1、15分以上なら秒数で1%のフレームを使用。 |
| 158 |
** 2、1分以上なら1秒目のフレームを使用。 |
| 159 |
** 3、それらよりも短いときは0秒目のフレームを使用。 |
| 160 |
**/ |
| 161 |
static QTTime calcDefaultThumbnailQTTime(QTMovie *movie) |
| 162 |
{ |
| 163 |
XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease]; |
| 164 |
|
| 165 |
NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute]; |
| 166 |
id pV = [t transformedValue:pTimeValue]; |
| 167 |
if([pV longValue] == 0) { |
| 168 |
NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute]; |
| 169 |
id v = [t transformedValue:duration]; |
| 170 |
|
| 171 |
double newPosterTime = 0; |
| 172 |
double dDur = [v doubleValue]; |
| 173 |
if(dDur > 15 * 60) { |
| 174 |
newPosterTime = dDur / 100; |
| 175 |
} else if(dDur > 60) { |
| 176 |
newPosterTime = 1; |
| 177 |
} |
| 178 |
pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]]; |
| 179 |
} |
| 180 |
|
| 181 |
return [pTimeValue QTTimeValue]; |
| 182 |
} |
| 183 |
static NSImage *thumbnailForTrackTime(HMXSPFComponent *track, NSTimeInterval time, CGSize size) |
| 184 |
{ |
| 185 |
NSError *theErr = nil; |
| 186 |
QTMovie *movie = loadFromMovieURL(track.movieLocation); |
| 187 |
|
| 188 |
NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute]; |
| 189 |
NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size); |
| 190 |
|
| 191 |
QTTime t; |
| 192 |
if(time == DBL_MIN) { |
| 193 |
t = calcDefaultThumbnailQTTime(movie); |
| 194 |
} else { |
| 195 |
t = QTMakeTimeWithTimeInterval(time); |
| 196 |
} |
| 197 |
|
| 198 |
NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys: |
| 199 |
QTMovieFrameImageTypeNSImage,QTMovieFrameImageType, |
| 200 |
[NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize, |
| 201 |
nil]; |
| 202 |
|
| 203 |
NSImage *theImage = (NSImage *)[movie frameImageAtTime:t |
| 204 |
withAttributes:imgProp |
| 205 |
error:&theErr]; |
| 206 |
if (theImage == nil) { |
| 207 |
if (theErr != nil) { |
| 208 |
HMLog(HMLogLevelError, @"Couldn't create CGImageRef, error = %@", theErr); |
| 209 |
} |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
return theImage; |
| 214 |
} |
| 215 |
|
| 216 |
static NSImage *thumbnailWithComponent(HMXSPFComponent *component) |
| 217 |
{ |
| 218 |
HMXSPFComponent *track = component.thumbnailTrack; |
| 219 |
NSTimeInterval interval = component.thumbnailTimeInterval; |
| 220 |
CGSize size = { 200, 200 }; |
| 221 |
|
| 222 |
if(!track) { |
| 223 |
HMXSPFComponent *trackList = [component childAtIndex:0]; |
| 224 |
trackList.selectionIndex = 0; |
| 225 |
track = trackList.currentTrack; |
| 226 |
} |
| 227 |
|
| 228 |
NSImage *thumbnail = thumbnailForTrackTime(track, interval, size); |
| 229 |
|
| 230 |
if(!thumbnail) { |
| 231 |
thumbnail = [NSImage imageNamed:@"Icon-round-Question_mark"]; |
| 232 |
} |
| 233 |
|
| 234 |
return thumbnail; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
- (void)fire |
| 239 |
{ |
| 240 |
HMXSPFComponent *item = componentForURL(self.object.url); |
| 241 |
if(!item) return; |
| 242 |
if(item.childrenCount == 0) return; |
| 243 |
|
| 244 |
HMXSPFComponent *trackList = [item childAtIndex:0]; |
| 245 |
self.object.movieNum = [NSNumber numberWithInt:trackList.childrenCount]; |
| 246 |
self.object.thumbnail = thumbnailWithComponent(item); |
| 247 |
} |
| 248 |
-(void)terminate |
| 249 |
{ |
| 250 |
[self doesNotRecognizeSelector:_cmd]; |
| 251 |
} |
| 252 |
@end |