Advertisement
Guest User

Untitled

a guest
Apr 26th, 2011
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // variables
  3.         AVPlayer                *videoPlayer;
  4.         AVPlayerLayer           *videoLayer;
  5.         AVAssetReader           *videoReader;
  6.         AVAssetReaderTrackOutput*videoOutput;
  7.  
  8.  
  9.  
  10.  
  11.  
  12. // init
  13.         videoPlayer = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:videoPath.c_str()]]];
  14.  
  15.         if(videoPlayer == nil) {
  16.             NSLog(@"videoPlayer == nil ERROR LOADING %s\n", videoPath.c_str());
  17.         } else {
  18.             NSLog(@"videoPlayer: %@", videoPlayer);
  19.             videoLayer = [[AVPlayerLayer playerLayerWithPlayer:videoPlayer] retain];
  20.             videoLayer.frame = [ThreeDView instance].bounds;
  21.             // [[ThreeDView instance].layer addSublayer:videoLayer]; // test to see if it's loading and running
  22.  
  23.             AVAsset *asset = videoPlayer.currentItem.asset;
  24.             NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
  25.             NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA], (NSString*)kCVPixelBufferPixelFormatTypeKey, nil];
  26.  
  27.             videoReader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
  28.             videoOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:[tracks objectAtIndex:0] outputSettings:settings];
  29.             [videoReader addOutput:videoOutput];
  30.             [videoReader startReading];
  31.         }
  32.  
  33.  
  34. // draw loop
  35.         if(videoPlayer == 0) {
  36.             ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoPlayer == 0");
  37.             return;
  38.         }
  39.  
  40.         if(videoOutput == 0) {
  41.             ofLog(OF_LOG_WARNING, "Shot::drawVideo: videoOutput == 0");
  42.             return;
  43.         }
  44.  
  45.         CMSampleBufferRef sampleBuffer = [videoOutput copyNextSampleBuffer];
  46.  
  47.         if(sampleBuffer == 0) {
  48.             ofLog(OF_LOG_WARNING, "Shot::drawVideo: sampleBuffer == 0");
  49.             return;
  50.         }
  51.  
  52.         CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  53.         CFRelease(sampleBuffer);
  54.  
  55.         CVPixelBufferLockBaseAddress(imageBuffer,0);
  56.  
  57.         unsigned char *pixels = ( unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
  58.         int width = CVPixelBufferGetWidth(imageBuffer);
  59.         int height = CVPixelBufferGetHeight(imageBuffer);
  60.  
  61.         if(videoTexture.bAllocated() == false || videoTexture.getWidth() != width || videoTexture.getHeight() != height) {
  62.             NSLog(@"Shot::drawVideo() allocating texture %i, %i\n", width, height);
  63.             videoTexture.allocate(width, height, GL_RGBA, true);
  64.         }
  65.  
  66.         videoTexture.loadData(pixels, width, height, GL_BGRA);
  67.  
  68.         CVPixelBufferUnlockBaseAddress(imageBuffer,0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement