blob: d726d46f0553303161a3cc66f7e502e0e0a3994b [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) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
Eric Laurentcb4dae22017-07-01 19:39:32 -0700116 aaudio_result_t result = AAUDIO_OK;
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 std::lock_guard<std::mutex> lock(mLockStreams);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700118 if (++mRunningStreams == 1) {
119 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 Burk87c9f642017-05-17 07:22:39 -0700126 int numRunningStreams = 0;
127 {
128 std::lock_guard<std::mutex> lock(mLockStreams);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700129 numRunningStreams = --mRunningStreams;
Phil Burk87c9f642017-05-17 07:22:39 -0700130 }
131 if (numRunningStreams == 0) {
132 // Don't call this under a lock because the callbackLoop also uses the lock.
133 stopSharingThread();
Eric Laurentcb4dae22017-07-01 19:39:32 -0700134 getStreamInternal()->requestStop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 }
136 return AAUDIO_OK;
137}
138
Phil Burk87c9f642017-05-17 07:22:39 -0700139static void *aaudio_endpoint_thread_proc(void *context) {
140 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
141 if (endpoint != NULL) {
142 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 } else {
144 return NULL;
145 }
146}
147
Phil Burk87c9f642017-05-17 07:22:39 -0700148aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700150 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800151 * AAUDIO_NANOS_PER_SECOND
152 / getSampleRate();
153 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700154 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800155}
156
Phil Burk87c9f642017-05-17 07:22:39 -0700157aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800158 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700159 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
Phil Burk87c9f642017-05-17 07:22:39 -0700160 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800161}
162
163void AAudioServiceEndpoint::disconnectRegisteredStreams() {
164 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk11e8d332017-05-24 09:59:02 -0700165 for(auto sharedStream : mRegisteredStreams) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700166 sharedStream->stop();
Phil Burk5ef003b2017-06-30 11:43:37 -0700167 sharedStream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 }
169 mRegisteredStreams.clear();
170}
Eric Laurenta17ae742017-06-29 15:43:55 -0700171
172bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
173 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
174 configuration.getDeviceId() != mStreamInternal->getDeviceId()) {
175 return false;
176 }
177 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
178 configuration.getSampleRate() != mStreamInternal->getSampleRate()) {
179 return false;
180 }
181 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
182 configuration.getSamplesPerFrame() != mStreamInternal->getSamplesPerFrame()) {
183 return false;
184 }
185
186 return true;
187}
Eric Laurentcb4dae22017-07-01 19:39:32 -0700188