• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "codec_service_stub.h"
17 #include <unistd.h>
18 #include "codec_server.h"
19 #include "avcodec_trace.h"
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "avcodec_parcel.h"
23 #include "avcodec_server_manager.h"
24 #include "avcodec_xcollie.h"
25 #include "avsharedmemory_ipc.h"
26 #include "codec_listener_proxy.h"
27 #ifdef SUPPORT_DRM
28 #include "key_session_service_proxy.h"
29 #endif
30 #include "ipc_skeleton.h"
31 #include "event_manager.h"
32 
33 namespace {
34 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "CodecServiceStub"};
35 
36 const std::map<uint32_t, std::string> CODEC_FUNC_NAME = {
37     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::SET_LISTENER_OBJ),
38      "CodecServiceStub SetListenerObject"},
39     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::INIT), "CodecServiceStub Init"},
40     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::CONFIGURE), "CodecServiceStub Configure"},
41     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::PREPARE), "CodecServiceStub Prepare"},
42     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::START), "CodecServiceStub Start"},
43     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::STOP), "CodecServiceStub Stop"},
44     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::FLUSH), "CodecServiceStub Flush"},
45     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::RESET), "CodecServiceStub Reset"},
46     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::RELEASE), "CodecServiceStub Release"},
47     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::NOTIFY_EOS), "CodecServiceStub NotifyEos"},
48     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::CREATE_INPUT_SURFACE),
49      "CodecServiceStub CreateInputSurface"},
50     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::SET_OUTPUT_SURFACE),
51      "CodecServiceStub SetOutputSurface"},
52     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::QUEUE_INPUT_BUFFER),
53      "CodecServiceStub QueueInputBuffer"},
54     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::GET_OUTPUT_FORMAT),
55      "CodecServiceStub GetOutputFormat"},
56     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::RELEASE_OUTPUT_BUFFER),
57      "CodecServiceStub ReleaseOutputBuffer"},
58     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::SET_PARAMETER),
59      "CodecServiceStub SetParameter"},
60     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::SET_INPUT_SURFACE),
61      "CodecServiceStub SetInputSurface"},
62     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::DEQUEUE_INPUT_BUFFER),
63      "CodecServiceStub DequeueInputBuffer"},
64     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::DEQUEUE_OUTPUT_BUFFER),
65      "CodecServiceStub DequeueOutputBuffer"},
66     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::DESTROY_STUB),
67      "CodecServiceStub DestroyStub"},
68     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::SET_DECRYPT_CONFIG),
69      "CodecServiceStub SetDecryptConfig"},
70     {static_cast<uint32_t>(OHOS::MediaAVCodec::CodecServiceInterfaceCode::RENDER_OUTPUT_BUFFER_AT_TIME),
71      "CodecServiceStub RenderOutputBufferAtTime"},
72 };
73 } // namespace
74 
75 namespace OHOS {
76 namespace MediaAVCodec {
Create(int32_t instanceId)77 sptr<CodecServiceStub> CodecServiceStub::Create(int32_t instanceId)
78 {
79     sptr<CodecServiceStub> codecStub = new (std::nothrow) CodecServiceStub();
80     CHECK_AND_RETURN_RET_LOG(codecStub != nullptr, nullptr, "Codec service stub create failed");
81 
82     int32_t ret = codecStub->InitStub(instanceId);
83     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Codec stub init failed");
84     return codecStub;
85 }
86 
CodecServiceStub()87 CodecServiceStub::CodecServiceStub()
88 {
89     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
90 }
91 
~CodecServiceStub()92 CodecServiceStub::~CodecServiceStub()
93 {
94     std::lock_guard<std::shared_mutex> lock(mutex_);
95     (void)InnerRelease();
96     AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
97 }
98 
InitStub(int32_t instanceId)99 int32_t CodecServiceStub::InitStub(int32_t instanceId)
100 {
101     std::lock_guard<std::shared_mutex> lock(mutex_);
102     AVCODEC_SYNC_TRACE;
103     codecServer_ = CodecServer::Create(instanceId);
104     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server create failed");
105     return AVCS_ERR_OK;
106 }
107 
DestroyStub()108 int32_t CodecServiceStub::DestroyStub()
109 {
110     std::lock_guard<std::shared_mutex> lock(mutex_);
111     if (codecServer_ == nullptr) {
112         return AVCS_ERR_OK;
113     }
114 
115     auto callerInfo = std::static_pointer_cast<CodecServer>(codecServer_)->GetCallerInfo();
116     (void)InnerRelease();
117     codecServer_ = nullptr;
118     AVCodecServerManager::GetInstance().DestroyStubObject(AVCodecServerManager::CODEC, AsObject());
119     EventManager::GetInstance().OnInstanceEvent(EventType::INSTANCE_RELEASE, *callerInfo);
120     return AVCS_ERR_OK;
121 }
122 
Dump(int32_t fd,const std::vector<std::u16string> & args)123 int32_t CodecServiceStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
124 {
125     (void)args;
126     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
127     return std::static_pointer_cast<CodecServer>(codecServer_)->DumpInfo(fd);
128 }
129 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)130 int CodecServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
131 {
132     auto remoteDescriptor = data.ReadInterfaceToken();
133     if (CodecServiceStub::GetDescriptor() != remoteDescriptor) {
134         AVCODEC_LOGE("Invalid descriptor");
135         return AVCS_ERR_INVALID_OPERATION;
136     }
137     int32_t ret = -1;
138     auto itFuncName = CODEC_FUNC_NAME.find(code);
139     std::string funcName =
140         itFuncName != CODEC_FUNC_NAME.end() ? itFuncName->second : "CodecServiceStub OnRemoteRequest";
141     switch (code) {
142         case static_cast<uint32_t>(CodecServiceInterfaceCode::QUEUE_INPUT_BUFFER):
143             ret = QueueInputBuffer(data, reply);
144             break;
145         case static_cast<uint32_t>(CodecServiceInterfaceCode::RELEASE_OUTPUT_BUFFER):
146             ret = ReleaseOutputBuffer(data, reply);
147             break;
148         case static_cast<uint32_t>(CodecServiceInterfaceCode::RENDER_OUTPUT_BUFFER_AT_TIME):
149             ret = RenderOutputBufferAtTime(data, reply);
150             break;
151         case static_cast<uint32_t>(CodecServiceInterfaceCode::INIT):
152             ret = Init(data, reply);
153             break;
154         case static_cast<uint32_t>(CodecServiceInterfaceCode::CONFIGURE):
155             ret = Configure(data, reply);
156             break;
157         case static_cast<uint32_t>(CodecServiceInterfaceCode::PREPARE):
158             ret = Prepare(data, reply);
159             break;
160         case static_cast<uint32_t>(CodecServiceInterfaceCode::START):
161             ret = Start(data, reply);
162             break;
163         case static_cast<uint32_t>(CodecServiceInterfaceCode::STOP):
164             ret = Stop(data, reply);
165             break;
166         case static_cast<uint32_t>(CodecServiceInterfaceCode::FLUSH):
167             ret = Flush(data, reply);
168             break;
169         case static_cast<uint32_t>(CodecServiceInterfaceCode::RESET):
170             ret = Reset(data, reply);
171             break;
172         case static_cast<uint32_t>(CodecServiceInterfaceCode::RELEASE):
173             ret = Release(data, reply);
174             break;
175         case static_cast<uint32_t>(CodecServiceInterfaceCode::NOTIFY_EOS):
176             ret = NotifyEos(data, reply);
177             break;
178         case static_cast<uint32_t>(CodecServiceInterfaceCode::CREATE_INPUT_SURFACE):
179             ret = CreateInputSurface(data, reply);
180             break;
181         case static_cast<uint32_t>(CodecServiceInterfaceCode::SET_OUTPUT_SURFACE):
182             ret = SetOutputSurface(data, reply);
183             break;
184         case static_cast<uint32_t>(CodecServiceInterfaceCode::GET_OUTPUT_FORMAT):
185             ret = GetOutputFormat(data, reply);
186             break;
187         case static_cast<uint32_t>(CodecServiceInterfaceCode::SET_PARAMETER):
188             ret = SetParameter(data, reply);
189             break;
190         case static_cast<uint32_t>(CodecServiceInterfaceCode::GET_INPUT_FORMAT):
191             ret = GetInputFormat(data, reply);
192             break;
193         case static_cast<uint32_t>(CodecServiceInterfaceCode::DESTROY_STUB):
194             ret = DestroyStub(data, reply);
195             break;
196         case static_cast<uint32_t>(CodecServiceInterfaceCode::SET_LISTENER_OBJ):
197             ret = SetListenerObject(data, reply);
198             break;
199         case static_cast<uint32_t>(CodecServiceInterfaceCode::SET_DECRYPT_CONFIG):
200 #ifdef SUPPORT_DRM
201             ret = SetDecryptConfig(data, reply);
202 #endif
203             break;
204         case static_cast<uint32_t>(CodecServiceInterfaceCode::SET_CUSTOM_BUFFER):
205             ret = SetCustomBuffer(data, reply);
206             break;
207         default:
208             AVCODEC_LOGW("No member func supporting, applying default process, code:%{public}u", code);
209             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
210     }
211     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "Failed to call member func %{public}s", funcName.c_str());
212     return ret;
213 }
214 
SetListenerObject(const sptr<IRemoteObject> & object)215 int32_t CodecServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
216 {
217     std::lock_guard<std::shared_mutex> lock(mutex_);
218     CHECK_AND_RETURN_RET_LOG(object != nullptr, AVCS_ERR_NO_MEMORY, "Object is nullptr");
219 
220     listener_ = iface_cast<IStandardCodecListener>(object);
221     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_NO_MEMORY, "Listener is nullptr");
222 
223     std::shared_ptr<MediaCodecCallback> callback = std::make_shared<CodecListenerCallback>(listener_);
224 
225     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
226     (void)codecServer_->SetCallback(callback);
227     return AVCS_ERR_OK;
228 }
229 
Init(AVCodecType type,bool isMimeType,const std::string & name,Meta & callerInfo)230 int32_t CodecServiceStub::Init(AVCodecType type, bool isMimeType, const std::string &name, Meta &callerInfo)
231 {
232     std::unique_lock<std::shared_mutex> lock(mutex_);
233     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
234     int32_t ret = codecServer_->Init(type, isMimeType, name, callerInfo);
235     if (ret != AVCS_ERR_OK) {
236         lock.unlock();
237         DestroyStub();
238     }
239     return ret;
240 }
241 
Configure(const Format & format)242 int32_t CodecServiceStub::Configure(const Format &format)
243 {
244     std::lock_guard<std::shared_mutex> lock(mutex_);
245     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
246     return codecServer_->Configure(format);
247 }
248 
Prepare()249 int32_t CodecServiceStub::Prepare()
250 {
251     std::lock_guard<std::shared_mutex> lock(mutex_);
252     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
253     return codecServer_->Prepare();
254 }
255 
SetCustomBuffer(std::shared_ptr<AVBuffer> buffer)256 int32_t CodecServiceStub::SetCustomBuffer(std::shared_ptr<AVBuffer> buffer)
257 {
258     std::lock_guard<std::shared_mutex> lock(mutex_);
259     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
260     return codecServer_->SetCustomBuffer(buffer);
261 }
262 
Start()263 int32_t CodecServiceStub::Start()
264 {
265     std::lock_guard<std::shared_mutex> lock(mutex_);
266     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
267     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec listener is nullptr");
268     CHECK_AND_RETURN_RET_LOG(!(codecServer_->CheckRunning()), AVCS_ERR_INVALID_STATE, "In invalid state, running");
269     (void)listener_->UpdateGeneration();
270     int32_t ret = codecServer_->Start();
271     if (ret != AVCS_ERR_OK) {
272         (void)listener_->RestoreGeneration();
273     }
274     return ret;
275 }
276 
Stop()277 int32_t CodecServiceStub::Stop()
278 {
279     std::lock_guard<std::shared_mutex> lock(mutex_);
280     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
281     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec listener is nullptr");
282     int32_t ret = codecServer_->Stop();
283     if (ret == AVCS_ERR_OK) {
284         (void)OHOS::IPCSkeleton::FlushCommands(listener_->AsObject().GetRefPtr());
285     }
286     return ret;
287 }
288 
Flush()289 int32_t CodecServiceStub::Flush()
290 {
291     std::lock_guard<std::shared_mutex> lock(mutex_);
292     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
293     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec listener is nullptr");
294     int32_t ret = codecServer_->Flush();
295     if (ret == AVCS_ERR_OK) {
296         (void)OHOS::IPCSkeleton::FlushCommands(listener_->AsObject().GetRefPtr());
297     }
298     return ret;
299 }
300 
Reset()301 int32_t CodecServiceStub::Reset()
302 {
303     std::lock_guard<std::shared_mutex> lock(mutex_);
304     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
305     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec listener is nullptr");
306     int32_t ret = codecServer_->Reset();
307     if (ret == AVCS_ERR_OK) {
308         (void)OHOS::IPCSkeleton::FlushCommands(listener_->AsObject().GetRefPtr());
309         static_cast<CodecListenerProxy *>(listener_.GetRefPtr())->ClearListenerCache();
310     }
311     return ret;
312 }
313 
Release()314 int32_t CodecServiceStub::Release()
315 {
316     std::lock_guard<std::shared_mutex> lock(mutex_);
317     return InnerRelease();
318 }
319 
NotifyEos()320 int32_t CodecServiceStub::NotifyEos()
321 {
322     std::lock_guard<std::shared_mutex> lock(mutex_);
323     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
324     return codecServer_->NotifyEos();
325 }
326 
CreateInputSurface()327 sptr<OHOS::Surface> CodecServiceStub::CreateInputSurface()
328 {
329     std::lock_guard<std::shared_mutex> lock(mutex_);
330     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, nullptr, "Codec server is nullptr");
331     return codecServer_->CreateInputSurface();
332 }
333 
SetOutputSurface(sptr<OHOS::Surface> surface)334 int32_t CodecServiceStub::SetOutputSurface(sptr<OHOS::Surface> surface)
335 {
336     std::lock_guard<std::shared_mutex> lock(mutex_);
337     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
338     return codecServer_->SetOutputSurface(surface);
339 }
340 
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)341 int32_t CodecServiceStub::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
342 {
343     std::shared_lock<std::shared_mutex> lock(mutex_);
344     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
345     return codecServer_->QueueInputBuffer(index, info, flag);
346 }
347 
QueueInputBuffer(uint32_t index)348 int32_t CodecServiceStub::QueueInputBuffer(uint32_t index)
349 {
350     (void)index;
351     return AVCS_ERR_UNSUPPORT;
352 }
353 
QueueInputParameter(uint32_t index)354 int32_t CodecServiceStub::QueueInputParameter(uint32_t index)
355 {
356     (void)index;
357     return AVCS_ERR_UNSUPPORT;
358 }
359 
GetOutputFormat(Format & format)360 int32_t CodecServiceStub::GetOutputFormat(Format &format)
361 {
362     std::lock_guard<std::shared_mutex> lock(mutex_);
363     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
364     return codecServer_->GetOutputFormat(format);
365 }
366 
ReleaseOutputBuffer(uint32_t index,bool render)367 int32_t CodecServiceStub::ReleaseOutputBuffer(uint32_t index, bool render)
368 {
369     std::shared_lock<std::shared_mutex> lock(mutex_);
370     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
371     return codecServer_->ReleaseOutputBuffer(index, render);
372 }
373 
RenderOutputBufferAtTime(uint32_t index,int64_t renderTimestampNs)374 int32_t CodecServiceStub::RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs)
375 {
376     std::shared_lock<std::shared_mutex> lock(mutex_);
377     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
378     return codecServer_->RenderOutputBufferAtTime(index, renderTimestampNs);
379 }
380 
SetParameter(const Format & format)381 int32_t CodecServiceStub::SetParameter(const Format &format)
382 {
383     std::lock_guard<std::shared_mutex> lock(mutex_);
384     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
385     return codecServer_->SetParameter(format);
386 }
387 
GetInputFormat(Format & format)388 int32_t CodecServiceStub::GetInputFormat(Format &format)
389 {
390     std::shared_lock<std::shared_mutex> lock(mutex_);
391     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
392     return codecServer_->GetInputFormat(format);
393 }
394 
395 #ifdef SUPPORT_DRM
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySession,const bool svpFlag)396 int32_t CodecServiceStub::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
397     const bool svpFlag)
398 {
399     std::lock_guard<std::shared_mutex> lock(mutex_);
400     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
401     return codecServer_->SetDecryptConfig(keySession, svpFlag);
402 }
403 #endif
404 
DestroyStub(MessageParcel & data,MessageParcel & reply)405 int32_t CodecServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
406 {
407     AVCODEC_SYNC_TRACE;
408     (void)data;
409 
410     bool ret = reply.WriteInt32(DestroyStub());
411     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
412     return AVCS_ERR_OK;
413 }
414 
SetListenerObject(MessageParcel & data,MessageParcel & reply)415 int32_t CodecServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
416 {
417     AVCODEC_SYNC_TRACE;
418     sptr<IRemoteObject> object = data.ReadRemoteObject();
419 
420     bool ret = reply.WriteInt32(SetListenerObject(object));
421     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
422     return AVCS_ERR_OK;
423 }
424 
Init(MessageParcel & data,MessageParcel & reply)425 int32_t CodecServiceStub::Init(MessageParcel &data, MessageParcel &reply)
426 {
427     AVCODEC_SYNC_TRACE;
428     Meta callerInfo;
429     callerInfo.FromParcel(data);
430     AVCodecType type = static_cast<AVCodecType>(data.ReadInt32());
431     bool isMimeType = data.ReadBool();
432     std::string name = data.ReadString();
433 
434     bool ret = reply.WriteInt32(Init(type, isMimeType, name, callerInfo));
435     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
436     return AVCS_ERR_OK;
437 }
438 
Configure(MessageParcel & data,MessageParcel & reply)439 int32_t CodecServiceStub::Configure(MessageParcel &data, MessageParcel &reply)
440 {
441     AVCODEC_SYNC_TRACE;
442     Format format;
443     (void)AVCodecParcel::Unmarshalling(data, format);
444     bool ret = reply.WriteInt32(Configure(format));
445     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
446     return AVCS_ERR_OK;
447 }
448 
Prepare(MessageParcel & data,MessageParcel & reply)449 int32_t CodecServiceStub::Prepare(MessageParcel &data, MessageParcel &reply)
450 {
451     AVCODEC_SYNC_TRACE;
452     (void)data;
453 
454     bool ret = reply.WriteInt32(Prepare());
455     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
456     return AVCS_ERR_OK;
457 }
458 
Start(MessageParcel & data,MessageParcel & reply)459 int32_t CodecServiceStub::Start(MessageParcel &data, MessageParcel &reply)
460 {
461     AVCODEC_SYNC_TRACE;
462     (void)data;
463 
464     bool ret = reply.WriteInt32(Start());
465     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
466     return AVCS_ERR_OK;
467 }
468 
Stop(MessageParcel & data,MessageParcel & reply)469 int32_t CodecServiceStub::Stop(MessageParcel &data, MessageParcel &reply)
470 {
471     AVCODEC_SYNC_TRACE;
472     (void)data;
473 
474     bool ret = reply.WriteInt32(Stop());
475     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
476     return AVCS_ERR_OK;
477 }
478 
Flush(MessageParcel & data,MessageParcel & reply)479 int32_t CodecServiceStub::Flush(MessageParcel &data, MessageParcel &reply)
480 {
481     AVCODEC_SYNC_TRACE;
482     (void)data;
483 
484     bool ret = reply.WriteInt32(Flush());
485     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
486     return AVCS_ERR_OK;
487 }
488 
Reset(MessageParcel & data,MessageParcel & reply)489 int32_t CodecServiceStub::Reset(MessageParcel &data, MessageParcel &reply)
490 {
491     AVCODEC_SYNC_TRACE;
492     (void)data;
493 
494     bool ret = reply.WriteInt32(Reset());
495     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
496     return AVCS_ERR_OK;
497 }
498 
Release(MessageParcel & data,MessageParcel & reply)499 int32_t CodecServiceStub::Release(MessageParcel &data, MessageParcel &reply)
500 {
501     AVCODEC_SYNC_TRACE;
502     (void)data;
503 
504     bool ret = reply.WriteInt32(Release());
505     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
506     return AVCS_ERR_OK;
507 }
508 
NotifyEos(MessageParcel & data,MessageParcel & reply)509 int32_t CodecServiceStub::NotifyEos(MessageParcel &data, MessageParcel &reply)
510 {
511     AVCODEC_SYNC_TRACE;
512     (void)data;
513 
514     bool ret = reply.WriteInt32(NotifyEos());
515     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
516     return AVCS_ERR_OK;
517 }
518 
CreateInputSurface(MessageParcel & data,MessageParcel & reply)519 int32_t CodecServiceStub::CreateInputSurface(MessageParcel &data, MessageParcel &reply)
520 {
521     AVCODEC_SYNC_TRACE;
522     (void)data;
523     sptr<OHOS::Surface> surface = CreateInputSurface();
524 
525     reply.WriteInt32(surface == nullptr ? AVCS_ERR_INVALID_OPERATION : AVCS_ERR_OK);
526     if (surface != nullptr && surface->GetProducer() != nullptr) {
527         sptr<IRemoteObject> object = surface->GetProducer()->AsObject();
528         bool ret = reply.WriteRemoteObject(object);
529         CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
530     }
531     return AVCS_ERR_OK;
532 }
533 
SetOutputSurface(MessageParcel & data,MessageParcel & reply)534 int32_t CodecServiceStub::SetOutputSurface(MessageParcel &data, MessageParcel &reply)
535 {
536     AVCODEC_SYNC_TRACE;
537 
538     sptr<IRemoteObject> object = data.ReadRemoteObject();
539     CHECK_AND_RETURN_RET_LOG(object != nullptr, AVCS_ERR_NO_MEMORY, "Object is nullptr");
540 
541     sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(object);
542     CHECK_AND_RETURN_RET_LOG(producer != nullptr, AVCS_ERR_NO_MEMORY, "Producer is nullptr");
543 
544     sptr<OHOS::Surface> surface = OHOS::Surface::CreateSurfaceAsProducer(producer);
545     CHECK_AND_RETURN_RET_LOG(surface != nullptr, AVCS_ERR_NO_MEMORY, "Surface create failed");
546 
547     std::string format = data.ReadString();
548     AVCODEC_LOGD("Surface format is %{public}s!", format.c_str());
549     const std::string surfaceFormat = "SURFACE_FORMAT";
550     (void)surface->SetUserData(surfaceFormat, format);
551 
552     bool ret = reply.WriteInt32(SetOutputSurface(surface));
553     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
554     return AVCS_ERR_OK;
555 }
556 
QueueInputBuffer(MessageParcel & data,MessageParcel & reply)557 int32_t CodecServiceStub::QueueInputBuffer(MessageParcel &data, MessageParcel &reply)
558 {
559     AVCODEC_SYNC_TRACE;
560 
561     uint32_t index = data.ReadUint32();
562     AVCodecBufferInfo info;
563     AVCodecBufferFlag flag;
564     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec listener is nullptr");
565     bool ret = static_cast<CodecListenerProxy *>(listener_.GetRefPtr())->
566         InputBufferInfoFromParcel(index, info, flag, data);
567     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Listener read meta data failed");
568 
569     ret = reply.WriteInt32(QueueInputBuffer(index, info, flag));
570     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
571     return AVCS_ERR_OK;
572 }
573 
GetOutputFormat(MessageParcel & data,MessageParcel & reply)574 int32_t CodecServiceStub::GetOutputFormat(MessageParcel &data, MessageParcel &reply)
575 {
576     AVCODEC_SYNC_TRACE;
577 
578     (void)data;
579     Format format;
580     (void)GetOutputFormat(format);
581     (void)AVCodecParcel::Marshalling(reply, format);
582     return AVCS_ERR_OK;
583 }
584 
ReleaseOutputBuffer(MessageParcel & data,MessageParcel & reply)585 int32_t CodecServiceStub::ReleaseOutputBuffer(MessageParcel &data, MessageParcel &reply)
586 {
587     AVCODEC_SYNC_TRACE;
588 
589     uint32_t index = data.ReadUint32();
590     bool render = data.ReadBool();
591 
592     bool ret = reply.WriteInt32(ReleaseOutputBuffer(index, render));
593     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
594     return AVCS_ERR_OK;
595 }
596 
RenderOutputBufferAtTime(MessageParcel & data,MessageParcel & reply)597 int32_t CodecServiceStub::RenderOutputBufferAtTime(MessageParcel &data, MessageParcel &reply)
598 {
599     AVCODEC_SYNC_TRACE;
600 
601     uint32_t index = data.ReadUint32();
602     int64_t renderTimestampNs = data.ReadInt64();
603     CHECK_AND_RETURN_RET_LOG(listener_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec listener is nullptr");
604     bool ret = static_cast<CodecListenerProxy *>(listener_.GetRefPtr())
605                    ->SetOutputBufferRenderTimestamp(index, renderTimestampNs);
606     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Listener read meta data failed");
607 
608     ret = reply.WriteInt32(RenderOutputBufferAtTime(index, renderTimestampNs));
609     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
610     return AVCS_ERR_OK;
611 }
612 
SetParameter(MessageParcel & data,MessageParcel & reply)613 int32_t CodecServiceStub::SetParameter(MessageParcel &data, MessageParcel &reply)
614 {
615     AVCODEC_SYNC_TRACE;
616     Format format;
617     (void)AVCodecParcel::Unmarshalling(data, format);
618 
619     bool ret = reply.WriteInt32(SetParameter(format));
620     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
621     return AVCS_ERR_OK;
622 }
623 
GetInputFormat(MessageParcel & data,MessageParcel & reply)624 int32_t CodecServiceStub::GetInputFormat(MessageParcel &data, MessageParcel &reply)
625 {
626     AVCODEC_SYNC_TRACE;
627 
628     (void)data;
629     Format format;
630     (void)GetInputFormat(format);
631     (void)AVCodecParcel::Marshalling(reply, format);
632     return AVCS_ERR_OK;
633 }
634 
635 #ifdef SUPPORT_DRM
SetDecryptConfig(MessageParcel & data,MessageParcel & reply)636 int32_t CodecServiceStub::SetDecryptConfig(MessageParcel &data, MessageParcel &reply)
637 {
638     AVCODEC_SYNC_TRACE;
639     sptr<IRemoteObject> object = data.ReadRemoteObject();
640     CHECK_AND_RETURN_RET_LOG(object != nullptr, AVCS_ERR_INVALID_OPERATION,
641         "SetDecryptConfig read object is null");
642     bool svpFlag = data.ReadBool();
643 
644     sptr<DrmStandard::MediaKeySessionServiceProxy> keySessionServiceProxy =
645         iface_cast<DrmStandard::MediaKeySessionServiceProxy>(object);
646     CHECK_AND_RETURN_RET_LOG(keySessionServiceProxy != nullptr, AVCS_ERR_INVALID_OPERATION,
647         "SetDecryptConfig cast object to proxy failed");
648     bool ret = reply.WriteInt32(SetDecryptConfig(keySessionServiceProxy, svpFlag));
649     CHECK_AND_RETURN_RET_LOG(ret == true, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
650     return AVCS_ERR_OK;
651 }
652 #endif
653 
SetCustomBuffer(MessageParcel & data,MessageParcel & reply)654 int32_t CodecServiceStub::SetCustomBuffer(MessageParcel &data, MessageParcel &reply)
655 {
656     AVCODEC_SYNC_TRACE;
657     std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer();
658     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, AVCS_ERR_NO_MEMORY, "Create buffer failed");
659     bool ret = buffer->ReadFromMessageParcel(data);
660     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Read From MessageParcel failed");
661     ret = reply.WriteInt32(SetCustomBuffer(buffer));
662     CHECK_AND_RETURN_RET_LOG(ret, AVCS_ERR_INVALID_OPERATION, "Reply write failed");
663     return AVCS_ERR_OK;
664 }
665 
InnerRelease()666 int32_t CodecServiceStub::InnerRelease()
667 {
668     CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, AVCS_ERR_NO_MEMORY, "Codec server is nullptr");
669     return codecServer_->Release();
670 }
671 } // namespace MediaAVCodec
672 } // namespace OHOS
673