Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 568 - (hide annotations) (download)
Tue Jul 5 15:10:28 2011 UTC (12 years, 9 months ago) by masakih
File size: 8669 byte(s)
[Mod] HMUtilities.hをインポート
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 masakih 568 #import "HMUtilities.h"
68 masaki 2
69    
70     @implementation XspfMMovieLoadRequest
71    
72     @synthesize object;
73    
74 masaki 238 + (id)requestWithObject:(XspfMXspfObject *)anObject
75 masaki 2 {
76 masaki 96 return [[[self alloc] initWithObject:anObject] autorelease];
77 masaki 2 }
78 masaki 238 - (id)initWithObject:(XspfMXspfObject *)anObject
79 masaki 2 {
80     self = [super init];
81     self.object = anObject;
82    
83     return self;
84     }
85 masaki 89 - (void)delloc
86     {
87     self.object = nil;
88    
89     [super dealloc];
90     }
91 masaki 95 - (NSTimeInterval)sleepTime
92     {
93     return 1.0;
94     }
95 masaki 2
96     static QTMovie *loadFromMovieURL(NSURL *url)
97     {
98 masaki 88 NSError *error = nil;
99 masaki 2
100 masaki 88 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 masaki 188 HMLog(HMLogLevelError, @"Couldn't load movie URL, error = %@", error);
108 masaki 88 }
109     }
110 masaki 2
111 masaki 88 return result;
112 masaki 2 }
113    
114 masakih 344 static HMXSPFComponent *componentForURL(NSURL *url)
115 masaki 2 {
116     NSError *theErr = nil;
117    
118     NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url
119     options:0
120     error:&theErr] autorelease];
121     if(!d) {
122 masaki 188 HMLog(HMLogLevelError, @"Could not load XML.");
123 masaki 2 if(theErr) {
124 masaki 188 HMLog(HMLogLevelError, @"%@", theErr);
125 masaki 2 }
126     return nil;
127     }
128     NSXMLElement *root = [d rootElement];
129 masakih 393 HMXSPFComponent *pl = [HMXSPFComponent xspfComponentWithXMLElement:root];
130 masaki 2 if(!pl) {
131 masaki 188 HMLog(HMLogLevelError, @"Can not create XspfQTComponent.");
132 masaki 2 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 masakih 344 static NSImage *thumbnailForTrackTime(HMXSPFComponent *track, NSTimeInterval time, CGSize size)
184 masaki 2 {
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 masaki 188 HMLog(HMLogLevelError, @"Couldn't create CGImageRef, error = %@", theErr);
209 masaki 2 }
210     return NULL;
211     }
212    
213     return theImage;
214     }
215    
216 masakih 344 static NSImage *thumbnailWithComponent(HMXSPFComponent *component)
217 masaki 2 {
218 masakih 344 HMXSPFComponent *track = [component thumbnailTrack];
219 masaki 2 NSTimeInterval interval = [component thumbnailTimeInterval];
220     CGSize size = { 200, 200 };
221    
222     if(!track) {
223 masakih 344 HMXSPFComponent *trackList = [component childAtIndex:0];
224 masaki 2 [trackList setSelectionIndex:0];
225     track = [trackList currentTrack ];
226     }
227    
228     NSImage *thumbnail = thumbnailForTrackTime(track, interval, size);
229    
230 masakih 532 if(!thumbnail) {
231     thumbnail = [NSImage imageNamed:@"Icon-round-Question_mark"];
232     }
233    
234 masaki 2 return thumbnail;
235     }
236    
237    
238 masaki 85 - (void)fire
239 masaki 2 {
240 masaki 96 id item = componentForURL(self.object.url);
241 masaki 2 if(!item) return;
242 masaki 7 if([item childrenCount] == 0) return;
243 masaki 2
244 masaki 7 id trackList = [item childAtIndex:0];
245 masaki 71 self.object.movieNum = [NSNumber numberWithInt:[trackList childrenCount]];
246     self.object.thumbnail = thumbnailWithComponent(item);
247 masaki 2 }
248     -(void)terminate
249     {
250     [self doesNotRecognizeSelector:_cmd];
251     }
252     @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26