1 // Copyright (C) 2021 The Android Open Source Project 2 // 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 #include "AudioProxyImpl.h" 16 17 #include <android-base/logging.h> 18 19 #include "AudioProxyError.h" 20 21 namespace audio_proxy::service { 22 AudioProxyImpl()23AudioProxyImpl::AudioProxyImpl() 24 : mDeathRecipient( 25 AIBinder_DeathRecipient_new(AudioProxyImpl::onStreamProviderDied)) {} 26 start(const std::shared_ptr<IStreamProvider> & provider)27ndk::ScopedAStatus AudioProxyImpl::start( 28 const std::shared_ptr<IStreamProvider>& provider) { 29 if (mBusStreamProvider.getStreamProvider()) { 30 LOG(ERROR) << "Service is already started."; 31 return ndk::ScopedAStatus::fromServiceSpecificError( 32 ERROR_STREAM_PROVIDER_EXIST); 33 } 34 35 binder_status_t binder_status = AIBinder_linkToDeath( 36 provider->asBinder().get(), mDeathRecipient.get(), this); 37 if (binder_status != STATUS_OK) { 38 LOG(ERROR) << "Failed to linkToDeath " << static_cast<int>(binder_status); 39 return ndk::ScopedAStatus::fromServiceSpecificError(ERROR_AIDL_FAILURE); 40 } 41 42 mBusStreamProvider.setStreamProvider(provider); 43 return ndk::ScopedAStatus::ok(); 44 } 45 getBusStreamProvider()46BusStreamProvider& AudioProxyImpl::getBusStreamProvider() { 47 return mBusStreamProvider; 48 } 49 resetStreamProvider()50void AudioProxyImpl::resetStreamProvider() { 51 mBusStreamProvider.setStreamProvider(nullptr); 52 } 53 onStreamProviderDied(void * cookie)54void AudioProxyImpl::onStreamProviderDied(void* cookie) { 55 // AudioProxyImpl lives longer than the death handler. The reinterpret_cast 56 // here is safe. 57 auto* audioProxy = reinterpret_cast<AudioProxyImpl*>(cookie); 58 audioProxy->resetStreamProvider(); 59 } 60 61 } // namespace audio_proxy::service 62