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); 121 122 virtual int32_t UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig &config) = 0; 123 virtual void SetClientID(int32_t clientPid, int32_t clientUid, uint32_t appTokenId, uint64_t fullTokenId) = 0; 124 virtual void SetRendererInfo(const AudioRendererInfo &rendererInfo) = 0; 125 virtual void SetCapturerInfo(const AudioCapturerInfo &capturerInfo) = 0; 126 virtual int32_t SetAudioStreamInfo(const AudioStreamParams info, 127 const std::shared_ptr<AudioClientTracker> &proxyObj) = 0; 128 virtual int32_t GetAudioStreamInfo(AudioStreamParams &info) = 0; 129 virtual bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 130 SourceType sourceType = SOURCE_TYPE_MIC) = 0; 131 virtual bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 132 AudioPermissionState state) = 0; 133 virtual int32_t GetAudioSessionID(uint32_t &sessionID) = 0; 134 virtual void GetAudioPipeType(AudioPipeType &pipeType) = 0; 135 virtual State GetState() = 0; 136 virtual bool GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base) = 0; 137 virtual bool GetAudioPosition(Timestamp ×tamp, Timestamp::Timestampbase base) = 0; 138 virtual int32_t GetBufferSize(size_t &bufferSize) = 0; 139 virtual int32_t GetFrameCount(uint32_t &frameCount) = 0; 140 virtual int32_t GetLatency(uint64_t &latency) = 0; 141 virtual int32_t SetAudioStreamType(AudioStreamType audioStreamType) = 0; 142 virtual int32_t SetVolume(float volume) = 0; 143 virtual float GetVolume() = 0; 144 virtual int32_t SetDuckVolume(float volume) = 0; 145 virtual int32_t SetRenderRate(AudioRendererRate renderRate) = 0; 146 virtual AudioRendererRate GetRenderRate() = 0; 147 virtual int32_t SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback) = 0; 148 virtual int32_t SetSpeed(float speed) = 0; 149 virtual float GetSpeed() = 0; 150 virtual int32_t ChangeSpeed(uint8_t *buffer, int32_t bufferSize, 151 std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize) = 0; 152 153 virtual void SetUnderflowCount(uint32_t underflowCount) = 0; 154 virtual void SetOverflowCount(uint32_t overflowCount) = 0; 155 156 // callback mode api 157 virtual int32_t SetRenderMode(AudioRenderMode renderMode) = 0; 158 virtual AudioRenderMode GetRenderMode() = 0; 159 virtual int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback) = 0; 160 161 virtual int32_t SetRendererFirstFrameWritingCallback( 162 const std::shared_ptr<AudioRendererFirstFrameWritingCallback> &callback) = 0; 163 virtual void OnFirstFrameWriting() = 0; 164 165 virtual int32_t SetCaptureMode(AudioCaptureMode captureMode) = 0; 166 virtual AudioCaptureMode GetCaptureMode() = 0; 167 virtual int32_t SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback) = 0; 168 169 virtual int32_t GetBufferDesc(BufferDesc &bufDesc) = 0; 170 virtual int32_t GetBufQueueState(BufferQueueState &bufState) = 0; 171 virtual int32_t Enqueue(const BufferDesc &bufDesc) = 0; 172 virtual int32_t Clear() = 0; 173 174 virtual int32_t SetLowPowerVolume(float volume) = 0; 175 virtual float GetLowPowerVolume() = 0; 176 virtual float GetSingleStreamVolume() = 0; 177 virtual int32_t SetOffloadMode(int32_t state, bool isAppBack) = 0; 178 virtual int32_t UnsetOffloadMode() = 0; 179 180 // for effect 181 virtual AudioEffectMode GetAudioEffectMode() = 0; 182 virtual int32_t SetAudioEffectMode(AudioEffectMode effectMode) = 0; 183 184 virtual int64_t GetFramesWritten() = 0; 185 virtual int64_t GetFramesRead() = 0; 186 187 // Common APIs 188 virtual bool StartAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT, 189 AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReasonExt::ExtEnum::UNKNOWN) = 0; 190 virtual bool PauseAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT) = 0; 191 virtual bool StopAudioStream() = 0; 192 virtual bool ReleaseAudioStream(bool releaseRunner = true, bool destroyAtOnce = false) = 0; 193 virtual bool FlushAudioStream() = 0; 194 195 // Playback related APIs 196 virtual bool DrainAudioStream(bool stopFlag = false) = 0; 197 virtual int32_t Write(uint8_t *buffer, size_t buffer_size) = 0; 198 virtual int32_t Write(uint8_t *pcmBuffer, size_t pcmSize, uint8_t *metaBuffer, size_t metaSize) = 0; 199 200 // Recording related APIs 201 virtual int32_t Read(uint8_t &buffer, size_t userSize, bool isBlockingRead) = 0; 202 203 // for inner capturer 204 virtual void SetInnerCapturerState(bool isInnerCapturer) = 0; 205 virtual void SetPrivacyType(AudioPrivacyType privacyType) = 0; 206 207 virtual uint32_t GetUnderflowCount() = 0; 208 virtual uint32_t GetOverflowCount() = 0; 209 210 virtual void SetRendererPositionCallback(int64_t markPosition, 211 const std::shared_ptr<RendererPositionCallback> &callback) = 0; 212 virtual void UnsetRendererPositionCallback() = 0; 213 214 virtual void SetRendererPeriodPositionCallback(int64_t markPosition, 215 const std::shared_ptr<RendererPeriodPositionCallback> &callback) = 0; 216 virtual void UnsetRendererPeriodPositionCallback() = 0; 217 218 virtual void SetCapturerPositionCallback(int64_t markPosition, 219 const std::shared_ptr<CapturerPositionCallback> &callback) = 0; 220 virtual void UnsetCapturerPositionCallback() = 0; 221 222 virtual void SetCapturerPeriodPositionCallback(int64_t markPosition, 223 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) = 0; 224 225 virtual void UnsetCapturerPeriodPositionCallback() = 0; 226 virtual int32_t SetRendererSamplingRate(uint32_t sampleRate) = 0; 227 virtual uint32_t GetRendererSamplingRate() = 0; 228 virtual int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec) = 0; 229 virtual void SetApplicationCachePath(const std::string cachePath) = 0; 230 virtual int32_t SetChannelBlendMode(ChannelBlendMode blendMode) = 0; 231 virtual int32_t SetVolumeWithRamp(float volume, int32_t duration) = 0; 232 virtual void SetPreferredFrameSize(int32_t frameSize) = 0; 233 virtual IAudioStream::StreamClass GetStreamClass() = 0; 234 virtual void SetStreamTrackerState(bool trackerRegisteredState) = 0; 235 virtual void GetSwitchInfo(SwitchInfo& info) = 0; 236 237 // for get pipetype 238 virtual bool GetOffloadEnable() = 0; 239 240 virtual bool GetSpatializationEnabled() = 0; 241 242 virtual bool GetHighResolutionEnabled() = 0; 243 244 //for wakeup capturer 245 virtual void SetWakeupCapturerState(bool isWakeupCapturer) = 0; 246 247 virtual void SetCapturerSource(int capturerSource) = 0; 248 249 virtual void UpdateLatencyTimestamp(std::string ×tamp, bool isRenderer) = 0; 250 251 virtual bool RestoreAudioStream(bool needStoreState = true) 252 { 253 return 0; 254 } 255 SetState()256 virtual void SetState() {} 257 258 bool IsFormatValid(uint8_t format); 259 260 bool IsRendererChannelValid(uint8_t channel); 261 262 bool IsCapturerChannelValid(uint8_t channel); 263 264 bool IsEncodingTypeValid(uint8_t encodingType); 265 266 bool IsSamplingRateValid(uint32_t samplingRate); 267 268 bool IsRendererChannelLayoutValid(uint64_t channelLayout); 269 270 bool IsCapturerChannelLayoutValid(uint64_t channelLayout); 271 272 bool IsPlaybackChannelRelatedInfoValid(uint8_t channels, uint64_t channelLayout); 273 274 bool IsRecordChannelRelatedInfoValid(uint8_t channels, uint64_t channelLayout); 275 276 virtual void SetSilentModeAndMixWithOthers(bool on) = 0; 277 278 virtual bool GetSilentModeAndMixWithOthers() = 0; 279 }; 280 } // namespace AudioStandard 281 } // namespace OHOS 282 #endif // I_AUDIO_STREAM_H 283