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