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 "helper_listener_stub.h"
17 #include "av_common.h"
18 #include "avmetadatahelper.h"
19 #include "media_log.h"
20 #include "media_errors.h"
21 #include "media_parcel.h"
22 
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_METADATA, "HelperListenerStub"};
25 }
26 
27 namespace OHOS {
28 namespace Media {
HelperListenerStub()29 HelperListenerStub::HelperListenerStub()
30 {
31     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
32 }
33 
~HelperListenerStub()34 HelperListenerStub::~HelperListenerStub()
35 {
36     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
37 }
38 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int HelperListenerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
40     MessageOption &option)
41 {
42     auto remoteDescriptor = data.ReadInterfaceToken();
43     CHECK_AND_RETURN_RET_LOG(HelperListenerStub::GetDescriptor() == remoteDescriptor,
44         MSERR_INVALID_OPERATION, "Invalid descriptor");
45 
46     switch (code) {
47         case HelperListenerMsg::ON_INFO: {
48             int32_t type = data.ReadInt32();
49             int32_t extra = data.ReadInt32();
50             Format format;
51             (void)MediaParcel::Unmarshalling(data, format);
52             std::string info = format.Stringify();
53             MEDIA_LOGI("0x%{public}06" PRIXPTR " listen on info type: %{public}d extra %{public}d, format %{public}s",
54                        FAKE_POINTER(this), type, extra, info.c_str());
55             OnInfo(static_cast<HelperOnInfoType>(type), extra, format);
56             return MSERR_OK;
57         }
58         case HelperListenerMsg::ON_ERROR_MSG: {
59             int32_t errorCode = data.ReadInt32();
60             std::string errorMsg = data.ReadString();
61             OnError(errorCode, errorMsg);
62             return MSERR_OK;
63         }
64         default: {
65             MEDIA_LOGE("default case, need check HelperListenerStub");
66             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67         }
68     }
69 }
70 
OnError(HelperErrorType errorType,int32_t errorCode)71 void HelperListenerStub::OnError(HelperErrorType errorType, int32_t errorCode)
72 {
73     std::shared_ptr<HelperCallback> cb = callback_.lock();
74     CHECK_AND_RETURN(cb != nullptr);
75     (void)errorType;
76     auto errorMsg = MSErrorToExtErrorString(static_cast<MediaServiceErrCode>(errorCode));
77     cb->OnError(errorCode, errorMsg);
78 }
79 
OnInfo(HelperOnInfoType type,int32_t extra,const Format & infoBody)80 void HelperListenerStub::OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody)
81 {
82     std::shared_ptr<HelperCallback> cb = callback_.lock();
83     CHECK_AND_RETURN(cb != nullptr);
84     cb->OnInfo(type, extra, infoBody);
85 }
86 
OnError(int32_t errorCode,const std::string & errorMsg)87 void HelperListenerStub::OnError(int32_t errorCode, const std::string &errorMsg)
88 {
89     std::shared_ptr<HelperCallback> cb = callback_.lock();
90     CHECK_AND_RETURN(cb != nullptr);
91     cb->OnError(errorCode, errorMsg);
92 }
93 
SetHelperCallback(const std::weak_ptr<HelperCallback> & callback)94 void HelperListenerStub::SetHelperCallback(const std::weak_ptr<HelperCallback> &callback)
95 {
96     callback_ = callback;
97 }
98 } // namespace Media
99 } // namespace OHOS
100