Created
April 26, 2011 23:39
-
-
Save egradman/943418 to your computer and use it in GitHub Desktop.
cocos2d video sprite (batch node)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface VideoSpriteBatchNode: CCSpriteBatchNode | |
{ | |
AVURLAsset *asset; | |
AVAssetTrack *videoTrack; | |
AVAssetReader *assetReader; | |
AVAssetReaderTrackOutput *trackOutput; | |
} | |
@property(nonatomic,retain) AVURLAsset *asset; | |
@property(nonatomic,retain) AVAssetTrack *videoTrack; | |
@property(nonatomic,retain) AVAssetReader *assetReader; | |
@property(nonatomic,retain) AVAssetReaderTrackOutput *trackOutput; | |
+ (id)batchNodeWithVideo:(NSString *)name; | |
- (id)initWithVideo:(NSString *)name; | |
- (void)updateTexture; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation VideoSpriteBatchNode | |
@synthesize asset; | |
@synthesize videoTrack; | |
@synthesize assetReader; | |
@synthesize trackOutput; | |
+ (id)batchNodeWithVideo:(NSString *)name | |
{ | |
VideoSpriteBatchNode *sprite = [[VideoSpriteBatchNode alloc] initWithVideo:name]; | |
return sprite; | |
} | |
- (id)initWithVideo:(NSString *)name | |
{ | |
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:name withExtension:nil]; | |
self.asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; | |
self.videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
[self rewindAssetReader]; | |
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer([self.trackOutput copyNextSampleBuffer]); | |
CVPixelBufferLockBaseAddress(imageBuffer,0); | |
/*Get information about the image*/ | |
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); | |
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); | |
size_t width = CVPixelBufferGetWidth(imageBuffer); | |
size_t height = CVPixelBufferGetHeight(imageBuffer); | |
CCTexture2D *texture = [[[CCTexture2D alloc] initWithData:baseAddress | |
pixelFormat:kCCTexture2DPixelFormat_RGBA8888 | |
pixelsWide:width | |
pixelsHigh:height | |
contentSize:CGSizeMake(width,height) | |
] autorelease]; | |
/*We unlock the image buffer*/ | |
CVPixelBufferUnlockBaseAddress(imageBuffer,0); | |
[self initWithTexture:texture capacity:16]; | |
// schedule texture updates for the frame duration (1/freq) | |
[self schedule:@selector(updateTexture) interval:1.0/self.videoTrack.nominalFrameRate]; | |
return self; | |
} | |
- (void)rewindAssetReader | |
{ | |
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], (NSString*)kCVPixelBufferPixelFormatTypeKey, | |
nil]; | |
self.trackOutput = nil; | |
self.trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:settings]; | |
NSError *error = [[NSError alloc] autorelease]; | |
self.assetReader = nil; | |
self.assetReader = [AVAssetReader assetReaderWithAsset:asset error:&error]; | |
[assetReader addOutput:trackOutput]; | |
[assetReader startReading]; | |
} | |
- (void)updateTexture | |
{ | |
if (self.assetReader.status == AVAssetReaderStatusCompleted) { | |
// this texture should repeat from the beginning | |
[self rewindAssetReader]; | |
} | |
CMSampleBufferRef *sampleBuffer = [trackOutput copyNextSampleBuffer]; | |
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); | |
CVPixelBufferLockBaseAddress(imageBuffer,0); | |
/*Get information about the image*/ | |
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); | |
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); | |
size_t width = CVPixelBufferGetWidth(imageBuffer); | |
size_t height = CVPixelBufferGetHeight(imageBuffer); | |
// update the texture | |
glBindTexture(GL_TEXTURE_2D, self.texture.name); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, baseAddress); | |
/*We unlock the image buffer*/ | |
CVPixelBufferUnlockBaseAddress(imageBuffer,0); | |
[sampleBuffer release]; | |
} | |
@end |
Nevermind I got it working, turns out I didn't have the spriteWithBatchNode rect set up to the right size for the video, wow this is really cool!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this looks really neat thanks for sharing, I'm trying to get video frames going in cocos2d and came across this. I'm a little confused on how to implement though, it seems like I need to add a CCSprite to the VideoBatchNode to use the video texture but I'm unsure what to init the sprite with as far as frame name goes. Or maybe I need to create a CCAnimation and use that. By chance does anyone have any advice or snippets on a simple implementation? I've got it compiling and it is reading the video but I can't seem to get the frames displayed on a sprite. Thanks