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