apply tizen coding rules
[platform/core/multimedia/libmm-player.git] / src / mm_player_audioeffect.c
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>,YeJin Cho <cho.yejin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <dlog.h>
22 #include <mm_error.h>
23
24 #include "mm_player_utils.h"
25 #include "mm_player_audioeffect.h"
26 #include "mm_player_ini.h"
27 #include "mm_player_priv.h"
28 #include <mm_sound.h>
29
30
31 int
32 mm_player_get_foreach_present_supported_effect_type(MMHandleType hplayer, MMAudioEffectType effect_type, mmplayer_supported_audio_effect_cb foreach_cb, void *user_data)
33 {
34         mm_player_t *player = NULL;
35         int result = MM_ERROR_NONE;
36         mm_sound_device_flags_e flags = MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG | MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG;
37         MMSoundDeviceList_t device_list;
38         MMSoundDevice_t device_h = NULL;
39         mm_sound_device_type_e device_type;
40         int i = 0;
41
42         MMPLAYER_FENTER();
43
44         MMPLAYER_RETURN_VAL_IF_FAIL(hplayer, MM_ERROR_PLAYER_NOT_INITIALIZED);
45
46         player = MM_PLAYER_CAST(hplayer);
47
48         /* get status if speaker is activated */
49         /* (1) get current device list */
50         result = mm_sound_get_current_device_list(flags, &device_list);
51
52         if (result) {
53                 LOGE("mm_sound_get_current_device_list() failed [%x]!!", result);
54                 MMPLAYER_FLEAVE();
55                 return result;
56         }
57
58         /* (2) get device handle of device list */
59         result = mm_sound_get_next_device(device_list, &device_h);
60
61         if (result) {
62                 LOGE("mm_sound_get_next_device() failed [%x]!!", result);
63                 MMPLAYER_FLEAVE();
64                 return result;
65         }
66
67         /* (3) get device type */
68         result = mm_sound_get_device_type(device_h, &device_type);
69
70         if (result) {
71                 LOGE("mm_sound_get_device_type() failed [%x]!!", result);
72                 MMPLAYER_FLEAVE();
73                 return result;
74         }
75
76         if (effect_type == MM_AUDIO_EFFECT_TYPE_PRESET) {
77                 /* preset */
78                 for (i = 0; i < MM_AUDIO_EFFECT_PRESET_NUM; i++) {
79                         if (player->ini.audio_effect_preset_list[i]) {
80                                 if (device_type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER &&
81                                         player->ini.audio_effect_preset_earphone_only_list[i])
82                                         continue;
83
84                                 if (!foreach_cb(effect_type, i, user_data))
85                                         goto CALLBACK_ERROR;
86                         }
87                 }
88         } else if (effect_type == MM_AUDIO_EFFECT_TYPE_CUSTOM) {
89                 /* custom */
90                 for (i = 0; i < MM_AUDIO_EFFECT_CUSTOM_NUM; i++) {
91                         if (player->ini.audio_effect_custom_list[i]) {
92                                 if (device_type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER &&
93                                         player->ini.audio_effect_custom_earphone_only_list[i]) {
94                                         continue;
95                                 }
96                                 if (!foreach_cb(effect_type, i, user_data))
97                                         goto CALLBACK_ERROR;
98                         }
99                 }
100         } else {
101                 LOGE("invalid effect type(%d)", effect_type);
102                 result = MM_ERROR_INVALID_ARGUMENT;
103         }
104
105         MMPLAYER_FLEAVE();
106
107         return result;
108
109 CALLBACK_ERROR:
110         LOGE("foreach callback returned error");
111         MMPLAYER_FLEAVE();
112         return MM_ERROR_PLAYER_INTERNAL;
113 }
114
115
116 int
117 __mmplayer_set_harmony_effect(mm_player_t *player, GstElement *audio_effect_element)
118 {
119         gint *ext_effect_level_list = NULL;
120         int count = 1;          /* start from 1, because of excepting eq index */
121         int ext_level_index = 0;
122         int result = MM_ERROR_NONE;
123
124         MMPLAYER_FENTER();
125
126         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
127         MMPLAYER_RETURN_VAL_IF_FAIL(audio_effect_element, MM_ERROR_INVALID_ARGUMENT);
128
129         /* Custom EQ */
130         if (player->ini.audio_effect_custom_eq_band_num) {
131                 LOGD("pass custom EQ level list to audio effect plugin");
132                 /* set custom-equalizer level list */
133                 g_object_set(audio_effect_element, "custom-eq", player->audio_effect_info.custom_eq_level, NULL);
134         } else
135                 LOGW("no custom EQ");
136
137         /* Custom Extension effects */
138         if (player->ini.audio_effect_custom_ext_num) {
139                 LOGD("pass custom extension level list to audio effect plugin");
140                 ext_effect_level_list = player->audio_effect_info.custom_ext_level_for_plugin;
141                 if (!ext_effect_level_list) {
142                         ext_effect_level_list = (gint*) malloc(sizeof(gint)*player->ini.audio_effect_custom_ext_num);
143                         if (!ext_effect_level_list) {
144                                 LOGE("memory allocation for extension effect list failed");
145                                 return MM_ERROR_OUT_OF_MEMORY;
146                         } else {
147                                 memset(ext_effect_level_list, 0, player->ini.audio_effect_custom_ext_num);
148
149                                 /* associate it to player handle */
150                                 player->audio_effect_info.custom_ext_level_for_plugin = ext_effect_level_list;
151                         }
152                 }
153
154                 while (count < MM_AUDIO_EFFECT_CUSTOM_NUM) {
155                         if (player->ini.audio_effect_custom_list[count]) {
156                                 ext_effect_level_list[ext_level_index] = player->audio_effect_info.custom_ext_level[count-1];
157                                 ext_level_index++;
158                                 if (ext_level_index == player->ini.audio_effect_custom_ext_num)
159                                         break;
160                         }
161                         count++;
162                 }
163
164                 /* set custom-extension effects level list */
165                 g_object_set(audio_effect_element, "custom-ext", ext_effect_level_list, NULL);
166         } else
167                 LOGW("no custom extension effect");
168
169         /* order action to audio effect plugin */
170         g_object_set(audio_effect_element, "filter-action", MM_AUDIO_EFFECT_TYPE_CUSTOM, NULL);
171         LOGD("filter-action = %d", MM_AUDIO_EFFECT_TYPE_CUSTOM);
172
173         MMPLAYER_FLEAVE();
174
175         return result;
176 }
177
178
179 gboolean
180 __mmplayer_is_earphone_only_effect_type(mm_player_t *player, MMAudioEffectType effect_type, int effect)
181 {
182         gboolean result = FALSE;
183         int i = 0;
184
185         MMPLAYER_FENTER();
186
187         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
188
189         if (effect_type == MM_AUDIO_EFFECT_TYPE_PRESET) {
190                 /* preset */
191                 if (player->ini.audio_effect_preset_earphone_only_list[effect]) {
192                         LOGI("this preset effect(%d) is only available with earphone", effect);
193                         result = TRUE;
194                 }
195         } else if (effect_type == MM_AUDIO_EFFECT_TYPE_CUSTOM) {
196                 /* custom */
197                 for (i = 1; i < MM_AUDIO_EFFECT_CUSTOM_NUM; i++) {
198                         /* it starts from 1(except testing for EQ) */
199                         if (player->ini.audio_effect_custom_earphone_only_list[i]) {
200                                 /* check if the earphone only custom effect was set */
201                                 if (player->audio_effect_info.custom_ext_level[i-1]) {
202                                         LOGI("this custom effect(%d) is only available with earphone", i);
203                                         result = TRUE;
204                                 }
205                         }
206                 }
207         } else
208                 LOGE("invalid effect type(%d)", effect_type);
209
210         MMPLAYER_FLEAVE();
211
212         return result;
213 }
214
215 int
216 __mmplayer_audio_set_output_type(mm_player_t *player, MMAudioEffectType effect_type, int effect)
217 {
218         GstElement *audio_effect_element = NULL;
219         mm_sound_device_flags_e flags = MM_SOUND_DEVICE_ALL_FLAG;
220         MMSoundDeviceList_t device_list;
221         MMSoundDevice_t device_h = NULL;
222         mm_sound_device_type_e device_type;
223         int output_type = 0;
224         int result = MM_ERROR_NONE;
225
226         MMPLAYER_FENTER();
227
228         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
229
230         audio_effect_element = player->pipeline->audiobin[MMPLAYER_A_FILTER].gst;
231
232         /* (1) get current device list */
233         result = mm_sound_get_current_device_list(flags, &device_list);
234
235         if (result) {
236                 LOGE("mm_sound_get_current_device_list() failed [%x]!!", result);
237                 MMPLAYER_FLEAVE();
238                 return result;
239         }
240
241         /* (2) get device handle of device list */
242         result = mm_sound_get_next_device(device_list, &device_h);
243
244         if (result) {
245                 LOGE("mm_sound_get_next_device() failed [%x]!!", result);
246                 MMPLAYER_FLEAVE();
247                 return result;
248         }
249
250         /* (3) get device type */
251         result = mm_sound_get_device_type(device_h, &device_type);
252
253         if (result) {
254                 LOGE("mm_sound_get_device_type() failed [%x]!!", result);
255                 MMPLAYER_FLEAVE();
256                 return result;
257         }
258
259         /* SPEAKER case */
260         if (device_type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER) {
261                 if (MM_AUDIO_EFFECT_TYPE_SQUARE != effect_type) {
262                         if (__mmplayer_is_earphone_only_effect_type(player, effect_type, effect)) {
263                                 LOGE("earphone is not equipped, this filter will not be applied");
264                                 MMPLAYER_FLEAVE();
265                                 return MM_ERROR_PLAYER_SOUND_EFFECT_INVALID_STATUS;
266                         }
267                 }
268                 output_type = MM_AUDIO_EFFECT_OUTPUT_SPK;
269         } else if (device_type == MM_SOUND_DEVICE_TYPE_MIRRORING) {
270                 output_type = MM_AUDIO_EFFECT_OUTPUT_OTHERS;
271         } else if (device_type == MM_SOUND_DEVICE_TYPE_HDMI) {
272                 output_type = MM_AUDIO_EFFECT_OUTPUT_HDMI;
273         } else if (device_type == MM_SOUND_DEVICE_TYPE_BLUETOOTH) {
274                 output_type = MM_AUDIO_EFFECT_OUTPUT_BT;
275         } else if (device_type == MM_SOUND_DEVICE_TYPE_USB_AUDIO) {
276                 output_type = MM_AUDIO_EFFECT_OUTPUT_USB_AUDIO;
277         } else {
278                 /* Other case, include WIRED_ACCESSORY */
279                 output_type = MM_AUDIO_EFFECT_OUTPUT_EAR;
280         }
281         LOGW("output_type = %d (0:spk,1:ear,2:others)", output_type);
282
283         /* set filter output mode */
284         g_object_set(audio_effect_element, "filter-output-mode", output_type, NULL);
285
286         return result;
287 }
288
289 gboolean
290 _mmplayer_is_supported_effect_type(mm_player_t* player, MMAudioEffectType effect_type, int effect)
291 {
292         gboolean result = TRUE;
293         mm_sound_device_flags_e flags = MM_SOUND_DEVICE_ALL_FLAG;
294         MMSoundDeviceList_t device_list;
295         MMSoundDevice_t device_h = NULL;
296         mm_sound_device_type_e device_type;
297         int ret = MM_ERROR_NONE;
298
299         MMPLAYER_FENTER();
300
301         /* get status if speaker is activated */
302         /* (1) get current device list */
303         ret = mm_sound_get_current_device_list(flags, &device_list);
304         if (ret) {
305                 MMPLAYER_FLEAVE();
306                 LOGE("mm_sound_get_current_device_list() failed [%x]!!", ret);
307                 return FALSE;
308         }
309
310         /* (2) get device handle of device list */
311         ret = mm_sound_get_next_device(device_list, &device_h);
312
313         if (ret) {
314                 LOGE("mm_sound_get_next_device() failed [%x]!!", ret);
315                 MMPLAYER_FLEAVE();
316                 return FALSE;
317         }
318
319         /* (3) get device type */
320         ret = mm_sound_get_device_type(device_h, &device_type);
321
322         if (ret) {
323                 LOGE("mm_sound_get_device_type() failed [%x]!!", ret);
324                 MMPLAYER_FLEAVE();
325                 return FALSE;
326         }
327
328         /* preset */
329         if (effect_type == MM_AUDIO_EFFECT_TYPE_PRESET) {
330                 if (effect < MM_AUDIO_EFFECT_PRESET_AUTO || effect >= MM_AUDIO_EFFECT_PRESET_NUM) {
331                         LOGE("out of range, preset effect(%d)", effect);
332                         result = FALSE;
333                 } else {
334                         if (!player->ini.audio_effect_preset_list[effect]) {
335                                 LOGE("this effect(%d) is not supported", effect);
336                                 result = FALSE;
337                         } else {
338                                 if (device_type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER &&
339                                                 player->ini.audio_effect_preset_earphone_only_list[effect]) {
340                                         result = FALSE;
341                                 }
342                         }
343                 }
344         } else if (effect_type == MM_AUDIO_EFFECT_TYPE_CUSTOM) {
345                 /* custom */
346                 if (effect < MM_AUDIO_EFFECT_CUSTOM_EQ || effect >= MM_AUDIO_EFFECT_CUSTOM_NUM) {
347                         LOGE("out of range, custom effect(%d)", effect);
348                         result = FALSE;
349                 } else {
350                         if (!player->ini.audio_effect_custom_list[effect]) {
351                                 LOGE("this custom effect(%d) is not supported", effect);
352                                 result = FALSE;
353                         } else {
354                                 if (device_type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER &&
355                                                 player->ini.audio_effect_custom_earphone_only_list[effect]) {
356                                         result = FALSE;
357                                 }
358                         }
359                 }
360         } else if (effect_type == MM_AUDIO_EFFECT_TYPE_SQUARE) {
361                 if (!player->ini.use_audio_effect_custom) {
362                         LOGE("Square effect is not supported");
363                         result = FALSE;
364                 }
365         } else {
366                 LOGE("invalid effect type(%d)", effect_type);
367                 result = FALSE;
368         }
369
370
371         MMPLAYER_FLEAVE();
372
373         return result;
374 }
375
376 int
377 _mmplayer_audio_effect_custom_apply(mm_player_t *player)
378 {
379         GstElement *audio_effect_element = NULL;
380         int result = MM_ERROR_NONE;
381
382         MMPLAYER_FENTER();
383
384         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
385
386         /* Music Player can set audio effect value before Audiobin is created. */
387         if (!player->pipeline || !player->pipeline->audiobin || !player->pipeline->audiobin[MMPLAYER_A_FILTER].gst) {
388                 LOGW("effect element is not created yet.");
389                 player->bypass_audio_effect = FALSE;
390
391                 /* store audio effect setting in order to apply it when audio effect plugin is created */
392                 player->audio_effect_info.effect_type = MM_AUDIO_EFFECT_TYPE_CUSTOM;
393         } else {
394                 audio_effect_element = player->pipeline->audiobin[MMPLAYER_A_FILTER].gst;
395
396                 /* get status if speaker is activated */
397                 result = __mmplayer_audio_set_output_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, 0);
398
399                 if (result != MM_ERROR_NONE) {
400                         LOGE("failed to set output mode");
401                         MMPLAYER_FLEAVE();
402                         return result;
403                 }
404
405                 result = __mmplayer_set_harmony_effect(player, audio_effect_element);
406                 if (result) {
407                         LOGE("_set_harmony_effect() failed(%x)", result);
408                         MMPLAYER_FLEAVE();
409                         return result;
410                 }
411         }
412         player->set_mode.rich_audio = TRUE;
413
414         MMPLAYER_FLEAVE();
415
416         return result;
417 }
418
419 int
420 _mmplayer_audio_effect_custom_update_level(mm_player_t *player)
421 {
422         GstElement *audio_effect_element = NULL;
423         int result = MM_ERROR_NONE;
424         int ext_level_index = 0;
425         int count = 1;
426         int i = 0;
427         gint *custom_eq = NULL;
428         gint *custom_ext_effect_level_list = NULL;
429
430         MMPLAYER_FENTER();
431
432         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
433
434         audio_effect_element = player->pipeline->audiobin[MMPLAYER_A_FILTER].gst;
435
436         /* get custom eq effect */
437         g_object_get(audio_effect_element, "custom-eq", &custom_eq, NULL);
438
439         memcpy(player->audio_effect_info.custom_eq_level, custom_eq, sizeof(gint) * player->ini.audio_effect_custom_eq_band_num);
440
441         for (i = 0; i < player->ini.audio_effect_custom_eq_band_num; i++)
442                 LOGD("updated custom-eq [%d] = %d", i, player->audio_effect_info.custom_eq_level[i]);
443
444         /* get custom ext effect */
445
446         g_object_get(audio_effect_element, "custom-ext", &custom_ext_effect_level_list, NULL);
447
448         while (count < MM_AUDIO_EFFECT_CUSTOM_NUM) {
449                 if (player->ini.audio_effect_custom_list[count]) {
450                         player->audio_effect_info.custom_ext_level[count-1]
451                                 = custom_ext_effect_level_list[ext_level_index];
452                         LOGD("updated custom-ext [%d] = %d", count, player->audio_effect_info.custom_ext_level[count-1]);
453                         ext_level_index++;
454                 }
455                 count++;
456         }
457
458
459         MMPLAYER_FLEAVE();
460         return result;
461 }
462
463
464 int
465 mm_player_audio_effect_custom_clear_eq_all(MMHandleType hplayer)
466 {
467         int result = MM_ERROR_NONE;
468         mm_player_t* player = (mm_player_t*)hplayer;
469
470         MMPLAYER_FENTER();
471
472         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
473
474         /* clear EQ custom effect */
475         memset(player->audio_effect_info.custom_eq_level, MM_AUDIO_EFFECT_CUSTOM_LEVEL_INIT, sizeof(int)*MM_AUDIO_EFFECT_EQ_BAND_NUM_MAX);
476
477         LOGI("All the EQ bands clearing success");
478
479         MMPLAYER_FLEAVE();
480
481         return result;
482 }
483
484
485 int
486 mm_player_audio_effect_custom_clear_ext_all(MMHandleType hplayer)
487 {
488         int i;
489         int result = MM_ERROR_NONE;
490         mm_player_t* player = (mm_player_t*)hplayer;
491
492         MMPLAYER_FENTER();
493
494         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
495
496         /* clear ALL custom effects, except EQ */
497         for (i = 0 ; i < MM_AUDIO_EFFECT_CUSTOM_NUM - 1 ; i++)
498                 player->audio_effect_info.custom_ext_level[i] = MM_AUDIO_EFFECT_CUSTOM_LEVEL_INIT;
499
500         LOGI("All the extension effects clearing success");
501
502         MMPLAYER_FLEAVE();
503
504         return result;
505 }
506
507
508 int
509 mm_player_is_supported_preset_effect_type(MMHandleType hplayer, MMAudioEffectPresetType effect)
510 {
511         mm_player_t* player = (mm_player_t*)hplayer;
512         int result = MM_ERROR_NONE;
513
514         MMPLAYER_FENTER();
515
516         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
517
518         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_PRESET, effect))
519                 result = MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
520
521         MMPLAYER_FLEAVE();
522
523         return result;
524 }
525
526
527 int
528 mm_player_is_supported_custom_effect_type(MMHandleType hplayer, MMAudioEffectCustomType effect)
529 {
530         mm_player_t* player = (mm_player_t*)hplayer;
531         int result = MM_ERROR_NONE;
532
533         MMPLAYER_FENTER();
534
535         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
536
537         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, effect))
538                 result = MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
539
540         MMPLAYER_FLEAVE();
541
542         return result;
543 }
544
545 int
546 mm_player_audio_effect_custom_apply(MMHandleType hplayer)
547 {
548         mm_player_t* player = (mm_player_t*)hplayer;
549         int result = MM_ERROR_NONE;
550
551         MMPLAYER_FENTER();
552
553         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
554
555         if (!player->ini.use_audio_effect_custom) {
556                 LOGE("audio effect(custom) is not supported");
557                 MMPLAYER_FLEAVE();
558                 return MM_ERROR_NOT_SUPPORT_API;
559         }
560
561         result = _mmplayer_audio_effect_custom_apply(player);
562
563         MMPLAYER_FLEAVE();
564
565         return result;
566 }
567
568
569 int
570 mm_player_audio_effect_bypass(MMHandleType hplayer)
571 {
572         mm_player_t* player = (mm_player_t*)hplayer;
573         int result = MM_ERROR_NONE;
574         GstElement *audio_effect_element = NULL;
575
576         MMPLAYER_FENTER();
577
578         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
579
580         if (!player->ini.use_audio_effect_preset && !player->ini.use_audio_effect_custom) {
581                 LOGE("audio effect(preset/custom) is not supported");
582                 MMPLAYER_FLEAVE();
583                 return MM_ERROR_NOT_SUPPORT_API;
584         }
585         if (!player->pipeline || !player->pipeline->audiobin || !player->pipeline->audiobin[MMPLAYER_A_FILTER].gst) {
586                 LOGW("effect element is not created yet.");
587         } else {
588                 audio_effect_element = player->pipeline->audiobin[MMPLAYER_A_FILTER].gst;
589
590                 /* order action to audio effect plugin */
591                 g_object_set(audio_effect_element, "filter-action", MM_AUDIO_EFFECT_TYPE_NONE, NULL);
592                 LOGD("filter-action = %d", MM_AUDIO_EFFECT_TYPE_NONE);
593         }
594
595         MMPLAYER_FLEAVE();
596
597         return result;
598 }
599
600
601 int
602 _mmplayer_audio_effect_custom_set_level_ext(mm_player_t *player, MMAudioEffectCustomType custom_effect_type, int level)
603 {
604         int effect_level_max = 0;
605         int effect_level_min = 0;
606         int count = 1;                  /* start from 1, because of excepting eq index */
607         int ext_level_index = 1;        /* start from 1, because of excepting eq index */
608         int result = MM_ERROR_NONE;
609
610         MMPLAYER_FENTER();
611
612         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
613
614         /* check if EQ is supported */
615         if (!_mmplayer_is_supported_effect_type(player,  MM_AUDIO_EFFECT_TYPE_CUSTOM, custom_effect_type)) {
616                 MMPLAYER_FLEAVE();
617                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
618         }
619
620         while (count < MM_AUDIO_EFFECT_CUSTOM_NUM) {
621                 if (player->ini.audio_effect_custom_list[count]) {
622                         if (count == custom_effect_type) {
623                                 effect_level_min = player->ini.audio_effect_custom_min_level_list[ext_level_index];
624                                 effect_level_max = player->ini.audio_effect_custom_max_level_list[ext_level_index];
625                                 LOGI("level min value(%d), level max value(%d)", effect_level_min, effect_level_max);
626                                 break;
627                         }
628                         ext_level_index++;
629                         if (ext_level_index == player->ini.audio_effect_custom_ext_num + 1) {
630                                 LOGE("could not find min, max value. maybe effect information in ini file is not proper for audio effect plugin");
631                                 break;
632                         }
633                 }
634                 count++;
635         }
636
637         if (level < effect_level_min || level > effect_level_max) {
638                 LOGE("out of range, level(%d)", level);
639                 result = MM_ERROR_INVALID_ARGUMENT;
640         } else {
641                 player->audio_effect_info.custom_ext_level[custom_effect_type-1] = level;
642                 LOGI("set ext[%d] = %d", custom_effect_type-1, level);
643         }
644
645         MMPLAYER_FLEAVE();
646
647         return result;
648 }
649
650
651 int
652 _mmplayer_audio_effect_custom_set_level_eq(mm_player_t *player, int index, int level)
653 {
654         gint eq_level_max = 0;
655         gint eq_level_min = 0;
656         int result = MM_ERROR_NONE;
657
658         MMPLAYER_FENTER();
659
660         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
661
662         /* check if EQ is supported */
663         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, MM_AUDIO_EFFECT_CUSTOM_EQ)) {
664                 MMPLAYER_FLEAVE();
665                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
666         }
667
668         if (index < 0 || index > player->ini.audio_effect_custom_eq_band_num - 1) {
669                 LOGE("out of range, index(%d)", index);
670                 result = MM_ERROR_INVALID_ARGUMENT;
671         } else {
672                 eq_level_min = player->ini.audio_effect_custom_min_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
673                 eq_level_max = player->ini.audio_effect_custom_max_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
674                 LOGI("EQ level min value(%d), EQ level max value(%d)", eq_level_min, eq_level_max);
675
676                 if (level < eq_level_min || level > eq_level_max) {
677                         LOGE("out of range, EQ level(%d)", level);
678                         result =  MM_ERROR_INVALID_ARGUMENT;
679                 } else {
680                         player->audio_effect_info.custom_eq_level[index] = level;
681                         LOGI("set EQ[%d] = %d", index, level);
682                 }
683         }
684
685         MMPLAYER_FLEAVE();
686
687         return result;
688 }
689
690
691 /* NOTE : parameter eq_index is only used for _set_eq_level() */
692 int
693 mm_player_audio_effect_custom_set_level(MMHandleType hplayer, MMAudioEffectCustomType effect_custom_type, int eq_index, int level)
694 {
695         mm_player_t* player = (mm_player_t*)hplayer;
696         int result = MM_ERROR_NONE;
697
698         MMPLAYER_FENTER();
699
700         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
701
702         /* check if this effect type is supported */
703         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, effect_custom_type)) {
704                 result = MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
705         } else {
706                 if (effect_custom_type == MM_AUDIO_EFFECT_CUSTOM_EQ) {
707                         result = _mmplayer_audio_effect_custom_set_level_eq(player, eq_index, level);
708                 } else if (effect_custom_type > MM_AUDIO_EFFECT_CUSTOM_EQ && effect_custom_type < MM_AUDIO_EFFECT_CUSTOM_NUM) {
709                         result = _mmplayer_audio_effect_custom_set_level_ext(player, effect_custom_type, level);
710                 } else {
711                         LOGE("out of range, effect type(%d)", effect_custom_type);
712                         result = MM_ERROR_INVALID_ARGUMENT;
713                 }
714         }
715
716         MMPLAYER_FLEAVE();
717
718         return result;
719 }
720
721
722 int
723 mm_player_audio_effect_custom_get_eq_bands_number(MMHandleType hplayer, int *bands)
724 {
725         mm_player_t* player = (mm_player_t*)hplayer;
726         int result = MM_ERROR_NONE;
727
728         MMPLAYER_FENTER();
729
730         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
731
732         /* check if EQ is supported */
733         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, MM_AUDIO_EFFECT_CUSTOM_EQ)) {
734                 MMPLAYER_FLEAVE();
735                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
736         }
737
738         *bands = player->ini.audio_effect_custom_eq_band_num;
739         LOGD("number of custom EQ band = %d", *bands);
740
741         MMPLAYER_FLEAVE();
742
743         return result;
744 }
745
746
747 int
748 mm_player_audio_effect_custom_get_eq_bands_width(MMHandleType hplayer, int band_idx, int *width)
749 {
750         mm_player_t* player = (mm_player_t*)hplayer;
751         int result = MM_ERROR_NONE;
752         unsigned int eq_num = 0;
753
754         MMPLAYER_FENTER();
755
756         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
757
758         /* check if EQ is supported */
759         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, MM_AUDIO_EFFECT_CUSTOM_EQ)) {
760                 MMPLAYER_FLEAVE();
761                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
762         }
763
764         eq_num = player->ini.audio_effect_custom_eq_band_num;
765         if (band_idx < 0 || band_idx > eq_num-1) {
766                 LOGE("out of range, invalid band_idx(%d)", band_idx);
767                 result = MM_ERROR_INVALID_ARGUMENT;
768         } else {
769                 /* set the width of EQ band */
770                 *width = player->ini.audio_effect_custom_eq_band_width[band_idx];
771                 LOGD("width of band_idx(%d) = %dHz", band_idx, *width);
772         }
773
774         MMPLAYER_FLEAVE();
775
776         return result;
777 }
778
779
780 int
781 mm_player_audio_effect_custom_get_eq_bands_freq(MMHandleType hplayer, int band_idx, int *freq)
782 {
783         mm_player_t* player = (mm_player_t*)hplayer;
784         int result = MM_ERROR_NONE;
785         unsigned int eq_num = 0;
786
787         MMPLAYER_FENTER();
788
789         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
790
791         /* check if EQ is supported */
792         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, MM_AUDIO_EFFECT_CUSTOM_EQ)) {
793                 MMPLAYER_FLEAVE();
794                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
795         }
796
797         eq_num = player->ini.audio_effect_custom_eq_band_num;
798         if (band_idx < 0 || band_idx > eq_num-1) {
799                 LOGE("out of range, invalid band_idx(%d)", band_idx);
800                 result = MM_ERROR_INVALID_ARGUMENT;
801         } else {
802                 /* set the frequency of EQ band */
803                 *freq = player->ini.audio_effect_custom_eq_band_freq[band_idx];
804                 LOGD("frequency of band_idx(%d) = %dHz", band_idx, *freq);
805         }
806
807         MMPLAYER_FLEAVE();
808
809         return result;
810 }
811
812
813 int
814 mm_player_audio_effect_custom_get_level(MMHandleType hplayer, MMAudioEffectCustomType type, int eq_index, int *level)
815 {
816         mm_player_t* player = (mm_player_t*)hplayer;
817         int result = MM_ERROR_NONE;
818
819         MMPLAYER_FENTER();
820
821         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
822         MMPLAYER_RETURN_VAL_IF_FAIL(level, MM_ERROR_PLAYER_NOT_INITIALIZED);
823
824         /* check if this effect type is supported */
825         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, type)) {
826                 MMPLAYER_FLEAVE();
827                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
828         }
829
830         if (type == MM_AUDIO_EFFECT_CUSTOM_EQ) {
831                 if (eq_index < 0 || eq_index > player->ini.audio_effect_custom_eq_band_num - 1) {
832                         LOGE("out of range, EQ index(%d)", eq_index);
833                         result = MM_ERROR_INVALID_ARGUMENT;
834                 } else {
835                         *level = player->audio_effect_info.custom_eq_level[eq_index];
836                         LOGD("EQ index = %d, level = %d", eq_index, *level);
837                 }
838         } else if (type > MM_AUDIO_EFFECT_CUSTOM_EQ && type < MM_AUDIO_EFFECT_CUSTOM_NUM) {
839                 *level = player->audio_effect_info.custom_ext_level[type-1];
840                 LOGD("extension effect index = %d, level = %d", type, *level);
841         } else {
842                 LOGE("out of range, type(%d)", type);
843                 result = MM_ERROR_INVALID_ARGUMENT;
844         }
845
846         MMPLAYER_FLEAVE();
847
848         return result;
849 }
850
851
852 int
853 mm_player_audio_effect_custom_get_level_range(MMHandleType hplayer, MMAudioEffectCustomType type, int *min, int *max)
854 {
855         mm_player_t* player = (mm_player_t*)hplayer;
856         int result = MM_ERROR_NONE;
857         int count = 1;                  /* start from 1, because of excepting eq index */
858         int ext_level_index = 1;        /* start from 1, because of excepting eq index */
859
860         MMPLAYER_FENTER();
861
862         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
863         MMPLAYER_RETURN_VAL_IF_FAIL(min, MM_ERROR_PLAYER_NOT_INITIALIZED);
864         MMPLAYER_RETURN_VAL_IF_FAIL(max, MM_ERROR_PLAYER_NOT_INITIALIZED);
865
866         /* check if this effect type is supported */
867         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, type)) {
868                 MMPLAYER_FLEAVE();
869                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
870         }
871
872         if (type == MM_AUDIO_EFFECT_CUSTOM_EQ) {
873                 *min = player->ini.audio_effect_custom_min_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
874                 *max = player->ini.audio_effect_custom_max_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
875                 LOGD("EQ min level = %d, max level = %d", *min, *max);
876         } else {
877                 while (count < MM_AUDIO_EFFECT_CUSTOM_NUM) {
878                         if (player->ini.audio_effect_custom_list[count]) {
879                                 if (count == type) {
880                                         *min = player->ini.audio_effect_custom_min_level_list[ext_level_index];
881                                         *max = player->ini.audio_effect_custom_max_level_list[ext_level_index];
882                                         LOGI("Extension effect(%d) min level = %d, max level = %d", count, *min, *max);
883                                         break;
884                                 }
885                                 ext_level_index++;
886                                 if (ext_level_index == player->ini.audio_effect_custom_ext_num + 1) {
887                                         LOGE("could not find min, max value. maybe effect information in ini file is not proper for audio effect plugin");
888                                         break;
889                                 }
890                         }
891                         count++;
892                 }
893         }
894
895         MMPLAYER_FLEAVE();
896
897         return result;
898 }
899
900
901 int
902 mm_player_audio_effect_custom_set_level_eq_from_list(MMHandleType hplayer, int *level_list, int size)
903 {
904         mm_player_t *player = (mm_player_t*)hplayer;
905         gint i = 0;
906         gint eq_level_min = 0;
907         gint eq_level_max = 0;
908         int result = MM_ERROR_NONE;
909
910         MMPLAYER_FENTER();
911
912         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
913
914         /* check if EQ is supported */
915         if (!_mmplayer_is_supported_effect_type(player, MM_AUDIO_EFFECT_TYPE_CUSTOM, MM_AUDIO_EFFECT_CUSTOM_EQ)) {
916                 MMPLAYER_FLEAVE();
917                 return MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER;
918         }
919
920         if (size != player->ini.audio_effect_custom_eq_band_num) {
921                 LOGE("input size variable(%d) does not match with number of eq band(%d)", size, player->ini.audio_effect_custom_eq_band_num);
922                 result = MM_ERROR_INVALID_ARGUMENT;
923         } else {
924                 eq_level_min = player->ini.audio_effect_custom_min_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
925                 eq_level_max = player->ini.audio_effect_custom_max_level_list[MM_AUDIO_EFFECT_CUSTOM_EQ];
926
927                 for (i = 0 ; i < size ; i++) {
928                         if (level_list[i] < eq_level_min || level_list[i] > eq_level_max) {
929                                 LOGE("out of range, level[%d]=%d", i, level_list[i]);
930                                 result = MM_ERROR_INVALID_ARGUMENT;
931                                 break;
932                         }
933                         player->audio_effect_info.custom_eq_level[i] = level_list[i];
934                 }
935         }
936         MMPLAYER_FLEAVE();
937
938         return result;
939 }