Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [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 | |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 19 | #include <mutex> |
| 20 | |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame] | 21 | #include <android-base/thread_annotations.h> |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 22 | #include <audio_utils/clock.h> |
| 23 | |
| 24 | #include <media/nbaio/MonoPipe.h> |
| 25 | #include <media/nbaio/MonoPipeReader.h> |
| 26 | |
| 27 | #include <aidl/android/media/audio/common/AudioChannelLayout.h> |
Mikhail Naganov | 2aab766 | 2023-10-24 13:56:07 -0700 | [diff] [blame] | 28 | #include <aidl/android/media/audio/common/AudioFormatDescription.h> |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 29 | |
| 30 | using aidl::android::media::audio::common::AudioChannelLayout; |
| 31 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 32 | using aidl::android::media::audio::common::AudioFormatType; |
| 33 | using aidl::android::media::audio::common::PcmType; |
| 34 | using ::android::MonoPipe; |
| 35 | using ::android::MonoPipeReader; |
| 36 | using ::android::sp; |
| 37 | |
| 38 | namespace aidl::android::hardware::audio::core::r_submix { |
| 39 | |
| 40 | static constexpr int kDefaultSampleRateHz = 48000; |
Mikhail Naganov | 1350187 | 2023-10-18 16:15:46 -0700 | [diff] [blame] | 41 | // Value used to divide the MonoPipe buffer into segments that are written to the source and |
| 42 | // read from the sink. The maximum latency of the device is the size of the MonoPipe's buffer |
| 43 | // the minimum latency is the MonoPipe buffer size divided by this value. |
| 44 | static constexpr int kDefaultPipePeriodCount = 4; |
| 45 | // Size at the default sample rate |
| 46 | // NOTE: This value will be rounded up to the nearest power of 2 by MonoPipe. |
| 47 | static constexpr int kDefaultPipeSizeInFrames = 1024 * kDefaultPipePeriodCount; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 48 | |
| 49 | // Configuration of the audio stream. |
| 50 | struct AudioConfig { |
| 51 | int sampleRate = kDefaultSampleRateHz; |
| 52 | AudioFormatDescription format = |
| 53 | AudioFormatDescription{.type = AudioFormatType::PCM, .pcm = PcmType::INT_16_BIT}; |
| 54 | AudioChannelLayout channelLayout = |
| 55 | AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>( |
| 56 | AudioChannelLayout::LAYOUT_STEREO); |
| 57 | size_t frameSize; |
| 58 | size_t frameCount; |
| 59 | }; |
| 60 | |
| 61 | class SubmixRoute { |
| 62 | public: |
| 63 | AudioConfig mPipeConfig; |
| 64 | |
| 65 | bool isStreamInOpen() { |
| 66 | std::lock_guard guard(mLock); |
| 67 | return mStreamInOpen; |
| 68 | } |
| 69 | bool getStreamInStandby() { |
| 70 | std::lock_guard guard(mLock); |
| 71 | return mStreamInStandby; |
| 72 | } |
| 73 | bool isStreamOutOpen() { |
| 74 | std::lock_guard guard(mLock); |
| 75 | return mStreamOutOpen; |
| 76 | } |
| 77 | bool getStreamOutStandby() { |
| 78 | std::lock_guard guard(mLock); |
| 79 | return mStreamOutStandby; |
| 80 | } |
| 81 | long getReadCounterFrames() { |
| 82 | std::lock_guard guard(mLock); |
| 83 | return mReadCounterFrames; |
| 84 | } |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 85 | sp<MonoPipe> getSink() { |
| 86 | std::lock_guard guard(mLock); |
| 87 | return mSink; |
| 88 | } |
| 89 | sp<MonoPipeReader> getSource() { |
| 90 | std::lock_guard guard(mLock); |
| 91 | return mSource; |
| 92 | } |
| 93 | |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 94 | bool isStreamConfigValid(bool isInput, const AudioConfig& streamConfig); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 95 | void closeStream(bool isInput); |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 96 | ::android::status_t createPipe(const AudioConfig& streamConfig); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 97 | void exitStandby(bool isInput); |
| 98 | bool hasAtleastOneStreamOpen(); |
| 99 | int notifyReadError(); |
| 100 | void openStream(bool isInput); |
| 101 | void releasePipe(); |
| 102 | ::android::status_t resetPipe(); |
| 103 | bool shouldBlockWrite(); |
| 104 | void standby(bool isInput); |
| 105 | long updateReadCounterFrames(size_t frameCount); |
| 106 | |
| 107 | private: |
Shraddha Basantwani | 7770c15 | 2023-07-19 16:43:50 +0530 | [diff] [blame] | 108 | bool isStreamConfigCompatible(const AudioConfig& streamConfig); |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 109 | |
| 110 | std::mutex mLock; |
| 111 | |
| 112 | bool mStreamInOpen GUARDED_BY(mLock) = false; |
| 113 | int mInputRefCount GUARDED_BY(mLock) = 0; |
| 114 | bool mStreamInStandby GUARDED_BY(mLock) = true; |
| 115 | bool mStreamOutStandbyTransition GUARDED_BY(mLock) = false; |
| 116 | bool mStreamOutOpen GUARDED_BY(mLock) = false; |
| 117 | bool mStreamOutStandby GUARDED_BY(mLock) = true; |
| 118 | // how many frames have been requested to be read since standby |
| 119 | long mReadCounterFrames GUARDED_BY(mLock) = 0; |
Shraddha Basantwani | 6bb6963 | 2023-04-25 15:26:38 +0530 | [diff] [blame] | 120 | |
| 121 | // Pipe variables: they handle the ring buffer that "pipes" audio: |
| 122 | // - from the submix virtual audio output == what needs to be played |
| 123 | // remotely, seen as an output for the client |
| 124 | // - to the virtual audio source == what is captured by the component |
| 125 | // which "records" the submix / virtual audio source, and handles it as needed. |
| 126 | // A usecase example is one where the component capturing the audio is then sending it over |
| 127 | // Wifi for presentation on a remote Wifi Display device (e.g. a dongle attached to a TV, or a |
| 128 | // TV with Wifi Display capabilities), or to a wireless audio player. |
| 129 | sp<MonoPipe> mSink GUARDED_BY(mLock); |
| 130 | sp<MonoPipeReader> mSource GUARDED_BY(mLock); |
| 131 | }; |
| 132 | |
| 133 | } // namespace aidl::android::hardware::audio::core::r_submix |