Video Recording (C/C++)
As another important function of the camera application, video recording is the process of cyclic frame capture. To smooth video recording, you can follow step 5 in Photo Capture to set the resolution, flash, focal length, photo quality, and rotation angle.
How to Develop
Read Camera for the API reference.
-
Import the NDK, which provides camera-related properties and methods.
// Include the NDK header files. #include "hilog/log.h" #include "ohcamera/camera.h" #include "ohcamera/camera_input.h" #include "ohcamera/capture_session.h" #include "ohcamera/photo_output.h" #include "ohcamera/preview_output.h" #include "ohcamera/video_output.h" #include "ohcamera/camera_manager.h" -
Link the dynamic library in the CMake script.
target_link_libraries(entry PUBLIC libace_napi.z.so libohcamera.so libhilog_ndk.z.so ) -
Obtain the surface ID.
Create an AVRecorder instance, and call getInputSurface() of the instance to obtain a surface ID.
-
Create a video output stream.
Based on the surface ID passed in, call OH_CameraManager_GetSupportedCameraOutputCapability to obtain Camera_OutputCapability. From there, you can access videoProfiles to obtain the video output streams supported by the device. Then, define video recording parameters and use OH_CameraManager_CreateVideoOutput to create a video output stream.
Camera_VideoOutput* CreateVideoOutput(Camera_Manager* cameraManager, char* videoSurfaceIdStr, const Camera_VideoProfile* videoProfile) { // Create a VideoOutput instance. Camera_VideoOutput* videoOutput = nullptr; Camera_ErrorCode ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceIdStr, &videoOutput); if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed."); } return videoOutput; } -
Start video recording.
Call OH_VideoOutput_Start() to start the video output stream.
// Start the video output stream. Camera_ErrorCode VideoOutputStart(Camera_VideoOutput* videoOutput) { Camera_ErrorCode ret = OH_VideoOutput_Start(videoOutput); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed."); } return ret; } -
Stop video recording.
Call OH_VideoOutput_Stop() to stop the video output stream.
// Stop the video output stream. Camera_ErrorCode VideoOutputStop(Camera_VideoOutput* videoOutput) { Camera_ErrorCode ret = OH_VideoOutput_Stop(videoOutput); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed."); } return ret; }
Status Listening
During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors.
-
Register the 'frameStart' event to listen for recording start events. This event can be registered when a VideoOutput instance is created and is triggered when the bottom layer starts exposure for recording for the first time. Video recording starts as long as the event is triggered.
void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput) { OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart"); } -
Register the 'frameEnd' event to listen for recording end events. This event can be registered when a VideoOutput instance is created and is triggered when the last frame of recording ends. Video recording ends as long as a result is returned.
void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount) { OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount); } -
Register the 'error' event to listen for video output errors. The callback function returns an error code when a video output API is incorrectly used. For details about the error code types, see Camera_ErrorCode.
void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) { OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode); }VideoOutput_Callbacks* GetVideoOutputListener(void) { static VideoOutput_Callbacks videoOutputListener = { .onFrameStart = VideoOutputOnFrameStart, .onFrameEnd = VideoOutputOnFrameEnd, .onError = VideoOutputOnError }; return &videoOutputListener; } Camera_ErrorCode RegisterVideoOutputCallback(Camera_VideoOutput* videoOutput) { Camera_ErrorCode ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener()); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed."); } return ret; }