1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "BTAudioProviderA2dpSoftware" 18 19 #include <android-base/logging.h> 20 21 #include "A2dpSoftwareAudioProvider.h" 22 #include "BluetoothAudioSessionReport.h" 23 #include "BluetoothAudioSupportedCodecsDB.h" 24 25 namespace android { 26 namespace hardware { 27 namespace bluetooth { 28 namespace audio { 29 namespace V2_0 { 30 namespace implementation { 31 32 using ::android::bluetooth::audio::BluetoothAudioSessionReport; 33 using ::android::hardware::Void; 34 35 static constexpr uint32_t kPcmFrameSize = 4; // 16 bits per sample / stereo 36 static constexpr uint32_t kPcmFrameCount = 128; 37 static constexpr uint32_t kRtpFrameSize = kPcmFrameSize * kPcmFrameCount; 38 static constexpr uint32_t kRtpFrameCount = 7; // max counts by 1 tick (20ms) 39 static constexpr uint32_t kBufferSize = kRtpFrameSize * kRtpFrameCount; 40 static constexpr uint32_t kBufferCount = 2; // double buffer 41 static constexpr uint32_t kDataMqSize = kBufferSize * kBufferCount; 42 43 A2dpSoftwareAudioProvider::A2dpSoftwareAudioProvider() 44 : BluetoothAudioProvider(), mDataMQ(nullptr) { 45 LOG(INFO) << __func__ << " - size of audio buffer " << kDataMqSize 46 << " byte(s)"; 47 std::unique_ptr<DataMQ> tempDataMQ( 48 new DataMQ(kDataMqSize, /* EventFlag */ true)); 49 if (tempDataMQ && tempDataMQ->isValid()) { 50 mDataMQ = std::move(tempDataMQ); 51 session_type_ = SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH; 52 } else { 53 ALOGE_IF(!tempDataMQ, "failed to allocate data MQ"); 54 ALOGE_IF(tempDataMQ && !tempDataMQ->isValid(), "data MQ is invalid"); 55 } 56 } 57 58 bool A2dpSoftwareAudioProvider::isValid(const SessionType& sessionType) { 59 return (sessionType == session_type_ && mDataMQ && mDataMQ->isValid()); 60 } 61 62 Return<void> A2dpSoftwareAudioProvider::startSession( 63 const sp<IBluetoothAudioPort>& hostIf, 64 const AudioConfiguration& audioConfig, startSession_cb _hidl_cb) { 65 /** 66 * Initialize the audio platform if audioConfiguration is supported. 67 * Save the the IBluetoothAudioPort interface, so that it can be used 68 * later to send stream control commands to the HAL client, based on 69 * interaction with Audio framework. 70 */ 71 if (audioConfig.getDiscriminator() != 72 AudioConfiguration::hidl_discriminator::pcmConfig) { 73 LOG(WARNING) << __func__ 74 << " - Invalid Audio Configuration=" << toString(audioConfig); 75 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION, 76 DataMQ::Descriptor()); 77 return Void(); 78 } else if (!android::bluetooth::audio::IsSoftwarePcmConfigurationValid( 79 audioConfig.pcmConfig())) { 80 LOG(WARNING) << __func__ << " - Unsupported PCM Configuration=" 81 << toString(audioConfig.pcmConfig()); 82 _hidl_cb(BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION, 83 DataMQ::Descriptor()); 84 return Void(); 85 } 86 87 return BluetoothAudioProvider::startSession(hostIf, audioConfig, _hidl_cb); 88 } 89 90 Return<void> A2dpSoftwareAudioProvider::onSessionReady( 91 startSession_cb _hidl_cb) { 92 if (mDataMQ && mDataMQ->isValid()) { 93 BluetoothAudioSessionReport::OnSessionStarted( 94 session_type_, stack_iface_, mDataMQ->getDesc(), audio_config_); 95 _hidl_cb(BluetoothAudioStatus::SUCCESS, *mDataMQ->getDesc()); 96 } else { 97 _hidl_cb(BluetoothAudioStatus::FAILURE, DataMQ::Descriptor()); 98 } 99 return Void(); 100 } 101 102 } // namespace implementation 103 } // namespace V2_0 104 } // namespace audio 105 } // namespace bluetooth 106 } // namespace hardware 107 } // namespace android 108