1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License") = 0; 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 I_AUDIO_STREAM_H 16 #define I_AUDIO_STREAM_H 17 18 #include <map> 19 #include <memory> 20 #include "timestamp.h" 21 #include "audio_info.h" 22 #include "audio_capturer.h" 23 #include "audio_renderer.h" 24 #include "audio_stream_manager.h" 25 #include "audio_device_info.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class AudioStreamCallback { 30 public: 31 virtual ~AudioStreamCallback() = default; 32 /** 33 * Called when stream state is updated. 34 * 35 * @param state Indicates the InterruptEvent information needed by client. 36 * For details, refer InterruptEvent struct in audio_info.h 37 */ 38 virtual void OnStateChange(const State state, const StateChangeCmdType cmdType = CMD_FROM_CLIENT) = 0; 39 }; 40 41 class RendererOrCapturerPolicyServiceDiedCallback { 42 public: 43 virtual ~RendererOrCapturerPolicyServiceDiedCallback() = default; 44 /** 45 * Called when AudioPolicyService died. 46 * 47 * since 11 48 */ 49 virtual void OnAudioPolicyServiceDied() = 0; 50 }; 51 52 class IAudioStream { 53 public: 54 enum StreamClass : uint32_t { 55 PA_STREAM = 0, 56 FAST_STREAM, 57 VOIP_STREAM, 58 }; 59 60 struct SwitchInfo { 61 AudioStreamParams params; 62 AudioStreamType eStreamType; 63 int32_t appUid; 64 AudioRendererInfo rendererInfo; 65 AudioCapturerInfo capturerInfo; 66 State state; 67 uint32_t sessionId; 68 std::string cachePath = ""; 69 uint32_t rendererSampleRate; 70 uint32_t underFlowCount = 0; 71 uint32_t overFlowCount = 0; 72 AudioEffectMode effectMode; 73 AudioRenderMode renderMode; 74 AudioCaptureMode captureMode; 75 AudioRendererRate renderRate; 76 int32_t clientPid = 0; 77 int32_t clientUid = 0; 78 std::shared_ptr<AudioClientTracker> proxyObj; 79 AudioPrivacyType privacyType; 80 float volume; 81 int32_t rendererFlags = AUDIO_FLAG_NORMAL; 82 83 bool streamTrackerRegistered = false; 84 85 uint64_t frameMarkPosition = 0; 86 uint64_t framePeriodNumber = 0; 87 88 uint64_t totalBytesWritten = 0; 89 uint64_t framePeriodWritten = 0; 90 std::shared_ptr<RendererPositionCallback> renderPositionCb; 91 std::shared_ptr<RendererPeriodPositionCallback> renderPeriodPositionCb; 92 93 uint64_t totalBytesRead = 0; 94 uint64_t framePeriodRead = 0; 95 std::shared_ptr<CapturerPositionCallback> capturePositionCb; 96 std::shared_ptr<CapturerPeriodPositionCallback> capturePeriodPositionCb; 97 98 // callback info 99 std::shared_ptr<AudioStreamCallback> audioStreamCallback; 100 std::shared_ptr<AudioRendererWriteCallback> rendererWriteCallback; 101 std::shared_ptr<AudioCapturerReadCallback> capturerReadCallback; 102 std::shared_ptr<AudioRendererFirstFrameWritingCallback> rendererFirstFrameWritingCallback; 103 104 std::optional<int32_t> userSettedPreferredFrameSize = std::nullopt; 105 bool silentModeAndMixWithOthers = false; 106 }; 107 108 virtual ~IAudioStream() = default; 109 110 static int32_t GetByteSizePerFrame(const AudioStreamParams ¶ms, size_t &result); 111 static bool IsStreamSupported(int32_t streamFlags, const AudioStreamParams ¶ms); 112 static std::shared_ptr<IAudioStream> GetPlaybackStream(StreamClass streamClass, AudioStreamParams params, 113 AudioStreamType eStreamType, int32_t appUid); 114 static std::shared_ptr<IAudioStream> GetRecordStream(StreamClass streamClass, AudioStreamParams params, 115 AudioStreamType eStreamType, int32_t appUid); 116 117 static AudioStreamType GetStreamType(ContentType contentType, StreamUsage streamUsage); 118 static std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> CreateStreamMap(); 119 static void CreateStreamMap(std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> &streamMap); 120 static const std::string GetEffectSceneName(const StreamUsage &streamUsage); IsFastStreamClass(StreamClass streamClass)121 static inline bool IsFastStreamClass(StreamClass streamClass) 122 { 123 if (streamClass == FAST_STREAM || streamClass == VOIP_STREAM) { 124 return true; 125 } 126 return false; 127 } 128 129 virtual int32_t UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig &config) = 0; 130 virtual void SetClientID(int32_t clientPid, int32_t clientUid, uint32_t appTokenId, uint64_t fullTokenId) = 0; 131 virtual void SetRendererInfo(const AudioRendererInfo &rendererInfo) = 0; 132 virtual void SetCapturerInfo(const AudioCapturerInfo &capturerInfo) = 0; 133 virtual int32_t SetAudioStreamInfo(const AudioStreamParams info, 134 const std::shared_ptr<AudioClientTracker> &proxyObj) = 0; 135 virtual int32_t GetAudioStreamInfo(AudioStreamParams &info) = 0; 136 virtual bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 137 SourceType sourceType = SOURCE_TYPE_MIC) = 0; 138 virtual bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 139 AudioPermissionState state) = 0; 140 virtual int32_t GetAudioSessionID(uint32_t &sessionID) = 0; 141 virtual void GetAudioPipeType(AudioPipeType &pipeType) = 0; 142 virtual State GetState() = 0; 143 virtual bool GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base) = 0; 144 virtual bool GetAudioPosition(Timestamp ×tamp, Timestamp::Timestampbase base) = 0; 145 virtual int32_t GetBufferSize(size_t &bufferSize) = 0; 146 virtual int32_t GetFrameCount(uint32_t &frameCount) = 0; 147 virtual int32_t GetLatency(uint64_t &latency) = 0; 148 virtual int32_t SetAudioStreamType(AudioStreamType audioStreamType) = 0; 149 virtual int32_t SetVolume(float volume) = 0; 150 virtual float GetVolume() = 0; 151 virtual int32_t SetDuckVolume(float volume) = 0; 152 virtual int32_t SetRenderRate(AudioRendererRate renderRate) = 0; 153 virtual AudioRendererRate GetRenderRate() = 0; 154 virtual int32_t SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback) = 0; 155 virtual int32_t SetSpeed(float speed) = 0; 156 virtual float GetSpeed() = 0; 157 virtual int32_t ChangeSpeed(uint8_t *buffer, int32_t bufferSize, 158 std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize) = 0; 159 160 virtual void SetUnderflowCount(uint32_t underflowCount) = 0; 161 virtual void SetOverflowCount(uint32_t overflowCount) = 0; 162 163 // callback mode api 164 virtual int32_t SetRenderMode(AudioRenderMode renderMode) = 0; 165 virtual AudioRenderMode GetRenderMode() = 0; 166 virtual int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback) = 0; 167 168 virtual int32_t SetRendererFirstFrameWritingCallback( 169 const std::shared_ptr<AudioRendererFirstFrameWritingCallback> &callback) = 0; 170 virtual void OnFirstFrameWriting() = 0; 171 172 virtual int32_t SetCaptureMode(AudioCaptureMode captureMode) = 0; 173 virtual AudioCaptureMode GetCaptureMode() = 0; 174 virtual int32_t SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback) = 0; 175 176 virtual int32_t GetBufferDesc(BufferDesc &bufDesc) = 0; 177 virtual int32_t GetBufQueueState(BufferQueueState &bufState) = 0; 178 virtual int32_t Enqueue(const BufferDesc &bufDesc) = 0; 179 virtual int32_t Clear() = 0; 180 181 virtual int32_t SetLowPowerVolume(float volume) = 0; 182 virtual float GetLowPowerVolume() = 0; 183 virtual float GetSingleStreamVolume() = 0; 184 virtual int32_t SetOffloadMode(int32_t state, bool isAppBack) = 0; 185 virtual int32_t UnsetOffloadMode() = 0; 186 187 // for effect 188 virtual AudioEffectMode GetAudioEffectMode() = 0; 189 virtual int32_t SetAudioEffectMode(AudioEffectMode effectMode) = 0; 190 191 virtual int64_t GetFramesWritten() = 0; 192 virtual int64_t GetFramesRead() = 0; 193 194 // Common APIs 195 virtual bool StartAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT, 196 AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN) = 0; 197 virtual bool PauseAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT) = 0; 198 virtual bool StopAudioStream() = 0; 199 virtual bool ReleaseAudioStream(bool releaseRunner = true, bool destroyAtOnce = false) = 0; 200 virtual bool FlushAudioStream() = 0; 201 202 // Playback related APIs 203 virtual bool DrainAudioStream(bool stopFlag = false) = 0; 204 virtual int32_t Write(uint8_t *buffer, size_t buffer_size) = 0; 205 virtual int32_t Write(uint8_t *pcmBuffer, size_t pcmSize, uint8_t *metaBuffer, size_t metaSize) = 0; 206 207 // Recording related APIs 208 virtual int32_t Read(uint8_t &buffer, size_t userSize, bool isBlockingRead) = 0; 209 210 // for inner capturer 211 virtual void SetInnerCapturerState(bool isInnerCapturer) = 0; 212 virtual void SetPrivacyType(AudioPrivacyType privacyType) = 0; 213 214 virtual uint32_t GetUnderflowCount() = 0; 215 virtual uint32_t GetOverflowCount() = 0; 216 217 virtual void SetRendererPositionCallback(int64_t markPosition, 218 const std::shared_ptr<RendererPositionCallback> &callback) = 0; 219 virtual void UnsetRendererPositionCallback() = 0; 220 221 virtual void SetRendererPeriodPositionCallback(int64_t markPosition, 222 const std::shared_ptr<RendererPeriodPositionCallback> &callback) = 0; 223 virtual void UnsetRendererPeriodPositionCallback() = 0; 224 225 virtual void SetCapturerPositionCallback(int64_t markPosition, 226 const std::shared_ptr<CapturerPositionCallback> &callback) = 0; 227 virtual void UnsetCapturerPositionCallback() = 0; 228 229 virtual void SetCapturerPeriodPositionCallback(int64_t markPosition, 230 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) = 0; 231 232 virtual void UnsetCapturerPeriodPositionCallback() = 0; 233 virtual int32_t SetRendererSamplingRate(uint32_t sampleRate) = 0; 234 virtual uint32_t GetRendererSamplingRate() = 0; 235 virtual int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec) = 0; 236 virtual void SetApplicationCachePath(const std::string cachePath) = 0; 237 virtual int32_t SetChannelBlendMode(ChannelBlendMode blendMode) = 0; 238 virtual int32_t SetVolumeWithRamp(float volume, int32_t duration) = 0; 239 virtual void SetPreferredFrameSize(int32_t frameSize) = 0; 240 virtual IAudioStream::StreamClass GetStreamClass() = 0; 241 virtual void SetStreamTrackerState(bool trackerRegisteredState) = 0; 242 virtual void GetSwitchInfo(SwitchInfo& info) = 0; 243 244 // for get pipetype 245 virtual bool GetOffloadEnable() = 0; 246 247 virtual bool GetSpatializationEnabled() = 0; 248 249 virtual bool GetHighResolutionEnabled() = 0; 250 251 //for wakeup capturer 252 virtual void SetWakeupCapturerState(bool isWakeupCapturer) = 0; 253 254 virtual void SetCapturerSource(int capturerSource) = 0; 255 256 virtual void UpdateLatencyTimestamp(std::string ×tamp, bool isRenderer) = 0; 257 258 virtual bool RestoreAudioStream(bool needStoreState = true) 259 { 260 return 0; 261 } 262 SetState()263 virtual void SetState() {} 264 265 bool IsFormatValid(uint8_t format); 266 267 bool IsRendererChannelValid(uint8_t channel); 268 269 bool IsCapturerChannelValid(uint8_t channel); 270 271 bool IsEncodingTypeValid(uint8_t encodingType); 272 273 bool IsSamplingRateValid(uint32_t samplingRate); 274 275 bool IsRendererChannelLayoutValid(uint64_t channelLayout); 276 277 bool IsCapturerChannelLayoutValid(uint64_t channelLayout); 278 279 bool IsPlaybackChannelRelatedInfoValid(uint8_t channels, uint64_t channelLayout); 280 281 bool IsRecordChannelRelatedInfoValid(uint8_t channels, uint64_t channelLayout); 282 283 virtual void SetSilentModeAndMixWithOthers(bool on) = 0; 284 285 virtual bool GetSilentModeAndMixWithOthers() = 0; 286 287 virtual int32_t GetAudioTimestampInfo(Timestamp ×tamp, Timestamp::Timestampbase base) = 0; 288 289 virtual void SetSwitchingStatus(bool isSwitching) = 0; 290 }; 291 } // namespace AudioStandard 292 } // namespace OHOS 293 #endif // I_AUDIO_STREAM_H 294