2012-06-17 21 views
13

iPad2の前面と背面のカメラのストリームを2つのUIViewに並べて表示します。私はどちらかのカメラのために正常に動作し、次のコード複数のAVCaptureSessionsを実行するか複数の入力を追加する

AVCaptureDeviceInput *captureInputFront = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
session addInput:captureInputFront]; 
session setSessionPreset:AVCaptureSessionPresetMedium]; 
session startRunning]; 

AVCaptureVideoPreviewLayer *prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; 
prevLayer.frame = self.view.frame; 
[self.view.layer addSublayer:prevLayer]; 

を使用する一つのデバイスの画像をストリームに 。 ストリームを並行して表示するには別のセッションを作成しようとしましたが、2番目のセッションが確立されるとすぐに最初のセッションがフリーズします。

次に、2つのAVCaptureDeviceInputをセッションに追加しようとしましたが、現時点で最大1つの入力がサポートされているようです。

両方のカメラからストリーミングする方法を教えてください。

+0

[セッションを再作成せずに2番目のAVCaptureSessionでオートフォーカスを動作させるにはどうすればよいですか?](http://stackoverflow.com/questions/5427561/how-can-i-get-autofocus-to-work -in-a-second-avcapturesession-without-recreating) –

答えて

13

それは、は手動AVCaptureConnectionオブジェクトのセットアップにあなたが持っているのMacOS X上で複数のビデオ機器からCMSampleBufferRef Sを得ることが可能です。たとえば、あなたがこれらのオブジェクトを持っていると仮定すると:

代わり
[session addOutput:videoOutput1]; 
[session addOutput:videoOutput2]; 

、それらを追加し、任意の接続をしないように、セッションを教えて:

AVCaptureSession *session; 
AVCaptureInput *videoInput1; 
AVCaptureInput *videoInput2; 
AVCaptureVideoDataOutput *videoOutput1; 
AVCaptureVideoDataOutput *videoOutput2; 

ませこのような出力を追加しています

[session addOutputWithNoConnections:videoOutput1]; 
[session addOutputWithNoConnections:videoOutput2]; 

次に、各入力/出力ペアについて、入力のビデオポートから出力に接続します手動:

for (AVCaptureInputPort *port in [videoInput1 ports]) { 
    if ([[port mediaType] isEqualToString:AVMediaTypeVideo]) { 
     AVCaptureConnection* cxn = [AVCaptureConnection 
      connectionWithInputPorts:[NSArray arrayWithObject:port] 
      output:videoOutput1 
     ]; 
     if ([session canAddConnection:cxn]) { 
      [session addConnection:cxn]; 
     } 
     break; 
    } 
} 

最後に、両方の出力用サンプルバッファーのデリゲートを設定してください:

[videoOutput1 setSampleBufferDelegate:self queue:someDispatchQueue]; 
[videoOutput2 setSampleBufferDelegate:self queue:someDispatchQueue]; 

、今あなたが両方のデバイスからのフレームを処理することができるはずです。

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    if (captureOutput == videoOutput1) 
    { 
     // handle frames from first device 
    } 
    else if (captureOutput == videoOutput2) 
    { 
     // handle frames from second device 
    } 
} 

参照してください。複数のビデオデバイスからのライブプレビューを結合する例については、AVVideoWall sample projectを参照してください。

+0

ありがとうございました。私もする必要があった: [セッションaddInputWithNoConnections:videoInput1]; [セッションのaddInputWithNoConnections:videoInput2]; –

+6

iOS 10では動作しません - セッションへの2番目の入力を追加できません:キャッチされていない例外 'NSInvalidArgumentException'、理由: '*** - [AVCaptureSession addInputWithNoConnections:] –

関連する問題