• 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_service_stub.h"
17 #include "media_server_manager.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "avsharedmemory_ipc.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVMetadataHelperServiceStub"};
24 }
25 
26 namespace OHOS {
27 namespace Media {
Create()28 sptr<AVMetadataHelperServiceStub> AVMetadataHelperServiceStub::Create()
29 {
30     sptr<AVMetadataHelperServiceStub> avMetadataHelperStub = new(std::nothrow) AVMetadataHelperServiceStub();
31     CHECK_AND_RETURN_RET_LOG(avMetadataHelperStub != nullptr, nullptr, "failed to new AVMetadataHelperServiceStub");
32 
33     int32_t ret = avMetadataHelperStub->Init();
34     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to avmetadatahlper stub init");
35     return avMetadataHelperStub;
36 }
37 
AVMetadataHelperServiceStub()38 AVMetadataHelperServiceStub::AVMetadataHelperServiceStub()
39 {
40     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
41 }
42 
~AVMetadataHelperServiceStub()43 AVMetadataHelperServiceStub::~AVMetadataHelperServiceStub()
44 {
45     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
46 }
47 
Init()48 int32_t AVMetadataHelperServiceStub::Init()
49 {
50     std::unique_lock<std::mutex> lock(mutex_);
51     avMetadateHelperServer_ = AVMetadataHelperServer::Create();
52     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY,
53         "failed to create AVMetadataHelper Service");
54 
55     avMetadataHelperFuncs_[SET_URI_SOURCE] = &AVMetadataHelperServiceStub::SetUriSource;
56     avMetadataHelperFuncs_[SET_FD_SOURCE] = &AVMetadataHelperServiceStub::SetFdSource;
57     avMetadataHelperFuncs_[RESOLVE_METADATA] = &AVMetadataHelperServiceStub::ResolveMetadata;
58     avMetadataHelperFuncs_[RESOLVE_METADATA_MAP] = &AVMetadataHelperServiceStub::ResolveMetadataMap;
59     avMetadataHelperFuncs_[FETCH_ART_PICTURE] = &AVMetadataHelperServiceStub::FetchArtPicture;
60     avMetadataHelperFuncs_[FETCH_FRAME_AT_TIME] = &AVMetadataHelperServiceStub::FetchFrameAtTime;
61     avMetadataHelperFuncs_[RELEASE] = &AVMetadataHelperServiceStub::Release;
62     avMetadataHelperFuncs_[DESTROY] = &AVMetadataHelperServiceStub::DestroyStub;
63     return MSERR_OK;
64 }
65 
DestroyStub()66 int32_t AVMetadataHelperServiceStub::DestroyStub()
67 {
68     std::unique_lock<std::mutex> lock(mutex_);
69     avMetadateHelperServer_ = nullptr;
70     MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::AVMETADATAHELPER, AsObject());
71     return MSERR_OK;
72 }
73 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)74 int AVMetadataHelperServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
75     MessageOption &option)
76 {
77     MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}u is received", code);
78 
79     auto remoteDescriptor = data.ReadInterfaceToken();
80     if (AVMetadataHelperServiceStub::GetDescriptor() != remoteDescriptor) {
81         MEDIA_LOGE("Invalid descriptor");
82         return MSERR_INVALID_OPERATION;
83     }
84 
85     auto itFunc = avMetadataHelperFuncs_.find(code);
86     if (itFunc != avMetadataHelperFuncs_.end()) {
87         auto memberFunc = itFunc->second;
88         if (memberFunc != nullptr) {
89             int32_t ret = (this->*memberFunc)(data, reply);
90             if (ret != MSERR_OK) {
91                 MEDIA_LOGE("Calling memberFunc is failed.");
92             }
93             return MSERR_OK;
94         }
95     }
96     MEDIA_LOGW("AVMetadataHelperServiceStub: no member func supporting, applying default process");
97 
98     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
99 }
100 
SetSource(const std::string & uri,int32_t usage)101 int32_t AVMetadataHelperServiceStub::SetSource(const std::string &uri, int32_t usage)
102 {
103     std::unique_lock<std::mutex> lock(mutex_);
104     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "avmetadatahelper server is nullptr");
105     return avMetadateHelperServer_->SetSource(uri, usage);
106 }
107 
SetSource(int32_t fd,int64_t offset,int64_t size,int32_t usage)108 int32_t AVMetadataHelperServiceStub::SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage)
109 {
110     std::unique_lock<std::mutex> lock(mutex_);
111     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "avmetadatahelper server is nullptr");
112     return avMetadateHelperServer_->SetSource(fd, offset, size, usage);
113 }
114 
ResolveMetadata(int32_t key)115 std::string AVMetadataHelperServiceStub::ResolveMetadata(int32_t key)
116 {
117     std::unique_lock<std::mutex> lock(mutex_);
118     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, "", "avmetadatahelper server is nullptr");
119     return avMetadateHelperServer_->ResolveMetadata(key);
120 }
121 
ResolveMetadataMap()122 std::unordered_map<int32_t, std::string> AVMetadataHelperServiceStub::ResolveMetadataMap()
123 {
124     std::unique_lock<std::mutex> lock(mutex_);
125     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, {}, "avmetadatahelper server is nullptr");
126     return avMetadateHelperServer_->ResolveMetadata();
127 }
128 
FetchArtPicture()129 std::shared_ptr<AVSharedMemory> AVMetadataHelperServiceStub::FetchArtPicture()
130 {
131     std::unique_lock<std::mutex> lock(mutex_);
132     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
133     return avMetadateHelperServer_->FetchArtPicture();
134 }
135 
FetchFrameAtTime(int64_t timeUs,int32_t option,const OutputConfiguration & param)136 std::shared_ptr<AVSharedMemory> AVMetadataHelperServiceStub::FetchFrameAtTime(int64_t timeUs,
137     int32_t option, const OutputConfiguration &param)
138 {
139     std::unique_lock<std::mutex> lock(mutex_);
140     CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
141     return avMetadateHelperServer_->FetchFrameAtTime(timeUs, option, param);
142 }
143 
Release()144 void AVMetadataHelperServiceStub::Release()
145 {
146     std::unique_lock<std::mutex> lock(mutex_);
147     CHECK_AND_RETURN_LOG(avMetadateHelperServer_ != nullptr, "avmetadatahelper server is nullptr");
148     return avMetadateHelperServer_->Release();
149 }
150 
SetUriSource(MessageParcel & data,MessageParcel & reply)151 int32_t AVMetadataHelperServiceStub::SetUriSource(MessageParcel &data, MessageParcel &reply)
152 {
153     std::string uri = data.ReadString();
154     int32_t usage = data.ReadInt32();
155     reply.WriteInt32(SetSource(uri, usage));
156     return MSERR_OK;
157 }
158 
SetFdSource(MessageParcel & data,MessageParcel & reply)159 int32_t AVMetadataHelperServiceStub::SetFdSource(MessageParcel &data, MessageParcel &reply)
160 {
161     int32_t fd = data.ReadFileDescriptor();
162     int64_t offset = data.ReadInt64();
163     int64_t size = data.ReadInt64();
164     int32_t usage = data.ReadInt32();
165     reply.WriteInt32(SetSource(fd, offset, size, usage));
166     (void)::close(fd);
167     return MSERR_OK;
168 }
169 
ResolveMetadata(MessageParcel & data,MessageParcel & reply)170 int32_t AVMetadataHelperServiceStub::ResolveMetadata(MessageParcel &data, MessageParcel &reply)
171 {
172     int32_t key = data.ReadInt32();
173     reply.WriteString(ResolveMetadata(key));
174     return MSERR_OK;
175 }
176 
ResolveMetadataMap(MessageParcel & data,MessageParcel & reply)177 int32_t AVMetadataHelperServiceStub::ResolveMetadataMap(MessageParcel &data, MessageParcel &reply)
178 {
179     (void)data;
180     std::unordered_map<int32_t, std::string> metadata = ResolveMetadataMap();
181     CHECK_AND_RETURN_RET_LOG(metadata.size() != 0, MSERR_INVALID_VAL, "No metadata");
182 
183     std::vector<int32_t> key;
184     std::vector<std::string> dataStr;
185     for (auto it = metadata.begin(); it != metadata.end(); it++) {
186         key.push_back(it->first);
187         dataStr.push_back(it->second);
188     }
189 
190     reply.WriteInt32Vector(key);
191     reply.WriteStringVector(dataStr);
192     return MSERR_OK;
193 }
194 
FetchArtPicture(MessageParcel & data,MessageParcel & reply)195 int32_t AVMetadataHelperServiceStub::FetchArtPicture(MessageParcel &data, MessageParcel &reply)
196 {
197     (void)data;
198     auto result = FetchArtPicture();
199     if (result == nullptr) {
200         return MSERR_INVALID_OPERATION;
201     }
202 
203     return WriteAVSharedMemoryToParcel(result, reply);
204 }
205 
FetchFrameAtTime(MessageParcel & data,MessageParcel & reply)206 int32_t AVMetadataHelperServiceStub::FetchFrameAtTime(MessageParcel &data, MessageParcel &reply)
207 {
208     int64_t timeUs = data.ReadInt64();
209     int32_t option = data.ReadInt32();
210     OutputConfiguration param = {data.ReadInt32(), data.ReadInt32(), static_cast<PixelFormat>(data.ReadInt32())};
211     std::shared_ptr<AVSharedMemory> ashMem = FetchFrameAtTime(timeUs, option, param);
212 
213     return WriteAVSharedMemoryToParcel(ashMem, reply);
214 }
215 
Release(MessageParcel & data,MessageParcel & reply)216 int32_t AVMetadataHelperServiceStub::Release(MessageParcel &data, MessageParcel &reply)
217 {
218     (void)data;
219     (void)reply;
220     Release();
221     return MSERR_OK;
222 }
223 
DestroyStub(MessageParcel & data,MessageParcel & reply)224 int32_t AVMetadataHelperServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
225 {
226     (void)data;
227     reply.WriteInt32(DestroyStub());
228     return MSERR_OK;
229 }
230 } // namespace Media
231 } // namespace OHOS
232