• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "session/capture_session.h"
17 #include "camera_util.h"
18 #include "hcapture_session_callback_stub.h"
19 #include "input/camera_input.h"
20 #include "media_log.h"
21 #include "output/photo_output.h"
22 #include "output/preview_output.h"
23 #include "output/video_output.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 class CaptureSessionCallback : public HCaptureSessionCallbackStub {
30 public:
31     sptr<CaptureSession> captureSession_ = nullptr;
CaptureSessionCallback()32     CaptureSessionCallback() : captureSession_(nullptr) {
33     }
34 
CaptureSessionCallback(const sptr<CaptureSession> & captureSession)35     explicit CaptureSessionCallback(const sptr<CaptureSession> &captureSession) : captureSession_(captureSession) {
36     }
37 
~CaptureSessionCallback()38     ~CaptureSessionCallback()
39     {
40         captureSession_ = nullptr;
41     }
42 
OnError(int32_t errorCode)43     int32_t OnError(int32_t errorCode) override
44     {
45         MEDIA_INFO_LOG("CaptureSessionCallback::OnError() is called!, errorCode: %{public}d",
46                        errorCode);
47         if (captureSession_ != nullptr && captureSession_->GetApplicationCallback() != nullptr) {
48             captureSession_->GetApplicationCallback()->OnError(errorCode);
49         } else {
50             MEDIA_INFO_LOG("CaptureSessionCallback::ApplicationCallback not set!, Discarding callback");
51         }
52         return CAMERA_OK;
53     }
54 };
55 
CaptureSession(sptr<ICaptureSession> & captureSession)56 CaptureSession::CaptureSession(sptr<ICaptureSession> &captureSession)
57 {
58     captureSession_ = captureSession;
59 }
60 
BeginConfig()61 int32_t CaptureSession::BeginConfig()
62 {
63     return captureSession_->BeginConfig();
64 }
65 
CommitConfig()66 int32_t CaptureSession::CommitConfig()
67 {
68     return captureSession_->CommitConfig();
69 }
70 
AddInput(sptr<CaptureInput> & input)71 int32_t CaptureSession::AddInput(sptr<CaptureInput> &input)
72 {
73     if (input == nullptr) {
74         MEDIA_ERR_LOG("CaptureSession::AddInput input is null");
75         return CAMERA_INVALID_ARG;
76     }
77     return captureSession_->AddInput(((sptr<CameraInput> &)input)->GetCameraDevice());
78 }
79 
AddOutput(sptr<CaptureOutput> & output)80 int32_t CaptureSession::AddOutput(sptr<CaptureOutput> &output)
81 {
82     if (output == nullptr) {
83         MEDIA_ERR_LOG("CaptureSession::AddOutput output is null");
84         return CAMERA_INVALID_ARG;
85     }
86     if (output->GetType() == CAPTURE_OUTPUT_TYPE::PHOTO_OUTPUT) {
87         return captureSession_->AddOutput(((sptr<PhotoOutput> &)output)->GetStreamCapture());
88     } else if (output->GetType() == CAPTURE_OUTPUT_TYPE::PREVIEW_OUTPUT) {
89         return captureSession_->AddOutput(((sptr<PreviewOutput> &)output)->GetStreamRepeat());
90     } else {
91         return captureSession_->AddOutput(((sptr<VideoOutput> &)output)->GetStreamRepeat());
92     }
93 }
94 
RemoveInput(sptr<CaptureInput> & input)95 int32_t CaptureSession::RemoveInput(sptr<CaptureInput> &input)
96 {
97     if (input == nullptr) {
98         MEDIA_ERR_LOG("CaptureSession::RemoveInput input is null");
99         return CAMERA_INVALID_ARG;
100     }
101     return captureSession_->RemoveInput(((sptr<CameraInput> &)input)->GetCameraDevice());
102 }
103 
RemoveOutput(sptr<CaptureOutput> & output)104 int32_t CaptureSession::RemoveOutput(sptr<CaptureOutput> &output)
105 {
106     if (output == nullptr) {
107         MEDIA_ERR_LOG("CaptureSession::RemoveOutput output is null");
108         return CAMERA_INVALID_ARG;
109     }
110     if (output->GetType() == CAPTURE_OUTPUT_TYPE::PHOTO_OUTPUT) {
111         return captureSession_->RemoveOutput(((sptr<PhotoOutput> &)output)->GetStreamCapture());
112     } else if (output->GetType() == CAPTURE_OUTPUT_TYPE::PREVIEW_OUTPUT) {
113         return captureSession_->RemoveOutput(((sptr<PreviewOutput> &)output)->GetStreamRepeat());
114     } else {
115         return captureSession_->RemoveOutput(((sptr<VideoOutput> &)output)->GetStreamRepeat());
116     }
117 }
118 
Start()119 int32_t CaptureSession::Start()
120 {
121     return captureSession_->Start();
122 }
123 
Stop()124 int32_t CaptureSession::Stop()
125 {
126     return captureSession_->Stop();
127 }
128 
SetCallback(std::shared_ptr<SessionCallback> callback)129 void CaptureSession::SetCallback(std::shared_ptr<SessionCallback> callback)
130 {
131     if (callback == nullptr) {
132         MEDIA_ERR_LOG("CaptureSession::SetCallback: Unregistering application callback!");
133     }
134     int32_t errorCode = CAMERA_OK;
135 
136     appCallback_ = callback;
137     if (appCallback_ != nullptr) {
138         if (captureSessionCallback_ == nullptr) {
139             captureSessionCallback_ = new CaptureSessionCallback(this);
140         }
141         errorCode = captureSession_->SetCallback(captureSessionCallback_);
142         if (errorCode != CAMERA_OK) {
143             MEDIA_ERR_LOG("CaptureSession::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
144             captureSessionCallback_ = nullptr;
145             appCallback_ = nullptr;
146         }
147     }
148     return;
149 }
150 
GetApplicationCallback()151 std::shared_ptr<SessionCallback> CaptureSession::GetApplicationCallback()
152 {
153     return appCallback_;
154 }
155 
Release()156 void CaptureSession::Release()
157 {
158     int32_t errCode = captureSession_->Release(0);
159     if (errCode != CAMERA_OK) {
160         MEDIA_ERR_LOG("Failed to Release capture session!, %{public}d", errCode);
161     }
162     return;
163 }
164 } // CameraStandard
165 } // OHOS
166