blob: 70d440d6dfb987a219b14ddbb510103b81cfc47c [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 /**
49 * Store a strong pointer to the stream and return a unique handle for future reference.
50 * The handle is guaranteed not to collide with an existing stream.
51 * @param serviceStream
52 * @return handle for identifying the stream
53 */
54 aaudio_handle_t addStreamForHandle(android::sp<AAudioServiceStreamBase> serviceStream);
55
56 /**
57 * @return string that can be added to dumpsys
58 */
59 std::string dump() const;
60
61private:
62 static aaudio_handle_t bumpHandle(aaudio_handle_t handle);
63
64 // Track stream using a unique handle that wraps. Only use positive half.
65 mutable std::mutex mHandleLock;
66 std::atomic<aaudio_handle_t> mPreviousHandle{0};
67 std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle;
68};
69
70
71} /* namespace aaudio */
72
73#endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */