a2dp_source.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once
  17. #include <atomic>
  18. #include <mutex>
  19. #include <string>
  20. #include <vector>
  21. #include <base/macros.h>
  22. #include "service/bluetooth_instance.h"
  23. #include "service/common/bluetooth/a2dp_codec_config.h"
  24. #include "service/hal/bluetooth_av_interface.h"
  25. namespace bluetooth {
  26. class A2dpSource : public BluetoothInstance,
  27. private hal::BluetoothAvInterface::A2dpSourceObserver {
  28. public:
  29. // We only allow one instance of this object at a time.
  30. static const int kSingletonInstanceId;
  31. class Delegate {
  32. public:
  33. virtual void OnConnectionState(const std::string& device_address,
  34. int state) = 0;
  35. virtual void OnAudioState(const std::string& device_address, int state) = 0;
  36. virtual void OnAudioConfig(
  37. const std::string& device_address, A2dpCodecConfig codec_config,
  38. const std::vector<A2dpCodecConfig>& codecs_local_capabilities,
  39. const std::vector<A2dpCodecConfig>& codecs_selectable_capabilities) = 0;
  40. protected:
  41. virtual ~Delegate() = default;
  42. };
  43. ~A2dpSource() override;
  44. void SetDelegate(Delegate* delegate);
  45. // BluetoothInstance implementation:
  46. const Uuid& GetAppIdentifier() const override;
  47. int GetInstanceId() const override;
  48. bool Enable(const std::vector<A2dpCodecConfig>& codec_priorities);
  49. void Disable();
  50. bool Connect(const std::string& device_address);
  51. bool Disconnect(const std::string& device_address);
  52. bool ConfigCodec(const std::string& device_address,
  53. const std::vector<A2dpCodecConfig>& codec_preferences);
  54. private:
  55. friend class A2dpSourceFactory;
  56. explicit A2dpSource(const Uuid& uuid);
  57. // hal::bluetooth::hal::BluetoothAvInterface::Observer implementation:
  58. void ConnectionStateCallback(hal::BluetoothAvInterface* iface,
  59. const RawAddress& bd_addr,
  60. btav_connection_state_t state) override;
  61. void AudioStateCallback(hal::BluetoothAvInterface* iface,
  62. const RawAddress& bd_addr,
  63. btav_audio_state_t state) override;
  64. void AudioConfigCallback(
  65. hal::BluetoothAvInterface* iface, const RawAddress& bd_addr,
  66. const btav_a2dp_codec_config_t& codec_config,
  67. const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
  68. const std::vector<btav_a2dp_codec_config_t>
  69. codecs_selectable_capabilities) override;
  70. // For |GetAppIdentifier|.
  71. const Uuid app_identifier_;
  72. std::mutex mutex_;
  73. // A second mutex is used only for |delegate_|. We cannot use |mutex_| because
  74. // it may cause a deadlock if the caller and Delegate both take the same lock
  75. // 'clock'.
  76. // In that scenario, the caller may take 'clock' first and will try to take
  77. // |mutex_| second. The callback will take |mutex_| first and invoke a
  78. // delegate function which attempts to take 'clock'.
  79. std::mutex delegate_mutex_;
  80. Delegate* delegate_ = nullptr;
  81. DISALLOW_COPY_AND_ASSIGN(A2dpSource);
  82. };
  83. class A2dpSourceFactory : public BluetoothInstanceFactory {
  84. public:
  85. A2dpSourceFactory();
  86. ~A2dpSourceFactory() override;
  87. // BluetoothInstanceFactory override:
  88. bool RegisterInstance(const Uuid& uuid,
  89. const RegisterCallback& callback) override;
  90. private:
  91. DISALLOW_COPY_AND_ASSIGN(A2dpSourceFactory);
  92. };
  93. } // namespace bluetooth