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
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVMetadataHelperServer"};
24 }
25
26 namespace OHOS {
27 namespace Media {
Create()28 std::shared_ptr<IAVMetadataHelperService> AVMetadataHelperServer::Create()
29 {
30 std::shared_ptr<AVMetadataHelperServer> server = std::make_shared<AVMetadataHelperServer>();
31 CHECK_AND_RETURN_RET_LOG(server != nullptr, nullptr, "Failed to new AVMetadataHelperServer");
32 return server;
33 }
34
AVMetadataHelperServer()35 AVMetadataHelperServer::AVMetadataHelperServer()
36 {
37 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
38 }
39
~AVMetadataHelperServer()40 AVMetadataHelperServer::~AVMetadataHelperServer()
41 {
42 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
43 std::lock_guard<std::mutex> lock(mutex_);
44 avMetadataHelperEngine_ = nullptr;
45 uriHelper_ = nullptr;
46 }
47
SetSource(const std::string & uri,int32_t usage)48 int32_t AVMetadataHelperServer::SetSource(const std::string &uri, int32_t usage)
49 {
50 std::lock_guard<std::mutex> lock(mutex_);
51 MEDIA_LOGD("Current uri is : %{public}s %{public}u", uri.c_str(), usage);
52
53 uriHelper_ = std::make_unique<UriHelper>(uri);
54 if (!uriHelper_->AccessCheck(UriHelper::URI_READ)) {
55 MEDIA_LOGE("Failed to read the file");
56 return MSERR_INVALID_VAL;
57 }
58
59 auto engineFactory = EngineFactoryRepo::Instance().GetEngineFactory(
60 IEngineFactory::Scene::SCENE_AVMETADATA, uriHelper_->FormattedUri());
61 CHECK_AND_RETURN_RET_LOG(engineFactory != nullptr, MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
62 "Failed to get engine factory");
63 avMetadataHelperEngine_ = engineFactory->CreateAVMetadataHelperEngine();
64 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
65 "Failed to create avmetadatahelper engine");
66
67 int32_t ret = avMetadataHelperEngine_->SetSource(uriHelper_->FormattedUri(), usage);
68 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetSource failed!");
69
70 return MSERR_OK;
71 }
72
SetSource(int32_t fd,int64_t offset,int64_t size,int32_t usage)73 int32_t AVMetadataHelperServer::SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage)
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 MEDIA_LOGD("Current is fd source, offset: %{public}" PRIi64 ", size: %{public}" PRIi64 " usage: %{public}u",
77 offset, size, usage);
78
79 uriHelper_ = std::make_unique<UriHelper>(fd, offset, size);
80 CHECK_AND_RETURN_RET_LOG(uriHelper_->AccessCheck(UriHelper::URI_READ), MSERR_INVALID_VAL, "Failed to read the fd");
81
82 auto engineFactory = EngineFactoryRepo::Instance().GetEngineFactory(
83 IEngineFactory::Scene::SCENE_AVMETADATA, uriHelper_->FormattedUri());
84 CHECK_AND_RETURN_RET_LOG(engineFactory != nullptr, MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
85 "Failed to get engine factory");
86 avMetadataHelperEngine_ = engineFactory->CreateAVMetadataHelperEngine();
87 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED,
88 "Failed to create avmetadatahelper engine");
89
90 int32_t ret = avMetadataHelperEngine_->SetSource(uriHelper_->FormattedUri(), usage);
91 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetSource failed!");
92
93 return MSERR_OK;
94 }
95
ResolveMetadata(int32_t key)96 std::string AVMetadataHelperServer::ResolveMetadata(int32_t key)
97 {
98 std::lock_guard<std::mutex> lock(mutex_);
99 MEDIA_LOGD("Key is %{public}d", key);
100 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, "", "avMetadataHelperEngine_ is nullptr");
101 return avMetadataHelperEngine_->ResolveMetadata(key);
102 }
103
ResolveMetadata()104 std::unordered_map<int32_t, std::string> AVMetadataHelperServer::ResolveMetadata()
105 {
106 std::lock_guard<std::mutex> lock(mutex_);
107 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, {}, "avMetadataHelperEngine_ is nullptr");
108 return avMetadataHelperEngine_->ResolveMetadata();
109 }
110
FetchArtPicture()111 std::shared_ptr<AVSharedMemory> AVMetadataHelperServer::FetchArtPicture()
112 {
113 std::lock_guard<std::mutex> lock(mutex_);
114 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, {}, "avMetadataHelperEngine_ is nullptr");
115 return avMetadataHelperEngine_->FetchArtPicture();
116 }
117
FetchFrameAtTime(int64_t timeUs,int32_t option,const OutputConfiguration & param)118 std::shared_ptr<AVSharedMemory> AVMetadataHelperServer::FetchFrameAtTime(int64_t timeUs, int32_t option,
119 const OutputConfiguration ¶m)
120 {
121 std::lock_guard<std::mutex> lock(mutex_);
122 CHECK_AND_RETURN_RET_LOG(avMetadataHelperEngine_ != nullptr, nullptr, "avMetadataHelperEngine_ is nullptr");
123 return avMetadataHelperEngine_->FetchFrameAtTime(timeUs, option, param);
124 }
125
Release()126 void AVMetadataHelperServer::Release()
127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129 avMetadataHelperEngine_ = nullptr;
130 uriHelper_ = nullptr;
131 }
132 } // namespace Media
133 } // namespace OHOS