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 16 #ifndef ISOUNDPOOL_H 17 #define ISOUNDPOOL_H 18 19 #include <string> 20 #include "audio_info.h" 21 #include "meta/format.h" 22 #include "media_errors.h" 23 24 namespace OHOS { 25 namespace Media { 26 struct PlayParams { 27 int32_t loop = 0; 28 int32_t rate = 0; // default AudioRendererRate::RENDER_RATE_NORMAL 29 float leftVolume = (float)1.0; 30 float rightVolume = (float)1.0; 31 int32_t priority = 0; 32 bool parallelPlayFlag = false; 33 std::string cacheDir; 34 int32_t audioHapticsSyncId = 0; 35 }; 36 37 class ISoundPoolCallback; 38 class ISoundPoolFrameWriteCallback; 39 40 class ISoundPool { 41 public: 42 virtual ~ISoundPool() = default; 43 44 /** 45 * @brief Load the sound from the specified path. 46 * 47 * @param url The path to the audio file 48 * @return Returns a sound ID. This value can be used to play or unload the sound. 49 * @since 1.0 50 * @version 1.0 51 */ 52 virtual int32_t Load(std::string url) = 0; 53 54 /** 55 * @brief Load the sound from a FileDescriptor.. 56 * 57 * @param fd A FileDescriptor object 58 * @param offset Offset to the start of the sound 59 * @param length Length of the sound 60 * @return Returns a sound ID. This value can be used to play or unload the sound. 61 * @since 1.0 62 * @version 1.0 63 */ 64 virtual int32_t Load(int32_t fd, int64_t offset, int64_t length) = 0; 65 66 /** 67 * @brief Play a sound from a sound ID. 68 * 69 * @param soundID Returned by the load() 70 * @param playParameters params Player parameters 71 * @return Returns a non-zero streamID if successful, zero if it fails. 72 * @since 1.0 73 * @version 1.0 74 */ 75 virtual int32_t Play(int32_t soundID, PlayParams playParameters) = 0; 76 77 /** 78 * @brief Stop a stream which is playing. 79 * 80 * @param streamID Returned by the play() 81 * @return Returns used to return the result. MSERR_OK if success 82 * @since 1.0 83 * @version 1.0 84 */ 85 virtual int32_t Stop(int32_t streamID) = 0; 86 87 /** 88 * @brief Set loop mode. 89 * 90 * @param streamID Returned by the play() 91 * @param loop Loop mode (0 = no loop, -1 = loop forever) 92 * @return Returns used to return the result. MSERR_OK if success 93 * @since 1.0 94 * @version 1.0 95 */ 96 virtual int32_t SetLoop(int32_t streamID, int32_t loop) = 0; 97 98 /** 99 * @brief Set stream priority. 100 * 101 * @param streamID Returned by the play() 102 * @param priority Stream priority (0 = lowest priority) 103 * @return Returns used to return the result. MSERR_OK if success 104 * @since 1.0 105 * @version 1.0 106 */ 107 virtual int32_t SetPriority(int32_t streamID, int32_t priority) = 0; 108 109 /** 110 * @brief Set playback rate. 111 * 112 * @param streamID Returned by the play() 113 * @param renderRate Playback rate 114 * @return Returns used to return the result. MSERR_OK if success 115 * @since 1.0 116 * @version 1.0 117 */ 118 virtual int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate) = 0; 119 120 /** 121 * @brief Set stream volume. 122 * 123 * @param streamID Returned by the play() 124 * @param leftVolume leftVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume 125 * @param rigthVolume rightVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume 126 * @return Returns used to return the result. MSERR_OK if success 127 * @since 1.0 128 * @version 1.0 129 */ 130 virtual int32_t SetVolume(int32_t streamID, float leftVolume, float rigthVolume) = 0; 131 132 /** 133 * @brief Unload a sound from a sound ID. 134 * 135 * @param soundID Returned by the load() 136 * @return Returns used to return the result. MSERR_OK if success 137 * @since 1.0 138 * @version 1.0 139 */ 140 virtual int32_t Unload(int32_t soundID) = 0; 141 142 /** 143 * @brief Releases the soundPool. This method uses an asynchronous callback to return the result. 144 * 145 * @return Returns used to return the result. MSERR_OK if success 146 * @since 1.0 147 * @version 1.0 148 */ 149 virtual int32_t Release() = 0; 150 151 /** 152 * @brief Register listens for soundpool 153 * 154 * @param soundPoolCallback The listen class for soundpool 155 * @return Returns used to return the result. MSERR_OK if success 156 * @since 1.0 157 * @version 1.0 158 */ 159 virtual int32_t SetSoundPoolCallback 160 (const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback) = 0; 161 162 /** 163 * @brief Register frame write listens for soundpool 164 * 165 * @param frameWriteCallback The frame writ listen class for soundpool 166 * @return Returns used to return the result. MSERR_OK if success 167 * @since 1.0 168 * @version 1.0 169 */ 170 virtual int32_t SetSoundPoolFrameWriteCallback 171 (const std::shared_ptr<ISoundPoolFrameWriteCallback> &frameWriteCallback) = 0; 172 }; 173 174 class ISoundPoolCallback { 175 public: 176 virtual ~ISoundPoolCallback() = default; 177 178 /** 179 * @brief Register listens for load result event. 180 * 181 * @param result used to listen for loaded soundId event 182 * @since 1.0 183 * @version 1.0 184 */ 185 virtual void OnLoadCompleted(int32_t soundId) = 0; 186 187 /** 188 * @brief Register the play finish event to listen for. 189 * 190 * @since 1.0 191 * @version 1.0 192 */ 193 virtual void OnPlayFinished(int32_t streamID) = 0; 194 195 /** 196 * @brief Register listens for sound play error events. 197 * 198 * @param errorCode Type of the sound play error event to listen for. 199 * @since 1.0 200 * @version 1.0 201 */ 202 virtual void OnError(int32_t errorCode) = 0; 203 204 /** 205 * @brief Register listens for sound play error events. 206 * 207 * @param errorInfo errorInfo 208 * @since 1.0 209 * @version 1.0 210 */ OnErrorOccurred(Format & errorInfo)211 virtual void OnErrorOccurred(Format &errorInfo) 212 { 213 (void)errorInfo; 214 } 215 }; 216 217 class ISoundPoolFrameWriteCallback { 218 public: 219 virtual ~ISoundPoolFrameWriteCallback() = default; 220 221 virtual void OnFirstAudioFrameWritingCallback(uint64_t &latency) = 0; 222 }; 223 224 class __attribute__((visibility("default"))) SoundPoolFactory { 225 public: 226 #ifdef UNSUPPORT_SOUND_POOL CreateSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)227 static std::shared_ptr<ISoundPool> CreateSoundPool(int maxStreams, 228 AudioStandard::AudioRendererInfo audioRenderInfo) 229 { 230 return nullptr; 231 } CreateParallelSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)232 static std::shared_ptr<ISoundPool> CreateParallelSoundPool(int maxStreams, 233 AudioStandard::AudioRendererInfo audioRenderInfo) 234 { 235 return nullptr; 236 } 237 #else 238 static std::shared_ptr<ISoundPool> CreateSoundPool(int maxStreams, 239 AudioStandard::AudioRendererInfo audioRenderInfo); 240 static std::shared_ptr<ISoundPool> CreateParallelSoundPool(int maxStreams, 241 AudioStandard::AudioRendererInfo audioRenderInfo); 242 #endif 243 private: 244 SoundPoolFactory() = default; 245 ~SoundPoolFactory() = default; 246 }; 247 248 class SoundPoolKeys { 249 public: 250 static constexpr std::string_view ERROR_CODE = "error_code"; 251 static constexpr std::string_view ERROR_MESSAGE = "error_message"; 252 static constexpr std::string_view ERROR_TYPE_FLAG = "error_type_flag"; 253 static constexpr std::string_view SOUND_ID = "sound_id"; 254 static constexpr std::string_view STREAM_ID = "stream_id"; 255 }; 256 257 enum ERROR_TYPE : int32_t { 258 LOAD_ERROR = 1, 259 PLAY_ERROR = 2 260 }; 261 262 class SoundPoolUtils { 263 public: 264 struct ErrorInfo { 265 int32_t errorCode = MSERR_INVALID_VAL; 266 int32_t soundId = 0; 267 int32_t streamId = 0; 268 ERROR_TYPE errorType = ERROR_TYPE::LOAD_ERROR; 269 std::shared_ptr<ISoundPoolCallback> callback = nullptr; 270 }; SendErrorInfo(const ErrorInfo & errorInfo)271 static void SendErrorInfo(const ErrorInfo& errorInfo) 272 { 273 Format format; 274 format.PutIntValue(SoundPoolKeys::ERROR_CODE, errorInfo.errorCode); 275 format.PutIntValue(SoundPoolKeys::ERROR_TYPE_FLAG, errorInfo.errorType); 276 format.PutIntValue(SoundPoolKeys::SOUND_ID, errorInfo.soundId); 277 if (errorInfo.streamId > 0) { 278 format.PutIntValue(SoundPoolKeys::STREAM_ID, errorInfo.streamId); 279 } 280 if (errorInfo.callback != nullptr) { 281 errorInfo.callback->OnErrorOccurred(format); 282 } 283 } 284 }; 285 } // namespace Media 286 } // namespace OHOS 287 #endif // ISOUNDPOOL_H 288