Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [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 | #include <vector> |
| 18 | |
| 19 | #define LOG_TAG "AHAL_ModulePrimary" |
| 20 | #include <Utils.h> |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include "core-impl/ModulePrimary.h" |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 24 | #include "core-impl/StreamOffloadStub.h" |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 25 | #include "core-impl/StreamPrimary.h" |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 26 | #include "core-impl/Telephony.h" |
| 27 | |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 28 | using aidl::android::hardware::audio::common::areAllBitPositionFlagsSet; |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 29 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 30 | using aidl::android::hardware::audio::common::SourceMetadata; |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 31 | using aidl::android::media::audio::common::AudioIoFlags; |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 32 | using aidl::android::media::audio::common::AudioOffloadInfo; |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 33 | using aidl::android::media::audio::common::AudioOutputFlags; |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 34 | using aidl::android::media::audio::common::AudioPort; |
| 35 | using aidl::android::media::audio::common::AudioPortConfig; |
| 36 | using aidl::android::media::audio::common::MicrophoneInfo; |
| 37 | |
| 38 | namespace aidl::android::hardware::audio::core { |
| 39 | |
| 40 | ndk::ScopedAStatus ModulePrimary::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) { |
| 41 | if (!mTelephony) { |
| 42 | mTelephony = ndk::SharedRefBase::make<Telephony>(); |
| 43 | } |
Mikhail Naganov | 780fefb | 2023-07-21 17:01:38 -0700 | [diff] [blame] | 44 | *_aidl_return = mTelephony.getInstance(); |
Mikhail Naganov | 2eabaf9 | 2023-07-19 14:28:47 -0700 | [diff] [blame] | 45 | LOG(DEBUG) << __func__ |
| 46 | << ": returning instance of ITelephony: " << _aidl_return->get()->asBinder().get(); |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 47 | return ndk::ScopedAStatus::ok(); |
| 48 | } |
| 49 | |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 50 | ndk::ScopedAStatus ModulePrimary::calculateBufferSizeFrames( |
| 51 | const ::aidl::android::media::audio::common::AudioFormatDescription& format, |
| 52 | int32_t latencyMs, int32_t sampleRateHz, int32_t* bufferSizeFrames) { |
| 53 | if (format.type != ::aidl::android::media::audio::common::AudioFormatType::PCM && |
| 54 | StreamOffloadStub::getSupportedEncodings().count(format.encoding)) { |
| 55 | *bufferSizeFrames = sampleRateHz / 2; // 1/2 of a second. |
| 56 | return ndk::ScopedAStatus::ok(); |
| 57 | } |
| 58 | return Module::calculateBufferSizeFrames(format, latencyMs, sampleRateHz, bufferSizeFrames); |
| 59 | } |
| 60 | |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 61 | ndk::ScopedAStatus ModulePrimary::createInputStream(StreamContext&& context, |
| 62 | const SinkMetadata& sinkMetadata, |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 63 | const std::vector<MicrophoneInfo>& microphones, |
| 64 | std::shared_ptr<StreamIn>* result) { |
Mikhail Naganov | cf824f6 | 2023-07-24 14:51:36 -0700 | [diff] [blame] | 65 | return createStreamInstance<StreamInPrimary>(result, std::move(context), sinkMetadata, |
| 66 | microphones); |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ndk::ScopedAStatus ModulePrimary::createOutputStream( |
Mikhail Naganov | 6ddefdb | 2023-07-19 17:30:06 -0700 | [diff] [blame] | 70 | StreamContext&& context, const SourceMetadata& sourceMetadata, |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 71 | const std::optional<AudioOffloadInfo>& offloadInfo, std::shared_ptr<StreamOut>* result) { |
Mikhail Naganov | efc2b32 | 2025-02-12 16:08:55 -0800 | [diff] [blame] | 72 | if (!areAllBitPositionFlagsSet( |
| 73 | context.getFlags().get<AudioIoFlags::output>(), |
| 74 | {AudioOutputFlags::COMPRESS_OFFLOAD, AudioOutputFlags::NON_BLOCKING})) { |
| 75 | return createStreamInstance<StreamOutPrimary>(result, std::move(context), sourceMetadata, |
| 76 | offloadInfo); |
| 77 | } else { |
| 78 | // "Stub" is used because there is no actual decoder. The stream just |
| 79 | // extracts the clip duration from the media file header and simulates |
| 80 | // playback over time. |
| 81 | return createStreamInstance<StreamOutOffloadStub>(result, std::move(context), |
| 82 | sourceMetadata, offloadInfo); |
| 83 | } |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 86 | int32_t ModulePrimary::getNominalLatencyMs(const AudioPortConfig&) { |
| 87 | // 85 ms is chosen considering 4096 frames @ 48 kHz. This is the value which allows |
| 88 | // the virtual Android device implementation to pass CTS. Hardware implementations |
| 89 | // should have significantly lower latency. |
| 90 | static constexpr int32_t kLatencyMs = 85; |
| 91 | return kLatencyMs; |
| 92 | } |
| 93 | |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 94 | } // namespace aidl::android::hardware::audio::core |