iosobjective-cvideo-captureavassetwritercmtime

AVAssetWriter startSessionAtSourceTime not accepting CMTIme value


My app is designed to record video & analyze the frames generated under iOS 11.4, using Xcode 10.0 as IDE. Succeeded in recording video using AVCaptureMovieFileOutput, but need to analyze frames so transitioned to AVAssetWriter and modeled code after RosyWriter [ https://github.com/WildDylan/appleSample/tree/master/RosyWriter ]. Code is written in ObjC.

I am stuck with problem inside captureOutput: didOutputSampleBuffer: fromConnection: delegate. After capturing first frame, the AVAssetWriter is configured along with its inputs (video and audio),using settings extracted from first frame. Once user selects record, the captured sampleBuffer is analyzed and written. I tried to use AVAssetWriter startSessionAtSourceTime: but there is clearly something wrong with the way CMSampleBufferGetPresentationTimeStamp is returning CMTime from the sample buffer. The sampleBuufer log seems to show CMTime with valid values.

If I implement: CMTime sampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); [self->assetWriter startSessionAtSourceTime: sampleTime] the error generated is '*** -[AVAssetWriter startSessionAtSourceTime:] invalid parameter not satisfying: CMTIME_IS_NUMERIC(startTime)' .

If I use [self->assetWriter startSessionAtSourceTime:kCMTimeZero] the error "warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available." is generated.

When I log sampleTime I read - value=0, timescale=0, epoch=0 & flags=0. I also log the sampleBuffer and show it below, followed by the relevant code:

SampleBuffer Content = 

2018-10-17 12:07:04.540816+0300 MyApp[10664:2111852] -[CameraCaptureManager captureOutput:didOutputSampleBuffer:fromConnection:] : sampleBuffer - CMSampleBuffer 0x100e388c0 retainCount: 1 allocator: 0x1c03a95e0
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
buffer-level attachments:
    Orientation(P) = 1
    {Exif}    (P) = <CFBasicHash 0x28161ce80 [0x1c03a95e0]>{type = mutable dict, count = 24,
entries => .....A LOT OF CAMERA DATA HERE.....
}

    DPIWidth  (P) = 72
    {TIFF}    (P) = <CFBasicHash 0x28161c540 [0x1c03a95e0]>{type =    mutable dict, count = 7,
entries => .....MORE CAMERA DATA HERE.....
}

    DPIHeight (P) = 72
    {MakerApple}(P) = {
1 = 3;
10 = 0;
14 = 0;
3 =     {
    epoch = 0;
    flags = 1;
    timescale = 1000000000;
    value = 390750488472916;
};
4 = 0;
5 = 221;
6 = 211;
7 = 1;
8 =     (
    "-0.04894018",
    "-0.6889497",
    "-0.7034443"
);
9 = 0;
}
formatDescription = <CMVideoFormatDescription 0x280ddc780 [0x1c03a95e0]> {
mediaType:'vide' 
mediaSubType:'BGRA' 
mediaSpecific: {
    codecType: 'BGRA'       dimensions: 720 x 1280 
} 
extensions: {<CFBasicHash 0x28161f880 [0x1c03a95e0]>{type = immutable dict, count = 5,
entries =>
0 : <CFString 0x1c0917068 [0x1c03a95e0]>{contents = "CVImageBufferYCbCrMatrix"} = <CFString 0x1c09170a8 [0x1c03a95e0]>{contents = "ITU_R_601_4"}
1 : <CFString 0x1c09171c8 [0x1c03a95e0]>{contents = "CVImageBufferTransferFunction"} = <CFString 0x1c0917088 [0x1c03a95e0]>{contents = "ITU_R_709_2"}
2 : <CFString 0x1c093f348 [0x1c03a95e0]>{contents = "CVBytesPerRow"} = <CFNumber 0x81092876519e5903 [0x1c03a95e0]>{value = +2880, type = kCFNumberSInt32Type}
3 : <CFString 0x1c093f3c8 [0x1c03a95e0]>{contents = "Version"} = <CFNumber 0x81092876519eed23 [0x1c03a95e0]>{value = +2, type = kCFNumberSInt32Type}
5 : <CFString 0x1c0917148 [0x1c03a95e0]>{contents = "CVImageBufferColorPrimaries"} = <CFString 0x1c0917088 [0x1c03a95e0]>{contents = "ITU_R_709_2"}
}
}
}
sbufToTrackReadiness = 0x0
numSamples = 1
sampleTimingArray[1] = {
    {PTS = {390750488483992/1000000000 = 390750.488}, DTS = {INVALID}, duration = {INVALID}},
}
imageBuffer = 0x2832ad2c0

====================================================

//AVCaptureVideoDataOutput Delegates
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{

if (connection == videoConnection)
{

    if (self.outputVideoFormatDescription == NULL )
    {
        self.outputVideoFormatDescription   =   CMSampleBufferGetFormatDescription(sampleBuffer);
        [self   setupVideoRecorder];
    }
    else    if (self.status==RecorderRecording)
    {
        NSLog(@"%s : self.outputVideoFormatDescription - %@",__FUNCTION__,self.outputVideoFormatDescription);

        [self.cmDelegate    manager:self capturedFrameBuffer:sampleBuffer];
        NSLog(@"%s : sampleBuffer - %@",__FUNCTION__,sampleBuffer);

        dispatch_async(vidWriteQueue, ^
            {
                if  (!self->wroteFirstFrame)
                {
                    CMTime sampleTime   =   CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
                    NSLog(@"%s : sampleTime value - %lld, timescale - %i, epoch - %lli, flags - %u",__FUNCTION__,sampleTime.value, sampleTime.timescale, sampleTime.epoch, sampleTime.flags);

                    [self->assetWriter  startSessionAtSourceTime:sampleTime];
                    self->wroteFirstFrame   =   YES;
                }
                if  (self->videoAWInput.readyForMoreMediaData)
                    //else if   (self->videoAWInput.readyForMoreMediaData)
                {
                    BOOL appendSuccess  =   [self->videoAWInput appendSampleBuffer:sampleBuffer];
                    NSLog(@"%s : appendSuccess - %i",__FUNCTION__,appendSuccess);

                    if (!appendSuccess) NSLog(@"%s : failed to append video buffer - %@@",__FUNCTION__,self->assetWriter.error.localizedDescription);
                }
            });
    }
    else if (connection == audioConnection)
    {
    }
}

}


Solution

  • My bad... my problem was that I was spawning off the frame capture using a thread that was already declared in AVCaptureDataOutput setSampleBufferDelegate:queue: . Was recursively putting a process on a thread within that same thread. Posting answer in case another idiot, like me, makes the same stupid mistake...