blob: 81f1d1b120bd67384925e0f0cbadf2c81f8686c0 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Eric Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceEndpoint"
Phil Burk71f35bb2017-04-13 16:05:07 -070018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk4501b352017-06-29 18:12:36 -070021#include <algorithm>
Phil Burk71f35bb2017-04-13 16:05:07 -070022#include <assert.h>
23#include <map>
24#include <mutex>
Phil Burk4501b352017-06-29 18:12:36 -070025#include <sstream>
26#include <vector>
27
Phil Burk71f35bb2017-04-13 16:05:07 -070028#include <utils/Singleton.h>
29
30#include "AAudioEndpointManager.h"
31#include "AAudioServiceEndpoint.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
33#include "core/AudioStreamBuilder.h"
34#include "AAudioServiceEndpoint.h"
35#include "AAudioServiceStreamShared.h"
36
37using namespace android; // TODO just import names needed
38using namespace aaudio; // TODO just import names needed
39
40#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND)
41
42// Wait at least this many times longer than the operation should take.
43#define MIN_TIMEOUT_OPERATIONS 4
44
Phil Burk71f35bb2017-04-13 16:05:07 -070045// This is the maximum size in frames. The effective size can be tuned smaller at runtime.
46#define DEFAULT_BUFFER_CAPACITY (48 * 8)
47
Phil Burk4501b352017-06-29 18:12:36 -070048std::string AAudioServiceEndpoint::dump() const {
49 std::stringstream result;
50
51 const bool isLocked = AAudio_tryUntilTrue(
52 [this]()->bool { return mLockStreams.try_lock(); } /* f */,
53 50 /* times */,
54 20 /* sleepMs */);
55 if (!isLocked) {
56 result << "EndpointManager may be deadlocked\n";
57 }
58
59 AudioStreamInternal *stream = mStreamInternal;
60 if (stream == nullptr) {
61 result << "null stream!" << "\n";
62 } else {
63 result << "mmap stream: rate = " << stream->getSampleRate() << "\n";
64 }
65
66 result << " Registered Streams:" << "\n";
Phil Burka5222e22017-07-28 13:31:14 -070067 result << AAudioServiceStreamShared::dumpHeader() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070068 for (sp<AAudioServiceStreamShared> sharedStream : mRegisteredStreams) {
Phil Burka5222e22017-07-28 13:31:14 -070069 result << sharedStream->dump() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070070 }
71
72 if (isLocked) {
73 mLockStreams.unlock();
74 }
75 return result.str();
76}
77
Phil Burkc0c70e32017-02-09 13:18:38 -080078// Set up an EXCLUSIVE MMAP stream that will be shared.
Eric Laurenta17ae742017-06-29 15:43:55 -070079aaudio_result_t AAudioServiceEndpoint::open(const AAudioStreamConfiguration& configuration) {
80 mRequestedDeviceId = configuration.getDeviceId();
Phil Burk87c9f642017-05-17 07:22:39 -070081 mStreamInternal = getStreamInternal();
82
Phil Burkc0c70e32017-02-09 13:18:38 -080083 AudioStreamBuilder builder;
84 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burk71f35bb2017-04-13 16:05:07 -070085 // Don't fall back to SHARED because that would cause recursion.
86 builder.setSharingModeMatchRequired(true);
Eric Laurenta17ae742017-06-29 15:43:55 -070087 builder.setDeviceId(mRequestedDeviceId);
jiabin901f65d2017-07-12 17:56:35 -070088 builder.setFormat(configuration.getFormat());
Eric Laurenta17ae742017-06-29 15:43:55 -070089 builder.setSampleRate(configuration.getSampleRate());
90 builder.setSamplesPerFrame(configuration.getSamplesPerFrame());
Phil Burk87c9f642017-05-17 07:22:39 -070091 builder.setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -070092 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
93
Phil Burk87c9f642017-05-17 07:22:39 -070094 return getStreamInternal()->open(builder);
Phil Burkc0c70e32017-02-09 13:18:38 -080095}
96
97aaudio_result_t AAudioServiceEndpoint::close() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070098 return getStreamInternal()->close();
Phil Burkc0c70e32017-02-09 13:18:38 -080099}
100
101// TODO, maybe use an interface to reduce exposure
Phil Burk11e8d332017-05-24 09:59:02 -0700102aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 std::lock_guard<std::mutex> lock(mLockStreams);
104 mRegisteredStreams.push_back(sharedStream);
105 return AAUDIO_OK;
106}
107
Phil Burk11e8d332017-05-24 09:59:02 -0700108aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 std::lock_guard<std::mutex> lock(mLockStreams);
110 mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream),
111 mRegisteredStreams.end());
112 return AAUDIO_OK;
113}
114
Phil Burk11e8d332017-05-24 09:59:02 -0700115aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700116 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700117 if (++mRunningStreams == 1) {
Phil Burk940083c2017-07-17 17:00:02 -0700118 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
119 std::lock_guard<std::mutex> lock(mLockStreams);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700120 result = getStreamInternal()->requestStart();
Phil Burk87c9f642017-05-17 07:22:39 -0700121 startSharingThread_l();
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700123 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800124}
125
Phil Burk11e8d332017-05-24 09:59:02 -0700126aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
Phil Burk940083c2017-07-17 17:00:02 -0700127 // Don't lock here because the disconnectRegisteredStreams also uses the lock.
128 if (--mRunningStreams == 0) { // atomic
Phil Burk87c9f642017-05-17 07:22:39 -0700129 stopSharingThread();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700130 getStreamInternal()->requestStop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 }
132 return AAUDIO_OK;
133}
134
Phil Burk87c9f642017-05-17 07:22:39 -0700135static void *aaudio_endpoint_thread_proc(void *context) {
136 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
137 if (endpoint != NULL) {
138 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 } else {
140 return NULL;
141 }
142}
143
Phil Burk87c9f642017-05-17 07:22:39 -0700144aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700146 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 * AAUDIO_NANOS_PER_SECOND
148 / getSampleRate();
149 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700150 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800151}
152
Phil Burk87c9f642017-05-17 07:22:39 -0700153aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800154 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700155 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
Phil Burk87c9f642017-05-17 07:22:39 -0700156 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800157}
158
159void AAudioServiceEndpoint::disconnectRegisteredStreams() {
160 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk11e8d332017-05-24 09:59:02 -0700161 for(auto sharedStream : mRegisteredStreams) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700162 sharedStream->stop();
Phil Burk5ef003b2017-06-30 11:43:37 -0700163 sharedStream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 }
165 mRegisteredStreams.clear();
166}
Eric Laurenta17ae742017-06-29 15:43:55 -0700167
168bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
169 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
170 configuration.getDeviceId() != mStreamInternal->getDeviceId()) {
171 return false;
172 }
173 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
174 configuration.getSampleRate() != mStreamInternal->getSampleRate()) {
175 return false;
176 }
177 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
178 configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) {
179 return false;
180 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700181 return true;
182}
Eric Laurentcb4dae22017-07-01 19:39:32 -0700183
Phil Burk97350f92017-07-21 15:59:44 -0700184
185aaudio_result_t AAudioServiceEndpoint::getTimestamp(int64_t *positionFrames, int64_t *timeNanos) {
186 return mStreamInternal->getTimestamp(CLOCK_MONOTONIC, positionFrames, timeNanos);
187}