• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "PictureManagerThread"
17 
18 #include "picture_manager_thread.h"
19 #include "file_utils.h"
20 #include "media_log.h"
21 #include "parameter.h"
22 #include "parameters.h"
23 
24 using namespace std;
25 namespace OHOS {
26 namespace Media {
27 unique_ptr<PictureManagerThread> PictureManagerThread::instance_ = nullptr;
28 mutex PictureManagerThread::mutex_;
GetInstance()29 PictureManagerThread* PictureManagerThread::GetInstance()
30 {
31     if (instance_ == nullptr) {
32         lock_guard<mutex> lock(mutex_);
33         if (instance_ == nullptr) {
34             instance_ = make_unique<PictureManagerThread>();
35         }
36     }
37     return instance_.get();
38 }
PictureManagerThread()39 PictureManagerThread::PictureManagerThread()
40     : thread_(nullptr),
41       pauseFlag_(false),
42       stopFlag_(false),
43       state_(State::STOPPED)
44 {
45 }
46 
~PictureManagerThread()47 PictureManagerThread::~PictureManagerThread()
48 {
49     MEDIA_INFO_LOG("~PictureManagerThread end");
50     pictureDataOperations_ = nullptr;
51     Stop();
52 }
53 
State() const54 State PictureManagerThread::State() const
55 {
56     return state_;
57 }
58 
Start()59 void PictureManagerThread::Start()
60 {
61     unique_lock<mutex> locker(runningMutex_);
62     if (pictureDataOperations_ == nullptr) {
63         pictureDataOperations_ = new PictureDataOperations();
64     }
65     if (pauseFlag_) {
66         Stop();
67     }
68     if (thread_ == nullptr) {
69         pauseFlag_ = false;
70         stopFlag_ = false;
71         state_ = State::RUNNING;
72         thread_ = std::make_unique<std::thread>(&PictureManagerThread::Run, this);
73     }
74 }
75 
Stop()76 void PictureManagerThread::Stop()
77 {
78     MEDIA_INFO_LOG("enter ");
79     if (thread_ != nullptr) {
80         pauseFlag_ = false;
81         stopFlag_ = true;
82         condition_.notify_all();  // Notify one waiting thread, if there is one.
83         if (thread_->joinable()) {
84             thread_->join(); // wait for thread finished
85         }
86         thread_ = nullptr;
87         state_ = State::STOPPED;
88     }
89 }
90 
Pause()91 void PictureManagerThread::Pause()
92 {
93     MEDIA_INFO_LOG("enter ");
94     if (thread_ != nullptr) {
95         pauseFlag_ = true;
96         state_ = State::PAUSED;
97     }
98 }
99 
Resume()100 void PictureManagerThread::Resume()
101 {
102     MEDIA_INFO_LOG("enter ");
103     if (thread_ != nullptr) {
104         pauseFlag_ = false;
105         condition_.notify_all();
106         state_ = State::RUNNING;
107     }
108 }
109 
Run()110 void PictureManagerThread::Run()
111 {
112     MEDIA_INFO_LOG("enter thread run:");
113     string name("PictureManagerThread");
114     pthread_setname_np(pthread_self(), name.c_str());
115     while (!stopFlag_) {
116         if (pictureDataOperations_ == nullptr) {
117             pictureDataOperations_ = new PictureDataOperations();
118         }
119         pictureDataOperations_->CleanDateForPeriodical();
120         {
121             unique_lock<mutex> locker(threadMutex_);
122             condition_.wait_for(locker, std::chrono::seconds(1)); // 实际1S扫描一次
123         }
124         int32_t taskSize = pictureDataOperations_->GetPendingTaskSize();
125         if (lastPendingTaskSize_ != 0 && taskSize == 0) {
126             pauseFlag_ = true;
127             MEDIA_INFO_LOG("PictureManagerThread end.");
128             return;
129         }
130         lastPendingTaskSize_ = taskSize;
131     }
132     MEDIA_INFO_LOG("end thread run:");
133 }
134 
InsertPictureData(const std::string & imageId,sptr<PicturePair> & PicturePair,PictureType pictureType)135 void PictureManagerThread::InsertPictureData(const std::string& imageId, sptr<PicturePair>& PicturePair,
136     PictureType pictureType)
137 {
138     Start();
139     CHECK_AND_RETURN_LOG(pictureDataOperations_ != nullptr,
140         "InsertPictureData failed, pictureDataOperations_ is null");
141     pictureDataOperations_->InsertPictureData(imageId, PicturePair, pictureType);
142 }
143 
DeleteDataWithImageId(const std::string & imageId,PictureType pictureType)144 void PictureManagerThread::DeleteDataWithImageId(const std::string& imageId, PictureType pictureType)
145 {
146     CHECK_AND_RETURN_LOG(pictureDataOperations_ != nullptr,
147         "InsertPictureData failed, pictureDataOperations_ is null");
148     pictureDataOperations_->DeleteDataWithImageId(imageId, pictureType);
149 }
150 
GetDataWithImageId(const std::string & imageId,bool & isHighQualityPicture,bool & isTakeEffect,bool isCleanImmediately)151 std::shared_ptr<Media::Picture> PictureManagerThread::GetDataWithImageId(const std::string& imageId,
152     bool &isHighQualityPicture, bool &isTakeEffect, bool isCleanImmediately)
153 {
154     MEDIA_DEBUG_LOG("enter ");
155     CHECK_AND_RETURN_RET_LOG(pictureDataOperations_ != nullptr, nullptr,
156         "GetDataWithImageId failed, pictureDataOperations_ is null");
157     return pictureDataOperations_->GetDataWithImageId(imageId, isHighQualityPicture, isTakeEffect, isCleanImmediately);
158 }
159 
SavePictureWithImageId(const std::string & imageId)160 void PictureManagerThread::SavePictureWithImageId(const std::string& imageId)
161 {
162     CHECK_AND_RETURN_LOG(pictureDataOperations_ != nullptr,
163         "SavePictureWithImageId failed, pictureDataOperations_ is null");
164     return pictureDataOperations_->SavePictureWithImageId(imageId);
165 }
166 
AddSavePictureTask(sptr<PicturePair> & picturePair)167 int32_t PictureManagerThread::AddSavePictureTask(sptr<PicturePair>& picturePair)
168 {
169     CHECK_AND_RETURN_RET_LOG(pictureDataOperations_ != nullptr, 0,
170         "AddSavePictureTask failed, pictureDataOperations_ is null");
171     pictureDataOperations_->AddSavePictureTask(picturePair);
172     return 0;
173 }
174 
GetPendingTaskSize()175 int32_t PictureManagerThread::GetPendingTaskSize()
176 {
177     CHECK_AND_RETURN_RET_LOG(pictureDataOperations_ != nullptr, 0,
178         "GetPendingTaskSize failed, pictureDataOperations_ is null");
179     return pictureDataOperations_->GetPendingTaskSize();
180 }
181 
IsExsitDataForPictureType(PictureType pictureType)182 bool PictureManagerThread::IsExsitDataForPictureType(PictureType pictureType)
183 {
184     MEDIA_INFO_LOG("enter ");
185     CHECK_AND_RETURN_RET_LOG(pictureDataOperations_ != nullptr, false,
186         "IsExsitDataForPictureType failed, pictureDataOperations_ is null");
187     return pictureDataOperations_->IsExsitDataForPictureType(pictureType);
188 }
189 
IsExsitPictureByImageId(const std::string & imageId)190 bool PictureManagerThread::IsExsitPictureByImageId(const std::string& imageId)
191 {
192     MEDIA_INFO_LOG("enter ");
193     CHECK_AND_RETURN_RET_LOG(pictureDataOperations_ != nullptr, false,
194         "IsExsitDataForPictureType failed, pictureDataOperations_ is null");
195 
196     enum PictureType pictureType;
197     for (pictureType = HIGH_QUALITY_PICTURE; pictureType >= LOW_QUALITY_PICTURE;
198         pictureType = (PictureType)(pictureType - 1)) {
199         CHECK_AND_RETURN_RET(!pictureDataOperations_->IsExsitDataForPictureType(imageId, pictureType), true);
200     }
201     return false;
202 }
203 
204 // 落盘低质量图,包括低质量裸图/低质量
SaveLowQualityPicture(const std::string & imageId)205 void PictureManagerThread::SaveLowQualityPicture(const std::string& imageId)
206 {
207     MEDIA_INFO_LOG("enter ");
208     CHECK_AND_RETURN_LOG(pictureDataOperations_ != nullptr,
209         "SaveLowQualityPicture failed, pictureDataOperations_ is null");
210     pictureDataOperations_->SaveLowQualityPicture(imageId);
211 }
212 
FinishAccessingPicture(const std::string & imageId)213 void PictureManagerThread::FinishAccessingPicture(const std::string& imageId)
214 {
215     MEDIA_INFO_LOG("enter ");
216     CHECK_AND_RETURN_LOG(pictureDataOperations_ != nullptr,
217         "FinishAccessingPicture failed, pictureDataOperations_ is null");
218 
219     enum PictureType pictureType;
220     for (pictureType = HIGH_QUALITY_PICTURE; pictureType >= LOW_QUALITY_PICTURE;
221         pictureType = (PictureType)(pictureType - 1)) {
222         pictureDataOperations_->FinishAccessingPicture(imageId, pictureType);
223     }
224     MEDIA_INFO_LOG("FinishAccessingPicture end: %{public}s", imageId.c_str());
225 }
226 } // namespace Media
227 } // namespace OHOS