1 /* 2 * Copyright 2022 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 #pragma once 18 19 #include <android/hardware/bluetooth/audio/2.1/types.h> 20 #include <hardware/audio.h> 21 22 #include <condition_variable> 23 #include <mutex> 24 #include <unordered_map> 25 26 #include "device_port_proxy.h" 27 28 enum class BluetoothStreamState : uint8_t; 29 30 namespace android { 31 namespace bluetooth { 32 namespace audio { 33 namespace hidl { 34 35 using SessionType_2_1 = 36 ::android::hardware::bluetooth::audio::V2_1::SessionType; 37 38 class BluetoothAudioPortHidl : public BluetoothAudioPort { 39 public: 40 BluetoothAudioPortHidl(); 41 virtual ~BluetoothAudioPortHidl() = default; 42 43 bool SetUp(audio_devices_t devices) override; 44 45 void TearDown() override; 46 ForcePcmStereoToMono(bool force)47 void ForcePcmStereoToMono(bool force) override { is_stereo_to_mono_ = force; } 48 49 bool Start() override; 50 51 bool Suspend() override; 52 53 void Stop() override; 54 55 bool GetPresentationPosition(uint64_t* delay_ns, uint64_t* bytes, 56 timespec* timestamp) const override; 57 58 void UpdateSourceMetadata( 59 const source_metadata* source_metadata) const override; 60 61 BluetoothStreamState GetState() const override; 62 63 void SetState(BluetoothStreamState state) override; 64 IsA2dp()65 bool IsA2dp() const override { 66 return session_type_hidl_ == 67 SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH || 68 session_type_hidl_ == 69 SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH; 70 } 71 IsLeAudio()72 bool IsLeAudio() const override { 73 return session_type_hidl_ == 74 SessionType_2_1::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH || 75 session_type_hidl_ == 76 SessionType_2_1::LE_AUDIO_SOFTWARE_DECODED_DATAPATH || 77 session_type_hidl_ == 78 SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH || 79 session_type_hidl_ == 80 SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH; 81 } 82 83 bool GetPreferredDataIntervalUs(size_t* interval_us) const override; 84 85 protected: 86 SessionType_2_1 session_type_hidl_; 87 uint16_t cookie_; 88 BluetoothStreamState state_; 89 // WR to support Mono: True if fetching Stereo and mixing into Mono 90 bool is_stereo_to_mono_ = false; 91 92 bool in_use() const; 93 94 private: 95 mutable std::mutex cv_mutex_; 96 std::condition_variable internal_cv_; 97 98 bool init_session_type(audio_devices_t device); 99 100 bool CondwaitState(BluetoothStreamState state); 101 102 void ControlResultHandler( 103 const ::android::hardware::bluetooth::audio::V2_0::Status& status); 104 105 void SessionChangedHandler(); 106 }; 107 108 class BluetoothAudioPortHidlOut : public BluetoothAudioPortHidl { 109 public: 110 ~BluetoothAudioPortHidlOut(); 111 112 size_t WriteData(const void* buffer, size_t bytes) const override; 113 bool LoadAudioConfig(audio_config_t* audio_cfg) const override; 114 }; 115 116 class BluetoothAudioPortHidlIn : public BluetoothAudioPortHidl { 117 public: 118 ~BluetoothAudioPortHidlIn(); 119 120 size_t ReadData(void* buffer, size_t bytes) const override; 121 bool LoadAudioConfig(audio_config_t* audio_cfg) const override; 122 }; 123 124 } // namespace hidl 125 } // namespace audio 126 } // namespace bluetooth 127 } // namespace android