示例#1
0
            public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
            {
                CMFormatDescription formatDescription = sampleBuffer.GetFormatDescription();

                if (connection == processor.videoConnection)
                {
                    // Get framerate
                    CMTime timestamp = sampleBuffer.PresentationTimeStamp;
                    CalculateFramerateAtTimestamp(timestamp);

                    // Get frame dimensions (for onscreen display)
                    if (processor.VideoDimensions.Width == 0 && processor.VideoDimensions.Height == 0)
                    {
                        processor.VideoDimensions = formatDescription.GetVideoPresentationDimensions(true, false);
                    }

                    // Get the buffer type
                    if (processor.VideoType == 0)
                    {
                        processor.VideoType = formatDescription.MediaSubType;
                    }
                    // TODO: processor.VideoType = (CMVideoCodecType)Enum.ToObject (typeof(CMVideoCodecType), formatDescription.MediaSubType);

                    // Synchronously process the pixel buffer to de-green it.
                    using (var pixelBuffer = sampleBuffer.GetImageBuffer())
                        ProcessPixelBuffer(pixelBuffer);

                    processor.previewBufferQueue.Enqueue(sampleBuffer);

                    //var writeBuffer = sampleBuffer.Duplicate ();
                    InvokeOnMainThread(() => {
                        var j = processor.previewBufferQueue.Dequeue();

                        var sbuf = j as CMSampleBuffer;
                        if (sbuf == null)
                        {
                            // Record the current sampleBuffer.ClassHandle
                            // Then run another iteration and on the next one, print the ClassHandle
                            Console.WriteLine("The type is {0}", new NSString(CFCopyDescription(j.Handle)));
                            return;
                        }

                        using (CVImageBuffer pixBuf = sbuf.GetImageBuffer()){
                            if (processor.PixelBufferReadyForDisplay != null)
                            {
                                processor.PixelBufferReadyForDisplay(pixBuf);
                            }
                        }

                        if (processor.assetWriter == null)
                        {
                            sbuf.Dispose();
                        }
                        else
                        {
                            processor.CompleteBufferUse(sbuf);
                        }
                    });
                }


                processor.movieWritingQueue.DispatchAsync(() => {
                    if (processor.assetWriter != null)
                    {
                        bool wasReadyToRecord = (processor.readyToRecordAudio && processor.readyToRecordVideo);

                        // Initialize the video input if this is not done yet
                        if (!processor.readyToRecordVideo)
                        {
                            processor.readyToRecordVideo = SetupAssetWriterVideoInput(formatDescription);
                        }

                        // Write the video data to file
                        if (processor.readyToRecordVideo && processor.readyToRecordAudio)
                        {
                            processor.WriteSampleBuffer(sampleBuffer, AVMediaType.Video);
                        }

                        bool isReadyToRecord = (processor.readyToRecordAudio && processor.readyToRecordVideo);

                        if (!wasReadyToRecord && isReadyToRecord)
                        {
                            processor.recordingWillBeStarted = false;
                            processor.IsRecording            = true;

                            if (processor.RecordingDidStart != null)
                            {
                                processor.RecordingDidStart();
                            }
                        }

                        processor.CompleteBufferUse(sampleBuffer);
                    }
                });
            }
        public virtual void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
        {
            CMFormatDescription formatDescription = sampleBuffer.GetFormatDescription();

            if (connection == videoConnection)
            {
                // Get framerate
                CMTime timestamp = sampleBuffer.PresentationTimeStamp;
                CalculateFramerateAtTimestamp(timestamp);

                // Get frame dimensions (for onscreen display)
                if (VideoDimensions.IsEmpty)
                {
                    VideoDimensions = formatDescription.GetVideoPresentationDimensions(true, false);
                }

                // Get the buffer type
                if (VideoType == 0)
                {
                    VideoType = formatDescription.MediaSubType;
                }

                // Synchronously process the pixel buffer to de-green it.
                using (var pixelBuffer = sampleBuffer.GetImageBuffer())
                    ProcessPixelBuffer(pixelBuffer);

                previewBufferQueue.Enqueue(sampleBuffer);

                //var writeBuffer = sampleBuffer.Duplicate ();
                InvokeOnMainThread(() => {
                    var j = previewBufferQueue.Dequeue();

                    var sbuf = j as CMSampleBuffer;
                    if (sbuf == null)
                    {
#if DEBUG
                        // Record the current sampleBuffer.ClassHandle
                        // Then run another iteration and on the next one, print the ClassHandle
                        Console.WriteLine("The type is {0}", new NSString(CFCopyDescription(j.Handle)));
#endif
                        return;
                    }

                    using (CVImageBuffer pixBuf = sbuf.GetImageBuffer()) {
                        if (PixelBufferReadyForDisplay != null)
                        {
                            PixelBufferReadyForDisplay(pixBuf);
                        }
                    }
                });
            }
            // keep a reference to 'sampleBuffer', movieWritingQueue will remove it
            CompleteBufferUse(sampleBuffer);

            movieWritingQueue.DispatchAsync(() => {
                if (assetWriter != null)
                {
                    bool wasReadyToRecord = (readyToRecordAudio && readyToRecordVideo);

                    if (connection == videoConnection)
                    {
                        // Initialize the video input if this is not done yet
                        if (!readyToRecordVideo)
                        {
                            readyToRecordVideo = SetupAssetWriterVideoInput(formatDescription);
                        }

                        // Write the video data to file
                        if (readyToRecordVideo && readyToRecordAudio)
                        {
                            WriteSampleBuffer(sampleBuffer, AVMediaType.Video);
                        }
                    }
                    else if (connection == audioConnection)
                    {
                        if (!readyToRecordAudio)
                        {
                            readyToRecordAudio = SetupAssetWriterAudioInput(formatDescription);
                        }

                        if (readyToRecordAudio && readyToRecordVideo)
                        {
                            WriteSampleBuffer(sampleBuffer, AVMediaType.Audio);
                        }
                    }
                    bool isReadyToRecord = (readyToRecordAudio && readyToRecordVideo);

                    if (!wasReadyToRecord && isReadyToRecord)
                    {
                        recordingWillBeStarted = false;
                        IsRecording            = true;

                        if (RecordingDidStart != null)
                        {
                            RecordingDidStart();
                        }
                    }
                }
                CompleteBufferUse(sampleBuffer);
            });
        }