blob: 3815711ed282d5800d07a9e55c9342681944ad0c [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
21#include <assert.h>
22#include <map>
23#include <mutex>
24#include <utils/Singleton.h>
25
26#include "AAudioEndpointManager.h"
27#include "AAudioServiceEndpoint.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include <algorithm>
29#include <mutex>
30#include <vector>
31
32#include "core/AudioStreamBuilder.h"
33#include "AAudioServiceEndpoint.h"
34#include "AAudioServiceStreamShared.h"
35
36using namespace android; // TODO just import names needed
37using namespace aaudio; // TODO just import names needed
38
39#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND)
40
41// Wait at least this many times longer than the operation should take.
42#define MIN_TIMEOUT_OPERATIONS 4
43
Phil Burk71f35bb2017-04-13 16:05:07 -070044// This is the maximum size in frames. The effective size can be tuned smaller at runtime.
45#define DEFAULT_BUFFER_CAPACITY (48 * 8)
46
Phil Burkc0c70e32017-02-09 13:18:38 -080047// Set up an EXCLUSIVE MMAP stream that will be shared.
Phil Burk87c9f642017-05-17 07:22:39 -070048aaudio_result_t AAudioServiceEndpoint::open(int32_t deviceId) {
Phil Burkec89b2e2017-06-20 15:05:06 -070049 mRequestedDeviceId = deviceId;
Phil Burk87c9f642017-05-17 07:22:39 -070050 mStreamInternal = getStreamInternal();
51
Phil Burkc0c70e32017-02-09 13:18:38 -080052 AudioStreamBuilder builder;
53 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burk71f35bb2017-04-13 16:05:07 -070054 // Don't fall back to SHARED because that would cause recursion.
55 builder.setSharingModeMatchRequired(true);
Phil Burkc0c70e32017-02-09 13:18:38 -080056 builder.setDeviceId(deviceId);
Phil Burk87c9f642017-05-17 07:22:39 -070057 builder.setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -070058 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
59
Phil Burk87c9f642017-05-17 07:22:39 -070060 return getStreamInternal()->open(builder);
Phil Burkc0c70e32017-02-09 13:18:38 -080061}
62
63aaudio_result_t AAudioServiceEndpoint::close() {
Phil Burk87c9f642017-05-17 07:22:39 -070064 return getStreamInternal()->close();
Phil Burkc0c70e32017-02-09 13:18:38 -080065}
66
67// TODO, maybe use an interface to reduce exposure
Phil Burk11e8d332017-05-24 09:59:02 -070068aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -080069 std::lock_guard<std::mutex> lock(mLockStreams);
70 mRegisteredStreams.push_back(sharedStream);
71 return AAUDIO_OK;
72}
73
Phil Burk11e8d332017-05-24 09:59:02 -070074aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamShared>sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -080075 std::lock_guard<std::mutex> lock(mLockStreams);
76 mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream),
77 mRegisteredStreams.end());
78 return AAUDIO_OK;
79}
80
Phil Burk11e8d332017-05-24 09:59:02 -070081aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -080082 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
Phil Burkc0c70e32017-02-09 13:18:38 -080083 std::lock_guard<std::mutex> lock(mLockStreams);
84 mRunningStreams.push_back(sharedStream);
85 if (mRunningStreams.size() == 1) {
Phil Burk87c9f642017-05-17 07:22:39 -070086 startSharingThread_l();
Phil Burkc0c70e32017-02-09 13:18:38 -080087 }
88 return AAUDIO_OK;
89}
90
Phil Burk11e8d332017-05-24 09:59:02 -070091aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
Phil Burk87c9f642017-05-17 07:22:39 -070092 int numRunningStreams = 0;
93 {
94 std::lock_guard<std::mutex> lock(mLockStreams);
95 mRunningStreams.erase(
96 std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream),
97 mRunningStreams.end());
98 numRunningStreams = mRunningStreams.size();
99 }
100 if (numRunningStreams == 0) {
101 // Don't call this under a lock because the callbackLoop also uses the lock.
102 stopSharingThread();
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 }
104 return AAUDIO_OK;
105}
106
Phil Burk87c9f642017-05-17 07:22:39 -0700107static void *aaudio_endpoint_thread_proc(void *context) {
108 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
109 if (endpoint != NULL) {
110 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 } else {
112 return NULL;
113 }
114}
115
Phil Burk87c9f642017-05-17 07:22:39 -0700116aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700118 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800119 * AAUDIO_NANOS_PER_SECOND
120 / getSampleRate();
121 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700122 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800123}
124
Phil Burk87c9f642017-05-17 07:22:39 -0700125aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700127 aaudio_result_t result = getStreamInternal()->joinThread(NULL);
Phil Burk87c9f642017-05-17 07:22:39 -0700128 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800129}
130
131void AAudioServiceEndpoint::disconnectRegisteredStreams() {
132 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk11e8d332017-05-24 09:59:02 -0700133 for(auto baseStream : mRunningStreams) {
134 baseStream->onStop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 }
136 mRunningStreams.clear();
Phil Burk11e8d332017-05-24 09:59:02 -0700137 for(auto sharedStream : mRegisteredStreams) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 sharedStream->onDisconnect();
139 }
140 mRegisteredStreams.clear();
141}