1# Camera Session Management (C/C++)
2
3Before using the camera application for preview, photo capture, video recording, and metadata management, you must create a camera session.
4
5You can implement the following functions in the session:
6
7- Configure the camera input and output streams. This is mandatory for photo capture.
8
9  Configuring an input stream is to add a device input, which means that the user selects a camera for photo capture. Configuring an output stream is to select a data output mode. For example, to implement photo capture, you must configure both the preview stream and photo stream as the output stream. The data of the preview stream is displayed on the **XComponent**, and that of the photo stream is saved to the Gallery application through the ImageReceiver API.
10
11- Perform more operations on the camera hardware. For example, add the flash and adjust the focal length. For details about the supported configurations and APIs, see [Camera API Reference](../../reference/apis-camera-kit/_o_h___camera.md).
12
13- Control session switching. The application can switch the camera mode by removing and adding output streams. For example, to switch from photo capture to video recording, the application must remove the photo output stream and add the video output stream.
14
15After the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
16
17## How to Develop
18
191. Import the NDK.
20
21   ```c++
22    #include "hilog/log.h"
23    #include "ohcamera/camera.h"
24    #include "ohcamera/camera_input.h"
25    #include "ohcamera/capture_session.h"
26    #include "ohcamera/photo_output.h"
27    #include "ohcamera/preview_output.h"
28    #include "ohcamera/video_output.h"
29    #include "ohcamera/camera_manager.h"
30   ```
31
322. Link the dynamic library in the CMake script.
33
34   ```txt
35    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
36   ```
37
383. Call [OH_CameraManager_CreateCaptureSession()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_createcapturesession) in the CameraManager class to create a session.
39
40   ```c++
41    Camera_Manager *cameraManager = nullptr;
42    Camera_Input* cameraInput = nullptr;
43    Camera_PreviewOutput* previewOutput = nullptr;
44    Camera_PhotoOutput* photoOutput = nullptr;
45    Camera_VideoOutput* videoOutput = nullptr;
46    Camera_CaptureSession* captureSession = nullptr;
47    Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
48    if (captureSession == nullptr || ret != CAMERA_OK) {
49        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
50    }
51   ```
52
534. Call [OH_CaptureSession_SetSessionMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_setsessionmode) in the CaptureSession class to set the session mode.
54
55   ```c++
56    ret = OH_CaptureSession_SetSessionMode(captureSession, NORMAL_PHOTO);
57    if (ret != CAMERA_OK) {
58        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetSessionMode failed.");
59    }
60   ```
61
625. Call [OH_CaptureSession_BeginConfig()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_beginconfig) in the CaptureSession class to start configuration for the session.
63
64   ```c++
65    ret = OH_CaptureSession_BeginConfig(captureSession);
66    if (ret != CAMERA_OK) {
67        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
68    }
69   ```
70
716. Configure the session. You can call [OH_CaptureSession_AddInput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_addinput) to add an input stream to the session, and call [OH_CaptureSession_AddPreviewOutput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_addpreviewoutput) and [OH_CaptureSession_AddPhotoOutput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_addphotooutput) to add output streams to the session. The code snippet below uses adding the preview stream **previewOutput** and photo stream **photoOutput** as an example to implement the photo capture and preview mode.
72
73     After the configuration, call [OH_CaptureSession_CommitConfig()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_commitconfig) and [OH_CaptureSession_Start()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_start) in the CaptureSession class in sequence to commit the configuration and start the session.
74
75   ```c++
76    // Add the camera input stream to the session.
77    ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
78    if (ret != CAMERA_OK) {
79        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
80    }
81
82    // Add the preview output stream to the session.
83    ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
84    if (ret != CAMERA_OK) {
85        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
86    }
87
88    // Add the photo output stream to the session.
89    ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
90    if (ret != CAMERA_OK) {
91        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.");
92    }
93
94    // Commit the session configuration.
95    ret = OH_CaptureSession_CommitConfig(captureSession);
96    if (ret != CAMERA_OK) {
97        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
98    }
99
100    // Start the session.
101    ret = OH_CaptureSession_Start(captureSession);
102    if (ret != CAMERA_OK) {
103        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
104    }
105   ```
106
1077. Control the session. You can call [OH_CaptureSession_Stop()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_stop) in the CaptureSession class to stop the session, and call [OH_CaptureSession_RemovePhotoOutput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_removephotooutput) and [OH_CaptureSession_AddVideoOutput()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_addvideooutput) in this class to switch to another session. The code snippet below uses removing the photo stream **photoOutput** and adding the video stream **videoOutput** as an example to complete the switching from photo capture to video recording.
108
109   ```c++
110    ret = OH_CaptureSession_Stop(captureSession);
111    if (ret == CAMERA_OK) {
112        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
113    } else {
114        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
115    }
116    ret = OH_CaptureSession_BeginConfig(captureSession);
117    if (ret != CAMERA_OK) {
118        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
119    }
120    // Remove the photo output stream from the session.
121    ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput);
122    if (ret == CAMERA_OK) {
123        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
124    } else {
125        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
126    }
127     // Add the video output stream to the session.
128    ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
129    if (ret == CAMERA_OK) {
130        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
131    } else {
132        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
133    }
134   ```
135