blob: 472a336c1592a356d8401bbef30d74191e729c66 [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 Burk87c9f642017-05-17 07:22:39 -070037
38using namespace android; // TODO just import names needed
39using namespace aaudio; // TODO just import names needed
40
41#define BURSTS_PER_BUFFER_DEFAULT 2
42
43AAudioServiceEndpointPlay::AAudioServiceEndpointPlay(AAudioService &audioService)
44 : mStreamInternalPlay(audioService, true) {
Phil Burk39f02dd2017-08-04 09:13:31 -070045 mStreamInternal = &mStreamInternalPlay;
Phil Burk87c9f642017-05-17 07:22:39 -070046}
47
48AAudioServiceEndpointPlay::~AAudioServiceEndpointPlay() {
49}
50
Phil Burk39f02dd2017-08-04 09:13:31 -070051aaudio_result_t AAudioServiceEndpointPlay::open(const aaudio::AAudioStreamRequest &request) {
52 aaudio_result_t result = AAudioServiceEndpointShared::open(request);
Phil Burk87c9f642017-05-17 07:22:39 -070053 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 Burk87c9f642017-05-17 07:22:39 -070062 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.
69void *AAudioServiceEndpointPlay::callbackLoop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070070 aaudio_result_t result = AAUDIO_OK;
Phil Burk87c9f642017-05-17 07:22:39 -070071 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 Burk39f02dd2017-08-04 09:13:31 -070077
Phil Burk97350f92017-07-21 15:59:44 -070078 { // brackets are for lock_guard
Phil Burkfd34a932017-07-19 07:03:52 -070079 int index = 0;
Phil Burk97350f92017-07-21 15:59:44 -070080 int64_t mmapFramesWritten = getStreamInternal()->getFramesWritten();
81
Phil Burk87c9f642017-05-17 07:22:39 -070082 std::lock_guard <std::mutex> lock(mLockStreams);
Phil Burk39f02dd2017-08-04 09:13:31 -070083 for (const auto clientStream : mRegisteredStreams) {
Phil Burk523b3042017-09-13 13:03:08 -070084 int64_t clientFramesRead = 0;
Phil Burk83fb8442017-10-05 16:55:17 -070085 bool allowUnderflow = true;
Phil Burk523b3042017-09-13 13:03:08 -070086
Phil Burk83fb8442017-10-05 16:55:17 -070087 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 Laurentcb4dae22017-07-01 19:39:32 -070092 }
Phil Burk39f02dd2017-08-04 09:13:31 -070093
Phil Burk523b3042017-09-13 13:03:08 -070094 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070095 static_cast<AAudioServiceStreamShared *>(clientStream.get());
96
Phil Burk523b3042017-09-13 13:03:08 -070097 {
98 // Lock the AudioFifo to protect against close.
99 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
Phil Burk39f02dd2017-08-04 09:13:31 -0700100
Phil Burk523b3042017-09-13 13:03:08 -0700101 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
102 if (fifo != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700103
Phil Burk523b3042017-09-13 13:03:08 -0700104 // 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 Burk39f02dd2017-08-04 09:13:31 -0700110
Phil Burk83fb8442017-10-05 16:55:17 -0700111 bool underflowed = mMixer.mix(index, fifo, allowUnderflow);
Phil Burk523b3042017-09-13 13:03:08 -0700112 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 Burk39f02dd2017-08-04 09:13:31 -0700125 }
126
127 index++; // just used for labelling tracks in systrace
Phil Burk87c9f642017-05-17 07:22:39 -0700128 }
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 Burk39f02dd2017-08-04 09:13:31 -0700135 AAudioServiceEndpointShared::disconnectRegisteredStreams();
Phil Burk87c9f642017-05-17 07:22:39 -0700136 break;
137 } else if (result != getFramesPerBurst()) {
138 ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d",
139 result, getFramesPerBurst());
140 break;
141 }
142 }
143
Phil Burk87c9f642017-05-17 07:22:39 -0700144 return NULL; // TODO review
145}