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 <optional> 21 #include <thread> 22 #include <vector> 23 24 #include <media/nbaio/MonoPipe.h> 25 #include <media/nbaio/MonoPipeReader.h> 26 27 #include "Stream.h" 28 #include "alsa/Utils.h" 29 30 namespace aidl::android::hardware::audio::core { 31 32 // This class is intended to be used as a base class for implementations 33 // that use TinyAlsa. 34 // This class does not define a complete stream implementation, 35 // and should never be used on its own. Derived classes are expected to 36 // provide necessary overrides for all interface methods omitted here. 37 class StreamAlsa : public StreamCommonImpl { 38 public: 39 StreamAlsa(StreamContext* context, const Metadata& metadata, int readWriteRetries); 40 ~StreamAlsa(); 41 42 // Methods of 'DriverInterface'. 43 ::android::status_t init(DriverCallbackInterface* callback) override; 44 ::android::status_t drain(StreamDescriptor::DrainMode) override; 45 ::android::status_t flush() override; 46 ::android::status_t pause() override; 47 ::android::status_t standby() override; 48 ::android::status_t start() override; 49 ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, 50 int32_t* latencyMs) override; 51 ::android::status_t refinePosition(StreamDescriptor::Position* position) override; 52 void shutdown() override; 53 ndk::ScopedAStatus setGain(float gain) override; 54 55 protected: 56 // Called from 'start' to initialize 'mAlsaDeviceProxies', the vector must be non-empty. 57 virtual std::vector<alsa::DeviceProfile> getDeviceProfiles() = 0; 58 59 const size_t mBufferSizeFrames; 60 const size_t mFrameSizeBytes; 61 const int mSampleRate; 62 const bool mIsInput; 63 const std::optional<struct pcm_config> mConfig; 64 const int mReadWriteRetries; 65 66 private: 67 ::android::NBAIO_Format getPipeFormat() const; 68 ::android::sp<::android::MonoPipe> makeSink(bool writeCanBlock); 69 ::android::sp<::android::MonoPipeReader> makeSource(::android::MonoPipe* pipe); 70 void inputIoThread(size_t idx); 71 void outputIoThread(size_t idx); 72 void teardownIo(); 73 74 std::atomic<float> mGain = 1.0; 75 76 // All fields below are only used on the worker thread. 77 std::vector<alsa::DeviceProxy> mAlsaDeviceProxies; 78 // Only 'libnbaio_mono' is vendor-accessible, thus no access to the multi-reader Pipe. 79 std::vector<::android::sp<::android::MonoPipe>> mSinks; 80 std::vector<::android::sp<::android::MonoPipeReader>> mSources; 81 std::vector<std::thread> mIoThreads; 82 std::atomic<bool> mIoThreadIsRunning = false; // used by all threads 83 }; 84 85 } // namespace aidl::android::hardware::audio::core 86