1# Camera Session Management (C/C++)
2
3Before using the camera application for preview, photographing, 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 photographing.
8  Configuring an input stream is to add a device input, which means that the user selects a camera for photographing. Configuring an output stream is to select a data output mode. For example, to implement photographing, 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.
9
10- 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).
11
12- Control session switching. The application can switch the camera mode by removing and adding output streams. For example, to switch from photographing to video recording, the application must remove the photo output stream and add the video output stream.
13
14After the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
15
16## How to Develop
17
181. Import the NDK.
19     
20   ```c++
21    #include "hilog/log.h"
22    #include "ohcamera/camera.h"
23    #include "ohcamera/camera_input.h"
24    #include "ohcamera/capture_session.h"
25    #include "ohcamera/photo_output.h"
26    #include "ohcamera/preview_output.h"
27    #include "ohcamera/video_output.h"
28    #include "ohcamera/camera_manager.h"
29   ```
30
312. Link the dynamic library in the CMake script.
32
33   ```txt
34    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
35   ```
36
373. Call **OH_CameraManager_CreateCaptureSession()** in the **CameraManager** class to create a session.
38     
39   ```c++
40    Camera_Manager *cameraManager = nullptr;
41    Camera_Input* cameraInput = nullptr;
42    Camera_PreviewOutput* previewOutput = nullptr;
43    Camera_PhotoOutput* photoOutput = nullptr;
44    Camera_VideoOutput* videoOutput = nullptr;
45    Camera_CaptureSession* captureSession = nullptr;
46    Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
47    if (captureSession == nullptr || ret != CAMERA_OK) {
48        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
49    }
50   ```
51
524. Call **OH_CaptureSession_BeginConfig()** in the **CaptureSession** class to start configuration for the session.
53     
54   ```c++
55    ret = OH_CaptureSession_BeginConfig(captureSession);
56    if (ret != CAMERA_OK) {
57        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
58    }
59   ```
60
615. Configure the session. You can call **OH_CaptureSession_AddInput()**, **OH_CaptureSession_AddPreviewOutput()**, and **OH_CaptureSession_AddPhotoOutput()** to add the input and output streams to the session, respectively. The code snippet below uses adding the preview stream **previewOutput** and photo stream **photoOutput** as an example to implement the photographing and preview mode.
62
63     After the configuration, call **commitConfig()** and **start()** in the **CaptureSession** class in sequence to commit the configuration and start the session.
64     
65   ```c++
66    // Add the camera input stream to the session.
67    ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
68    if (ret != CAMERA_OK) {
69        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
70    }
71
72    // Add the preview output stream to the session.
73    ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
74    if (ret != CAMERA_OK) {
75        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
76    }
77
78    // Add the photo output stream to the session.
79    ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
80    if (ret != CAMERA_OK) {
81        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.");
82    }
83
84    // Commit the session configuration.
85    ret = OH_CaptureSession_CommitConfig(captureSession);
86    if (ret != CAMERA_OK) {
87        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
88    }
89
90    // Start the session.
91    ret = OH_CaptureSession_Start(captureSession);
92    if (ret != CAMERA_OK) {
93        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
94    }
95   ```
96
976. Control the session. You can call **stop()** in the **CaptureSession** class to stop the session, and call **removeOutput()** and **addOutput()** 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 photographing to recording.
98     
99   ```c++
100    ret = OH_CaptureSession_Stop(captureSession);
101    if (ret == CAMERA_OK) {
102        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
103    } else {
104        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
105    }
106    ret = OH_CaptureSession_BeginConfig(captureSession);
107    if (ret != CAMERA_OK) {
108        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
109    }
110    // Remove the photo output stream from the session.
111    ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput);
112    if (ret == CAMERA_OK) {
113        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
114    } else {
115        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
116    }
117     // Add the video output stream to the session.
118    ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
119    if (ret == CAMERA_OK) {
120        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
121    } else {
122        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
123    }
124   ```
125