• 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 #ifndef AUDIO_ZONE_INFO_H
16 #define AUDIO_ZONE_INFO_H
17 
18 #include <string>
19 #include <set>
20 #include <vector>
21 #include "refbase.h"
22 #include "parcel.h"
23 #include "audio_device_info.h"
24 #include "audio_device_descriptor.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 enum class AudioZoneChangeReason {
29     UNKNOWN = 0,
30     BIND_NEW_DEVICE = 1,
31     BIND_NEW_APP,
32     UNBIND_APP,
33 };
34 
35 enum class AudioZoneInterruptReason {
36     UNKNOWN = 0,
37     LOCAL_INTERRUPT = 1,
38     REMOTE_INJECT = 2,
39     RELEASE_AUDIO_ZONE,
40     BIND_APP_TO_ZONE,
41     UNBIND_APP_FROM_ZONE,
42 };
43 
44 enum class AudioZoneFocusStrategy {
45     LOCAL_FOCUS_STRATEGY = 0,
46     DISTRIBUTED_FOCUS_STRATEGY = 1,
47 };
48 
49 class AudioZoneContext : public Parcelable {
50 public:
51     AudioZoneFocusStrategy focusStrategy_ = AudioZoneFocusStrategy::LOCAL_FOCUS_STRATEGY;
52 
53     AudioZoneContext() = default;
54 
Marshalling(Parcel & parcel)55     bool Marshalling(Parcel &parcel) const
56     {
57         return parcel.WriteInt32(static_cast<int32_t>(focusStrategy_));
58     }
59 
Unmarshalling(Parcel & parcel)60     static AudioZoneContext *Unmarshalling(Parcel &parcel)
61     {
62         auto info = new(std::nothrow) AudioZoneContext();
63         if (info == nullptr) {
64             return nullptr;
65         }
66 
67         info->focusStrategy_ = static_cast<AudioZoneFocusStrategy>(parcel.ReadInt32());
68         return info;
69     }
70 };
71 
72 class AudioZoneDescriptor : public Parcelable {
73 public:
74     int32_t zoneId_ = -1;
75     std::string name_;
76     std::set<int32_t> uids_;
77     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices_;
78 
79     AudioZoneDescriptor() = default;
80 
Marshalling(Parcel & parcel)81     bool Marshalling(Parcel &parcel) const
82     {
83         bool result = parcel.WriteInt32(zoneId_);
84         result &= parcel.WriteString(name_);
85         result &= MarshallingSetInt32(uids_, parcel);
86         if (!result) {
87             return false;
88         }
89 
90         size_t size = devices_.size();
91         if (!parcel.WriteUint64(size)) {
92             return false;
93         }
94         for (const auto &device : devices_) {
95             if (!device->Marshalling(parcel)) {
96                 return false;
97             }
98         }
99         return true;
100     }
101 
UnmarshallingSelf(Parcel & parcel)102     void UnmarshallingSelf(Parcel &parcel)
103     {
104         zoneId_ = parcel.ReadInt32();
105         name_ = parcel.ReadString();
106         uids_ = UnmarshallingSetInt32<int32_t>(parcel);
107 
108         size_t size = parcel.ReadUint64();
109         if (size > std::numeric_limits<size_t>::max()) {
110             size = std::numeric_limits<size_t>::max();
111         }
112 
113         for (size_t i = 0; i < size; i++) {
114             std::shared_ptr<AudioDeviceDescriptor> device(AudioDeviceDescriptor::Unmarshalling(parcel));
115             if (device == nullptr) {
116                 devices_.clear();
117                 return;
118             }
119             devices_.emplace_back(device);
120         }
121     }
122 
Unmarshalling(Parcel & parcel)123     static AudioZoneDescriptor *Unmarshalling(Parcel &parcel)
124     {
125         auto desc = new(std::nothrow) AudioZoneDescriptor();
126         if (desc == nullptr) {
127             return nullptr;
128         }
129 
130         desc->UnmarshallingSelf(parcel);
131         return desc;
132     }
133 };
134 
135 struct AudioZoneStream : public Parcelable {
136     StreamUsage streamUsage = STREAM_USAGE_INVALID;
137     SourceType sourceType = SOURCE_TYPE_INVALID;
138     bool isPlay = true;
139     bool operator==(const AudioZoneStream &value) const
140     {
141         return streamUsage == value.streamUsage && sourceType == value.sourceType && isPlay == value.isPlay;
142     }
143 
144     bool operator<(const AudioZoneStream &value) const
145     {
146         return streamUsage < value.streamUsage || (streamUsage == value.streamUsage && sourceType < value.sourceType);
147     }
148 
149     bool operator>(const AudioZoneStream &value) const
150     {
151         return streamUsage > value.streamUsage || (streamUsage == value.streamUsage && sourceType > value.sourceType);
152     }
153 
MarshallingAudioZoneStream154     bool Marshalling(Parcel &parcel) const override
155     {
156         return parcel.WriteInt32(static_cast<int32_t>(streamUsage))
157             && parcel.WriteInt32(static_cast<int32_t>(sourceType))
158             && parcel.WriteBool(isPlay);
159     }
160 
UnmarshallingAudioZoneStream161     static AudioZoneStream *Unmarshalling(Parcel &parcel)
162     {
163         auto stream = new(std::nothrow) AudioZoneStream();
164         if (stream == nullptr) {
165             return nullptr;
166         }
167 
168         stream->streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
169         stream->sourceType = static_cast<SourceType>(parcel.ReadInt32());
170         stream->isPlay = parcel.ReadBool();
171         return stream;
172     }
173 };
174 } // namespace AudioStandard
175 } // namespace OHOS
176 #endif // AUDIO_ZONE_INFO_H