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