blob: 3095bc9e6191c482b27c4e38ac846c0ed7851367 [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 Burk39f02dd2017-08-04 09:13:31 -070041AAudioServiceEndpoint::~AAudioServiceEndpoint() {
42 ALOGD("AAudioServiceEndpoint::~AAudioServiceEndpoint() destroying endpoint %p", this);
43}
Phil Burk71f35bb2017-04-13 16:05:07 -070044
Phil Burk4501b352017-06-29 18:12:36 -070045std::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) {
Phil Burk39f02dd2017-08-04 09:13:31 -070053 result << "AAudioServiceEndpoint may be deadlocked\n";
Phil Burk4501b352017-06-29 18:12:36 -070054 }
55
Phil Burk39f02dd2017-08-04 09:13:31 -070056 result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT)
57 ? "OUTPUT" : "INPUT") << "\n";
58 result << " Sample Rate: " << getSampleRate() << "\n";
59 result << " Frames Per Burst: " << mFramesPerBurst << "\n";
60 result << " Reference Count: " << mOpenCount << "\n";
61 result << " Requested Device Id: " << mRequestedDeviceId << "\n";
62 result << " Device Id: " << getDeviceId() << "\n";
Phil Burk523b3042017-09-13 13:03:08 -070063 result << " Registered Streams:" << "\n";
Phil Burka5222e22017-07-28 13:31:14 -070064 result << AAudioServiceStreamShared::dumpHeader() << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070065 for (const auto stream : mRegisteredStreams) {
66 result << stream->dump() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070067 }
68
69 if (isLocked) {
70 mLockStreams.unlock();
71 }
72 return result.str();
73}
74
Phil Burkc0c70e32017-02-09 13:18:38 -080075void AAudioServiceEndpoint::disconnectRegisteredStreams() {
76 std::lock_guard<std::mutex> lock(mLockStreams);
Phil Burk39f02dd2017-08-04 09:13:31 -070077 for (const auto stream : mRegisteredStreams) {
78 stream->stop();
79 stream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -080080 }
81 mRegisteredStreams.clear();
82}
Eric Laurenta17ae742017-06-29 15:43:55 -070083
Phil Burk39f02dd2017-08-04 09:13:31 -070084aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) {
85 std::lock_guard<std::mutex> lock(mLockStreams);
86 mRegisteredStreams.push_back(stream);
87 return AAUDIO_OK;
88}
89
90aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) {
91 std::lock_guard<std::mutex> lock(mLockStreams);
92 mRegisteredStreams.erase(std::remove(
93 mRegisteredStreams.begin(), mRegisteredStreams.end(), stream),
94 mRegisteredStreams.end());
95 return AAUDIO_OK;
96}
97
Eric Laurenta17ae742017-06-29 15:43:55 -070098bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
Phil Burk39f02dd2017-08-04 09:13:31 -070099 if (configuration.getDirection() != getDirection()) {
100 return false;
101 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700102 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700103 configuration.getDeviceId() != getDeviceId()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700104 return false;
105 }
106 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700107 configuration.getSampleRate() != getSampleRate()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700108 return false;
109 }
110 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 configuration.getSamplesPerFrame() != getSamplesPerFrame()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700112 return false;
113 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700114 return true;
115}