1 // 2 // Copyright (C) 2017 Google, Inc. 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 <atomic> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include <base/macros.h> 25 26 #include "service/bluetooth_instance.h" 27 #include "service/hal/bluetooth_av_interface.h" 28 29 namespace bluetooth { 30 31 class A2dpSink : public BluetoothInstance, 32 private hal::BluetoothAvInterface::A2dpSinkObserver { 33 public: 34 // We only allow one instance of this object at a time. 35 static const int kSingletonInstanceId; 36 37 class Delegate { 38 public: 39 virtual void OnConnectionState(const std::string& device_address, 40 int state) = 0; 41 virtual void OnAudioState(const std::string& device_address, int state) = 0; 42 virtual void OnAudioConfig(const std::string& device_address, 43 uint32_t sample_rate, uint8_t channel_count) = 0; 44 45 protected: 46 virtual ~Delegate() = default; 47 }; 48 49 ~A2dpSink() override; 50 51 void SetDelegate(Delegate* delegate); 52 53 // BluetoothInstance implementation: 54 const Uuid& GetAppIdentifier() const override; 55 int GetInstanceId() const override; 56 57 bool Enable(); 58 void Disable(); 59 bool Connect(const std::string& device_address); 60 bool Disconnect(const std::string& device_address); 61 void SetAudioFocusState(int focus_state); 62 void SetAudioTrackGain(float gain); 63 64 private: 65 friend class A2dpSinkFactory; 66 67 explicit A2dpSink(const Uuid& uuid); 68 69 // hal::bluetooth::hal::BluetoothAvInterface::Observer implementation: 70 void ConnectionStateCallback(bluetooth::hal::BluetoothAvInterface* iface, 71 const RawAddress& bd_addr, 72 btav_connection_state_t state) override; 73 void AudioStateCallback(bluetooth::hal::BluetoothAvInterface* iface, 74 const RawAddress& bd_addr, 75 btav_audio_state_t state) override; 76 void AudioConfigCallback(bluetooth::hal::BluetoothAvInterface* iface, 77 const RawAddress& bd_addr, uint32_t sample_rate, 78 uint8_t channel_count) override; 79 80 // See getters above for documentation. 81 const Uuid app_identifier_; 82 83 std::mutex mutex_; 84 std::mutex delegate_mutex_; 85 Delegate* delegate_ = nullptr; 86 87 DISALLOW_COPY_AND_ASSIGN(A2dpSink); 88 }; 89 90 class A2dpSinkFactory : public BluetoothInstanceFactory { 91 public: 92 A2dpSinkFactory(); 93 ~A2dpSinkFactory() override; 94 95 // BluetoothInstanceFactory override: 96 bool RegisterInstance(const Uuid& uuid, 97 const RegisterCallback& callback) override; 98 99 private: 100 DISALLOW_COPY_AND_ASSIGN(A2dpSinkFactory); 101 }; 102 103 } // namespace bluetooth 104