a2dp_sink.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/hal/bluetooth_av_interface.h"
  24. namespace bluetooth {
  25. class A2dpSink : public BluetoothInstance,
  26. private hal::BluetoothAvInterface::A2dpSinkObserver {
  27. public:
  28. // We only allow one instance of this object at a time.
  29. static const int kSingletonInstanceId;
  30. class Delegate {
  31. public:
  32. virtual void OnConnectionState(const std::string& device_address,
  33. int state) = 0;
  34. virtual void OnAudioState(const std::string& device_address, int state) = 0;
  35. virtual void OnAudioConfig(const std::string& device_address,
  36. uint32_t sample_rate, uint8_t channel_count) = 0;
  37. protected:
  38. virtual ~Delegate() = default;
  39. };
  40. ~A2dpSink() override;
  41. void SetDelegate(Delegate* delegate);
  42. // BluetoothInstance implementation:
  43. const Uuid& GetAppIdentifier() const override;
  44. int GetInstanceId() const override;
  45. bool Enable();
  46. void Disable();
  47. bool Connect(const std::string& device_address);
  48. bool Disconnect(const std::string& device_address);
  49. void SetAudioFocusState(int focus_state);
  50. void SetAudioTrackGain(float gain);
  51. private:
  52. friend class A2dpSinkFactory;
  53. explicit A2dpSink(const Uuid& uuid);
  54. // hal::bluetooth::hal::BluetoothAvInterface::Observer implementation:
  55. void ConnectionStateCallback(bluetooth::hal::BluetoothAvInterface* iface,
  56. const RawAddress& bd_addr,
  57. btav_connection_state_t state) override;
  58. void AudioStateCallback(bluetooth::hal::BluetoothAvInterface* iface,
  59. const RawAddress& bd_addr,
  60. btav_audio_state_t state) override;
  61. void AudioConfigCallback(bluetooth::hal::BluetoothAvInterface* iface,
  62. const RawAddress& bd_addr, uint32_t sample_rate,
  63. uint8_t channel_count) override;
  64. // See getters above for documentation.
  65. const Uuid app_identifier_;
  66. std::mutex mutex_;
  67. std::mutex delegate_mutex_;
  68. Delegate* delegate_ = nullptr;
  69. DISALLOW_COPY_AND_ASSIGN(A2dpSink);
  70. };
  71. class A2dpSinkFactory : public BluetoothInstanceFactory {
  72. public:
  73. A2dpSinkFactory();
  74. ~A2dpSinkFactory() override;
  75. // BluetoothInstanceFactory override:
  76. bool RegisterInstance(const Uuid& uuid,
  77. const RegisterCallback& callback) override;
  78. private:
  79. DISALLOW_COPY_AND_ASSIGN(A2dpSinkFactory);
  80. };
  81. } // namespace bluetooth