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 request result type.
56  */
57 enum InterruptRequestResultType {
58     INTERRUPT_REQUEST_GRANT = 0,
59     INTERRUPT_REQUEST_REJECT = 1
60 };
61 
62 enum InterruptType {
63     INTERRUPT_TYPE_BEGIN = 1,
64     INTERRUPT_TYPE_END = 2,
65 };
66 
67 enum InterruptHint {
68     INTERRUPT_HINT_NONE = 0,
69     INTERRUPT_HINT_RESUME,
70     INTERRUPT_HINT_PAUSE,
71     INTERRUPT_HINT_STOP,
72     INTERRUPT_HINT_DUCK,
73     INTERRUPT_HINT_UNDUCK
74 };
75 
76 enum InterruptForceType {
77     /**
78      * Force type, system change audio state.
79      */
80     INTERRUPT_FORCE = 0,
81     /**
82      * Share type, application change audio state.
83      */
84     INTERRUPT_SHARE
85 };
86 
87 struct InterruptEvent {
88     /**
89      * Interrupt event type, begin or end
90      */
91     InterruptType eventType;
92     /**
93      * Interrupt force type, force or share
94      */
95     InterruptForceType forceType;
96     /**
97      * Interrupt hint type. In force type, the audio state already changed,
98      * but in share mode, only provide a hint for application to decide.
99      */
100     InterruptHint hintType;
101     /**
102      * Should callback to app. Default true;
103      * If false, interruptEvent should not callback to app.
104      */
105     bool callbackToApp = true;
106 };
107 
108 // Used internally only by AudioFramework
109 struct InterruptEventInternal {
110     InterruptType eventType;
111     InterruptForceType forceType;
112     InterruptHint hintType;
113     float duckVolume;
114     bool callbackToApp = true;
115 };
116 
117 enum AudioInterruptChangeType {
118     ACTIVATE_AUDIO_INTERRUPT = 0,
119     DEACTIVATE_AUDIO_INTERRUPT = 1,
120 };
121 
122 // Below APIs are added to handle compilation error in call manager
123 // Once call manager adapt to new interrupt APIs, this will be removed
124 enum InterruptActionType {
125     TYPE_ACTIVATED = 0,
126     TYPE_INTERRUPT = 1
127 };
128 
129 struct InterruptAction {
130     InterruptActionType actionType;
131     InterruptType interruptType;
132     InterruptHint interruptHint;
133     bool activated;
134 };
135 
136 struct AudioFocusEntry {
137     InterruptForceType forceType;
138     InterruptHint hintType;
139     ActionTarget actionOn;
140     bool isReject;
141 };
142 
143 struct AudioFocusConcurrency {
144     std::vector<SourceType> sourcesTypes;
145 };
146 
147 struct AudioFocusType {
148     AudioStreamType streamType = STREAM_DEFAULT;
149     SourceType sourceType = SOURCE_TYPE_INVALID;
150     bool isPlay = true;
151     bool operator==(const AudioFocusType &value) const
152     {
153         return streamType == value.streamType && sourceType == value.sourceType && isPlay == value.isPlay;
154     }
155 
156     bool operator<(const AudioFocusType &value) const
157     {
158         return streamType < value.streamType || (streamType == value.streamType && sourceType < value.sourceType);
159     }
160 
161     bool operator>(const AudioFocusType &value) const
162     {
163         return streamType > value.streamType || (streamType == value.streamType && sourceType > value.sourceType);
164     }
165 };
166 
167 class AudioInterrupt {
168 public:
169     StreamUsage streamUsage = STREAM_USAGE_INVALID;
170     ContentType contentType = CONTENT_TYPE_UNKNOWN;
171     AudioFocusType audioFocusType;
172     uint32_t sessionId = 0;
173     bool pauseWhenDucked = false;
174     int32_t pid { -1 };
175     InterruptMode mode { SHARE_MODE };
176     bool parallelPlayFlag {false};
177     AudioFocusConcurrency currencySources;
178     AudioSessionStrategy sessionStrategy = { AudioConcurrencyMode::INVALID };
179 
180     AudioInterrupt() = default;
AudioInterrupt(StreamUsage streamUsage_,ContentType contentType_,AudioFocusType audioFocusType_,uint32_t sessionId_)181     AudioInterrupt(StreamUsage streamUsage_, ContentType contentType_, AudioFocusType audioFocusType_,
182         uint32_t sessionId_) : streamUsage(streamUsage_), contentType(contentType_), audioFocusType(audioFocusType_),
183         sessionId(sessionId_) {}
184     ~AudioInterrupt() = default;
Marshalling(Parcel & parcel,const AudioInterrupt & interrupt)185     static bool Marshalling(Parcel &parcel, const AudioInterrupt &interrupt)
186     {
187         bool res = parcel.WriteInt32(static_cast<int32_t>(interrupt.streamUsage));
188         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.contentType));
189         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.streamType));
190         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.audioFocusType.sourceType));
191         res = res && parcel.WriteBool(interrupt.audioFocusType.isPlay);
192         res = res && parcel.WriteUint32(interrupt.sessionId);
193         res = res && parcel.WriteBool(interrupt.pauseWhenDucked);
194         res = res && parcel.WriteInt32(interrupt.pid);
195         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.mode));
196         res = res && parcel.WriteBool(interrupt.parallelPlayFlag);
197         size_t vct = interrupt.currencySources.sourcesTypes.size();
198         res = res && parcel.WriteInt32(static_cast<int32_t>(vct));
199         for (size_t i = 0; i < vct; i++) {
200             res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.currencySources.sourcesTypes[i]));
201         }
202         res = res && parcel.WriteInt32(static_cast<int32_t>(interrupt.sessionStrategy.concurrencyMode));
203         return res;
204     }
Unmarshalling(Parcel & parcel,AudioInterrupt & interrupt)205     static void Unmarshalling(Parcel &parcel, AudioInterrupt &interrupt)
206     {
207         interrupt.streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
208         interrupt.contentType = static_cast<ContentType>(parcel.ReadInt32());
209         interrupt.audioFocusType.streamType = static_cast<AudioStreamType>(parcel.ReadInt32());
210         interrupt.audioFocusType.sourceType = static_cast<SourceType>(parcel.ReadInt32());
211         interrupt.audioFocusType.isPlay = parcel.ReadBool();
212         interrupt.sessionId = parcel.ReadUint32();
213         interrupt.pauseWhenDucked = parcel.ReadBool();
214         interrupt.pid = parcel.ReadInt32();
215         interrupt.mode = static_cast<InterruptMode>(parcel.ReadInt32());
216         interrupt.parallelPlayFlag = parcel.ReadBool();
217         int32_t vct = parcel.ReadInt32();
218         if (vct > MAX_SOURCE_TYPE_NUM) {
219             return;
220         }
221 
222         for (int32_t i = 0; i < vct; i++) {
223             SourceType sourceType = static_cast<SourceType>(parcel.ReadInt32());
224             interrupt.currencySources.sourcesTypes.push_back(sourceType);
225         }
226         interrupt.sessionStrategy.concurrencyMode = static_cast<AudioConcurrencyMode>(parcel.ReadInt32());
227     }
228 };
229 } // namespace AudioStandard
230 } // namespace OHOS
231 #endif // AUDIO_INTERRUPT_INFO_H