blob: f5de59ffbcd619f5a600868b0826f340b1b9308b [file] [log] [blame]
Phil Burk39f02dd2017-08-04 09:13:31 -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
17
18#define LOG_TAG "AAudioServiceEndpointShared"
19//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25
26#include "binding/AAudioServiceMessage.h"
27#include "client/AudioStreamInternal.h"
28#include "client/AudioStreamInternalPlay.h"
29#include "core/AudioStreamBuilder.h"
30
31#include "AAudioServiceEndpointShared.h"
32#include "AAudioServiceStreamShared.h"
33#include "AAudioServiceStreamMMAP.h"
34#include "AAudioMixer.h"
35#include "AAudioService.h"
36
37using namespace android;
38using namespace aaudio;
39
40// This is the maximum size in frames. The effective size can be tuned smaller at runtime.
41#define DEFAULT_BUFFER_CAPACITY (48 * 8)
42
Phil Burk79224ca2020-08-12 14:29:10 +000043AAudioServiceEndpointShared::AAudioServiceEndpointShared(AudioStreamInternal *streamInternal)
44 : mStreamInternal(streamInternal) {}
45
Phil Burk39f02dd2017-08-04 09:13:31 -070046std::string AAudioServiceEndpointShared::dump() const {
47 std::stringstream result;
48
49 result << " SHARED: sharing exclusive stream with handle = 0x"
50 << std::setfill('0') << std::setw(8)
51 << std::hex << mStreamInternal->getServiceHandle()
52 << std::dec << std::setfill(' ');
Phil Burk23296382017-11-20 15:45:11 -080053 result << ", XRuns = " << mStreamInternal->getXRunCount();
Phil Burk39f02dd2017-08-04 09:13:31 -070054 result << "\n";
55 result << " Running Stream Count: " << mRunningStreamCount << "\n";
56
57 result << AAudioServiceEndpoint::dump();
58 return result.str();
59}
60
61// Share an AudioStreamInternal.
62aaudio_result_t AAudioServiceEndpointShared::open(const aaudio::AAudioStreamRequest &request) {
63 aaudio_result_t result = AAUDIO_OK;
64 const AAudioStreamConfiguration &configuration = request.getConstantConfiguration();
65
Phil Burka62fb952018-01-16 12:44:06 -080066 copyFrom(configuration);
Phil Burk39f02dd2017-08-04 09:13:31 -070067 mRequestedDeviceId = configuration.getDeviceId();
Phil Burk39f02dd2017-08-04 09:13:31 -070068
69 AudioStreamBuilder builder;
Phil Burka62fb952018-01-16 12:44:06 -080070 builder.copyFrom(configuration);
71
Phil Burk39f02dd2017-08-04 09:13:31 -070072 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
73 // Don't fall back to SHARED because that would cause recursion.
74 builder.setSharingModeMatchRequired(true);
Phil Burka62fb952018-01-16 12:44:06 -080075
Phil Burk39f02dd2017-08-04 09:13:31 -070076 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
77
78 result = mStreamInternal->open(builder);
79
80 setSampleRate(mStreamInternal->getSampleRate());
81 setSamplesPerFrame(mStreamInternal->getSamplesPerFrame());
82 setDeviceId(mStreamInternal->getDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -080083 setSessionId(mStreamInternal->getSessionId());
Phil Burk0127c1b2018-03-29 13:48:06 -070084 setFormat(AUDIO_FORMAT_PCM_FLOAT); // force for mixer
Phil Burk39f02dd2017-08-04 09:13:31 -070085 mFramesPerBurst = mStreamInternal->getFramesPerBurst();
86
87 return result;
88}
89
Phil Burk47190482020-08-12 14:29:10 +000090void AAudioServiceEndpointShared::close() {
91 getStreamInternal()->releaseCloseFinal();
Phil Burk39f02dd2017-08-04 09:13:31 -070092}
93
94// Glue between C and C++ callbacks.
Phil Burkbe645502018-03-22 13:48:18 -070095static void *aaudio_endpoint_thread_proc(void *arg) {
96 assert(arg != nullptr);
97
98 // The caller passed in a smart pointer to prevent the endpoint from getting deleted
99 // while the thread was launching.
100 sp<AAudioServiceEndpointShared> *endpointForThread =
101 static_cast<sp<AAudioServiceEndpointShared> *>(arg);
102 sp<AAudioServiceEndpointShared> endpoint = *endpointForThread;
103 delete endpointForThread; // Just use scoped smart pointer. Don't need this anymore.
104 void *result = endpoint->callbackLoop();
105 // Close now so that the HW resource is freed and we can open a new device.
106 if (!endpoint->isConnected()) {
107 endpoint->close();
Phil Burk39f02dd2017-08-04 09:13:31 -0700108 }
Phil Burkbe645502018-03-22 13:48:18 -0700109
110 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700111}
112
113aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
114 // Launch the callback loop thread.
115 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
116 * AAUDIO_NANOS_PER_SECOND
117 / getSampleRate();
118 mCallbackEnabled.store(true);
Phil Burkbe645502018-03-22 13:48:18 -0700119 // Pass a smart pointer so the thread can hold a reference.
120 sp<AAudioServiceEndpointShared> *endpointForThread = new sp<AAudioServiceEndpointShared>(this);
121 aaudio_result_t result = getStreamInternal()->createThread(periodNanos,
122 aaudio_endpoint_thread_proc,
123 endpointForThread);
124 if (result != AAUDIO_OK) {
125 // The thread can't delete it so we have to do it here.
126 delete endpointForThread;
127 }
128 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700129}
130
131aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
132 mCallbackEnabled.store(false);
133 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
134 return result;
135}
136
137aaudio_result_t AAudioServiceEndpointShared::startStream(sp<AAudioServiceStreamBase> sharedStream,
138 audio_port_handle_t *clientHandle) {
139 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700140
141 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700143 if (++mRunningStreamCount == 1) { // atomic
144 result = getStreamInternal()->requestStart();
145 if (result != AAUDIO_OK) {
146 --mRunningStreamCount;
147 } else {
148 result = startSharingThread_l();
149 if (result != AAUDIO_OK) {
150 getStreamInternal()->requestStop();
151 --mRunningStreamCount;
152 }
153 }
154 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700155 }
Phil Burkb92c9882017-09-15 11:39:15 -0700156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 if (result == AAUDIO_OK) {
jiabind1f1cb62020-03-24 11:57:57 -0700158 const audio_attributes_t attr = getAudioAttributesFrom(sharedStream.get());
159 result = getStreamInternal()->startClient(
160 sharedStream->getAudioClient(), &attr, clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700161 if (result != AAUDIO_OK) {
162 if (--mRunningStreamCount == 0) { // atomic
163 stopSharingThread();
164 getStreamInternal()->requestStop();
165 }
166 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700167 }
Phil Burkb92c9882017-09-15 11:39:15 -0700168
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 return result;
170}
171
172aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
173 audio_port_handle_t clientHandle) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 // Ignore result.
175 (void) getStreamInternal()->stopClient(clientHandle);
176
177 if (--mRunningStreamCount == 0) { // atomic
Phil Burk7ebbc112020-05-13 15:55:17 -0700178 stopSharingThread(); // the sharing thread locks mLockStreams
Phil Burk39f02dd2017-08-04 09:13:31 -0700179 getStreamInternal()->requestStop();
180 }
181 return AAUDIO_OK;
182}
183
Phil Burk39f02dd2017-08-04 09:13:31 -0700184// Get timestamp that was written by the real-time service thread, eg. mixer.
185aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
186 int64_t *timeNanos) {
Phil Burka53ffa62018-10-10 16:21:37 -0700187 if (mAtomicEndpointTimestamp.isValid()) {
188 Timestamp timestamp = mAtomicEndpointTimestamp.read();
Phil Burk39f02dd2017-08-04 09:13:31 -0700189 *positionFrames = timestamp.getPosition();
190 *timeNanos = timestamp.getNanoseconds();
191 return AAUDIO_OK;
192 } else {
193 return AAUDIO_ERROR_UNAVAILABLE;
194 }
195}
196
197aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
198 int64_t *timeNanos) {
Phil Burk56d34f22018-10-11 13:30:56 -0700199 aaudio_result_t result = mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
200 if (result == AAUDIO_ERROR_INVALID_STATE) {
201 // getTimestamp() can return AAUDIO_ERROR_INVALID_STATE if the stream has
202 // not completely started. This can cause a race condition that kills the
203 // timestamp service thread. So we reduce the error to a less serious one
204 // that allows the timestamp thread to continue.
205 result = AAUDIO_ERROR_UNAVAILABLE;
206 }
207 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700208}