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 <android-base/thread_annotations.h> 23 24 #include "DriverStubImpl.h" 25 #include "StreamAlsa.h" 26 #include "primary/PrimaryMixer.h" 27 28 namespace aidl::android::hardware::audio::core { 29 30 class StreamPrimary : public StreamAlsa { 31 public: 32 StreamPrimary(StreamContext* context, const Metadata& metadata); 33 34 // Methods of 'DriverInterface'. 35 ::android::status_t init() override; 36 ::android::status_t drain(StreamDescriptor::DrainMode mode) override; 37 ::android::status_t flush() override; 38 ::android::status_t pause() override; 39 ::android::status_t standby() override; 40 ::android::status_t start() override; 41 ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, 42 int32_t* latencyMs) override; 43 ::android::status_t refinePosition(StreamDescriptor::Position* position) override; 44 void shutdown() override; 45 46 // Overridden methods of 'StreamCommonImpl', called on a Binder thread. 47 ndk::ScopedAStatus setConnectedDevices(const ConnectedDevices& devices) override; 48 49 protected: 50 std::vector<alsa::DeviceProfile> getDeviceProfiles() override; 51 bool isStubStream(); 52 53 const bool mIsAsynchronous; 54 int64_t mStartTimeNs = 0; 55 long mFramesSinceStart = 0; 56 bool mSkipNextTransfer = false; 57 58 private: 59 using AlsaDeviceId = std::pair<int, int>; 60 61 static constexpr StreamPrimary::AlsaDeviceId kDefaultCardAndDeviceId{ 62 primary::PrimaryMixer::kAlsaCard, primary::PrimaryMixer::kAlsaDevice}; 63 static constexpr StreamPrimary::AlsaDeviceId kStubDeviceId{ 64 primary::PrimaryMixer::kInvalidAlsaCard, primary::PrimaryMixer::kInvalidAlsaDevice}; 65 66 static AlsaDeviceId getCardAndDeviceId( 67 const std::vector<::aidl::android::media::audio::common::AudioDevice>& devices); 68 static bool useStubStream(bool isInput, 69 const ::aidl::android::media::audio::common::AudioDevice& device); 70 isStubStreamOnWorker()71 bool isStubStreamOnWorker() const { return mCurrAlsaDeviceId == kStubDeviceId; } 72 73 DriverStubImpl mStubDriver; 74 mutable std::mutex mLock; 75 AlsaDeviceId mAlsaDeviceId GUARDED_BY(mLock) = kStubDeviceId; 76 77 // Used by the worker thread only. 78 AlsaDeviceId mCurrAlsaDeviceId = kStubDeviceId; 79 }; 80 81 class StreamInPrimary final : public StreamIn, public StreamPrimary, public StreamInHwGainHelper { 82 public: 83 friend class ndk::SharedRefBase; 84 StreamInPrimary( 85 StreamContext&& context, 86 const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata, 87 const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones); 88 89 private: onClose(StreamDescriptor::State)90 void onClose(StreamDescriptor::State) override { defaultOnClose(); } 91 92 ndk::ScopedAStatus getHwGain(std::vector<float>* _aidl_return) override; 93 ndk::ScopedAStatus setHwGain(const std::vector<float>& in_channelGains) override; 94 }; 95 96 class StreamOutPrimary final : public StreamOut, 97 public StreamPrimary, 98 public StreamOutHwVolumeHelper { 99 public: 100 friend class ndk::SharedRefBase; 101 StreamOutPrimary(StreamContext&& context, 102 const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata, 103 const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>& 104 offloadInfo); 105 106 private: onClose(StreamDescriptor::State)107 void onClose(StreamDescriptor::State) override { defaultOnClose(); } 108 109 ndk::ScopedAStatus getHwVolume(std::vector<float>* _aidl_return) override; 110 ndk::ScopedAStatus setHwVolume(const std::vector<float>& in_channelVolumes) override; 111 }; 112 113 } // namespace aidl::android::hardware::audio::core 114