blob: 0f863fe160b10198bfb4d7ea123677683843e568 [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";
67 for (sp<AAudioServiceStreamShared> sharedStream : mRegisteredStreams) {
68 result << sharedStream->dump();
69 }
70
71 if (isLocked) {
72 mLockStreams.unlock();
73 }
74 return result.str();
75}
76
Phil Burkc0c70e32017-02-09 13:18:38 -080077// Set up an EXCLUSIVE MMAP stream that will be shared.
Eric Laurenta17ae742017-06-29 15:43:55 -070078aaudio_result_t AAudioServiceEndpoint::open(const AAudioStreamConfiguration& configuration) {
79 mRequestedDeviceId = configuration.getDeviceId();
Phil Burk87c9f642017-05-17 07:22:39 -070080 mStreamInternal = getStreamInternal();
81
Phil Burkc0c70e32017-02-09 13:18:38 -080082 AudioStreamBuilder builder;
83 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burk71f35bb2017-04-13 16:05:07 -070084 // Don't fall back to SHARED because that would cause recursion.
85 builder.setSharingModeMatchRequired(true);
Eric Laurenta17ae742017-06-29 15:43:55 -070086 builder.setDeviceId(mRequestedDeviceId);
jiabin901f65d2017-07-12 17:56:35 -070087 builder.setFormat(configuration.getFormat());
Eric Laurenta17ae742017-06-29 15:43:55 -070088 builder.setSampleRate(configuration.getSampleRate());
89 builder.setSamplesPerFrame(configuration.getSamplesPerFrame());
Phil Burk87c9f642017-05-17 07:22:39 -070090 builder.setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -070091 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
92
Phil Burk87c9f642017-05-17 07:22:39 -070093 return getStreamInternal()->open(builder);
Phil Burkc0c70e32017-02-09 13:18:38 -080094}
95
96aaudio_result_t AAudioServiceEndpoint::close() {
Eric Laurentcb4dae22017-07-01 19:39:32 -070097 return getStreamInternal()->close();
Phil Burkc0c70e32017-02-09 13:18:38 -080098}
99
100// TODO, maybe use an interface to reduce exposure
Phil Burk11e8d332017-05-24 09:59:02 -0700101aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 std::lock_guard<std::mutex> lock(mLockStreams);
103 mRegisteredStreams.push_back(sharedStream);
104 return AAUDIO_OK;
105}
106
Phil Burk11e8d332017-05-24 09:59:02 -0700107aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 std::lock_guard<std::mutex> lock(mLockStreams);
109 mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream),
110 mRegisteredStreams.end());
111 return AAUDIO_OK;
112}
113
Phil Burk11e8d332017-05-24 09:59:02 -0700114aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700115 aaudio_result_t result = AAUDIO_OK;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700116 if (++mRunningStreams == 1) {
Phil Burk940083c2017-07-17 17:00:02 -0700117 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
118 std::lock_guard<std::mutex> lock(mLockStreams);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119 result = getStreamInternal()->requestStart();
Phil Burk87c9f642017-05-17 07:22:39 -0700120 startSharingThread_l();
Phil Burkc0c70e32017-02-09 13:18:38 -0800121 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700122 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800123}
124
Phil Burk11e8d332017-05-24 09:59:02 -0700125aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
Phil Burk940083c2017-07-17 17:00:02 -0700126 // Don't lock here because the disconnectRegisteredStreams also uses the lock.
127 if (--mRunningStreams == 0) { // atomic
Phil Burk87c9f642017-05-17 07:22:39 -0700128 stopSharingThread();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700129 getStreamInternal()->requestStop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800130 }
131 return AAUDIO_OK;
132}
133
Phil Burk87c9f642017-05-17 07:22:39 -0700134static void *aaudio_endpoint_thread_proc(void *context) {
135 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
136 if (endpoint != NULL) {
137 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 } else {
139 return NULL;
140 }
141}
142
Phil Burk87c9f642017-05-17 07:22:39 -0700143aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700145 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 * AAUDIO_NANOS_PER_SECOND
147 / getSampleRate();
148 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700149 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800150}
151
Phil Burk87c9f642017-05-17 07:22:39 -0700152aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700154 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
Phil Burk87c9f642017-05-17 07:22:39 -0700155 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800156}
157
158void AAudioServiceEndpoint::disconnectRegisteredStreams() {
159 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk11e8d332017-05-24 09:59:02 -0700160 for(auto sharedStream : mRegisteredStreams) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700161 sharedStream->stop();
Phil Burk5ef003b2017-06-30 11:43:37 -0700162 sharedStream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800163 }
164 mRegisteredStreams.clear();
165}
Eric Laurenta17ae742017-06-29 15:43:55 -0700166
167bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
168 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
169 configuration.getDeviceId() != mStreamInternal->getDeviceId()) {
170 return false;
171 }
172 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
173 configuration.getSampleRate() != mStreamInternal->getSampleRate()) {
174 return false;
175 }
176 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
177 configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) {
178 return false;
179 }
180
181 return true;
182}
Eric Laurentcb4dae22017-07-01 19:39:32 -0700183