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