ios didOutputSampleBuffer首先丢弃几帧

时间:2017-08-24 14:49:10

标签: ios avcapturesession avassetwriter cmsamplebuffer avcaptureoutput

我正在制作一款需要使用AVCaptureVideoDataOutputSampleBufferDelegate

录制视频和音频的应用

我使用的功能是:

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
{
    self.captureQueue.async {
        if !self.isCapturing
        {
            return
        }
        var isVideo = true
        if connection != self.videoConnection
        {
            isVideo = false
        }

        self.encoder!.encodeFrame(sampleBuffer: sampleBuffer, isVideo: isVideo)

    }
}

和编码帧功能:

func encodeFrame(sampleBuffer : CMSampleBuffer , isVideo : Bool)
{

        if (CMSampleBufferDataIsReady(sampleBuffer))
        {
            if self.writer.status == .unknown
            {
                print("INIT")
                let startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
                self.writer.startWriting()
                self.writer.startSession(atSourceTime: startTime)

            }
            if self.writer.status == .failed
            {
                print("writer failed : \(self.writer.error!)")
            }
            if isVideo
            {
                if self.writerVideoInput.isReadyForMoreMediaData
                {
                    if self.writerVideoInput.append(sampleBuffer)
                    {
                        print("writing video")
                    }
                    else
                    {
                        print("failed to append video")
                    }
                }
                else
                {
                    print("video input data isn't ready ")
                }
            }
            else
            {
                if self.writerAudioInput.isReadyForMoreMediaData
                {
                    if self.writerAudioInput.append(sampleBuffer)
                    {
                        print("writing audio")
                    }
                    else
                    {
                        print("failed to append audio")
                    }

                }
                else
                {
                    print("audio input isn't ready")
                }
            }
        }
        else
        {
            print("sample buffer isnt ready ")
        }
}

问题在于,当我开始录制(将isCapturing标志设置为true)时,前几帧被删除(原因是FrameWasLate),Apple的文档说它是因为sampleBuffer没有得到足够快的释放! ,但我所做的只是初始化Avassetwriter而已!

我试图将编码功能放在一个串行队列中,但它不起作用! 怎么了 ?!

0 个答案:

没有答案