• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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_INTERRUPT_INFO_H
16 #define AUDIO_INTERRUPT_INFO_H
17 
18 #include <parcel.h>
19 #include <audio_stream_info.h>
20 #include <audio_source_type.h>
21 #include <audio_session_info.h>
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 static constexpr int32_t AUDIO_INTERRUPT_INFO_SIZE_LIMIT = 65535;
26 
27 enum ActionTarget {
28     CURRENT = 0,
29     INCOMING,
30     BOTH
31 };
32 
33 enum AudioFocuState {
34     ACTIVE = 0,
35     DUCK = 1,
36     PAUSE = 2,
37     STOP = 3,
38     PLACEHOLDER = 4,
39     PAUSEDBYREMOTE = 5,
40 };
41 
42 enum InterruptMode {
43     SHARE_MODE = 0,
44     INDEPENDENT_MODE = 1
45 };
46 
47 /**
48  * Enumerates the audio interrupt request type.
49  */
50 enum InterruptRequestType {
51     INTERRUPT_REQUEST_TYPE_DEFAULT = 0,
52 };
53 
54 /**
55  * Enumerates audio interrupt event.
56  */
57 enum InterruptCallbackEvent {
58     NO_EVENT = 0,
59     FORCE_EVENT = 1,
60     FORCE_PAUSED_TO_RESUME_EVENT = 2
61 };
62 
63 /**
64  * Enumerates audio interrupt request result type.
65  */
66 enum InterruptRequestResultType {
67     INTERRUPT_REQUEST_GRANT = 0,
68     INTERRUPT_REQUEST_REJECT = 1
69 };
70 
71 enum InterruptType {
72     INTERRUPT_TYPE_BEGIN = 1,
73     INTERRUPT_TYPE_END = 2,
74 };
75 
76 enum InterruptHint {
77     INTERRUPT_HINT_NONE = 0,
78     INTERRUPT_HINT_RESUME,
79     INTERRUPT_HINT_PAUSE,
80     INTERRUPT_HINT_STOP,
81     INTERRUPT_HINT_DUCK,
82     INTERRUPT_HINT_UNDUCK
83 };
84 
85 enum InterruptForceType {
86     /**
87      * Force type, system change audio state.
88      */
89     INTERRUPT_FORCE = 0,
90     /**
91      * Share type, application change audio state.
92      */
93     INTERRUPT_SHARE
94 };
95 
96 struct InterruptEvent {
97     /**
98      * Interrupt event type, begin or end
99      */
100     InterruptType eventType;
101     /**
102      * Interrupt force type, force or share
103      */
104     InterruptForceType forceType;
105     /**
106      * Interrupt hint type. In force type, the audio state already changed,
107      * but in share mode, only provide a hint for application to decide.
108      */
109     InterruptHint hintType;
110     /**
111      * Should callback to app. Default true;
112      * If false, interruptEvent should not callback to app.
113      */
114     bool callbackToApp = true;
115 };
116 
117 // Used internally only by AudioFramework
118 struct InterruptEventInternal {
119     InterruptType eventType;
120     InterruptForceType forceType;
121     InterruptHint hintType;
122     float duckVolume;
123     bool callbackToApp = true;
124 };
125 
126 enum AudioInterruptChangeType {
127     ACTIVATE_AUDIO_INTERRUPT = 0,
128     DEACTIVATE_AUDIO_INTERRUPT = 1,
129 };
130 
131 // Below APIs are added to handle compilation error in call manager
132 // Once call manager adapt to new interrupt APIs, this will be removed
133 enum InterruptActionType {
134     TYPE_ACTIVATED = 0,
135     TYPE_INTERRUPT = 1
136 };
137 
138 struct InterruptAction {
139     InterruptActionType actionType;
140     InterruptType interruptType;
141     InterruptHint interruptHint;
142     bool activated;
143 };
144 
145 struct AudioFocusEntry {
146     InterruptForceType forceType;
147     InterruptHint hintType;
148     ActionTarget actionOn;
149     bool isReject;
150 };
151 
152 struct AudioFocusConcurrency {
153     std::vector<SourceType> sourcesTypes;
154 };
155 
156 struct AudioFocusType {
157     AudioStreamType streamType = STREAM_DEFAULT;
158     SourceType sourceType = SOURCE_TYPE_INVALID;
159     bool isPlay = true;
160     bool operator==(const AudioFocusType &value) const
161     {
162         return streamType == value.streamType && sourceType == value.sourceType && isPlay == value.isPlay;
163     }
164 
165     bool operator<(const AudioFocusType &value) const
166     {
167         return streamType < value.streamType || (streamType == value.streamType && sourceType < value.sourceType);
168     }
169 
170     bool operator>(const AudioFocusType &value) const
171     {
172         return streamType > value.streamType || (streamType == value.streamType && sourceType > value.sourceType);
173     }
174 };
175 
176 enum InterruptStage {
177     INTERRUPT_STAGE_START = 0x10,
178     INTERRUPT_STAGE_RESTART = 0x11,
179     INTERRUPT_STAGE_STOP = 0x12,
180     INTERRUPT_STAGE_PAUSED = 0x20,
181     INTERRUPT_STAGE_RESUMED = 0x21,
182     INTERRUPT_STAGE_STOPPED = 0x30,
183     INTERRUPT_STAGE_DUCK_BEGIN = 0x40,
184     INTERRUPT_STAGE_DUCK_END = 0x41,
185     INTERRUPT_STAGE_TIMEOUT = 0x50
186 };
187 
188 enum InterruptSummary {
189     INTERRUPT_SUMMARY_INTERRUPT_OTHERS = 0,
190     INTERRUPT_SUMMARY_INTERRUPTED,
191     INTERRUPT_SUMMARY_INTERRUPT_BACKGROUND,
192 };
193 
194 class AudioInterrupt {
195 public:
196     static constexpr int32_t MAX_SOURCE_TYPE_NUM = 20;
197     StreamUsage streamUsage = STREAM_USAGE_INVALID;
198     ContentType contentType = CONTENT_TYPE_UNKNOWN;
199     AudioFocusType audioFocusType;
200     uint32_t streamId = 0;
201     bool pauseWhenDucked = false;
202     int32_t pid { -1 };
203     int32_t uid { -1 };
204     std::string deviceTag;
205     InterruptMode mode { SHARE_MODE };
206     bool parallelPlayFlag {false};
207     AudioFocusConcurrency currencySources;
208     AudioSessionStrategy sessionStrategy = { AudioConcurrencyMode::INVALID };
209     int32_t api = 0;
210     int32_t state {-1};
211 
212     AudioInterrupt() = default;
AudioInterrupt(StreamUsage streamUsage_,ContentType contentType_,AudioFocusType audioFocusType_,uint32_t streamId_)213     AudioInterrupt(StreamUsage streamUsage_, ContentType contentType_, AudioFocusType audioFocusType_,
214         uint32_t streamId_) : streamUsage(streamUsage_), contentType(contentType_), audioFocusType(audioFocusType_),
215         streamId(streamId_) {}
216     ~AudioInterrupt() = default;
Marshalling(Parcel & parcel,const AudioInterrupt & interrupt)217     static bool Marshalling(Parcel &parcel, const AudioInterrupt &interrupt)
218     {
219         bool res = parcel.WriteInt32(static_cast<int32_t>(interrupt.streamUsage));
220         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.contentType));
221         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.streamType));
222         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.sourceType));
223         res = res && parcel.WriteBool(interrupt.audioFocusType.isPlay);
224         res = res && parcel.WriteUint32(interrupt.streamId);
225         res = res && parcel.WriteBool(interrupt.pauseWhenDucked);
226         res = res && parcel.WriteInt32(interrupt.pid);
227         res = res && parcel.WriteInt32(interrupt.uid);
228         res = res && parcel.WriteString(interrupt.deviceTag);
229         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.mode));
230         res = res && parcel.WriteBool(interrupt.parallelPlayFlag);
231         size_t vct = interrupt.currencySources.sourcesTypes.size();
232         res = res && parcel.WriteInt32(static_cast<int32_t>(vct));
233         for (size_t i = 0; i < vct; i++) {
234             res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.currencySources.sourcesTypes[i]));
235         }
236         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.sessionStrategy.concurrencyMode));
237         res = res && parcel.WriteInt32(interrupt.api);
238         res = res && parcel.WriteInt32(interrupt.state);
239         return res;
240     }
Unmarshalling(Parcel & parcel,AudioInterrupt & interrupt)241     static void Unmarshalling(Parcel &parcel, AudioInterrupt &interrupt)
242     {
243         interrupt.streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
244         interrupt.contentType = static_cast<ContentType>(parcel.ReadInt32());
245         interrupt.audioFocusType.streamType = static_cast<AudioStreamType>(parcel.ReadInt32());
246         interrupt.audioFocusType.sourceType = static_cast<SourceType>(parcel.ReadInt32());
247         interrupt.audioFocusType.isPlay = parcel.ReadBool();
248         interrupt.streamId = parcel.ReadUint32();
249         interrupt.pauseWhenDucked = parcel.ReadBool();
250         interrupt.pid = parcel.ReadInt32();
251         interrupt.uid = parcel.ReadInt32();
252         interrupt.deviceTag = parcel.ReadString();
253         interrupt.mode = static_cast<InterruptMode>(parcel.ReadInt32());
254         interrupt.parallelPlayFlag = parcel.ReadBool();
255         int32_t vct = parcel.ReadInt32();
256         if (vct > MAX_SOURCE_TYPE_NUM) {
257             return;
258         }
259 
260         for (int32_t i = 0; i < vct; i++) {
261             SourceType sourceType = static_cast<SourceType>(parcel.ReadInt32());
262             interrupt.currencySources.sourcesTypes.push_back(sourceType);
263         }
264         interrupt.sessionStrategy.concurrencyMode = static_cast<AudioConcurrencyMode>(parcel.ReadInt32());
265         interrupt.api = parcel.ReadInt32();
266         interrupt.state = parcel.ReadInt32();
267     }
268 };
269 } // namespace AudioStandard
270 } // namespace OHOS
271 #endif // AUDIO_INTERRUPT_INFO_H