Record video

You can record video to file.

If you have multiple cameras, the process is the same as using a single camera. For more information, see Synchronized recording from multiple cameras.” Here are the steps to create record a video to file. You can record to either H.264 encoded video or uncompressed video (UCV or MOV).
  1. Open the camera.
  2. Set the viewfinder mode for camera. Ensure that you open the camera with the camera mode that includes CAMERA_MODE_ROLL.
  3. Set up Screen. See Use Screen with the Camera API for more information.
  4. Start the viewfinder and configure the camera settings as necessary as described in Start viewfinder and configure camera settings in this guide.
  5. Create a Screen context, window, and buffers. For more information, see Using streams in the Resource Sharing chapter of the Screen Developer's Guide.
  6. If you change the format, width, height of the viewfinder from its default setings, you must set the video properties for these to those new values since the default properties for video should match the default viewfinder properties that were overwritten. The exception is framerate, where you can set the viewfinder to record at an integer multiple of the viewfinder framerate as long as the framerate isn 't higher. For example, if your viewfinder framerate is set to 30 fps, you can record 15 fps (multiple of two) or 10 fps (multiple of three).
    You can get the viewfinder properties from the camera using camera_get_vf_property(). Here are the typical properties that you should check match:
    • CAMERA_IMGPROP_FORMAT
    • CAMERA_IMGPROP_WIDTH
    • CAMERA_IMGPROP_HEIGHT
    • CAMERA_IMGPROP_ROTATION
    • CAMERA_IMGPROP_FRAMERATE
    • CAMERA_IMGPROP_VIDEOCODEC
      Note: If you set this property to CAMERA_VIDEOCODEC_NONE, you record to uncompressed. To specify whether to record to MOV or UCV (UnCompressed Video), you can specify the format when you call the camera_roll_open_video(), and pass whether record using MOV or UCV format, which are CAMERA_ROLL_VIDEO_FMT_MOV for .mov files and CAMERA_ROLL_VIDEO_FMT_UCV for .ucv files, respectively. If you choose not use to use camera_roll_open_video() to generate filename, specify .mov or .ucv to specify the format to use to record.
    • CAMERA_IMGPROP_AUDIOCODEC
    Then, you can set the properties using camera_set_video_property(). For some properties, you can use the camera_set_*() function, such as :
    • CAMERA_IMGPROP_FORMAT
    • CAMERA_IMGPROP_WIDTH
    • CAMERA_IMGPROP_HEIGHT
    • CAMERA_IMGPROP_ROTATION
    • CAMERA_IMGPROP_FRAMERATE
    • CAMERA_IMGPROP_VIDEOCODEC
    • CAMERA_IMGPROP_AUDIOCODEC
  7. Open a filename to record the video. The filename must be unique. If you had specified to record an uncompressed format, you can specify the format to use for the recording by including .mov or .ucv as part of the file extension. Optionally, to make this esier, you call camera_roll_open_video() to determine a unique name for your file and the uncompressed format to use.
  8. Start recording video by calling camera_start_video().
  9. After you done are recording video, stop recording video by calling camera_stop_video().
  10. Close the video file by calling camera_roll_close_video().
  11. This requires that you stop the viewfinder and the image buffer from streaming. You don't need to disconnect from the camera (close) until you are done using it. For example, you can stop and then start the viewfinder again. For more information, see Stop viewfinder and close camera.”
The following code snippet presumes that you're using a camera configured as CAMERA_UNIT_1 and shows to record video from a single camera. For more information, see the source for the Camera example application provided with this release.
...
...

camera_handle_t cameraHandle;
uint32_t numSupported;
camera_res_t* supportedResolutions;
camera_vfmode_t modes;
int err;
int i;

screen_context_t context;
screen_window_t window;

// Create a screen context and a window and set up Screen in your application.
err = screen_create_context(&context, 0);
if (err != 0) {
        err = errno;
        printf("Failed to create screen context: err = %d\n", err);
}
//Create the viewfinder window to put our viewfinder (image buffers) into
int screenUsage = SCREEN_USAGE_NATIVE; // will be filling it later so specify native
int screenFormat = SCREEN_FORMAT_RGBA8888; // depends on your display - this requires that you know what formats are suppported

//Create a window for filling in with color
screen_create_window(&window, context);

screen_set_window_property_iv(window, SCREEN_PROPERTY_USAGE, &screenUsage);
screen_set_window_property_iv(window, SCREEN_PROPERTY_FORMAT, &screenFormat);

