blob: e7d9e0d322a941826b777baaa8ad64cb57bd1af7 [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) {
49 mStreamInternal = getStreamInternal();
50
Phil Burkc0c70e32017-02-09 13:18:38 -080051 AudioStreamBuilder builder;
52 builder.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burk71f35bb2017-04-13 16:05:07 -070053 // Don't fall back to SHARED because that would cause recursion.
54 builder.setSharingModeMatchRequired(true);
Phil Burkc0c70e32017-02-09 13:18:38 -080055 builder.setDeviceId(deviceId);
Phil Burk87c9f642017-05-17 07:22:39 -070056 builder.setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -070057 builder.setBufferCapacity(DEFAULT_BUFFER_CAPACITY);
58
Phil Burk87c9f642017-05-17 07:22:39 -070059 return getStreamInternal()->open(builder);
Phil Burkc0c70e32017-02-09 13:18:38 -080060}
61
62aaudio_result_t AAudioServiceEndpoint::close() {
Phil Burk87c9f642017-05-17 07:22:39 -070063 return getStreamInternal()->close();
Phil Burkc0c70e32017-02-09 13:18:38 -080064}
65
66// TODO, maybe use an interface to reduce exposure
67aaudio_result_t AAudioServiceEndpoint::registerStream(AAudioServiceStreamShared *sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -080068 std::lock_guard<std::mutex> lock(mLockStreams);
69 mRegisteredStreams.push_back(sharedStream);
70 return AAUDIO_OK;
71}
72
73aaudio_result_t AAudioServiceEndpoint::unregisterStream(AAudioServiceStreamShared *sharedStream) {
Phil Burkc0c70e32017-02-09 13:18:38 -080074 std::lock_guard<std::mutex> lock(mLockStreams);
75 mRegisteredStreams.erase(std::remove(mRegisteredStreams.begin(), mRegisteredStreams.end(), sharedStream),
76 mRegisteredStreams.end());
77 return AAUDIO_OK;
78}
79
80aaudio_result_t AAudioServiceEndpoint::startStream(AAudioServiceStreamShared *sharedStream) {
81 // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
Phil Burkc0c70e32017-02-09 13:18:38 -080082 std::lock_guard<std::mutex> lock(mLockStreams);
83 mRunningStreams.push_back(sharedStream);
84 if (mRunningStreams.size() == 1) {
Phil Burk87c9f642017-05-17 07:22:39 -070085 startSharingThread_l();
Phil Burkc0c70e32017-02-09 13:18:38 -080086 }
87 return AAUDIO_OK;
88}
89
90aaudio_result_t AAudioServiceEndpoint::stopStream(AAudioServiceStreamShared *sharedStream) {
Phil Burk87c9f642017-05-17 07:22:39 -070091 int numRunningStreams = 0;
92 {
93 std::lock_guard<std::mutex> lock(mLockStreams);
94 mRunningStreams.erase(
95 std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream),
96 mRunningStreams.end());
97 numRunningStreams = mRunningStreams.size();
98 }
99 if (numRunningStreams == 0) {
100 // Don't call this under a lock because the callbackLoop also uses the lock.
101 stopSharingThread();
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 }
103 return AAUDIO_OK;
104}
105
Phil Burk87c9f642017-05-17 07:22:39 -0700106static void *aaudio_endpoint_thread_proc(void *context) {
107 AAudioServiceEndpoint *endpoint = (AAudioServiceEndpoint *) context;
108 if (endpoint != NULL) {
109 return endpoint->callbackLoop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 } else {
111 return NULL;
112 }
113}
114
Phil Burk87c9f642017-05-17 07:22:39 -0700115aaudio_result_t AAudioServiceEndpoint::startSharingThread_l() {
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 // Launch the callback loop thread.
Phil Burk87c9f642017-05-17 07:22:39 -0700117 int64_t periodNanos = getStreamInternal()->getFramesPerBurst()
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 * AAUDIO_NANOS_PER_SECOND
119 / getSampleRate();
120 mCallbackEnabled.store(true);
Phil Burk87c9f642017-05-17 07:22:39 -0700121 return getStreamInternal()->createThread(periodNanos, aaudio_endpoint_thread_proc, this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800122}
123
Phil Burk87c9f642017-05-17 07:22:39 -0700124aaudio_result_t AAudioServiceEndpoint::stopSharingThread() {
125 ALOGD("AAudioServiceEndpoint(): call joinThread()");
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);
128 ALOGD("AAudioServiceEndpoint(): joinThread() returned %d", result);
129 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800130}
131
132void AAudioServiceEndpoint::disconnectRegisteredStreams() {
133 std::lock_guard<std::mutex> lock(mLockStreams);
134 for(AAudioServiceStreamShared *sharedStream : mRunningStreams) {
135 sharedStream->onStop();
136 }
137 mRunningStreams.clear();
138 for(AAudioServiceStreamShared *sharedStream : mRegisteredStreams) {
139 sharedStream->onDisconnect();
140 }
141 mRegisteredStreams.clear();
142}