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 "AudioClientTrackerCallbackStub"
17 #endif
18
19 #include "audio_client_tracker_callback_stub.h"
20 #include "audio_policy_log.h"
21
22 namespace OHOS {
23 namespace AudioStandard {
AudioClientTrackerCallbackStub()24 AudioClientTrackerCallbackStub::AudioClientTrackerCallbackStub()
25 {
26 }
27
~AudioClientTrackerCallbackStub()28 AudioClientTrackerCallbackStub::~AudioClientTrackerCallbackStub()
29 {
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int AudioClientTrackerCallbackStub::OnRemoteRequest(
33 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35 CHECK_AND_RETURN_RET_LOG(data.ReadInterfaceToken() == GetDescriptor(), -1,
36 "AudioClientTrackerCallbackStub: ReadInterfaceToken failed");
37
38 switch (code) {
39 case PAUSEDSTREAM: {
40 StreamSetStateEventInternal sreamSetStateEventInternal = {};
41 sreamSetStateEventInternal.streamSetState= static_cast<StreamSetState>(data.ReadInt32());
42 sreamSetStateEventInternal.streamUsage = static_cast<StreamUsage>(data.ReadInt32());
43 PausedStreamImpl(sreamSetStateEventInternal);
44 return AUDIO_OK;
45 }
46 case RESUMESTREAM: {
47 StreamSetStateEventInternal sreamSetStateEventInternal = {};
48 sreamSetStateEventInternal.streamSetState= static_cast<StreamSetState>(data.ReadInt32());
49 sreamSetStateEventInternal.streamUsage = static_cast<StreamUsage>(data.ReadInt32());
50 ResumeStreamImpl(sreamSetStateEventInternal);
51 return AUDIO_OK;
52 }
53 case SETLOWPOWERVOL: {
54 float volume = data.ReadFloat();
55 SetLowPowerVolumeImpl(volume);
56 return AUDIO_OK;
57 }
58 case GETLOWPOWERVOL: {
59 float volume;
60 GetLowPowerVolumeImpl(volume);
61 reply.WriteFloat(volume);
62 return AUDIO_OK;
63 }
64 case GETSINGLESTREAMVOL: {
65 float volume;
66 GetSingleStreamVolumeImpl(volume);
67 reply.WriteFloat(volume);
68 return AUDIO_OK;
69 }
70 case SETOFFLOADMODE:
71 case UNSETOFFLOADMODE: {
72 return OffloadRemoteRequest(code, data, reply, option);
73 }
74 default: {
75 AUDIO_ERR_LOG("default case, need check AudioListenerStub");
76 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
77 }
78 }
79
80 return 0;
81 }
82
OffloadRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)83 int AudioClientTrackerCallbackStub::OffloadRemoteRequest(
84 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
85 {
86 if (code == SETOFFLOADMODE) {
87 int32_t state = data.ReadInt32();
88 bool isAppBack = data.ReadBool();
89 SetOffloadModeImpl(state, isAppBack);
90 return AUDIO_OK;
91 }
92
93 if (code == UNSETOFFLOADMODE) {
94 UnsetOffloadModeImpl();
95 return AUDIO_OK;
96 }
97 return 0;
98 }
99
SetClientTrackerCallback(const std::weak_ptr<AudioClientTracker> & callback)100 void AudioClientTrackerCallbackStub::SetClientTrackerCallback(
101 const std::weak_ptr<AudioClientTracker> &callback)
102 {
103 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
104 callback_ = callback;
105 }
106
UnsetClientTrackerCallback()107 void AudioClientTrackerCallbackStub::UnsetClientTrackerCallback()
108 {
109 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
110 callback_.reset();
111 }
112
PausedStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)113 void AudioClientTrackerCallbackStub::PausedStreamImpl(
114 const StreamSetStateEventInternal &streamSetStateEventInternal)
115 {
116 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
117 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
118 lock.unlock();
119 if (cb != nullptr) {
120 cb->PausedStreamImpl(streamSetStateEventInternal);
121 } else {
122 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: PausedStreamImpl callback_ is nullptr");
123 }
124 }
125
SetLowPowerVolumeImpl(float volume)126 void AudioClientTrackerCallbackStub::SetLowPowerVolumeImpl(float volume)
127 {
128 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
129 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
130 lock.unlock();
131 if (cb != nullptr) {
132 cb->SetLowPowerVolumeImpl(volume);
133 } else {
134 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: SetLowPowerVolumeImpl callback_ is nullptr");
135 }
136 }
137
ResumeStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)138 void AudioClientTrackerCallbackStub::ResumeStreamImpl(
139 const StreamSetStateEventInternal &streamSetStateEventInternal)
140 {
141 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
142 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
143 lock.unlock();
144 if (cb != nullptr) {
145 cb->ResumeStreamImpl(streamSetStateEventInternal);
146 } else {
147 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: ResumeStreamImpl callback_ is nullptr");
148 }
149 }
150
SetOffloadModeImpl(int32_t state,bool isAppBack)151 void AudioClientTrackerCallbackStub::SetOffloadModeImpl(int32_t state, bool isAppBack)
152 {
153 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
154 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
155 lock.unlock();
156 if (cb != nullptr) {
157 cb->SetOffloadModeImpl(state, isAppBack);
158 } else {
159 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: SetOffloadModeImpl callback_ is nullptr");
160 }
161 }
162
UnsetOffloadModeImpl()163 void AudioClientTrackerCallbackStub::UnsetOffloadModeImpl()
164 {
165 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
166 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
167 lock.unlock();
168 if (cb != nullptr) {
169 cb->UnsetOffloadModeImpl();
170 } else {
171 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: UnsetOffloadModeImpl callback_ is nullptr");
172 }
173 }
174
GetLowPowerVolumeImpl(float & volume)175 void AudioClientTrackerCallbackStub::GetLowPowerVolumeImpl(float &volume)
176 {
177 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
178 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
179 lock.unlock();
180 if (cb != nullptr) {
181 cb->GetLowPowerVolumeImpl(volume);
182 } else {
183 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: GetLowPowerVolumeImpl callback_ is nullptr");
184 }
185 }
186
GetSingleStreamVolumeImpl(float & volume)187 void AudioClientTrackerCallbackStub::GetSingleStreamVolumeImpl(float &volume)
188 {
189 std::unique_lock<std::mutex> lock(clientTrackerMutex_);
190 std::shared_ptr<AudioClientTracker> cb = callback_.lock();
191 lock.unlock();
192 if (cb != nullptr) {
193 cb->GetSingleStreamVolumeImpl(volume);
194 } else {
195 AUDIO_WARNING_LOG("AudioClientTrackerCallbackStub: GetSingleStreamVolumeImpl callback_ is nullptr");
196 }
197 }
198 } // namespace AudioStandard
199 } // namespace OHOS
200