blob: 501e8c0cc30f86e9991f02e2d10fdafb8a28871c [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());
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 Burk320910f2020-08-12 14:29:10 +000090void AAudioServiceEndpointShared::close() {
Phil Burkdd582922020-10-15 20:29:51 +000091 stopSharingThread();
92 getStreamInternal()->safeReleaseClose();
Phil Burk39f02dd2017-08-04 09:13:31 -070093}
94
95// Glue between C and C++ callbacks.
Phil Burkbe645502018-03-22 13:48:18 -070096static void *aaudio_endpoint_thread_proc(void *arg) {
97 assert(arg != nullptr);
Phil Burkdd582922020-10-15 20:29:51 +000098 ALOGD("%s() called", __func__);
Phil Burkbe645502018-03-22 13:48:18 -070099
Phil Burkdd582922020-10-15 20:29:51 +0000100 // Prevent the stream from being deleted while being used.
101 // This is just for extra safety. It is probably not needed because
102 // this callback should be joined before the stream is closed.
103 AAudioServiceEndpointShared *endpointPtr =
104 static_cast<AAudioServiceEndpointShared *>(arg);
105 android::sp<AAudioServiceEndpointShared> endpoint(endpointPtr);
106 // Balance the incStrong() in startSharingThread_l().
107 endpoint->decStrong(nullptr);
108
Phil Burkbe645502018-03-22 13:48:18 -0700109 void *result = endpoint->callbackLoop();
110 // Close now so that the HW resource is freed and we can open a new device.
111 if (!endpoint->isConnected()) {
Phil Burkdd582922020-10-15 20:29:51 +0000112 ALOGD("%s() call safeReleaseCloseFromCallback()", __func__);
113 // Release and close under a lock with no check for callback collisions.
114 endpoint->getStreamInternal()->safeReleaseCloseFromCallback();
Phil Burk39f02dd2017-08-04 09:13:31 -0700115 }
Phil Burkbe645502018-03-22 13:48:18 -0700116
117 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700118}
119
120aaudio_result_t aaudio::AAudioServiceEndpointShared::startSharingThread_l() {
121 // Launch the callback loop thread.
122 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
123 * AAUDIO_NANOS_PER_SECOND
124 / getSampleRate();
125 mCallbackEnabled.store(true);
Phil Burkdd582922020-10-15 20:29:51 +0000126 // Prevent this object from getting deleted before the thread has a chance to create
127 // its strong pointer. Assume the thread will call decStrong().
128 this->incStrong(nullptr);
129 aaudio_result_t result = getStreamInternal()->createThread_l(periodNanos,
130 aaudio_endpoint_thread_proc,
131 this);
Phil Burkbe645502018-03-22 13:48:18 -0700132 if (result != AAUDIO_OK) {
Phil Burkdd582922020-10-15 20:29:51 +0000133 this->decStrong(nullptr); // Because the thread won't do it.
Phil Burkbe645502018-03-22 13:48:18 -0700134 }
135 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700136}
137
138aaudio_result_t aaudio::AAudioServiceEndpointShared::stopSharingThread() {
139 mCallbackEnabled.store(false);
Phil Burk0bd745e2020-10-17 18:20:01 +0000140 return getStreamInternal()->joinThread(NULL);
Phil Burk39f02dd2017-08-04 09:13:31 -0700141}
142
Phil Burk0bd745e2020-10-17 18:20:01 +0000143aaudio_result_t AAudioServiceEndpointShared::startStream(
144 sp<AAudioServiceStreamBase> sharedStream,
145 audio_port_handle_t *clientHandle)
146 NO_THREAD_SAFETY_ANALYSIS {
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 aaudio_result_t result = AAUDIO_OK;
Phil Burkb92c9882017-09-15 11:39:15 -0700148
149 {
Phil Burk39f02dd2017-08-04 09:13:31 -0700150 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkb92c9882017-09-15 11:39:15 -0700151 if (++mRunningStreamCount == 1) { // atomic
Phil Burk0bd745e2020-10-17 18:20:01 +0000152 result = getStreamInternal()->systemStart();
Phil Burkb92c9882017-09-15 11:39:15 -0700153 if (result != AAUDIO_OK) {
154 --mRunningStreamCount;
155 } else {
156 result = startSharingThread_l();
157 if (result != AAUDIO_OK) {
Phil Burk0bd745e2020-10-17 18:20:01 +0000158 getStreamInternal()->systemStopFromApp();
Phil Burkb92c9882017-09-15 11:39:15 -0700159 --mRunningStreamCount;
160 }
161 }
162 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700163 }
Phil Burkb92c9882017-09-15 11:39:15 -0700164
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 if (result == AAUDIO_OK) {
jiabind1f1cb62020-03-24 11:57:57 -0700166 const audio_attributes_t attr = getAudioAttributesFrom(sharedStream.get());
167 result = getStreamInternal()->startClient(
168 sharedStream->getAudioClient(), &attr, clientHandle);
Phil Burkb92c9882017-09-15 11:39:15 -0700169 if (result != AAUDIO_OK) {
170 if (--mRunningStreamCount == 0) { // atomic
171 stopSharingThread();
Phil Burk0bd745e2020-10-17 18:20:01 +0000172 getStreamInternal()->systemStopFromApp();
Phil Burkb92c9882017-09-15 11:39:15 -0700173 }
174 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700175 }
Phil Burkb92c9882017-09-15 11:39:15 -0700176
Phil Burk39f02dd2017-08-04 09:13:31 -0700177 return result;
178}
179
180aaudio_result_t AAudioServiceEndpointShared::stopStream(sp<AAudioServiceStreamBase> sharedStream,
181 audio_port_handle_t clientHandle) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700182 // Ignore result.
183 (void) getStreamInternal()->stopClient(clientHandle);
184
185 if (--mRunningStreamCount == 0) { // atomic
Phil Burk7ebbc112020-05-13 15:55:17 -0700186 stopSharingThread(); // the sharing thread locks mLockStreams
Phil Burk0bd745e2020-10-17 18:20:01 +0000187 getStreamInternal()->systemStopFromApp();
Phil Burk39f02dd2017-08-04 09:13:31 -0700188 }
189 return AAUDIO_OK;
190}
191
Phil Burk39f02dd2017-08-04 09:13:31 -0700192// Get timestamp that was written by the real-time service thread, eg. mixer.
193aaudio_result_t AAudioServiceEndpointShared::getFreeRunningPosition(int64_t *positionFrames,
194 int64_t *timeNanos) {
Phil Burka53ffa62018-10-10 16:21:37 -0700195 if (mAtomicEndpointTimestamp.isValid()) {
196 Timestamp timestamp = mAtomicEndpointTimestamp.read();
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 *positionFrames = timestamp.getPosition();
198 *timeNanos = timestamp.getNanoseconds();
199 return AAUDIO_OK;
200 } else {
201 return AAUDIO_ERROR_UNAVAILABLE;
202 }
203}
204
205aaudio_result_t AAudioServiceEndpointShared::getTimestamp(int64_t *positionFrames,
206 int64_t *timeNanos) {
Phil Burk56d34f22018-10-11 13:30:56 -0700207 aaudio_result_t result = mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
208 if (result == AAUDIO_ERROR_INVALID_STATE) {
209 // getTimestamp() can return AAUDIO_ERROR_INVALID_STATE if the stream has
210 // not completely started. This can cause a race condition that kills the
211 // timestamp service thread. So we reduce the error to a less serious one
212 // that allows the timestamp thread to continue.
213 result = AAUDIO_ERROR_UNAVAILABLE;
214 }
215 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700216}