client_interface.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2019 The Android Open Source Project
  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 <time.h>
  18. #include <mutex>
  19. #include <android/hardware/bluetooth/audio/2.0/IBluetoothAudioProvider.h>
  20. #include <android/hardware/bluetooth/audio/2.0/types.h>
  21. #include <fmq/MessageQueue.h>
  22. #include <hardware/audio.h>
  23. #include "common/message_loop_thread.h"
  24. #define BLUETOOTH_AUDIO_HAL_PROP_DISABLED "persist.bluetooth.bluetooth_audio_hal.disabled"
  25. namespace bluetooth {
  26. namespace audio {
  27. using ::android::hardware::bluetooth::audio::V2_0::AudioCapabilities;
  28. using ::android::hardware::bluetooth::audio::V2_0::AudioConfiguration;
  29. using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
  30. using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
  31. using ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration;
  32. using ::android::hardware::bluetooth::audio::V2_0::CodecType;
  33. using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioProvider;
  34. using ::android::hardware::bluetooth::audio::V2_0::PcmParameters;
  35. using ::android::hardware::bluetooth::audio::V2_0::SampleRate;
  36. using ::android::hardware::bluetooth::audio::V2_0::SessionType;
  37. using ::android::hardware::bluetooth::audio::V2_0::TimeSpec;
  38. using BluetoothAudioStatus =
  39. ::android::hardware::bluetooth::audio::V2_0::Status;
  40. enum class BluetoothAudioCtrlAck : uint8_t {
  41. SUCCESS_FINISHED = 0,
  42. PENDING,
  43. FAILURE_UNSUPPORTED,
  44. FAILURE_BUSY,
  45. FAILURE_DISCONNECTING,
  46. FAILURE
  47. };
  48. std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack);
  49. inline BluetoothAudioStatus BluetoothAudioCtrlAckToHalStatus(
  50. const BluetoothAudioCtrlAck& ack) {
  51. switch (ack) {
  52. case BluetoothAudioCtrlAck::SUCCESS_FINISHED:
  53. return BluetoothAudioStatus::SUCCESS;
  54. case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED:
  55. return BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION;
  56. case BluetoothAudioCtrlAck::PENDING:
  57. return BluetoothAudioStatus::FAILURE;
  58. case BluetoothAudioCtrlAck::FAILURE_BUSY:
  59. return BluetoothAudioStatus::FAILURE;
  60. case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING:
  61. return BluetoothAudioStatus::FAILURE;
  62. default:
  63. return BluetoothAudioStatus::FAILURE;
  64. }
  65. }
  66. // An IBluetoothTransportInstance needs to be implemented by a Bluetooth audio
  67. // transport, such as A2DP or Hearing Aid, to handle callbacks from Audio HAL.
  68. class IBluetoothTransportInstance {
  69. public:
  70. IBluetoothTransportInstance(SessionType sessionType,
  71. AudioConfiguration audioConfig)
  72. : session_type_(sessionType), audio_config_(std::move(audioConfig)){};
  73. virtual ~IBluetoothTransportInstance() = default;
  74. SessionType GetSessionType() const { return session_type_; }
  75. AudioConfiguration GetAudioConfiguration() const { return audio_config_; }
  76. void UpdateAudioConfiguration(const AudioConfiguration& audio_config) {
  77. audio_config_ = audio_config;
  78. }
  79. virtual BluetoothAudioCtrlAck StartRequest() = 0;
  80. virtual BluetoothAudioCtrlAck SuspendRequest() = 0;
  81. virtual void StopRequest() = 0;
  82. virtual bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
  83. uint64_t* total_bytes_readed,
  84. timespec* data_position) = 0;
  85. virtual void MetadataChanged(const source_metadata_t& source_metadata) = 0;
  86. // Invoked when the transport is requested to reset presentation position
  87. virtual void ResetPresentationPosition() = 0;
  88. // Invoked when the transport is requested to log bytes read
  89. virtual void LogBytesRead(size_t bytes_readed) = 0;
  90. private:
  91. const SessionType session_type_;
  92. AudioConfiguration audio_config_;
  93. };
  94. // common object is shared between different kind of SessionType
  95. class BluetoothAudioDeathRecipient;
  96. // The client interface connects an IBluetoothTransportInstance to
  97. // IBluetoothAudioProvider and helps to route callbacks to
  98. // IBluetoothTransportInstance
  99. class BluetoothAudioClientInterface {
  100. public:
  101. // Constructs an BluetoothAudioClientInterface to communicate to
  102. // BluetoothAudio HAL. |sink| is the implementation for the transport, and
  103. // |message_loop| is the thread where callbacks are invoked.
  104. BluetoothAudioClientInterface(
  105. IBluetoothTransportInstance* sink,
  106. bluetooth::common::MessageLoopThread* message_loop);
  107. ~BluetoothAudioClientInterface();
  108. bool IsValid() const {
  109. return provider_ != nullptr;
  110. }
  111. std::vector<AudioCapabilities> GetAudioCapabilities() const;
  112. bool UpdateAudioConfig(const AudioConfiguration& audioConfig);
  113. int StartSession();
  114. void StreamStarted(const BluetoothAudioCtrlAck& ack);
  115. void StreamSuspended(const BluetoothAudioCtrlAck& ack);
  116. int EndSession();
  117. // Read data from audio HAL through fmq
  118. size_t ReadAudioData(uint8_t* p_buf, uint32_t len);
  119. // Write data to audio HAL through fmq
  120. size_t WriteAudioData(uint8_t* p_buf, uint32_t len);
  121. // Renew the connection and usually is used when HIDL restarted
  122. void RenewAudioProviderAndSession();
  123. static constexpr PcmParameters kInvalidPcmConfiguration = {
  124. .sampleRate = SampleRate::RATE_UNKNOWN,
  125. .bitsPerSample = BitsPerSample::BITS_UNKNOWN,
  126. .channelMode = ChannelMode::UNKNOWN};
  127. private:
  128. // Helper function to connect to an IBluetoothAudioProvider
  129. void fetch_audio_provider();
  130. mutable std::mutex internal_mutex_;
  131. IBluetoothTransportInstance* sink_;
  132. android::sp<IBluetoothAudioProvider> provider_;
  133. std::vector<AudioCapabilities> capabilities_;
  134. bool session_started_;
  135. std::unique_ptr<::android::hardware::MessageQueue<
  136. uint8_t, ::android::hardware::kSynchronizedReadWrite>>
  137. mDataMQ;
  138. android::sp<BluetoothAudioDeathRecipient> death_recipient_;
  139. };
  140. } // namespace audio
  141. } // namespace bluetooth