blob: 57ec42633f2c196955472ff30d50d367cc4937a6 [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 Burk2fe718b2018-05-14 12:28:32 -070035 * Find the stream associated with the handle.
36 * Decrement its reference counter. If zero and the stream needs
37 * to be closed then remove the stream and return a pointer to the stream.
38 * Otherwise return null if it does not need to be closed.
39 *
Phil Burk523b3042017-09-13 13:03:08 -070040 * @param streamHandle
Phil Burk2fe718b2018-05-14 12:28:32 -070041 * @return strong pointer to the stream if it needs to be closed, or nullptr
Phil Burk523b3042017-09-13 13:03:08 -070042 */
Phil Burk2fe718b2018-05-14 12:28:32 -070043 android::sp<AAudioServiceStreamBase> decrementAndRemoveStreamByHandle(
44 aaudio_handle_t streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -070045
46 /**
47 * Look up a stream based on the handle.
Phil Burk2fe718b2018-05-14 12:28:32 -070048 * Increment its service reference count if found.
49 *
Phil Burk523b3042017-09-13 13:03:08 -070050 * @param streamHandle
Phil Burk2fe718b2018-05-14 12:28:32 -070051 * @return strong pointer to the stream if found, or nullptr
Phil Burk523b3042017-09-13 13:03:08 -070052 */
Phil Burk2fe718b2018-05-14 12:28:32 -070053 android::sp<aaudio::AAudioServiceStreamBase> getStreamByHandleAndIncrement(
54 aaudio_handle_t streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -070055
56 /**
Phil Burkbbd52862018-04-13 11:37:42 -070057 * Look up a stream based on the AudioPolicy portHandle.
Phil Burk2fe718b2018-05-14 12:28:32 -070058 * Increment its service reference count if found.
59 *
Phil Burkbbd52862018-04-13 11:37:42 -070060 * @param portHandle
Phil Burk2fe718b2018-05-14 12:28:32 -070061 * @return strong pointer to the stream if found, or nullptr
Phil Burkbbd52862018-04-13 11:37:42 -070062 */
Phil Burk2fe718b2018-05-14 12:28:32 -070063 android::sp<aaudio::AAudioServiceStreamBase> findStreamByPortHandleAndIncrement(
Phil Burkbbd52862018-04-13 11:37:42 -070064 audio_port_handle_t portHandle);
65
66 /**
Phil Burk523b3042017-09-13 13:03:08 -070067 * Store a strong pointer to the stream and return a unique handle for future reference.
68 * The handle is guaranteed not to collide with an existing stream.
69 * @param serviceStream
70 * @return handle for identifying the stream
71 */
72 aaudio_handle_t addStreamForHandle(android::sp<AAudioServiceStreamBase> serviceStream);
73
74 /**
75 * @return string that can be added to dumpsys
76 */
77 std::string dump() const;
78
79private:
80 static aaudio_handle_t bumpHandle(aaudio_handle_t handle);
81
82 // Track stream using a unique handle that wraps. Only use positive half.
83 mutable std::mutex mHandleLock;
Phil Burk2fe718b2018-05-14 12:28:32 -070084 // protected by mHandleLock
85 aaudio_handle_t mPreviousHandle = 0;
86 // protected by mHandleLock
Phil Burk523b3042017-09-13 13:03:08 -070087 std::map<aaudio_handle_t, android::sp<aaudio::AAudioServiceStreamBase>> mStreamsByHandle;
88};
89
90
91} /* namespace aaudio */
92
93#endif /* AAUDIO_AAUDIO_STREAM_TRACKER_H */