Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (hide annotations) (download)
Mon Nov 9 13:55:53 2009 UTC (14 years, 5 months ago) by masaki
File size: 6064 byte(s)
[New]  ワーカースレッド用のリクエストをファイルの内容の操作と、ファイル属性の調査の2つに分けた。
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     @interface XspfMMovieLoader : NSObject
16     {
17     QTMovie *result;
18     }
19     - (QTMovie *)loadedMovie;
20     - (void)loadOnMainThread:(id)array;
21     @end
22    
23     @implementation XspfMMovieLoader
24     - (void)loadOnMainThread:(id)url
25     {
26     NSError *error = nil;
27    
28     NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
29     url, QTMovieURLAttribute,
30     [NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute,
31     nil];
32     result = [[QTMovie alloc] initWithAttributes:attrs error:&error];
33     if (result == nil) {
34     if (error != nil) {
35     NSLog(@"Couldn't load movie URL, error = %@", error);
36     }
37     }
38     }
39     - (QTMovie *)loadedMovie { return result; }
40     - (void)dealoc
41     {
42     [result release];
43     [super dealloc];
44     }
45     @end
46    
47    
48     @implementation XspfMMovieLoadRequest
49    
50     @synthesize object;
51     @synthesize url;
52    
53     + (id)requestWithObject:(id)anObject url:(NSURL *)anUrl;
54     {
55     return [[[self alloc] initWithObject:anObject url:anUrl] autorelease];
56     }
57     - (id)initWithObject:(id)anObject url:(NSURL *)anUrl
58     {
59     self = [super init];
60     self.object = anObject;
61     self.url = anUrl;
62    
63     return self;
64     }
65    
66     static QTMovie *loadFromMovieURL(NSURL *url)
67     {
68     id loader = [[[XspfMMovieLoader alloc] init] autorelease];
69    
70     [loader performSelectorOnMainThread:@selector(loadOnMainThread:)
71     withObject:url
72     waitUntilDone:YES];
73    
74     return [loader loadedMovie];
75     }
76    
77     static XspfQTComponent *componentForURL(NSURL *url)
78     {
79     NSError *theErr = nil;
80    
81     NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url
82     options:0
83     error:&theErr] autorelease];
84     if(!d) {
85     if(theErr) {
86     NSLog(@"%@", theErr);
87     }
88     return nil;
89     }
90     NSXMLElement *root = [d rootElement];
91     XspfQTComponent *pl = [XspfQTComponent xspfComponemtWithXMLElement:root];
92     if(!pl) {
93     NSLog(@"Can not create XspfQTComponent.");
94     return nil;
95     }
96    
97     return pl;
98     }
99    
100     static inline NSSize maxSizeForFrame(NSSize size, CGSize frame)
101     {
102     NSSize result = size;
103     CGFloat aspectRetio = size.width / size.height;
104     CGFloat frameAspectRetio = frame.width / frame.height;
105    
106     if(aspectRetio > frameAspectRetio) {
107     result.width = frame.width;
108     result.height = result.width / aspectRetio;
109     } else {
110     result.height = frame.height;
111     result.width = result.height * aspectRetio;
112     }
113    
114     return result;
115     }
116    
117     /** ���������������������������������������������������������������������������������������������������������������������������������������������������
118     ** ������������������������������������������������������������
119     ** ���������������������������������������������������������������������
120     ** ������������������������������������������������������������
121     ** ���������������������������������������������������������������������������
122     **/
123     static QTTime calcDefaultThumbnailQTTime(QTMovie *movie)
124     {
125     XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease];
126    
127     NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute];
128     id pV = [t transformedValue:pTimeValue];
129     if([pV longValue] == 0) {
130     NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute];
131     id v = [t transformedValue:duration];
132    
133     double newPosterTime = 0;
134     double dDur = [v doubleValue];
135     if(dDur > 15 * 60) {
136     newPosterTime = dDur / 100;
137     } else if(dDur > 60) {
138     newPosterTime = 1;
139     }
140     pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]];
141     }
142    
143     return [pTimeValue QTTimeValue];
144     }
145     static NSImage *thumbnailForTrackTime(XspfQTComponent *track, NSTimeInterval time, CGSize size)
146     {
147     NSError *theErr = nil;
148     QTMovie *movie = loadFromMovieURL([track movieLocation]);
149    
150     NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute];
151     NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size);
152    
153     QTTime t;
154     if(time == DBL_MIN) {
155     t = calcDefaultThumbnailQTTime(movie);
156     } else {
157     t = QTMakeTimeWithTimeInterval(time);
158     }
159    
160     NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys:
161     QTMovieFrameImageTypeNSImage,QTMovieFrameImageType,
162     [NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize,
163     nil];
164    
165     NSImage *theImage = (NSImage *)[movie frameImageAtTime:t
166     withAttributes:imgProp
167     error:&theErr];
168     if (theImage == nil) {
169     if (theErr != nil) {
170     NSLog(@"Couldn't create CGImageRef, error = %@", theErr);
171     }
172     return NULL;
173     }
174    
175     return theImage;
176     }
177    
178     static NSImage *thumbnailWithComponent(XspfQTComponent *component)
179     {
180     XspfQTComponent *track = [component thumbnailTrack];
181     NSTimeInterval interval = [component thumbnailTimeInterval];
182     CGSize size = { 200, 200 };
183    
184     if(!track) {
185     XspfQTComponent *trackList = [component childAtIndex:0];
186     [trackList setSelectionIndex:0];
187     track = [trackList currentTrack ];
188     }
189    
190     NSImage *thumbnail = thumbnailForTrackTime(track, interval, size);
191    
192     return thumbnail;
193     }
194    
195 masaki 50 //- (void)setupFromFileAttribute
196     //{
197     // NSError *error = nil;
198     //
199     // id attrs = [[NSFileManager defaultManager ] attributesOfItemAtPath:[self.url path] error:&error];
200     // if(!attrs) {
201     // if(error) {
202     // NSLog(@"Error at registering XSPF. %@", error);
203     // }
204     // NSLog(@"Error at registering XSPF.");
205     // return;
206     // }
207     // id attr = [attrs fileModificationDate];
208     // if(attr) {
209     // [self.object setValue:attr forKey:@"modificationDate"];
210     // }
211     // attr = [attrs fileCreationDate];
212     // if(attr) {
213     // [self.object setValue:attr forKey:@"creationDate"];
214     // }
215     //}
216 masaki 2
217     - (void)operate
218     {
219 masaki 50 // [self setupFromFileAttribute];
220 masaki 33
221 masaki 2 id item = componentForURL(self.url);
222     if(!item) return;
223 masaki 7 if([item childrenCount] == 0) return;
224 masaki 2
225 masaki 7 id trackList = [item childAtIndex:0];
226     [self.object setValue:[NSNumber numberWithInt:[trackList childrenCount]] forKey:@"movieNum"];
227 masaki 2 [self.object setValue:thumbnailWithComponent(item) forKey:@"thumbnail"];
228     }
229     -(void)terminate
230     {
231     [self doesNotRecognizeSelector:_cmd];
232     }
233     @end

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