blob: 1dd0c3ac51bd0584f35f7fc9212835b31c1bc877 [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 Burk58f5ce12020-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());
jiabina9094092021-06-28 20:36:45 +000081 setChannelMask(mStreamInternal->getChannelMask());
Phil Burk39f02dd2017-08-04 09:13:31 -070082 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
Robert Wu310037a2022-09-06 21:48:18 +000085 setHardwareSampleRate(mStreamInternal->getHardwareSampleRate());
86 setHardwareFormat(mStreamInternal->getHardwareFormat());
87 setHardwareSamplesPerFrame(mStreamInternal->getHardwareSamplesPerFrame());
Phil Burk39f02dd2017-08-04 09:13:31 -070088 mFramesPerBurst = mStreamInternal->getFramesPerBurst();
89
90 return result;
91}
92
Phil Burk320910f2020-08-12 14:29:10 +000093void AAudioServiceEndpointShared::close() {
Phil Burkdd582922020-10-15 20:29:51 +000094 stopSharingThread();
95 getStreamInternal()->safeReleaseClose();
Phil Burk39f02dd2017-08-04 09:13:31 -070096}
97
98// Glue between C and C++ callbacks.
Phil Burkbe645502018-03-22 13:48:18 -070099static void *aaudio_endpoint_thread_proc(void *arg) {
100 assert(arg != nullptr);
Phil Burkdd582922020-10-15 20:29:51 +0000101 ALOGD("%s() called", __func__);
Phil Burkbe645502018-03-22 13:48:18 -0700102
Phil Burkdd582922020-10-15 20:29:51 +0000103 // Prevent the stream from being deleted while being used.
104 // This is just for extra safety. It is probably not needed because
105 // this callback should be joined before the stream is closed.
jiabin613e6ae2022-12-21 20:20:11 +0000106 auto endpointPtr = static_cast<AAudioServiceEndpointShared *>(arg);
Phil Burkdd582922020-10-15 20:29:51 +0000107 android::sp<AAudioServiceEndpointShared> endpoint(endpointPtr);
108 // Balance the incStrong() in startSharingThread_l().
109 endpoint->decStrong(nullptr);
110
Phil Burkbe645502018-03-22 13:48:18 -0700111 void *result = endpoint->callbackLoop();
112 // Close now so that the HW resource is freed and we can open a new device.
113 if (!endpoint->isConnected()) {
Phil Burkdd582922020-10-15 20:29:51 +0000114 ALOGD("%s() call safeReleaseCloseFromCallback()", __func__);
115 // Release and close under a lock with no check for callback collisions.
Phil Burk5ff3b952021-04-02 17:29:11 +0000116 endpoint->getStreamInternal()->safeReleaseCloseInternal();
Phil Burk39f02dd2017-08-04 09:13:31 -0700117 }
Phil Burkbe645502018-03-22 13:48:18 -0700118
119 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700120}
121
122aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
123 // Launch the callback loop thread.
124 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
125 * AAUDIO_NANOS_PER_SECOND
126 / getSampleRate();
127 mCallbackEnabled.store(true);
Phil Burkdd582922020-10-15 20:29:51 +0000128 // Prevent this object from getting deleted before the thread has a chance to create
129 // its strong pointer. Assume the thread will call decStrong().
130 this->incStrong(nullptr);
Phil Burkf28b33d2021-07-17 01:04:00 +0000131 aaudio_result_t result = getStreamInternal()->createThread(periodNanos,
132 aaudio_endpoint_thread_proc,
133 this);
Phil Burkbe645502018-03-22 13:48:18 -0700134 if (result != AAUDIO_OK) {
Phil Burkdd582922020-10-15 20:29:51 +0000135 this->decStrong(nullptr); // Because the thread won't do it.
Phil Burkbe645502018-03-22 13:48:18 -0700136 }
137 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700138}
139
140aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
141 mCallbackEnabled.store(false);
jiabin613e6ae2022-12-21 20:20:11 +0000142 return getStreamInternal()->joinThread(nullptr);
Phil Burk39f02dd2017-08-04 09:13:31 -0700143}
144
Phil Burk0bd745e2020-10-17 18:20:01 +0000145aaudio_result_t AAudioServiceEndpointShared::startStream(
146 sp<AAudioServiceStreamBase> sharedStream,
147 audio_port_handle_t *clientHandle)
148 NO_THREAD_SAFETY_ANALYSIS {
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700150
151 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700153 if (++mRunningStreamCount == 1) { // atomic
Phil Burk0bd745e2020-10-17 18:20:01 +0000154 result = getStreamInternal()->systemStart();
Phil Burkb92c9882017-09-15 11:39:15 -0700155 if (result != AAUDIO_OK) {
156 --mRunningStreamCount;
157 } else {
158 result = startSharingThread_l();
159 if (result != AAUDIO_OK) {
Phil Burk0bd745e2020-10-17 18:20:01 +0000160 getStreamInternal()->systemStopFromApp();
Phil Burkb92c9882017-09-15 11:39:15 -0700161 --mRunningStreamCount;
162 }
163 }
164 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 }
Phil Burkb92c9882017-09-15 11:39:15 -0700166
Phil Burk39f02dd2017-08-04 09:13:31 -0700167 if (result == AAUDIO_OK) {
jiabind1f1cb62020-03-24 11:57:57 -0700168 const audio_attributes_t attr = getAudioAttributesFrom(sharedStream.get());
169 result = getStreamInternal()->startClient(
170 sharedStream->getAudioClient(), &attr, clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700171 if (result != AAUDIO_OK) {
172 if (--mRunningStreamCount == 0) { // atomic
173 stopSharingThread();
Phil Burk0bd745e2020-10-17 18:20:01 +0000174 getStreamInternal()->systemStopFromApp();
Phil Burkb92c9882017-09-15 11:39:15 -0700175 }
176 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700177 }
Phil Burkb92c9882017-09-15 11:39:15 -0700178
Phil Burk39f02dd2017-08-04 09:13:31 -0700179 return result;
180}
181
jiabin613e6ae2022-12-21 20:20:11 +0000182aaudio_result_t AAudioServiceEndpointShared::stopStream(
183 sp<AAudioServiceStreamBase> /*sharedStream*/, audio_port_handle_t clientHandle) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700184 // Ignore result.
185 (void) getStreamInternal()->stopClient(clientHandle);
186
187 if (--mRunningStreamCount == 0) { // atomic
Phil Burk7ebbc112020-05-13 15:55:17 -0700188 stopSharingThread(); // the sharing thread locks mLockStreams
Phil Burk0bd745e2020-10-17 18:20:01 +0000189 getStreamInternal()->systemStopFromApp();
Phil Burk39f02dd2017-08-04 09:13:31 -0700190 }
191 return AAUDIO_OK;
192}
193
Phil Burk39f02dd2017-08-04 09:13:31 -0700194// Get timestamp that was written by the real-time service thread, eg. mixer.
195aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
196 int64_t *timeNanos) {
Phil Burka53ffa62018-10-10 16:21:37 -0700197 if (mAtomicEndpointTimestamp.isValid()) {
198 Timestamp timestamp = mAtomicEndpointTimestamp.read();
Phil Burk39f02dd2017-08-04 09:13:31 -0700199 *positionFrames = timestamp.getPosition();
200 *timeNanos = timestamp.getNanoseconds();
201 return AAUDIO_OK;
202 } else {
203 return AAUDIO_ERROR_UNAVAILABLE;
204 }
205}
206
207aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
208 int64_t *timeNanos) {
Phil Burk56d34f22018-10-11 13:30:56 -0700209 aaudio_result_t result = mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
210 if (result == AAUDIO_ERROR_INVALID_STATE) {
211 // getTimestamp() can return AAUDIO_ERROR_INVALID_STATE if the stream has
212 // not completely started. This can cause a race condition that kills the
213 // timestamp service thread. So we reduce the error to a less serious one
214 // that allows the timestamp thread to continue.
215 result = AAUDIO_ERROR_UNAVAILABLE;
216 }
217 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700218}
jiabin6604ce92022-01-22 00:29:17 +0000219
220void AAudioServiceEndpointShared::handleDisconnectRegisteredStreamsAsync() {
221 android::sp<AAudioServiceEndpointShared> holdEndpoint(this);
222 std::thread asyncTask([holdEndpoint]() {
223 // We do not need the returned vector.
224 holdEndpoint->disconnectRegisteredStreams();
225 });
226 asyncTask.detach();
227}