AVFoundation: agrega text al marco de video CMSampleBufferRef

Estoy construyendo una aplicación con AVFoundation.

Justo antes de llamar a [assetWriterInput appendSampleBuffer:sampleBuffer] en - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection -method.

Manejo de los píxeles en el búfer de muestra (utilizando un pixelbuffer para aplicar un efecto).

Pero el cliente quiere que coloque un text (timestamp y framecounter) en los cuadros, pero aún no he encontrado una manera de hacerlo.

Traté de convertir el buffer de muestra a una image, aplico text en la image y convertir la image a un buffer de muestra, pero luego

 CMSampleBufferDataIsReady(sampleBuffer) 

falla

Aquí están mis methods de categoría UIImage:

  + (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer { CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress(imageBuffer,0); uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); size_t width = CVPixelBufferGetWidth(imageBuffer); size_t height = CVPixelBufferGetHeight(imageBuffer); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); CGImageRef newImage = CGBitmapContextCreateImage(newContext); CGContextRelease(newContext); CGColorSpaceRelease(colorSpace); UIImage *newUIImage = [UIImage imageWithCGImage:newImage]; CFRelease(newImage); return newUIImage; } 

Y

  - (CMSampleBufferRef) cmSampleBuffer { CGImageRef image = self.CGImage; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil]; CVPixelBufferRef pxbuffer = NULL; CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, self.size.width, self.size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, &pxbuffer); NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); CVPixelBufferLockBaseAddress(pxbuffer, 0); void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); NSParameterAssert(pxdata != NULL); CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pxdata, self.size.width, self.size.height, 8, 4*self.size.width, rgbColorSpace, kCGImageAlphaNoneSkipFirst); NSParameterAssert(context); CGContextConcatCTM(context, CGAffineTransformMakeRotation(0)); CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image); CGColorSpaceRelease(rgbColorSpace); CGContextRelease(context); CVPixelBufferUnlockBaseAddress(pxbuffer, 0); CMVideoFormatDescriptionRef videoInfo = NULL; CMSampleBufferRef sampleBuffer = NULL; CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pxbuffer, true, NULL, NULL, videoInfo, NULL, &sampleBuffer); return sampleBuffer; } 

¿Algunas ideas?

EDITAR:

Cambié mi código con la respuesta de Tony. (¡Gracias!) Este código funciona:

 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVPixelBufferLockBaseAddress( pixelBuffer, 0 ); EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; CIContext *ciContext = [CIContext contextWithEAGLContext:eaglContext options:@{kCIContextWorkingColorSpace : [NSNull null]} ]; UIFont *font = [UIFont fontWithName:@"Helvetica" size:40]; NSDictionary *attributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor lightTextColor]}; UIImage *img = [UIImage imageFromText:@"01 - 13/02/2014 15:18:21:654" withAttributes:attributes]; CIImage *filtenetworkingImage = [[CIImage alloc] initWithCGImage:img.CGImage]; [ciContext render:filtenetworkingImage toCVPixelBuffer:pixelBuffer bounds:[filtenetworkingImage extent] colorSpace:CGColorSpaceCreateDeviceRGB()]; CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); 

Debe referir la muestra CIFunHouse de apple, y puede usar esta API para dibujar directamente en el búfer

-(void)render:(CIImage *)image toCVPixelBuffer:(CVPixelBufferRef)buffer bounds:(CGRect)r colorSpace:(CGColorSpaceRef)cs

Puede downloadlo aquí WWDC2013

Crea el context

 _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; _ciContext = [CIContext contextWithEAGLContext:_eaglContext options:@{kCIContextWorkingColorSpace : [NSNull null]} ]; 

Ahora renderice la image

 CVPixelBufferRef rendenetworkingOutputPixelBuffer = NULL; OSStatus err = CVPixelBufferPoolCreatePixelBuffer(nil, self.pixelBufferAdaptor.pixelBufferPool, &rendenetworkingOutputPixelBuffer); [_ciContext render:filtenetworkingImage toCVPixelBuffer:rendenetworkingOutputPixelBuffer bounds:[filtenetworkingImage extent]