Sample video capture program

Updated: October 28, 2024

This sample video capture code can be used for reference when building an application that uses video capture.

The following code sample shows how the video capture API can be used in an application. Note that the sample code doesn't include error checking, which may be quite useful in a production application.

main() {
    void    *pointers[n_pointers] = { 0 };
    // connect to screen
    // create a window
    // create screen buffers
    // obtain pointers to the buffers

    // Connect to a capture device
    capture_context_t context = capture_create_context( flags );
    if( !context ) {
        // TODO: Handle errors...
    }

    // Validate device's properties
    if( !capture_is_property( context, CAPTURE_PROPERTY_BRIGHTNESS )
          || !capture_is_property( context, CAPTURE_PROPERTY_CONTRAST )
          || !capture_is_property( context, ... )
     ) {
        capture_destroy_context( context );
        fprintf( stderr, "Unable to use buffer. Driver doesn't support some required properties.\n" );
        return EXIT_FAILURE;
    }

    // setup capture parameters
    capture_set_property_i( context, CAPTURE_PROPERTY_DEVICE, 1 );

    const char *info = NULL;
    capture_get_property_p( context, CAPTURE_PROPERTY_DEVICE_INFO, &info );
    fprintf( stderr, "device-info = '%s'\n", info );

    capture_set_property_i( context, CAPTURE_PROPERTY_BRIGHTNESS, 10 );
    capture_set_property_i( context, CAPTURE_PROPERTY_FRAME_NBUFFERS, n_pointers );
    capture_set_property_p( context, CAPTURE_PROPERTY_FRAME_BUFFERS, pointers );

    // tell the driver to start capturing (when capture_update() is called).
    capture_set_property_i( context, CAPTURE_ENABLE, 1 );

    // commit changes to the H/W -- and start capturing...
    capture_update( context, 0 );

    while( capturing ) {
        int n_dropped;

        // get next captured frame...
        int idx = capture_get_frame( context, CAPTURE_TIMEOUT_INFINITE, flags );

        // the returned idx-ed pointer is 'locked' upon return from the capture_get_frame()
        // this buffer will remain locked until the capture_get_frame() is called again.

        // update screen
        screen_post_window( win, buf[idx], n_dirty_rects, dirty_rects, flags );

        // Mark the buffer identifed by the idx as available for capturing.
        capture_release_frame( context, idx );
    }

    // stop capturing...
    capture_set_property_i( context, CAPTURE_ENABLE, 0 );
    capture_update( context, 0 );

    ...
}
      
Note: The sample above posts then releases each frame buffer. In a production application, you should use at least two frame buffers, so that you do not have to release a frame before the next one is posted. This will avoid delays and jitter.
  翻译: