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 <optional> 20 #include <vector> 21 22 #include "Stream.h" 23 #include "alsa/Utils.h" 24 25 namespace aidl::android::hardware::audio::core { 26 27 // This class is intended to be used as a base class for implementations 28 // that use TinyAlsa. 29 // This class does not define a complete stream implementation, 30 // and should never be used on its own. Derived classes are expected to 31 // provide necessary overrides for all interface methods omitted here. 32 class StreamAlsa : public StreamCommonImpl { 33 public: 34 StreamAlsa(StreamContext* context, const Metadata& metadata, int readWriteRetries); 35 ~StreamAlsa(); 36 37 // Methods of 'DriverInterface'. 38 ::android::status_t init() override; 39 ::android::status_t drain(StreamDescriptor::DrainMode) override; 40 ::android::status_t flush() override; 41 ::android::status_t pause() override; 42 ::android::status_t standby() override; 43 ::android::status_t start() override; 44 ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, 45 int32_t* latencyMs) override; 46 ::android::status_t refinePosition(StreamDescriptor::Position* position) override; 47 void shutdown() override; 48 ndk::ScopedAStatus setGain(float gain) override; 49 50 protected: 51 // Called from 'start' to initialize 'mAlsaDeviceProxies', the vector must be non-empty. 52 virtual std::vector<alsa::DeviceProfile> getDeviceProfiles() = 0; 53 54 const size_t mBufferSizeFrames; 55 const size_t mFrameSizeBytes; 56 const int mSampleRate; 57 const bool mIsInput; 58 const std::optional<struct pcm_config> mConfig; 59 const int mReadWriteRetries; 60 // All fields below are only used on the worker thread. 61 std::vector<alsa::DeviceProxy> mAlsaDeviceProxies; 62 63 private: 64 std::atomic<float> mGain = 1.0; 65 }; 66 67 } // namespace aidl::android::hardware::audio::core 68