Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame^] | 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 | #define LOG_TAG "AHAL_Stream" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include "core-impl/Module.h" |
| 21 | #include "core-impl/StreamStub.h" |
| 22 | |
| 23 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 24 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 25 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 26 | |
| 27 | namespace aidl::android::hardware::audio::core { |
| 28 | |
| 29 | DriverStub::DriverStub(const StreamContext& context, bool isInput) |
| 30 | : mFrameSizeBytes(context.getFrameSize()), mIsInput(isInput) {} |
| 31 | |
| 32 | ::android::status_t DriverStub::init() { |
| 33 | usleep(1000); |
| 34 | return ::android::OK; |
| 35 | } |
| 36 | |
| 37 | ::android::status_t DriverStub::drain(StreamDescriptor::DrainMode) { |
| 38 | usleep(1000); |
| 39 | return ::android::OK; |
| 40 | } |
| 41 | |
| 42 | ::android::status_t DriverStub::flush() { |
| 43 | usleep(1000); |
| 44 | return ::android::OK; |
| 45 | } |
| 46 | |
| 47 | ::android::status_t DriverStub::pause() { |
| 48 | usleep(1000); |
| 49 | return ::android::OK; |
| 50 | } |
| 51 | |
| 52 | ::android::status_t DriverStub::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount, |
| 53 | int32_t* latencyMs) { |
| 54 | usleep(3000); |
| 55 | if (mIsInput) { |
| 56 | uint8_t* byteBuffer = static_cast<uint8_t*>(buffer); |
| 57 | for (size_t i = 0; i < frameCount * mFrameSizeBytes; ++i) { |
| 58 | byteBuffer[i] = std::rand() % 255; |
| 59 | } |
| 60 | } |
| 61 | *actualFrameCount = frameCount; |
| 62 | *latencyMs = Module::kLatencyMs; |
| 63 | return ::android::OK; |
| 64 | } |
| 65 | |
| 66 | ::android::status_t DriverStub::standby() { |
| 67 | usleep(1000); |
| 68 | return ::android::OK; |
| 69 | } |
| 70 | |
| 71 | // static |
| 72 | ndk::ScopedAStatus StreamInStub::createInstance(const SinkMetadata& sinkMetadata, |
| 73 | StreamContext&& context, |
| 74 | const std::vector<MicrophoneInfo>& microphones, |
| 75 | std::shared_ptr<StreamIn>* result) { |
| 76 | std::shared_ptr<StreamIn> stream = |
| 77 | ndk::SharedRefBase::make<StreamInStub>(sinkMetadata, std::move(context), microphones); |
| 78 | if (auto status = initInstance(stream); !status.isOk()) { |
| 79 | return status; |
| 80 | } |
| 81 | *result = std::move(stream); |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
| 85 | StreamInStub::StreamInStub(const SinkMetadata& sinkMetadata, StreamContext&& context, |
| 86 | const std::vector<MicrophoneInfo>& microphones) |
| 87 | : StreamIn( |
| 88 | sinkMetadata, std::move(context), |
| 89 | [](const StreamContext& ctx) -> DriverInterface* { |
| 90 | return new DriverStub(ctx, true /*isInput*/); |
| 91 | }, |
| 92 | [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* { |
| 93 | // The default worker implementation is used. |
| 94 | return new StreamInWorker(ctx, driver); |
| 95 | }, |
| 96 | microphones) {} |
| 97 | |
| 98 | // static |
| 99 | ndk::ScopedAStatus StreamOutStub::createInstance(const SourceMetadata& sourceMetadata, |
| 100 | StreamContext&& context, |
| 101 | const std::optional<AudioOffloadInfo>& offloadInfo, |
| 102 | std::shared_ptr<StreamOut>* result) { |
| 103 | std::shared_ptr<StreamOut> stream = ndk::SharedRefBase::make<StreamOutStub>( |
| 104 | sourceMetadata, std::move(context), offloadInfo); |
| 105 | if (auto status = initInstance(stream); !status.isOk()) { |
| 106 | return status; |
| 107 | } |
| 108 | *result = std::move(stream); |
| 109 | return ndk::ScopedAStatus::ok(); |
| 110 | } |
| 111 | |
| 112 | StreamOutStub::StreamOutStub(const SourceMetadata& sourceMetadata, StreamContext&& context, |
| 113 | const std::optional<AudioOffloadInfo>& offloadInfo) |
| 114 | : StreamOut( |
| 115 | sourceMetadata, std::move(context), |
| 116 | [](const StreamContext& ctx) -> DriverInterface* { |
| 117 | return new DriverStub(ctx, false /*isInput*/); |
| 118 | }, |
| 119 | [](const StreamContext& ctx, DriverInterface* driver) -> StreamWorkerInterface* { |
| 120 | // The default worker implementation is used. |
| 121 | return new StreamOutWorker(ctx, driver); |
| 122 | }, |
| 123 | offloadInfo) {} |
| 124 | |
| 125 | } // namespace aidl::android::hardware::audio::core |