2 // Copyright (C) 2017 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <base/macros.h>
26 #include "service/bluetooth_instance.h"
27 #include "service/hal/bluetooth_av_interface.h"
31 class A2dpSink : public BluetoothInstance,
32 private hal::BluetoothAvInterface::A2dpSinkObserver {
34 // We only allow one instance of this object at a time.
35 static const int kSingletonInstanceId;
39 virtual void OnConnectionState(const std::string& device_address,
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;
46 virtual ~Delegate() = default;
51 void SetDelegate(Delegate* delegate);
53 // BluetoothInstance implementation:
54 const Uuid& GetAppIdentifier() const override;
55 int GetInstanceId() const override;
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);
65 friend class A2dpSinkFactory;
67 explicit A2dpSink(const Uuid& uuid);
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;
80 // See getters above for documentation.
81 const Uuid app_identifier_;
84 std::mutex delegate_mutex_;
85 Delegate* delegate_ = nullptr;
87 DISALLOW_COPY_AND_ASSIGN(A2dpSink);
90 class A2dpSinkFactory : public BluetoothInstanceFactory {
93 ~A2dpSinkFactory() override;
95 // BluetoothInstanceFactory override:
96 bool RegisterInstance(const Uuid& uuid,
97 const RegisterCallback& callback) override;
100 DISALLOW_COPY_AND_ASSIGN(A2dpSinkFactory);
103 } // namespace bluetooth