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> |
| 24 | #include <utils/Singleton.h> |
| 25 | |
| 26 | #include "AAudioEndpointManager.h" |
| 27 | #include "AAudioServiceEndpoint.h" |
| 28 | #include <algorithm> |
| 29 | #include <mutex> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "core/AudioStreamBuilder.h" |
| 33 | #include "AAudioServiceEndpoint.h" |
| 34 | #include "AAudioServiceStreamShared.h" |
| 35 | #include "AAudioServiceEndpointPlay.h" |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 36 | #include "AAudioServiceEndpointShared.h" |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 37 | |
| 38 | using namespace android; // TODO just import names needed |
| 39 | using namespace aaudio; // TODO just import names needed |
| 40 | |
| 41 | #define BURSTS_PER_BUFFER_DEFAULT 2 |
| 42 | |
| 43 | AAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService &audioService) |
| 44 | : mStreamInternalPlay(audioService, true) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 45 | mStreamInternal = &mStreamInternalPlay; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | AAudioServiceEndpointPlay::~AAudioServiceEndpointPlay() { |
| 49 | } |
| 50 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 51 | aaudio_result_t AAudioServiceEndpointPlay::open(const aaudio::AAudioStreamRequest &request) { |
| 52 | aaudio_result_t result = AAudioServiceEndpointShared::open(request); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 53 | if (result == AAUDIO_OK) { |
| 54 | mMixer.allocate(getStreamInternal()->getSamplesPerFrame(), |
| 55 | getStreamInternal()->getFramesPerBurst()); |
| 56 | |
| 57 | int32_t burstsPerBuffer = AAudioProperty_getMixerBursts(); |
| 58 | if (burstsPerBuffer == 0) { |
| 59 | mLatencyTuningEnabled = true; |
| 60 | burstsPerBuffer = BURSTS_PER_BUFFER_DEFAULT; |
| 61 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 62 | int32_t desiredBufferSize = burstsPerBuffer * getStreamInternal()->getFramesPerBurst(); |
| 63 | getStreamInternal()->setBufferSize(desiredBufferSize); |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | // Mix data from each application stream and write result to the shared MMAP stream. |
| 69 | void *AAudioServiceEndpointPlay::callbackLoop() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 70 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 71 | int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout(); |
| 72 | |
| 73 | // result might be a frame count |
| 74 | while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) { |
| 75 | // Mix data from each active stream. |
| 76 | mMixer.clear(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 77 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 78 | { // brackets are for lock_guard |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 79 | int index = 0; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 80 | int64_t mmapFramesWritten = getStreamInternal()->getFramesWritten(); |
| 81 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 82 | std::lock_guard <std::mutex> lock(mLockStreams); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 83 | for (const auto clientStream : mRegisteredStreams) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 84 | int64_t clientFramesRead = 0; |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame^] | 85 | bool allowUnderflow = true; |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 86 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame^] | 87 | aaudio_stream_state_t state = clientStream->getState(); |
| 88 | if (state == AAUDIO_STREAM_STATE_STOPPING) { |
| 89 | allowUnderflow = false; // just read what is already in the FIFO |
| 90 | } else if (state != AAUDIO_STREAM_STATE_STARTED) { |
| 91 | continue; // this stream is not running so skip it. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 92 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 93 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 94 | sp<AAudioServiceStreamShared> streamShared = |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 95 | static_cast<AAudioServiceStreamShared *>(clientStream.get()); |
| 96 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 97 | { |
| 98 | // Lock the AudioFifo to protect against close. |
| 99 | std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 100 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 101 | FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l(); |
| 102 | if (fifo != nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 103 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 104 | // Determine offset between framePosition in client's stream |
| 105 | // vs the underlying MMAP stream. |
| 106 | clientFramesRead = fifo->getReadCounter(); |
| 107 | // These two indices refer to the same frame. |
| 108 | int64_t positionOffset = mmapFramesWritten - clientFramesRead; |
| 109 | streamShared->setTimestampPositionOffset(positionOffset); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 110 | |
Phil Burk | 83fb844 | 2017-10-05 16:55:17 -0700 | [diff] [blame^] | 111 | bool underflowed = mMixer.mix(index, fifo, allowUnderflow); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 112 | if (underflowed) { |
| 113 | streamShared->incrementXRunCount(); |
| 114 | } |
| 115 | clientFramesRead = fifo->getReadCounter(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (clientFramesRead > 0) { |
| 120 | // This timestamp represents the completion of data being read out of the |
| 121 | // client buffer. It is sent to the client and used in the timing model |
| 122 | // to decide when the client has room to write more data. |
| 123 | Timestamp timestamp(clientFramesRead, AudioClock::getNanoseconds()); |
| 124 | streamShared->markTransferTime(timestamp); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | index++; // just used for labelling tracks in systrace |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Write mixer output to stream using a blocking write. |
| 132 | result = getStreamInternal()->write(mMixer.getOutputBuffer(), |
| 133 | getFramesPerBurst(), timeoutNanos); |
| 134 | if (result == AAUDIO_ERROR_DISCONNECTED) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 135 | AAudioServiceEndpointShared::disconnectRegisteredStreams(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 136 | break; |
| 137 | } else if (result != getFramesPerBurst()) { |
| 138 | ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d", |
| 139 | result, getFramesPerBurst()); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 144 | return NULL; // TODO review |
| 145 | } |