// // ElisMedia.m // Elis Colors // // Created by 柳 on 09/09/12. // Copyright 2009 __MyCompanyName__. All rights reserved. // #import "ElisMedia.h" @implementation ElisMedia // QuickTime X を試すためにかなり書き換えた。 - (id)initWithMovieFile:(NSString*)path { image = nil; movie = [[QTMovie alloc] initWithFile:path error:nil]; [movie gotoBeginning]; QTOpenGLTextureContextCreate(kCFAllocatorDefault, [[mainView openGLContext] CGLContextObj], [[mainView pixelFormat] CGLPixelFormatObj], NULL, &textureContext); SetMovieVisualContext([movie quickTimeMovie], textureContext); // [mainView setContext:textureContext]; attr = [NSDictionary dictionaryWithObjectsAndKeys: QTMovieFrameImageTypeCIImage, QTMovieFrameImageType, [NSNumber numberWithBool:YES], QTMovieFrameImageSessionMode, // 内部でキャッシュしろ [NSNumber numberWithBool:YES], QTMovieFrameImageHighQuality, // 画質低下を許さない [movie attributeForKey:QTMovieNaturalSizeAttribute], QTMovieFrameImageSize, // 勝手にリサイズするな [NSNumber numberWithBool:NO], QTMovieFrameImageSingleField, // インターレース解除? nil]; // [attr retain]; speed = 1.0; _path = path; playing = NO; return self; } - (id)initWithImageFile:(NSString*)path { movie = nil; textureContext = nil; sound = nil; image = [[CIImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path]]; speed = 1.0; _path = path; return self; } - (id)initWithSoundFile:(NSString*)path { movie = nil; image = nil; textureContext = nil; sound = [[QTMovie alloc] initWithFile:path error:nil]; [sound setVolume:0.5]; // あまりにうるさい。 speed = 1.0; _path = path; return self; } - (id)initWithText:(NSString*)t { NSImage* im; movie = nil; sound = nil; text = t; attr = [[NSMutableDictionary alloc] init]; // [attr retain]; [attr setValue:[NSFont fontWithName:@"HiraKakuPro-W3" size:24.0f] forKey:NSFontAttributeName]; [attr setValue:[NSColor colorWithCalibratedRed:255 green:255 blue:255 alpha:255] forKey:NSForegroundColorAttributeName]; im = [[NSImage alloc] initWithSize:NSMakeSize(640, 480)]; [im lockFocus]; [t drawInRect:NSMakeRect(0, 0, 640, 480) withAttributes:attr]; [im unlockFocus]; image = [[CIImage alloc] initWithData:[im TIFFRepresentation]]; // [im release]; speed = 1.0; return self; } - (id)initWithQuartzFile:(NSString*)path { return [self initWithMovieFile:path]; } // レガシーなので捨てました。 // もう一度試してる。 - (CIImage*)getFrameForTime:(CVTimeStamp*)timeStamp { CVImageBufferRef currentFrame; CIImage* ret; if(movie){ QTVisualContextCopyImageForTime(textureContext, NULL, timeStamp, ¤tFrame); ret = [CIImage imageWithCVImageBuffer:currentFrame]; CVOpenGLTextureRelease(currentFrame); return ret; }else if(image){ return image; } return nil; } - (CIImage*)getFrameForQTTime:(QTTime)time { // 64ビットでframeImageAtTimeメソッドは使えないみたい。どうしろと。 time.timeValue *= speed; if(movie) return [movie frameImageAtTime:time withAttributes:attr error:nil]; else if(image) return image; return nil; } - (void)releaseContext { if(movie) QTVisualContextTask(textureContext); } - (void)setCurrentTime:(QTTime)time { // time.timeValue *= speed; if(movie) [movie setAttribute:[NSValue valueWithQTTime:time] forKey:QTMovieCurrentTimeAttribute]; if(sound) [sound setCurrentTime:time]; } - (void)play { if(movie && !playing){ [movie play]; playing = YES; } if(sound && !playing){ [sound play]; playing = YES; } } - (void)stop { if(movie && YES){ [movie stop]; playing = NO; } if(sound && playing){ [sound stop]; playing = NO; } } - (QTTime)duration { if(movie) return [movie duration]; } - (void)finalize { if(movie){ NSLog(@"movie finalize"); [movie stop]; SetMovieVisualContext([movie quickTimeMovie], NULL); movie = nil; CFRelease(textureContext); } [super finalize]; } @end