• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ST_AUDIO_ZONE_H
17 #define ST_AUDIO_ZONE_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <set>
22 #include <list>
23 #include <utility>
24 #include <string>
25 #include "audio_zone_info.h"
26 #include "audio_device_descriptor.h"
27 #include "audio_zone_client_manager.h"
28 #include "audio_connected_device.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 class AudioZoneBindKey {
33 public:
34     explicit AudioZoneBindKey(int32_t uid);
35     AudioZoneBindKey(int32_t uid, const std::string &deviceTag);
36     AudioZoneBindKey(int32_t uid, const std::string &deviceTag, const std::string &streamTag);
37     AudioZoneBindKey(int32_t uid, const std::string &deviceTag, const std::string &streamTag,
38         const StreamUsage &usage);
39     AudioZoneBindKey(const AudioZoneBindKey &other);
40     AudioZoneBindKey(AudioZoneBindKey &&other);
41     AudioZoneBindKey &operator=(const AudioZoneBindKey &other);
42     AudioZoneBindKey &operator=(AudioZoneBindKey &&other);
43 
44     bool operator==(const AudioZoneBindKey &other) const;
45     bool operator!=(const AudioZoneBindKey &other) const;
46 
47     int32_t GetUid() const;
48     const std::string GetString() const;
49     bool IsContain(const AudioZoneBindKey &other) const;
50     const static std::vector<AudioZoneBindKey> GetSupportKeys(int32_t uid, const std::string &deviceTag,
51         const std::string &streamTag, const StreamUsage &usage);
52     const static std::vector<AudioZoneBindKey> GetSupportKeys(const AudioZoneBindKey &key);
53 
54 private:
55     int32_t uid_ = INVALID_UID;
56     std::string deviceTag_ = "";
57     std::string streamTag_ = "";
58     StreamUsage usage_ = StreamUsage::STREAM_USAGE_INVALID;
59 
60     void Assign(const AudioZoneBindKey &other);
61     void Swap(AudioZoneBindKey &&other);
62 };
63 
64 class AudioZone {
65 public:
66     AudioZone(std::shared_ptr<AudioZoneClientManager> manager, const std::string &name,
67         const AudioZoneContext &context, pid_t clientPid = 0);
68     ~AudioZone() = default;
69 
70     int32_t GetId();
71     const std::shared_ptr<AudioZoneDescriptor> GetDescriptor();
72     const std::string GetStringDescriptor();
73     const std::string GetName();
74 
75     void BindByKey(const AudioZoneBindKey &key);
76     void RemoveKey(const AudioZoneBindKey &key);
77     bool IsContainKey(const AudioZoneBindKey &key);
78 
79     int32_t AddDeviceDescriptor(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &devices);
80     int32_t RemoveDeviceDescriptor(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &devices);
81     int32_t EnableDeviceDescriptor(std::shared_ptr<AudioDeviceDescriptor> device);
82     int32_t DisableDeviceDescriptor(std::shared_ptr<AudioDeviceDescriptor> device);
83     bool IsDeviceConnect(std::shared_ptr<AudioDeviceDescriptor> device);
84     std::vector<std::shared_ptr<AudioDeviceDescriptor>> FetchOutputDevices(StreamUsage streamUsage,
85         int32_t clientUid, const RouterType &bypassType);
86     std::shared_ptr<AudioDeviceDescriptor> FetchInputDevice(SourceType sourceType, int32_t clientUid);
87 
88     int32_t EnableSystemVolumeProxy(pid_t clientPid, bool enable);
89     int32_t SetSystemVolumeLevel(const AudioVolumeType volumeType,
90         const int32_t volumeLevel, const int32_t volumeFlag = 0);
91     int32_t GetSystemVolumeLevel(AudioVolumeType volumeType);
92     bool IsVolumeProxyEnable();
93 
94     int32_t EnableChangeReport(pid_t clientPid, bool enable);
95 
96     int32_t UpdateDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> device);
97 
98     pid_t GetClientPid();
99 
100 private:
101     int32_t zoneId_ = -1;
102     std::string name_ = "";
103     std::list<AudioZoneBindKey> keys_;
104     std::list<std::pair<std::shared_ptr<AudioDeviceDescriptor>, bool>> devices_;
105     std::mutex zoneMutex_;
106     std::shared_ptr<AudioZoneClientManager> clientManager_;
107     std::set<pid_t> changeReportClientList_;
108     pid_t volumeProxyClientPid_ = 0;
109     pid_t zoneClientPid_ = 0;
110     bool isVolumeProxyEnabled_ = false;
111 
112     int32_t SetDeviceDescriptorState(const std::shared_ptr<AudioDeviceDescriptor> device, const bool enable);
113     void SendZoneChangeEvent(AudioZoneChangeReason reason);
114     const std::shared_ptr<AudioZoneDescriptor> GetDescriptorNoLock();
115 };
116 } // namespace AudioStandard
117 } // namespace OHOS
118 
119 #endif // ST_AUDIO_ZONE_H