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