Error while recording multiple screens using AVFoundation

559 views Asked by At

I am trying to record primary and secondary monitor screens into two separate files using below code.

const uint32_t MAX_DISPLAY = 2;
CGDirectDisplayID displays[MAX_DISPLAY] = {0};
CGGetActiveDisplayList(MAX_DISPLAY, displays, &m_nDisplays);

NSString* dest_file[2] = {0};
NSURL* dest_path[2] = {0};

AVCaptureConnection *CaptureConnection[2] = {0};

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @2048, AVVideoCleanApertureWidthKey,
                                            @1152, AVVideoCleanApertureHeightKey,
                                            @0, AVVideoCleanApertureHorizontalOffsetKey,
                                            @0, AVVideoCleanApertureVerticalOffsetKey,
                                            nil];

NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          @3,AVVideoPixelAspectRatioHorizontalSpacingKey,
                                          @3,AVVideoPixelAspectRatioVerticalSpacingKey,
                                          nil];

 NSNumber* bitsPerSecond = [NSNumber numberWithDouble:1024*1000];

NSDictionary *codecSettings =  [NSDictionary dictionaryWithObjectsAndKeys:
                               bitsPerSecond, AVVideoAverageBitRateKey,
                               videoCleanApertureSettings, AVVideoCleanApertureKey,
                               videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
                               nil];

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               codecSettings,AVVideoCompressionPropertiesKey,
                               AVVideoScalingModeResize,AVVideoScalingModeKey,
                               @2048, AVVideoWidthKey,
                               @1152, AVVideoHeightKey,
                               nil];

for( int nIdx = 0; nIdx < m_nDisplays; ++nIdx )
{
    m_session[nIdx] = [[AVCaptureSession alloc] init];
    dest_file[nIdx] = [NSString stringWithFormat:@"%@_%d.MOV",destination_path,nIdx];
    dest_path[nIdx] = [NSURL fileURLWithPath: dest_file[nIdx] ];


    // Create a ScreenInput with the display and add it to the session
    m_movie_file_input[nIdx] = [[[AVCaptureScreenInput alloc] initWithDisplayID:displays[nIdx]] autorelease];    
    [m_movie_file_input[nIdx] removesDuplicateFrames ];

    if ([m_session[nIdx] canAddInput:m_movie_file_input[nIdx]])
    {
        [m_session[nIdx] addInput:m_movie_file_input[nIdx]];
    }

    // Create a MovieFileOutput and add it to the session
    m_movie_file_output[nIdx] = [[[AVCaptureMovieFileOutput alloc] init] autorelease];
    if ([m_session[nIdx] canAddOutput:m_movie_file_output[nIdx]])
    {
        [m_session[nIdx] addOutput:m_movie_file_output[nIdx]];
    }

    CaptureConnection[nIdx] = [m_movie_file_output[nIdx] connectionWithMediaType:AVMediaTypeVideo];

    [m_movie_file_output[nIdx] setOutputSettings : videoSettings forConnection : CaptureConnection[nIdx]];

    // Start running the session
    [m_session[nIdx] startRunning];

    [m_movie_file_output[nIdx] startRecordingToOutputFileURL:dest_path[nIdx]    recordingDelegate:self];

}

I am getting both the screens saved into two separate files. But while calling startRecordingToOutputFileURL API for the secondary monitor i.e. for the second pass of loop, I am getting an error as shown below :

VTCompressionSessionCreate signalled err=-8973 (err) (VTVideoEncoderStartSession failed) at /SourceCache/CoreMedia_frameworks/CoreMedia-1562.19/Sources/VideoToolbox/VTCompressionSession.c line 897

Also compression parameters(bitrate) are not setting properly for Secondary monitor, it takes some different values other than the one I have specified in the program.

Can somebody please help me on this ? Also please let me know this is the proper way of doing this.

Thanks in Advance

George

0

There are 0 answers