blob: 4dfb62a3454e456a4147c0883946a50139f775f5 [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"
Phil Burk39f02dd2017-08-04 09:13:31 -070036#include "AAudioServiceEndpointShared.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037
38using namespace android; // TODO just import names needed
39using namespace aaudio; // TODO just import names needed
40
Phil Burk4501b352017-06-29 18:12:36 -070041std::string AAudioServiceEndpoint::dump() const {
42 std::stringstream result;
43
44 const bool isLocked = AAudio_tryUntilTrue(
45 [this]()->bool { return mLockStreams.try_lock(); } /* f */,
46 50 /* times */,
47 20 /* sleepMs */);
48 if (!isLocked) {
Phil Burk39f02dd2017-08-04 09:13:31 -070049 result << "AAudioServiceEndpoint may be deadlocked\n";
Phil Burk4501b352017-06-29 18:12:36 -070050 }
51
Phil Burk39f02dd2017-08-04 09:13:31 -070052 result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT)
53 ? "OUTPUT" : "INPUT") << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070054 result << " Requested Device Id: " << mRequestedDeviceId << "\n";
55 result << " Device Id: " << getDeviceId() << "\n";
Phil Burka62fb952018-01-16 12:44:06 -080056 result << " Sample Rate: " << getSampleRate() << "\n";
57 result << " Channel Count: " << getSamplesPerFrame() << "\n";
Phil Burk0127c1b2018-03-29 13:48:06 -070058 result << " Format: " << getFormat() << "\n";
Phil Burka62fb952018-01-16 12:44:06 -080059 result << " Frames Per Burst: " << mFramesPerBurst << "\n";
60 result << " Usage: " << getUsage() << "\n";
61 result << " ContentType: " << getContentType() << "\n";
62 result << " InputPreset: " << getInputPreset() << "\n";
63 result << " Reference Count: " << mOpenCount << "\n";
Phil Burk4e1af9f2018-01-03 15:54:35 -080064 result << " Session Id: " << getSessionId() << "\n";
Phil Burkbe0a5b62017-10-12 15:56:00 -070065 result << " Connected: " << mConnected.load() << "\n";
Phil Burk523b3042017-09-13 13:03:08 -070066 result << " Registered Streams:" << "\n";
Phil Burka5222e22017-07-28 13:31:14 -070067 result << AAudioServiceStreamShared::dumpHeader() << "\n";
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080068 for (const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070069 result << stream->dump() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070070 }
71
72 if (isLocked) {
73 mLockStreams.unlock();
74 }
75 return result.str();
76}
77
Phil Burkbbd52862018-04-13 11:37:42 -070078// @return true if stream found
79bool AAudioServiceEndpoint::isStreamRegistered(audio_port_handle_t portHandle) {
80 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080081 for (const auto& stream : mRegisteredStreams) {
Phil Burkbbd52862018-04-13 11:37:42 -070082 if (stream->getPortHandle() == portHandle) {
83 return true;
84 }
85 }
86 return false;
87}
88
Phil Burkc0c70e32017-02-09 13:18:38 -080089void AAudioServiceEndpoint::disconnectRegisteredStreams() {
90 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burkbe0a5b62017-10-12 15:56:00 -070091 mConnected.store(false);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080092 for (const auto& stream : mRegisteredStreams) {
Phil Burkbe0a5b62017-10-12 15:56:00 -070093 ALOGD("disconnectRegisteredStreams() stop and disconnect %p", stream.get());
Phil Burk39f02dd2017-08-04 09:13:31 -070094 stream->stop();
95 stream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -080096 }
97 mRegisteredStreams.clear();
98}
Eric Laurenta17ae742017-06-29 15:43:55 -070099
Phil Burk39f02dd2017-08-04 09:13:31 -0700100aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) {
101 std::lock_guard<std::mutex> lock(mLockStreams);
102 mRegisteredStreams.push_back(stream);
103 return AAUDIO_OK;
104}
105
106aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) {
107 std::lock_guard<std::mutex> lock(mLockStreams);
108 mRegisteredStreams.erase(std::remove(
109 mRegisteredStreams.begin(), mRegisteredStreams.end(), stream),
110 mRegisteredStreams.end());
111 return AAUDIO_OK;
112}
113
Eric Laurenta17ae742017-06-29 15:43:55 -0700114bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
Phil Burkbe0a5b62017-10-12 15:56:00 -0700115 if (!mConnected.load()) {
116 return false; // Only use an endpoint if it is connected to a device.
117 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 if (configuration.getDirection() != getDirection()) {
119 return false;
120 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700121 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 configuration.getDeviceId() != getDeviceId()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700123 return false;
124 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800125 if (configuration.getSessionId() != AAUDIO_SESSION_ID_ALLOCATE &&
126 configuration.getSessionId() != getSessionId()) {
127 return false;
128 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700129 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 configuration.getSampleRate() != getSampleRate()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700131 return false;
132 }
133 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700134 configuration.getSamplesPerFrame() != getSamplesPerFrame()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700135 return false;
136 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700137 return true;
138}