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 | #ifndef AAUDIO_AAUDIO_STREAM_TRACKER_H |
| 18 | #define AAUDIO_AAUDIO_STREAM_TRACKER_H |
| 19 | |
| 20 | #include <time.h> |
| 21 | #include <pthread.h> |
| 22 | |
| 23 | #include <aaudio/AAudio.h> |
| 24 | |
| 25 | #include "binding/AAudioCommon.h" |
| 26 | |
| 27 | #include "AAudioServiceStreamBase.h" |
| 28 | |
| 29 | namespace aaudio { |
| 30 | |
| 31 | class AAudioStreamTracker { |
| 32 | |
| 33 | public: |
| 34 | /** |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 35 | * Remove any streams with the matching handle. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 36 | * |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 37 | * @param streamHandle |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 38 | * @return number of streams removed |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 39 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 40 | int32_t removeStreamByHandle(aaudio_handle_t streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Look up a stream based on the handle. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 44 | * |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 45 | * @param streamHandle |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 46 | * @return strong pointer to the stream if found, or nullptr |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 47 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 48 | android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandle( |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 49 | aaudio_handle_t streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 52 | * Look up a stream based on the AudioPolicy portHandle. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 53 | * Increment its service reference count if found. |
| 54 | * |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 55 | * @param portHandle |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 56 | * @return strong pointer to the stream if found, or nullptr |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 57 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 58 | android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandle( |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 59 | audio_port_handle_t portHandle); |
| 60 | |
| 61 | /** |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 62 | * Store a strong pointer to the stream and return a unique handle for future reference. |
| 63 | * The handle is guaranteed not to collide with an existing stream. |
| 64 | * @param serviceStream |
| 65 | * @return handle for identifying the stream |
| 66 | */ |
| 67 | aaudio_handle_t addStreamForHandle(android::sp<AAudioServiceStreamBase> serviceStream); |
| 68 | |
| 69 | /** |
| 70 | * @return string that can be added to dumpsys |
| 71 | */ |
| 72 | std::string dump() const; |
| 73 | |
| 74 | private: |
| 75 | static aaudio_handle_t bumpHandle(aaudio_handle_t handle); |
| 76 | |
| 77 | // Track stream using a unique handle that wraps. Only use positive half. |
| 78 | mutable std::mutex mHandleLock; |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 79 | // protected by mHandleLock |
| 80 | aaudio_handle_t mPreviousHandle = 0; |
| 81 | // protected by mHandleLock |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 82 | std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle; |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | } /* namespace aaudio */ |
| 87 | |
| 88 | #endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */ |