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     pictureDataOperations_ = nullptr;
50     Stop();
51 }
52 
State() const53 State PictureManagerThread::State() const
54 {
55     return state_;
56 }
57 
Start()58 void PictureManagerThread::Start()
59 {
60     unique_lock<mutex> locker(runningMutex_);
61     if (pictureDataOperations_ == nullptr) {
62         pictureDataOperations_ = new PictureDataOperations();
63     }
64     if (pauseFlag_) {
65         Stop();
66     }
67     if (thread_ == nullptr) {
68         thread_ = std::make_unique<std::thread>(&PictureManagerThread::Run, this);
69         pauseFlag_ = false;
70         stopFlag_ = false;
71         state_ = State::RUNNING;
72     }
73 }
74 
Stop()75 void PictureManagerThread::Stop()
76 {
77     MEDIA_INFO_LOG("enter ");
78     if (thread_ != nullptr) {
79         pauseFlag_ = false;
80         stopFlag_ = true;
81         condition_.notify_all();  // Notify one waiting thread, if there is one.
82         if (thread_->joinable()) {
83             thread_->join(); // wait for thread finished
84         }
85         thread_ = nullptr;
86         state_ = State::STOPPED;
87     }
88 }
89 
Pause()90 void PictureManagerThread::Pause()
91 {
92     MEDIA_INFO_LOG("enter ");
93     if (thread_ != nullptr) {
94         pauseFlag_ = true;
95         state_ = State::PAUSED;
96     }
97 }
98 
Resume()99 void PictureManagerThread::Resume()
100 {
101     MEDIA_INFO_LOG("enter ");
102     if (thread_ != nullptr) {
103         pauseFlag_ = false;
104         condition_.notify_all();
105         state_ = State::RUNNING;
106     }
107 }
108 
Run()109 void PictureManagerThread::Run()
110 {
111     MEDIA_INFO_LOG("enter thread run:");
112     string name("PictureManagerThread");
113     pthread_setname_np(pthread_self(), name.c_str());
114     while (!stopFlag_) {
115         if (pictureDataOperations_ == nullptr) {
116             pictureDataOperations_ = new PictureDataOperations();
117         }
118         pictureDataOperations_->CleanDateForPeriodical();
119         unique_lock<mutex> locker(threadMutex_);
120         condition_.wait_for(locker, std::chrono::seconds(1)); // 实际1S扫描一次
121         int32_t taskSize = pictureDataOperations_->GetPendingTaskSize();
122         if (lastPendingTaskSize_ != 0 && taskSize == 0) {
123             pauseFlag_ = true;
124             return;
125         }
126         lastPendingTaskSize_ = taskSize;
127     }
128 }
129 
InsertPictureData(const std::string & imageId,sptr<PicturePair> & PicturePair,PictureType pictureType)130 void PictureManagerThread::InsertPictureData(const std::string& imageId, sptr<PicturePair>& PicturePair,
131     PictureType pictureType)
132 {
133     Start();
134     if (pictureDataOperations_ == nullptr) {
135         MEDIA_ERR_LOG("InsertPictureData failed, pictureDataOperations_ is null");
136         return;
137     }
138     pictureDataOperations_->InsertPictureData(imageId, PicturePair, pictureType);
139 }
140 
DeleteDataWithImageId(const std::string & imageId,PictureType pictureType)141 void PictureManagerThread::DeleteDataWithImageId(const std::string& imageId, PictureType pictureType)
142 {
143     if (pictureDataOperations_ == nullptr) {
144         MEDIA_ERR_LOG("InsertPictureData failed, pictureDataOperations_ is null");
145         return;
146     }
147     pictureDataOperations_->DeleteDataWithImageId(imageId, pictureType);
148 }
149 
GetDataWithImageId(const std::string & imageId,bool & isHighQualityPicture,bool isCleanImmediately)150 std::shared_ptr<Media::Picture> PictureManagerThread::GetDataWithImageId(const std::string& imageId,
151     bool &isHighQualityPicture, bool isCleanImmediately)
152 {
153     MEDIA_DEBUG_LOG("enter ");
154     if (pictureDataOperations_ == nullptr) {
155         MEDIA_ERR_LOG("GetDataWithImageId failed, pictureDataOperations_ is null");
156         return nullptr;
157     }
158     return pictureDataOperations_->GetDataWithImageId(imageId, isHighQualityPicture, isCleanImmediately);
159 }
160 
SavePictureWithImageId(const std::string & imageId)161 void PictureManagerThread::SavePictureWithImageId(const std::string& imageId)
162 {
163     if (pictureDataOperations_ == nullptr) {
164         MEDIA_ERR_LOG("SavePictureWithImageId failed, pictureDataOperations_ is null");
165         return;
166     }
167     return pictureDataOperations_->SavePictureWithImageId(imageId);
168 }
169 
AddSavePictureTask(sptr<PicturePair> & picturePair)170 int32_t PictureManagerThread::AddSavePictureTask(sptr<PicturePair>& picturePair)
171 {
172     if (pictureDataOperations_ == nullptr) {
173         MEDIA_ERR_LOG("AddSavePictureTask failed, pictureDataOperations_ is null");
174         return 0;
175     }
176     pictureDataOperations_->AddSavePictureTask(picturePair);
177     return 0;
178 }
179 
GetPendingTaskSize()180 int32_t PictureManagerThread::GetPendingTaskSize()
181 {
182     if (pictureDataOperations_ == nullptr) {
183         MEDIA_ERR_LOG("GetPendingTaskSize failed, pictureDataOperations_ is null");
184         return 0;
185     }
186     return pictureDataOperations_->GetPendingTaskSize();
187 }
188 
IsExsitDataForPictureType(PictureType pictureType)189 bool PictureManagerThread::IsExsitDataForPictureType(PictureType pictureType)
190 {
191     MEDIA_INFO_LOG("enter ");
192     if (pictureDataOperations_ == nullptr) {
193         MEDIA_ERR_LOG("IsExsitDataForPictureType failed, pictureDataOperations_ is null");
194         return false;
195     }
196     return pictureDataOperations_->IsExsitDataForPictureType(pictureType);
197 }
198 
IsExsitPictureByImageId(const std::string & imageId)199 bool PictureManagerThread::IsExsitPictureByImageId(const std::string& imageId)
200 {
201     MEDIA_INFO_LOG("enter ");
202     if (pictureDataOperations_ == nullptr) {
203         MEDIA_ERR_LOG("IsExsitDataForPictureType failed, pictureDataOperations_ is null");
204         return false;
205     }
206     enum PictureType pictureType;
207     for (pictureType = HIGH_QUALITY_PICTURE; pictureType >= LOW_QUALITY_PICTURE;
208         pictureType = (PictureType)(pictureType - 1)) {
209         if (pictureDataOperations_->IsExsitDataForPictureType(imageId, pictureType)) {
210             return true;
211         }
212     }
213     return false;
214 }
215 
216 // 落盘低质量图,包括低质量裸图/低质量
SaveLowQualityPicture(const std::string & imageId)217 void PictureManagerThread::SaveLowQualityPicture(const std::string& imageId)
218 {
219     MEDIA_INFO_LOG("enter ");
220     if (pictureDataOperations_ == nullptr) {
221         MEDIA_ERR_LOG("SaveLowQualityPicture failed, pictureDataOperations_ is null");
222         return;
223     }
224     pictureDataOperations_->SaveLowQualityPicture(imageId);
225 }
226 
FinishAccessingPicture(const std::string & imageId)227 void PictureManagerThread::FinishAccessingPicture(const std::string& imageId)
228 {
229     MEDIA_INFO_LOG("enter ");
230     if (pictureDataOperations_ == nullptr) {
231         MEDIA_ERR_LOG("FinishAccessingPicture failed, pictureDataOperations_ is null");
232         return;
233     }
234     enum PictureType pictureType;
235     for (pictureType = HIGH_QUALITY_PICTURE; pictureType >= LOW_QUALITY_PICTURE;
236         pictureType = (PictureType)(pictureType - 1)) {
237         pictureDataOperations_->FinishAccessingPicture(imageId, pictureType);
238     }
239     MEDIA_INFO_LOG("FinishAccessingPicture end: %{public}s", imageId.c_str());
240 }
241 } // namespace Media
242 } // namespace OHOS