1 /*
2  * Copyright (c) 2020-2021 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 AUDIO_MODULE_H
17 #define AUDIO_MODULE_H
18 
19 #include "audio_player.h"
20 
21 namespace OHOS {
22 namespace ACELite {
23 class AudioModule final : public MemoryHeap {
24 public:
25     /**
26      * constructor
27      */
28     AudioModule() = default;
29 
30     /**
31      * desconstructor
32      */
33     ~AudioModule() = default;
34 
35     /**
36      * Set volume, 0.0 means the no silcence, 1.0 means the original volume.
37      *
38      * @param volume Indicates the target volume to set, which ranges from 0.0 to 1.0
39      * @return Returns {@code true} if setting is successful; returns {@code false} otherwise.
40      */
41     static bool SetVolume(float volume);
42 
43     /**
44      * Call when the app is terminated
45      */
46     static void OnTerminate();
47 
48     /**
49      * define property
50      */
51     static void
52         DefineProperty(JSIValue target, const char *propName, JSIFunctionHandler getter, JSIFunctionHandler setter);
53 
54     /**
55      * the play method of audio
56      *
57      * @param thisVal Indicates function execute context
58      * @param args Indicates the function arguments
59      * @param argsSize Indicates the size of function arguments
60      */
61     static JSIValue Play(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
62 
63     /**
64      * the pause method of audio
65      *
66      * @param thisVal Indicates function execute context
67      * @param args Indicates the function arguments
68      * @param argsSize Indicates the size of function arguments
69      */
70     static JSIValue Pause(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
71 
72     /**
73      * the stop method of audio
74      *
75      * @param thisVal Indicates function execute context
76      * @param args Indicates the function arguments
77      * @param argsSize Indicates the size of function arguments
78      */
79     static JSIValue Stop(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
80 
81     /**
82      * the getPlayState method of audio
83      *
84      * @param thisVal Indicates function execute context
85      * @param args Indicates the function arguments
86      * @param argsSize Indicates the size of function arguments
87      */
88     static JSIValue GetPlayState(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
89 
90     /**
91      * the getter of audio src property
92      *
93      * @param thisVal Indicates function execute context
94      * @param args Indicates the function arguments
95      * @param argsSize Indicates the size of function arguments
96      */
97     static JSIValue SrcGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
98 
99     /**
100      * the setter of audio src property
101      *
102      * @param thisVal Indicates function execute context
103      * @param args Indicates the function arguments
104      * @param argsSize Indicates the size of function arguments
105      */
106     static JSIValue SrcSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
107 
108     /**
109      * the getter of audio currentTime property
110      *
111      * @param thisVal Indicates function execute context
112      * @param args Indicates the function arguments
113      * @param argsSize Indicates the size of function arguments
114      */
115     static JSIValue CurrentTimeGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
116 
117     /**
118      * the setter of audio currentTime property
119      *
120      * @param thisVal Indicates function execute context
121      * @param args Indicates the function arguments
122      * @param argsSize Indicates the size of function arguments
123      */
124     static JSIValue CurrentTimeSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
125 
126     /**
127      * the getter of audio duration property
128      *
129      * @param thisVal Indicates function execute context
130      * @param args Indicates the function arguments
131      * @param argsSize Indicates the size of function arguments
132      */
133     static JSIValue DurationGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
134 
135     /**
136      * the getter of audio duration property
137      *
138      * @param thisVal Indicates function execute context
139      * @param args Indicates the function arguments
140      * @param argsSize Indicates the size of function arguments
141      */
142     static JSIValue DurationSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
143 
144     /**
145      * the getter of audio autoplay property
146      *
147      * @param thisVal Indicates function execute context
148      * @param args Indicates the function arguments
149      * @param argsSize Indicates the size of function arguments
150      */
151     static JSIValue AutoPlayGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
152 
153     /**
154      * the setter of audio autoplay property
155      *
156      * @param thisVal Indicates function execute context
157      * @param args Indicates the function arguments
158      * @param argsSize Indicates the size of function arguments
159      */
160     static JSIValue AutoPlaySetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
161 
162     /**
163      * the getter of audio loop property
164      *
165      * @param thisVal Indicates function execute context
166      * @param args Indicates the function arguments
167      * @param argsSize Indicates the size of function arguments
168      */
169     static JSIValue LoopGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
170 
171     /**
172      * the setter of audio loop property
173      *
174      * @param thisVal Indicates function execute context
175      * @param args Indicates the function arguments
176      * @param argsSize Indicates the size of function arguments
177      */
178     static JSIValue LoopSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
179 
180     /**
181      * the getter of audio volume property
182      *
183      * @param thisVal Indicates function execute context
184      * @param args Indicates the function arguments
185      * @param argsSize Indicates the size of function arguments
186      */
187     static JSIValue VolumeGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
188 
189     /**
190      * the setter of audio volume property
191      *
192      * @param thisVal Indicates function execute context
193      * @param args Indicates the function arguments
194      * @param argsSize Indicates the size of function arguments
195      */
196     static JSIValue VolumeSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
197 
198     /**
199      * the getter of audio muted property
200      *
201      * @param thisVal Indicates function execute context
202      * @param args Indicates the function arguments
203      * @param argsSize Indicates the size of function arguments
204      */
205     static JSIValue MutedGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
206 
207     /**
208      * the setter of audio muted property
209      *
210      * @param thisVal Indicates function execute context
211      * @param args Indicates the function arguments
212      * @param argsSize Indicates the size of function arguments
213      */
214     static JSIValue MutedSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
215 
216     /**
217      * the getter of audio onplay event
218      *
219      * @param thisVal Indicates function execute context
220      * @param args Indicates the function arguments
221      * @param argsSize Indicates the size of function arguments
222      */
223     static JSIValue OnPlayGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
224 
225     /**
226      * the setter of audio onplay event
227      *
228      * @param thisVal Indicates function execute context
229      * @param args Indicates the function arguments
230      * @param argsSize Indicates the size of function arguments
231      */
232     static JSIValue OnPlaySetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
233 
234     /**
235      * the getter of audio onpause event
236      *
237      * @param thisVal Indicates function execute context
238      * @param args Indicates the function arguments
239      * @param argsSize Indicates the size of function arguments
240      */
241     static JSIValue OnPauseGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
242 
243     /**
244      * the setter of audio onpause event
245      *
246      * @param thisVal Indicates function execute context
247      * @param args Indicates the function arguments
248      * @param argsSize Indicates the size of function arguments
249      */
250     static JSIValue OnPauseSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
251 
252     /**
253      * the getter of audio onstop event
254      *
255      * @param thisVal Indicates function execute context
256      * @param args Indicates the function arguments
257      * @param argsSize Indicates the size of function arguments
258      */
259     static JSIValue OnStopGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
260 
261     /**
262      * the setter of audio onstop event
263      *
264      * @param thisVal Indicates function execute context
265      * @param args Indicates the function arguments
266      * @param argsSize Indicates the size of function arguments
267      */
268     static JSIValue OnStopSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
269 
270     /**
271      * the getter of audio onloadeddata event
272      *
273      * @param thisVal Indicates function execute context
274      * @param args Indicates the function arguments
275      * @param argsSize Indicates the size of function arguments
276      */
277     static JSIValue OnLoadedDataGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
278 
279     /**
280      * the setter of audio onloadeddata event
281      *
282      * @param thisVal Indicates function execute context
283      * @param args Indicates the function arguments
284      * @param argsSize Indicates the size of function arguments
285      */
286     static JSIValue OnLoadedDataSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
287 
288     /**
289      * the getter of audio onerror event
290      *
291      * @param thisVal Indicates function execute context
292      * @param args Indicates the function arguments
293      * @param argsSize Indicates the size of function arguments
294      */
295     static JSIValue OnEndedGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
296 
297     /**
298      * the setter of audio onerror event
299      *
300      * @param thisVal Indicates function execute context
301      * @param args Indicates the function arguments
302      * @param argsSize Indicates the size of function arguments
303      */
304     static JSIValue OnEndedSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
305 
306     /**
307      * the getter of audio onerror event
308      *
309      * @param thisVal Indicates function execute context
310      * @param args Indicates the function arguments
311      * @param argsSize Indicates the size of function arguments
312      */
313     static JSIValue OnErrorGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
314 
315     /**
316      * the setter of audio onerror event
317      *
318      * @param thisVal Indicates function execute context
319      * @param args Indicates the function arguments
320      * @param argsSize Indicates the size of function arguments
321      */
322     static JSIValue OnErrorSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
323 
324     /**
325      * the getter of audio ontimeupdate event
326      *
327      * @param thisVal Indicates function execute context
328      * @param args Indicates the function arguments
329      * @param argsSize Indicates the size of function arguments
330      */
331     static JSIValue OnTimeUpdateGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
332 
333     /**
334      * the setter of audio ontimeupdate event
335      *
336      * @param thisVal Indicates function execute context
337      * @param args Indicates the function arguments
338      * @param argsSize Indicates the size of function arguments
339      */
340     static JSIValue OnTimeUpdateSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize);
341 
342 private:
343     static JSIValue GetEventCallback(const AudioEventListener *listener);
344 
345     static AudioEventListener *CreateAudioEventListener(const JSIValue *args, uint8_t argsSize);
346 };
347 
348 void InitAudioModule(JSIValue exports);
349 } // namespace ACELite
350 } // namespace OHOS
351 #endif // AUDIO_MODULE_H