1# Video Recording (C/C++)
2
3As 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](native-camera-shooting.md) to set the resolution, flash, focal length, photo quality, and rotation angle.
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   Create an AVRecorder instance, and call **getInputSurface()** of the instance to obtain a surface ID.
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 **createVideoOutput()** to create a video output stream.
36
37   ```c++
38    NDKCamera::NDKCamera(char *str)
39    {
40      Camera_Manager *cameraManager = nullptr;
41      Camera_Device* cameras = nullptr;
42      Camera_OutputCapability* cameraOutputCapability = nullptr;
43      Camera_VideoOutput* videoOutput = nullptr;
44      const Camera_VideoProfile* videoProfile;
45      uint32_t size = 0;
46      uint32_t cameraDeviceIndex = 0;
47      char* videoSurfaceId = str;
48      Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
49      if (cameraManager == nullptr || ret != CAMERA_OK) {
50            OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
51      }
52      ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
53      if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
54          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
55      }
56      ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
57                                                                  &cameraOutputCapability);
58      if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
59          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
60      }
61      if (cameraOutputCapability->videoProfilesSize < 0) {
62        OH_LOG_ERROR(LOG_APP, "videorofilesSize == null");
63      }
64      videoProfile = cameraOutputCapability->videoProfiles[0];
65      // Create a VideoOutput instance.
66      ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput);
67      if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) {
68          OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
69      }
70    }
71   ```
72
735. Start video recording.
74
75   Call [OH_VideoOutput_Start()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_start) of the VideoOutput instance to start the video output stream.
76
77   ```c++
78      // Start the video output stream.
79      ret = OH_VideoOutput_Start(videoOutput);
80      if (ret != CAMERA_OK) {
81        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
82      }
83   ```
84
856. Stop video recording.
86
87   Call [OH_VideoOutput_Stop()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_stop) of the VideoOutput instance to stop the video output stream.
88
89   ```c++
90      // Stop the video output stream.
91      ret = OH_VideoOutput_Stop(videoOutput);
92      if (ret != CAMERA_OK) {
93        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
94      }
95   ```
96
97
98## Status Listening
99
100During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors.
101
102- 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.
103
104  ```c++
105    ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
106    if (ret != CAMERA_OK) {
107      OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
108    }
109  ```
110  ```c++
111    void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
112    {
113        OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
114    }
115
116    VideoOutput_Callbacks* GetVideoOutputListener(void)
117    {
118        static VideoOutput_Callbacks videoOutputListener = {
119            .onFrameStart = VideoOutputOnFrameStart,
120            .onFrameEnd = VideoOutputOnFrameEnd,
121            .onError = VideoOutputOnError
122        };
123        return &videoOutputListener;
124    }
125  ```
126
127- 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.
128
129  ```c++
130    void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
131    {
132        OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
133    }
134  ```
135
136- 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).
137
138  ```c++
139    void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
140    {
141        OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
142    }
143  ```
144