• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Video Recording (C/C++)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8As 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.
9
10## How to Develop
11
12Read [Camera](../../reference/apis-camera-kit/capi-oh-camera.md) for the API reference.
13
141. Import the NDK, which provides camera-related attributes and methods.
15
16   ```c++
17   // Include the NDK header files.
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
282. Link the dynamic library in the CMake script.
29
30   ```txt
31   target_link_libraries(entry PUBLIC
32       libace_napi.z.so
33       libohcamera.so
34       libhilog_ndk.z.so
35   )
36   ```
37
383. Obtain the surface ID.
39
40   Create an AVRecorder instance, and call **getInputSurface()** of the instance to obtain a surface ID.
41
424. Create a video output stream.
43
44   Based on the surface ID passed in, call [OH_CameraManager_GetSupportedCameraOutputCapability](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_getsupportedcameraoutputcapability) to obtain [Camera_OutputCapability](../../reference/apis-camera-kit/capi-oh-camera-camera-outputcapability.md). From there, you can access **videoProfiles** to obtain the video output streams supported by the device. Then, define video recording parameters and use **OH_CameraManager_CreateVideoOutput** to create a video output stream.
45
46   ```c++
47   Camera_VideoOutput* CreateVideoOutput(Camera_Manager* cameraManager, char* videoSurfaceIdStr,
48       const Camera_VideoProfile* videoProfile)
49   {
50       // Create a VideoOutput instance.
51       Camera_VideoOutput* videoOutput = nullptr;
52       Camera_ErrorCode ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceIdStr,
53           &videoOutput);
54       if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) {
55           OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
56       }
57       return videoOutput;
58   }
59   ```
60
615. Start video recording.
62
63   Call [OH_VideoOutput_Start()](../../reference/apis-camera-kit/capi-video-output-h.md#oh_videooutput_start) to start the video output stream.
64
65   ```c++
66   // Start the video output stream.
67   Camera_ErrorCode VideoOutputStart(Camera_VideoOutput* videoOutput)
68   {
69       Camera_ErrorCode ret = OH_VideoOutput_Start(videoOutput);
70       if (ret != CAMERA_OK) {
71           OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
72       }
73       return ret;
74   }
75   ```
76
776. Stop video recording.
78
79   Call [OH_VideoOutput_Stop()](../../reference/apis-camera-kit/capi-video-output-h.md#oh_videooutput_stop) to stop the video output stream.
80
81   ```c++
82   // Stop the video output stream.
83   Camera_ErrorCode VideoOutputStop(Camera_VideoOutput* videoOutput)
84   {
85       Camera_ErrorCode ret = OH_VideoOutput_Stop(videoOutput);
86       if (ret != CAMERA_OK) {
87           OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
88       }
89       return ret;
90   }
91   ```
92
93
94## Status Listening
95
96During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors.
97
98- 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 the event is triggered.
99
100  ```c++
101  void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
102  {
103      OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
104  }
105  ```
106
107- 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.
108
109  ```c++
110  void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
111  {
112      OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
113  }
114  ```
115
116- 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/capi-camera-h.md#camera_errorcode).
117
118  ```c++
119  void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
120  {
121      OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
122  }
123  ```
124  ```c++
125  VideoOutput_Callbacks* GetVideoOutputListener(void)
126  {
127      static VideoOutput_Callbacks videoOutputListener = {
128          .onFrameStart = VideoOutputOnFrameStart,
129          .onFrameEnd = VideoOutputOnFrameEnd,
130          .onError = VideoOutputOnError
131      };
132      return &videoOutputListener;
133  }
134
135  Camera_ErrorCode RegisterVideoOutputCallback(Camera_VideoOutput* videoOutput)
136  {
137      Camera_ErrorCode ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
138      if (ret != CAMERA_OK) {
139        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
140      }
141      return ret;
142  }
143  ```
144