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