You can record video to file.
...
...
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);
...
...