• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
21 #include <time.h>
22 
23 #include <android-base/thread_annotations.h>
24 #include <aaudio/AAudio.h>
25 
26 #include "binding/AAudioCommon.h"
27 #include "AAudioServiceStreamBase.h"
28 
29 namespace aaudio {
30 
31 class AAudioStreamTracker {
32 
33 public:
34     /**
35      * Remove any streams with the matching handle.
36      *
37      * @param streamHandle
38      * @return number of streams removed
39      */
40     int32_t removeStreamByHandle(aaudio_handle_t streamHandle);
41 
42     /**
43      * Look up a stream based on the handle.
44      *
45      * @param streamHandle
46      * @return strong pointer to the stream if found, or nullptr
47      */
48     android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandle(
49             aaudio_handle_t streamHandle);
50 
51     /**
52      * Look up a stream based on the AudioPolicy portHandle.
53      * Increment its service reference count if found.
54      *
55      * @param portHandle
56      * @return strong pointer to the stream if found, or nullptr
57      */
58     android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandle(
59             audio_port_handle_t portHandle);
60 
61     /**
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;
79     aaudio_handle_t               mPreviousHandle GUARDED_BY(mHandleLock) = 0;
80     std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>>
81             mStreamsByHandle GUARDED_BY(mHandleLock);
82 };
83 
84 
85 } /* namespace aaudio */
86 
87 #endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */
88