• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 MAX_SOURCE_TYPE_NUM = 20;
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 class AudioInterrupt {
177 public:
178     StreamUsage streamUsage = STREAM_USAGE_INVALID;
179     ContentType contentType = CONTENT_TYPE_UNKNOWN;
180     AudioFocusType audioFocusType;
181     uint32_t sessionId = 0;
182     bool pauseWhenDucked = false;
183     int32_t pid { -1 };
184     InterruptMode mode { SHARE_MODE };
185     bool parallelPlayFlag {false};
186     AudioFocusConcurrency currencySources;
187     AudioSessionStrategy sessionStrategy = { AudioConcurrencyMode::INVALID };
188 
189     AudioInterrupt() = default;
AudioInterrupt(StreamUsage streamUsage_,ContentType contentType_,AudioFocusType audioFocusType_,uint32_t sessionId_)190     AudioInterrupt(StreamUsage streamUsage_, ContentType contentType_, AudioFocusType audioFocusType_,
191         uint32_t sessionId_) : streamUsage(streamUsage_), contentType(contentType_), audioFocusType(audioFocusType_),
192         sessionId(sessionId_) {}
193     ~AudioInterrupt() = default;
Marshalling(Parcel & parcel,const AudioInterrupt & interrupt)194     static bool Marshalling(Parcel &parcel, const AudioInterrupt &interrupt)
195     {
196         bool res = parcel.WriteInt32(static_cast<int32_t>(interrupt.streamUsage));
197         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.contentType));
198         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.streamType));
199         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.sourceType));
200         res = res && parcel.WriteBool(interrupt.audioFocusType.isPlay);
201         res = res && parcel.WriteUint32(interrupt.sessionId);
202         res = res && parcel.WriteBool(interrupt.pauseWhenDucked);
203         res = res && parcel.WriteInt32(interrupt.pid);
204         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.mode));
205         res = res && parcel.WriteBool(interrupt.parallelPlayFlag);
206         size_t vct = interrupt.currencySources.sourcesTypes.size();
207         res = res && parcel.WriteInt32(static_cast<int32_t>(vct));
208         for (size_t i = 0; i < vct; i++) {
209             res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.currencySources.sourcesTypes[i]));
210         }
211         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.sessionStrategy.concurrencyMode));
212         return res;
213     }
Unmarshalling(Parcel & parcel,AudioInterrupt & interrupt)214     static void Unmarshalling(Parcel &parcel, AudioInterrupt &interrupt)
215     {
216         interrupt.streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
217         interrupt.contentType = static_cast<ContentType>(parcel.ReadInt32());
218         interrupt.audioFocusType.streamType = static_cast<AudioStreamType>(parcel.ReadInt32());
219         interrupt.audioFocusType.sourceType = static_cast<SourceType>(parcel.ReadInt32());
220         interrupt.audioFocusType.isPlay = parcel.ReadBool();
221         interrupt.sessionId = parcel.ReadUint32();
222         interrupt.pauseWhenDucked = parcel.ReadBool();
223         interrupt.pid = parcel.ReadInt32();
224         interrupt.mode = static_cast<InterruptMode>(parcel.ReadInt32());
225         interrupt.parallelPlayFlag = parcel.ReadBool();
226         int32_t vct = parcel.ReadInt32();
227         if (vct > MAX_SOURCE_TYPE_NUM) {
228             return;
229         }
230 
231         for (int32_t i = 0; i < vct; i++) {
232             SourceType sourceType = static_cast<SourceType>(parcel.ReadInt32());
233             interrupt.currencySources.sourcesTypes.push_back(sourceType);
234         }
235         interrupt.sessionStrategy.concurrencyMode = static_cast<AudioConcurrencyMode>(parcel.ReadInt32());
236     }
237 };
238 } // namespace AudioStandard
239 } // namespace OHOS
240 #endif // AUDIO_INTERRUPT_INFO_H