• 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 "hstream_operator.h"
17 
18 #include <algorithm>
19 #include <atomic>
20 #include <cerrno>
21 #include <cinttypes>
22 #include <cstddef>
23 #include <cstdint>
24 #include <functional>
25 #include <memory>
26 #include <mutex>
27 #include <new>
28 #include <sched.h>
29 #include <string>
30 #include <sync_fence.h>
31 #include <utility>
32 #include <vector>
33 
34 #include "avcodec_task_manager.h"
35 #include "blocking_queue.h"
36 #include "bundle_mgr_interface.h"
37 #include "camera_dynamic_loader.h"
38 #include "camera_info_dumper.h"
39 #include "camera_log.h"
40 #include "camera_report_uitls.h"
41 #include "camera_server_photo_proxy.h"
42 #include "camera_service_ipc_interface_code.h"
43 #include "camera_timer.h"
44 #include "camera_util.h"
45 #include "datetime_ex.h"
46 #include "deferred_processing_service.h"
47 #include "display/composer/v1_1/display_composer_type.h"
48 #include "display_manager.h"
49 #include "errors.h"
50 #include "fixed_size_list.h"
51 #include "hcamera_device_manager.h"
52 #include "hcamera_restore_param.h"
53 #include "hstream_capture.h"
54 #include "hstream_common.h"
55 #include "hstream_depth_data.h"
56 #include "hstream_metadata.h"
57 #include "hstream_repeat.h"
58 #include "icamera_util.h"
59 #include "icapture_session.h"
60 #include "iconsumer_surface.h"
61 #include "image_type.h"
62 #include "ipc_skeleton.h"
63 #include "iservice_registry.h"
64 #include "istream_common.h"
65 #include "media_library/photo_asset_interface.h"
66 #include "media_library/photo_asset_proxy.h"
67 #include "metadata_utils.h"
68 #include "moving_photo/moving_photo_surface_wrapper.h"
69 #include "moving_photo_video_cache.h"
70 #include "parameters.h"
71 #include "picture_interface.h"
72 #include "refbase.h"
73 #include "smooth_zoom.h"
74 #include "surface.h"
75 #include "surface_buffer.h"
76 #include "system_ability_definition.h"
77 #include "v1_0/types.h"
78 #include "camera_report_dfx_uitls.h"
79 #include "hstream_operator_manager.h"
80 #include "res_type.h"
81 #include "res_sched_client.h"
82 #include "camera_device_ability_items.h"
83 
84 using namespace OHOS::AAFwk;
85 namespace OHOS {
86 namespace CameraStandard {
87 using namespace OHOS::HDI::Display::Composer::V1_1;
88 namespace {
89 #ifdef CAMERA_USE_SENSOR
90 constexpr int32_t POSTURE_INTERVAL = 100000000; //100ms
91 constexpr int VALID_INCLINATION_ANGLE_THRESHOLD_COEFFICIENT = 3;
92 #endif
93 static GravityData gravityData = {0.0, 0.0, 0.0};
94 static int32_t sensorRotation = 0;
95 } // namespace
96 
NewInstance(const uint32_t callerToken,int32_t opMode)97 sptr<HStreamOperator> HStreamOperator::NewInstance(const uint32_t callerToken, int32_t opMode)
98 {
99     sptr<HStreamOperator> hStreamOperator = new HStreamOperator();
100     CHECK_ERROR_RETURN_RET(hStreamOperator->Initialize(callerToken, opMode) == CAMERA_OK, hStreamOperator);
101     return nullptr;
102 }
103 
Initialize(const uint32_t callerToken,int32_t opMode)104 int32_t HStreamOperator::Initialize(const uint32_t callerToken, int32_t opMode)
105 {
106     pid_ = IPCSkeleton::GetCallingPid();
107     uid_ = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
108     callerToken_ = callerToken;
109     opMode_ = opMode;
110     MEDIA_INFO_LOG(
111         "HStreamOperator::opMode_= %{public}d", opMode_);
112     InitDefaultColortSpace(static_cast<SceneMode>(opMode));
113     return CAMERA_OK;
114 }
115 
InitDefaultColortSpace(SceneMode opMode)116 void HStreamOperator::InitDefaultColortSpace(SceneMode opMode)
117 {
118     static const std::unordered_map<SceneMode, ColorSpace> colorSpaceMap = {
119         {SceneMode::NORMAL, ColorSpace::SRGB},
120         {SceneMode::CAPTURE, ColorSpace::SRGB},
121         {SceneMode::VIDEO, ColorSpace::BT709_LIMIT},
122         {SceneMode::PORTRAIT, ColorSpace::DISPLAY_P3},
123         {SceneMode::NIGHT, ColorSpace::DISPLAY_P3},
124         {SceneMode::PROFESSIONAL, ColorSpace::DISPLAY_P3},
125         {SceneMode::SLOW_MOTION, ColorSpace::BT709_LIMIT},
126         {SceneMode::SCAN, ColorSpace::BT709_LIMIT},
127         {SceneMode::CAPTURE_MACRO, ColorSpace::DISPLAY_P3},
128         {SceneMode::VIDEO_MACRO, ColorSpace::BT709_LIMIT},
129         {SceneMode::PROFESSIONAL_PHOTO, ColorSpace::DISPLAY_P3},
130         {SceneMode::PROFESSIONAL_VIDEO, ColorSpace::BT709_LIMIT},
131         {SceneMode::HIGH_FRAME_RATE, ColorSpace::BT709_LIMIT},
132         {SceneMode::HIGH_RES_PHOTO, ColorSpace::DISPLAY_P3},
133         {SceneMode::SECURE, ColorSpace::SRGB},
134         {SceneMode::QUICK_SHOT_PHOTO, ColorSpace::DISPLAY_P3},
135         {SceneMode::LIGHT_PAINTING, ColorSpace::DISPLAY_P3},
136         {SceneMode::PANORAMA_PHOTO, ColorSpace::DISPLAY_P3},
137         {SceneMode::TIMELAPSE_PHOTO, ColorSpace::DISPLAY_P3},
138         {SceneMode::APERTURE_VIDEO, ColorSpace::BT709_LIMIT},
139         {SceneMode::FLUORESCENCE_PHOTO, ColorSpace::DISPLAY_P3},
140     };
141     auto it = colorSpaceMap.find(opMode);
142     if (it != colorSpaceMap.end()) {
143         currColorSpace_ = it->second;
144     } else {
145         currColorSpace_ = ColorSpace::SRGB;
146     }
147     MEDIA_DEBUG_LOG("HStreamOperator::InitDefaultColortSpace colorSpace:%{public}d", currColorSpace_);
148 }
149 
HStreamOperator()150 HStreamOperator::HStreamOperator()
151 {
152     pid_ = 0;
153     uid_ = 0;
154     callerToken_ = 0;
155     opMode_ = 0;
156 }
157 
HStreamOperator(const uint32_t callingTokenId,int32_t opMode)158 HStreamOperator::HStreamOperator(const uint32_t callingTokenId, int32_t opMode)
159 {
160     Initialize(callingTokenId, opMode);
161     ResetHdiStreamId();
162 }
163 
~HStreamOperator()164 HStreamOperator::~HStreamOperator()
165 {
166     CAMERA_SYNC_TRACE;
167     Release();
168 }
169 
GetCurrentStreamInfos(std::vector<StreamInfo_V1_1> & streamInfos)170 int32_t HStreamOperator::GetCurrentStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos)
171 {
172     auto streams = streamContainer_.GetAllStreams();
173     for (auto& stream : streams) {
174         if (stream) {
175             StreamInfo_V1_1 curStreamInfo;
176             stream->SetStreamInfo(curStreamInfo);
177             CHECK_EXECUTE(stream->GetStreamType() != StreamType::METADATA, streamInfos.push_back(curStreamInfo));
178         }
179     }
180     return CAMERA_OK;
181 }
182 
AddOutputStream(sptr<HStreamCommon> stream)183 int32_t HStreamOperator::AddOutputStream(sptr<HStreamCommon> stream)
184 {
185     CAMERA_SYNC_TRACE;
186     CHECK_ERROR_RETURN_RET_LOG(stream == nullptr, CAMERA_INVALID_ARG,
187         "HStreamOperator::AddOutputStream stream is null");
188     MEDIA_INFO_LOG("HStreamOperator::AddOutputStream streamId:%{public}d streamType:%{public}d",
189         stream->GetFwkStreamId(), stream->GetStreamType());
190     CHECK_ERROR_RETURN_RET_LOG(
191         stream->GetFwkStreamId() == STREAM_ID_UNSET && stream->GetStreamType() != StreamType::METADATA,
192         CAMERA_INVALID_ARG, "HStreamOperator::AddOutputStream stream is released!");
193     bool isAddSuccess = streamContainer_.AddStream(stream);
194     CHECK_ERROR_RETURN_RET_LOG(!isAddSuccess, CAMERA_INVALID_SESSION_CFG,
195         "HStreamOperator::AddOutputStream add stream fail");
196     if (stream->GetStreamType() == StreamType::CAPTURE) {
197         auto captureStream = CastStream<HStreamCapture>(stream);
198         captureStream->SetMode(opMode_);
199         CameraDynamicLoader::LoadDynamiclibAsync(MEDIA_LIB_SO); // cache dynamiclib
200     }
201     MEDIA_INFO_LOG("HCaptureSession::AddOutputStream stream colorSpace:%{public}d", currColorSpace_);
202     stream->SetColorSpace(currColorSpace_);
203     return CAMERA_OK;
204 }
205 
StartMovingPhotoStream(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)206 void HStreamOperator::StartMovingPhotoStream(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings)
207 {
208     int32_t errorCode = 0;
209     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
210     bool isPreviewStarted = false;
211     for (auto& item : repeatStreams) {
212         auto curStreamRepeat = CastStream<HStreamRepeat>(item);
213         auto repeatType = curStreamRepeat->GetRepeatStreamType();
214         if (repeatType != RepeatStreamType::PREVIEW) {
215             continue;
216         }
217         if (curStreamRepeat->GetPreparedCaptureId() != CAPTURE_ID_UNSET && curStreamRepeat->producer_ != nullptr) {
218             isPreviewStarted = true;
219             break;
220         }
221     }
222     CHECK_ERROR_RETURN_LOG(!isPreviewStarted, "EnableMovingPhoto, preview is not streaming");
223     for (auto& item : repeatStreams) {
224         auto curStreamRepeat = CastStream<HStreamRepeat>(item);
225         auto repeatType = curStreamRepeat->GetRepeatStreamType();
226         if (repeatType != RepeatStreamType::LIVEPHOTO) {
227             continue;
228         }
229         if (isSetMotionPhoto_) {
230             errorCode = curStreamRepeat->Start(settings);
231             #ifdef MOVING_PHOTO_ADD_AUDIO
232             std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
233             audioCapturerSession_ != nullptr && audioCapturerSession_->StartAudioCapture();
234             #endif
235         } else {
236             errorCode = curStreamRepeat->Stop();
237             StopMovingPhoto();
238         }
239         break;
240     }
241     MEDIA_INFO_LOG("HStreamOperator::StartMovingPhotoStream result:%{public}d", errorCode);
242 }
243 
244 class DisplayRotationListener : public OHOS::Rosen::DisplayManager::IDisplayListener {
245 public:
DisplayRotationListener()246     explicit DisplayRotationListener() {};
247     virtual ~DisplayRotationListener() = default;
OnCreate(OHOS::Rosen::DisplayId)248     void OnCreate(OHOS::Rosen::DisplayId) override {}
OnDestroy(OHOS::Rosen::DisplayId)249     void OnDestroy(OHOS::Rosen::DisplayId) override {}
OnChange(OHOS::Rosen::DisplayId displayId)250     void OnChange(OHOS::Rosen::DisplayId displayId) override
251     {
252         sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
253         if (display == nullptr) {
254             MEDIA_INFO_LOG("Get display info failed, display:%{public}" PRIu64 "", displayId);
255             display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
256             CHECK_ERROR_RETURN_LOG(display == nullptr, "Get display info failed, display is nullptr");
257         }
258         {
259             Rosen::Rotation currentRotation = display->GetRotation();
260             std::lock_guard<std::mutex> lock(mStreamManagerLock_);
261             for (auto& repeatStream : repeatStreamList_) {
262                 CHECK_EXECUTE(repeatStream, repeatStream->SetStreamTransform(static_cast<int>(currentRotation)));
263             }
264         }
265     }
266 
AddHstreamRepeatForListener(sptr<HStreamRepeat> repeatStream)267     void AddHstreamRepeatForListener(sptr<HStreamRepeat> repeatStream)
268     {
269         std::lock_guard<std::mutex> lock(mStreamManagerLock_);
270         CHECK_EXECUTE(repeatStream, repeatStreamList_.push_back(repeatStream));
271     }
272 
RemoveHstreamRepeatForListener(sptr<HStreamRepeat> repeatStream)273     void RemoveHstreamRepeatForListener(sptr<HStreamRepeat> repeatStream)
274     {
275         std::lock_guard<std::mutex> lock(mStreamManagerLock_);
276         if (repeatStream) {
277             repeatStreamList_.erase(
278                 std::remove(repeatStreamList_.begin(), repeatStreamList_.end(), repeatStream), repeatStreamList_.end());
279         }
280     }
281 
282 public:
283     std::list<sptr<HStreamRepeat>> repeatStreamList_;
284     std::mutex mStreamManagerLock_;
285 };
286 
RegisterDisplayListener(sptr<HStreamRepeat> repeat)287 void HStreamOperator::RegisterDisplayListener(sptr<HStreamRepeat> repeat)
288 {
289     if (displayListener_ == nullptr) {
290         displayListener_ = new DisplayRotationListener();
291         OHOS::Rosen::DisplayManager::GetInstance().RegisterDisplayListener(displayListener_);
292     }
293     displayListener_->AddHstreamRepeatForListener(repeat);
294 }
295 
UnRegisterDisplayListener(sptr<HStreamRepeat> repeatStream)296 void HStreamOperator::UnRegisterDisplayListener(sptr<HStreamRepeat> repeatStream)
297 {
298     CHECK_EXECUTE(displayListener_, displayListener_->RemoveHstreamRepeatForListener(repeatStream));
299 }
300 
SetPreviewRotation(std::string & deviceClass)301 int32_t HStreamOperator::SetPreviewRotation(std::string &deviceClass)
302 {
303     enableStreamRotate_ = true;
304     deviceClass_ = deviceClass;
305     return CAMERA_OK;
306 }
307 
AddOutput(StreamType streamType,sptr<IStreamCommon> stream)308 int32_t HStreamOperator::AddOutput(StreamType streamType, sptr<IStreamCommon> stream)
309 {
310     int32_t errorCode = CAMERA_INVALID_ARG;
311     if (stream == nullptr) {
312         MEDIA_ERR_LOG("HStreamOperator::AddOutput stream is null");
313         CameraReportUtils::ReportCameraError(
314             "HStreamOperator::AddOutput", errorCode, false, CameraReportUtils::GetCallerInfo());
315         return errorCode;
316     }
317     // Temp hack to fix the library linking issue
318     sptr<IConsumerSurface> captureSurface = IConsumerSurface::Create();
319     if (streamType == StreamType::CAPTURE) {
320         HStreamCapture* captureSteam = static_cast<HStreamCapture*>(stream.GetRefPtr());
321         errorCode = AddOutputStream(captureSteam);
322         captureSteam->SetStreamOperator(this);
323     } else if (streamType == StreamType::REPEAT) {
324         HStreamRepeat* repeatSteam = static_cast<HStreamRepeat*>(stream.GetRefPtr());
325         if (enableStreamRotate_ && repeatSteam != nullptr &&
326             repeatSteam->GetRepeatStreamType() == RepeatStreamType::PREVIEW) {
327             RegisterDisplayListener(repeatSteam);
328             repeatSteam->SetPreviewRotation(deviceClass_);
329         }
330         errorCode = AddOutputStream(repeatSteam);
331     } else if (streamType == StreamType::METADATA) {
332         errorCode = AddOutputStream(static_cast<HStreamMetadata*>(stream.GetRefPtr()));
333     } else if (streamType == StreamType::DEPTH) {
334         errorCode = AddOutputStream(static_cast<HStreamDepthData*>(stream.GetRefPtr()));
335     }
336     MEDIA_INFO_LOG("CaptureSession::AddOutput with with %{public}d, rc = %{public}d", streamType, errorCode);
337     return errorCode;
338 }
339 
RemoveOutput(StreamType streamType,sptr<IStreamCommon> stream)340 int32_t HStreamOperator::RemoveOutput(StreamType streamType, sptr<IStreamCommon> stream)
341 {
342     int32_t errorCode = CAMERA_INVALID_ARG;
343     if (stream == nullptr) {
344         MEDIA_ERR_LOG("HStreamOperator::RemoveOutput stream is null");
345         CameraReportUtils::ReportCameraError(
346             "HStreamOperator::RemoveOutput", errorCode, false, CameraReportUtils::GetCallerInfo());
347         return errorCode;
348     }
349     if (streamType == StreamType::CAPTURE) {
350         errorCode = RemoveOutputStream(static_cast<HStreamCapture*>(stream.GetRefPtr()));
351     } else if (streamType == StreamType::REPEAT) {
352         HStreamRepeat* repeatSteam = static_cast<HStreamRepeat*>(stream.GetRefPtr());
353         if (enableStreamRotate_ && repeatSteam != nullptr &&
354             repeatSteam->GetRepeatStreamType() == RepeatStreamType::PREVIEW) {
355             UnRegisterDisplayListener(repeatSteam);
356         }
357         errorCode = RemoveOutputStream(repeatSteam);
358     } else if (streamType == StreamType::METADATA) {
359         errorCode = RemoveOutputStream(static_cast<HStreamMetadata*>(stream.GetRefPtr()));
360     }
361     return errorCode;
362 }
363 
RemoveOutputStream(sptr<HStreamCommon> stream)364 int32_t HStreamOperator::RemoveOutputStream(sptr<HStreamCommon> stream)
365 {
366     CAMERA_SYNC_TRACE;
367     CHECK_ERROR_RETURN_RET_LOG(stream == nullptr, CAMERA_INVALID_ARG,
368         "HStreamOperator::RemoveOutputStream stream is null");
369     MEDIA_INFO_LOG("HStreamOperator::RemoveOutputStream,streamType:%{public}d, streamId:%{public}d",
370         stream->GetStreamType(), stream->GetFwkStreamId());
371     bool isRemoveSuccess = streamContainer_.RemoveStream(stream);
372     CHECK_ERROR_RETURN_RET_LOG(!isRemoveSuccess, CAMERA_INVALID_SESSION_CFG,
373         "HStreamOperator::RemoveOutputStream Invalid output");
374     if (stream->GetStreamType() != StreamType::CAPTURE) {
375         return CAMERA_OK;
376     }
377     HStreamCapture* captureSteam = static_cast<HStreamCapture*>(stream.GetRefPtr());
378     if (captureSteam != nullptr && captureSteam->IsHasEnableOfflinePhoto()) {
379         bool isAddSuccess = streamContainerOffline_.AddStream(stream);
380         MEDIA_INFO_LOG("HStreamOperator::RemoveOutputStream,streamsize:%{public}zu",
381             streamContainerOffline_.Size());
382         if (streamContainerOffline_.Size() > 0) {
383             auto allStream = streamContainerOffline_.GetAllStreams();
384             for (auto& streamTemp : allStream) {
385                 MEDIA_INFO_LOG("HStreamOperator::RemoveOutputStream fkwstreamid is %{public}d hdistreamid"
386                     "is %{public}d", streamTemp->GetFwkStreamId(), streamTemp->GetHdiStreamId());
387             }
388         }
389         captureSteam->SwitchToOffline();
390         CHECK_ERROR_RETURN_RET_LOG(!isAddSuccess, CAMERA_INVALID_SESSION_CFG,
391             "HStreamOperator::AddOutputStream add stream fail");
392     }
393     return CAMERA_OK;
394 }
395 
GetAllStreams()396 std::list<sptr<HStreamCommon>> HStreamOperator::GetAllStreams()
397 {
398     return streamContainer_.GetAllStreams();
399 }
400 
GetStreamsSize()401 int32_t HStreamOperator::GetStreamsSize()
402 {
403     return streamContainer_.Size() ;
404 }
405 
GetStreamOperator()406 void  HStreamOperator::GetStreamOperator()
407 {
408     if (cameraDevice_ == nullptr) {
409         MEDIA_INFO_LOG("HStreamOperator::GetStreamOperator cameraDevice_ is nullptr");
410         return;
411     }
412     std::lock_guard<std::mutex> lock(streamOperatorLock_);
413     if (streamOperator_ == nullptr) {
414         cameraDevice_->GetStreamOperator(this, streamOperator_);
415     }
416 }
417 
IsOfflineCapture()418 bool HStreamOperator::IsOfflineCapture()
419 {
420     std::list<sptr<HStreamCommon>> allStream = streamContainer_.GetAllStreams();
421     std::list<sptr<HStreamCommon>> allOfflineStreams = streamContainerOffline_.GetAllStreams();
422     allStream.insert(allStream.end(), allOfflineStreams.begin(), allOfflineStreams.end());
423     for (auto& stream : allStream) {
424         if (stream->GetStreamType() != StreamType::CAPTURE) {
425             continue;
426         }
427         HStreamCapture* captureStream = static_cast<HStreamCapture*>(stream.GetRefPtr());
428         if (captureStream->IsHasSwitchToOffline()) {
429             return true;
430         }
431     }
432     return false;
433 }
434 
LinkInputAndOutputs(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings,int32_t opMode)435 int32_t HStreamOperator::LinkInputAndOutputs(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings,
436     int32_t opMode)
437 {
438     int32_t rc;
439     std::vector<StreamInfo_V1_1> allStreamInfos;
440     auto allStream = streamContainer_.GetAllStreams();
441     for (auto& stream : allStream) {
442         rc = stream->LinkInput(streamOperator_, settings);
443         if (rc == CAMERA_OK) {
444             CHECK_EXECUTE(stream->GetHdiStreamId() == STREAM_ID_UNSET,
445                 stream->SetHdiStreamId(GenerateHdiStreamId()));
446         }
447         MEDIA_INFO_LOG(
448             "HStreamOperator::LinkInputAndOutputs streamType:%{public}d, streamId:%{public}d ,hdiStreamId:%{public}d",
449             stream->GetStreamType(), stream->GetFwkStreamId(), stream->GetHdiStreamId());
450         CHECK_ERROR_RETURN_RET_LOG(rc != CAMERA_OK, rc, "HStreamOperator::LinkInputAndOutputs IsValidMode false");
451         StreamInfo_V1_1 curStreamInfo;
452         stream->SetStreamInfo(curStreamInfo);
453         CHECK_EXECUTE(stream->GetStreamType() != StreamType::METADATA,
454             allStreamInfos.push_back(curStreamInfo));
455     }
456 
457     rc = CreateAndCommitStreams(allStreamInfos, settings, opMode);
458     MEDIA_INFO_LOG("HStreamOperator::LinkInputAndOutputs execute success");
459     return rc;
460 }
461 
UnlinkInputAndOutputs()462 int32_t HStreamOperator::UnlinkInputAndOutputs()
463 {
464     CAMERA_SYNC_TRACE;
465     int32_t rc = CAMERA_UNKNOWN_ERROR;
466     std::vector<int32_t> fwkStreamIds;
467     std::vector<int32_t> hdiStreamIds;
468     auto allStream = streamContainer_.GetAllStreams();
469     for (auto& stream : allStream) {
470         fwkStreamIds.emplace_back(stream->GetFwkStreamId());
471         hdiStreamIds.emplace_back(stream->GetHdiStreamId());
472         stream->UnlinkInput();
473     }
474     MEDIA_INFO_LOG("HStreamOperator::UnlinkInputAndOutputs() streamIds size() = %{public}zu, streamIds:%{public}s, "
475         "hdiStreamIds:%{public}s",
476         fwkStreamIds.size(), Container2String(fwkStreamIds.begin(), fwkStreamIds.end()).c_str(),
477         Container2String(hdiStreamIds.begin(), hdiStreamIds.end()).c_str());
478     ReleaseStreams(hdiStreamIds);
479     std::vector<StreamInfo_V1_1> emptyStreams;
480     UpdateStreams(emptyStreams);
481     ResetHdiStreamId();
482     return rc;
483 }
484 
ExpandSketchRepeatStream()485 void HStreamOperator::ExpandSketchRepeatStream()
486 {
487     MEDIA_DEBUG_LOG("Enter HStreamOperator::ExpandSketchRepeatStream()");
488     std::set<sptr<HStreamCommon>> sketchStreams;
489     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
490     for (auto& stream : repeatStreams) {
491         if (stream == nullptr) {
492             continue;
493         }
494         auto streamRepeat = CastStream<HStreamRepeat>(stream);
495         if (streamRepeat->GetRepeatStreamType() == RepeatStreamType::SKETCH) {
496             continue;
497         }
498         sptr<HStreamRepeat> sketchStream = streamRepeat->GetSketchStream();
499         if (sketchStream == nullptr) {
500             continue;
501         }
502         sketchStreams.insert(sketchStream);
503     }
504     MEDIA_DEBUG_LOG("HStreamOperator::ExpandSketchRepeatStream() sketch size is:%{public}zu", sketchStreams.size());
505     for (auto& stream : sketchStreams) {
506         AddOutputStream(stream);
507     }
508     MEDIA_DEBUG_LOG("Exit HStreamOperator::ExpandSketchRepeatStream()");
509 }
510 
ExpandMovingPhotoRepeatStream()511 void HStreamOperator::ExpandMovingPhotoRepeatStream()
512 {
513     CAMERA_SYNC_TRACE;
514     MEDIA_DEBUG_LOG("ExpandMovingPhotoRepeatStream enter");
515     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
516     for (auto& stream : repeatStreams) {
517         if (stream == nullptr) {
518             continue;
519         }
520         auto streamRepeat = CastStream<HStreamRepeat>(stream);
521         if (streamRepeat && streamRepeat->GetRepeatStreamType() == RepeatStreamType::PREVIEW) {
522             std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
523             auto movingPhotoSurfaceWrapper =
524                 MovingPhotoSurfaceWrapper::CreateMovingPhotoSurfaceWrapper(streamRepeat->width_, streamRepeat->height_);
525             if (movingPhotoSurfaceWrapper == nullptr) {
526                 MEDIA_ERR_LOG("HStreamOperator::ExpandMovingPhotoRepeatStream CreateMovingPhotoSurfaceWrapper fail.");
527                 continue;
528             }
529             auto producer = movingPhotoSurfaceWrapper->GetProducer();
530             metaSurface_ = Surface::CreateSurfaceAsConsumer("movingPhotoMeta");
531             auto metaCache = make_shared<FixedSizeList<pair<int64_t, sptr<SurfaceBuffer>>>>(6);
532             CHECK_WARNING_CONTINUE_LOG(producer == nullptr, "get producer fail.");
533             livephotoListener_ = new (std::nothrow) MovingPhotoListener(movingPhotoSurfaceWrapper,
534                 metaSurface_, metaCache, preCacheFrameCount_, postCacheFrameCount_);
535             CHECK_WARNING_CONTINUE_LOG(livephotoListener_ == nullptr, "failed to new livephotoListener_!");
536             movingPhotoSurfaceWrapper->SetSurfaceBufferListener(livephotoListener_);
537             livephotoMetaListener_ = new(std::nothrow) MovingPhotoMetaListener(metaSurface_, metaCache);
538             CHECK_WARNING_CONTINUE_LOG(livephotoMetaListener_ == nullptr, "failed to new livephotoMetaListener_!");
539             metaSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)livephotoMetaListener_);
540             CreateMovingPhotoStreamRepeat(streamRepeat->format_, streamRepeat->width_, streamRepeat->height_, producer);
541             std::lock_guard<std::mutex> streamLock(livePhotoStreamLock_);
542             AddOutputStream(livePhotoStreamRepeat_);
543             if (!audioCapturerSession_) {
544                 audioCapturerSession_ = new AudioCapturerSession();
545             }
546             if (!taskManager_ && audioCapturerSession_) {
547                 taskManager_ = new AvcodecTaskManager(audioCapturerSession_, VideoCodecType::VIDEO_ENCODE_TYPE_HEVC,
548                     currColorSpace_);
549                 taskManager_->SetVideoBufferDuration(preCacheFrameCount_, postCacheFrameCount_);
550             }
551             if (!videoCache_ && taskManager_) {
552                 videoCache_ = new MovingPhotoVideoCache(taskManager_);
553             }
554             break;
555         }
556     }
557     MEDIA_DEBUG_LOG("ExpandMovingPhotoRepeatStream Exit");
558 }
559 
CreateMovingPhotoStreamRepeat(int32_t format,int32_t width,int32_t height,sptr<OHOS::IBufferProducer> producer)560 int32_t HStreamOperator::CreateMovingPhotoStreamRepeat(
561     int32_t format, int32_t width, int32_t height, sptr<OHOS::IBufferProducer> producer)
562 {
563     CAMERA_SYNC_TRACE;
564     std::lock_guard<std::mutex> lock(livePhotoStreamLock_);
565     CHECK_ERROR_RETURN_RET_LOG(
566         width <= 0 || height <= 0, CAMERA_INVALID_ARG, "HCameraService::CreateLivePhotoStreamRepeat args is illegal");
567     CHECK_EXECUTE(livePhotoStreamRepeat_ != nullptr, livePhotoStreamRepeat_->Release());
568     auto streamRepeat = new (std::nothrow) HStreamRepeat(producer, format, width, height, RepeatStreamType::LIVEPHOTO);
569     CHECK_ERROR_RETURN_RET_LOG(streamRepeat == nullptr, CAMERA_ALLOC_ERROR, "HStreamRepeat allocation failed");
570     MEDIA_DEBUG_LOG("para is:%{public}dx%{public}d,%{public}d", width, height, format);
571     livePhotoStreamRepeat_ = streamRepeat;
572     streamRepeat->SetMetaProducer(metaSurface_->GetProducer());
573     streamRepeat->SetMirror(isMovingPhotoMirror_);
574     MEDIA_INFO_LOG("HCameraService::CreateLivePhotoStreamRepeat end");
575     return CAMERA_OK;
576 }
577 
GetStreamByStreamID(int32_t streamId)578 const sptr<HStreamCommon> HStreamOperator::GetStreamByStreamID(int32_t streamId)
579 {
580     auto stream = streamContainer_.GetStream(streamId) != nullptr ? streamContainer_.GetStream(streamId) :
581         streamContainerOffline_.GetStream(streamId);
582     if (stream == nullptr) {
583         MEDIA_DEBUG_LOG("HStreamOperator::GetStreamByStreamID get stream fail, streamId is:%{public}d", streamId);
584     }
585     return stream;
586 }
587 
GetHdiStreamByStreamID(int32_t streamId)588 const sptr<HStreamCommon> HStreamOperator::GetHdiStreamByStreamID(int32_t streamId)
589 {
590     auto stream = streamContainer_.GetHdiStream(streamId) != nullptr ? streamContainer_.GetHdiStream(streamId) :
591         streamContainerOffline_.GetHdiStream(streamId);
592     CHECK_ERROR_PRINT_LOG(stream == nullptr,
593         "HStreamOperator::GetHdiStreamByStreamID get stream fail, streamId is:%{public}d", streamId);
594     return stream;
595 }
596 
ClearSketchRepeatStream()597 void HStreamOperator::ClearSketchRepeatStream()
598 {
599     MEDIA_DEBUG_LOG("Enter HStreamOperator::ClearSketchRepeatStream()");
600 
601     // Already added session lock in BeginConfig()
602     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
603     for (auto& repeatStream : repeatStreams) {
604         if (repeatStream == nullptr) {
605             continue;
606         }
607         auto sketchStream = CastStream<HStreamRepeat>(repeatStream);
608         if (sketchStream->GetRepeatStreamType() != RepeatStreamType::SKETCH) {
609             continue;
610         }
611         MEDIA_DEBUG_LOG(
612             "HStreamOperator::ClearSketchRepeatStream() stream id is:%{public}d", sketchStream->GetFwkStreamId());
613         RemoveOutputStream(repeatStream);
614     }
615     MEDIA_DEBUG_LOG("Exit HStreamOperator::ClearSketchRepeatStream()");
616 }
617 
ClearMovingPhotoRepeatStream()618 void HStreamOperator::ClearMovingPhotoRepeatStream()
619 {
620     CAMERA_SYNC_TRACE;
621     MEDIA_DEBUG_LOG("Enter HStreamOperator::ClearMovingPhotoRepeatStream()");
622     // Already added session lock in BeginConfig()
623     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
624     for (auto& repeatStream : repeatStreams) {
625         if (repeatStream == nullptr) {
626             continue;
627         }
628         auto movingPhotoStream = CastStream<HStreamRepeat>(repeatStream);
629         if (movingPhotoStream->GetRepeatStreamType() != RepeatStreamType::LIVEPHOTO) {
630             continue;
631         }
632         StopMovingPhoto();
633         std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
634         livephotoListener_ = nullptr;
635         videoCache_ = nullptr;
636         MEDIA_DEBUG_LOG("HStreamOperator::ClearLivePhotoRepeatStream() stream id is:%{public}d",
637             movingPhotoStream->GetFwkStreamId());
638         RemoveOutputStream(repeatStream);
639     }
640     MEDIA_DEBUG_LOG("Exit HStreamOperator::ClearLivePhotoRepeatStream()");
641 }
642 
StopMovingPhoto()643 void HStreamOperator::StopMovingPhoto() __attribute__((no_sanitize("cfi")))
644 {
645     CAMERA_SYNC_TRACE;
646     MEDIA_DEBUG_LOG("Enter HStreamOperator::StopMovingPhoto");
647     std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
648     CHECK_EXECUTE(livephotoListener_, livephotoListener_->StopDrainOut());
649     CHECK_EXECUTE(videoCache_, videoCache_->ClearCache());
650 #ifdef MOVING_PHOTO_ADD_AUDIO
651     CHECK_EXECUTE(audioCapturerSession_, audioCapturerSession_->Stop());
652 #endif
653     CHECK_EXECUTE(taskManager_, taskManager_->Stop());
654 }
655 
GetActiveColorSpace(ColorSpace & colorSpace)656 int32_t HStreamOperator::GetActiveColorSpace(ColorSpace& colorSpace)
657 {
658     colorSpace = currColorSpace_;
659     return CAMERA_OK;
660 }
661 
SetColorSpace(ColorSpace colorSpace,bool isNeedUpdate)662 int32_t HStreamOperator::SetColorSpace(ColorSpace colorSpace, bool isNeedUpdate)
663 {
664     int32_t result = CAMERA_OK;
665     CHECK_ERROR_RETURN_RET_LOG(colorSpace == currColorSpace_, result,
666         "HStreamOperator::SetColorSpace() colorSpace no need to update.");
667     currColorSpace_ = colorSpace;
668     MEDIA_INFO_LOG("HStreamOperator::SetColorSpace() old ColorSpace : %{public}d, old ColorSpace : %{public}d",
669         currColorSpace_, colorSpace);
670     result = CheckIfColorSpaceMatchesFormat(colorSpace);
671     if (result != CAMERA_OK) {
672         if (opMode_ == static_cast<int32_t>(SceneMode::VIDEO) && !isNeedUpdate) {
673             MEDIA_ERR_LOG(
674                 "HStreamOperator::SetColorSpace() %{public}d, format and colorSpace : %{public}d not match.",
675                 result, colorSpace);
676             currColorSpace_ = ColorSpace::BT709;
677         } else {
678             MEDIA_ERR_LOG("HStreamOperator::SetColorSpace() Failed, format and colorSpace not match.");
679             return result;
680         }
681     }
682     SetColorSpaceForStreams();
683     MEDIA_INFO_LOG("HStreamOperator::SetColorSpace() colorSpace: %{public}d, isNeedUpdate: %{public}d",
684         currColorSpace_, isNeedUpdate);
685     return result;
686 }
687 
SetColorSpaceForStreams()688 void HStreamOperator::SetColorSpaceForStreams()
689 {
690     auto streams = streamContainer_.GetAllStreams();
691     for (auto& stream : streams) {
692         MEDIA_DEBUG_LOG("HStreamOperator::SetColorSpaceForStreams() streams type %{public}d", stream->GetStreamType());
693         stream->SetColorSpace(currColorSpace_);
694     }
695 }
696 
CancelStreamsAndGetStreamInfos(std::vector<StreamInfo_V1_1> & streamInfos)697 void HStreamOperator::CancelStreamsAndGetStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos)
698 {
699     MEDIA_INFO_LOG("HStreamOperator::CancelStreamsAndGetStreamInfos enter.");
700     StreamInfo_V1_1 curStreamInfo;
701     auto streams = streamContainer_.GetAllStreams();
702     for (auto& stream : streams) {
703         if (stream && stream->GetStreamType() == StreamType::METADATA) {
704             continue;
705         }
706         if (stream && stream->GetStreamType() == StreamType::CAPTURE && isSessionStarted_) {
707             static_cast<HStreamCapture*>(stream.GetRefPtr())->CancelCapture();
708         } else if (stream && stream->GetStreamType() == StreamType::REPEAT && isSessionStarted_) {
709             static_cast<HStreamRepeat*>(stream.GetRefPtr())->Stop();
710         }
711         if (stream) {
712             stream->SetStreamInfo(curStreamInfo);
713             streamInfos.push_back(curStreamInfo);
714         }
715     }
716 }
717 
RestartStreams(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)718 void HStreamOperator::RestartStreams(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings)
719 {
720     MEDIA_INFO_LOG("HStreamOperator::RestartStreams() enter.");
721     auto streams = streamContainer_.GetAllStreams();
722     for (auto& stream : streams) {
723         if (stream && stream->GetStreamType() == StreamType::REPEAT &&
724             CastStream<HStreamRepeat>(stream)->GetRepeatStreamType() == RepeatStreamType::PREVIEW) {
725             CastStream<HStreamRepeat>(stream)->Start(settings);
726         }
727     }
728 }
729 
UpdateStreamInfos(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings)730 int32_t HStreamOperator::UpdateStreamInfos(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings)
731 {
732     std::vector<StreamInfo_V1_1> streamInfos;
733     CancelStreamsAndGetStreamInfos(streamInfos);
734     int errorCode = UpdateStreams(streamInfos);
735     if (errorCode == CAMERA_OK) {
736         RestartStreams(settings);
737     } else {
738         MEDIA_DEBUG_LOG("HStreamOperator::UpdateStreamInfos err %{public}d", errorCode);
739     }
740     return errorCode;
741 }
742 
CheckIfColorSpaceMatchesFormat(ColorSpace colorSpace)743 int32_t HStreamOperator::CheckIfColorSpaceMatchesFormat(ColorSpace colorSpace)
744 {
745     if (!(colorSpace == ColorSpace::BT2020_HLG || colorSpace == ColorSpace::BT2020_PQ ||
746         colorSpace == ColorSpace::BT2020_HLG_LIMIT || colorSpace == ColorSpace::BT2020_PQ_LIMIT)) {
747         return CAMERA_OK;
748     }
749     MEDIA_DEBUG_LOG("HStreamOperator::CheckIfColorSpaceMatchesFormat start");
750     // 选择BT2020,需要匹配10bit的format;若不匹配,返回error
751     auto streams = streamContainer_.GetAllStreams();
752     for (auto& curStream : streams) {
753         if (!curStream) {
754             continue;
755         }
756         if (curStream->GetStreamType() == StreamType::CAPTURE) {
757             if (!(curStream->format_ == OHOS_CAMERA_FORMAT_YCRCB_420_SP ||
758                 curStream->format_ == OHOS_CAMERA_FORMAT_JPEG ||
759                 curStream->format_ == OHOS_CAMERA_FORMAT_HEIC)) {
760                 MEDIA_ERR_LOG("HCaptureSession::CheckFormat, streamType: %{public}d, format not match",
761                     curStream->GetStreamType());
762                 return CAMERA_OPERATION_NOT_ALLOWED;
763             }
764         } else if (curStream->GetStreamType() == StreamType::REPEAT) {
765             if (!(curStream->format_ == OHOS_CAMERA_FORMAT_YCRCB_P010 ||
766                 curStream->format_ == OHOS_CAMERA_FORMAT_YCBCR_P010)) {
767                 MEDIA_ERR_LOG("HCaptureSession::CheckFormat, streamType: %{public}d, format not match",
768                     curStream->GetStreamType());
769                 return CAMERA_OPERATION_NOT_ALLOWED;
770             }
771         }
772     }
773     MEDIA_DEBUG_LOG("HStreamOperator::CheckIfColorSpaceMatchesFormat end");
774     return CAMERA_OK;
775 }
776 
GetMovingPhotoBufferDuration()777 int32_t HStreamOperator::GetMovingPhotoBufferDuration()
778 {
779     uint32_t preBufferDuration = 0;
780     uint32_t postBufferDuration = 0;
781     constexpr int32_t MILLSEC_MULTIPLE = 1000;
782     CHECK_ERROR_RETURN_RET_LOG(
783         cameraDevice_ == nullptr, 0, "HCaptureSession::GetMovingPhotoBufferDuration() cameraDevice is null");
784     std::shared_ptr<OHOS::Camera::CameraMetadata> ability = cameraDevice_->GetDeviceAbility();
785     CHECK_ERROR_RETURN_RET(ability == nullptr, 0);
786     camera_metadata_item_t item;
787     int ret = OHOS::Camera::FindCameraMetadataItem(ability->get(), OHOS_MOVING_PHOTO_BUFFER_DURATION, &item);
788     CHECK_ERROR_RETURN_RET_LOG(
789         ret != CAM_META_SUCCESS, 0, "HCaptureSession::GetMovingPhotoBufferDuration get buffer duration failed");
790     preBufferDuration = item.data.ui32[0];
791     postBufferDuration = item.data.ui32[1];
792     preCacheFrameCount_ = preBufferDuration == 0
793                               ? preCacheFrameCount_
794                               : static_cast<uint32_t>(float(preBufferDuration) / MILLSEC_MULTIPLE * VIDEO_FRAME_RATE);
795     postCacheFrameCount_ = preBufferDuration == 0
796                                ? postCacheFrameCount_
797                                : static_cast<uint32_t>(float(postBufferDuration) / MILLSEC_MULTIPLE * VIDEO_FRAME_RATE);
798     MEDIA_INFO_LOG(
799         "HStreamOperator::GetMovingPhotoBufferDuration preBufferDuration : %{public}u, "
800         "postBufferDuration : %{public}u, preCacheFrameCount_ : %{public}u, postCacheFrameCount_ : %{public}u",
801         preBufferDuration, postBufferDuration, preCacheFrameCount_, postCacheFrameCount_);
802     return CAMERA_OK;
803 }
804 
805 
GetMovingPhotoStartAndEndTime()806 void HStreamOperator::GetMovingPhotoStartAndEndTime()
807 {
808     CHECK_ERROR_RETURN_LOG(cameraDevice_ == nullptr, "HCaptureSession::GetMovingPhotoStartAndEndTime device is null");
809     cameraDevice_->SetMovingPhotoStartTimeCallback([this](int32_t captureId, int64_t startTimeStamp) {
810         MEDIA_INFO_LOG("SetMovingPhotoStartTimeCallback function enter");
811         CHECK_ERROR_RETURN_LOG(this->taskManager_ == nullptr, "Set start time callback taskManager_ is null");
812         std::lock_guard<mutex> lock(this->taskManager_->startTimeMutex_);
813         if (this->taskManager_->mPStartTimeMap_.count(captureId) == 0) {
814             MEDIA_INFO_LOG("Save moving photo start info, captureId : %{public}d, start timestamp : %{public}" PRIu64,
815                 captureId, startTimeStamp);
816             this->taskManager_->mPStartTimeMap_.insert(make_pair(captureId, startTimeStamp));
817         }
818     });
819 
820     cameraDevice_->SetMovingPhotoEndTimeCallback([this](int32_t captureId, int64_t endTimeStamp) {
821         CHECK_ERROR_RETURN_LOG(this->taskManager_ == nullptr, "Set end time callback taskManager_ is null");
822         std::lock_guard<mutex> lock(this->taskManager_->endTimeMutex_);
823         if (this->taskManager_->mPEndTimeMap_.count(captureId) == 0) {
824             MEDIA_INFO_LOG("Save moving photo end info, captureId : %{public}d, end timestamp : %{public}" PRIu64,
825                 captureId, endTimeStamp);
826             this->taskManager_->mPEndTimeMap_.insert(make_pair(captureId, endTimeStamp));
827         }
828     });
829 }
830 
EnableMovingPhoto(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings,bool isEnable,int32_t sensorOritation)831 int32_t HStreamOperator::EnableMovingPhoto(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings,
832     bool isEnable, int32_t sensorOritation)
833 {
834     MEDIA_INFO_LOG("HStreamOperator::EnableMovingPhoto is %{public}d", isEnable);
835     isSetMotionPhoto_ = isEnable;
836     deviceSensorOritation_ = sensorOritation;
837     #ifdef CAMERA_USE_SENSOR
838     if (isSetMotionPhoto_) {
839         RegisterSensorCallback();
840     } else {
841         UnRegisterSensorCallback();
842     }
843     #endif
844     StartMovingPhotoStream(settings);
845     CHECK_EXECUTE(cameraDevice_ != nullptr, cameraDevice_->EnableMovingPhoto(isEnable));
846     GetMovingPhotoBufferDuration();
847     GetMovingPhotoStartAndEndTime();
848     return CAMERA_OK;
849 }
850 
StartMovingPhoto(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings,sptr<HStreamRepeat> & curStreamRepeat)851 void HStreamOperator::StartMovingPhoto(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings,
852     sptr<HStreamRepeat>& curStreamRepeat)
853 {
854     auto thisPtr = wptr<HStreamOperator>(this);
855     curStreamRepeat->SetMovingPhotoStartCallback([thisPtr, settings]() {
856         auto sessionPtr = thisPtr.promote();
857         if (sessionPtr != nullptr) {
858             MEDIA_INFO_LOG("StartMovingPhotoStream when addDeferedSurface");
859             sessionPtr->StartMovingPhotoStream(settings);
860         }
861     });
862 }
863 
StartPreviewStream(const std::shared_ptr<OHOS::Camera::CameraMetadata> & settings,camera_position_enum_t cameraPosition)864 int32_t HStreamOperator::StartPreviewStream(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings,
865     camera_position_enum_t cameraPosition)
866 {
867     int32_t errorCode = CAMERA_OK;
868     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
869     bool hasDerferedPreview = false;
870     // start preview
871     for (auto& item : repeatStreams) {
872         auto curStreamRepeat = CastStream<HStreamRepeat>(item);
873         auto repeatType = curStreamRepeat->GetRepeatStreamType();
874         if (repeatType != RepeatStreamType::PREVIEW) {
875             continue;
876         }
877         if (curStreamRepeat->GetPreparedCaptureId() != CAPTURE_ID_UNSET) {
878             continue;
879         }
880         curStreamRepeat->SetUsedAsPosition(cameraPosition);
881         errorCode = curStreamRepeat->Start(settings);
882         hasDerferedPreview = curStreamRepeat->producer_ == nullptr;
883         if (isSetMotionPhoto_ && hasDerferedPreview) {
884             StartMovingPhoto(settings, curStreamRepeat);
885         }
886         if (errorCode != CAMERA_OK) {
887             MEDIA_ERR_LOG("HStreamOperator::Start(), Failed to start preview, rc: %{public}d", errorCode);
888             break;
889         }
890     }
891     // start movingPhoto
892     for (auto& item : repeatStreams) {
893         auto curStreamRepeat = CastStream<HStreamRepeat>(item);
894         auto repeatType = curStreamRepeat->GetRepeatStreamType();
895         if (repeatType != RepeatStreamType::LIVEPHOTO) {
896             continue;
897         }
898         int32_t movingPhotoErrorCode = CAMERA_OK;
899         if (isSetMotionPhoto_ && !hasDerferedPreview) {
900             movingPhotoErrorCode = curStreamRepeat->Start(settings);
901             #ifdef MOVING_PHOTO_ADD_AUDIO
902             std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
903             audioCapturerSession_ != nullptr && audioCapturerSession_->StartAudioCapture();
904             #endif
905         }
906         if (movingPhotoErrorCode != CAMERA_OK) {
907             MEDIA_ERR_LOG("Failed to start movingPhoto, rc: %{public}d", movingPhotoErrorCode);
908             break;
909         }
910     }
911     return errorCode;
912 }
913 
Stop()914 int32_t HStreamOperator::Stop()
915 {
916     CAMERA_SYNC_TRACE;
917     int32_t errorCode = CAMERA_OK;
918     MEDIA_INFO_LOG("HStreamOperator::Stop prepare execute");
919     auto allStreams = streamContainer_.GetAllStreams();
920     for (auto& item : allStreams) {
921         if (item->GetStreamType() == StreamType::REPEAT) {
922             auto repeatStream = CastStream<HStreamRepeat>(item);
923             if (repeatStream->GetRepeatStreamType() == RepeatStreamType::PREVIEW) {
924                 errorCode = repeatStream->Stop();
925             } else if (repeatStream->GetRepeatStreamType() == RepeatStreamType::LIVEPHOTO) {
926                 repeatStream->Stop();
927                 StopMovingPhoto();
928             } else {
929                 repeatStream->Stop();
930             }
931         } else if (item->GetStreamType() == StreamType::METADATA) {
932             CastStream<HStreamMetadata>(item)->Stop();
933         } else if (item->GetStreamType() == StreamType::CAPTURE) {
934             CastStream<HStreamCapture>(item)->CancelCapture();
935         } else if (item->GetStreamType() == StreamType::DEPTH) {
936             CastStream<HStreamDepthData>(item)->Stop();
937         } else {
938             MEDIA_ERR_LOG("HStreamOperator::Stop(), get unknow stream, streamType: %{public}d, streamId:%{public}d",
939                 item->GetStreamType(), item->GetFwkStreamId());
940         }
941         if (errorCode != CAMERA_OK) {
942             MEDIA_ERR_LOG("HStreamOperator::Stop(), Failed to stop stream, rc: %{public}d, streamId:%{public}d",
943                 errorCode, item->GetFwkStreamId());
944         }
945     }
946     MEDIA_INFO_LOG("HStreamOperator::Stop execute success");
947     return errorCode;
948 }
949 
ReleaseStreams()950 void HStreamOperator::ReleaseStreams()
951 {
952     CAMERA_SYNC_TRACE;
953     std::vector<int32_t> fwkStreamIds;
954     std::vector<int32_t> hdiStreamIds;
955     auto allStream = streamContainer_.GetAllStreams();
956     for (auto& stream : allStream) {
957         if (stream->GetStreamType() == StreamType::CAPTURE &&
958             CastStream<HStreamCapture>(stream)->IsHasSwitchToOffline()) {
959             continue;
960         }
961         auto fwkStreamId = stream->GetFwkStreamId();
962         if (fwkStreamId != STREAM_ID_UNSET) {
963             fwkStreamIds.emplace_back(fwkStreamId);
964         }
965         auto hdiStreamId = stream->GetHdiStreamId();
966         if (hdiStreamId != STREAM_ID_UNSET) {
967             hdiStreamIds.emplace_back(hdiStreamId);
968         }
969         stream->ReleaseStream(true);
970         streamContainer_.RemoveStream(stream);
971     }
972     MEDIA_INFO_LOG("HStreamOperator::ReleaseStreams() streamIds size() = %{public}zu, fwkStreamIds:%{public}s, "
973         "hdiStreamIds:%{public}s,",
974         fwkStreamIds.size(), Container2String(fwkStreamIds.begin(), fwkStreamIds.end()).c_str(),
975         Container2String(hdiStreamIds.begin(), hdiStreamIds.end()).c_str());
976     if (!hdiStreamIds.empty()) {
977         ReleaseStreams(hdiStreamIds);
978     }
979 }
980 
GetOfflineOutptSize()981 int32_t HStreamOperator::GetOfflineOutptSize()
982 {
983     std::list<sptr<HStreamCommon>> captureStreams = streamContainer_.GetStreams(StreamType::CAPTURE);
984     std::list<sptr<HStreamCommon>> captureStreamsOffline = streamContainerOffline_.GetStreams(StreamType::CAPTURE);
985     captureStreams.insert(captureStreams.end(), captureStreamsOffline.begin(), captureStreamsOffline.end());
986     int32_t offlineCount = 0;
987     for (auto& stream : captureStreams) {
988         if (stream->GetStreamType() == StreamType::CAPTURE &&
989             CastStream<HStreamCapture>(stream)->IsHasEnableOfflinePhoto()) {
990             offlineCount++;
991         } else {
992             continue;
993         }
994     }
995     return offlineCount;
996 }
997 
GetAllOutptSize()998 int32_t HStreamOperator::GetAllOutptSize()
999 {
1000     int32_t outputCount = streamContainer_.Size();
1001     return outputCount;
1002 }
1003 
ReleaseStreams(std::vector<int32_t> & releaseStreamIds)1004 int32_t HStreamOperator::ReleaseStreams(std::vector<int32_t>& releaseStreamIds)
1005 {
1006     CAMERA_SYNC_TRACE;
1007     auto streamOperator = GetHDIStreamOperator();
1008     if (streamOperator != nullptr && !releaseStreamIds.empty()) {
1009         MEDIA_INFO_LOG("HStreamOperator::ReleaseStreams %{public}s",
1010             Container2String(releaseStreamIds.begin(), releaseStreamIds.end()).c_str());
1011         int32_t rc = streamOperator->ReleaseStreams(releaseStreamIds);
1012         if (rc != HDI::Camera::V1_0::NO_ERROR) {
1013             MEDIA_ERR_LOG("HCameraDevice::ClearStreamOperator ReleaseStreams fail, error Code:%{public}d", rc);
1014             CameraReportUtils::ReportCameraError(
1015                 "HCameraDevice::ReleaseStreams", rc, true, CameraReportUtils::GetCallerInfo());
1016         }
1017     }
1018     return CAMERA_OK;
1019 }
1020 
Release()1021 int32_t HStreamOperator::Release()
1022 {
1023     CAMERA_SYNC_TRACE;
1024     int32_t errorCode = CAMERA_OK;
1025     {
1026         std::lock_guard<std::mutex> lock(releaseOperatorLock_);
1027         #ifdef CAMERA_USE_SENSOR
1028         if (isSetMotionPhoto_) {
1029             UnRegisterSensorCallback();
1030             isSetMotionPhoto_ = false;
1031         }
1032         #endif
1033         if (displayListener_) {
1034             OHOS::Rosen::DisplayManager::GetInstance().UnregisterDisplayListener(displayListener_);
1035             displayListener_ = nullptr;
1036         }
1037         auto streamOperator = GetHDIStreamOperator();
1038         if (streamOperator != nullptr) {
1039             ResetHDIStreamOperator();
1040             MEDIA_INFO_LOG("HStreamOperator::Release ResetHDIStreamOperator");
1041         }
1042         HStreamOperatorManager::GetInstance()->RemoveStreamOperator(streamOperatorId_);
1043     }
1044     std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
1045     CHECK_EXECUTE(livephotoListener_, livephotoListener_ = nullptr);
1046     CHECK_EXECUTE(videoCache_, videoCache_ = nullptr);
1047     if (taskManager_) {
1048         taskManager_->ClearTaskResource();
1049         taskManager_ = nullptr;
1050     }
1051     MEDIA_INFO_LOG("HStreamOperator::Release execute success");
1052     return errorCode;
1053 }
1054 
CreateAndCommitStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1> & streamInfos,const std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceSettings,int32_t operationMode)1055 int32_t HStreamOperator::CreateAndCommitStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1>& streamInfos,
1056     const std::shared_ptr<OHOS::Camera::CameraMetadata>& deviceSettings, int32_t operationMode)
1057 {
1058     int retCode = CreateStreams(streamInfos);
1059     CHECK_ERROR_RETURN_RET(retCode != CAMERA_OK, retCode);
1060     return CommitStreams(deviceSettings, operationMode);
1061 }
1062 
CommitStreams(const std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceSettings,int32_t operationMode)1063 int32_t HStreamOperator::CommitStreams(
1064     const std::shared_ptr<OHOS::Camera::CameraMetadata>& deviceSettings, int32_t operationMode)
1065 {
1066     CamRetCode hdiRc = HDI::Camera::V1_0::NO_ERROR;
1067     uint32_t major;
1068     uint32_t minor;
1069     sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator;
1070     sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperatorV1_1;
1071     std::lock_guard<std::mutex> lock(opMutex_);
1072     streamOperator = GetHDIStreamOperator();
1073     CHECK_ERROR_RETURN_RET_LOG(streamOperator == nullptr, CAMERA_UNKNOWN_ERROR,
1074         "HStreamOperator::CommitStreams GetStreamOperator is null!");
1075     // get higher streamOperator version
1076     streamOperator->GetVersion(major, minor);
1077     MEDIA_INFO_LOG(
1078         "HStreamOperator::CommitStreams streamOperator GetVersion major:%{public}d, minor:%{public}d", major, minor);
1079     if (major >= HDI_VERSION_1 && minor >= HDI_VERSION_1) {
1080         MEDIA_DEBUG_LOG("HStreamOperator::CommitStreams IStreamOperator cast to V1_1");
1081         streamOperatorV1_1 = OHOS::HDI::Camera::V1_1::IStreamOperator::CastFrom(streamOperator);
1082         if (streamOperatorV1_1 == nullptr) {
1083             MEDIA_ERR_LOG("HStreamOperator::CommitStreams IStreamOperator cast to V1_1 error");
1084             streamOperatorV1_1 = static_cast<OHOS::HDI::Camera::V1_1::IStreamOperator*>(streamOperator.GetRefPtr());
1085         }
1086     }
1087 
1088     std::vector<uint8_t> setting;
1089     OHOS::Camera::MetadataUtils::ConvertMetadataToVec(deviceSettings, setting);
1090     MEDIA_INFO_LOG("HStreamOperator::CommitStreams, commit mode %{public}d", operationMode);
1091     if (streamOperatorV1_1 != nullptr) {
1092         MEDIA_DEBUG_LOG("HStreamOperator::CommitStreams IStreamOperator V1_1");
1093         hdiRc = (CamRetCode)(streamOperatorV1_1->CommitStreams_V1_1(
1094             static_cast<OHOS::HDI::Camera::V1_1::OperationMode_V1_1>(operationMode), setting));
1095     } else {
1096         MEDIA_DEBUG_LOG("HStreamOperator::CommitStreams IStreamOperator V1_0");
1097         OperationMode opMode = OperationMode::NORMAL;
1098         hdiRc = (CamRetCode)(streamOperator->CommitStreams(opMode, setting));
1099     }
1100     if (hdiRc != HDI::Camera::V1_0::NO_ERROR) {
1101         MEDIA_ERR_LOG("HStreamOperator::CommitStreams failed with error Code:%d", hdiRc);
1102         CameraReportUtils::ReportCameraError(
1103             "HStreamOperator::CommitStreams", hdiRc, true, CameraReportUtils::GetCallerInfo());
1104     }
1105     MEDIA_DEBUG_LOG("HStreamOperator::CommitStreams end");
1106     return HdiToServiceError(hdiRc);
1107 }
1108 
EnableMovingPhotoMirror(bool isMirror,bool isConfig)1109 int32_t HStreamOperator::EnableMovingPhotoMirror(bool isMirror, bool isConfig)
1110 {
1111     if (!isConfig) {
1112         isMovingPhotoMirror_ = isMirror;
1113         return CAMERA_OK;
1114     }
1115     if (!isSetMotionPhoto_ || isMirror == isMovingPhotoMirror_) {
1116         return CAMERA_OK;
1117     }
1118     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
1119     for (auto& stream : repeatStreams) {
1120         if (stream == nullptr) {
1121             continue;
1122         }
1123         auto streamRepeat = CastStream<HStreamRepeat>(stream);
1124         if (streamRepeat->GetRepeatStreamType() == RepeatStreamType::LIVEPHOTO) {
1125             MEDIA_INFO_LOG("restart movingphoto stream.");
1126             if (streamRepeat->SetMirrorForLivePhoto(isMirror, opMode_)) {
1127                 isMovingPhotoMirror_ = isMirror;
1128                 // set clear cache flag
1129                 std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
1130                 CHECK_ERROR_RETURN_RET_LOG(livephotoListener_ == nullptr, CAMERA_OK, "livephotoListener_ is null");
1131                 livephotoListener_->SetClearFlag();
1132             }
1133             break;
1134         }
1135     }
1136     return CAMERA_OK;
1137 }
1138 
GetOutputStatus(int32_t & status)1139 void HStreamOperator::GetOutputStatus(int32_t &status)
1140 {
1141     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
1142     for (auto& stream : repeatStreams) {
1143         if (stream == nullptr) {
1144             continue;
1145         }
1146         auto streamRepeat = CastStream<HStreamRepeat>(stream);
1147         if (streamRepeat->GetRepeatStreamType() == RepeatStreamType::VIDEO) {
1148             if (streamRepeat->GetPreparedCaptureId() != CAPTURE_ID_UNSET) {
1149                 const int32_t videoStartStatus = 2;
1150                 status = videoStartStatus;
1151             }
1152         }
1153     }
1154 }
1155 
1156 
1157 #ifdef CAMERA_USE_SENSOR
RegisterSensorCallback()1158 void HStreamOperator::RegisterSensorCallback()
1159 {
1160     std::lock_guard<std::mutex> lock(sensorLock_);
1161     if (isRegisterSensorSuccess_) {
1162         MEDIA_INFO_LOG("HCaptureSession::RegisterSensorCallback isRegisterSensorSuccess return");
1163         return;
1164     }
1165     MEDIA_INFO_LOG("HCaptureSession::RegisterSensorCallback start");
1166     user.callback = GravityDataCallbackImpl;
1167     int32_t subscribeRet = SubscribeSensor(SENSOR_TYPE_ID_GRAVITY, &user);
1168     MEDIA_INFO_LOG("RegisterSensorCallback, subscribeRet: %{public}d", subscribeRet);
1169     int32_t setBatchRet = SetBatch(SENSOR_TYPE_ID_GRAVITY, &user, POSTURE_INTERVAL, 0);
1170     MEDIA_INFO_LOG("RegisterSensorCallback, setBatchRet: %{public}d", setBatchRet);
1171     int32_t activateRet = ActivateSensor(SENSOR_TYPE_ID_GRAVITY, &user);
1172     MEDIA_INFO_LOG("RegisterSensorCallback, activateRet: %{public}d", activateRet);
1173     if (subscribeRet != CAMERA_OK || setBatchRet != CAMERA_OK || activateRet != CAMERA_OK) {
1174         isRegisterSensorSuccess_ = false;
1175         MEDIA_INFO_LOG("RegisterSensorCallback failed.");
1176     } else {
1177         isRegisterSensorSuccess_ = true;
1178     }
1179 }
1180 
UnRegisterSensorCallback()1181 void HStreamOperator::UnRegisterSensorCallback()
1182 {
1183     std::lock_guard<std::mutex> lock(sensorLock_);
1184     int32_t deactivateRet = DeactivateSensor(SENSOR_TYPE_ID_GRAVITY, &user);
1185     int32_t unsubscribeRet = UnsubscribeSensor(SENSOR_TYPE_ID_GRAVITY, &user);
1186     if (deactivateRet == CAMERA_OK && unsubscribeRet == CAMERA_OK) {
1187         MEDIA_INFO_LOG("HCameraService.UnRegisterSensorCallback success.");
1188         isRegisterSensorSuccess_ = false;
1189     } else {
1190         MEDIA_INFO_LOG("HCameraService.UnRegisterSensorCallback failed.");
1191     }
1192 }
1193 
GravityDataCallbackImpl(SensorEvent * event)1194 void HStreamOperator::GravityDataCallbackImpl(SensorEvent* event)
1195 {
1196     MEDIA_DEBUG_LOG("GravityDataCallbackImpl prepare execute");
1197     CHECK_ERROR_RETURN_LOG(event == nullptr, "SensorEvent is nullptr.");
1198     CHECK_ERROR_RETURN_LOG(event[0].data == nullptr, "SensorEvent[0].data is nullptr.");
1199     CHECK_ERROR_RETURN_LOG(event->sensorTypeId != SENSOR_TYPE_ID_GRAVITY, "SensorCallback error type.");
1200     // this data will be delete when callback execute finish
1201     GravityData* nowGravityData = reinterpret_cast<GravityData*>(event->data);
1202     gravityData = { nowGravityData->x, nowGravityData->y, nowGravityData->z };
1203     sensorRotation = CalcSensorRotation(CalcRotationDegree(gravityData));
1204 }
1205 
CalcSensorRotation(int32_t sensorDegree)1206 int32_t HStreamOperator::CalcSensorRotation(int32_t sensorDegree)
1207 {
1208     // Use ROTATION_0 when degree range is [0, 30]∪[330, 359]
1209     if (sensorDegree >= 0 && (sensorDegree <= 30 || sensorDegree >= 330)) {
1210         return STREAM_ROTATE_0;
1211     } else if (sensorDegree >= 60 && sensorDegree <= 120) { // Use ROTATION_90 when degree range is [60, 120]
1212         return STREAM_ROTATE_90;
1213     } else if (sensorDegree >= 150 && sensorDegree <= 210) { // Use ROTATION_180 when degree range is [150, 210]
1214         return STREAM_ROTATE_180;
1215     } else if (sensorDegree >= 240 && sensorDegree <= 300) { // Use ROTATION_270 when degree range is [240, 300]
1216         return STREAM_ROTATE_270;
1217     } else {
1218         return sensorRotation;
1219     }
1220 }
1221 
CalcRotationDegree(GravityData data)1222 int32_t HStreamOperator::CalcRotationDegree(GravityData data)
1223 {
1224     float x = data.x;
1225     float y = data.y;
1226     float z = data.z;
1227     int degree = -1;
1228     if ((x * x + y * y) * VALID_INCLINATION_ANGLE_THRESHOLD_COEFFICIENT < z * z) {
1229         return degree;
1230     }
1231     // arccotx = pi / 2 - arctanx, 90 is used to calculate acot(in degree); degree = rad / pi * 180
1232     degree = 90 - static_cast<int>(round(atan2(y, -x) / M_PI * 180));
1233     // Normalize the degree to the range of 0~360
1234     return degree >= 0 ? degree % 360 : degree % 360 + 360;
1235 }
1236 #endif
1237 
SetSensorRotation(int32_t rotationValue,int32_t sensorOrientation,int32_t cameraPosition)1238 void HStreamOperator::SetSensorRotation(int32_t rotationValue, int32_t sensorOrientation, int32_t cameraPosition)
1239 {
1240     MEDIA_INFO_LOG("SetSensorRotation rotationValue : %{public}d, sensorOrientation : %{public}d",
1241         rotationValue, sensorOrientation);
1242     // 获取当前重力传感器角度
1243     if (cameraPosition == OHOS_CAMERA_POSITION_BACK) {
1244         sensorRotation_ = rotationValue - sensorOrientation;
1245     } else if (cameraPosition == OHOS_CAMERA_POSITION_FRONT) {
1246         sensorRotation_ = sensorOrientation - rotationValue;
1247     }
1248 }
1249 
StartMovingPhotoEncode(int32_t rotation,uint64_t timestamp,int32_t format,int32_t captureId)1250 void HStreamOperator::StartMovingPhotoEncode(int32_t rotation, uint64_t timestamp, int32_t format, int32_t captureId)
1251 {
1252     if (!isSetMotionPhoto_) {
1253         return;
1254     }
1255     int32_t addMirrorRotation = 0;
1256     MEDIA_INFO_LOG("sensorRotation is %{public}d", sensorRotation_);
1257     if ((sensorRotation_ == STREAM_ROTATE_0 || sensorRotation_ == STREAM_ROTATE_180) && isMovingPhotoMirror_) {
1258         addMirrorRotation = STREAM_ROTATE_180;
1259     }
1260     int32_t realRotation = rotation + addMirrorRotation;
1261     realRotation = realRotation % ROTATION_360;
1262     StartRecord(timestamp, realRotation, captureId);
1263 }
1264 
CreateDisplayName()1265 std::string HStreamOperator::CreateDisplayName()
1266 {
1267     struct tm currentTime;
1268     std::string formattedTime = "";
1269     if (GetSystemCurrentTime(&currentTime)) {
1270         std::stringstream ss;
1271         ss << prefix << std::setw(yearWidth) << std::setfill(placeholder) << currentTime.tm_year + startYear
1272            << std::setw(otherWidth) << std::setfill(placeholder) << (currentTime.tm_mon + 1) << std::setw(otherWidth)
1273            << std::setfill(placeholder) << currentTime.tm_mday << connector << std::setw(otherWidth)
1274            << std::setfill(placeholder) << currentTime.tm_hour << std::setw(otherWidth) << std::setfill(placeholder)
1275            << currentTime.tm_min << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_sec;
1276         formattedTime = ss.str();
1277     } else {
1278         MEDIA_ERR_LOG("Failed to get current time.");
1279     }
1280     if (lastDisplayName_ == formattedTime) {
1281         saveIndex++;
1282         formattedTime = formattedTime + connector + std::to_string(saveIndex);
1283         MEDIA_INFO_LOG("CreateDisplayName is %{private}s", formattedTime.c_str());
1284         return formattedTime;
1285     }
1286     lastDisplayName_ = formattedTime;
1287     saveIndex = 0;
1288     MEDIA_INFO_LOG("CreateDisplayName is %{private}s", formattedTime.c_str());
1289     return formattedTime;
1290 }
1291 
CreateBurstDisplayName(int32_t imageSeqId,int32_t seqId)1292 std::string HStreamOperator::CreateBurstDisplayName(int32_t imageSeqId, int32_t seqId)
1293 {
1294     struct tm currentTime;
1295     std::string formattedTime = "";
1296     std::stringstream ss;
1297     // a group of burst capture use the same prefix
1298     if (imageSeqId == 1) {
1299         CHECK_ERROR_RETURN_RET_LOG(!GetSystemCurrentTime(&currentTime), formattedTime, "Failed to get current time.");
1300         ss << prefix << std::setw(yearWidth) << std::setfill(placeholder) << currentTime.tm_year + startYear
1301            << std::setw(otherWidth) << std::setfill(placeholder) << (currentTime.tm_mon + 1) << std::setw(otherWidth)
1302            << std::setfill(placeholder) << currentTime.tm_mday << connector << std::setw(otherWidth)
1303            << std::setfill(placeholder) << currentTime.tm_hour << std::setw(otherWidth) << std::setfill(placeholder)
1304            << currentTime.tm_min << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_sec
1305            << connector << burstTag;
1306         lastBurstPrefix_ = ss.str();
1307         ss << std::setw(burstWidth) << std::setfill(placeholder) << seqId;
1308     } else {
1309         ss << lastBurstPrefix_ << std::setw(burstWidth) << std::setfill(placeholder) << seqId;
1310     }
1311     MEDIA_DEBUG_LOG("burst prefix is %{private}s", lastBurstPrefix_.c_str());
1312 
1313     if (seqId == 1) {
1314         ss << coverTag;
1315     }
1316     formattedTime = ss.str();
1317     MEDIA_INFO_LOG("CreateBurstDisplayName is %{private}s", formattedTime.c_str());
1318     return formattedTime;
1319 }
1320 
SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy,int32_t & cameraShotType,bool & isBursting,std::string & burstKey)1321 void HStreamOperator::SetCameraPhotoProxyInfo(sptr<CameraServerPhotoProxy> cameraPhotoProxy, int32_t &cameraShotType,
1322     bool &isBursting, std::string &burstKey)
1323 {
1324     cameraPhotoProxy->SetShootingMode(opMode_);
1325     int32_t captureId = cameraPhotoProxy->GetCaptureId();
1326     std::string imageId = cameraPhotoProxy->GetPhotoId();
1327     isBursting = false;
1328     bool isCoverPhoto = false;
1329     int32_t invalidBurstSeqId = -1;
1330     std::list<sptr<HStreamCommon>> captureStreams = streamContainer_.GetStreams(StreamType::CAPTURE);
1331     std::list<sptr<HStreamCommon>> captureStreamsOffline = streamContainerOffline_.GetStreams(StreamType::CAPTURE);
1332     captureStreams.insert(captureStreams.end(), captureStreamsOffline.begin(), captureStreamsOffline.end());
1333     for (auto& stream : captureStreams) {
1334         CHECK_WARNING_CONTINUE_LOG(stream == nullptr, "stream is null");
1335         MEDIA_INFO_LOG("CreateMediaLibrary get captureStream");
1336         auto streamCapture = CastStream<HStreamCapture>(stream);
1337         isBursting = streamCapture->IsBurstCapture(captureId);
1338         if (isBursting) {
1339             burstKey = streamCapture->GetBurstKey(captureId);
1340             streamCapture->SetBurstImages(captureId, imageId);
1341             isCoverPhoto = streamCapture->IsBurstCover(captureId);
1342             int32_t burstSeqId = cameraPhotoProxy->GetBurstSeqId();
1343             int32_t imageSeqId = streamCapture->GetCurBurstSeq(captureId);
1344             int32_t displaySeqId = (burstSeqId != invalidBurstSeqId) ? burstSeqId : imageSeqId;
1345             cameraPhotoProxy->SetDisplayName(CreateBurstDisplayName(imageSeqId, displaySeqId));
1346             streamCapture->CheckResetBurstKey(captureId);
1347             MEDIA_INFO_LOG("isBursting burstKey:%{public}s isCoverPhoto:%{public}d", burstKey.c_str(), isCoverPhoto);
1348             int32_t burstShotType = 3;
1349             cameraShotType = burstShotType;
1350             cameraPhotoProxy->SetBurstInfo(burstKey, isCoverPhoto);
1351             break;
1352         }
1353         MEDIA_INFO_LOG("CreateMediaLibrary not Bursting");
1354     }
1355 }
1356 
ConfigPayload(uint32_t pid,uint32_t tid,const char * bundleName,int32_t qosLevel,std::unordered_map<std::string,std::string> & mapPayload)1357 void HStreamOperator::ConfigPayload(uint32_t pid, uint32_t tid, const char *bundleName, int32_t qosLevel,
1358     std::unordered_map<std::string, std::string> &mapPayload)
1359 {
1360     std::string strBundleName = bundleName;
1361     std::string strPid = std::to_string(pid);
1362     std::string strTid = std::to_string(tid);
1363     std::string strQos = std::to_string(qosLevel);
1364     mapPayload["pid"] = strPid;
1365     mapPayload[strTid] = strQos;
1366     mapPayload["bundleName"] = strBundleName;
1367     MEDIA_INFO_LOG("mapPayload pid: %{public}s. tid: %{public}s. Qos: %{public}s",
1368         strPid.c_str(), strTid.c_str(), strQos.c_str());
1369 }
1370 
CreateMediaLibrary(sptr<CameraPhotoProxy> & photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1371 int32_t HStreamOperator::CreateMediaLibrary(sptr<CameraPhotoProxy>& photoProxy, std::string& uri,
1372     int32_t& cameraShotType, std::string& burstKey, int64_t timestamp)
1373 {
1374     CAMERA_SYNC_TRACE;
1375     constexpr int32_t movingPhotoShotType = 2;
1376     constexpr int32_t imageShotType = 0;
1377     cameraShotType = isSetMotionPhoto_ ? movingPhotoShotType : imageShotType;
1378     MessageParcel data;
1379     photoProxy->WriteToParcel(data);
1380     photoProxy->CameraFreeBufferHandle();
1381     sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
1382     cameraPhotoProxy->ReadFromParcel(data);
1383     cameraPhotoProxy->SetDisplayName(CreateDisplayName());
1384     int32_t captureId = cameraPhotoProxy->GetCaptureId();
1385     bool isBursting = false;
1386     string pictureId = cameraPhotoProxy->GetTitle() + "." + cameraPhotoProxy->GetExtension();
1387     CameraReportDfxUtils::GetInstance()->SetPictureId(captureId, pictureId);
1388     CameraReportDfxUtils::GetInstance()->SetPrepareProxyEndInfo(captureId);
1389     CameraReportDfxUtils::GetInstance()->SetAddProxyStartInfo(captureId);
1390     SetCameraPhotoProxyInfo(cameraPhotoProxy, cameraShotType, isBursting, burstKey);
1391     auto photoAssetProxy = PhotoAssetProxy::GetPhotoAssetProxy(cameraShotType, IPCSkeleton::GetCallingUid());
1392     CHECK_ERROR_RETURN_RET_LOG(
1393         photoAssetProxy == nullptr, CAMERA_ALLOC_ERROR, "HCaptureSession::CreateMediaLibrary get photoAssetProxy fail");
1394     photoAssetProxy->AddPhotoProxy((sptr<PhotoProxy>&)cameraPhotoProxy);
1395     uri = photoAssetProxy->GetPhotoAssetUri();
1396     if (!isBursting && isSetMotionPhoto_ && taskManager_) {
1397         MEDIA_INFO_LOG("taskManager setVideoFd start");
1398         taskManager_->SetVideoFd(timestamp, photoAssetProxy, captureId);
1399     } else {
1400         photoAssetProxy.reset();
1401     }
1402     CameraReportDfxUtils::GetInstance()->SetAddProxyEndInfo(captureId);
1403     return CAMERA_OK;
1404 }
1405 
RotatePicture(std::weak_ptr<PictureIntf> picture)1406 void RotatePicture(std::weak_ptr<PictureIntf> picture)
1407 {
1408     CAMERA_SYNC_TRACE;
1409     auto ptr = picture.lock();
1410     if (ptr) {
1411         ptr->RotatePicture();
1412     }
1413 }
1414 
ProcessPhotoProxy(int32_t captureId,std::shared_ptr<PictureIntf> picturePtr,bool isBursting,sptr<CameraServerPhotoProxy> cameraPhotoProxy,std::string & uri)1415 std::shared_ptr<PhotoAssetIntf> HStreamOperator::ProcessPhotoProxy(int32_t captureId,
1416     std::shared_ptr<PictureIntf> picturePtr, bool isBursting, sptr<CameraServerPhotoProxy> cameraPhotoProxy,
1417     std::string& uri)
1418 {
1419     CAMERA_SYNC_TRACE;
1420     CHECK_ERROR_RETURN_RET_LOG(picturePtr == nullptr, nullptr, "picturePtr is null");
1421     sptr<HStreamCapture> captureStream = nullptr;
1422     std::list<sptr<HStreamCommon>> captureStreams = streamContainer_.GetStreams(StreamType::CAPTURE);
1423     std::list<sptr<HStreamCommon>> captureStreamsOffline = streamContainerOffline_.GetStreams(StreamType::CAPTURE);
1424     captureStreams.insert(captureStreams.end(), captureStreamsOffline.begin(), captureStreamsOffline.end());
1425     for (auto& stream : captureStreams) {
1426         captureStream = CastStream<HStreamCapture>(stream);
1427         if (captureStream != nullptr) {
1428             break;
1429         }
1430     }
1431     CHECK_ERROR_RETURN_RET_LOG(captureStream == nullptr, nullptr, "stream is null");
1432     std::shared_ptr<PhotoAssetIntf> photoAssetProxy = nullptr;
1433     std::thread taskThread;
1434     if (isBursting) {
1435         int32_t cameraShotType = 3;
1436         photoAssetProxy = PhotoAssetProxy::GetPhotoAssetProxy(cameraShotType, IPCSkeleton::GetCallingUid());
1437     } else {
1438         photoAssetProxy = captureStream->GetPhotoAssetInstance(captureId);
1439     }
1440     CHECK_ERROR_RETURN_RET_LOG(photoAssetProxy == nullptr, nullptr, "photoAssetProxy is null");
1441     if (!isBursting && picturePtr) {
1442         MEDIA_DEBUG_LOG("CreateMediaLibrary RotatePicture E");
1443         taskThread = std::thread(RotatePicture, picturePtr);
1444     }
1445     bool isProfessionalPhoto = (opMode_ == static_cast<int32_t>(HDI::Camera::V1_3::OperationMode::PROFESSIONAL_PHOTO));
1446     if (isBursting || captureStream->GetAddPhotoProxyEnabled() == false || isProfessionalPhoto) {
1447         MEDIA_DEBUG_LOG("CreateMediaLibrary AddPhotoProxy E");
1448         string pictureId = cameraPhotoProxy->GetTitle() + "." + cameraPhotoProxy->GetExtension();
1449         CameraReportDfxUtils::GetInstance()->SetPictureId(captureId, pictureId);
1450         photoAssetProxy->AddPhotoProxy((sptr<PhotoProxy>&)cameraPhotoProxy);
1451         MEDIA_DEBUG_LOG("CreateMediaLibrary AddPhotoProxy X");
1452     }
1453     uri = photoAssetProxy->GetPhotoAssetUri();
1454     if (!isBursting && taskThread.joinable()) {
1455         taskThread.join();
1456         MEDIA_DEBUG_LOG("CreateMediaLibrary RotatePicture X");
1457     }
1458     MEDIA_DEBUG_LOG("CreateMediaLibrary NotifyLowQualityImage E");
1459     DeferredProcessing::DeferredProcessingService::GetInstance().NotifyLowQualityImage(
1460         photoAssetProxy->GetUserId(), uri, picturePtr);
1461     MEDIA_DEBUG_LOG("CreateMediaLibrary NotifyLowQualityImage X");
1462     return photoAssetProxy;
1463 }
1464 
CreateMediaLibrary(std::shared_ptr<PictureIntf> picture,sptr<CameraPhotoProxy> & photoProxy,std::string & uri,int32_t & cameraShotType,std::string & burstKey,int64_t timestamp)1465 int32_t HStreamOperator::CreateMediaLibrary(std::shared_ptr<PictureIntf> picture, sptr<CameraPhotoProxy>& photoProxy,
1466     std::string& uri, int32_t& cameraShotType, std::string& burstKey, int64_t timestamp)
1467 {
1468     CAMERA_SYNC_TRACE;
1469     const int MAX_RETRIES = 7;
1470     int32_t tempPid = getpid();
1471     int32_t tempTid = gettid();
1472     std::unordered_map<std::string, std::string> mapPayload;
1473     ConfigPayload(tempPid, tempTid, "camera_service", MAX_RETRIES, mapPayload);
1474     OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
1475         OHOS::ResourceSchedule::ResType::RES_TYPE_THREAD_QOS_CHANGE, 0, mapPayload);
1476 
1477     constexpr int32_t movingPhotoShotType = 2;
1478     constexpr int32_t imageShotType = 0;
1479     cameraShotType = isSetMotionPhoto_ ? movingPhotoShotType : imageShotType;
1480     MessageParcel data;
1481     photoProxy->WriteToParcel(data);
1482     photoProxy->CameraFreeBufferHandle();
1483     sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
1484     cameraPhotoProxy->ReadFromParcel(data);
1485     cameraPhotoProxy->SetDisplayName(CreateDisplayName());
1486     int32_t captureId = cameraPhotoProxy->GetCaptureId();
1487     bool isBursting = false;
1488     CameraReportDfxUtils::GetInstance()->SetPrepareProxyEndInfo(captureId);
1489     CameraReportDfxUtils::GetInstance()->SetAddProxyStartInfo(captureId);
1490     SetCameraPhotoProxyInfo(cameraPhotoProxy, cameraShotType, isBursting, burstKey);
1491     std::shared_ptr<PhotoAssetIntf> photoAssetProxy =
1492         ProcessPhotoProxy(captureId, picture, isBursting, cameraPhotoProxy, uri);
1493     CHECK_ERROR_RETURN_RET_LOG(photoAssetProxy == nullptr, CAMERA_INVALID_ARG, "photoAssetProxy is null");
1494     if (!isBursting && isSetMotionPhoto_ && taskManager_) {
1495         MEDIA_INFO_LOG("CreateMediaLibrary captureId :%{public}d", captureId);
1496         if (taskManager_) {
1497             taskManager_->SetVideoFd(timestamp, photoAssetProxy, captureId);
1498         }
1499     } else {
1500         photoAssetProxy.reset();
1501     }
1502     CameraReportDfxUtils::GetInstance()->SetAddProxyEndInfo(captureId);
1503     return CAMERA_OK;
1504 }
1505 
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)1506 int32_t HStreamOperator::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds)
1507 {
1508     MEDIA_INFO_LOG("HStreamOperator::OnCaptureStarted captureId:%{public}d, streamIds:%{public}s", captureId,
1509         Container2String(streamIds.begin(), streamIds.end()).c_str());
1510     std::lock_guard<std::mutex> lock(cbMutex_);
1511     for (auto& streamId : streamIds) {
1512         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(streamId);
1513         if (curStream == nullptr) {
1514             MEDIA_ERR_LOG("HStreamOperator::OnCaptureStarted StreamId: %{public}d not found", streamId);
1515             return CAMERA_INVALID_ARG;
1516         } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1517             CastStream<HStreamRepeat>(curStream)->OnFrameStarted();
1518             CameraReportUtils::GetInstance().SetOpenCamPerfEndInfo();
1519             CameraReportUtils::GetInstance().SetModeChangePerfEndInfo();
1520             CameraReportUtils::GetInstance().SetSwitchCamPerfEndInfo();
1521         } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1522             CastStream<HStreamCapture>(curStream)->OnCaptureStarted(captureId);
1523         }
1524     }
1525     return CAMERA_OK;
1526 }
1527 
StartRecord(uint64_t timestamp,int32_t rotation,int32_t captureId)1528 void HStreamOperator::StartRecord(uint64_t timestamp, int32_t rotation, int32_t captureId)
1529 {
1530     if (isSetMotionPhoto_) {
1531         taskManager_->SubmitTask([this, timestamp, rotation, captureId]() {
1532             this->StartOnceRecord(timestamp, rotation, captureId);
1533         });
1534     }
1535 }
1536 
SessionDrainImageCallback(std::vector<sptr<FrameRecord>> & frameCacheList,wptr<MovingPhotoListener> listener,wptr<MovingPhotoVideoCache> cache,uint64_t timestamp,int32_t rotation,int32_t captureId)1537 SessionDrainImageCallback::SessionDrainImageCallback(std::vector<sptr<FrameRecord>>& frameCacheList,
1538                                                      wptr<MovingPhotoListener> listener,
1539                                                      wptr<MovingPhotoVideoCache> cache,
1540                                                      uint64_t timestamp,
1541                                                      int32_t rotation,
1542                                                      int32_t captureId)
1543     : frameCacheList_(frameCacheList), listener_(listener), videoCache_(cache), timestamp_(timestamp),
1544       rotation_(rotation), captureId_(captureId)
1545 {
1546 }
1547 
~SessionDrainImageCallback()1548 SessionDrainImageCallback::~SessionDrainImageCallback()
1549 {
1550     MEDIA_INFO_LOG("~SessionDrainImageCallback enter");
1551 }
1552 
OnDrainImage(sptr<FrameRecord> frame)1553 void SessionDrainImageCallback::OnDrainImage(sptr<FrameRecord> frame)
1554 {
1555     MEDIA_INFO_LOG("OnDrainImage enter");
1556     {
1557         std::lock_guard<std::mutex> lock(mutex_);
1558         frameCacheList_.push_back(frame);
1559     }
1560     auto videoCache = videoCache_.promote();
1561     if (frame->IsIdle() && videoCache) {
1562         videoCache->CacheFrame(frame);
1563     } else if (frame->IsFinishCache() && videoCache) {
1564         videoCache->OnImageEncoded(frame, frame->IsEncoded());
1565     } else if (frame->IsReadyConvert()) {
1566         MEDIA_DEBUG_LOG("frame is ready convert");
1567     } else {
1568         MEDIA_INFO_LOG("videoCache and frame is not useful");
1569     }
1570 }
1571 
OnDrainImageFinish(bool isFinished)1572 void SessionDrainImageCallback::OnDrainImageFinish(bool isFinished)
1573 {
1574     MEDIA_INFO_LOG("OnDrainImageFinish enter");
1575     auto videoCache = videoCache_.promote();
1576     if (videoCache) {
1577         std::lock_guard<std::mutex> lock(mutex_);
1578         videoCache_->GetFrameCachedResult(
1579             frameCacheList_,
1580             [videoCache](const std::vector<sptr<FrameRecord>>& frameRecords,
1581                          uint64_t timestamp,
1582                          int32_t rotation,
1583                          int32_t captureId) { videoCache->DoMuxerVideo(frameRecords, timestamp, rotation, captureId); },
1584             timestamp_,
1585             rotation_,
1586             captureId_);
1587     }
1588     auto listener = listener_.promote();
1589     CHECK_EXECUTE(listener && isFinished, listener->RemoveDrainImageManager(this));
1590 }
1591 
StartOnceRecord(uint64_t timestamp,int32_t rotation,int32_t captureId)1592 void HStreamOperator::StartOnceRecord(uint64_t timestamp, int32_t rotation, int32_t captureId)
1593 {
1594     MEDIA_INFO_LOG("StartOnceRecord enter");
1595     // frameCacheList only used by now thread
1596     std::lock_guard<std::mutex> lock(movingPhotoStatusLock_);
1597     if (!livephotoListener_) {
1598         MEDIA_ERR_LOG("HCaptureSession::StartOnceRecord livephotoListener_ is null");
1599         return;
1600     }
1601     std::vector<sptr<FrameRecord>> frameCacheList;
1602     sptr<SessionDrainImageCallback> imageCallback = new SessionDrainImageCallback(frameCacheList,
1603         livephotoListener_, videoCache_, timestamp, rotation, captureId);
1604     livephotoListener_->ClearCache(timestamp);
1605     livephotoListener_->DrainOutImage(imageCallback);
1606     MEDIA_INFO_LOG("StartOnceRecord end");
1607 }
1608 
CreateStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1> & streamInfos)1609 int32_t HStreamOperator::CreateStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1>& streamInfos)
1610 {
1611     CamRetCode hdiRc = HDI::Camera::V1_0::NO_ERROR;
1612     uint32_t major;
1613     uint32_t minor;
1614     CHECK_ERROR_RETURN_RET_LOG(streamInfos.empty(), CAMERA_OK, "HStreamOperator::CreateStreams streamInfos is empty!");
1615     std::lock_guard<std::mutex> lock(opMutex_);
1616     sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperatorV1_1;
1617     sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator = GetHDIStreamOperator();
1618     CHECK_ERROR_RETURN_RET_LOG(streamOperator == nullptr, CAMERA_UNKNOWN_ERROR,
1619         "HStreamOperator::CreateStreams GetStreamOperator is null!");
1620     // get higher streamOperator version
1621     streamOperator->GetVersion(major, minor);
1622     MEDIA_INFO_LOG("streamOperator GetVersion major:%{public}d, minor:%{public}d", major, minor);
1623     if (major >= HDI_VERSION_1 && minor >= HDI_VERSION_1) {
1624         streamOperatorV1_1 = OHOS::HDI::Camera::V1_1::IStreamOperator::CastFrom(streamOperator);
1625         if (streamOperatorV1_1 == nullptr) {
1626             MEDIA_ERR_LOG("HStreamOperator::CreateStreams IStreamOperator cast to V1_1 error");
1627             streamOperatorV1_1 = static_cast<OHOS::HDI::Camera::V1_1::IStreamOperator*>(streamOperator.GetRefPtr());
1628         }
1629     }
1630     if (streamOperatorV1_1 != nullptr) {
1631         MEDIA_INFO_LOG("HStreamOperator::CreateStreams streamOperator V1_1");
1632         for (auto streamInfo : streamInfos) {
1633             if (streamInfo.extendedStreamInfos.size() > 0) {
1634                 MEDIA_INFO_LOG("HCameraDevice::CreateStreams streamId:%{public}d width:%{public}d height:%{public}d"
1635                     "format:%{public}d dataspace:%{public}d", streamInfo.v1_0.streamId_, streamInfo.v1_0.width_,
1636                     streamInfo.v1_0.height_, streamInfo.v1_0.format_, streamInfo.v1_0.dataspace_);
1637                 MEDIA_INFO_LOG("HStreamOperator::CreateStreams streamOperator V1_1 type %{public}d",
1638                     streamInfo.extendedStreamInfos[0].type);
1639             }
1640         }
1641         hdiRc = (CamRetCode)(streamOperatorV1_1->CreateStreams_V1_1(streamInfos));
1642     } else {
1643         MEDIA_INFO_LOG("HStreamOperator::CreateStreams streamOperator V1_0");
1644         std::vector<StreamInfo> streamInfos_V1_0;
1645         for (auto streamInfo : streamInfos) {
1646             streamInfos_V1_0.emplace_back(streamInfo.v1_0);
1647         }
1648         hdiRc = (CamRetCode)(streamOperator->CreateStreams(streamInfos_V1_0));
1649     }
1650     if (hdiRc != HDI::Camera::V1_0::NO_ERROR) {
1651         MEDIA_ERR_LOG("HStreamOperator::CreateStreams(), Failed to commit %{public}d", hdiRc);
1652         CameraReportUtils::ReportCameraError(
1653             "HStreamOperator::CreateStreams", hdiRc, true, CameraReportUtils::GetCallerInfo());
1654         std::vector<int32_t> streamIds;
1655         for (auto& streamInfo : streamInfos) {
1656             streamIds.emplace_back(streamInfo.v1_0.streamId_);
1657         }
1658         CHECK_ERROR_PRINT_LOG(!streamIds.empty() &&
1659             streamOperator->ReleaseStreams(streamIds) != HDI::Camera::V1_0::NO_ERROR,
1660             "HStreamOperator::CreateStreams(), Failed to release streams");
1661     }
1662     for (auto& info : streamInfos) {
1663         MEDIA_INFO_LOG("HStreamOperator::CreateStreams stream id is:%{public}d", info.v1_0.streamId_);
1664     }
1665     return HdiToServiceError(hdiRc);
1666 }
1667 
UpdateStreams(std::vector<StreamInfo_V1_1> & streamInfos)1668 int32_t HStreamOperator::UpdateStreams(std::vector<StreamInfo_V1_1>& streamInfos)
1669 {
1670     sptr<OHOS::HDI::Camera::V1_2::IStreamOperator> streamOperatorV1_2;
1671     auto streamOperator = GetHDIStreamOperator();
1672     CHECK_ERROR_RETURN_RET_LOG(streamOperator == nullptr, CAMERA_UNKNOWN_ERROR,
1673         "HStreamOperator::UpdateStreams GetStreamOperator is null!");
1674     uint32_t major;
1675     uint32_t minor;
1676     streamOperator->GetVersion(major, minor);
1677     MEDIA_INFO_LOG("UpdateStreams::UpdateStreams GetVersion major:%{public}d, minor:%{public}d", major, minor);
1678     if (major >= HDI_VERSION_1 && minor >= HDI_VERSION_2) {
1679         streamOperatorV1_2 = OHOS::HDI::Camera::V1_2::IStreamOperator::CastFrom(streamOperator);
1680         if (streamOperatorV1_2 == nullptr) {
1681             MEDIA_ERR_LOG("HStreamOperator::UpdateStreams IStreamOperator cast to V1_2 error");
1682             streamOperatorV1_2 = static_cast<OHOS::HDI::Camera::V1_2::IStreamOperator*>(streamOperator.GetRefPtr());
1683         }
1684     }
1685     CamRetCode hdiRc = HDI::Camera::V1_0::CamRetCode::NO_ERROR;
1686     if (streamOperatorV1_2 != nullptr) {
1687         MEDIA_DEBUG_LOG("HStreamOperator::UpdateStreams streamOperator V1_2");
1688         hdiRc = (CamRetCode)(streamOperatorV1_2->UpdateStreams(streamInfos));
1689     } else {
1690         MEDIA_DEBUG_LOG("HStreamOperator::UpdateStreams failed, streamOperator V1_2 is null.");
1691         return CAMERA_UNKNOWN_ERROR;
1692     }
1693     return HdiToServiceError(hdiRc);
1694 }
1695 
OnCaptureStarted_V1_2(int32_t captureId,const std::vector<OHOS::HDI::Camera::V1_2::CaptureStartedInfo> & infos)1696 int32_t HStreamOperator::OnCaptureStarted_V1_2(
1697     int32_t captureId, const std::vector<OHOS::HDI::Camera::V1_2::CaptureStartedInfo>& infos)
1698 {
1699     MEDIA_INFO_LOG("HStreamOperator::OnCaptureStarted_V1_2 captureId:%{public}d", captureId);
1700     std::lock_guard<std::mutex> lock(cbMutex_);
1701     for (auto& captureInfo : infos) {
1702         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(captureInfo.streamId_);
1703         if (curStream == nullptr) {
1704             MEDIA_ERR_LOG("HStreamOperator::OnCaptureStarted_V1_2 StreamId: %{public}d not found."
1705                           " exposureTime: %{public}u",
1706                 captureInfo.streamId_, captureInfo.exposureTime_);
1707             return CAMERA_INVALID_ARG;
1708         } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1709             MEDIA_DEBUG_LOG("HStreamOperator::OnCaptureStarted_V1_2 StreamId: %{public}d."
1710                             " exposureTime: %{public}u",
1711                 captureInfo.streamId_, captureInfo.exposureTime_);
1712             CastStream<HStreamCapture>(curStream)->OnCaptureStarted(captureId, captureInfo.exposureTime_);
1713         }
1714     }
1715     return CAMERA_OK;
1716 }
1717 
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)1718 int32_t HStreamOperator::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
1719 {
1720     MEDIA_INFO_LOG("HStreamOperator::OnCaptureEnded");
1721     std::lock_guard<std::mutex> lock(cbMutex_);
1722     for (auto& captureInfo : infos) {
1723         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(captureInfo.streamId_);
1724         if (curStream == nullptr) {
1725             MEDIA_ERR_LOG("HStreamOperator::OnCaptureEnded StreamId: %{public}d not found."
1726                           " Framecount: %{public}d",
1727                 captureInfo.streamId_, captureInfo.frameCount_);
1728             return CAMERA_INVALID_ARG;
1729         } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1730             CastStream<HStreamRepeat>(curStream)->OnFrameEnded(captureInfo.frameCount_);
1731         } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1732             CastStream<HStreamCapture>(curStream)->OnCaptureEnded(captureId, captureInfo.frameCount_);
1733         }
1734     }
1735     return CAMERA_OK;
1736 }
1737 
OnCaptureEndedExt(int32_t captureId,const std::vector<OHOS::HDI::Camera::V1_3::CaptureEndedInfoExt> & infos)1738 int32_t HStreamOperator::OnCaptureEndedExt(int32_t captureId,
1739     const std::vector<OHOS::HDI::Camera::V1_3::CaptureEndedInfoExt>& infos)
1740 {
1741     MEDIA_INFO_LOG("HStreamOperator::OnCaptureEndedExt captureId:%{public}d", captureId);
1742     std::lock_guard<std::mutex> lock(cbMutex_);
1743     for (auto& captureInfo : infos) {
1744         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(captureInfo.streamId_);
1745         if (curStream == nullptr) {
1746             MEDIA_ERR_LOG("HStreamOperator::OnCaptureEndedExt StreamId: %{public}d not found."
1747                           " Framecount: %{public}d",
1748                 captureInfo.streamId_, captureInfo.frameCount_);
1749             return CAMERA_INVALID_ARG;
1750         } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1751             CastStream<HStreamRepeat>(curStream)->OnFrameEnded(captureInfo.frameCount_);
1752             CaptureEndedInfoExt extInfo;
1753             extInfo.streamId = captureInfo.streamId_;
1754             extInfo.frameCount = captureInfo.frameCount_;
1755             extInfo.isDeferredVideoEnhancementAvailable = captureInfo.isDeferredVideoEnhancementAvailable_;
1756             extInfo.videoId = captureInfo.videoId_;
1757             MEDIA_INFO_LOG("HStreamOperator::OnCaptureEndedExt captureId:%{public}d videoId:%{public}s "
1758                            "isDeferredVideo:%{public}d",
1759                 captureId, extInfo.videoId.c_str(), extInfo.isDeferredVideoEnhancementAvailable);
1760             CastStream<HStreamRepeat>(curStream)->OnDeferredVideoEnhancementInfo(extInfo);
1761         }
1762     }
1763     return CAMERA_OK;
1764 }
1765 
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)1766 int32_t HStreamOperator::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
1767 {
1768     MEDIA_INFO_LOG("HStreamOperator::OnCaptureError");
1769     std::lock_guard<std::mutex> lock(cbMutex_);
1770     for (auto& errInfo : infos) {
1771         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(errInfo.streamId_);
1772         if (curStream == nullptr) {
1773             MEDIA_ERR_LOG("HStreamOperator::OnCaptureError StreamId: %{public}d not found."
1774                           " Error: %{public}d",
1775                 errInfo.streamId_, errInfo.error_);
1776             return CAMERA_INVALID_ARG;
1777         } else if (curStream->GetStreamType() == StreamType::REPEAT) {
1778             CastStream<HStreamRepeat>(curStream)->OnFrameError(errInfo.error_);
1779         } else if (curStream->GetStreamType() == StreamType::CAPTURE) {
1780             auto captureStream = CastStream<HStreamCapture>(curStream);
1781             captureStream->rotationMap_.Erase(captureId);
1782             captureStream->OnCaptureError(captureId, errInfo.error_);
1783         }
1784     }
1785     return CAMERA_OK;
1786 }
1787 
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)1788 int32_t HStreamOperator::OnFrameShutter(
1789     int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp)
1790 {
1791     MEDIA_INFO_LOG("HStreamOperator::OnFrameShutter ts is:%{public}" PRIu64, timestamp);
1792     std::lock_guard<std::mutex> lock(cbMutex_);
1793     for (auto& streamId : streamIds) {
1794         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(streamId);
1795         if ((curStream != nullptr) && (curStream->GetStreamType() == StreamType::CAPTURE)) {
1796             auto captureStream = CastStream<HStreamCapture>(curStream);
1797             int32_t rotation = 0;
1798             captureStream->rotationMap_.Find(captureId, rotation);
1799             StartMovingPhotoEncode(rotation, timestamp, captureStream->format_, captureId);
1800             captureStream->OnFrameShutter(captureId, timestamp);
1801         } else {
1802             MEDIA_ERR_LOG("HStreamOperator::OnFrameShutter StreamId: %{public}d not found", streamId);
1803             return CAMERA_INVALID_ARG;
1804         }
1805     }
1806     return CAMERA_OK;
1807 }
1808 
OnFrameShutterEnd(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)1809 int32_t HStreamOperator::OnFrameShutterEnd(
1810     int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp)
1811 {
1812     MEDIA_INFO_LOG("HStreamOperator::OnFrameShutterEnd ts is:%{public}" PRIu64, timestamp);
1813     std::lock_guard<std::mutex> lock(cbMutex_);
1814     for (auto& streamId : streamIds) {
1815         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(streamId);
1816         if ((curStream != nullptr) && (curStream->GetStreamType() == StreamType::CAPTURE)) {
1817             auto captureStream = CastStream<HStreamCapture>(curStream);
1818             captureStream->rotationMap_.Erase(captureId);
1819             captureStream->OnFrameShutterEnd(captureId, timestamp);
1820             mlastCaptureId = captureId;
1821         } else {
1822             MEDIA_ERR_LOG("HStreamOperator::OnFrameShutterEnd StreamId: %{public}d not found", streamId);
1823             return CAMERA_INVALID_ARG;
1824         }
1825     }
1826     return CAMERA_OK;
1827 }
1828 
OnCaptureReady(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)1829 int32_t HStreamOperator::OnCaptureReady(
1830     int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp)
1831 {
1832     MEDIA_DEBUG_LOG("HStreamOperator::OnCaptureReady");
1833     std::lock_guard<std::mutex> lock(cbMutex_);
1834     for (auto& streamId : streamIds) {
1835         sptr<HStreamCommon> curStream = GetHdiStreamByStreamID(streamId);
1836         if ((curStream != nullptr) && (curStream->GetStreamType() == StreamType::CAPTURE)) {
1837             CastStream<HStreamCapture>(curStream)->OnCaptureReady(captureId, timestamp);
1838         } else {
1839             MEDIA_ERR_LOG("HStreamOperator::OnCaptureReady StreamId: %{public}d not found", streamId);
1840             return CAMERA_INVALID_ARG;
1841         }
1842     }
1843     return CAMERA_OK;
1844 }
1845 
OnResult(int32_t streamId,const std::vector<uint8_t> & result)1846 int32_t HStreamOperator::OnResult(int32_t streamId, const std::vector<uint8_t>& result)
1847 {
1848     MEDIA_DEBUG_LOG("HStreamOperator::OnResult");
1849     sptr<HStreamCommon> curStream;
1850     const int32_t metaStreamId = -1;
1851     if (streamId == metaStreamId) {
1852         curStream = GetStreamByStreamID(streamId);
1853     } else {
1854         curStream = GetHdiStreamByStreamID(streamId);
1855     }
1856     if ((curStream != nullptr) && (curStream->GetStreamType() == StreamType::METADATA)) {
1857         CastStream<HStreamMetadata>(curStream)->OnMetaResult(streamId, result);
1858     } else {
1859         MEDIA_ERR_LOG("HStreamOperator::OnResult StreamId: %{public}d is null or not Not adapted", streamId);
1860         return CAMERA_INVALID_ARG;
1861     }
1862     return CAMERA_OK;
1863 }
1864 
GetFrameRateRange()1865 std::vector<int32_t> HStreamOperator::GetFrameRateRange()
1866 {
1867     auto repeatStreams = streamContainer_.GetStreams(StreamType::REPEAT);
1868     for (auto& item : repeatStreams) {
1869         auto curStreamRepeat = CastStream<HStreamRepeat>(item);
1870         auto repeatType = curStreamRepeat->GetRepeatStreamType();
1871         if (repeatType == RepeatStreamType::PREVIEW) {
1872             return curStreamRepeat->GetFrameRateRange();
1873         }
1874     }
1875     return {};
1876 }
1877 
AddStream(sptr<HStreamCommon> stream)1878 bool StreamContainer::AddStream(sptr<HStreamCommon> stream)
1879 {
1880     std::lock_guard<std::mutex> lock(streamsLock_);
1881     auto& list = streams_[stream->GetStreamType()];
1882     auto it = std::find_if(list.begin(), list.end(), [stream](auto item) { return item == stream; });
1883     if (it == list.end()) {
1884         list.emplace_back(stream);
1885         return true;
1886     }
1887     return false;
1888 }
1889 
RemoveStream(sptr<HStreamCommon> stream)1890 bool StreamContainer::RemoveStream(sptr<HStreamCommon> stream)
1891 {
1892     std::lock_guard<std::mutex> lock(streamsLock_);
1893     auto& list = streams_[stream->GetStreamType()];
1894     auto it = std::find_if(list.begin(), list.end(), [stream](auto item) { return item == stream; });
1895     CHECK_ERROR_RETURN_RET(it == list.end(), false);
1896     list.erase(it);
1897     return true;
1898 }
1899 
GetStream(int32_t streamId)1900 sptr<HStreamCommon> StreamContainer::GetStream(int32_t streamId)
1901 {
1902     std::lock_guard<std::mutex> lock(streamsLock_);
1903     for (auto& pair : streams_) {
1904         for (auto& stream : pair.second) {
1905             CHECK_ERROR_RETURN_RET(stream->GetFwkStreamId() == streamId, stream);
1906         }
1907     }
1908     return nullptr;
1909 }
1910 
GetHdiStream(int32_t streamId)1911 sptr<HStreamCommon> StreamContainer::GetHdiStream(int32_t streamId)
1912 {
1913     std::lock_guard<std::mutex> lock(streamsLock_);
1914     for (auto& pair : streams_) {
1915         for (auto& stream : pair.second) {
1916             CHECK_ERROR_RETURN_RET(stream->GetHdiStreamId() == streamId, stream);
1917         }
1918     }
1919     return nullptr;
1920 }
1921 
Clear()1922 void StreamContainer::Clear()
1923 {
1924     std::lock_guard<std::mutex> lock(streamsLock_);
1925     streams_.clear();
1926 }
1927 
Size()1928 size_t StreamContainer::Size()
1929 {
1930     std::lock_guard<std::mutex> lock(streamsLock_);
1931     size_t totalSize = 0;
1932     for (auto& pair : streams_) {
1933         totalSize += pair.second.size();
1934     }
1935     return totalSize;
1936 }
1937 
GetStreams(const StreamType streamType)1938 std::list<sptr<HStreamCommon>> StreamContainer::GetStreams(const StreamType streamType)
1939 {
1940     std::lock_guard<std::mutex> lock(streamsLock_);
1941     std::list<sptr<HStreamCommon>> totalOrderedStreams;
1942     for (auto& stream : streams_[streamType]) {
1943         auto insertPos = std::find_if(totalOrderedStreams.begin(), totalOrderedStreams.end(),
1944             [&stream](auto& it) { return stream->GetFwkStreamId() <= it->GetFwkStreamId(); });
1945         totalOrderedStreams.emplace(insertPos, stream);
1946     }
1947     return totalOrderedStreams;
1948 }
1949 
GetAllStreams()1950 std::list<sptr<HStreamCommon>> StreamContainer::GetAllStreams()
1951 {
1952     std::lock_guard<std::mutex> lock(streamsLock_);
1953     std::list<sptr<HStreamCommon>> totalOrderedStreams;
1954     for (auto& pair : streams_) {
1955         for (auto& stream : pair.second) {
1956             auto insertPos = std::find_if(totalOrderedStreams.begin(), totalOrderedStreams.end(),
1957                 [&stream](auto& it) { return stream->GetFwkStreamId() <= it->GetFwkStreamId(); });
1958             totalOrderedStreams.emplace(insertPos, stream);
1959         }
1960     }
1961     return totalOrderedStreams;
1962 }
1963 
MovingPhotoListener(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper,sptr<Surface> metaSurface,shared_ptr<FixedSizeList<MetaElementType>> metaCache,uint32_t preCacheFrameCount,uint32_t postCacheFrameCount)1964 MovingPhotoListener::MovingPhotoListener(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper, sptr<Surface> metaSurface,
1965     shared_ptr<FixedSizeList<MetaElementType>> metaCache, uint32_t preCacheFrameCount, uint32_t postCacheFrameCount)
1966     : movingPhotoSurfaceWrapper_(surfaceWrapper),
1967       metaSurface_(metaSurface),
1968       metaCache_(metaCache),
1969       recorderBufferQueue_("videoBuffer", preCacheFrameCount),
1970       postCacheFrameCount_(postCacheFrameCount)
1971 {
1972     shutterTime_ = 0;
1973     bufferTaskManager_ = make_shared<TaskManager>("BufferTaskManager", OPERATOR_DEFAULT_ENCODER_THREAD_NUMBER, true);
1974 }
1975 
~MovingPhotoListener()1976 MovingPhotoListener::~MovingPhotoListener()
1977 {
1978     recorderBufferQueue_.SetActive(false);
1979     metaCache_->clear();
1980     recorderBufferQueue_.Clear();
1981     MEDIA_ERR_LOG("HStreamRepeat::LivePhotoListener ~ end");
1982 }
1983 
RemoveDrainImageManager(sptr<SessionDrainImageCallback> callback)1984 void MovingPhotoListener::RemoveDrainImageManager(sptr<SessionDrainImageCallback> callback)
1985 {
1986     callbackMap_.Erase(callback);
1987     MEDIA_INFO_LOG("RemoveDrainImageManager drainImageManagerVec_ Start %d", callbackMap_.Size());
1988 }
1989 
ClearCache(uint64_t timestamp)1990 void MovingPhotoListener::ClearCache(uint64_t timestamp)
1991 {
1992     CHECK_ERROR_RETURN(!isNeededClear_.load());
1993     MEDIA_INFO_LOG("ClearCache enter");
1994     shutterTime_ = static_cast<int64_t>(timestamp);
1995     while (!recorderBufferQueue_.Empty()) {
1996         sptr<FrameRecord> popFrame = recorderBufferQueue_.Front();
1997         MEDIA_DEBUG_LOG("surface_ release surface buffer %{public}llu, timestamp %{public}llu",
1998             (long long unsigned)popFrame->GetTimeStamp(), (long long unsigned)timestamp);
1999         if (popFrame->GetTimeStamp() > shutterTime_) {
2000             isNeededClear_ = false;
2001             MEDIA_INFO_LOG("ClearCache end");
2002             return;
2003         }
2004         recorderBufferQueue_.Pop();
2005         popFrame->ReleaseSurfaceBuffer(movingPhotoSurfaceWrapper_);
2006         popFrame->ReleaseMetaBuffer(metaSurface_, true);
2007     }
2008 }
2009 
SetClearFlag()2010 void MovingPhotoListener::SetClearFlag()
2011 {
2012     MEDIA_INFO_LOG("need clear cache!");
2013     isNeededClear_ = true;
2014 }
2015 
StopDrainOut()2016 void MovingPhotoListener::StopDrainOut()
2017 {
2018     MEDIA_INFO_LOG("StopDrainOut drainImageManagerVec_ Start %d", callbackMap_.Size());
2019     callbackMap_.Iterate([](const sptr<SessionDrainImageCallback> callback, sptr<DrainImageManager> manager) {
2020         manager->DrainFinish(false);
2021     });
2022     callbackMap_.Clear();
2023 }
2024 
OnBufferArrival(sptr<SurfaceBuffer> buffer,int64_t timestamp,GraphicTransformType transform)2025 void MovingPhotoListener::OnBufferArrival(sptr<SurfaceBuffer> buffer, int64_t timestamp, GraphicTransformType transform)
2026 {
2027     MEDIA_DEBUG_LOG("OnBufferArrival timestamp %{public}llu", (long long unsigned)timestamp);
2028     auto thisPtr = sptr<MovingPhotoListener>(this);
2029     bufferTaskManager_->SubmitTask([thisPtr, buffer, timestamp, transform]() {
2030         if (thisPtr->recorderBufferQueue_.Full()) {
2031             MEDIA_DEBUG_LOG("surface_ release surface buffer");
2032             sptr<FrameRecord> popFrame = thisPtr->recorderBufferQueue_.Pop();
2033             popFrame->ReleaseSurfaceBuffer(thisPtr->movingPhotoSurfaceWrapper_);
2034             popFrame->ReleaseMetaBuffer(thisPtr->metaSurface_, true);
2035             MEDIA_DEBUG_LOG("surface_ release surface buffer: %{public}s, refCount: %{public}d",
2036                 popFrame->GetFrameId().c_str(), popFrame->GetSptrRefCount());
2037         }
2038         MEDIA_DEBUG_LOG("surface_ push buffer %{public}d x %{public}d, stride is %{public}d",
2039             buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight(), buffer->GetStride());
2040         sptr<FrameRecord> frameRecord = new (std::nothrow) FrameRecord(buffer, timestamp, transform);
2041         CHECK_ERROR_RETURN_LOG(frameRecord == nullptr,
2042             "MovingPhotoListener::OnBufferAvailable create FrameRecord fail!");
2043         if (thisPtr->isNeededClear_ && thisPtr->isNeededPop_) {
2044             if (timestamp < thisPtr->shutterTime_) {
2045                 frameRecord->ReleaseSurfaceBuffer(thisPtr->movingPhotoSurfaceWrapper_);
2046                 MEDIA_INFO_LOG("Drop this frame in cache");
2047                 return;
2048             } else {
2049                 thisPtr->isNeededClear_ = false;
2050                 thisPtr->isNeededPop_ = false;
2051                 MEDIA_INFO_LOG("ClearCache end");
2052             }
2053         }
2054         thisPtr->recorderBufferQueue_.Push(frameRecord);
2055         auto metaPair = thisPtr->metaCache_->find_if([timestamp](const MetaElementType& value) {
2056             return value.first == timestamp;
2057         });
2058         if (metaPair.has_value()) {
2059             MEDIA_DEBUG_LOG("frame has meta");
2060             frameRecord->SetMetaBuffer(metaPair.value().second);
2061         }
2062         vector<sptr<SessionDrainImageCallback>> callbacks;
2063         thisPtr->callbackMap_.Iterate([frameRecord, &callbacks](const sptr<SessionDrainImageCallback> callback,
2064             sptr<DrainImageManager> manager) {
2065             callbacks.push_back(callback);
2066         });
2067         for (sptr<SessionDrainImageCallback> drainImageCallback : callbacks) {
2068             sptr<DrainImageManager> drainImageManager;
2069             if (thisPtr->callbackMap_.Find(drainImageCallback, drainImageManager)) {
2070                 std::lock_guard<std::mutex> lock(drainImageManager->drainImageLock_);
2071                 drainImageManager->DrainImage(frameRecord);
2072             }
2073         }
2074     });
2075 }
2076 
DrainOutImage(sptr<SessionDrainImageCallback> drainImageCallback)2077 void MovingPhotoListener::DrainOutImage(sptr<SessionDrainImageCallback> drainImageCallback)
2078 {
2079     sptr<DrainImageManager> drainImageManager =
2080         new DrainImageManager(drainImageCallback, recorderBufferQueue_.Size() + postCacheFrameCount_);
2081     {
2082         MEDIA_INFO_LOG("DrainOutImage enter %{public}zu", recorderBufferQueue_.Size());
2083         callbackMap_.Insert(drainImageCallback, drainImageManager);
2084     }
2085     // Convert recorderBufferQueue_ to a vector
2086     std::vector<sptr<FrameRecord>> frameList = recorderBufferQueue_.GetAllElements();
2087     CHECK_EXECUTE(!frameList.empty(), frameList.back()->SetCoverFrame());
2088     std::lock_guard<std::mutex> lock(drainImageManager->drainImageLock_);
2089     for (const auto& frame : frameList) {
2090         MEDIA_DEBUG_LOG("DrainOutImage enter DrainImage");
2091         drainImageManager->DrainImage(frame);
2092     }
2093 }
2094 
OnBufferAvailable()2095 void MovingPhotoMetaListener::OnBufferAvailable()
2096 {
2097     CHECK_ERROR_RETURN_LOG(!surface_, "streamRepeat surface is null");
2098     MEDIA_DEBUG_LOG("metaSurface_ OnBufferAvailable %{public}u", surface_->GetQueueSize());
2099     int64_t timestamp;
2100     OHOS::Rect damage;
2101     sptr<SurfaceBuffer> buffer;
2102     sptr<SyncFence> syncFence = SyncFence::INVALID_FENCE;
2103     SurfaceError surfaceRet = surface_->AcquireBuffer(buffer, syncFence, timestamp, damage);
2104     CHECK_ERROR_RETURN_LOG(surfaceRet != SURFACE_ERROR_OK, "Failed to acquire meta surface buffer");
2105     surfaceRet = surface_->DetachBufferFromQueue(buffer);
2106     CHECK_ERROR_RETURN_LOG(surfaceRet != SURFACE_ERROR_OK, "Failed to detach meta buffer. %{public}d", surfaceRet);
2107     metaCache_->add({timestamp, buffer});
2108 }
2109 
MovingPhotoMetaListener(sptr<Surface> surface,shared_ptr<FixedSizeList<MetaElementType>> metaCache)2110 MovingPhotoMetaListener::MovingPhotoMetaListener(sptr<Surface> surface,
2111     shared_ptr<FixedSizeList<MetaElementType>> metaCache)
2112     : surface_(surface), metaCache_(metaCache)
2113 {
2114 }
2115 
~MovingPhotoMetaListener()2116 MovingPhotoMetaListener::~MovingPhotoMetaListener()
2117 {
2118     MEDIA_ERR_LOG("HStreamRepeat::MovingPhotoMetaListener ~ end");
2119 }
2120 
2121 } // namespace CameraStandard
2122 } // namespace OHOS
2123