Skip to content

Instantly share code, notes, and snippets.

@egradman
Created April 26, 2011 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save egradman/943418 to your computer and use it in GitHub Desktop.
Save egradman/943418 to your computer and use it in GitHub Desktop.
cocos2d video sprite (batch node)
@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
@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
@refresh
Copy link

refresh commented Sep 5, 2012

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

@refresh
Copy link

refresh commented Sep 5, 2012

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