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