2014-03-13 95 views
15

我正在成功發送一個NSData流。下面的委託方法獲取該流並附加到NSMutableData self.data。我如何獲取這些數據並將其製作成UIView/AVCaptureVideoPreviewLayer(應該顯示視頻)?我覺得我錯過了另一個轉換,AVCaptureSession> NSStream> MCSession> NSStream>?將傳入的NSStream轉換爲視圖

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { 
    switch(eventCode) { 
     case NSStreamEventHasBytesAvailable: 
     { 
      if(!self.data) { 
       self.data = [NSMutableData data]; 
      } 
      uint8_t buf[1024]; 
      unsigned int len = 0; 
      len = [(NSInputStream *)stream read:buf maxLength:1024]; 
      if(len) { 
       [self.data appendBytes:(const void *)buf length:len]; 
      } else { 
       NSLog(@"no buffer!"); 
      } 

// Code here to take self.data and convert the NSData to UIView/Video 
} 

我這個送流:

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

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    CVPixelBufferLockBaseAddress(imageBuffer,0); 

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
// size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 
    void *src_buff = CVPixelBufferGetBaseAddress(imageBuffer); 

    NSData *data = [NSData dataWithBytes:src_buff length:bytesPerRow * height]; 

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0); 


    NSError *error; 
    self.oStream = [self.mySession startStreamWithName:@"videoOut" toPeer:[[self.mySession connectedPeers]objectAtIndex:0] error:&error]; 
    self.oStream.delegate = self; 
    [self.oStream scheduleInRunLoop:[NSRunLoop mainRunLoop] 
          forMode:NSDefaultRunLoopMode]; 
    [self.oStream open]; 

    [self.oStream write:[data bytes] maxLength:[data length]]; 






// CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

    CGSize imageSize = CVImageBufferGetEncodedSize(imageBuffer); 
    // also in the 'mediaSpecific' dict of the sampleBuffer 

    NSLog(@"frame captured at %.fx%.f", imageSize.width, imageSize.height); 
} 
+0

您可能想看看您是否可以使用Open GL。把你的數據,轉換成GL紋理,然後用GL來顯示它。這可能是一個更高級別的API。數據不是以任何標準格式? – nielsbot

+0

什麼是視頻格式?一個'UIView'?什麼是視頻的鏈接? – Larme

+0

視頻格式爲AVCaptureSession – Eric

回答

-2

您可以作出這樣的YOUT處理事件的一個的UIImageView:

UIImageView * iv = [[UIImageView alloc] initWithImage: [UIImage imageWithData: self.data];

您也可以ALLOC只有一次,只是調用init。

每當你從套接字接收到,你初始化UIImageView,你可以顯示它將UIImageView添加到UIView。

對不起,我的英語,我不知道如果我理解你

+0

這似乎不起作用。 – Eric

1

我想你需要AVCaptureManager,看看下面的代碼對你的作品..

AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init]; 
[self setCaptureManager:manager]; 

[[self captureManager] setDelegate:self]; 

if ([[self captureManager] setupSession]) { 
    // Create video preview layer and add it to the UI 
    AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]]; 
    UIView *view = self.videoPreviewView;//Add a view in XIB where you want to show video 
    CALayer *viewLayer = [view layer]; 
    [viewLayer setMasksToBounds:YES]; 
    CGRect bounds = [view bounds]; 

    [newCaptureVideoPreviewLayer setFrame:bounds]; 

    [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

    [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]]; 

    [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     [[[self captureManager] session] startRunning]; 
    }); 
} 

管理代表

- (void)captureManager:(AVCamCaptureManager *)captureManager didFailWithError:(NSError *)error 
{ 

} 

- (void)captureManagerRecordingBegan:(AVCamCaptureManager *)captureManager 
{ 

} 

- (void)captureManagerRecordingFinished:(AVCamCaptureManager *)captureManager outputURL:(NSURL *)url 
{ 



} 

- (void)captureManagerStillImageCaptured:(AVCamCaptureManager *)captureManager 
{ 



} 

- (void)captureManagerDeviceConfigurationChanged:(AVCamCaptureManager *)captureManager 
{ 

} 

我希望它有幫助。

+0

CaptureManager的委託方法都不能處理視頻。我錯過了什麼嗎? – Eric

+0

@Eric如果有幫助,請參閱https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112-Intro-DontLinkElementID_2。 – iphonic