1 /* 2 * Copyright (c) 2021-2024 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 "AudioClientTrackerCallbackService" 17 #endif 18 19 #include "audio_policy_log.h" 20 #include "audio_client_tracker_callback_service.h" 21 #include "audio_errors.h" 22 23 namespace OHOS { 24 namespace AudioStandard { 25 AudioClientTrackerCallbackService()26AudioClientTrackerCallbackService::AudioClientTrackerCallbackService() 27 { 28 } 29 ~AudioClientTrackerCallbackService()30AudioClientTrackerCallbackService::~AudioClientTrackerCallbackService() 31 { 32 } 33 SetClientTrackerCallback(const std::weak_ptr<AudioClientTracker> & callback)34void AudioClientTrackerCallbackService::SetClientTrackerCallback( 35 const std::weak_ptr<AudioClientTracker> &callback) 36 { 37 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 38 callback_ = callback; 39 } 40 UnsetClientTrackerCallback()41void AudioClientTrackerCallbackService::UnsetClientTrackerCallback() 42 { 43 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 44 callback_.reset(); 45 } 46 MuteStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)47int32_t AudioClientTrackerCallbackService::MuteStreamImpl( 48 const StreamSetStateEventInternal &streamSetStateEventInternal) 49 { 50 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 51 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 52 lock.unlock(); 53 if (cb != nullptr) { 54 cb->MuteStreamImpl(streamSetStateEventInternal); 55 } else { 56 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: MuteStreamImpl callback_ is nullptr"); 57 } 58 return SUCCESS; 59 } 60 UnmuteStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)61int32_t AudioClientTrackerCallbackService::UnmuteStreamImpl( 62 const StreamSetStateEventInternal &streamSetStateEventInternal) 63 { 64 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 65 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 66 lock.unlock(); 67 if (cb != nullptr) { 68 cb->UnmuteStreamImpl(streamSetStateEventInternal); 69 } else { 70 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: UnmuteStreamImpl callback_ is nullptr"); 71 } 72 return SUCCESS; 73 } 74 PausedStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)75int32_t AudioClientTrackerCallbackService::PausedStreamImpl( 76 const StreamSetStateEventInternal &streamSetStateEventInternal) 77 { 78 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 79 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 80 lock.unlock(); 81 if (cb != nullptr) { 82 cb->PausedStreamImpl(streamSetStateEventInternal); 83 } else { 84 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: PausedStreamImpl callback_ is nullptr"); 85 } 86 return SUCCESS; 87 } 88 SetLowPowerVolumeImpl(float volume)89int32_t AudioClientTrackerCallbackService::SetLowPowerVolumeImpl(float volume) 90 { 91 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 92 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 93 lock.unlock(); 94 if (cb != nullptr) { 95 cb->SetLowPowerVolumeImpl(volume); 96 } else { 97 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: SetLowPowerVolumeImpl callback_ is nullptr"); 98 } 99 return SUCCESS; 100 } 101 ResumeStreamImpl(const StreamSetStateEventInternal & streamSetStateEventInternal)102int32_t AudioClientTrackerCallbackService::ResumeStreamImpl( 103 const StreamSetStateEventInternal &streamSetStateEventInternal) 104 { 105 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 106 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 107 lock.unlock(); 108 if (cb != nullptr) { 109 cb->ResumeStreamImpl(streamSetStateEventInternal); 110 } else { 111 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: ResumeStreamImpl callback_ is nullptr"); 112 } 113 return SUCCESS; 114 } 115 SetOffloadModeImpl(int32_t state,bool isAppBack)116int32_t AudioClientTrackerCallbackService::SetOffloadModeImpl(int32_t state, bool isAppBack) 117 { 118 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 119 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 120 lock.unlock(); 121 if (cb != nullptr) { 122 cb->SetOffloadModeImpl(state, isAppBack); 123 } else { 124 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: SetOffloadModeImpl callback_ is nullptr"); 125 } 126 return SUCCESS; 127 } 128 UnsetOffloadModeImpl()129int32_t AudioClientTrackerCallbackService::UnsetOffloadModeImpl() 130 { 131 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 132 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 133 lock.unlock(); 134 if (cb != nullptr) { 135 cb->UnsetOffloadModeImpl(); 136 } else { 137 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: UnsetOffloadModeImpl callback_ is nullptr"); 138 } 139 return SUCCESS; 140 } 141 GetLowPowerVolumeImpl(float & volume)142int32_t AudioClientTrackerCallbackService::GetLowPowerVolumeImpl(float &volume) 143 { 144 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 145 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 146 lock.unlock(); 147 if (cb != nullptr) { 148 cb->GetLowPowerVolumeImpl(volume); 149 } else { 150 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: GetLowPowerVolumeImpl callback_ is nullptr"); 151 } 152 return SUCCESS; 153 } 154 GetSingleStreamVolumeImpl(float & volume)155int32_t AudioClientTrackerCallbackService::GetSingleStreamVolumeImpl(float &volume) 156 { 157 std::unique_lock<std::mutex> lock(clientTrackerMutex_); 158 std::shared_ptr<AudioClientTracker> cb = callback_.lock(); 159 lock.unlock(); 160 if (cb != nullptr) { 161 cb->GetSingleStreamVolumeImpl(volume); 162 } else { 163 AUDIO_WARNING_LOG("AudioClientTrackerCallbackService: GetSingleStreamVolumeImpl callback_ is nullptr"); 164 } 165 return SUCCESS; 166 } 167 168 } // namespace AudioStandard 169 } // namespace OHOS