• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <string>
21 
22 #include "core-impl/DriverStubImpl.h"
23 #include "core-impl/Stream.h"
24 
25 namespace aidl::android::hardware::audio::core {
26 
27 namespace mmap {
28 
29 struct DspSimulatorState {
30     const bool isInput;
31     const int sampleRate;
32     const int frameSizeBytes;
33     const size_t bufferSizeBytes;
34     std::mutex lock;
35     // The lock is also used to prevent un-mapping while the memory is in use.
36     uint8_t* sharedMemory GUARDED_BY(lock) = nullptr;
37     StreamDescriptor::Position mmapPos GUARDED_BY(lock);
38 };
39 
40 class DspSimulatorLogic : public ::android::hardware::audio::common::StreamLogic {
41   protected:
DspSimulatorLogic(DspSimulatorState & sharedState)42     explicit DspSimulatorLogic(DspSimulatorState& sharedState) : mSharedState(sharedState) {}
43     std::string init() override;
44     Status cycle() override;
45 
46   private:
47     DspSimulatorState& mSharedState;
48     uint32_t mCycleDurationUs = 0;
49     uint8_t* mMemBegin = nullptr;
50     uint8_t* mMemPos = nullptr;
51     int64_t mLastFrames = 0;
52 };
53 
54 class DspSimulatorWorker
55     : public ::android::hardware::audio::common::StreamWorker<DspSimulatorLogic> {
56   public:
DspSimulatorWorker(DspSimulatorState & sharedState)57     explicit DspSimulatorWorker(DspSimulatorState& sharedState)
58         : ::android::hardware::audio::common::StreamWorker<DspSimulatorLogic>(sharedState) {}
59 };
60 
61 }  // namespace mmap
62 
63 class DriverMmapStubImpl : public DriverStubImpl {
64   public:
65     explicit DriverMmapStubImpl(const StreamContext& context);
66     ::android::status_t init(DriverCallbackInterface* callback) override;
67     ::android::status_t drain(StreamDescriptor::DrainMode drainMode) override;
68     ::android::status_t pause() override;
69     ::android::status_t start() override;
70     ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
71                                  int32_t* latencyMs) override;
72     void shutdown() override;
73     ::android::status_t refinePosition(StreamDescriptor::Position* position) override;
74     ::android::status_t getMmapPositionAndLatency(StreamDescriptor::Position* position,
75                                                   int32_t* latency) override;
76 
77   protected:
78     ::android::status_t initSharedMemory(int ashmemFd);
79 
80   private:
81     ::android::status_t releaseSharedMemory() REQUIRES(mState.lock);
82     ::android::status_t startWorkerIfNeeded();
83 
84     mmap::DspSimulatorState mState;
85     mmap::DspSimulatorWorker mDspWorker;
86     bool mDspWorkerStarted = false;
87 };
88 
89 class StreamMmapStub : public StreamCommonImpl, public DriverMmapStubImpl {
90   public:
91     static const std::string kCreateMmapBufferName;
92 
93     StreamMmapStub(StreamContext* context, const Metadata& metadata);
94     ~StreamMmapStub();
95 
96     ndk::ScopedAStatus getVendorParameters(const std::vector<std::string>& in_ids,
97                                            std::vector<VendorParameter>* _aidl_return) override;
98     ndk::ScopedAStatus setVendorParameters(const std::vector<VendorParameter>& in_parameters,
99                                            bool in_async) override;
100 
101   private:
102     ndk::ScopedAStatus createMmapBuffer(MmapBufferDescriptor* desc);
103 
104     ndk::ScopedFileDescriptor mSharedMemoryFd;
105 };
106 
107 class StreamInMmapStub final : public StreamIn, public StreamMmapStub {
108   public:
109     friend class ndk::SharedRefBase;
110     StreamInMmapStub(
111             StreamContext&& context,
112             const ::aidl::android::hardware::audio::common::SinkMetadata& sinkMetadata,
113             const std::vector<::aidl::android::media::audio::common::MicrophoneInfo>& microphones);
114 
115   private:
onClose(StreamDescriptor::State)116     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
117 };
118 
119 class StreamOutMmapStub final : public StreamOut, public StreamMmapStub {
120   public:
121     friend class ndk::SharedRefBase;
122     StreamOutMmapStub(
123             StreamContext&& context,
124             const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata,
125             const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>&
126                     offloadInfo);
127 
128   private:
onClose(StreamDescriptor::State)129     void onClose(StreamDescriptor::State) override { defaultOnClose(); }
130 };
131 
132 }  // namespace aidl::android::hardware::audio::core
133