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