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 | |
| 17 | #define LOG_TAG "AAudioService" |
| 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" |
| 36 | |
| 37 | using namespace android; // TODO just import names needed |
| 38 | using namespace aaudio; // TODO just import names needed |
| 39 | |
| 40 | #define BURSTS_PER_BUFFER_DEFAULT 2 |
| 41 | |
| 42 | AAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService &audioService) |
| 43 | : mStreamInternalPlay(audioService, true) { |
| 44 | } |
| 45 | |
| 46 | AAudioServiceEndpointPlay::~AAudioServiceEndpointPlay() { |
| 47 | } |
| 48 | |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 49 | aaudio_result_t AAudioServiceEndpointPlay::open(const AAudioStreamConfiguration& configuration) { |
| 50 | aaudio_result_t result = AAudioServiceEndpoint::open(configuration); |
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 | |
| 55 | int32_t burstsPerBuffer = AAudioProperty_getMixerBursts(); |
| 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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 68 | int32_t underflowCount = 0; |
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 | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame^] | 76 | { // brackets are for lock_guard |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 77 | int index = 0; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame^] | 78 | int64_t mmapFramesWritten = getStreamInternal()->getFramesWritten(); |
| 79 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 80 | std::lock_guard <std::mutex> lock(mLockStreams); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame^] | 81 | for (sp<AAudioServiceStreamShared> clientStream : mRegisteredStreams) { |
| 82 | if (clientStream->isRunning()) { |
| 83 | FifoBuffer *fifo = clientStream->getDataFifoBuffer(); |
| 84 | // Determine offset between framePosition in client's stream vs the underlying |
| 85 | // MMAP stream. |
| 86 | int64_t clientFramesRead = fifo->getReadCounter(); |
| 87 | // These two indices refer to the same frame. |
| 88 | int64_t positionOffset = mmapFramesWritten - clientFramesRead; |
| 89 | clientStream->setTimestampPositionOffset(positionOffset); |
| 90 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 91 | float volume = 1.0; // to match legacy volume |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 92 | bool underflowed = mMixer.mix(index, fifo, volume); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 93 | underflowCount += underflowed ? 1 : 0; |
| 94 | // TODO log underflows in each stream |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame^] | 95 | |
| 96 | // This timestamp represents the completion of data being read out of the |
| 97 | // client buffer. It is sent to the client and used in the timing model |
| 98 | // to decide when the client has room to write more data. |
| 99 | Timestamp timestamp(fifo->getReadCounter(), AudioClock::getNanoseconds()); |
| 100 | clientStream->markTransferTime(timestamp); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 101 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 102 | index++; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Write mixer output to stream using a blocking write. |
| 107 | result = getStreamInternal()->write(mMixer.getOutputBuffer(), |
| 108 | getFramesPerBurst(), timeoutNanos); |
| 109 | if (result == AAUDIO_ERROR_DISCONNECTED) { |
| 110 | disconnectRegisteredStreams(); |
| 111 | break; |
| 112 | } else if (result != getFramesPerBurst()) { |
| 113 | ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d", |
| 114 | result, getFramesPerBurst()); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 119 | ALOGW_IF((underflowCount > 0), |
| 120 | "AAudioServiceEndpointPlay(): callbackLoop() had %d underflows", underflowCount); |
| 121 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 122 | return NULL; // TODO review |
| 123 | } |