1 /*
2  * Copyright (c) 2022 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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioManagerListenerProxy"
17 #endif
18 
19 #include "audio_manager_listener_proxy.h"
20 #include "audio_system_manager.h"
21 #include "audio_service_log.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
AudioManagerListenerProxy(const sptr<IRemoteObject> & impl)25 AudioManagerListenerProxy::AudioManagerListenerProxy(const sptr<IRemoteObject> &impl)
26     : IRemoteProxy<IStandardAudioServerManagerListener>(impl)
27 {
28 }
29 
~AudioManagerListenerProxy()30 AudioManagerListenerProxy::~AudioManagerListenerProxy()
31 {
32 }
33 
WriteParameterEventParams(MessageParcel & data,const std::string networkId,const AudioParamKey key,const std::string & condition,const std::string & value)34 void AudioManagerListenerProxy::WriteParameterEventParams(MessageParcel& data, const std::string networkId,
35     const AudioParamKey key, const std::string& condition, const std::string& value)
36 {
37     data.WriteString(static_cast<std::string>(networkId));
38     data.WriteInt32(static_cast<std::int32_t>(key));
39     data.WriteString(static_cast<std::string>(condition));
40     data.WriteString(static_cast<std::string>(value));
41 }
42 
OnAudioParameterChange(const std::string networkId,const AudioParamKey key,const std::string & condition,const std::string & value)43 void AudioManagerListenerProxy::OnAudioParameterChange(const std::string networkId, const AudioParamKey key,
44     const std::string& condition, const std::string& value)
45 {
46     MessageParcel data;
47     MessageParcel reply;
48     MessageOption option(MessageOption::TF_ASYNC);
49     bool ret = data.WriteInterfaceToken(GetDescriptor());
50     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
51 
52     data.WriteString(static_cast<std::string>(networkId));
53     data.WriteInt32(static_cast<std::int32_t>(key));
54     data.WriteString(static_cast<std::string>(condition));
55     data.WriteString(static_cast<std::string>(value));
56 
57     int error = Remote()->SendRequest(ON_PARAMETER_CHANGED, data, reply, option);
58     if (error != ERR_NONE) {
59         AUDIO_ERR_LOG("ON_PARAMETER_CHANGED failed, error: %{public}d", error);
60     }
61 }
62 
OnCapturerState(bool isActive)63 void AudioManagerListenerProxy::OnCapturerState(bool isActive)
64 {
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option;
68     bool ret = data.WriteInterfaceToken(GetDescriptor());
69     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
70 
71     data.WriteBool(isActive);
72 
73     int error = Remote()->SendRequest(ON_CAPTURER_STATE, data, reply, option);
74     if (error != ERR_NONE) {
75         AUDIO_ERR_LOG("ON_CAPTURER_STATE failed, error: %{public}d", error);
76     }
77 }
78 
OnWakeupClose()79 void AudioManagerListenerProxy::OnWakeupClose()
80 {
81     MessageParcel data;
82     MessageParcel reply;
83     MessageOption option;
84     bool ret = data.WriteInterfaceToken(GetDescriptor());
85     CHECK_AND_RETURN_LOG(ret, "WriteInterfaceToken failed");
86 
87     int error = Remote()->SendRequest(ON_WAKEUP_CLOSE, data, reply, option);
88     if (error != ERR_NONE) {
89         AUDIO_ERR_LOG("ON_WAKEUP_CLOSE failed, error: %{public}d", error);
90     }
91 }
92 
AudioManagerListenerCallback(const sptr<IStandardAudioServerManagerListener> & listener)93 AudioManagerListenerCallback::AudioManagerListenerCallback(const sptr<IStandardAudioServerManagerListener>& listener)
94     : listener_(listener)
95 {
96 }
97 
~AudioManagerListenerCallback()98 AudioManagerListenerCallback::~AudioManagerListenerCallback()
99 {
100 }
101 
OnAudioParameterChange(const std::string networkId,const AudioParamKey key,const std::string & condition,const std::string & value)102 void AudioManagerListenerCallback::OnAudioParameterChange(const std::string networkId, const AudioParamKey key,
103     const std::string& condition, const std::string& value)
104 {
105     if (listener_ != nullptr) {
106         listener_->OnAudioParameterChange(networkId, key, condition, value);
107     }
108 }
109 
OnCapturerState(bool isActive)110 void AudioManagerListenerCallback::OnCapturerState(bool isActive)
111 {
112     if (listener_ != nullptr) {
113         isFirstOnCapturerStateCallbackSent_ = true;
114         listener_->OnCapturerState(isActive);
115     }
116 }
117 
OnWakeupClose()118 void AudioManagerListenerCallback::OnWakeupClose()
119 {
120     if (listener_ != nullptr) {
121         listener_->OnWakeupClose();
122     }
123 }
124 
TrigerFirstOnCapturerStateCallback(bool isActive)125 void AudioManagerListenerCallback::TrigerFirstOnCapturerStateCallback(bool isActive)
126 {
127     if (!isFirstOnCapturerStateCallbackSent_.exchange(true)) {
128         OnCapturerState(isActive);
129     }
130 }
131 } // namespace AudioStandard
132 } // namespace OHOS
133