blob: 51a783d308521b02aa413b68a8417beb32e70b9c [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
Phil Burk0bd745e2020-10-17 18:20:01 +000020#include <mutex>
Phil Burk523b3042017-09-13 13:03:08 -070021#include <time.h>
Phil Burk523b3042017-09-13 13:03:08 -070022
Phil Burk0bd745e2020-10-17 18:20:01 +000023#include <android-base/thread_annotations.h>
Phil Burk523b3042017-09-13 13:03:08 -070024#include <aaudio/AAudio.h>
25
26#include "binding/AAudioCommon.h"
Phil Burk523b3042017-09-13 13:03:08 -070027#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 */
jiabine1001322023-12-04 23:58:39 +000040 int32_t removeStreamByHandle(aaudio_handle_t streamHandle) EXCLUDES(mHandleLock);
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(
jiabine1001322023-12-04 23:58:39 +000049 aaudio_handle_t streamHandle) EXCLUDES(mHandleLock);
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(
jiabine1001322023-12-04 23:58:39 +000059 audio_port_handle_t portHandle) EXCLUDES(mHandleLock);
Phil Burkbbd52862018-04-13 11:37:42 -070060
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 */
jiabine1001322023-12-04 23:58:39 +000067 aaudio_handle_t addStreamForHandle(const android::sp<AAudioServiceStreamBase>& serviceStream)
68 EXCLUDES(mHandleLock);
Phil Burk523b3042017-09-13 13:03:08 -070069
70 /**
71 * @return string that can be added to dumpsys
72 */
73 std::string dump() const;
74
75private:
76 static aaudio_handle_t bumpHandle(aaudio_handle_t handle);
77
78 // Track stream using a unique handle that wraps. Only use positive half.
Phil Burk0bd745e2020-10-17 18:20:01 +000079 mutable std::mutex mHandleLock;
80 aaudio_handle_t mPreviousHandle GUARDED_BY(mHandleLock) = 0;
81 std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>>
82 mStreamsByHandle GUARDED_BY(mHandleLock);
Phil Burk523b3042017-09-13 13:03:08 -070083};
84
85
86} /* namespace aaudio */
87
88#endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */