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 /** 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 * 40 * @param streamHandle 41 * @return strong pointer to the stream if it needs to be closed, or nullptr 42 */ 43 android::sp<AAudioServiceStreamBase> decrementAndRemoveStreamByHandle( 44 aaudio_handle_t streamHandle); 45 46 /** 47 * Look up a stream based on the handle. 48 * Increment its service reference count if found. 49 * 50 * @param streamHandle 51 * @return strong pointer to the stream if found, or nullptr 52 */ 53 android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandleAndIncrement( 54 aaudio_handle_t streamHandle); 55 56 /** 57 * Look up a stream based on the AudioPolicy portHandle. 58 * Increment its service reference count if found. 59 * 60 * @param portHandle 61 * @return strong pointer to the stream if found, or nullptr 62 */ 63 android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandleAndIncrement( 64 audio_port_handle_t portHandle); 65 66 /** 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; 84 // protected by mHandleLock 85 aaudio_handle_t mPreviousHandle = 0; 86 // protected by mHandleLock 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 */ 94