xref: /aosp_15_r20/hardware/interfaces/audio/aidl/default/include/core-impl/StreamBluetooth.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2023 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 
17 #pragma once
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include <aidl/android/hardware/audio/core/IBluetooth.h>
23 #include <aidl/android/hardware/audio/core/IBluetoothA2dp.h>
24 #include <aidl/android/hardware/audio/core/IBluetoothLe.h>
25 
26 #include "core-impl/DevicePortProxy.h"
27 #include "core-impl/ModuleBluetooth.h"
28 #include "core-impl/Stream.h"
29 
30 namespace aidl::android::hardware::audio::core {
31 
32 class StreamBluetooth : public StreamCommonImpl {
33   public:
34     static bool checkConfigParams(
35             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig,
36             const ::aidl::android::media::audio::common::AudioConfigBase& config);
37 
38     StreamBluetooth(
39             StreamContext* context, const Metadata& metadata,
40             ModuleBluetooth::BtProfileHandles&& btHandles,
41             const std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl>&
42                     btDeviceProxy,
43             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig);
44     ~StreamBluetooth();
45 
46     // Methods of 'DriverInterface'.
47     ::android::status_t init() override;
48     ::android::status_t drain(StreamDescriptor::DrainMode) override;
49     ::android::status_t flush() override;
50     ::android::status_t pause() override;
51     ::android::status_t standby() override;
52     ::android::status_t start() override;
53     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
54                                  int32_t* latencyMs) override;
55     void shutdown() override;
56 
57     // Overridden methods of 'StreamCommonImpl', called on a Binder thread.
58     ndk::ScopedAStatus updateMetadataCommon(const Metadata& metadata) override;
59     ndk::ScopedAStatus prepareToClose() override;
60     ndk::ScopedAStatus bluetoothParametersUpdated() override;
61 
62   private:
63     const size_t mFrameSizeBytes;
64     const bool mIsInput;
65     const std::weak_ptr<IBluetoothA2dp> mBluetoothA2dp;
66     const std::weak_ptr<IBluetoothLe> mBluetoothLe;
67     const size_t mPreferredDataIntervalUs;
68     mutable std::mutex mLock;
69     // The lock is also used to serialize calls to the proxy.
70     std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl> mBtDeviceProxy
71             GUARDED_BY(mLock);  // proxy may be null if the stream is not connected to a device
72 };
73 
74 class StreamInBluetooth final : public StreamIn, public StreamBluetooth {
75   public:
76     friend class ndk::SharedRefBase;
77 
78     static int32_t getNominalLatencyMs(size_t dataIntervalUs);
79 
80     StreamInBluetooth(
81             StreamContext&& context,
82             const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata,
83             const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones,
84             ModuleBluetooth::BtProfileHandles&& btHandles,
85             const std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl>&
86                     btDeviceProxy,
87             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig);
88 
89   private:
onClose(StreamDescriptor::State)90     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
91     ndk::ScopedAStatus getActiveMicrophones(
92             std::vector<::aidl::android::media::audio::common::MicrophoneDynamicInfo>* _aidl_return)
93             override;
94 };
95 
96 class StreamOutBluetooth final : public StreamOut, public StreamBluetooth {
97   public:
98     friend class ndk::SharedRefBase;
99 
100     static int32_t getNominalLatencyMs(size_t dataIntervalUs);
101 
102     StreamOutBluetooth(
103             StreamContext&& context,
104             const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata,
105             const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>&
106                     offloadInfo,
107             ModuleBluetooth::BtProfileHandles&& btHandles,
108             const std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl>&
109                     btDeviceProxy,
110             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig);
111 
112   private:
onClose(StreamDescriptor::State)113     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
114 };
115 
116 }  // namespace aidl::android::hardware::audio::core
117