1 /*
2 * Copyright (C) 2023 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 <unistd.h>
17 #include <string>
18 #include <map>
19 #include "avsharedmemory_ipc.h"
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "avcodec_server_manager.h"
23 #include "avcodec_xcollie.h"
24 #include "codeclist_service_stub.h"
25
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "CodecListServiceStub"};
28
29 const std::map<uint32_t, std::string> CODECLIST_FUNC_NAME = {
30 { static_cast<uint32_t>(OHOS::MediaAVCodec::AVCodecListServiceInterfaceCode::FIND_DECODER),
31 "CodecListServiceStub DoFindDecoder" },
32 { static_cast<uint32_t>(OHOS::MediaAVCodec::AVCodecListServiceInterfaceCode::FIND_ENCODER),
33 "CodecListServiceStub DoFindEncoder" },
34 { static_cast<uint32_t>(OHOS::MediaAVCodec::AVCodecListServiceInterfaceCode::GET_CAPABILITY),
35 "CodecListServiceStub DoGetCapability" },
36 { static_cast<uint32_t>(OHOS::MediaAVCodec::AVCodecListServiceInterfaceCode::DESTROY),
37 "CodecListServiceStub DoDestroyStub" },
38 };
39 }
40
41 namespace OHOS {
42 namespace MediaAVCodec {
43 using namespace Media;
Create()44 sptr<CodecListServiceStub> CodecListServiceStub::Create()
45 {
46 sptr<CodecListServiceStub> codecListStub = new (std::nothrow) CodecListServiceStub();
47 CHECK_AND_RETURN_RET_LOG(codecListStub != nullptr, nullptr, "Create codeclist service stub failed");
48
49 int32_t ret = codecListStub->Init();
50 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init codeclist stub failed");
51 return codecListStub;
52 }
53
CodecListServiceStub()54 CodecListServiceStub::CodecListServiceStub()
55 {
56 AVCODEC_LOGD("Create codeclist service stub instance successful");
57 }
58
~CodecListServiceStub()59 CodecListServiceStub::~CodecListServiceStub()
60 {
61 AVCODEC_LOGD("Destroy codeclist service stub instance successful");
62 }
63
Init()64 int32_t CodecListServiceStub::Init()
65 {
66 std::lock_guard<std::shared_mutex> lock(mutex_);
67 codecListServer_ = CodecListServer::Create();
68 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Create codecList server failed");
69 return AVCS_ERR_OK;
70 }
71
DestroyStub()72 int32_t CodecListServiceStub::DestroyStub()
73 {
74 std::lock_guard<std::shared_mutex> lock(mutex_);
75 codecListServer_ = nullptr;
76 AVCodecServerManager::GetInstance().DestroyStubObject(AVCodecServerManager::CODECLIST, AsObject());
77 return AVCS_ERR_OK;
78 }
79
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)80 int CodecListServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
81 MessageOption &option)
82 {
83 AVCODEC_LOGD("Stub: OnRemoteRequest of code: %{public}u is received", code);
84
85 auto remoteDescriptor = data.ReadInterfaceToken();
86 if (CodecListServiceStub::GetDescriptor() != remoteDescriptor) {
87 AVCODEC_LOGE("Invalid descriptor");
88 return AVCS_ERR_INVALID_OPERATION;
89 }
90 int32_t ret = -1;
91 auto itFuncName = CODECLIST_FUNC_NAME.find(code);
92 std::string funcName =
93 itFuncName != CODECLIST_FUNC_NAME.end() ? itFuncName->second : "CodecListServiceStub OnRemoteRequest";
94 switch (code) {
95 case static_cast<uint32_t>(AVCodecListServiceInterfaceCode::FIND_DECODER):
96 ret = DoFindDecoder(data, reply);
97 break;
98 case static_cast<uint32_t>(AVCodecListServiceInterfaceCode::FIND_ENCODER):
99 ret = DoFindEncoder(data, reply);
100 break;
101 case static_cast<uint32_t>(AVCodecListServiceInterfaceCode::GET_CAPABILITY):
102 ret = DoGetCapability(data, reply);
103 break;
104 case static_cast<uint32_t>(AVCodecListServiceInterfaceCode::DESTROY):
105 ret = DoDestroyStub(data, reply);
106 break;
107 default:
108 AVCODEC_LOGW("CodecListServiceStub: no member func supporting, applying default process");
109 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
110 }
111 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Failed to call member func %{public}s", funcName.c_str());
112 return ret;
113 }
114
FindDecoder(const Format & format)115 std::string CodecListServiceStub::FindDecoder(const Format &format)
116 {
117 std::shared_lock<std::shared_mutex> lock(mutex_);
118 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "Find decoder failed: avcodeclist server is nullptr");
119 return codecListServer_->FindDecoder(format);
120 }
121
FindEncoder(const Format & format)122 std::string CodecListServiceStub::FindEncoder(const Format &format)
123 {
124 std::shared_lock<std::shared_mutex> lock(mutex_);
125 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, "", "Find encoder failed: avcodeclist server is nullptr");
126 return codecListServer_->FindEncoder(format);
127 }
128
GetCapability(CapabilityData & capabilityData,const std::string & mime,const bool isEncoder,const AVCodecCategory & category)129 int32_t CodecListServiceStub::GetCapability(CapabilityData &capabilityData, const std::string &mime,
130 const bool isEncoder, const AVCodecCategory &category)
131 {
132 std::shared_lock<std::shared_mutex> lock(mutex_);
133 CHECK_AND_RETURN_RET_LOG(codecListServer_ != nullptr, AVCS_ERR_NO_MEMORY,
134 "Get capability failed: avcodeclist server is null");
135 return codecListServer_->GetCapability(capabilityData, mime, isEncoder, category);
136 }
137
DoFindDecoder(MessageParcel & data,MessageParcel & reply)138 int32_t CodecListServiceStub::DoFindDecoder(MessageParcel &data, MessageParcel &reply)
139 {
140 Format format;
141 (void)AVCodecParcel::Unmarshalling(data, format);
142 reply.WriteString(FindDecoder(format));
143 return AVCS_ERR_OK;
144 }
145
DoFindEncoder(MessageParcel & data,MessageParcel & reply)146 int32_t CodecListServiceStub::DoFindEncoder(MessageParcel &data, MessageParcel &reply)
147 {
148 Format format;
149 (void)AVCodecParcel::Unmarshalling(data, format);
150 reply.WriteString(FindEncoder(format));
151 return AVCS_ERR_OK;
152 }
153
DoGetCapability(MessageParcel & data,MessageParcel & reply)154 int32_t CodecListServiceStub::DoGetCapability(MessageParcel &data, MessageParcel &reply)
155 {
156 std::string mime = data.ReadString();
157 bool isEncoder = data.ReadBool();
158 AVCodecCategory category = static_cast<AVCodecCategory>(data.ReadInt32());
159 CapabilityData capabilityData;
160 (void)GetCapability(capabilityData, mime, isEncoder, category);
161 (void)CodecListParcel::Marshalling(reply, capabilityData);
162 return AVCS_ERR_OK;
163 }
164
DoDestroyStub(MessageParcel & data,MessageParcel & reply)165 int32_t CodecListServiceStub::DoDestroyStub(MessageParcel &data, MessageParcel &reply)
166 {
167 (void)data;
168 reply.WriteInt32(DestroyStub());
169 return AVCS_ERR_OK;
170 }
171 } // namespace MediaAVCodec
172 } // namespace OHOS