1e41f4b71Sopenharmony_ci# Secondary Processing of Video Streams (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can use the APIs in the **ImageReceiver** class to create a **VideoOutput** instance and obtain real-time data of the video stream for secondary processing. For example, you can add a filter algorithm to the preview stream. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci1. Import the NDK, which provides camera-related attributes and methods. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci ```c++ 12e41f4b71Sopenharmony_ci // Include the NDK header files. 13e41f4b71Sopenharmony_ci #include "hilog/log.h" 14e41f4b71Sopenharmony_ci #include "ohcamera/camera.h" 15e41f4b71Sopenharmony_ci #include "ohcamera/camera_input.h" 16e41f4b71Sopenharmony_ci #include "ohcamera/capture_session.h" 17e41f4b71Sopenharmony_ci #include "ohcamera/photo_output.h" 18e41f4b71Sopenharmony_ci #include "ohcamera/preview_output.h" 19e41f4b71Sopenharmony_ci #include "ohcamera/video_output.h" 20e41f4b71Sopenharmony_ci #include "ohcamera/camera_manager.h" 21e41f4b71Sopenharmony_ci ``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci2. Link the dynamic library in the CMake script. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci ```txt 26e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 27e41f4b71Sopenharmony_ci ``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci3. Obtain the surface ID. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci Call **createImageReceiver()** of the image module to create an **ImageReceiver** instance, and use **getReceivingSurfaceId()** of the instance to obtain the surface ID, which is associated with the video output stream to process the stream data. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci4. Create a video output stream. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci Based on the surface ID passed in, obtain the video output streams supported by the current device from **videoProfiles** in the **CameraOutputCapability** class. Then, define video recording parameters and use **OH_CameraManager_CreateVideoOutput()** to create a video output stream. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci > **NOTE** 38e41f4b71Sopenharmony_ci > 39e41f4b71Sopenharmony_ci > The preview stream and video output stream must have the same aspect ratio of the resolution. For example, the aspect ratio in the code snippet below is 640:480 (which is equal to 4:3), then the aspect ratio of the resolution of the preview stream must also be 4:3. This means that the resolution can be 640:480, 960:720, 1440:1080, or the like. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```c++ 42e41f4b71Sopenharmony_ci NDKCamera::NDKCamera(char *str) 43e41f4b71Sopenharmony_ci { 44e41f4b71Sopenharmony_ci Camera_Manager *cameraManager = nullptr; 45e41f4b71Sopenharmony_ci Camera_Device* cameras = nullptr; 46e41f4b71Sopenharmony_ci Camera_OutputCapability* cameraOutputCapability = nullptr; 47e41f4b71Sopenharmony_ci Camera_VideoOutput* videoOutput = nullptr; 48e41f4b71Sopenharmony_ci const Camera_VideoProfile* videoProfile; 49e41f4b71Sopenharmony_ci uint32_t size = 0; 50e41f4b71Sopenharmony_ci uint32_t cameraDeviceIndex = 0; 51e41f4b71Sopenharmony_ci char* videoSurfaceId = str; 52e41f4b71Sopenharmony_ci Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 53e41f4b71Sopenharmony_ci if (cameraManager == nullptr || ret != CAMERA_OK) { 54e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 57e41f4b71Sopenharmony_ci if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 58e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 61e41f4b71Sopenharmony_ci &cameraOutputCapability); 62e41f4b71Sopenharmony_ci if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 63e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 64e41f4b71Sopenharmony_ci } 65e41f4b71Sopenharmony_ci if (cameraOutputCapability->videoProfilesSize < 0) { 66e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "videorofilesSize == null"); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci videoProfile = cameraOutputCapability->videoProfiles[0]; 69e41f4b71Sopenharmony_ci // Create a VideoOutput instance. 70e41f4b71Sopenharmony_ci ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput); 71e41f4b71Sopenharmony_ci if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) { 72e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed."); 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci ``` 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci5. Start video recording. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci Call **OH_VideoOutput_Start()** of the **VideoOutput** instance to start the video output stream. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci ```c++ 82e41f4b71Sopenharmony_ci // Start the video output stream. 83e41f4b71Sopenharmony_ci ret = OH_VideoOutput_Start(videoOutput); 84e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 85e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed."); 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci ``` 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci6. Stop video recording. 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci Call **OH_VideoOutput_Stop()** of the **VideoOutput** instance to stop the video output stream. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci ```c++ 94e41f4b71Sopenharmony_ci // Stop the video output stream. 95e41f4b71Sopenharmony_ci ret = OH_VideoOutput_Stop(videoOutput); 96e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 97e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed."); 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci## Status Listening 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci- 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 a result is returned. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ```c++ 108e41f4b71Sopenharmony_ci ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener()); 109e41f4b71Sopenharmony_ci if (ret != CAMERA_OK) { 110e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed."); 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci ``` 113e41f4b71Sopenharmony_ci ```c++ 114e41f4b71Sopenharmony_ci void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput) 115e41f4b71Sopenharmony_ci { 116e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart"); 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci VideoOutput_Callbacks* GetVideoOutputListener(void) 120e41f4b71Sopenharmony_ci { 121e41f4b71Sopenharmony_ci static VideoOutput_Callbacks videoOutputListener = { 122e41f4b71Sopenharmony_ci .onFrameStart = VideoOutputOnFrameStart, 123e41f4b71Sopenharmony_ci .onFrameEnd = VideoOutputOnFrameEnd, 124e41f4b71Sopenharmony_ci .onError = VideoOutputOnError 125e41f4b71Sopenharmony_ci }; 126e41f4b71Sopenharmony_ci return &videoOutputListener; 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci ``` 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci- 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. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci ```c++ 133e41f4b71Sopenharmony_ci void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount) 134e41f4b71Sopenharmony_ci { 135e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount); 136e41f4b71Sopenharmony_ci } 137e41f4b71Sopenharmony_ci ``` 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci- Register the **'error'** event to listen for video output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1). 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci ```c++ 142e41f4b71Sopenharmony_ci void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) 143e41f4b71Sopenharmony_ci { 144e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode); 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci ``` 147