// Create buffers for the window - memory associated with window
err = screen_create_window_buffers(window, 1);



screen_buffer_t screenBuffer;
int size[2];
int blitInfo[] = {SCREEN_BLIT_COLOR, (int) 0xff00ffff, SCREEN_BLIT_END};


// Get a  buffer
screen_get_window_property_pv(window, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&screenBuffer);
// now fill the blue window
    screen_fill(context, screenBuffer, blitInfo);

// Obtain the size of the buffer
err = screen_get_window_property_iv(window, SCREEN_PROPERTY_BUFFER_SIZE, size);

// Post the changes to the display to position 0, 0 and with the screen size of 720, 1280.
int rects[4] = {0,0,720,1280};
screen_post_window(window, screenBuffer, 1, rects, 0);

// Create a unique group ID.
char groupId[64];
snprintf(groupId, sizeof(groupId), "camera_unique_group_%x_%x", getpid(), cameraHandle);
screen_create_window_group(window, groupId);




// First connect to the camera
if (camera_open(CAMERA_UNIT_1, CAMERA_MODE_RW | CAMERA_MODE_ROLL, &cameraHandle) != CAMERA_EOK) {
    printf("Failed to open the camera: err");
    return 1;
}
// Set the viewfinder image stream mode
if (camera_set_vf_mode(cameraHandle, CAMERA_VFMODE_VIDEO) != CAMERA_EOK)
{
   printf("Failed to set the viewfinder mode");
   camera_close(cameraHandle);
}


screen_flush_context(context, SCREEN_WAIT_IDLE);
// Get Apply the Window ID to the viewfinder window and pass the Group ID
// to the Camera library. You should also set the window ID of the viewfinder windo
char windowId[64];
snprintf(windowId, sizeof(windowId), "viewfinder_window_%x", cameraHandle);
camera_set_vf_property(cameraHandle,
                       CAMERA_IMGPROP_WIN_GROUPID, groupId,
                       CAMERA_IMGPROP_WIN_ID, windowId);
                       
                       

// Start the viewfinder
err = camera_start_viewfinder(cameraHandle, NULL, NULL, NULL);
if (err != CAMERA_EOK) {
    printf("Failed to start viewfinder: err = %d\n", err);
    camera_close(cameraHandle);
    return err;
}


// Need to catch the Create event. Catching it
// gives us the handle to the viewfinder window that
// the Camera library creates for you.
screen_event_t screenEvent;
bool gotEvent = false;
screen_window_t viewfinder;
// This is the viewfinder (image buffer contents) window's that's passed to you.
screen_window_t *viewfinderHandle = NULL; 
screen_stream_t *streamHandle = NULL;
viewfinderHandle = &viewfinder;

if (viewfinderHandle) {
   *viewfinderHandle = NULL;
}

if (streamHandle){
   *streamHandle = NULL;
}


// Application is created to hold the event so its local (client)
if (screen_create_event(&screenEvent) == -1) {
    err = errno;
    printf("Failed to create screen event: err = %d\n", err);
    return err;
}

// Handling the Screen events
while (screen_get_event(context, screenEvent, -1) == 0) {
   int eventType;
   int objectType;

   if (screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_TYPE, &eventType) == -1) {
       printf("Failed to get type of screen event: err = %d\n", err);
       screen_destroy_event(screenEvent);
       break;
   }
   if (eventType == SCREEN_EVENT_PROPERTY) {
      screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_OBJECT_TYPE, &objectType);
   }
   else if (eventType == SCREEN_EVENT_CREATE) {
       // Got the SCREEN_EVENT_CREATE, which indicates that the viewfinder window has
       // joined your main application window 
       screen_get_event_property_iv(screenEvent, SCREEN_PROPERTY_OBJECT_TYPE, &objectType);
       //Get a reference to the Stream object from the Camera service
           if (streamHandle && (objectType == SCREEN_OBJECT_TYPE_STREAM)) {
               screen_get_event_property_pv(screenEvent, SCREEN_PROPERTY_STREAM, (void**)streamHandle);
           }
           else if (viewfinderHandle && (objectType == SCREEN_OBJECT_TYPE_WINDOW)) {
               screen_get_event_property_pv(screenEvent, SCREEN_PROPERTY_WINDOW, (void**)viewfinderHandle);
        }
        //You now have references to the memory that Screen allocated for you.
        //You're responsible for cleaning this up!
        if ((!streamHandle || *streamHandle) && (!viewfinderHandle || *viewfinderHandle)) {
            gotEvent = true;
            break; 
        }
    } // end else if
} // end while

