Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 85 - (hide annotations) (download)
Thu Nov 19 11:44:30 2009 UTC (14 years, 5 months ago) by masaki
File size: 5548 byte(s)
[New] メインスレッドで実行するリクエストと別スレッドで実行するリクエストに分け統合管理するようにした。
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 masaki 71 + (id)requestWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl;
54 masaki 2 {
55     return [[[self alloc] initWithObject:anObject url:anUrl] autorelease];
56     }
57 masaki 71 - (id)initWithObject:(XSPFMXspfObject *)anObject url:(NSURL *)anUrl
58 masaki 2 {
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 masaki 85 // [loader performSelectorOnMainThread:@selector(loadOnMainThread:)
71     // withObject:url
72     // waitUntilDone:YES];
73     [loader performSelector:@selector(loadOnMainThread:)
74     withObject:url];
75 masaki 2
76     return [loader loadedMovie];
77     }
78    
79     static XspfQTComponent *componentForURL(NSURL *url)
80     {
81     NSError *theErr = nil;
82    
83     NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url
84     options:0
85     error:&theErr] autorelease];
86     if(!d) {
87     if(theErr) {
88     NSLog(@"%@", theErr);
89     }
90     return nil;
91     }
92     NSXMLElement *root = [d rootElement];
93     XspfQTComponent *pl = [XspfQTComponent xspfComponemtWithXMLElement:root];
94     if(!pl) {
95     NSLog(@"Can not create XspfQTComponent.");
96     return nil;
97     }
98    
99     return pl;
100     }
101    
102     static inline NSSize maxSizeForFrame(NSSize size, CGSize frame)
103     {
104     NSSize result = size;
105     CGFloat aspectRetio = size.width / size.height;
106     CGFloat frameAspectRetio = frame.width / frame.height;
107    
108     if(aspectRetio > frameAspectRetio) {
109     result.width = frame.width;
110     result.height = result.width / aspectRetio;
111     } else {
112     result.height = frame.height;
113     result.width = result.height * aspectRetio;
114     }
115    
116     return result;
117     }
118    
119     /** ���������������������������������������������������������������������������������������������������������������������������������������������������
120     ** ������������������������������������������������������������
121     ** ���������������������������������������������������������������������
122     ** ������������������������������������������������������������
123     ** ���������������������������������������������������������������������������
124     **/
125     static QTTime calcDefaultThumbnailQTTime(QTMovie *movie)
126     {
127     XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease];
128    
129     NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute];
130     id pV = [t transformedValue:pTimeValue];
131     if([pV longValue] == 0) {
132     NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute];
133     id v = [t transformedValue:duration];
134    
135     double newPosterTime = 0;
136     double dDur = [v doubleValue];
137     if(dDur > 15 * 60) {
138     newPosterTime = dDur / 100;
139     } else if(dDur > 60) {
140     newPosterTime = 1;
141     }
142     pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]];
143     }
144    
145     return [pTimeValue QTTimeValue];
146     }
147     static NSImage *thumbnailForTrackTime(XspfQTComponent *track, NSTimeInterval time, CGSize size)
148     {
149     NSError *theErr = nil;
150     QTMovie *movie = loadFromMovieURL([track movieLocation]);
151    
152     NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute];
153     NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size);
154    
155     QTTime t;
156     if(time == DBL_MIN) {
157     t = calcDefaultThumbnailQTTime(movie);
158     } else {
159     t = QTMakeTimeWithTimeInterval(time);
160     }
161    
162     NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys:
163     QTMovieFrameImageTypeNSImage,QTMovieFrameImageType,
164     [NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize,
165     nil];
166    
167     NSImage *theImage = (NSImage *)[movie frameImageAtTime:t
168     withAttributes:imgProp
169     error:&theErr];
170     if (theImage == nil) {
171     if (theErr != nil) {
172     NSLog(@"Couldn't create CGImageRef, error = %@", theErr);
173     }
174     return NULL;
175     }
176    
177     return theImage;
178     }
179    
180     static NSImage *thumbnailWithComponent(XspfQTComponent *component)
181     {
182     XspfQTComponent *track = [component thumbnailTrack];
183     NSTimeInterval interval = [component thumbnailTimeInterval];
184     CGSize size = { 200, 200 };
185    
186     if(!track) {
187     XspfQTComponent *trackList = [component childAtIndex:0];
188     [trackList setSelectionIndex:0];
189     track = [trackList currentTrack ];
190     }
191    
192     NSImage *thumbnail = thumbnailForTrackTime(track, interval, size);
193    
194     return thumbnail;
195     }
196    
197    
198 masaki 85 - (void)fire
199 masaki 2 {
200     id item = componentForURL(self.url);
201     if(!item) return;
202 masaki 7 if([item childrenCount] == 0) return;
203 masaki 2
204 masaki 7 id trackList = [item childAtIndex:0];
205 masaki 71 self.object.movieNum = [NSNumber numberWithInt:[trackList childrenCount]];
206     self.object.thumbnail = thumbnailWithComponent(item);
207 masaki 2 }
208     -(void)terminate
209     {
210     [self doesNotRecognizeSelector:_cmd];
211     }
212     @end

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