blob: b5198298f420afc54d70ecf42e455641f93b768f [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
Phil Burk71f35bb2017-04-13 16:05:07 -070017#define LOG_TAG "AAudioService"
18//#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);
87 builder.setFormat(configuration.getAudioFormat());
88 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() {
Phil Burk87c9f642017-05-17 07:22:39 -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) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 std::lock_guard<std::mutex> lock(mLockStreams);
117 mRunningStreams.push_back(sharedStream);
118 if (mRunningStreams.size() == 1) {
Phil Burk87c9f642017-05-17 07:22:39 -0700119 startSharingThread_l();
Phil Burkc0c70e32017-02-09 13:18:38 -0800120 }
121 return AAUDIO_OK;
122}
123
Phil Burk11e8d332017-05-24 09:59:02 -0700124aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
Phil Burk87c9f642017-05-17 07:22:39 -0700125 int numRunningStreams = 0;
126 {
127 std::lock_guard<std::mutex> lock(mLockStreams);
128 mRunningStreams.erase(
129 std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream),
130 mRunningStreams.end());
131 numRunningStreams = mRunningStreams.size();
132 }
133 if (numRunningStreams == 0) {
134 // Don't call this under a lock because the callbackLoop also uses the lock.
135 stopSharingThread();
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 }
137 return AAUDIO_OK;
138}
139
Phil Burk87c9f642017-05-17 07:22:39 -0700140static void *aaudio_endpoint_thread_proc(void *context) {
141 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
142 if (endpoint != NULL) {
143 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 } else {
145 return NULL;
146 }
147}
148
Phil Burk87c9f642017-05-17 07:22:39 -0700149aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700151 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 * AAUDIO_NANOS_PER_SECOND
153 / getSampleRate();
154 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700155 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800156}
157
Phil Burk87c9f642017-05-17 07:22:39 -0700158aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700160 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
Phil Burk87c9f642017-05-17 07:22:39 -0700161 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800162}
163
164void AAudioServiceEndpoint::disconnectRegisteredStreams() {
165 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk11e8d332017-05-24 09:59:02 -0700166 for(auto baseStream : mRunningStreams) {
167 baseStream->onStop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 }
169 mRunningStreams.clear();
Phil Burk11e8d332017-05-24 09:59:02 -0700170 for(auto sharedStream : mRegisteredStreams) {
Phil Burk5ef003b2017-06-30 11:43:37 -0700171 sharedStream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800172 }
173 mRegisteredStreams.clear();
174}
Eric Laurenta17ae742017-06-29 15:43:55 -0700175
176bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
177 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
178 configuration.getDeviceId() != mStreamInternal->getDeviceId()) {
179 return false;
180 }
181 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
182 configuration.getSampleRate() != mStreamInternal->getSampleRate()) {
183 return false;
184 }
185 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
186 configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) {
187 return false;
188 }
189
190 return true;
191}