| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 |  | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceEndpoint" | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 | #include <utils/Log.h> | 
|  | 20 |  | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 21 | #include <algorithm> | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 22 | #include <assert.h> | 
|  | 23 | #include <map> | 
|  | 24 | #include <mutex> | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 25 | #include <sstream> | 
|  | 26 | #include <vector> | 
|  | 27 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 28 | #include <utils/Singleton.h> | 
|  | 29 |  | 
|  | 30 | #include "AAudioEndpointManager.h" | 
|  | 31 | #include "AAudioServiceEndpoint.h" | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | #include "core/AudioStreamBuilder.h" | 
|  | 34 | #include "AAudioServiceEndpoint.h" | 
|  | 35 | #include "AAudioServiceStreamShared.h" | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 36 | #include "AAudioServiceEndpointShared.h" | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | using namespace android;  // TODO just import names needed | 
|  | 39 | using namespace aaudio;   // TODO just import names needed | 
|  | 40 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 41 | AAudioServiceEndpoint::~AAudioServiceEndpoint() { | 
|  | 42 | ALOGD("AAudioServiceEndpoint::~AAudioServiceEndpoint() destroying endpoint %p", this); | 
|  | 43 | } | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 44 |  | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 45 | 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) { | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 53 | result << "AAudioServiceEndpoint may be deadlocked\n"; | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 56 | 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 Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 63 | result << "    Connected:            " << mConnected.load() << "\n"; | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 64 | result << "    Registered Streams:" << "\n"; | 
| Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 65 | result << AAudioServiceStreamShared::dumpHeader() << "\n"; | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 66 | for (const auto stream : mRegisteredStreams) { | 
|  | 67 | result << stream->dump() << "\n"; | 
| Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 70 | if (isLocked) { | 
|  | 71 | mLockStreams.unlock(); | 
|  | 72 | } | 
|  | 73 | return result.str(); | 
|  | 74 | } | 
|  | 75 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 76 | void AAudioServiceEndpoint::disconnectRegisteredStreams() { | 
|  | 77 | std::lock_guard<std::mutex> lock(mLockStreams); | 
| Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 78 | mConnected.store(false); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 79 | for (const auto stream : mRegisteredStreams) { | 
| Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 80 | ALOGD("disconnectRegisteredStreams() stop and disconnect %p", stream.get()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 81 | stream->stop(); | 
|  | 82 | stream->disconnect(); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | } | 
|  | 84 | mRegisteredStreams.clear(); | 
|  | 85 | } | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 86 |  | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 87 | aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) { | 
|  | 88 | std::lock_guard<std::mutex> lock(mLockStreams); | 
|  | 89 | mRegisteredStreams.push_back(stream); | 
|  | 90 | return AAUDIO_OK; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) { | 
|  | 94 | std::lock_guard<std::mutex> lock(mLockStreams); | 
|  | 95 | mRegisteredStreams.erase(std::remove( | 
|  | 96 | mRegisteredStreams.begin(), mRegisteredStreams.end(), stream), | 
|  | 97 | mRegisteredStreams.end()); | 
|  | 98 | return AAUDIO_OK; | 
|  | 99 | } | 
|  | 100 |  | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 101 | bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) { | 
| Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 102 | if (!mConnected.load()) { | 
|  | 103 | return false; // Only use an endpoint if it is connected to a device. | 
|  | 104 | } | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 105 | if (configuration.getDirection() != getDirection()) { | 
|  | 106 | return false; | 
|  | 107 | } | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 108 | if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED && | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 109 | configuration.getDeviceId() != getDeviceId()) { | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 110 | return false; | 
|  | 111 | } | 
|  | 112 | if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED && | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 113 | configuration.getSampleRate() != getSampleRate()) { | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 114 | return false; | 
|  | 115 | } | 
|  | 116 | if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED && | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 117 | configuration.getSamplesPerFrame() != getSamplesPerFrame()) { | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 118 | return false; | 
|  | 119 | } | 
| Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 120 | return true; | 
|  | 121 | } |