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_proxy.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "media_parcel.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_METADATA, "HelperListenerProxy"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 #define LISTENER(statement, args...) { OHOS::Media::XcollieTimer xCollie(args); statement; }
28 
HelperListenerProxy(const sptr<IRemoteObject> & impl)29 HelperListenerProxy::HelperListenerProxy(const sptr<IRemoteObject> &impl)
30     : IRemoteProxy<IStandardHelperListener>(impl)
31 {
32     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
33 }
34 
~HelperListenerProxy()35 HelperListenerProxy::~HelperListenerProxy()
36 {
37     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
38 }
39 
OnError(int32_t errorCode,const std::string & errorMsg)40 void HelperListenerProxy::OnError(int32_t errorCode, const std::string &errorMsg)
41 {
42     MessageParcel data;
43     MessageParcel reply;
44     MessageOption option(MessageOption::TF_ASYNC);
45 
46     bool token = data.WriteInterfaceToken(HelperListenerProxy::GetDescriptor());
47     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
48 
49     data.WriteInt32(errorCode);
50     data.WriteString(errorMsg);
51     int error = SendRequest(HelperListenerMsg::ON_ERROR_MSG, data, reply, option);
52     CHECK_AND_RETURN_LOG(error == MSERR_OK, "on error failed, error: %{public}d", error);
53 }
54 
OnInfo(HelperOnInfoType type,int32_t extra,const Format & infoBody)55 void HelperListenerProxy::OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody)
56 {
57     MessageParcel data;
58     MessageParcel reply;
59     MessageOption option(MessageOption::TF_ASYNC);
60 
61     bool token = data.WriteInterfaceToken(HelperListenerProxy::GetDescriptor());
62     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
63 
64     data.WriteInt32(type);
65     data.WriteInt32(extra);
66     MediaParcel::Marshalling(data, infoBody);
67     int error = SendRequest(HelperListenerMsg::ON_INFO, data, reply, option);
68     CHECK_AND_RETURN_LOG(error == MSERR_OK, "on info failed, error: %{public}d", error);
69 }
70 
HelperListenerCallback(const sptr<IStandardHelperListener> & listener)71 HelperListenerCallback::HelperListenerCallback(const sptr<IStandardHelperListener> &listener) : listener_(listener)
72 {
73     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
74 }
75 
~HelperListenerCallback()76 HelperListenerCallback::~HelperListenerCallback()
77 {
78     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
79 }
80 
OnError(int32_t errorCode,const std::string & errorMsg)81 void HelperListenerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
82 {
83     MEDIA_LOGE("Helper callback onError, errorCode: %{public}d, errorMsg: %{public}s", errorCode, errorMsg.c_str());
84     CHECK_AND_RETURN(listener_ != nullptr);
85     listener_->OnError(errorCode, errorMsg);
86 }
87 
OnInfo(HelperOnInfoType type,int32_t extra,const Format & infoBody)88 void HelperListenerCallback::OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody)
89 {
90     CHECK_AND_RETURN(listener_ != nullptr);
91     listener_->OnInfo(type, extra, infoBody);
92 }
93 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t HelperListenerProxy::SendRequest(uint32_t code, MessageParcel &data,
95     MessageParcel &reply, MessageOption &option)
96 {
97     int32_t error = Remote()->SendRequest(code, data, reply, option);
98     CHECK_AND_RETURN_RET_LOG(error == MSERR_OK, MSERR_INVALID_OPERATION,
99         "SendRequest failed, error: %{public}d", error);
100     return error;
101 }
102 } // namespace Media
103 } // namespace OHOS
104