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