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     // Methods of 'DriverInterface'.
36     ::android::status_t init() override;
37     ::android::status_t drain(StreamDescriptor::DrainMode) override;
38     ::android::status_t flush() override;
39     ::android::status_t pause() override;
40     ::android::status_t standby() override;
41     ::android::status_t start() override;
42     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
43                                  int32_t* latencyMs) override;
44     ::android::status_t refinePosition(StreamDescriptor::Position* position) override;
45     void shutdown() override;
46 
47   protected:
48     // Called from 'start' to initialize 'mAlsaDeviceProxies', the vector must be non-empty.
49     virtual std::vector<alsa::DeviceProfile> getDeviceProfiles() = 0;
50 
51     const size_t mBufferSizeFrames;
52     const size_t mFrameSizeBytes;
53     const int mSampleRate;
54     const bool mIsInput;
55     const std::optional<struct pcm_config> mConfig;
56     const int mReadWriteRetries;
57     // All fields below are only used on the worker thread.
58     std::vector<alsa::DeviceProxy> mAlsaDeviceProxies;
59 };
60 
61 }  // namespace aidl::android::hardware::audio::core
62