1# Video Recording Sample (C/C++) 2 3Before developing a camera application, request permissions by following the instructions provided in [Camera Development Preparations](camera-preparation.md). 4 5This topic provides sample code that covers the complete video recording process and the API calling sequence. For details about a single process (such as device input, session management, and video recording), see the corresponding C/C++ development guide links provided in [Camera Development Preparations](camera-preparation.md). 6 7## Development Process 8 9After obtaining the output stream capabilities supported by the camera, create a video stream. The development process is as follows: 10 11 12 13## Sample Code 14 151. Link the dynamic library in the CMake script. 16 ```txt 17 target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 18 ``` 19 202. Import the NDK APIs on the C++ side, and perform video recording based on the surface ID passed in. 21 ```c++ 22 #include "hilog/log.h" 23 #include "ohcamera/camera.h" 24 #include "ohcamera/camera_input.h" 25 #include "ohcamera/capture_session.h" 26 #include "ohcamera/photo_output.h" 27 #include "ohcamera/preview_output.h" 28 #include "ohcamera/video_output.h" 29 #include "ohcamera/camera_manager.h" 30 31 void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode) 32 { 33 OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode); 34 } 35 36 CameraInput_Callbacks* GetCameraInputListener(void) 37 { 38 static CameraInput_Callbacks cameraInputCallbacks = { 39 .onError = OnCameraInputError 40 }; 41 return &cameraInputCallbacks; 42 } 43 44 void CaptureSessionOnFocusStateChange(Camera_CaptureSession* session, Camera_FocusState focusState) 45 { 46 OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange"); 47 } 48 49 void CaptureSessionOnError(Camera_CaptureSession* session, Camera_ErrorCode errorCode) 50 { 51 OH_LOG_INFO(LOG_APP, "CaptureSessionOnError = %{public}d", errorCode); 52 } 53 54 CaptureSession_Callbacks* GetCaptureSessionRegister(void) 55 { 56 static CaptureSession_Callbacks captureSessionCallbacks = { 57 .onFocusStateChange = CaptureSessionOnFocusStateChange, 58 .onError = CaptureSessionOnError 59 }; 60 return &captureSessionCallbacks; 61 } 62 63 void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput) 64 { 65 OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart"); 66 } 67 68 void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount) 69 { 70 OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount); 71 } 72 73 void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) 74 { 75 OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode); 76 } 77 78 VideoOutput_Callbacks* GetVideoOutputListener(void) 79 { 80 static VideoOutput_Callbacks videoOutputListener = { 81 .onFrameStart = VideoOutputOnFrameStart, 82 .onFrameEnd = VideoOutputOnFrameEnd, 83 .onError = VideoOutputOnError 84 }; 85 return &videoOutputListener; 86 } 87 88 void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status) 89 { 90 OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called"); 91 } 92 93 CameraManager_Callbacks* GetCameraManagerListener() 94 { 95 static CameraManager_Callbacks cameraManagerListener = { 96 .onCameraStatus = CameraManagerStatusCallback 97 }; 98 return &cameraManagerListener; 99 } 100 101 NDKCamera::NDKCamera(char *previewId, char *videoId) 102 { 103 Camera_Manager* cameraManager = nullptr; 104 Camera_Device* cameras = nullptr; 105 Camera_CaptureSession* captureSession = nullptr; 106 Camera_OutputCapability* cameraOutputCapability = nullptr; 107 Camera_VideoOutput* videoOutput = nullptr; 108 const Camera_Profile* previewProfile = nullptr; 109 const Camera_Profile* photoProfile = nullptr; 110 const Camera_VideoProfile* videoProfile = nullptr; 111 Camera_PreviewOutput* previewOutput = nullptr; 112 Camera_PhotoOutput* photoOutput = nullptr; 113 Camera_Input* cameraInput = nullptr; 114 uint32_t size = 0; 115 uint32_t cameraDeviceIndex = 0; 116 char* videoSurfaceId = videoId; 117 char* previewSurfaceId = previewId; 118 // Create a CameraManager object. 119 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 120 if (cameraManager == nullptr || ret != CAMERA_OK) { 121 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed."); 122 } 123 // Listen for camera status changes. 124 ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener()); 125 if (ret != CAMERA_OK) { 126 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed."); 127 } 128 129 // Obtain the camera list. 130 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 131 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 132 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 133 } 134 135 for (int index = 0; index < size; index++) { 136 OH_LOG_ERROR(LOG_APP, "cameraId = %{public}s ", cameras[index].cameraId); // Obtain the camera ID. 137 OH_LOG_ERROR(LOG_APP, "cameraPosition = %{public}d ", cameras[index].cameraPosition); // Obtain the camera position. 138 OH_LOG_ERROR(LOG_APP, "cameraType = %{public}d ", cameras[index].cameraType); // Obtain the camera type. 139 OH_LOG_ERROR(LOG_APP, "connectionType = %{public}d ", cameras[index].connectionType); // Obtain the camera connection type. 140 } 141 142 // Obtain the output streams supported by the camera. 143 ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 144 &cameraOutputCapability); 145 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 146 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 147 } 148 149 if (cameraOutputCapability->previewProfilesSize < 0) { 150 OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null"); 151 } 152 previewProfile = cameraOutputCapability->previewProfiles[0]; 153 154 if (cameraOutputCapability->photoProfilesSize < 0) { 155 OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null"); 156 } 157 photoProfile = cameraOutputCapability->photoProfiles[0]; 158 159 if (cameraOutputCapability->videoProfilesSize < 0) { 160 OH_LOG_ERROR(LOG_APP, "videorofilesSize == null"); 161 } 162 videoProfile = cameraOutputCapability->videoProfiles[0]; 163 164 // Create a VideoOutput instance. 165 ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput); 166 if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) { 167 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed."); 168 } 169 170 // Listen for video output errors. 171 ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener()); 172 if (ret != CAMERA_OK) { 173 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed."); 174 } 175 176 // Create a session. 177 ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession); 178 if (captureSession == nullptr || ret != CAMERA_OK) { 179 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed."); 180 } 181 // Listen for session errors. 182 ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister()); 183 if (ret != CAMERA_OK) { 184 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed."); 185 } 186 187 // Start configuration for the session. 188 ret = OH_CaptureSession_BeginConfig(captureSession); 189 if (ret != CAMERA_OK) { 190 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed."); 191 } 192 193 // Create a camera input stream. 194 ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput); 195 if (cameraInput == nullptr || ret != CAMERA_OK) { 196 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed."); 197 } 198 199 // Listen for camera input errors. 200 ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener()); 201 if (ret != CAMERA_OK) { 202 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed."); 203 } 204 205 // Open the camera. 206 ret = OH_CameraInput_Open(cameraInput); 207 if (ret != CAMERA_OK) { 208 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed."); 209 } 210 211 // Add the camera input stream to the session. 212 ret = OH_CaptureSession_AddInput(captureSession, cameraInput); 213 if (ret != CAMERA_OK) { 214 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed."); 215 } 216 217 // Create a preview output stream. For details about the surfaceId parameter, see the XComponent. The preview stream is the surface provided by the XComponent. 218 ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, 0, &previewOutput); 219 if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) { 220 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed."); 221 } 222 223 // Add the preview output stream to the session. 224 ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput); 225 if (ret != CAMERA_OK) { 226 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed."); 227 } 228 229 // Add the video output stream to the session. 230 ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput); 231 if (ret != CAMERA_OK) { 232 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed."); 233 } 234 235 // Commit the session configuration. 236 ret = OH_CaptureSession_CommitConfig(captureSession); 237 if (ret != CAMERA_OK) { 238 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed."); 239 } 240 241 // Start the session. 242 ret = OH_CaptureSession_Start(captureSession); 243 if (ret != CAMERA_OK) { 244 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); 245 } 246 247 // Start the video output stream. 248 ret = OH_VideoOutput_Start(videoOutput); 249 if (ret != CAMERA_OK) { 250 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed."); 251 } 252 253 // Call avRecorder.start() on the TS side to start video recording. 254 255 // Stop the video output stream. 256 ret = OH_VideoOutput_Stop(videoOutput); 257 if (ret != CAMERA_OK) { 258 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed."); 259 } 260 261 // Call avRecorder.stop() on the TS side to stop video recording. 262 263 // Stop the session. 264 ret = OH_CaptureSession_Stop(captureSession); 265 if (ret == CAMERA_OK) { 266 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success "); 267 } else { 268 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret); 269 } 270 271 // Release the camera input stream. 272 ret = OH_CameraInput_Close(cameraInput); 273 if (ret == CAMERA_OK) { 274 OH_LOG_INFO(LOG_APP, "OH_CameraInput_Close success "); 275 } else { 276 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Close failed. %d ", ret); 277 } 278 279 // Release the preview output stream. 280 ret = OH_PreviewOutput_Release(previewOutput); 281 if (ret == CAMERA_OK) { 282 OH_LOG_INFO(LOG_APP, "OH_PreviewOutput_Release success "); 283 } else { 284 OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Release failed. %d ", ret); 285 } 286 287 // Release the video output stream. 288 ret = OH_VideoOutput_Release(videoOutput); 289 if (ret == CAMERA_OK) { 290 OH_LOG_INFO(LOG_APP, "OH_VideoOutput_Release success "); 291 } else { 292 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Release failed. %d ", ret); 293 } 294 295 // Release the session. 296 ret = OH_CaptureSession_Release(captureSession); 297 if (ret == CAMERA_OK) { 298 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Release success "); 299 } else { 300 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Release failed. %d ", ret); 301 } 302 303 // Release the resources. 304 ret = OH_CameraManager_DeleteSupportedCameras(cameraManager, cameras, size); 305 if (ret != CAMERA_OK) { 306 OH_LOG_ERROR(LOG_APP, "Delete Cameras failed."); 307 } else { 308 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameras. ok"); 309 } 310 ret = OH_CameraManager_DeleteSupportedCameraOutputCapability(cameraManager, cameraOutputCapability); 311 if (ret != CAMERA_OK) { 312 OH_LOG_ERROR(LOG_APP, "Delete Cameras failed."); 313 } else { 314 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameraOutputCapability. ok"); 315 } 316 ret = OH_Camera_DeleteCameraManager(cameraManager); 317 if (ret != CAMERA_OK) { 318 OH_LOG_ERROR(LOG_APP, "Delete Cameras failed."); 319 } else { 320 OH_LOG_ERROR(LOG_APP, "OH_Camera_DeleteCameraManager. ok"); 321 } 322 } 323 ``` 324