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