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