4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <mm_source.h>
33 #include "include/mm_sound_mgr_common.h"
34 #include "include/mm_sound_mgr_codec.h"
35 #include "include/mm_sound_plugin_codec.h"
36 #include "include/mm_sound_thread_pool.h"
38 #include "include/mm_sound_mgr_asm.h"
42 #define _ENABLE_KEYTONE /* Temporal test code */
45 int (*callback)(int, void *, void *); /* msg_type(pid) client callback & client data info */
50 MMHandleType plughandle;
56 } __mmsound_mgr_codec_handle_t;
58 static MMSoundPluginType *g_codec_plugins = NULL;
59 static __mmsound_mgr_codec_handle_t g_slots[MANAGER_HANDLE_MAX];
60 static mmsound_codec_interface_t g_plugins[MM_SOUND_SUPPORTED_CODEC_NUM];
61 static pthread_mutex_t g_slot_mutex;
63 static int _MMSoundMgrCodecStopCallback(int param);
64 static int _MMSoundMgrCodecFindKeytoneSlot(int *slotid);
65 static int _MMSoundMgrCodecGetEmptySlot(int *slotid);
66 static int _MMSoundMgrCodecFindLocaleSlot(int *slotid);
67 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin);
70 #define STATUS_KEYTONE 1
71 #define STATUS_LOCALE 2
72 #define STATUS_SOUND 3
74 #define SOUND_SLOT_START 0
79 sound_codec_asm_callback(int handle, ASM_event_sources_t event_src, ASM_sound_commands_t command, unsigned int sound_status, void* cb_data)
81 int slotid = (int)cb_data;
82 int result = MM_ERROR_NONE;
83 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
85 debug_log("Got audio session callback msg for session_handle %d\n", handle);
89 case ASM_COMMAND_STOP:
90 case ASM_COMMAND_PAUSE:
91 debug_log("Got msg from asm to Stop or Pause %d\n", command);
92 result = MMSoundMgrCodecStop(slotid);
93 cb_res = ASM_CB_RES_STOP;
95 case ASM_COMMAND_RESUME:
96 case ASM_COMMAND_PLAY:
97 debug_log("Got msg from asm to Play or Resume %d\n", command);
98 cb_res = ASM_CB_RES_NONE;;
106 int MMSoundMgrCodecInit(const char *targetdir)
113 memset (g_slots, 0, sizeof(g_slots));
115 if(pthread_mutex_init(&g_slot_mutex, NULL)) {
116 debug_error("pthread_mutex_init failed [%s][%d]\n", __func__, __LINE__);
117 return MM_ERROR_SOUND_INTERNAL;
120 for (count = 0; count < MANAGER_HANDLE_MAX; count++) {
121 g_slots[count].status = STATUS_IDLE;
122 g_slots[count].plughandle = -1;
125 if (g_codec_plugins) {
126 debug_warning("Please Check Init twice\n");
127 MMSoundPluginRelease(g_codec_plugins);
130 MMSoundPluginScan(targetdir, MM_SOUND_PLUGIN_TYPE_CODEC, &g_codec_plugins);
132 while (g_codec_plugins[loop].type != MM_SOUND_PLUGIN_TYPE_NONE) {
133 _MMSoundMgrCodecRegisterInterface(&g_codec_plugins[loop++]);
137 return MM_ERROR_NONE;
140 int MMSoundMgrCodecFini(void)
144 memset(g_plugins, 0, sizeof(mmsound_codec_interface_t) * MM_SOUND_SUPPORTED_CODEC_NUM);
145 MMSoundPluginRelease(g_codec_plugins);
146 g_codec_plugins = NULL;
147 pthread_mutex_destroy(&g_slot_mutex);
150 return MM_ERROR_NONE;
154 int MMSoundMgrCodecPlay(int *slotid, const mmsound_mgr_codec_param_t *param)
157 mmsound_codec_info_t info;
158 mmsound_codec_param_t codec_param;
159 int err = MM_ERROR_NONE;
161 int need_asm_unregister = 0;
165 debug_msg("DTMF : [%d]\n",param->tone);
166 debug_msg("Repeat : [%d]\n",param->repeat_count);
167 debug_msg("Volume : [%f]\n",param->volume);
169 for (count = 0; g_plugins[count].GetSupportTypes; count++) {
171 if (g_plugins[count].Parse(param->source, &info) == MM_ERROR_NONE)
175 debug_msg("Find plugin codec ::: [%d]\n", count); /*The count num means codec type WAV, MP3 */
177 if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
178 debug_error("unsupported file type %d\n", count);
179 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
184 if (param->keytone == 1) {
185 /* Find keytone slot */
186 err = _MMSoundMgrCodecFindKeytoneSlot(slotid);
187 /* Not First connect */
188 if (err == MM_ERROR_NONE) {
189 if(g_slots[*slotid].status != STATUS_IDLE) {
190 MMSoundMgrCodecStop(*slotid);
192 debug_msg("Key tone : Stop to Play !!!\n");
194 codec_param.keytone = param->keytone;
195 } else if (param->keytone == 2) {
196 /* Find keytone slot */
197 err = _MMSoundMgrCodecFindLocaleSlot(slotid);
198 /* Not First connect */
199 if (err == MM_ERROR_NONE) {
200 if(g_slots[*slotid].status != STATUS_IDLE) {
201 MMSoundMgrCodecStop(*slotid);
203 debug_msg("Key tone : Stop to Play !!!\n");
205 codec_param.keytone = param->keytone;
207 debug_msg("Get New handle\n");
208 codec_param.keytone = 0;
211 err = _MMSoundMgrCodecGetEmptySlot(slotid);
212 if (err != MM_ERROR_NONE) {
213 debug_error("Empty g_slot is not found\n");
217 codec_param.tone = param->tone;
218 codec_param.volume_config = param->volume_config;
219 codec_param.repeat_count = param->repeat_count;
220 codec_param.volume = param->volume;
221 codec_param.source = param->source;
222 codec_param.priority = param->priority;
223 codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
224 codec_param.param = *slotid;
225 codec_param.pid = (int)param->param;
226 codec_param.handle_route = param->handle_route;
228 pthread_mutex_lock(&g_slot_mutex);
229 debug_msg("After Slot_mutex LOCK\n");
231 /* In case of KEYTONE */
232 if (param->keytone == 1)
233 g_slots[*slotid].status = STATUS_KEYTONE;
235 /* In case of LOCALE */
236 if (param->keytone == 2) /* KeyTone */
237 g_slots[*slotid].status = STATUS_LOCALE;
243 if(param->session_type != ASM_EVENT_CALL && param->session_type != ASM_EVENT_VIDEOCALL) {
244 if(!ASM_register_sound_ex((int)param->param, ¶m->session_handle, param->session_type, ASM_STATE_PLAYING,
245 sound_codec_asm_callback, (void*)*slotid, ASM_RESOURCE_NONE, &errorcode, __asm_process_message)) {
246 debug_critical("ASM_register_sound() failed %d\n", errorcode);
247 pthread_mutex_unlock(&g_slot_mutex);
248 return MM_ERROR_POLICY_INTERNAL;
253 /* Codec id WAV or MP3 */
254 g_slots[*slotid].pluginid = count;
255 g_slots[*slotid].callback = param->callback;
256 g_slots[*slotid].msgcallback = param->msgcallback;
257 g_slots[*slotid].msgdata = param->msgdata;
258 g_slots[*slotid].param = param->param; /* This arg is used callback data */
259 g_slots[*slotid].session_type = param->session_type;
260 g_slots[*slotid].session_handle = param->session_handle;
262 debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
266 err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
267 debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
268 if (err != MM_ERROR_NONE) {
269 debug_error("Plugin create fail : 0x%08X\n", err);
270 g_slots[*slotid].status = STATUS_IDLE;
271 pthread_mutex_unlock(&g_slot_mutex);
272 debug_warning("After Slot_mutex UNLOCK\n");
273 need_asm_unregister = 1;
277 err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
278 if (err != MM_ERROR_NONE) {
279 debug_error("Fail to play : 0x%08X\n", err);
280 g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
281 need_asm_unregister = 1;
284 pthread_mutex_unlock(&g_slot_mutex);
285 debug_msg("After Slot_mutex UNLOCK\n");
288 if(param->session_type != ASM_EVENT_CALL && param->session_type != ASM_EVENT_VIDEOCALL && need_asm_unregister == 1) {
289 if(!ASM_unregister_sound_ex(param->session_handle, param->session_type, &errorcode,__asm_process_message)) {
290 debug_error("Unregister sound failed 0x%X\n", errorcode);
291 return MM_ERROR_POLICY_INTERNAL;
300 #define DTMF_PLUGIN_COUNT 2
301 int MMSoundMgrCodecPlayDtmf(int *slotid, const mmsound_mgr_codec_param_t *param)
305 mmsound_codec_info_t info;
306 mmsound_codec_param_t codec_param;
307 int err = MM_ERROR_NONE;
311 debug_msg("DTMF : [%d]\n",param->tone);
312 debug_msg("Repeat : [%d]\n",param->repeat_count);
313 debug_msg("Volume : [%f]\n",param->volume);
315 for (count = 0; g_plugins[count].GetSupportTypes; count++) {
317 codec_type = g_plugins[count].GetSupportTypes();
318 if(codec_type && (MM_SOUND_SUPPORTED_CODEC_DTMF == codec_type[0]))
322 debug_msg("Find plugin codec ::: [%d]\n", count); /*The count num means codec type DTMF */
324 if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
325 debug_error("unsupported file type %d\n", count);
326 printf("unsupported file type %d\n", count);
327 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
331 debug_msg("Get New handle\n");
332 codec_param.keytone = 0;
334 err = _MMSoundMgrCodecGetEmptySlot(slotid);
335 if(err != MM_ERROR_NONE)
337 debug_error("Empty g_slot is not found\n");
341 codec_param.tone = param->tone;
342 codec_param.priority = 0;
343 codec_param.volume_config = param->volume_config;
344 codec_param.repeat_count = param->repeat_count;
345 codec_param.volume = param->volume;
346 codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
347 codec_param.param = *slotid;
348 codec_param.pid = (int)param->param;
350 pthread_mutex_lock(&g_slot_mutex);
351 debug_msg("After Slot_mutex LOCK\n");
358 int need_asm_unregister = 0;
360 if(param->session_type != ASM_EVENT_CALL && param->session_type != ASM_EVENT_VIDEOCALL) {
361 if(!ASM_register_sound_ex((int)param->param, ¶m->session_handle, param->session_type, ASM_STATE_PLAYING,
362 sound_codec_asm_callback, (void*)*slotid, ASM_RESOURCE_NONE, &errorcode, __asm_process_message)) {
363 debug_critical("ASM_register_sound() failed %d\n", errorcode);
364 pthread_mutex_unlock(&g_slot_mutex);
365 return MM_ERROR_POLICY_INTERNAL;
369 g_slots[*slotid].pluginid = count;
370 g_slots[*slotid].callback = param->callback;
371 g_slots[*slotid].param = param->param; /* This arg is used callback data */
372 g_slots[*slotid].session_type = param->session_type;
373 g_slots[*slotid].session_handle = param->session_handle;
375 debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
377 err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
378 debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
379 if (err != MM_ERROR_NONE) {
380 debug_error("Plugin create fail : 0x%08X\n", err);
381 g_slots[*slotid].status = STATUS_IDLE;
382 pthread_mutex_unlock(&g_slot_mutex);
383 debug_warning("After Slot_mutex UNLOCK\n");
384 need_asm_unregister = 1;
388 err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
389 if (err != MM_ERROR_NONE) {
390 debug_error("Fail to play : 0x%08X\n", err);
391 g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
392 need_asm_unregister = 1;
395 pthread_mutex_unlock(&g_slot_mutex);
397 debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
398 debug_msg("After Slot_mutex UNLOCK\n")
407 int MMSoundMgrCodecStop(const int slotid)
409 int err = MM_ERROR_NONE;
411 debug_enter("(Slotid : [%d])\n", slotid);
413 if (slotid < 0 || MANAGER_HANDLE_MAX <= slotid) {
414 return MM_ERROR_INVALID_ARGUMENT;
417 pthread_mutex_lock (&g_slot_mutex);
418 debug_msg("After Slot_mutex LOCK\n");
419 if (g_slots[slotid].status == STATUS_IDLE) {
420 err = MM_ERROR_SOUND_INVALID_STATE;
421 debug_warning("The playing slots is not found, Slot ID : [%d]\n", slotid);
424 debug_msg("Found slot, Slotid [%d] State [%d]\n", slotid, g_slots[slotid].status);
426 err = g_plugins[g_slots[slotid].pluginid].Stop(g_slots[slotid].plughandle);
427 if (err != MM_ERROR_NONE) {
428 debug_error("Fail to STOP Code : 0x%08X\n", err);
430 debug_msg("Found slot, Slotid [%d] State [%d]\n", slotid, g_slots[slotid].status);
432 pthread_mutex_unlock(&g_slot_mutex);
433 debug_msg("After Slot_mutex UNLOCK\n");
434 debug_leave("(err : 0x%08X)\n", err);
439 static int _MMSoundMgrCodecStopCallback(int param)
441 int err = MM_ERROR_NONE;
443 debug_enter("(Slot : %d)\n", param);
445 pthread_mutex_lock(&g_slot_mutex);
446 debug_msg("[CODEC MGR] Slot_mutex lock done\n");
450 * Unregister ASM here
455 if(g_slots[param].session_type != ASM_EVENT_CALL && g_slots[param].session_type != ASM_EVENT_VIDEOCALL) {
456 debug_msg("[CODEC MGR] ASM unregister\n");
457 if(!ASM_unregister_sound_ex(g_slots[param].session_handle, g_slots[param].session_type, &errorcode, __asm_process_message)) {
458 debug_error("[CODEC MGR] Unregister sound failed 0x%X\n", errorcode);
464 if (g_slots[param].msgcallback) {
465 debug_msg("[CODEC MGR] msgcallback : %p\n", g_slots[param].msgcallback);
466 debug_msg("[CODEC MGR] msg data : %p\n", g_slots[param].msgdata);
467 debug_msg("[CODEC MGR] mgr codec callback : %p\n", g_slots[param].callback);
468 g_slots[param].callback((int)g_slots[param].param, g_slots[param].msgcallback, g_slots[param].msgdata); /*param means client msg_type */
470 debug_msg("Client callback msg_type (instance) : [%d]\n", (int)g_slots[param].param);
471 debug_msg("Handle allocated handle : [0x%08X]\n", g_slots[param].plughandle);
472 err = g_plugins[g_slots[param].pluginid].Destroy(g_slots[param].plughandle);
474 debug_critical("[CODEC MGR] Fail to destroy slot number : [%d] err [0x%x]\n", param, err);
476 memset(&g_slots[param], 0, sizeof(__mmsound_mgr_codec_handle_t));
477 g_slots[param].status = STATUS_IDLE;
478 pthread_mutex_unlock(&g_slot_mutex);
479 debug_msg("[CODEC MGR] Slot_mutex done\n");
484 static int _MMSoundMgrCodecFindKeytoneSlot(int *slotid)
487 int err = MM_ERROR_NONE;
491 pthread_mutex_lock(&g_slot_mutex);
492 debug_warning("After Slot_mutex LOCK\n");
494 for (count = SOUND_SLOT_START; count < MANAGER_HANDLE_MAX ; count++) {
495 if (g_slots[count].status == STATUS_KEYTONE) {
499 pthread_mutex_unlock(&g_slot_mutex);
500 debug_warning("After Slot_mutex UNLOCK\n");
501 if (count < MANAGER_HANDLE_MAX) {
502 debug_msg("Found keytone handle allocated (Slot : [%d])\n", count);
506 debug_warning("Handle is full handle [KEY TONE] : [%d]\n", count);
507 err = MM_ERROR_SOUND_INTERNAL;
515 static int _MMSoundMgrCodecFindLocaleSlot(int *slotid)
518 int err = MM_ERROR_NONE;
522 pthread_mutex_lock(&g_slot_mutex);
523 debug_warning("After Slot_mutex LOCK\n");
525 for (count = SOUND_SLOT_START; count < MANAGER_HANDLE_MAX ; count++) {
526 if (g_slots[count].status == STATUS_LOCALE) {
530 pthread_mutex_unlock(&g_slot_mutex);
532 debug_warning("After Slot_mutex UNLOCK\n");
533 if (count < MANAGER_HANDLE_MAX) {
534 debug_msg("Found locale handle allocated (Slot : [%d])\n", count);
538 debug_warning("Handle is full handle [KEY TONE] \n");
539 err = MM_ERROR_SOUND_INTERNAL;
547 static int _MMSoundMgrCodecGetEmptySlot(int *slot)
550 int err = MM_ERROR_NONE;
553 debug_msg("Codec slot ID : [%d]\n", *slot);
554 pthread_mutex_lock(&g_slot_mutex);
555 debug_msg("After Slot_mutex LOCK\n");
557 for (count = SOUND_SLOT_START; count < MANAGER_HANDLE_MAX ; count++) {
558 if (g_slots[count].status == STATUS_IDLE) {
559 g_slots[count].status = STATUS_SOUND;
563 pthread_mutex_unlock(&g_slot_mutex);
564 debug_msg("After Slot_mutex UNLOCK\n");
566 if (count < MANAGER_HANDLE_MAX) {
567 debug_msg("New handle allocated (codec slot ID : [%d])\n", count);
571 debug_warning("Handle is full handle : [%d]\n", count);
573 /* Temporal code for reset */
575 g_slots[count].status = STATUS_IDLE;
577 err = MM_ERROR_SOUND_INTERNAL;
585 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin)
587 int err = MM_ERROR_NONE;
589 void *getinterface = NULL;
593 /* find emptry slot */
594 for (count = 0; count < MM_SOUND_SUPPORTED_CODEC_NUM; count++) {
595 if (g_plugins[count].GetSupportTypes == NULL)
599 if (count == MM_SOUND_SUPPORTED_CODEC_NUM) {
600 debug_critical("The plugin support type is not valid\n");
601 return MM_ERROR_COMMON_OUT_OF_RANGE;
604 debug_msg("Empty slot find : %d\n", count);
606 err = MMSoundPluginGetSymbol(plugin, CODEC_GET_INTERFACE_FUNC_NAME, &getinterface);
607 if (err != MM_ERROR_NONE) {
608 debug_error("Get Symbol CODEC_GET_INTERFACE_FUNC_NAME is fail : %x\n", err);
611 debug_msg("Getinterface name : %s\n", (char*)getinterface );
613 err = MMSoundPlugCodecCastGetInterface(getinterface)(&g_plugins[count]);
614 if (err != MM_ERROR_NONE) {
615 debug_error("Get interface fail : %x\n", err);
618 /* If error occur, clean interface */
619 memset(&g_plugins[count], 0, sizeof(mmsound_codec_interface_t));
621 if (g_plugins[count].SetThreadPool)
622 g_plugins[count].SetThreadPool(MMSoundThreadPoolRun);