blob: c42a6e20b0d1634ba06980ca446b58d0d7b7c799 [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) {
84 if (!clientStream->isRunning()) {
85 continue;
Eric Laurentcb4dae22017-07-01 19:39:32 -070086 }
Phil Burk39f02dd2017-08-04 09:13:31 -070087
88 AAudioServiceStreamShared *streamShared =
89 static_cast<AAudioServiceStreamShared *>(clientStream.get());
90
91 FifoBuffer *fifo = streamShared->getDataFifoBuffer();
92 // Determine offset between framePosition in client's stream vs the underlying
93 // MMAP stream.
94 int64_t clientFramesRead = fifo->getReadCounter();
95 // These two indices refer to the same frame.
96 int64_t positionOffset = mmapFramesWritten - clientFramesRead;
97 streamShared->setTimestampPositionOffset(positionOffset);
98
99 float volume = 1.0; // to match legacy volume
100 bool underflowed = mMixer.mix(index, fifo, volume);
101
102 // This timestamp represents the completion of data being read out of the
103 // client buffer. It is sent to the client and used in the timing model
104 // to decide when the client has room to write more data.
105 Timestamp timestamp(fifo->getReadCounter(), AudioClock::getNanoseconds());
106 streamShared->markTransferTime(timestamp);
107
108 if (underflowed) {
109 streamShared->incrementXRunCount();
110 }
111
112 index++; // just used for labelling tracks in systrace
Phil Burk87c9f642017-05-17 07:22:39 -0700113 }
114 }
115
116 // Write mixer output to stream using a blocking write.
117 result = getStreamInternal()->write(mMixer.getOutputBuffer(),
118 getFramesPerBurst(), timeoutNanos);
119 if (result == AAUDIO_ERROR_DISCONNECTED) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 AAudioServiceEndpointShared::disconnectRegisteredStreams();
Phil Burk87c9f642017-05-17 07:22:39 -0700121 break;
122 } else if (result != getFramesPerBurst()) {
123 ALOGW("AAudioServiceEndpoint(): callbackLoop() wrote %d / %d",
124 result, getFramesPerBurst());
125 break;
126 }
127 }
128
Phil Burk87c9f642017-05-17 07:22:39 -0700129 return NULL; // TODO review
130}