blob: 54e46ca18676a460eb42abe9d31993e45c25edf0 [file] [log] [blame]
Phil Burk523b3042017-09-13 13:03:08 -07001/*
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
29namespace aaudio {
30
31class AAudioStreamTracker {
32
33public:
34 /**
35 * Remove the stream associated with the handle.
36 * @param streamHandle
37 * @return strong pointer to the stream if found or to nullptr
38 */
39 android::sp<AAudioServiceStreamBase> removeStreamByHandle(aaudio_handle_t streamHandle);
40
41 /**
42 * Look up a stream based on the handle.
43 * @param streamHandle
44 * @return strong pointer to the stream if found or to nullptr
45 */
46 android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandle(aaudio_handle_t streamHandle);
47
48 /**
Phil Burkbbd52862018-04-13 11:37:42 -070049 * Look up a stream based on the AudioPolicy portHandle.
50 * @param portHandle
51 * @return strong pointer to the stream if found or to nullptr
52 */
53 android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandle(
54 audio_port_handle_t portHandle);
55
56 /**
Phil Burk523b3042017-09-13 13:03:08 -070057 * Store a strong pointer to the stream and return a unique handle for future reference.
58 * The handle is guaranteed not to collide with an existing stream.
59 * @param serviceStream
60 * @return handle for identifying the stream
61 */
62 aaudio_handle_t addStreamForHandle(android::sp<AAudioServiceStreamBase> serviceStream);
63
64 /**
65 * @return string that can be added to dumpsys
66 */
67 std::string dump() const;
68
69private:
70 static aaudio_handle_t bumpHandle(aaudio_handle_t handle);
71
72 // Track stream using a unique handle that wraps. Only use positive half.
73 mutable std::mutex mHandleLock;
74 std::atomic<aaudio_handle_t> mPreviousHandle{0};
75 std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle;
76};
77
78
79} /* namespace aaudio */
80
81#endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */