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 22 namespace OHOS { 23 namespace Media { 24 struct PlayParams { 25 int32_t loop = 0; 26 int32_t rate = 0; // default AudioRendererRate::RENDER_RATE_NORMAL 27 float leftVolume = (float)1.0; 28 float rightVolume = (float)1.0; 29 int32_t priority = 0; 30 bool parallelPlayFlag = false; 31 std::string cacheDir; 32 }; 33 34 class ISoundPoolCallback; 35 class ISoundPoolFrameWriteCallback; 36 37 class ISoundPool { 38 public: 39 virtual ~ISoundPool() = default; 40 41 /** 42 * @brief Load the sound from the specified path. 43 * 44 * @param url The path to the audio file 45 * @return Returns a sound ID. This value can be used to play or unload the sound. 46 * @since 1.0 47 * @version 1.0 48 */ 49 virtual int32_t Load(std::string url) = 0; 50 51 /** 52 * @brief Load the sound from a FileDescriptor.. 53 * 54 * @param fd A FileDescriptor object 55 * @param offset Offset to the start of the sound 56 * @param length Length of the sound 57 * @return Returns a sound ID. This value can be used to play or unload the sound. 58 * @since 1.0 59 * @version 1.0 60 */ 61 virtual int32_t Load(int32_t fd, int64_t offset, int64_t length) = 0; 62 63 /** 64 * @brief Play a sound from a sound ID. 65 * 66 * @param soundID Returned by the load() 67 * @param playParameters params Player parameters 68 * @return Returns a non-zero streamID if successful, zero if it fails. 69 * @since 1.0 70 * @version 1.0 71 */ 72 virtual int32_t Play(int32_t soundID, PlayParams playParameters) = 0; 73 74 /** 75 * @brief Stop a stream which is playing. 76 * 77 * @param streamID Returned by the play() 78 * @return Returns used to return the result. MSERR_OK if success 79 * @since 1.0 80 * @version 1.0 81 */ 82 virtual int32_t Stop(int32_t streamID) = 0; 83 84 /** 85 * @brief Set loop mode. 86 * 87 * @param streamID Returned by the play() 88 * @param loop Loop mode (0 = no loop, -1 = loop forever) 89 * @return Returns used to return the result. MSERR_OK if success 90 * @since 1.0 91 * @version 1.0 92 */ 93 virtual int32_t SetLoop(int32_t streamID, int32_t loop) = 0; 94 95 /** 96 * @brief Set stream priority. 97 * 98 * @param streamID Returned by the play() 99 * @param priority Stream priority (0 = lowest priority) 100 * @return Returns used to return the result. MSERR_OK if success 101 * @since 1.0 102 * @version 1.0 103 */ 104 virtual int32_t SetPriority(int32_t streamID, int32_t priority) = 0; 105 106 /** 107 * @brief Set playback rate. 108 * 109 * @param streamID Returned by the play() 110 * @param renderRate Playback rate 111 * @return Returns used to return the result. MSERR_OK if success 112 * @since 1.0 113 * @version 1.0 114 */ 115 virtual int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate) = 0; 116 117 /** 118 * @brief Set stream volume. 119 * 120 * @param streamID Returned by the play() 121 * @param leftVolume leftVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume 122 * @param rigthVolume rightVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume 123 * @return Returns used to return the result. MSERR_OK if success 124 * @since 1.0 125 * @version 1.0 126 */ 127 virtual int32_t SetVolume(int32_t streamID, float leftVolume, float rigthVolume) = 0; 128 129 /** 130 * @brief Unload a sound from a sound ID. 131 * 132 * @param soundID Returned by the load() 133 * @return Returns used to return the result. MSERR_OK if success 134 * @since 1.0 135 * @version 1.0 136 */ 137 virtual int32_t Unload(int32_t soundID) = 0; 138 139 /** 140 * @brief Releases the soundPool. This method uses an asynchronous callback to return the result. 141 * 142 * @return Returns used to return the result. MSERR_OK if success 143 * @since 1.0 144 * @version 1.0 145 */ 146 virtual int32_t Release() = 0; 147 148 /** 149 * @brief Register listens for soundpool 150 * 151 * @param soundPoolCallback The listen class for soundpool 152 * @return Returns used to return the result. MSERR_OK if success 153 * @since 1.0 154 * @version 1.0 155 */ 156 virtual int32_t SetSoundPoolCallback 157 (const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback) = 0; 158 159 /** 160 * @brief Register frame write listens for soundpool 161 * 162 * @param frameWriteCallback The frame writ listen class for soundpool 163 * @return Returns used to return the result. MSERR_OK if success 164 * @since 1.0 165 * @version 1.0 166 */ 167 virtual int32_t SetSoundPoolFrameWriteCallback 168 (const std::shared_ptr<ISoundPoolFrameWriteCallback> &frameWriteCallback) = 0; 169 }; 170 171 class ISoundPoolCallback { 172 public: 173 virtual ~ISoundPoolCallback() = default; 174 175 /** 176 * @brief Register listens for load result event. 177 * 178 * @param result used to listen for loaded soundId event 179 * @since 1.0 180 * @version 1.0 181 */ 182 virtual void OnLoadCompleted(int32_t soundId) = 0; 183 184 /** 185 * @brief Register the play finish event to listen for. 186 * 187 * @since 1.0 188 * @version 1.0 189 */ 190 virtual void OnPlayFinished() = 0; 191 192 /** 193 * @brief Register listens for sound play error events. 194 * 195 * @param errorCode Type of the sound play error event to listen for. 196 * @since 1.0 197 * @version 1.0 198 */ 199 virtual void OnError(int32_t errorCode) = 0; 200 }; 201 202 class ISoundPoolFrameWriteCallback { 203 public: 204 virtual ~ISoundPoolFrameWriteCallback() = default; 205 206 virtual void OnFirstAudioFrameWritingCallback(uint64_t &latency) = 0; 207 }; 208 209 class __attribute__((visibility("default"))) SoundPoolFactory { 210 public: 211 #ifdef UNSUPPORT_SOUND_POOL CreateSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)212 static std::shared_ptr<ISoundPool> CreateSoundPool(int maxStreams, 213 AudioStandard::AudioRendererInfo audioRenderInfo) 214 { 215 return nullptr; 216 } 217 #else 218 static std::shared_ptr<ISoundPool> CreateSoundPool(int maxStreams, 219 AudioStandard::AudioRendererInfo audioRenderInfo); 220 #endif 221 private: 222 SoundPoolFactory() = default; 223 ~SoundPoolFactory() = default; 224 }; 225 } // namespace Media 226 } // namespace OHOS 227 #endif // ISOUNDPOOL_H 228