Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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