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 #include "media_data_source_proxy.h"
22 #include "media_dfx.h"
23 #include "surface_buffer.h"
24
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_METADATA, "AVMetadataHelperServiceStub"};
27 }
28
29 namespace OHOS {
30 namespace Media {
Create()31 sptr<AVMetadataHelperServiceStub> AVMetadataHelperServiceStub::Create()
32 {
33 sptr<AVMetadataHelperServiceStub> avMetadataHelperStub = new(std::nothrow) AVMetadataHelperServiceStub();
34 CHECK_AND_RETURN_RET_LOG(avMetadataHelperStub != nullptr, nullptr, "failed to new AVMetadataHelperServiceStub");
35
36 int32_t ret = avMetadataHelperStub->Init();
37 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to avmetadatahlper stub init");
38 StatisticEventWriteBundleName("create", "AVMetadataHelperServiceStub");
39 return avMetadataHelperStub;
40 }
41
AVMetadataHelperServiceStub()42 AVMetadataHelperServiceStub::AVMetadataHelperServiceStub()
43 {
44 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
45 }
46
~AVMetadataHelperServiceStub()47 AVMetadataHelperServiceStub::~AVMetadataHelperServiceStub()
48 {
49 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
50 }
51
Init()52 int32_t AVMetadataHelperServiceStub::Init()
53 {
54 std::unique_lock<std::mutex> lock(mutex_);
55 avMetadateHelperServer_ = AVMetadataHelperServer::Create();
56 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY,
57 "failed to create AVMetadataHelper Service");
58
59 avMetadataHelperFuncs_ = {
60 { SET_URI_SOURCE,
61 [this](MessageParcel &data, MessageParcel &reply) { return SetUriSource(data, reply); } },
62 { SET_FD_SOURCE,
63 [this](MessageParcel &data, MessageParcel &reply) { return SetFdSource(data, reply); } },
64 { SET_MEDIA_DATA_SRC_OBJ,
65 [this](MessageParcel &data, MessageParcel &reply) { return SetMediaDataSource(data, reply); } },
66 { RESOLVE_METADATA,
67 [this](MessageParcel &data, MessageParcel &reply) { return ResolveMetadata(data, reply); } },
68 { RESOLVE_METADATA_MAP,
69 [this](MessageParcel &data, MessageParcel &reply) { return ResolveMetadataMap(data, reply); } },
70 { FETCH_ALBUM_COVER,
71 [this](MessageParcel &data, MessageParcel &reply) { return FetchArtPicture(data, reply); } },
72 { FETCH_FRAME_AT_TIME,
73 [this](MessageParcel &data, MessageParcel &reply) { return FetchFrameAtTime(data, reply); } },
74 { FETCH_FRAME_YUV,
75 [this](MessageParcel &data, MessageParcel &reply) { return FetchFrameYuv(data, reply); } },
76 { RELEASE,
77 [this](MessageParcel &data, MessageParcel &reply) { return Release(data, reply); } },
78 { DESTROY,
79 [this](MessageParcel &data, MessageParcel &reply) { return DestroyStub(data, reply); } },
80 { SET_CALLBACK,
81 [this](MessageParcel &data, MessageParcel &reply) { return SetHelperCallback(data, reply); } },
82 { SET_LISTENER_OBJ,
83 [this](MessageParcel &data, MessageParcel &reply) { return SetListenerObject(data, reply); } },
84 { GET_AVMETADATA,
85 [this](MessageParcel &data, MessageParcel &reply) { return GetAVMetadata(data, reply); } },
86 { GET_TIME_BY_FRAME_INDEX,
87 [this](MessageParcel &data, MessageParcel &reply) { return GetTimeByFrameIndex(data, reply); } },
88 { GET_FRAME_INDEX_BY_TIME,
89 [this](MessageParcel &data, MessageParcel &reply) { return GetFrameIndexByTime(data, reply); } },
90 };
91 return MSERR_OK;
92 }
93
DestroyStub()94 int32_t AVMetadataHelperServiceStub::DestroyStub()
95 {
96 helperCallback_ = nullptr;
97 avMetadateHelperServer_ = nullptr;
98 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::AVMETADATAHELPER, AsObject());
99 return MSERR_OK;
100 }
101
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)102 int AVMetadataHelperServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
103 MessageOption &option)
104 {
105 MEDIA_LOGI("0x%{public}06" PRIXPTR " Stub: OnRemoteRequest of code: %{public}u is received",
106 FAKE_POINTER(this), code);
107
108 auto remoteDescriptor = data.ReadInterfaceToken();
109 if (AVMetadataHelperServiceStub::GetDescriptor() != remoteDescriptor) {
110 MEDIA_LOGE("Invalid descriptor");
111 return MSERR_INVALID_OPERATION;
112 }
113
114 auto itFunc = avMetadataHelperFuncs_.find(code);
115 if (itFunc != avMetadataHelperFuncs_.end()) {
116 auto memberFunc = itFunc->second;
117 if (memberFunc != nullptr) {
118 int32_t ret = memberFunc(data, reply);
119 if (ret != MSERR_OK) {
120 MEDIA_LOGE("Calling memberFunc is failed.");
121 }
122 return ret;
123 }
124 }
125 MEDIA_LOGW("AVMetadataHelperServiceStub: no member func supporting, applying default process");
126
127 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
128 }
129
SetSource(const std::string & uri,int32_t usage)130 int32_t AVMetadataHelperServiceStub::SetSource(const std::string &uri, int32_t usage)
131 {
132 std::unique_lock<std::mutex> lock(mutex_);
133 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "avmetadatahelper server is nullptr");
134 return avMetadateHelperServer_->SetSource(uri, usage);
135 }
136
SetSource(int32_t fd,int64_t offset,int64_t size,int32_t usage)137 int32_t AVMetadataHelperServiceStub::SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage)
138 {
139 std::unique_lock<std::mutex> lock(mutex_);
140 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "avmetadatahelper server is nullptr");
141 return avMetadateHelperServer_->SetSource(fd, offset, size, usage);
142 }
143
SetSource(const sptr<IRemoteObject> & object)144 int32_t AVMetadataHelperServiceStub::SetSource(const sptr<IRemoteObject> &object)
145 {
146 std::unique_lock<std::mutex> lock(mutex_);
147 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set mediadatasrc object is nullptr");
148 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "avmetadatahelper server is nullptr");
149
150 sptr<IStandardMediaDataSource> proxy = iface_cast<IStandardMediaDataSource>(object);
151 CHECK_AND_RETURN_RET_LOG(proxy != nullptr, MSERR_NO_MEMORY, "failed to convert MediaDataSourceProxy");
152
153 std::shared_ptr<IMediaDataSource> mediaDataSrc = std::make_shared<MediaDataCallback>(proxy);
154 CHECK_AND_RETURN_RET_LOG(mediaDataSrc != nullptr, MSERR_NO_MEMORY, "failed to new MetaListenerCallback");
155
156 return avMetadateHelperServer_->SetSource(mediaDataSrc);
157 }
158
ResolveMetadata(int32_t key)159 std::string AVMetadataHelperServiceStub::ResolveMetadata(int32_t key)
160 {
161 std::unique_lock<std::mutex> lock(mutex_);
162 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, "", "avmetadatahelper server is nullptr");
163 return avMetadateHelperServer_->ResolveMetadata(key);
164 }
165
ResolveMetadataMap()166 std::unordered_map<int32_t, std::string> AVMetadataHelperServiceStub::ResolveMetadataMap()
167 {
168 std::unique_lock<std::mutex> lock(mutex_);
169 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, {}, "avmetadatahelper server is nullptr");
170 return avMetadateHelperServer_->ResolveMetadata();
171 }
172
GetAVMetadata()173 std::shared_ptr<Meta> AVMetadataHelperServiceStub::GetAVMetadata()
174 {
175 std::unique_lock<std::mutex> lock(mutex_);
176 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
177 return avMetadateHelperServer_->GetAVMetadata();
178 }
179
FetchArtPicture()180 std::shared_ptr<AVSharedMemory> AVMetadataHelperServiceStub::FetchArtPicture()
181 {
182 std::unique_lock<std::mutex> lock(mutex_);
183 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
184 return avMetadateHelperServer_->FetchArtPicture();
185 }
186
FetchFrameAtTime(int64_t timeUs,int32_t option,const OutputConfiguration & param)187 std::shared_ptr<AVSharedMemory> AVMetadataHelperServiceStub::FetchFrameAtTime(int64_t timeUs,
188 int32_t option, const OutputConfiguration ¶m)
189 {
190 std::unique_lock<std::mutex> lock(mutex_);
191 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
192 return avMetadateHelperServer_->FetchFrameAtTime(timeUs, option, param);
193 }
194
FetchFrameYuv(int64_t timeUs,int32_t option,const OutputConfiguration & param)195 std::shared_ptr<AVBuffer> AVMetadataHelperServiceStub::FetchFrameYuv(int64_t timeUs,
196 int32_t option, const OutputConfiguration ¶m)
197 {
198 std::unique_lock<std::mutex> lock(mutex_);
199 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, nullptr, "avmetadatahelper server is nullptr");
200 return avMetadateHelperServer_->FetchFrameYuv(timeUs, option, param);
201 }
202
Release()203 void AVMetadataHelperServiceStub::Release()
204 {
205 CHECK_AND_RETURN_LOG(avMetadateHelperServer_ != nullptr, "avmetadatahelper server is nullptr");
206 return avMetadateHelperServer_->Release();
207 }
208
SetHelperCallback()209 int32_t AVMetadataHelperServiceStub::SetHelperCallback()
210 {
211 MEDIA_LOGD("SetHelperCallback");
212 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, MSERR_NO_MEMORY, "metadata server is nullptr");
213 return avMetadateHelperServer_->SetHelperCallback(helperCallback_);
214 }
215
SetListenerObject(const sptr<IRemoteObject> & object)216 int32_t AVMetadataHelperServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
217 {
218 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
219
220 sptr<IStandardHelperListener> listener = iface_cast<IStandardHelperListener>(object);
221 CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY, "failed to convert IStandardHelperListener");
222
223 std::shared_ptr<HelperCallback> callback = std::make_shared<HelperListenerCallback>(listener);
224 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "failed to new HelperListenerCallback");
225
226 helperCallback_ = callback;
227 return MSERR_OK;
228 }
229
GetTimeByFrameIndex(uint32_t index,uint64_t & time)230 int32_t AVMetadataHelperServiceStub::GetTimeByFrameIndex(uint32_t index, uint64_t &time)
231 {
232 MEDIA_LOGI("GetTimeByFrameIndex");
233 std::unique_lock<std::mutex> lock(mutex_);
234 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, 0, "avmetadatahelper server is nullptr");
235 return avMetadateHelperServer_->GetTimeByFrameIndex(index, time);
236 }
237
GetFrameIndexByTime(uint64_t time,uint32_t & index)238 int32_t AVMetadataHelperServiceStub::GetFrameIndexByTime(uint64_t time, uint32_t &index)
239 {
240 std::unique_lock<std::mutex> lock(mutex_);
241 CHECK_AND_RETURN_RET_LOG(avMetadateHelperServer_ != nullptr, 0, "avmetadatahelper server is nullptr");
242 return avMetadateHelperServer_->GetFrameIndexByTime(time, index);
243 }
244
SetUriSource(MessageParcel & data,MessageParcel & reply)245 int32_t AVMetadataHelperServiceStub::SetUriSource(MessageParcel &data, MessageParcel &reply)
246 {
247 std::string uri = data.ReadString();
248 int32_t usage = data.ReadInt32();
249 reply.WriteInt32(SetSource(uri, usage));
250 return MSERR_OK;
251 }
252
SetFdSource(MessageParcel & data,MessageParcel & reply)253 int32_t AVMetadataHelperServiceStub::SetFdSource(MessageParcel &data, MessageParcel &reply)
254 {
255 int32_t fd = data.ReadFileDescriptor();
256 int64_t offset = data.ReadInt64();
257 int64_t size = data.ReadInt64();
258 int32_t usage = data.ReadInt32();
259 reply.WriteInt32(SetSource(fd, offset, size, usage));
260 (void)::close(fd);
261 return MSERR_OK;
262 }
263
SetMediaDataSource(MessageParcel & data,MessageParcel & reply)264 int32_t AVMetadataHelperServiceStub::SetMediaDataSource(MessageParcel &data, MessageParcel &reply)
265 {
266 sptr<IRemoteObject> object = data.ReadRemoteObject();
267 reply.WriteInt32(SetSource(object));
268 return MSERR_OK;
269 }
270
ResolveMetadata(MessageParcel & data,MessageParcel & reply)271 int32_t AVMetadataHelperServiceStub::ResolveMetadata(MessageParcel &data, MessageParcel &reply)
272 {
273 int32_t key = data.ReadInt32();
274 reply.WriteString(ResolveMetadata(key));
275 return MSERR_OK;
276 }
277
ResolveMetadataMap(MessageParcel & data,MessageParcel & reply)278 int32_t AVMetadataHelperServiceStub::ResolveMetadataMap(MessageParcel &data, MessageParcel &reply)
279 {
280 (void)data;
281 std::unordered_map<int32_t, std::string> metadata = ResolveMetadataMap();
282 CHECK_AND_RETURN_RET_LOG(metadata.size() != 0, MSERR_INVALID_VAL, "No metadata");
283
284 std::vector<int32_t> key;
285 std::vector<std::string> dataStr;
286 for (auto it = metadata.begin(); it != metadata.end(); it++) {
287 key.push_back(it->first);
288 dataStr.push_back(it->second);
289 }
290
291 reply.WriteInt32Vector(key);
292 reply.WriteStringVector(dataStr);
293 return MSERR_OK;
294 }
295
GetAVMetadata(MessageParcel & data,MessageParcel & reply)296 int32_t AVMetadataHelperServiceStub::GetAVMetadata(MessageParcel &data, MessageParcel &reply)
297 {
298 (void)data;
299 bool ret = true;
300 std::shared_ptr<Meta> customInfo = std::make_shared<Meta>();
301 auto metadata = GetAVMetadata();
302 if (metadata == nullptr) {
303 MEDIA_LOGE("metadata is null");
304 metadata = std::make_shared<Meta>();
305 }
306
307 auto iter = metadata->Find("customInfo");
308 if (iter != metadata->end()) {
309 ret &= metadata->GetData("customInfo", customInfo);
310 ret &= reply.WriteString("customInfo");
311 ret &= customInfo->ToParcel(reply);
312 }
313 metadata->Remove("customInfo");
314 ret &= reply.WriteString("AVMetadata");
315 ret &= metadata->ToParcel(reply);
316 if (!ret) {
317 MEDIA_LOGE("GetAVMetadata ToParcel error");
318 }
319 return MSERR_OK;
320 }
321
FetchArtPicture(MessageParcel & data,MessageParcel & reply)322 int32_t AVMetadataHelperServiceStub::FetchArtPicture(MessageParcel &data, MessageParcel &reply)
323 {
324 (void)data;
325 auto result = FetchArtPicture();
326 if (result == nullptr) {
327 MEDIA_LOGE("result is null");
328 return MSERR_INVALID_OPERATION;
329 }
330
331 return WriteAVSharedMemoryToParcel(result, reply);
332 }
333
FetchFrameAtTime(MessageParcel & data,MessageParcel & reply)334 int32_t AVMetadataHelperServiceStub::FetchFrameAtTime(MessageParcel &data, MessageParcel &reply)
335 {
336 int64_t timeUs = data.ReadInt64();
337 int32_t option = data.ReadInt32();
338 OutputConfiguration param = {data.ReadInt32(), data.ReadInt32(), static_cast<PixelFormat>(data.ReadInt32())};
339 std::shared_ptr<AVSharedMemory> ashMem = FetchFrameAtTime(timeUs, option, param);
340
341 return WriteAVSharedMemoryToParcel(ashMem, reply);
342 }
343
FetchFrameYuv(MessageParcel & data,MessageParcel & reply)344 int32_t AVMetadataHelperServiceStub::FetchFrameYuv(MessageParcel &data, MessageParcel &reply)
345 {
346 int64_t timeUs = data.ReadInt64();
347 int32_t option = data.ReadInt32();
348 OutputConfiguration param = {data.ReadInt32(), data.ReadInt32(), static_cast<PixelFormat>(data.ReadInt32())};
349
350 std::shared_ptr<AVBuffer> avBuffer = FetchFrameYuv(timeUs, option, param);
351 reply.WriteInt32(avBuffer != nullptr ? MSERR_OK : MSERR_INVALID_VAL);
352 CHECK_AND_RETURN_RET(avBuffer != nullptr, MSERR_OK);
353 avBuffer->WriteToMessageParcel(reply);
354 return MSERR_OK;
355 }
356
Release(MessageParcel & data,MessageParcel & reply)357 int32_t AVMetadataHelperServiceStub::Release(MessageParcel &data, MessageParcel &reply)
358 {
359 (void)data;
360 (void)reply;
361 Release();
362 return MSERR_OK;
363 }
364
DestroyStub(MessageParcel & data,MessageParcel & reply)365 int32_t AVMetadataHelperServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
366 {
367 (void)data;
368 reply.WriteInt32(DestroyStub());
369 return MSERR_OK;
370 }
371
SetHelperCallback(MessageParcel & data,MessageParcel & reply)372 int32_t AVMetadataHelperServiceStub::SetHelperCallback(MessageParcel &data, MessageParcel &reply)
373 {
374 (void)data;
375 reply.WriteInt32(SetHelperCallback());
376 return MSERR_OK;
377 }
378
SetListenerObject(MessageParcel & data,MessageParcel & reply)379 int32_t AVMetadataHelperServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
380 {
381 sptr<IRemoteObject> object = data.ReadRemoteObject();
382 reply.WriteInt32(SetListenerObject(object));
383 return MSERR_OK;
384 }
385
GetTimeByFrameIndex(MessageParcel & data,MessageParcel & reply)386 int32_t AVMetadataHelperServiceStub::GetTimeByFrameIndex(MessageParcel &data, MessageParcel &reply)
387 {
388 uint32_t index = data.ReadUint32();
389 uint64_t time = 0;
390 auto res = GetTimeByFrameIndex(index, time);
391 CHECK_AND_RETURN_RET(res == MSERR_OK, res);
392 reply.WriteUint64(time);
393 return MSERR_OK;
394 }
395
GetFrameIndexByTime(MessageParcel & data,MessageParcel & reply)396 int32_t AVMetadataHelperServiceStub::GetFrameIndexByTime(MessageParcel &data, MessageParcel &reply)
397 {
398 uint64_t time = data.ReadUint64();
399 uint32_t index = 0;
400 auto res = GetFrameIndexByTime(time, index);
401 CHECK_AND_RETURN_RET(res == MSERR_OK, res);
402 reply.WriteUint32(index);
403 return MSERR_OK;
404 }
405 } // namespace Media
406 } // namespace OHOS
407