• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Sample of Secondary Processing of Video Streams (C/C++)
2
3## Development Process
4
5Before developing a camera application, request the camera permission. For details, see [Camera Development Preparations](camera-preparation.md).
6
7After obtaining the output stream capabilities supported by the camera, create a video stream. The development process is as follows:
8
9![Recording Development Process](figures/recording-ndk-imageReceiver-development-process.png)
10
11## Sample Code
12
131. Link the dynamic library in the CMake script.
14    ```txt
15        target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
16    ```
17
182. Import the NDK APIs on the C++ side, and perform video recording based on the surface ID passed in.
19    ```c++
20    #include "hilog/log.h"
21    #include "ohcamera/camera.h"
22    #include "ohcamera/camera_input.h"
23    #include "ohcamera/capture_session.h"
24    #include "ohcamera/photo_output.h"
25    #include "ohcamera/preview_output.h"
26    #include "ohcamera/video_output.h"
27    #include "ohcamera/camera_manager.h"
28
29    void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
30    {
31        OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode);
32    }
33
34    CameraInput_Callbacks* GetCameraInputListener(void)
35    {
36        static CameraInput_Callbacks cameraInputCallbacks = {
37            .onError = OnCameraInputError
38        };
39        return &cameraInputCallbacks;
40    }
41
42    void CaptureSessionOnFocusStateChange(Camera_CaptureSession* session, Camera_FocusState focusState)
43    {
44        OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange");
45    }
46
47    void CaptureSessionOnError(Camera_CaptureSession* session, Camera_ErrorCode errorCode)
48    {
49        OH_LOG_INFO(LOG_APP, "CaptureSessionOnError = %{public}d", errorCode);
50    }
51
52    CaptureSession_Callbacks* GetCaptureSessionRegister(void)
53    {
54        static CaptureSession_Callbacks captureSessionCallbacks = {
55            .onFocusStateChange = CaptureSessionOnFocusStateChange,
56            .onError = CaptureSessionOnError
57        };
58        return &captureSessionCallbacks;
59    }
60
61    void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
62    {
63        OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
64    }
65
66    void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
67    {
68        OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
69    }
70
71    void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
72    {
73        OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
74    }
75
76    VideoOutput_Callbacks* GetVideoOutputListener(void)
77    {
78        static VideoOutput_Callbacks videoOutputListener = {
79            .onFrameStart = VideoOutputOnFrameStart,
80            .onFrameEnd = VideoOutputOnFrameEnd,
81            .onError = VideoOutputOnError
82        };
83        return &videoOutputListener;
84    }
85
86    void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
87    {
88        OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
89    }
90
91    CameraManager_Callbacks* GetCameraManagerListener()
92    {
93        static CameraManager_Callbacks cameraManagerListener = {
94            .onCameraStatus = CameraManagerStatusCallback
95        };
96        return &cameraManagerListener;
97    }
98
99    NDKCamera::NDKCamera(char *previewId, char *videoId)
100    {
101        Camera_Manager* cameraManager = nullptr;
102        Camera_Device* cameras = nullptr;
103        Camera_CaptureSession* captureSession = nullptr;
104        Camera_OutputCapability* cameraOutputCapability = nullptr;
105        Camera_VideoOutput* videoOutput = nullptr;
106        const Camera_Profile* previewProfile = nullptr;
107        const Camera_Profile* photoProfile = nullptr;
108        const Camera_VideoProfile* videoProfile = nullptr;
109        Camera_PreviewOutput* previewOutput = nullptr;
110        Camera_PhotoOutput* photoOutput = nullptr;
111        Camera_Input* cameraInput = nullptr;
112        uint32_t size = 0;
113        uint32_t cameraDeviceIndex = 0;
114        char* videoSurfaceId = videoId;
115        char* previewSurfaceId = previewId;
116        // Create a CameraManager object.
117        Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
118        if (cameraManager == nullptr || ret != CAMERA_OK) {
119        OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraMananger failed.");
120        }
121        // Listen for camera status changes.
122        ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
123        if (ret != CAMERA_OK) {
124        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
125        }
126
127        // Obtain the camera list.
128        ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
129        if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
130        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
131        }
132
133        for (int index = 0; index < size; index++) {
134        OH_LOG_ERROR(LOG_APP, "cameraId  =  %{public}s ", cameras[index].cameraId);              // Obtain the camera ID.
135        OH_LOG_ERROR(LOG_APP, "cameraPosition  =  %{public}d ", cameras[index].cameraPosition);  // Obtain the camera position.
136        OH_LOG_ERROR(LOG_APP, "cameraType  =  %{public}d ", cameras[index].cameraType);          // Obtain the camera type.
137        OH_LOG_ERROR(LOG_APP, "connectionType  =  %{public}d ", cameras[index].connectionType);  // Obtain the camera connection type.
138        }
139
140        // Obtain the output streams supported by the camera.
141        ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
142                                                                &cameraOutputCapability);
143        if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
144        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
145        }
146
147        if (cameraOutputCapability->previewProfilesSize < 0) {
148        OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
149        }
150        previewProfile = cameraOutputCapability->previewProfiles[0];
151
152        if (cameraOutputCapability->photoProfilesSize < 0) {
153        OH_LOG_ERROR(LOG_APP, "photoProfilesSize == null");
154        }
155        photoProfile = cameraOutputCapability->photoProfiles[0];
156
157        if (cameraOutputCapability->videoProfilesSize < 0) {
158        OH_LOG_ERROR(LOG_APP, "videorofilesSize == null");
159        }
160        videoProfile = cameraOutputCapability->videoProfiles[0];
161
162        // Call getVideoSurfaceID() on the TS side.
163
164        // Create a VideoOutput instance.
165        ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput);
166        if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) {
167        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
168        }
169
170        // Listen for video output errors.
171        ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
172        if (ret != CAMERA_OK) {
173        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
174        }
175
176        // Create a session.
177        ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
178        if (captureSession == nullptr || ret != CAMERA_OK) {
179        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
180        }
181        // Listen for session errors.
182        ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
183        if (ret != CAMERA_OK) {
184        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
185        }
186
187        // Start configuration for the session.
188        ret = OH_CaptureSession_BeginConfig(captureSession);
189        if (ret != CAMERA_OK) {
190        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
191        }
192
193        // Create a camera input stream.
194        ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
195        if (cameraInput == nullptr || ret != CAMERA_OK) {
196        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
197        }
198
199        // Listen for camera input errors.
200        ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
201        if (ret != CAMERA_OK) {
202        OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
203        }
204
205        // Open the camera.
206        ret = OH_CameraInput_Open(cameraInput);
207        if (ret != CAMERA_OK) {
208        OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed.");
209        }
210
211        // Add the camera input stream to the session.
212        ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
213        if (ret != CAMERA_OK) {
214        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
215        }
216
217        // Create a preview output stream. For details about the surfaceId parameter, see the XComponent. The preview stream is the surface provided by the XComponent.
218        ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
219        if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
220        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
221        }
222
223        // Add the preview output stream to the session.
224        ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
225        if (ret != CAMERA_OK) {
226        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
227        }
228
229        // Add the video output stream to the session.
230        ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
231        if (ret != CAMERA_OK) {
232        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed.");
233        }
234
235        // Commit the session configuration.
236        ret = OH_CaptureSession_CommitConfig(captureSession);
237        if (ret != CAMERA_OK) {
238        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
239        }
240
241        // Start the session.
242        ret = OH_CaptureSession_Start(captureSession);
243        if (ret != CAMERA_OK) {
244        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
245        }
246
247        // Start the video output stream.
248        ret = OH_VideoOutput_Start(videoOutput);
249        if (ret != CAMERA_OK) {
250        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
251        }
252
253        // Stop the video output stream.
254        ret = OH_VideoOutput_Stop(videoOutput);
255        if (ret != CAMERA_OK) {
256        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
257        }
258
259        // Stop the session.
260        ret = OH_CaptureSession_Stop(captureSession);
261        if (ret == CAMERA_OK) {
262        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
263        } else {
264        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
265        }
266
267        // Release the camera input stream.
268        ret = OH_CameraInput_Close(cameraInput);
269        if (ret == CAMERA_OK) {
270        OH_LOG_INFO(LOG_APP, "OH_CameraInput_Close success ");
271        } else {
272        OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Close failed. %d ", ret);
273        }
274
275        // Release the preview output stream.
276        ret = OH_PreviewOutput_Release(previewOutput);
277        if (ret == CAMERA_OK) {
278        OH_LOG_INFO(LOG_APP, "OH_PreviewOutput_Release success ");
279        } else {
280        OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Release failed. %d ", ret);
281        }
282
283        // Release the video output stream.
284        ret = OH_VideoOutput_Release(videoOutput);
285        if (ret == CAMERA_OK) {
286        OH_LOG_INFO(LOG_APP, "OH_VideoOutput_Release success ");
287        } else {
288        OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Release failed. %d ", ret);
289        }
290
291        // Release the session.
292        ret = OH_CaptureSession_Release(captureSession);
293        if (ret == CAMERA_OK) {
294        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Release success ");
295        } else {
296        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Release failed. %d ", ret);
297        }
298    }
299    ```
300