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 | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 35 | * Find the stream associated with the handle. |
| 36 | * Decrement its reference counter. If zero and the stream needs |
| 37 | * to be closed then remove the stream and return a pointer to the stream. |
| 38 | * Otherwise return null if it does not need to be closed. |
| 39 | * |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 40 | * @param streamHandle |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 41 | * @return strong pointer to the stream if it needs to be closed, or nullptr |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 42 | */ |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 43 | android::sp<AAudioServiceStreamBase> decrementAndRemoveStreamByHandle( |
| 44 | aaudio_handle_t streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Look up a stream based on the handle. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 48 | * Increment its service reference count if found. |
| 49 | * |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 50 | * @param streamHandle |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 51 | * @return strong pointer to the stream if found, or nullptr |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 52 | */ |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 53 | android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandleAndIncrement( |
| 54 | aaudio_handle_t streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 57 | * Look up a stream based on the AudioPolicy portHandle. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 58 | * Increment its service reference count if found. |
| 59 | * |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 60 | * @param portHandle |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 61 | * @return strong pointer to the stream if found, or nullptr |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 62 | */ |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 63 | android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandleAndIncrement( |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 64 | audio_port_handle_t portHandle); |
| 65 | |
| 66 | /** |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 67 | * Store a strong pointer to the stream and return a unique handle for future reference. |
| 68 | * The handle is guaranteed not to collide with an existing stream. |
| 69 | * @param serviceStream |
| 70 | * @return handle for identifying the stream |
| 71 | */ |
| 72 | aaudio_handle_t addStreamForHandle(android::sp<AAudioServiceStreamBase> serviceStream); |
| 73 | |
| 74 | /** |
| 75 | * @return string that can be added to dumpsys |
| 76 | */ |
| 77 | std::string dump() const; |
| 78 | |
| 79 | private: |
| 80 | static aaudio_handle_t bumpHandle(aaudio_handle_t handle); |
| 81 | |
| 82 | // Track stream using a unique handle that wraps. Only use positive half. |
| 83 | mutable std::mutex mHandleLock; |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame^] | 84 | // protected by mHandleLock |
| 85 | aaudio_handle_t mPreviousHandle = 0; |
| 86 | // protected by mHandleLock |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 87 | std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | } /* namespace aaudio */ |
| 92 | |
| 93 | #endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */ |