Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMMovieLoadRequest.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (hide annotations) (download)
Sun Jan 10 13:19:26 2010 UTC (14 years, 3 months ago) by masaki
File size: 5094 byte(s)
[Refactoring] クラス名変更。XSPFMXspfObject -> XpsfMXspfObject
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    
16     @implementation XspfMMovieLoadRequest
17    
18     @synthesize object;
19    
20 masaki 238 + (id)requestWithObject:(XspfMXspfObject *)anObject
21 masaki 2 {
22 masaki 96 return [[[self alloc] initWithObject:anObject] autorelease];
23 masaki 2 }
24 masaki 238 - (id)initWithObject:(XspfMXspfObject *)anObject
25 masaki 2 {
26     self = [super init];
27     self.object = anObject;
28    
29     return self;
30     }
31 masaki 89 - (void)delloc
32     {
33     self.object = nil;
34    
35     [super dealloc];
36     }
37 masaki 95 - (NSTimeInterval)sleepTime
38     {
39     return 1.0;
40     }
41 masaki 2
42     static QTMovie *loadFromMovieURL(NSURL *url)
43     {
44 masaki 88 NSError *error = nil;
45 masaki 2
46 masaki 88 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
47     url, QTMovieURLAttribute,
48     [NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute,
49     nil];
50     QTMovie *result = [[QTMovie alloc] initWithAttributes:attrs error:&error];
51     if (result == nil) {
52     if (error != nil) {
53 masaki 188 HMLog(HMLogLevelError, @"Couldn't load movie URL, error = %@", error);
54 masaki 88 }
55     }
56 masaki 2
57 masaki 88 return result;
58 masaki 2 }
59    
60     static XspfQTComponent *componentForURL(NSURL *url)
61     {
62     NSError *theErr = nil;
63    
64     NSXMLDocument *d = [[[NSXMLDocument alloc] initWithContentsOfURL:url
65     options:0
66     error:&theErr] autorelease];
67     if(!d) {
68 masaki 188 HMLog(HMLogLevelError, @"Could not load XML.");
69 masaki 2 if(theErr) {
70 masaki 188 HMLog(HMLogLevelError, @"%@", theErr);
71 masaki 2 }
72     return nil;
73     }
74     NSXMLElement *root = [d rootElement];
75     XspfQTComponent *pl = [XspfQTComponent xspfComponemtWithXMLElement:root];
76     if(!pl) {
77 masaki 188 HMLog(HMLogLevelError, @"Can not create XspfQTComponent.");
78 masaki 2 return nil;
79     }
80    
81     return pl;
82     }
83    
84     static inline NSSize maxSizeForFrame(NSSize size, CGSize frame)
85     {
86     NSSize result = size;
87     CGFloat aspectRetio = size.width / size.height;
88     CGFloat frameAspectRetio = frame.width / frame.height;
89    
90     if(aspectRetio > frameAspectRetio) {
91     result.width = frame.width;
92     result.height = result.width / aspectRetio;
93     } else {
94     result.height = frame.height;
95     result.width = result.height * aspectRetio;
96     }
97    
98     return result;
99     }
100    
101     /** ���������������������������������������������������������������������������������������������������������������������������������������������������
102     ** ������������������������������������������������������������
103     ** ���������������������������������������������������������������������
104     ** ������������������������������������������������������������
105     ** ���������������������������������������������������������������������������
106     **/
107     static QTTime calcDefaultThumbnailQTTime(QTMovie *movie)
108     {
109     XspfQTTimeTransformer *t = [[[XspfQTTimeTransformer alloc] init] autorelease];
110    
111     NSValue *pTimeValue = [movie attributeForKey:QTMoviePosterTimeAttribute];
112     id pV = [t transformedValue:pTimeValue];
113     if([pV longValue] == 0) {
114     NSValue *duration = [movie attributeForKey:QTMovieDurationAttribute];
115     id v = [t transformedValue:duration];
116    
117     double newPosterTime = 0;
118     double dDur = [v doubleValue];
119     if(dDur > 15 * 60) {
120     newPosterTime = dDur / 100;
121     } else if(dDur > 60) {
122     newPosterTime = 1;
123     }
124     pTimeValue = [t reverseTransformedValue:[NSNumber numberWithDouble:newPosterTime]];
125     }
126    
127     return [pTimeValue QTTimeValue];
128     }
129     static NSImage *thumbnailForTrackTime(XspfQTComponent *track, NSTimeInterval time, CGSize size)
130     {
131     NSError *theErr = nil;
132     QTMovie *movie = loadFromMovieURL([track movieLocation]);
133    
134     NSValue *sizeValue = [movie attributeForKey:QTMovieNaturalSizeAttribute];
135     NSSize newMaxSize = maxSizeForFrame([sizeValue sizeValue], size);
136    
137     QTTime t;
138     if(time == DBL_MIN) {
139     t = calcDefaultThumbnailQTTime(movie);
140     } else {
141     t = QTMakeTimeWithTimeInterval(time);
142     }
143    
144     NSDictionary *imgProp = [NSDictionary dictionaryWithObjectsAndKeys:
145     QTMovieFrameImageTypeNSImage,QTMovieFrameImageType,
146     [NSValue valueWithSize:newMaxSize], QTMovieFrameImageSize,
147     nil];
148    
149     NSImage *theImage = (NSImage *)[movie frameImageAtTime:t
150     withAttributes:imgProp
151     error:&theErr];
152     if (theImage == nil) {
153     if (theErr != nil) {
154 masaki 188 HMLog(HMLogLevelError, @"Couldn't create CGImageRef, error = %@", theErr);
155 masaki 2 }
156     return NULL;
157     }
158    
159     return theImage;
160     }
161    
162     static NSImage *thumbnailWithComponent(XspfQTComponent *component)
163     {
164     XspfQTComponent *track = [component thumbnailTrack];
165     NSTimeInterval interval = [component thumbnailTimeInterval];
166     CGSize size = { 200, 200 };
167    
168     if(!track) {
169     XspfQTComponent *trackList = [component childAtIndex:0];
170     [trackList setSelectionIndex:0];
171     track = [trackList currentTrack ];
172     }
173    
174     NSImage *thumbnail = thumbnailForTrackTime(track, interval, size);
175    
176     return thumbnail;
177     }
178    
179    
180 masaki 85 - (void)fire
181 masaki 2 {
182 masaki 96 id item = componentForURL(self.object.url);
183 masaki 2 if(!item) return;
184 masaki 7 if([item childrenCount] == 0) return;
185 masaki 2
186 masaki 7 id trackList = [item childAtIndex:0];
187 masaki 71 self.object.movieNum = [NSNumber numberWithInt:[trackList childrenCount]];
188     self.object.thumbnail = thumbnailWithComponent(item);
189 masaki 2 }
190     -(void)terminate
191     {
192     [self doesNotRecognizeSelector:_cmd];
193     }
194     @end

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