• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fmq/AidlMessageQueue.h>
20 #include <hardware/audio.h>
21 
22 #include <ctime>
23 #include <mutex>
24 #include <vector>
25 
26 #include "audio_aidl_interfaces.h"
27 #include "audio_ctrl_ack.h"
28 #include "bluetooth_audio_port_impl.h"
29 #include "common/message_loop_thread.h"
30 #include "transport_instance.h"
31 
32 #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED \
33   "persist.bluetooth.bluetooth_audio_hal.disabled"
34 
35 namespace bluetooth {
36 namespace audio {
37 namespace aidl {
38 
39 using ::aidl::android::hardware::bluetooth::audio::AudioCapabilities;
40 using ::aidl::android::hardware::bluetooth::audio::AudioConfiguration;
41 using ::aidl::android::hardware::bluetooth::audio::BluetoothAudioStatus;
42 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioPort;
43 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider;
44 using ::aidl::android::hardware::bluetooth::audio::
45     IBluetoothAudioProviderFactory;
46 using ::aidl::android::hardware::bluetooth::audio::LatencyMode;
47 using ::aidl::android::hardware::bluetooth::audio::PcmConfiguration;
48 
49 using ::aidl::android::hardware::common::fmq::MQDescriptor;
50 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
51 using ::android::AidlMessageQueue;
52 
53 using MqDataType = int8_t;
54 using MqDataMode = SynchronizedReadWrite;
55 using DataMQ = AidlMessageQueue<MqDataType, MqDataMode>;
56 using DataMQDesc = MQDescriptor<MqDataType, MqDataMode>;
57 
58 /***
59  * The client interface connects an IBluetoothTransportInstance to
60  * IBluetoothAudioProvider and helps to route callbacks to
61  * IBluetoothTransportInstance
62  ***/
63 class BluetoothAudioClientInterface {
64  public:
65   BluetoothAudioClientInterface(IBluetoothTransportInstance* instance);
66   virtual ~BluetoothAudioClientInterface() = default;
67 
IsValid()68   bool IsValid() const { return provider_ != nullptr; }
69 
70   std::vector<AudioCapabilities> GetAudioCapabilities() const;
71   static std::vector<AudioCapabilities> GetAudioCapabilities(
72       SessionType session_type);
73   void StreamStarted(const BluetoothAudioCtrlAck& ack);
74 
75   void StreamSuspended(const BluetoothAudioCtrlAck& ack);
76 
77   int StartSession();
78 
79   /***
80    * Renew the connection and usually is used when aidl restarted
81    ***/
82   void RenewAudioProviderAndSession();
83 
84   int EndSession();
85 
86   bool UpdateAudioConfig(const AudioConfiguration& audioConfig);
87 
88   bool SetLowLatencyModeAllowed(bool allowed);
89 
90   void FlushAudioData();
91 
92   static constexpr PcmConfiguration kInvalidPcmConfiguration = {};
93 
94   static bool is_aidl_available();
95 
96  protected:
97   mutable std::mutex internal_mutex_;
98   /***
99    * Helper function to connect to an IBluetoothAudioProvider
100    ***/
101   void FetchAudioProvider();
102 
103   /***
104    * Invoked when binder died
105    ***/
106   static void binderDiedCallbackAidl(void* cookie_ptr);
107 
108   std::shared_ptr<IBluetoothAudioProvider> provider_;
109 
110   std::shared_ptr<IBluetoothAudioProviderFactory> provider_factory_;
111 
112   bool session_started_;
113   std::unique_ptr<DataMQ> data_mq_;
114 
115   ::ndk::ScopedAIBinder_DeathRecipient death_recipient_;
116   // static constexpr const char* kDefaultAudioProviderFactoryInterface =
117   //     "android.hardware.bluetooth.audio.IBluetoothAudioProviderFactory/default";
118   static inline const std::string kDefaultAudioProviderFactoryInterface =
119       std::string() + IBluetoothAudioProviderFactory::descriptor + "/default";
120 
121  private:
122   IBluetoothTransportInstance* transport_;
123   std::vector<AudioCapabilities> capabilities_;
124   bool is_low_latency_allowed_{false};
125 };
126 
127 /***
128  * The client interface connects an IBluetoothTransportInstance to
129  * IBluetoothAudioProvider and helps to route callbacks to
130  * IBluetoothTransportInstance
131  ***/
132 class BluetoothAudioSinkClientInterface : public BluetoothAudioClientInterface {
133  public:
134   /***
135    * Constructs an BluetoothAudioSinkClientInterface to communicate to
136    * BluetoothAudio HAL. |sink| is the implementation for the transport, and
137    * |message_loop| is the thread where callbacks are invoked.
138    ***/
139   BluetoothAudioSinkClientInterface(
140       IBluetoothSinkTransportInstance* sink,
141       bluetooth::common::MessageLoopThread* message_loop);
142   virtual ~BluetoothAudioSinkClientInterface();
143 
GetTransportInstance()144   IBluetoothSinkTransportInstance* GetTransportInstance() const {
145     return sink_;
146   }
147 
148   /***
149    * Read data from audio HAL through fmq
150    ***/
151   size_t ReadAudioData(uint8_t* p_buf, uint32_t len);
152 
153  private:
154   IBluetoothSinkTransportInstance* sink_;
155 
156   static constexpr int kDefaultDataReadTimeoutMs = 10;
157   static constexpr int kDefaultDataReadPollIntervalMs = 1;
158 };
159 
160 class BluetoothAudioSourceClientInterface
161     : public BluetoothAudioClientInterface {
162  public:
163   /***
164    * Constructs an BluetoothAudioSourceClientInterface to communicate to
165    * BluetoothAudio HAL. |source| is the implementation for the transport, and
166    * |message_loop| is the thread where callbacks are invoked.
167    ***/
168   BluetoothAudioSourceClientInterface(
169       IBluetoothSourceTransportInstance* source,
170       bluetooth::common::MessageLoopThread* message_loop);
171   virtual ~BluetoothAudioSourceClientInterface();
172 
GetTransportInstance()173   IBluetoothSourceTransportInstance* GetTransportInstance() const {
174     return source_;
175   }
176 
177   /***
178    * Write data to audio HAL through fmq
179    ***/
180   size_t WriteAudioData(const uint8_t* p_buf, uint32_t len);
181 
182  private:
183   IBluetoothSourceTransportInstance* source_;
184 
185   static constexpr int kDefaultDataWriteTimeoutMs = 10;
186   static constexpr int kDefaultDataWritePollIntervalMs = 1;
187 };
188 
189 }  // namespace aidl
190 }  // namespace audio
191 }  // namespace bluetooth
192