//Clean-up memory allocated for the Screen Event;
screen_destroy_event(screenEvent);

if (gotEvent == false) {
  err = errno;
  printf("Didn't get a Screen event...indicates something didn't go as expected");
  //return err;
}

// You now have a handle to the viewfinder window. Let's
// make it visible.
int vsize[2] = {1280, 720 };
int vposition[2]={0,0};
screen_set_window_property_iv(*viewfinderHandle, SCREEN_PROPERTY_SIZE, vsize);
screen_set_window_property_iv(*viewfinderHandle, SCREEN_PROPERTY_BUFFER_SIZE, vsize);
screen_set_window_property_iv(*viewfinderHandle, SCREEN_PROPERTY_SOURCE_SIZE, vsize);
screen_set_window_property_iv(*viewfinderHandle, SCREEN_PROPERTY_POSITION, vposition);

//Make the window visible. The Camera library takes care of reposting the
//content.
int visible = 1;
screen_set_window_property_iv(*viewfinderHandle, SCREEN_PROPERTY_VISIBLE, &visible);

// Flush the changes to make them take effect.
// This is where you should see the viewfinder window on the display.
screen_flush_context(context, SCREEN_WAIT_IDLE);

//Retrieve viewfinder properties and default framerate, video codec, and audio codec.
// Retrieve other video parameters
double framerate;
camera_videocodec_t videoCodec;
camera_audiocodec_t audioCodec;
camera_frametype_t format;
uint32_t width, height, rotation;

err = camera_get_vf_property(cameraHandle,
                                    CAMERA_IMGPROP_FORMAT, &format,
                                    CAMERA_IMGPROP_WIDTH, &width,
                                    CAMERA_IMGPROP_HEIGHT, &height,
                                    CAMERA_IMGPROP_ROTATION, &rotation);
   }
err = camera_get_video_property(cameraHandle,
                                CAMERA_IMGPROP_FRAMERATE, &framerate,
                                CAMERA_IMGPROP_VIDEOCODEC, &videoCodec,
                                CAMERA_IMGPROP_AUDIOCODEC, &audioCodec);



err = camera_set_video_property(cameraHandle,
                                       CAMERA_IMGPROP_FORMAT, format,
                                       CAMERA_IMGPROP_WIDTH, width,
                                       CAMERA_IMGPROP_HEIGHT, height,
                                       CAMERA_IMGPROP_ROTATION, rotation,
                                       CAMERA_IMGPROP_FRAMERATE, framerate,
                                       CAMERA_IMGPROP_VIDEOCODEC, videoCodec,
                                       CAMERA_IMGPROP_AUDIOCODEC, audioCodec);

int exitExample = 0;
static char userString[80];
while (exitExample == 0) {
     printf("\tPress c to start recording:\n");
     fgets(userString, sizeof(userString), stdin);
     if ( (userString[0] == 'c') || (userString[0] == 'C') ) {
       exitExample = 1;
     }
}

//Open a file for recording
char* rollFilename = (char*) malloc(sizeof(char) * CAMERA_ROLL_NAMELEN);
int fd;
camera_roll_open_video(cameraHandle, &fd, rollFilename, CAMERA_ROLL_NAMELEN, CAMERA_ROLL_VIDEO_FMT_MP4);

//Start recording video
camera_start_video(cameraHandle, rollFilename, NULL, NULL, NULL);
exitExample=0;
while (exitExample == 0) {
    printf("\tPress c to stop recording:\n");
    fgets(userString, sizeof(userString), stdin);
    if ( (userString[0] == 'c') || (userString[0] == 'C') ) {
        exitExample = 1;
    }
}

//Stop recording video and close the file in the camera roll
camera_stop_video(cameraHandle);
camera_roll_close_video(fd);

//Stop receiving image buffers and disconnect from the camera.
camera_stop_viewfinder(cameraHandle);
camera_close(cameraHandle);

//Clean-up memory allocated for the video file
free(rollFilename);
          
//Clean-up the Screen objects.
screen_destroy_window(viewfinder);
screen_destroy_window(window);
screen_destroy_context(context);
...
...