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 "avcodeclist_service_stub.h"
17 #include <unistd.h>
18 #include "avsharedmemory_ipc.h"
19 #include "media_errors.h"
20 #include "media_log.h"
21 #include "media_server_manager.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecListServiceStub"};
25 }
26
27 namespace OHOS {
28 namespace Media {
Create()29 sptr<AVCodecListServiceStub> AVCodecListServiceStub::Create()
30 {
31 sptr<AVCodecListServiceStub> codecListStub = new(std::nothrow) AVCodecListServiceStub();
32 CHECK_AND_RETURN_RET_LOG(codecListStub != nullptr, nullptr, "failed to new AVCodecListServiceStub");
33
34 int32_t ret = codecListStub->Init();
35 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to codeclist stub init");
36 return codecListStub;
37 }
38
AVCodecListServiceStub()39 AVCodecListServiceStub::AVCodecListServiceStub()
40 {
41 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
42 }
43
~AVCodecListServiceStub()44 AVCodecListServiceStub::~AVCodecListServiceStub()
45 {
46 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
47 }
48
Init()49 int32_t AVCodecListServiceStub::Init()
50 {
51 std::unique_lock<std::mutex> lock(mutex_);
52 codecListServer_ = AVCodecListServer::Create();
53 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, MSERR_NO_MEMORY, "failed to create AVCodecListServer");
54 codecListFuncs_[FIND_VIDEO_DECODER] = &AVCodecListServiceStub::FindVideoDecoder;
55 codecListFuncs_[FIND_VIDEO_ENCODER] = &AVCodecListServiceStub::FindVideoEncoder;
56 codecListFuncs_[FIND_AUDIO_DECODER] = &AVCodecListServiceStub::FindAudioDecoder;
57 codecListFuncs_[FIND_AUDIO_ENCODER] = &AVCodecListServiceStub::FindAudioEncoder;
58 codecListFuncs_[GET_CAPABILITY_INFOS] = &AVCodecListServiceStub::GetCodecCapabilityInfos;
59 codecListFuncs_[DESTROY] = &AVCodecListServiceStub::DestroyStub;
60 return MSERR_OK;
61 }
62
DestroyStub()63 int32_t AVCodecListServiceStub::DestroyStub()
64 {
65 std::unique_lock<std::mutex> lock(mutex_);
66 codecListServer_ = nullptr;
67
68 MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::AVCODECLIST, AsObject());
69 return MSERR_OK;
70 }
71
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)72 int AVCodecListServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
73 MessageOption &option)
74 {
75 MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}u is received", code);
76
77 auto remoteDescriptor = data.ReadInterfaceToken();
78 if (AVCodecListServiceStub::GetDescriptor() != remoteDescriptor) {
79 MEDIA_LOGE("Invalid descriptor");
80 return MSERR_INVALID_OPERATION;
81 }
82
83 auto itFunc = codecListFuncs_.find(code);
84 if (itFunc != codecListFuncs_.end()) {
85 auto memberFunc = itFunc->second;
86 if (memberFunc != nullptr) {
87 int32_t ret = (this->*memberFunc)(data, reply);
88 if (ret != MSERR_OK) {
89 MEDIA_LOGE("calling memberFunc is failed.");
90 }
91 return MSERR_OK;
92 }
93 }
94 MEDIA_LOGW("AVCodecListServiceStub: no member func supporting, applying default process");
95
96 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97 }
98
FindVideoDecoder(const Format & format)99 std::string AVCodecListServiceStub::FindVideoDecoder(const Format &format)
100 {
101 std::unique_lock<std::mutex> lock(mutex_);
102 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "avcodeclist server is nullptr");
103 return codecListServer_->FindVideoDecoder(format);
104 }
105
FindVideoEncoder(const Format & format)106 std::string AVCodecListServiceStub::FindVideoEncoder(const Format &format)
107 {
108 std::unique_lock<std::mutex> lock(mutex_);
109 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "avcodeclist server is nullptr");
110 return codecListServer_->FindVideoEncoder(format);
111 }
112
FindAudioDecoder(const Format & format)113 std::string AVCodecListServiceStub::FindAudioDecoder(const Format &format)
114 {
115 std::unique_lock<std::mutex> lock(mutex_);
116 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "avcodeclist server is nullptr");
117 return codecListServer_->FindAudioDecoder(format);
118 }
119
FindAudioEncoder(const Format & format)120 std::string AVCodecListServiceStub::FindAudioEncoder(const Format &format)
121 {
122 std::unique_lock<std::mutex> lock(mutex_);
123 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "avcodeclist server is nullptr");
124 return codecListServer_->FindAudioEncoder(format);
125 }
126
GetCodecCapabilityInfos()127 std::vector<CapabilityData> AVCodecListServiceStub::GetCodecCapabilityInfos()
128 {
129 std::unique_lock<std::mutex> lock(mutex_);
130 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, std::vector<CapabilityData>(),
131 "avcodeclist server is nullptr");
132 return codecListServer_->GetCodecCapabilityInfos();
133 }
134
FindVideoDecoder(MessageParcel & data,MessageParcel & reply)135 int32_t AVCodecListServiceStub::FindVideoDecoder(MessageParcel &data, MessageParcel &reply)
136 {
137 Format format;
138 (void)MediaParcel::Unmarshalling(data, format);
139 reply.WriteString(FindVideoDecoder(format));
140 return MSERR_OK;
141 }
142
FindVideoEncoder(MessageParcel & data,MessageParcel & reply)143 int32_t AVCodecListServiceStub::FindVideoEncoder(MessageParcel &data, MessageParcel &reply)
144 {
145 Format format;
146 (void)MediaParcel::Unmarshalling(data, format);
147 reply.WriteString(FindVideoEncoder(format));
148 return MSERR_OK;
149 }
150
FindAudioDecoder(MessageParcel & data,MessageParcel & reply)151 int32_t AVCodecListServiceStub::FindAudioDecoder(MessageParcel &data, MessageParcel &reply)
152 {
153 Format format;
154 (void)MediaParcel::Unmarshalling(data, format);
155 reply.WriteString(FindAudioDecoder(format));
156 return MSERR_OK;
157 }
158
FindAudioEncoder(MessageParcel & data,MessageParcel & reply)159 int32_t AVCodecListServiceStub::FindAudioEncoder(MessageParcel &data, MessageParcel &reply)
160 {
161 Format format;
162 (void)MediaParcel::Unmarshalling(data, format);
163 reply.WriteString(FindAudioEncoder(format));
164 return MSERR_OK;
165 }
166
GetCodecCapabilityInfos(MessageParcel & data,MessageParcel & reply)167 int32_t AVCodecListServiceStub::GetCodecCapabilityInfos(MessageParcel &data, MessageParcel &reply)
168 {
169 std::string configFile = data.ReadString();
170 std::vector<CapabilityData> capabilityDataArray = GetCodecCapabilityInfos();
171 (void)AVCodecListParcel::Marshalling(reply, capabilityDataArray); // vector<CapabilityData> to MessageParcel
172
173 return MSERR_OK;
174 }
175
DestroyStub(MessageParcel & data,MessageParcel & reply)176 int32_t AVCodecListServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
177 {
178 (void)data;
179 reply.WriteInt32(DestroyStub());
180 return MSERR_OK;
181 }
182 } // namespace Media
183 } // namespace OHOS
184