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/common/bluetooth/a2dp_codec_config.h"
28 #include "service/hal/bluetooth_av_interface.h"
32 class A2dpSource : public BluetoothInstance,
33 private hal::BluetoothAvInterface::A2dpSourceObserver {
35 // We only allow one instance of this object at a time.
36 static const int kSingletonInstanceId;
40 virtual void OnConnectionState(const std::string& device_address,
42 virtual void OnAudioState(const std::string& device_address, int state) = 0;
43 virtual void OnAudioConfig(
44 const std::string& device_address, A2dpCodecConfig codec_config,
45 const std::vector<A2dpCodecConfig>& codecs_local_capabilities,
46 const std::vector<A2dpCodecConfig>& codecs_selectable_capabilities) = 0;
49 virtual ~Delegate() = default;
52 ~A2dpSource() override;
54 void SetDelegate(Delegate* delegate);
56 // BluetoothInstance implementation:
57 const Uuid& GetAppIdentifier() const override;
58 int GetInstanceId() const override;
60 bool Enable(const std::vector<A2dpCodecConfig>& codec_priorities);
62 bool Connect(const std::string& device_address);
63 bool Disconnect(const std::string& device_address);
64 bool ConfigCodec(const std::string& device_address,
65 const std::vector<A2dpCodecConfig>& codec_preferences);
68 friend class A2dpSourceFactory;
70 explicit A2dpSource(const Uuid& uuid);
72 // hal::bluetooth::hal::BluetoothAvInterface::Observer implementation:
73 void ConnectionStateCallback(hal::BluetoothAvInterface* iface,
74 const RawAddress& bd_addr,
75 btav_connection_state_t state) override;
76 void AudioStateCallback(hal::BluetoothAvInterface* iface,
77 const RawAddress& bd_addr,
78 btav_audio_state_t state) override;
79 void AudioConfigCallback(
80 hal::BluetoothAvInterface* iface, const RawAddress& bd_addr,
81 const btav_a2dp_codec_config_t& codec_config,
82 const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
83 const std::vector<btav_a2dp_codec_config_t>
84 codecs_selectable_capabilities) override;
86 // For |GetAppIdentifier|.
87 const Uuid app_identifier_;
91 // A second mutex is used only for |delegate_|. We cannot use |mutex_| because
92 // it may cause a deadlock if the caller and Delegate both take the same lock
94 // In that scenario, the caller may take 'clock' first and will try to take
95 // |mutex_| second. The callback will take |mutex_| first and invoke a
96 // delegate function which attempts to take 'clock'.
97 std::mutex delegate_mutex_;
98 Delegate* delegate_ = nullptr;
100 DISALLOW_COPY_AND_ASSIGN(A2dpSource);
103 class A2dpSourceFactory : public BluetoothInstanceFactory {
106 ~A2dpSourceFactory() override;
108 // BluetoothInstanceFactory override:
109 bool RegisterInstance(const Uuid& uuid,
110 const RegisterCallback& callback) override;
113 DISALLOW_COPY_AND_ASSIGN(A2dpSourceFactory);
116 } // namespace bluetooth