• 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 "output/preview_output.h"
17 #include "camera_util.h"
18 #include "hstream_repeat_callback_stub.h"
19 #include "camera_log.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
PreviewOutput(sptr<IStreamRepeat> & streamRepeat)23 PreviewOutput::PreviewOutput(sptr<IStreamRepeat> &streamRepeat)
24     : CaptureOutput(CAPTURE_OUTPUT_TYPE_PREVIEW, StreamType::REPEAT, streamRepeat) {
25 }
26 
~PreviewOutput()27 PreviewOutput::~PreviewOutput()
28 {
29 }
30 
Release()31 int32_t PreviewOutput::Release()
32 {
33     {
34         std::lock_guard<std::mutex> lock(outputCallbackMutex_);
35         svcCallback_ = nullptr;
36         appCallback_ = nullptr;
37     }
38     std::lock_guard<std::mutex> lock(asyncOpMutex_);
39     MEDIA_DEBUG_LOG("Enter Into PreviewOutput::Release");
40     if (GetStream() == nullptr) {
41         MEDIA_ERR_LOG("PreviewOutput Failed to Release!, GetStream is nullptr");
42         return CameraErrorCode::SERVICE_FATL_ERROR;
43     }
44     auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
45     int32_t errCode = CAMERA_UNKNOWN_ERROR;
46     if (itemStream) {
47         errCode = itemStream->Release();
48         if (errCode != CAMERA_OK) {
49             MEDIA_ERR_LOG("Failed to release PreviewOutput!, errCode: %{public}d", errCode);
50         }
51     } else {
52         MEDIA_ERR_LOG("PreviewOutput::Release() itemStream is nullptr");
53     }
54     CaptureOutput::Release();
55     return ServiceToCameraError(errCode);
56 }
57 
OnFrameStarted()58 int32_t PreviewOutputCallbackImpl::OnFrameStarted()
59 {
60     CAMERA_SYNC_TRACE;
61     if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
62         previewOutput_->GetApplicationCallback()->OnFrameStarted();
63     } else {
64         MEDIA_INFO_LOG("Discarding PreviewOutputCallbackImpl::OnFrameStarted callback in preview");
65     }
66     return CAMERA_OK;
67 }
68 
OnFrameEnded(int32_t frameCount)69 int32_t PreviewOutputCallbackImpl::OnFrameEnded(int32_t frameCount)
70 {
71     CAMERA_SYNC_TRACE;
72     if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
73         previewOutput_->GetApplicationCallback()->OnFrameEnded(frameCount);
74     } else {
75         MEDIA_INFO_LOG("Discarding PreviewOutputCallbackImpl::OnFrameEnded callback in preview");
76     }
77     return CAMERA_OK;
78 }
79 
OnFrameError(int32_t errorCode)80 int32_t PreviewOutputCallbackImpl::OnFrameError(int32_t errorCode)
81 {
82     if (previewOutput_ != nullptr && previewOutput_->GetApplicationCallback() != nullptr) {
83         previewOutput_->GetApplicationCallback()->OnError(errorCode);
84     } else {
85         MEDIA_INFO_LOG("Discarding PreviewOutputCallbackImpl::OnFrameError callback in preview");
86     }
87     return CAMERA_OK;
88 }
89 
AddDeferredSurface(sptr<Surface> surface)90 void PreviewOutput::AddDeferredSurface(sptr<Surface> surface)
91 {
92     MEDIA_INFO_LOG("PreviewOutput::AddDeferredSurface called");
93     if (surface == nullptr) {
94         MEDIA_ERR_LOG("PreviewOutput::AddDeferredSurface surface is null");
95         return;
96     }
97     auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
98     if (!itemStream) {
99         MEDIA_ERR_LOG("PreviewOutput::AddDeferredSurface itemStream is nullptr");
100         return;
101     }
102     itemStream->AddDeferredSurface(surface->GetProducer());
103 }
104 
Start()105 int32_t PreviewOutput::Start()
106 {
107     std::lock_guard<std::mutex> lock(asyncOpMutex_);
108     MEDIA_DEBUG_LOG("Enter Into PreviewOutput::Start");
109     CaptureSession* captureSession = GetSession();
110     if (captureSession == nullptr || !captureSession->IsSessionCommited()) {
111         MEDIA_ERR_LOG("PreviewOutput Failed to Start!, session not config");
112         return CameraErrorCode::SESSION_NOT_CONFIG;
113     }
114     if (GetStream() == nullptr) {
115         MEDIA_ERR_LOG("PreviewOutput Failed to Start!, GetStream is nullptr");
116         return CameraErrorCode::SERVICE_FATL_ERROR;
117     }
118     auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
119     int32_t errCode = CAMERA_UNKNOWN_ERROR;
120     if (itemStream) {
121         errCode = itemStream->Start();
122         if (errCode != CAMERA_OK) {
123             MEDIA_ERR_LOG("PreviewOutput Failed to Start!, errCode: %{public}d", errCode);
124         }
125     } else {
126         MEDIA_ERR_LOG("PreviewOutput::Start itemStream is nullptr");
127     }
128     return ServiceToCameraError(errCode);
129 }
130 
Stop()131 int32_t PreviewOutput::Stop()
132 {
133     std::lock_guard<std::mutex> lock(asyncOpMutex_);
134     MEDIA_DEBUG_LOG("Enter Into PreviewOutput::Stop");
135     if (GetStream() == nullptr) {
136         MEDIA_ERR_LOG("PreviewOutput Failed to Stop!, GetStream is nullptr");
137         return CameraErrorCode::SERVICE_FATL_ERROR;
138     }
139     auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
140     int32_t errCode = CAMERA_UNKNOWN_ERROR;
141     if (itemStream) {
142         errCode = itemStream->Stop();
143         if (errCode != CAMERA_OK) {
144             MEDIA_ERR_LOG("PreviewOutput Failed to Stop!, errCode: %{public}d", errCode);
145         }
146     } else {
147         MEDIA_ERR_LOG("PreviewOutput::Stop itemStream is nullptr");
148     }
149     return ServiceToCameraError(errCode);
150 }
151 
SetCallback(std::shared_ptr<PreviewStateCallback> callback)152 void PreviewOutput::SetCallback(std::shared_ptr<PreviewStateCallback> callback)
153 {
154     std::lock_guard<std::mutex> lock(outputCallbackMutex_);
155     appCallback_ = callback;
156     if (appCallback_ != nullptr) {
157         if (svcCallback_ == nullptr) {
158             svcCallback_ = new(std::nothrow) PreviewOutputCallbackImpl(this);
159             if (svcCallback_ == nullptr) {
160                 MEDIA_ERR_LOG("new PreviewOutputCallbackImpl Failed to register callback");
161                 appCallback_ = nullptr;
162                 return;
163             }
164         }
165         if (GetStream() == nullptr) {
166             MEDIA_ERR_LOG("PreviewOutput Failed to SetCallback!, GetStream is nullptr");
167             return;
168         }
169         auto itemStream = static_cast<IStreamRepeat *>(GetStream().GetRefPtr());
170         int32_t errorCode = CAMERA_OK;
171         if (itemStream) {
172             errorCode = itemStream->SetCallback(svcCallback_);
173         } else {
174             MEDIA_ERR_LOG("PreviewOutput::SetCallback itemStream is nullptr");
175         }
176         if (errorCode != CAMERA_OK) {
177             MEDIA_ERR_LOG("PreviewOutput::SetCallback: Failed to register callback, errorCode: %{public}d", errorCode);
178             svcCallback_ = nullptr;
179             appCallback_ = nullptr;
180         }
181     }
182     return;
183 }
184 
GetApplicationCallback()185 std::shared_ptr<PreviewStateCallback> PreviewOutput::GetApplicationCallback()
186 {
187     std::lock_guard<std::mutex> lock(outputCallbackMutex_);
188     return appCallback_;
189 }
190 } // CameraStandard
191 } // OHOS
192 
193