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     // Methods of 'DriverInterface'.
45     ::android::status_t init() override;
46     ::android::status_t drain(StreamDescriptor::DrainMode) override;
47     ::android::status_t flush() override;
48     ::android::status_t pause() override;
49     ::android::status_t standby() override;
50     ::android::status_t start() override;
51     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
52                                  int32_t* latencyMs) override;
53     void shutdown() override;
54 
55     // Overridden methods of 'StreamCommonImpl', called on a Binder thread.
56     ndk::ScopedAStatus updateMetadataCommon(const Metadata& metadata) override;
57     ndk::ScopedAStatus prepareToClose() override;
58     ndk::ScopedAStatus bluetoothParametersUpdated() override;
59 
60   private:
61     const size_t mFrameSizeBytes;
62     const bool mIsInput;
63     const std::weak_ptr<IBluetoothA2dp> mBluetoothA2dp;
64     const std::weak_ptr<IBluetoothLe> mBluetoothLe;
65     const size_t mPreferredDataIntervalUs;
66     mutable std::mutex mLock;
67     // The lock is also used to serialize calls to the proxy.
68     std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl> mBtDeviceProxy
69             GUARDED_BY(mLock);  // proxy may be null if the stream is not connected to a device
70 };
71 
72 class StreamInBluetooth final : public StreamIn, public StreamBluetooth {
73   public:
74     friend class ndk::SharedRefBase;
75 
76     static int32_t getNominalLatencyMs(size_t dataIntervalUs);
77 
78     StreamInBluetooth(
79             StreamContext&& context,
80             const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata,
81             const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones,
82             ModuleBluetooth::BtProfileHandles&& btHandles,
83             const std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl>&
84                     btDeviceProxy,
85             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig);
86 
87   private:
onClose(StreamDescriptor::State)88     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
89     ndk::ScopedAStatus getActiveMicrophones(
90             std::vector<::aidl::android::media::audio::common::MicrophoneDynamicInfo>* _aidl_return)
91             override;
92 };
93 
94 class StreamOutBluetooth final : public StreamOut, public StreamBluetooth {
95   public:
96     friend class ndk::SharedRefBase;
97 
98     static int32_t getNominalLatencyMs(size_t dataIntervalUs);
99 
100     StreamOutBluetooth(
101             StreamContext&& context,
102             const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata,
103             const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>&
104                     offloadInfo,
105             ModuleBluetooth::BtProfileHandles&& btHandles,
106             const std::shared_ptr<::android::bluetooth::audio::aidl::BluetoothAudioPortAidl>&
107                     btDeviceProxy,
108             const ::aidl::android::hardware::bluetooth::audio::PcmConfiguration& pcmConfig);
109 
110   private:
onClose(StreamDescriptor::State)111     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
112 };
113 
114 }  // namespace aidl::android::hardware::audio::core
115