Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceEndpointPlay" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <map> |
| 23 | #include <mutex> |
jiabin | e504e7b | 2021-09-18 00:27:08 +0000 | [diff] [blame] | 24 | #include <media/AudioSystem.h> |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 25 | #include <utils/Singleton.h> |
| 26 | |
| 27 | #include "AAudioEndpointManager.h" |
| 28 | #include "AAudioServiceEndpoint.h" |
| 29 | #include <algorithm> |
| 30 | #include <mutex> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "core/AudioStreamBuilder.h" |
| 34 | #include "AAudioServiceEndpoint.h" |
| 35 | #include "AAudioServiceStreamShared.h" |
| 36 | #include "AAudioServiceEndpointPlay.h" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 37 | #include "AAudioServiceEndpointShared.h" |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 38 | #include "AAudioServiceStreamBase.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 39 | |
| 40 | using namespace android; // TODO just import names needed |
| 41 | using namespace aaudio; // TODO just import names needed |
| 42 | |
| 43 | #define BURSTS_PER_BUFFER_DEFAULT 2 |
| 44 | |
Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame] | 45 | AAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService& audioService) |
| 46 | : AAudioServiceEndpointShared( |
| 47 | new AudioStreamInternalPlay(audioService.asAAudioServiceInterface(), true)) {} |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 48 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 49 | aaudio_result_t AAudioServiceEndpointPlay::open(const aaudio::AAudioStreamRequest &request) { |
| 50 | aaudio_result_t result = AAudioServiceEndpointShared::open(request); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 51 | if (result == AAUDIO_OK) { |
| 52 | mMixer.allocate(getStreamInternal()->getSamplesPerFrame(), |
| 53 | getStreamInternal()->getFramesPerBurst()); |
| 54 | |
jiabin | e504e7b | 2021-09-18 00:27:08 +0000 | [diff] [blame] | 55 | int32_t burstsPerBuffer = AudioSystem::getAAudioMixerBurstCount(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 56 | if (burstsPerBuffer == 0) { |
| 57 | mLatencyTuningEnabled = true; |
| 58 | burstsPerBuffer = BURSTS_PER_BUFFER_DEFAULT; |
| 59 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 60 | int32_t desiredBufferSize = burstsPerBuffer * getStreamInternal()->getFramesPerBurst(); |
| 61 | getStreamInternal()->setBufferSize(desiredBufferSize); |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | // Mix data from each application stream and write result to the shared MMAP stream. |
| 67 | void *AAudioServiceEndpointPlay::callbackLoop() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 68 | ALOGD("%s() entering >>>>>>>>>>>>>>> MIXER", __func__); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 69 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 70 | int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout(); |
| 71 | |
| 72 | // result might be a frame count |
| 73 | while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) { |
| 74 | // Mix data from each active stream. |
| 75 | mMixer.clear(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 76 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 77 | { // brackets are for lock_guard |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 78 | int index = 0; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 79 | int64_t mmapFramesWritten = getStreamInternal()->getFramesWritten(); |
| 80 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 81 | std::lock_guard <std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 82 | for (const auto& clientStream : mRegisteredStreams) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 83 | int64_t clientFramesRead = 0; |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 84 | bool allowUnderflow = true; |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 85 | |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 86 | if (clientStream->isSuspended()) { |
| 87 | continue; // dead stream |
| 88 | } |
| 89 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 90 | aaudio_stream_state_t state = clientStream->getState(); |
jiabin | 1de7952 | 2024-05-10 16:14:51 +0000 | [diff] [blame] | 91 | if (state == AAUDIO_STREAM_STATE_STOPPING || |
| 92 | state == AAUDIO_STREAM_STATE_PAUSING) { |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame] | 93 | allowUnderflow = false; // just read what is already in the FIFO |
| 94 | } else if (state != AAUDIO_STREAM_STATE_STARTED) { |
| 95 | continue; // this stream is not running so skip it. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 96 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 97 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 98 | sp<AAudioServiceStreamShared> streamShared = |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 99 | static_cast<AAudioServiceStreamShared *>(clientStream.get()); |
| 100 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 101 | { |
| 102 | // Lock the AudioFifo to protect against close. |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 103 | std::lock_guard <std::mutex> lock(streamShared->audioDataQueueLock); |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 104 | std::shared_ptr<SharedRingBuffer> audioDataQueue |
| 105 | = streamShared->getAudioDataQueue_l(); |
| 106 | std::shared_ptr<FifoBuffer> fifo; |
| 107 | if (audioDataQueue && (fifo = audioDataQueue->getFifoBuffer())) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 108 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 109 | // Determine offset between framePosition in client's stream |
| 110 | // vs the underlying MMAP stream. |
| 111 | clientFramesRead = fifo->getReadCounter(); |
| 112 | // These two indices refer to the same frame. |
| 113 | int64_t positionOffset = mmapFramesWritten - clientFramesRead; |
| 114 | streamShared->setTimestampPositionOffset(positionOffset); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 115 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 116 | int32_t framesMixed = mMixer.mix(index, fifo, allowUnderflow); |
| 117 | |
| 118 | if (streamShared->isFlowing()) { |
| 119 | // Consider it an underflow if we got less than a burst |
| 120 | // after the data started flowing. |
| 121 | bool underflowed = allowUnderflow |
| 122 | && framesMixed < mMixer.getFramesPerBurst(); |
| 123 | if (underflowed) { |
| 124 | streamShared->incrementXRunCount(); |
| 125 | } |
| 126 | } else if (framesMixed > 0) { |
| 127 | // Mark beginning of data flow after a start. |
| 128 | streamShared->setFlowing(true); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 129 | } |
| 130 | clientFramesRead = fifo->getReadCounter(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (clientFramesRead > 0) { |
| 135 | // This timestamp represents the completion of data being read out of the |
| 136 | // client buffer. It is sent to the client and used in the timing model |
| 137 | // to decide when the client has room to write more data. |
| 138 | Timestamp timestamp(clientFramesRead, AudioClock::getNanoseconds()); |
| 139 | streamShared->markTransferTime(timestamp); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | index++; // just used for labelling tracks in systrace |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | // Write mixer output to stream using a blocking write. |
| 147 | result = getStreamInternal()->write(mMixer.getOutputBuffer(), |
| 148 | getFramesPerBurst(), timeoutNanos); |
| 149 | if (result == AAUDIO_ERROR_DISCONNECTED) { |
Phil Burk | e358ec6 | 2020-10-12 23:42:30 +0000 | [diff] [blame] | 150 | ALOGD("%s() write() returned AAUDIO_ERROR_DISCONNECTED", __func__); |
jiabin | 6604ce9 | 2022-01-22 00:29:17 +0000 | [diff] [blame] | 151 | AAudioServiceEndpointShared::handleDisconnectRegisteredStreamsAsync(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 152 | break; |
| 153 | } else if (result != getFramesPerBurst()) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 154 | ALOGW("callbackLoop() wrote %d / %d", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 155 | result, getFramesPerBurst()); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 160 | ALOGD("%s() exiting, enabled = %d, state = %d, result = %d <<<<<<<<<<<<< MIXER", |
| 161 | __func__, mCallbackEnabled.load(), getStreamInternal()->getState(), result); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 162 | return nullptr; // TODO review |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 163 | } |