blob: d1301a21c5b498b1655f990fd8b98038bf74df55 [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 /**
Phil Burk7ebbc112020-05-13 15:55:17 -070035 * Remove any streams with the matching handle.
Phil Burk2fe718b2018-05-14 12:28:32 -070036 *
Phil Burk523b3042017-09-13 13:03:08 -070037 * @param streamHandle
Phil Burk7ebbc112020-05-13 15:55:17 -070038 * @return number of streams removed
Phil Burk523b3042017-09-13 13:03:08 -070039 */
Phil Burk7ebbc112020-05-13 15:55:17 -070040 int32_t removeStreamByHandle(aaudio_handle_t streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -070041
42 /**
43 * Look up a stream based on the handle.
Phil Burk2fe718b2018-05-14 12:28:32 -070044 *
Phil Burk523b3042017-09-13 13:03:08 -070045 * @param streamHandle
Phil Burk2fe718b2018-05-14 12:28:32 -070046 * @return strong pointer to the stream if found, or nullptr
Phil Burk523b3042017-09-13 13:03:08 -070047 */
Phil Burk7ebbc112020-05-13 15:55:17 -070048 android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -070049 aaudio_handle_t streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -070050
51 /**
Phil Burkbbd52862018-04-13 11:37:42 -070052 * Look up a stream based on the AudioPolicy portHandle.
Phil Burk2fe718b2018-05-14 12:28:32 -070053 * Increment its service reference count if found.
54 *
Phil Burkbbd52862018-04-13 11:37:42 -070055 * @param portHandle
Phil Burk2fe718b2018-05-14 12:28:32 -070056 * @return strong pointer to the stream if found, or nullptr
Phil Burkbbd52862018-04-13 11:37:42 -070057 */
Phil Burk7ebbc112020-05-13 15:55:17 -070058 android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandle(
Phil Burkbbd52862018-04-13 11:37:42 -070059 audio_port_handle_t portHandle);
60
61 /**
Phil Burk523b3042017-09-13 13:03:08 -070062 * 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
74private:
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;
Phil Burk2fe718b2018-05-14 12:28:32 -070079 // protected by mHandleLock
80 aaudio_handle_t mPreviousHandle = 0;
81 // protected by mHandleLock
Phil Burk523b3042017-09-13 13:03:08 -070082 std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle;
83};
84
85
86} /* namespace aaudio */
87
88#endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */