1 /*
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
17 #define LOG_TAG "AAudioServiceEndpoint"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <algorithm>
22 #include <assert.h>
23 #include <map>
24 #include <mutex>
25 #include <sstream>
26 #include <vector>
27
28 #include <utils/Singleton.h>
29
30 #include "AAudioEndpointManager.h"
31 #include "AAudioServiceEndpoint.h"
32
33 #include "core/AudioStreamBuilder.h"
34 #include "AAudioServiceEndpoint.h"
35 #include "AAudioServiceStreamShared.h"
36 #include "AAudioServiceEndpointShared.h"
37
38 using namespace android; // TODO just import names needed
39 using namespace aaudio; // TODO just import names needed
40
~AAudioServiceEndpoint()41 AAudioServiceEndpoint::~AAudioServiceEndpoint() {
42 ALOGD("%s(%p) destroyed", __func__, this);
43 }
44
dump() const45 std::string AAudioServiceEndpoint::dump() const {
46 std::stringstream result;
47
48 const bool isLocked = AAudio_tryUntilTrue(
49 [this]()->bool { return mLockStreams.try_lock(); } /* f */,
50 50 /* times */,
51 20 /* sleepMs */);
52 if (!isLocked) {
53 result << "AAudioServiceEndpoint may be deadlocked\n";
54 }
55
56 result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT)
57 ? "OUTPUT" : "INPUT") << "\n";
58 result << " Requested Device Id: " << mRequestedDeviceId << "\n";
59 result << " Device Id: " << getDeviceId() << "\n";
60 result << " Sample Rate: " << getSampleRate() << "\n";
61 result << " Channel Count: " << getSamplesPerFrame() << "\n";
62 result << " Frames Per Burst: " << mFramesPerBurst << "\n";
63 result << " Usage: " << getUsage() << "\n";
64 result << " ContentType: " << getContentType() << "\n";
65 result << " InputPreset: " << getInputPreset() << "\n";
66 result << " Reference Count: " << mOpenCount << "\n";
67 result << " Session Id: " << getSessionId() << "\n";
68 result << " Connected: " << mConnected.load() << "\n";
69 result << " Registered Streams:" << "\n";
70 result << AAudioServiceStreamShared::dumpHeader() << "\n";
71 for (const auto stream : mRegisteredStreams) {
72 result << stream->dump() << "\n";
73 }
74
75 if (isLocked) {
76 mLockStreams.unlock();
77 }
78 return result.str();
79 }
80
81 // @return true if stream found
isStreamRegistered(audio_port_handle_t portHandle)82 bool AAudioServiceEndpoint::isStreamRegistered(audio_port_handle_t portHandle) {
83 std::lock_guard<std::mutex> lock(mLockStreams);
84 for (const auto stream : mRegisteredStreams) {
85 if (stream->getPortHandle() == portHandle) {
86 return true;
87 }
88 }
89 return false;
90 }
91
disconnectRegisteredStreams()92 void AAudioServiceEndpoint::disconnectRegisteredStreams() {
93 std::lock_guard<std::mutex> lock(mLockStreams);
94 mConnected.store(false);
95 for (const auto stream : mRegisteredStreams) {
96 ALOGD("disconnectRegisteredStreams() stop and disconnect %p", stream.get());
97 stream->stop();
98 stream->disconnect();
99 }
100 mRegisteredStreams.clear();
101 }
102
registerStream(sp<AAudioServiceStreamBase> stream)103 aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) {
104 std::lock_guard<std::mutex> lock(mLockStreams);
105 mRegisteredStreams.push_back(stream);
106 return AAUDIO_OK;
107 }
108
unregisterStream(sp<AAudioServiceStreamBase> stream)109 aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) {
110 std::lock_guard<std::mutex> lock(mLockStreams);
111 mRegisteredStreams.erase(std::remove(
112 mRegisteredStreams.begin(), mRegisteredStreams.end(), stream),
113 mRegisteredStreams.end());
114 return AAUDIO_OK;
115 }
116
matches(const AAudioStreamConfiguration & configuration)117 bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
118 if (!mConnected.load()) {
119 return false; // Only use an endpoint if it is connected to a device.
120 }
121 if (configuration.getDirection() != getDirection()) {
122 return false;
123 }
124 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
125 configuration.getDeviceId() != getDeviceId()) {
126 return false;
127 }
128 if (configuration.getSessionId() != AAUDIO_SESSION_ID_ALLOCATE &&
129 configuration.getSessionId() != getSessionId()) {
130 return false;
131 }
132 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
133 configuration.getSampleRate() != getSampleRate()) {
134 return false;
135 }
136 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
137 configuration.getSamplesPerFrame() != getSamplesPerFrame()) {
138 return false;
139 }
140 return true;
141 }
142