• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Secondary Processing of Video Streams (C/C++)
2
3You 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.
4
5## How to Develop
6
7Read [Camera](../reference/apis-camera-kit/_o_h___camera.md) for the API reference.
8
91. Import the NDK, which provides camera-related attributes and methods.
10
11   ```c++
12    // Include the NDK header files.
13    #include "hilog/log.h"
14    #include "ohcamera/camera.h"
15    #include "ohcamera/camera_input.h"
16    #include "ohcamera/capture_session.h"
17    #include "ohcamera/photo_output.h"
18    #include "ohcamera/preview_output.h"
19    #include "ohcamera/video_output.h"
20    #include "ohcamera/camera_manager.h"
21   ```
22
232. Link the dynamic library in the CMake script.
24
25   ```txt
26    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
27   ```
28
293. Obtain the surface ID.
30
31    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.
32
334. Create a video output stream.
34
35   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.
36
37   > **NOTE**
38   >
39   > 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.
40
41    ```c++
42    NDKCamera::NDKCamera(char *str)
43    {
44      Camera_Manager *cameraManager = nullptr;
45      Camera_Device* cameras = nullptr;
46      Camera_OutputCapability* cameraOutputCapability = nullptr;
47      Camera_VideoOutput* videoOutput = nullptr;
48      const Camera_VideoProfile* videoProfile;
49      uint32_t size = 0;
50      uint32_t cameraDeviceIndex = 0;
51      char* videoSurfaceId = str;
52      Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
53      if (cameraManager == nullptr || ret != CAMERA_OK) {
54            OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
55      }
56      ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
57      if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
58          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
59      }
60      ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
61                                                                  &cameraOutputCapability);
62      if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
63          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
64      }
65      if (cameraOutputCapability->videoProfilesSize < 0) {
66          OH_LOG_ERROR(LOG_APP, "videorofilesSize == null");
67      }
68      videoProfile = cameraOutputCapability->videoProfiles[0];
69      // Create a VideoOutput instance.
70      ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput);
71      if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) {
72          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
73      }
74    }
75    ```
76
775. Start video recording.
78
79   Call **OH_VideoOutput_Start()** of the **VideoOutput** instance to start the video output stream.
80
81     ```c++
82      // Start the video output stream.
83      ret = OH_VideoOutput_Start(videoOutput);
84      if (ret != CAMERA_OK) {
85          OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
86      }
87     ```
88
896. Stop video recording.
90
91   Call **OH_VideoOutput_Stop()** of the **VideoOutput** instance to stop the video output stream.
92
93   ```c++
94    // Stop the video output stream.
95    ret = OH_VideoOutput_Stop(videoOutput);
96    if (ret != CAMERA_OK) {
97        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
98    }
99   ```
100
101## Status Listening
102
103During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors.
104
105- 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.
106
107  ```c++
108    ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
109    if (ret != CAMERA_OK) {
110      OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
111    }
112  ```
113  ```c++
114    void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
115    {
116        OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
117    }
118
119    VideoOutput_Callbacks* GetVideoOutputListener(void)
120    {
121        static VideoOutput_Callbacks videoOutputListener = {
122            .onFrameStart = VideoOutputOnFrameStart,
123            .onFrameEnd = VideoOutputOnFrameEnd,
124            .onError = VideoOutputOnError
125        };
126        return &videoOutputListener;
127    }
128  ```
129
130- 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.
131
132  ```c++
133    void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
134    {
135        OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
136    }
137  ```
138
139- 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).
140
141  ```c++
142    void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
143    {
144        OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
145    }
146  ```
147