blob: 9b1833ac7fa5199abbb2aa18a631d2fbbaa13a94 [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;
85
Phil Burk39f02dd2017-08-04 09:13:31 -070086 if (!clientStream->isRunning()) {
87 continue;
Eric Laurentcb4dae22017-07-01 19:39:32 -070088 }
Phil Burk39f02dd2017-08-04 09:13:31 -070089
Phil Burk523b3042017-09-13 13:03:08 -070090 sp<AAudioServiceStreamShared> streamShared =
Phil Burk39f02dd2017-08-04 09:13:31 -070091 static_cast<AAudioServiceStreamShared *>(clientStream.get());
92
Phil Burk523b3042017-09-13 13:03:08 -070093 {
94 // Lock the AudioFifo to protect against close.
95 std::lock_guard <std::mutex> lock(streamShared->getAudioDataQueueLock());
Phil Burk39f02dd2017-08-04 09:13:31 -070096
Phil Burk523b3042017-09-13 13:03:08 -070097 FifoBuffer *fifo = streamShared->getAudioDataFifoBuffer_l();
98 if (fifo != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -070099
Phil Burk523b3042017-09-13 13:03:08 -0700100 // Determine offset between framePosition in client's stream
101 // vs the underlying MMAP stream.
102 clientFramesRead = fifo->getReadCounter();
103 // These two indices refer to the same frame.
104 int64_t positionOffset = mmapFramesWritten - clientFramesRead;
105 streamShared->setTimestampPositionOffset(positionOffset);
Phil Burk39f02dd2017-08-04 09:13:31 -0700106
Phil Burk523b3042017-09-13 13:03:08 -0700107 float volume = 1.0; // to match legacy volume
108 bool underflowed = mMixer.mix(index, fifo, volume);
109 if (underflowed) {
110 streamShared->incrementXRunCount();
111 }
112 clientFramesRead = fifo->getReadCounter();
113 }
114 }
115
116 if (clientFramesRead > 0) {
117 // This timestamp represents the completion of data being read out of the
118 // client buffer. It is sent to the client and used in the timing model
119 // to decide when the client has room to write more data.
120 Timestamp timestamp(clientFramesRead, AudioClock::getNanoseconds());
121 streamShared->markTransferTime(timestamp);
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 }
123
124 index++; // just used for labelling tracks in systrace
Phil Burk87c9f642017-05-17 07:22:39 -0700125 }
126 }
127
128 // Write mixer output to stream using a blocking write.
129 result = getStreamInternal()->write(mMixer.getOutputBuffer(),
130 getFramesPerBurst(), timeoutNanos);
131 if (result == AAUDIO_ERROR_DISCONNECTED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700132 AAudioServiceEndpointShared::disconnectRegisteredStreams();
Phil Burk87c9f642017-05-17 07:22:39 -0700133 break;
134 } else if (result != getFramesPerBurst()) {
135 ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d",
136 result, getFramesPerBurst());
137 break;
138 }
139 }
140
Phil Burk87c9f642017-05-17 07:22:39 -0700141 return NULL; // TODO review
142}