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