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 <atomic>
20 #include <mutex>
21 #include <vector>
22 
23 #include <aidl/android/media/audio/common/AudioChannelLayout.h>
24 
25 #include "StreamAlsa.h"
26 
27 namespace aidl::android::hardware::audio::core {
28 
29 class StreamUsb : public StreamAlsa {
30   public:
31     StreamUsb(StreamContext* context, const Metadata& metadata);
32     // Methods of 'DriverInterface'.
33     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
34                                  int32_t* latencyMs) override;
35 
36     // Overridden methods of 'StreamCommonImpl', called on a Binder thread.
37     ndk::ScopedAStatus setConnectedDevices(const ConnectedDevices& devices) override;
38 
39   protected:
40     std::vector<alsa::DeviceProfile> getDeviceProfiles() override;
41 
42     mutable std::mutex mLock;
43     std::vector<alsa::DeviceProfile> mConnectedDeviceProfiles GUARDED_BY(mLock);
44     std::atomic<bool> mConnectedDevicesUpdated = false;
45 };
46 
47 class StreamInUsb final : public StreamIn, public StreamUsb {
48   public:
49     friend class ndk::SharedRefBase;
50     StreamInUsb(
51             StreamContext&& context,
52             const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata,
53             const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones);
54 
55   private:
onClose(StreamDescriptor::State)56     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
57     ndk::ScopedAStatus getActiveMicrophones(
58             std::vector<::aidl::android::media::audio::common::MicrophoneDynamicInfo>* _aidl_return)
59             override;
60 };
61 
62 class StreamOutUsb final : public StreamOut, public StreamUsb, public StreamOutHwVolumeHelper {
63   public:
64     friend class ndk::SharedRefBase;
65     StreamOutUsb(StreamContext&& context,
66                  const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata,
67                  const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>&
68                          offloadInfo);
69 
70   private:
onClose(StreamDescriptor::State)71     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
72     ndk::ScopedAStatus getHwVolume(std::vector<float>* _aidl_return) override;
73     ndk::ScopedAStatus setHwVolume(const std::vector<float>& in_channelVolumes) override;
74 };
75 
76 }  // namespace aidl::android::hardware::audio::core
77