device_port_proxy.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <android/hardware/bluetooth/audio/2.0/types.h>
  18. #include <hardware/audio.h>
  19. #include <condition_variable>
  20. #include <mutex>
  21. #include <unordered_map>
  22. enum class BluetoothStreamState : uint8_t;
  23. namespace android {
  24. namespace bluetooth {
  25. namespace audio {
  26. // Proxy for Bluetooth Audio HW Module to communicate with Bluetooth Audio
  27. // Session Control. All methods are not thread safe, so users must acquire a
  28. // lock. Note: currently, in stream_apis.cc, if GetState() is only used for
  29. // verbose logging, it is not locked, so the state may not be synchronized.
  30. class BluetoothAudioPortOut {
  31. public:
  32. BluetoothAudioPortOut();
  33. ~BluetoothAudioPortOut() = default;
  34. // Fetch output control / data path of BluetoothAudioPortOut and setup
  35. // callbacks into BluetoothAudioProvider. If SetUp() returns false, the audio
  36. // HAL must delete this BluetoothAudioPortOut and return EINVAL to caller
  37. bool SetUp(audio_devices_t devices);
  38. // Unregister this BluetoothAudioPortOut from BluetoothAudioSessionControl.
  39. // Audio HAL must delete this BluetoothAudioPortOut after calling this.
  40. void TearDown();
  41. // When the Audio framework / HAL tries to query audio config about format,
  42. // channel mask and sample rate, it uses this function to fetch from the
  43. // Bluetooth stack
  44. bool LoadAudioConfig(audio_config_t* audio_cfg) const;
  45. // WAR to support Mono mode / 16 bits per sample
  46. void ForcePcmStereoToMono(bool force) {
  47. is_stereo_to_mono_ = force;
  48. }
  49. // When the Audio framework / HAL wants to change the stream state, it invokes
  50. // these 3 functions to control the Bluetooth stack (Audio Control Path).
  51. // Note: Both Start() and Suspend() will return ture when there are no errors.
  52. // Called by Audio framework / HAL to start the stream
  53. bool Start();
  54. // Called by Audio framework / HAL to suspend the stream
  55. bool Suspend();
  56. // Called by Audio framework / HAL to stop the stream
  57. void Stop();
  58. // The audio data path to the Bluetooth stack (Software encoding)
  59. size_t WriteData(const void* buffer, size_t bytes) const;
  60. // Called by the Audio framework / HAL to fetch informaiton about audio frames
  61. // presented to an external sink.
  62. bool GetPresentationPosition(uint64_t* delay_ns, uint64_t* bytes,
  63. timespec* timestamp) const;
  64. // Called by the Audio framework / HAL when the metadata of the stream's
  65. // source has been changed.
  66. void UpdateMetadata(const source_metadata* source_metadata) const;
  67. // Return the current BluetoothStreamState
  68. BluetoothStreamState GetState() const;
  69. // Set the current BluetoothStreamState
  70. void SetState(BluetoothStreamState state);
  71. private:
  72. BluetoothStreamState state_;
  73. ::android::hardware::bluetooth::audio::V2_0::SessionType session_type_;
  74. uint16_t cookie_;
  75. mutable std::mutex cv_mutex_;
  76. std::condition_variable internal_cv_;
  77. // WR to support Mono: True if fetching Stereo and mixing into Mono
  78. bool is_stereo_to_mono_ = false;
  79. // Check and initialize session type for |devices| If failed, this
  80. // BluetoothAudioPortOut is not initialized and must be deleted.
  81. bool init_session_type(audio_devices_t device);
  82. bool in_use() const;
  83. bool CondwaitState(BluetoothStreamState state);
  84. void ControlResultHandler(
  85. const ::android::hardware::bluetooth::audio::V2_0::Status& status);
  86. void SessionChangedHandler();
  87. };
  88. } // namespace audio
  89. } // namespace bluetooth
  90. } // namespace android