blob: 2601f3f4c21cde47fce65da9f6ef83ca236fa199 [file] [log] [blame]
Phil Burk87c9f642017-05-17 07:22:39 -07001/*
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 Burk39f02dd2017-08-04 09:13:31 -070017#define LOG_TAG "AAudioServiceEndpointPlay"
Phil Burk87c9f642017-05-17 07:22:39 -070018//#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 Burk39f02dd2017-08-04 09:13:31 -070036#include "AAudioServiceEndpointShared.h"
Phil Burk23296382017-11-20 15:45:11 -080037#include "AAudioServiceStreamBase.h"
Phil Burk87c9f642017-05-17 07:22:39 -070038
39using namespace android; // TODO just import names needed
40using namespace aaudio; // TODO just import names needed
41
42#define BURSTS_PER_BUFFER_DEFAULT 2
43
44AAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService &audioService)
45 : mStreamInternalPlay(audioService, true) {
Phil Burk39f02dd2017-08-04 09:13:31 -070046 mStreamInternal = &mStreamInternalPlay;
Phil Burk87c9f642017-05-17 07:22:39 -070047}
48
49AAudioServiceEndpointPlay::~AAudioServiceEndpointPlay() {
50}
51
Phil Burk39f02dd2017-08-04 09:13:31 -070052aaudio_result_t AAudioServiceEndpointPlay::open(const aaudio::AAudioStreamRequest &request) {
53 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
Phil Burk87c9f642017-05-17 07:22:39 -070054 if (result == AAUDIO_OK) {
55 mMixer.allocate(getStreamInternal()->getSamplesPerFrame(),
56 getStreamInternal()->getFramesPerBurst());
57
58 int32_t burstsPerBuffer = AAudioProperty_getMixerBursts();
59 if (burstsPerBuffer == 0) {
60 mLatencyTuningEnabled = true;
61 burstsPerBuffer = BURSTS_PER_BUFFER_DEFAULT;
62 }
Phil Burk87c9f642017-05-17 07:22:39 -070063 int32_t desiredBufferSize = burstsPerBuffer * getStreamInternal()->getFramesPerBurst();
64 getStreamInternal()->setBufferSize(desiredBufferSize);
65 }
66 return result;
67}
68
69// Mix data from each application stream and write result to the shared MMAP stream.
70void *AAudioServiceEndpointPlay::callbackLoop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070071 aaudio_result_t result = AAUDIO_OK;
Phil Burk87c9f642017-05-17 07:22:39 -070072 int64_t timeoutNanos = getStreamInternal()->calculateReasonableTimeout();
73
74 // result might be a frame count
75 while (mCallbackEnabled.load() && getStreamInternal()->isActive() && (result >= 0)) {
76 // Mix data from each active stream.
77 mMixer.clear();
Phil Burk39f02dd2017-08-04 09:13:31 -070078
Phil Burk97350f92017-07-21 15:59:44 -070079 { // brackets are for lock_guard
Phil Burkfd34a932017-07-19 07:03:52 -070080 int index = 0;
Phil Burk97350f92017-07-21 15:59:44 -070081 int64_t mmapFramesWritten = getStreamInternal()->getFramesWritten();
82
Phil Burk87c9f642017-05-17 07:22:39 -070083 std::lock_guard <std::mutex> lock(mLockStreams);
Phil Burk39f02dd2017-08-04 09:13:31 -070084 for (const auto clientStream : mRegisteredStreams) {
Phil Burk523b3042017-09-13 13:03:08 -070085 int64_t clientFramesRead = 0;
Phil Burk83fb8442017-10-05 16:55:17 -070086 bool allowUnderflow = true;
Phil Burk523b3042017-09-13 13:03:08 -070087
Phil Burk83fb8442017-10-05 16:55:17 -070088 aaudio_stream_state_t state = clientStream->getState();
89 if (state == AAUDIO_STREAM_STATE_STOPPING) {
90 allowUnderflow = false; // just read what is already in the FIFO
91 } else if (state != AAUDIO_STREAM_STATE_STARTED) {
92 continue; // this stream is not running so skip it.
Eric Laurentcb4dae22017-07-01 19:39:32 -070093 }
Phil Burk39f02dd2017-08-04 09:13:31 -070094
Phil Burk523b3042017-09-13 13:03:08 -070095 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070096 static_cast<AAudioServiceStreamShared *>(clientStream.get());
97
Phil Burk523b3042017-09-13 13:03:08 -070098 {
99 // Lock the AudioFifo to protect against close.
100 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
Phil Burk39f02dd2017-08-04 09:13:31 -0700101
Phil Burk523b3042017-09-13 13:03:08 -0700102 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
103 if (fifo != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700104
Phil Burk523b3042017-09-13 13:03:08 -0700105 // Determine offset between framePosition in client's stream
106 // vs the underlying MMAP stream.
107 clientFramesRead = fifo->getReadCounter();
108 // These two indices refer to the same frame.
109 int64_t positionOffset = mmapFramesWritten - clientFramesRead;
110 streamShared->setTimestampPositionOffset(positionOffset);
Phil Burk39f02dd2017-08-04 09:13:31 -0700111
Phil Burk23296382017-11-20 15:45:11 -0800112 int32_t framesMixed = mMixer.mix(index, fifo, allowUnderflow);
113
114 if (streamShared->isFlowing()) {
115 // Consider it an underflow if we got less than a burst
116 // after the data started flowing.
117 bool underflowed = allowUnderflow
118 && framesMixed < mMixer.getFramesPerBurst();
119 if (underflowed) {
120 streamShared->incrementXRunCount();
121 }
122 } else if (framesMixed > 0) {
123 // Mark beginning of data flow after a start.
124 streamShared->setFlowing(true);
Phil Burk523b3042017-09-13 13:03:08 -0700125 }
126 clientFramesRead = fifo->getReadCounter();
127 }
128 }
129
130 if (clientFramesRead > 0) {
131 // This timestamp represents the completion of data being read out of the
132 // client buffer. It is sent to the client and used in the timing model
133 // to decide when the client has room to write more data.
134 Timestamp timestamp(clientFramesRead, AudioClock::getNanoseconds());
135 streamShared->markTransferTime(timestamp);
Phil Burk39f02dd2017-08-04 09:13:31 -0700136 }
137
138 index++; // just used for labelling tracks in systrace
Phil Burk87c9f642017-05-17 07:22:39 -0700139 }
140 }
141
142 // Write mixer output to stream using a blocking write.
143 result = getStreamInternal()->write(mMixer.getOutputBuffer(),
144 getFramesPerBurst(), timeoutNanos);
145 if (result == AAUDIO_ERROR_DISCONNECTED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700146 AAudioServiceEndpointShared::disconnectRegisteredStreams();
Phil Burk87c9f642017-05-17 07:22:39 -0700147 break;
148 } else if (result != getFramesPerBurst()) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700149 ALOGW("callbackLoop() wrote %d / %d",
Phil Burk87c9f642017-05-17 07:22:39 -0700150 result, getFramesPerBurst());
151 break;
152 }
153 }
154
Phil Burk87c9f642017-05-17 07:22:39 -0700155 return NULL; // TODO review
156}