2012-09-25 23 views
1

在我的應用程序,如我使用幀如何將捕獲的幀寫入套接字?

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 

委託我捕捉媒體,我已經記錄和播放使用的MPMoviePlayerController錄製的文件。現在我的問題是,我想將這些緩衝區寫入套接字併發送到服務器,而不是寫入文件。我需要做什麼樣的改變?

感謝您的幫助。

回答

0

,似乎你的問題包含兩個部分:

  1. 要通過幀捕獲框架,我用這個片段來自蘋果:(把它放在你提到captureOutput方法內:

    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); 
    
    // NSImage *image = [UIImage imageWithCGImage:newImage]; 
    // self.imgData = UIImageJPEGRepresentation(image , 1.0); //convert to NSData to send 
    CGImageRelease(newImage); 
    CVPixelBufferUnlockBaseAddress(imageBuffer, 0); 
    
  2. 對於網絡套接字通信,可以使用CocoaAsyncSocket。如果不需要套接字級別,也可以使用NSStream