Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "AAudioStreamTracker" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <iomanip> |
| 22 | #include <iostream> |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include <aaudio/AAudio.h> |
| 26 | #include <utils/String16.h> |
| 27 | |
| 28 | #include "AAudioStreamTracker.h" |
| 29 | |
| 30 | using namespace android; |
| 31 | using namespace aaudio; |
| 32 | |
| 33 | sp<AAudioServiceStreamBase> AAudioStreamTracker::removeStreamByHandle( |
| 34 | aaudio_handle_t streamHandle) { |
| 35 | std::lock_guard<std::mutex> lock(mHandleLock); |
| 36 | sp<AAudioServiceStreamBase> serviceStream; |
| 37 | auto it = mStreamsByHandle.find(streamHandle); |
| 38 | if (it != mStreamsByHandle.end()) { |
| 39 | serviceStream = it->second; |
| 40 | mStreamsByHandle.erase(it); |
| 41 | } |
| 42 | return serviceStream; |
| 43 | } |
| 44 | |
| 45 | sp<AAudioServiceStreamBase> AAudioStreamTracker::getStreamByHandle( |
| 46 | aaudio_handle_t streamHandle) { |
| 47 | std::lock_guard<std::mutex> lock(mHandleLock); |
| 48 | sp<AAudioServiceStreamBase> serviceStream; |
| 49 | auto it = mStreamsByHandle.find(streamHandle); |
| 50 | if (it != mStreamsByHandle.end()) { |
| 51 | serviceStream = it->second; |
| 52 | } |
| 53 | return serviceStream; |
| 54 | } |
| 55 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame^] | 56 | |
| 57 | // The port handle is only available when the stream is started. |
| 58 | // So we have to iterate over all the streams. |
| 59 | // Luckily this rarely happens. |
| 60 | sp<AAudioServiceStreamBase> AAudioStreamTracker::findStreamByPortHandle( |
| 61 | audio_port_handle_t portHandle) { |
| 62 | std::lock_guard<std::mutex> lock(mHandleLock); |
| 63 | sp<AAudioServiceStreamBase> serviceStream; |
| 64 | auto it = mStreamsByHandle.begin(); |
| 65 | while (it != mStreamsByHandle.end()) { |
| 66 | auto candidate = it->second; |
| 67 | if (candidate->getPortHandle() == portHandle) { |
| 68 | serviceStream = candidate; |
| 69 | break; |
| 70 | } |
| 71 | it++; |
| 72 | } |
| 73 | return serviceStream; |
| 74 | } |
| 75 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 76 | // advance to next legal handle value |
| 77 | __attribute__((no_sanitize("integer"))) |
| 78 | aaudio_handle_t AAudioStreamTracker::bumpHandle(aaudio_handle_t handle) { |
| 79 | handle++; |
| 80 | // Only use positive integers. |
| 81 | if (handle <= 0) { |
| 82 | handle = 1; |
| 83 | } |
| 84 | return handle; |
| 85 | } |
| 86 | |
| 87 | aaudio_handle_t AAudioStreamTracker::addStreamForHandle(sp<AAudioServiceStreamBase> serviceStream) { |
| 88 | std::lock_guard<std::mutex> lock(mHandleLock); |
| 89 | aaudio_handle_t handle = mPreviousHandle.load(); |
| 90 | // Assign a unique handle. |
| 91 | while (true) { |
| 92 | handle = bumpHandle(handle); |
| 93 | sp<AAudioServiceStreamBase> oldServiceStream = mStreamsByHandle[handle]; |
| 94 | // Is this an unused handle? It would be extremely unlikely to wrap |
| 95 | // around and collide with a very old handle. But just in case. |
| 96 | if (oldServiceStream.get() == nullptr) { |
| 97 | mStreamsByHandle[handle] = serviceStream; |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | mPreviousHandle.store(handle); |
| 102 | return handle; |
| 103 | } |
| 104 | |
| 105 | std::string AAudioStreamTracker::dump() const { |
| 106 | std::stringstream result; |
| 107 | const bool isLocked = AAudio_tryUntilTrue( |
| 108 | [this]()->bool { return mHandleLock.try_lock(); } /* f */, |
| 109 | 50 /* times */, |
| 110 | 20 /* sleepMs */); |
| 111 | if (!isLocked) { |
| 112 | result << "AAudioStreamTracker may be deadlocked\n"; |
| 113 | } else { |
| 114 | result << "Stream Handles:\n"; |
| 115 | for (const auto& it : mStreamsByHandle) { |
| 116 | aaudio_handle_t handle = it.second->getHandle(); |
| 117 | result << " 0x" << std::setfill('0') << std::setw(8) << std::hex << handle |
| 118 | << std::dec << std::setfill(' ') << "\n"; |
| 119 | } |
| 120 | |
| 121 | mHandleLock.unlock(); |
| 122 | } |
| 123 | return result.str(); |
| 124 | } |