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 <set> 21 #include <string> 22 #include <vector> 23 24 #include "core-impl/DriverStubImpl.h" 25 #include "core-impl/Stream.h" 26 27 namespace aidl::android::hardware::audio::core { 28 29 namespace offload { 30 31 struct DspSimulatorState { 32 static constexpr int64_t kSkipBufferNotifyFrames = -1; 33 34 const std::string formatEncoding; 35 const int sampleRate; 36 const int64_t earlyNotifyFrames; 37 DriverCallbackInterface* callback = nullptr; // set before starting DSP worker 38 std::mutex lock; 39 std::vector<int64_t> clipFramesLeft GUARDED_BY(lock); 40 int64_t bufferFramesLeft GUARDED_BY(lock) = 0; 41 int64_t bufferNotifyFrames GUARDED_BY(lock) = kSkipBufferNotifyFrames; 42 }; 43 44 class DspSimulatorLogic : public ::android::hardware::audio::common::StreamLogic { 45 protected: DspSimulatorLogic(DspSimulatorState & sharedState)46 explicit DspSimulatorLogic(DspSimulatorState& sharedState) : mSharedState(sharedState) {} 47 std::string init() override; 48 Status cycle() override; 49 50 private: 51 DspSimulatorState& mSharedState; 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 offload 62 63 class DriverOffloadStubImpl : public DriverStubImpl { 64 public: 65 explicit DriverOffloadStubImpl(const StreamContext& context); 66 ::android::status_t init(DriverCallbackInterface* callback) override; 67 ::android::status_t drain(StreamDescriptor::DrainMode drainMode) override; 68 ::android::status_t flush() override; 69 ::android::status_t pause() override; 70 ::android::status_t start() override; 71 ::android::status_t transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, 72 int32_t* latencyMs) override; 73 void shutdown() override; 74 75 private: 76 ::android::status_t startWorkerIfNeeded(); 77 78 const int64_t mBufferNotifyFrames; 79 offload::DspSimulatorState mState; 80 offload::DspSimulatorWorker mDspWorker; 81 bool mDspWorkerStarted = false; 82 }; 83 84 class StreamOffloadStub : public StreamCommonImpl, public DriverOffloadStubImpl { 85 public: 86 static const std::set<std::string>& getSupportedEncodings(); 87 88 StreamOffloadStub(StreamContext* context, const Metadata& metadata); 89 ~StreamOffloadStub(); 90 }; 91 92 class StreamOutOffloadStub final : public StreamOut, public StreamOffloadStub { 93 public: 94 friend class ndk::SharedRefBase; 95 StreamOutOffloadStub( 96 StreamContext&& context, 97 const ::aidl::android::hardware::audio::common::SourceMetadata& sourceMetadata, 98 const std::optional<::aidl::android::media::audio::common::AudioOffloadInfo>& 99 offloadInfo); 100 101 private: onClose(StreamDescriptor::State)102 void onClose(StreamDescriptor::State) override { defaultOnClose(); } 103 }; 104 105 } // namespace aidl::android::hardware::audio::core 106