0

Iam making an app which needs to record video and audio usingAVCaptureVideoDataOutputSampleBufferDelegate

the functions i use are :

    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)

    }
}

and the encode frame function :

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 ")
        }
}

the problem is that when I start recording (setting isCapturing flag to true) the first few frames get dropped (the reason is FrameWasLate) , the documentation of Apple says that its because the sampleBuffer doesn't get released fast enough ! , but all I do is initializing the theAvassetwriter nothing more ! .

I tried to put the encoding function in a serial Queue but it didn't work ! whats wrong ?!

user3703910
  • 624
  • 1
  • 5
  • 25
  • Hi did you get any solution? When I change device type front to back or back to front some of the frames dropped and freeze for a 3-4 sec which is noticeable easily. – Chanchal Warde Aug 30 '17 at 06:05
  • no I didn't , and also when you switch the cam , you disconnect the connections from it , so some frames are dropped . it freezes fro 0.5-1 second , not 3-4 . – user3703910 Aug 30 '17 at 10:55
  • But in my case its freezing for 3 to 4 sec. Are you working for filters on live camera ? – Chanchal Warde Aug 30 '17 at 11:15
  • for the moment no , but Iv'e worked on this , it down't have anything to do with the freeze , check if you do a lot of heavy operations when switching between cameras . – user3703910 Aug 30 '17 at 11:20
  • To check I just added print statement with time interval. I dont think its taking too much time. But my delegate calls after some seconds and this is not happening every time.--- Update camera 1504094120.50588 1504094120.50603, ---- Camera updated ------- delegate called 1504094120.53889 --------- delegate called 1504094129.54066 – Chanchal Warde Aug 30 '17 at 11:56
  • You can see its almost 9 sec here. But it happens randomly but very frequently. I also checked memory but its taking 9 MB with a filter applied and 5 MB with no filter applied in the delegate. – Chanchal Warde Aug 30 '17 at 11:57
  • I solved this. Now my it delays only 0.4 sec delay when switching camera and 2 sec when first time initialize. Thanks for relpy – Chanchal Warde Aug 31 '17 at 07:19
  • Can you please post how did you solve it ? @ChanchalWarde – Ankish Jain Sep 03 '21 at 15:05

0 Answers0