• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "avmetadatahelper_server.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "engine_factory_repo.h"
20 #include "uri_helper.h"
21 #include "media_dfx.h"
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVMetadataHelperServer"};
25 }
26 
27 namespace OHOS {
28 namespace Media {
Create()29 std::shared_ptr<IAVMetadataHelperService> AVMetadataHelperServer::Create()
30 {
31     std::shared_ptr<AVMetadataHelperServer> server = std::make_shared<AVMetadataHelperServer>();
32     CHECK_AND_RETURN_RET_LOG(server != nullptr, nullptr, "Failed to new AVMetadataHelperServer");
33     return server;
34 }
35 
AVMetadataHelperServer()36 AVMetadataHelperServer::AVMetadataHelperServer()
37     : taskQue_("AVMetadata")
38 {
39     (void)taskQue_.Start();
40     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
41 }
42 
~AVMetadataHelperServer()43 AVMetadataHelperServer::~AVMetadataHelperServer()
44 {
45     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
46     std::lock_guard<std::mutex> lock(mutex_);
47     auto task = std::make_shared<TaskHandler<void>>([&, this] {
48         avMetadataHelperEngine_ = nullptr;
49     });
50     (void)taskQue_.EnqueueTask(task);
51     (void)task->GetResult();
52     uriHelper_ = nullptr;
53     taskQue_.Stop();
54 }
55 
SetSource(const std::string & uri,int32_t usage)56 int32_t AVMetadataHelperServer::SetSource(const std::string &uri, int32_t usage)
57 {
58     std::lock_guard<std::mutex> lock(mutex_);
59     MediaTrace trace("AVMetadataHelperServer::SetSource_uri");
60     MEDIA_LOGD("Current uri is : %{public}s %{public}u", uri.c_str(), usage);
61 
62     uriHelper_ = std::make_unique<UriHelper>(uri);
63     if (!uriHelper_->AccessCheck(UriHelper::URI_READ)) {
64         MEDIA_LOGE("Failed to read the file");
65         return MSERR_INVALID_VAL;
66     }
67     auto task = std::make_shared<TaskHandler<int32_t>>([&, this] {
68         auto engineFactory = EngineFactoryRepo::Instance().GetEngineFactory(
69             IEngineFactory::Scene::SCENE_AVMETADATA, uriHelper_->FormattedUri());
70         CHECK_AND_RETURN_RET_LOG(engineFactory != nullptr, (int32_t)MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
71             "Failed to get engine factory");
72         avMetadataHelperEngine_ = engineFactory->CreateAVMetadataHelperEngine();
73         CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr,
74             (int32_t)MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED, "Failed to create avmetadatahelper engine");
75         int32_t ret = avMetadataHelperEngine_->SetSource(uriHelper_->FormattedUri(), usage);
76         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetSource failed!");
77         return ret;
78     });
79     int32_t ret = taskQue_.EnqueueTask(task);
80     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "EnqueueTask failed");
81 
82     auto result = task->GetResult();
83     return result.Value();
84 }
85 
SetSource(int32_t fd,int64_t offset,int64_t size,int32_t usage)86 int32_t AVMetadataHelperServer::SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage)
87 {
88     std::lock_guard<std::mutex> lock(mutex_);
89     MediaTrace trace("AVMetadataHelperServer::SetSource_fd");
90     MEDIA_LOGD("Current is fd source, offset: %{public}" PRIi64 ", size: %{public}" PRIi64 " usage: %{public}u",
91                offset, size, usage);
92 
93     uriHelper_ = std::make_unique<UriHelper>(fd, offset, size);
94     CHECK_AND_RETURN_RET_LOG(uriHelper_->AccessCheck(UriHelper::URI_READ), MSERR_INVALID_VAL, "Failed to read the fd");
95 
96     auto task = std::make_shared<TaskHandler<int32_t>>([&, this] {
97         auto engineFactory = EngineFactoryRepo::Instance().GetEngineFactory(
98             IEngineFactory::Scene::SCENE_AVMETADATA, uriHelper_->FormattedUri());
99         CHECK_AND_RETURN_RET_LOG(engineFactory != nullptr, (int32_t)MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
100             "Failed to get engine factory");
101         avMetadataHelperEngine_ = engineFactory->CreateAVMetadataHelperEngine();
102         CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr,
103             (int32_t)MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED, "Failed to create avmetadatahelper engine");
104 
105         int32_t ret = avMetadataHelperEngine_->SetSource(uriHelper_->FormattedUri(), usage);
106         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetSource failed!");
107         return ret;
108     });
109     int32_t ret = taskQue_.EnqueueTask(task);
110     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "EnqueueTask failed");
111 
112     auto result = task->GetResult();
113     return result.Value();
114 }
115 
ResolveMetadata(int32_t key)116 std::string AVMetadataHelperServer::ResolveMetadata(int32_t key)
117 {
118     std::lock_guard<std::mutex> lock(mutex_);
119     MediaTrace trace("AVMetadataHelperServer::ResolveMetadata_key");
120     MEDIA_LOGD("Key is %{public}d", key);
121     CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, "", "avMetadataHelperEngine_ is nullptr");
122     auto task = std::make_shared<TaskHandler<std::string>>([&, this] {
123         return avMetadataHelperEngine_->ResolveMetadata(key);
124     });
125     int32_t ret = taskQue_.EnqueueTask(task);
126     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, "", "EnqueueTask failed");
127 
128     auto result = task->GetResult();
129     return result.Value();
130 }
131 
ResolveMetadata()132 std::unordered_map<int32_t, std::string> AVMetadataHelperServer::ResolveMetadata()
133 {
134     std::lock_guard<std::mutex> lock(mutex_);
135     MediaTrace trace("AVMetadataHelperServer::ResolveMetadata");
136     CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, {}, "avMetadataHelperEngine_ is nullptr");
137     auto task = std::make_shared<TaskHandler<std::unordered_map<int32_t, std::string>>>([&, this] {
138         return avMetadataHelperEngine_->ResolveMetadata();
139     });
140     int32_t ret = taskQue_.EnqueueTask(task);
141     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, {}, "EnqueueTask failed");
142 
143     auto result = task->GetResult();
144     return result.Value();
145 }
146 
FetchArtPicture()147 std::shared_ptr<AVSharedMemory> AVMetadataHelperServer::FetchArtPicture()
148 {
149     std::lock_guard<std::mutex> lock(mutex_);
150     MediaTrace trace("AVMetadataHelperServer::FetchArtPicture");
151     CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, {}, "avMetadataHelperEngine_ is nullptr");
152     auto task = std::make_shared<TaskHandler<std::shared_ptr<AVSharedMemory>>>([&, this] {
153         return avMetadataHelperEngine_->FetchArtPicture();
154     });
155     int32_t ret = taskQue_.EnqueueTask(task);
156     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "EnqueueTask failed");
157 
158     auto result = task->GetResult();
159     return result.Value();
160 }
161 
FetchFrameAtTime(int64_t timeUs,int32_t option,const OutputConfiguration & param)162 std::shared_ptr<AVSharedMemory> AVMetadataHelperServer::FetchFrameAtTime(int64_t timeUs, int32_t option,
163     const OutputConfiguration &param)
164 {
165     std::lock_guard<std::mutex> lock(mutex_);
166     MediaTrace trace("AVMetadataHelperServer::FetchFrameAtTime");
167     CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, nullptr, "avMetadataHelperEngine_ is nullptr");
168     auto task = std::make_shared<TaskHandler<std::shared_ptr<AVSharedMemory>>>([&, this] {
169         return avMetadataHelperEngine_->FetchFrameAtTime(timeUs, option, param);
170     });
171     int32_t ret = taskQue_.EnqueueTask(task);
172     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "EnqueueTask failed");
173 
174     auto result = task->GetResult();
175     return result.Value();
176 }
177 
Release()178 void AVMetadataHelperServer::Release()
179 {
180     std::lock_guard<std::mutex> lock(mutex_);
181     MediaTrace trace("AVMetadataHelperServer::Release");
182     auto task = std::make_shared<TaskHandler<void>>([&, this] {
183         avMetadataHelperEngine_ = nullptr;
184     });
185     (void)taskQue_.EnqueueTask(task);
186     (void)task->GetResult();
187     uriHelper_ = nullptr;
188 }
189 } // namespace Media
190 } // namespace OHOS
191