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 ?!