xref: /aosp_15_r20/frameworks/av/media/psh_utils/include/psh_utils/Token.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 
2 /*
3  * Copyright (C) 2024 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <string>
21 
22 namespace android::media::psh_utils {
23 
24 class Token {
25 public:
26     virtual ~Token() = default;
27     virtual std::string toString() const = 0;
28 };
29 
30 // Client tokens (one per Audio Client PID)
31 std::unique_ptr<Token> createAudioClientToken(pid_t pid, uid_t uid,
32         const std::string& additional = {});
33 
34 enum class WakeFlag {
35     kNone = 0,
36     kLowLatency = 1,
37     kLowPower = 2,
38 };
39 
toString(WakeFlag wakeFlag)40 inline std::string toString(WakeFlag wakeFlag) {
41     std::string result;
42     for (const auto& [flag, name] : std::initializer_list<std::pair<WakeFlag, std::string>> {
43             {WakeFlag::kLowLatency, "kLowLatency"},
44             {WakeFlag::kLowPower, "kLowPower"},
45         }) {
46         if (static_cast<int>(flag) & static_cast<int>(wakeFlag)) {
47             if (!result.empty()) result.append("|");
48             result.append(name);
49         }
50     }
51     return result;
52 }
53 
54 // Thread tokens (one per ThreadBase PID started).
55 std::unique_ptr<Token> createAudioThreadToken(
56         pid_t pid, const std::string& wakeLockName,
57         WakeFlag wakeFlag = WakeFlag::kNone, const std::string& additional = {});
58 
59 // AudioTrack/AudioRecord tokens.
60 std::unique_ptr<Token> createAudioTrackToken(uid_t uid, const std::string& additional = {});
61 
62 } // namespace android::media::psh_utils
63