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 "EnhancementTaskManager"
17 
18 #include "media_log.h"
19 #include "enhancement_task_manager.h"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace Media {
25 unordered_map<string, shared_ptr<EnhancementTaskInfo>>
26     EnhancementTaskManager::taskInProcess_ = {};
27 unordered_map<int32_t, string> EnhancementTaskManager::fileId2PhotoId_ = {};
28 mutex EnhancementTaskManager::mutex_;
29 
AddEnhancementTask(int32_t fileId,const string & photoId)30 void EnhancementTaskManager::AddEnhancementTask(int32_t fileId, const string &photoId)
31 {
32     unique_lock<mutex> lock(mutex_);
33     fileId2PhotoId_.emplace(fileId, photoId);
34     taskInProcess_.emplace(photoId, make_shared<EnhancementTaskInfo>(photoId, fileId, 0));
35 }
36 
RemoveEnhancementTask(const std::string & photoId)37 void EnhancementTaskManager::RemoveEnhancementTask(const std::string &photoId)
38 {
39     unique_lock<mutex> lock(mutex_);
40     if (taskInProcess_.find(photoId) == taskInProcess_.end()) {
41         return;
42     }
43     int32_t fileId = taskInProcess_[photoId]->fileId;
44     fileId2PhotoId_.erase(fileId);
45     taskInProcess_.erase(photoId);
46 }
47 
RemoveAllEnhancementTask(vector<string> & taskIds)48 void EnhancementTaskManager::RemoveAllEnhancementTask(vector<string> &taskIds)
49 {
50     unique_lock<mutex> lock(mutex_);
51     transform(taskInProcess_.begin(), taskInProcess_.end(), back_inserter(taskIds),
52         [](const auto& pair) { return pair.first; });
53     fileId2PhotoId_.clear();
54     taskInProcess_.clear();
55 }
56 
InProcessingTask(const string & photoId)57 bool EnhancementTaskManager::InProcessingTask(const string &photoId)
58 {
59     unique_lock<mutex> lock(mutex_);
60     if (photoId.empty() || taskInProcess_.find(photoId) == taskInProcess_.end()) {
61         return false;
62     }
63     return true;
64 }
65 
QueryPhotoIdByFileId(int32_t fileId)66 string EnhancementTaskManager::QueryPhotoIdByFileId(int32_t fileId)
67 {
68     unique_lock<mutex> lock(mutex_);
69     if (fileId2PhotoId_.find(fileId) != fileId2PhotoId_.end()) {
70         return fileId2PhotoId_[fileId];
71     }
72     return "";
73 }
74 
SetTaskRequestCount(const string & photoId,int32_t count)75 void EnhancementTaskManager::SetTaskRequestCount(const string &photoId, int32_t count)
76 {
77     unique_lock<mutex> lock(mutex_);
78     if (taskInProcess_.find(photoId) != taskInProcess_.end()) {
79         taskInProcess_[photoId]->requestCount = count;
80     }
81 }
82 
GetTaskRequestCount(const string & photoId)83 int32_t EnhancementTaskManager::GetTaskRequestCount(const string &photoId)
84 {
85     unique_lock<mutex> lock(mutex_);
86     if (taskInProcess_.find(photoId) != taskInProcess_.end()) {
87         return taskInProcess_[photoId]->requestCount;
88     }
89     return -1;
90 }
91 } // namespace Media
92 } // namespace OHOS
93