Remove useless parameters(route_info, priority) regarding audio path routing since...
[platform/core/multimedia/libmm-sound.git] / server / mm_sound_mgr_codec.c
1
2 /*
3  * libmm-sound
4  *
5  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6  *
7  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <pthread.h>
27
28 #include <mm_source.h>
29 #include <mm_error.h>
30 #include <mm_types.h>
31 #include <mm_debug.h>
32 #include <mm_ipc.h>
33 #include <mm_session.h>
34
35 #include "include/mm_sound_mgr_common.h"
36 #include "include/mm_sound_mgr_codec.h"
37 #include "include/mm_sound_mgr_ipc.h"
38 #include "include/mm_sound_plugin_codec.h"
39 #include "include/mm_sound_thread_pool.h"
40
41 #include "../include/mm_sound_common.h"
42 #include "../include/mm_sound_focus.h"
43
44 #define _ENABLE_KEYTONE /* Temporal test code */
45
46 #define FOCUS_INTEGRATION
47
48 typedef struct {
49         int (*callback)(int, void *, void *, int);      /* msg_type(pid) client callback & client data info */
50         void *param;
51         int pid;
52         void *msgcallback;
53         void *msgdata;
54         MMHandleType plughandle;
55
56         int pluginid;
57         int status;
58         int session_type;
59         int session_options;
60 #ifdef FOCUS_INTEGRATION
61         int focus_handle;
62         int focus_wcb_id;
63         unsigned int subs_id;
64         mm_sound_focus_type_e current_focus_type;
65 #endif
66
67         bool enable_session;
68  } __mmsound_mgr_codec_handle_t;
69
70 static MMSoundPluginType *g_codec_plugins = NULL;
71 static __mmsound_mgr_codec_handle_t g_slots[MANAGER_HANDLE_MAX];
72 static mmsound_codec_interface_t g_plugins[MM_SOUND_SUPPORTED_CODEC_NUM];
73 static pthread_mutex_t g_slot_mutex;
74 static pthread_mutex_t codec_wave_mutex;
75 static int _MMSoundMgrCodecStopCallback(int param);
76 static int _MMSoundMgrCodecGetEmptySlot(int *slotid);
77 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin);
78
79 #define STATUS_IDLE 0
80 #define STATUS_SOUND 3
81
82 #define SOUND_SLOT_START 0
83
84 #ifdef FOCUS_INTEGRATION
85
86 void sound_codec_focus_callback(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e focus_state, const char *reason_for_change, int option, const char *ext_info, void *user_data)
87 {
88
89         int slotid = (int)user_data;
90         int result = MM_ERROR_NONE;
91
92         debug_warning ("focus callback called -> focus_stae(%d), reasoun_for_change(%s)", focus_state, reason_for_change ? reason_for_change : "N/A");
93
94         if(g_slots[slotid].session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE){
95                 debug_warning ("session option is UNINTERRUPTIBLE, nothing to do with focus");
96                 return;
97         }
98         if (focus_state == FOCUS_IS_RELEASED) {
99                 g_slots[slotid].current_focus_type = FOCUS_FOR_BOTH&(~focus_type);
100                 debug_warning ("focus is released -> stop playing");
101                 result = MMSoundMgrCodecStop(slotid);
102                 if (result != MM_ERROR_NONE) {
103                         debug_log("result error %d\n", result);
104                 }
105
106         }
107         return;
108 }
109
110 void sound_codec_focus_watch_callback(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e focus_state, const char *reason_for_change, const char* ext_info, void *user_data)
111 {
112         int slotid = (int)user_data;
113         int result = MM_ERROR_NONE;
114
115         debug_warning ("focus callback called -> focus_stae(%d), reasoun_for_change(%s)", focus_state, reason_for_change ? reason_for_change : "N/A");
116
117         if(g_slots[slotid].session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE){
118                 debug_warning ("session option is UNINTERRUPTIBLE, nothing to do with focus");
119                 return;
120         }
121         if (focus_state == FOCUS_IS_ACQUIRED) {
122                 debug_warning ("focus is released -> stop playing");
123                 result = MMSoundMgrCodecStop(slotid);
124                 if (result != MM_ERROR_NONE) {
125                         debug_log("result error %d\n", result);
126                 }
127
128         }
129         return;
130 }
131
132
133 void sound_codec_device_connected_callback(MMSoundDevice_t device, bool is_connected, void *user_data)
134 {
135         int slotid = (int)user_data;
136         int result = MM_ERROR_NONE;
137         mm_sound_device_type_e type;
138
139         debug_warning ("device_connected_callback called");
140
141         if (mm_sound_get_device_type (device, &type) != MM_ERROR_NONE) {
142                 debug_error("getting device type failed");
143         } else {
144                 switch (type) {
145                         case MM_SOUND_DEVICE_TYPE_AUDIOJACK:
146                         case MM_SOUND_DEVICE_TYPE_BLUETOOTH_A2DP:
147                         case MM_SOUND_DEVICE_TYPE_BLUETOOTH_SCO:
148                         case MM_SOUND_DEVICE_TYPE_HDMI:
149                         case MM_SOUND_DEVICE_TYPE_MIRRORING:
150                         case MM_SOUND_DEVICE_TYPE_USB_AUDIO:
151                                 if (!is_connected) {
152                                         debug_warning("sound device unplugged");
153                                         result = MMSoundMgrCodecStop(slotid);
154                                         if (result != MM_ERROR_NONE) {
155                                                 debug_error("MMSoundMgrCodecStop error %d\n", result);
156                                         }
157                                         result = mm_sound_remove_device_connected_callback(g_slots[slotid].subs_id);
158                                         if (result != MM_ERROR_NONE) {
159                                                 debug_error("mm_sound_remove_device_connected_callback error %d\n", result);
160                                         }
161                                 }
162                                 break;
163                         default:
164                                 break;
165                 }
166         }
167 }
168 #endif
169
170 int MMSoundMgrCodecInit(const char *targetdir)
171 {
172         int loop = 0;
173         int count = 0;
174
175         debug_enter("\n");
176
177         memset (g_slots, 0, sizeof(g_slots));
178
179         if(pthread_mutex_init(&g_slot_mutex, NULL)) {
180                 debug_error("pthread_mutex_init failed\n");
181                 return MM_ERROR_SOUND_INTERNAL;
182         }
183
184         if(pthread_mutex_init(&codec_wave_mutex, NULL)) {
185                 debug_error("pthread_mutex_init failed\n");
186                 return MM_ERROR_SOUND_INTERNAL;
187         }
188
189         for (count = 0; count < MANAGER_HANDLE_MAX; count++) {
190                 g_slots[count].status = STATUS_IDLE;
191                 g_slots[count].plughandle = 0;
192         }
193
194         if (g_codec_plugins) {
195                 debug_warning("Please Check Init twice\n");
196                 MMSoundPluginRelease(g_codec_plugins);
197         }
198
199         MMSoundPluginScan(targetdir, MM_SOUND_PLUGIN_TYPE_CODEC, &g_codec_plugins);
200         if (g_codec_plugins) {
201                 while (g_codec_plugins[loop].type != MM_SOUND_PLUGIN_TYPE_NONE) {
202                         _MMSoundMgrCodecRegisterInterface(&g_codec_plugins[loop++]);
203                 }
204         }
205
206         debug_leave("\n");
207         return MM_ERROR_NONE;
208 }
209
210 int MMSoundMgrCodecFini(void)
211 {
212         debug_enter("\n");
213
214         memset(g_plugins, 0, sizeof(mmsound_codec_interface_t) * MM_SOUND_SUPPORTED_CODEC_NUM);
215         MMSoundPluginRelease(g_codec_plugins);
216         g_codec_plugins = NULL;
217         pthread_mutex_destroy(&g_slot_mutex);
218         pthread_mutex_destroy(&codec_wave_mutex);
219         debug_leave("\n");
220         return MM_ERROR_NONE;
221 }
222
223
224 int MMSoundMgrCodecPlay(int *slotid, const mmsound_mgr_codec_param_t *param)
225 {
226         int count = 0;
227         mmsound_codec_info_t info;
228         mmsound_codec_param_t codec_param;
229         int err = MM_ERROR_NONE;
230 #ifdef FOCUS_INTEGRATION
231         int need_focus_unregister = 0;
232 #endif
233
234 #ifdef DEBUG_DETAIL
235         debug_enter("\n");
236 #endif
237
238         for (count = 0; g_plugins[count].GetSupportTypes; count++) {
239                 /* Find codec */
240                 if (g_plugins[count].Parse(param->source, &info) == MM_ERROR_NONE)
241                         break;
242         }
243
244         /*The count num means codec type WAV, MP3 */
245         debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d], session(type[%d],option[%d])\n",
246                 param->tone, param->repeat_count, param->volume, count, param->session_type, param->session_options);
247
248         if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
249                 debug_error("unsupported file type %d\n", count);
250                 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
251                 goto cleanup;
252         }
253
254 #ifdef DEBUG_DETAIL
255         debug_msg("Get New handle\n");
256 #endif
257
258         err = _MMSoundMgrCodecGetEmptySlot(slotid);
259         if (err != MM_ERROR_NONE || *slotid < 0) {
260                 debug_error("Empty g_slot is not found\n");
261                 goto cleanup;
262         }
263
264         codec_param.tone = param->tone;
265         codec_param.volume_config = param->volume_config;
266         codec_param.repeat_count = param->repeat_count;
267         codec_param.volume = param->volume;
268         codec_param.source = param->source;
269         codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
270         codec_param.param = *slotid;
271         codec_param.pid = (int)param->param;
272         codec_param.codec_wave_mutex = &codec_wave_mutex;
273         codec_param.stream_index = param->stream_index;
274         MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
275         pthread_mutex_lock(&g_slot_mutex);
276 #ifdef DEBUG_DETAIL
277         debug_msg("After Slot_mutex LOCK\n");
278 #endif
279
280 #ifdef FOCUS_INTEGRATION
281         /*
282          * Register FOCUS here
283          */
284
285         if (param->session_type != MM_SESSION_TYPE_CALL &&
286                 param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
287                 param->session_type != MM_SESSION_TYPE_VOIP &&
288                 param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
289                 param->enable_session) {
290
291                 unsigned int subs_id = 0;
292
293                 err = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG, (mm_sound_device_connected_cb)sound_codec_device_connected_callback, (void*) *slotid, &subs_id);
294                 if (err) {
295                         debug_error("mm_sound_add_device_connected_callback failed [0x%x]", err);
296                         pthread_mutex_unlock(&g_slot_mutex);
297                         return MM_ERROR_POLICY_INTERNAL;
298                 }
299                 g_slots[*slotid].subs_id = subs_id;
300
301                 if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) || param->session_type == MM_SESSION_TYPE_ALARM || param->session_type == MM_SESSION_TYPE_NOTIFY || param->session_type == MM_SESSION_TYPE_EMERGENCY) {
302                         debug_warning("session option is PAUSE_OTHERS -> acquire focus");
303                         err = mm_sound_focus_get_id((int *)(&param->focus_handle));
304                         err = mm_sound_register_focus_for_session(param->focus_handle, (int)param->param, "media", sound_codec_focus_callback, (void*)*slotid);
305                         if (err) {
306                                 debug_error("mm_sound_register_focus_for_session failed [0x%x]", err);
307                                 pthread_mutex_unlock(&g_slot_mutex);
308                                 return MM_ERROR_POLICY_INTERNAL;
309                         }
310                         err = mm_sound_acquire_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
311                         if (err) {
312                                 debug_error("mm_sound_acquire_focus failed [0x%x]", err);
313                                 err = mm_sound_unregister_focus(param->focus_handle);
314                                 pthread_mutex_unlock(&g_slot_mutex);
315                                 return MM_ERROR_POLICY_INTERNAL;
316                         }
317                         g_slots[*slotid].current_focus_type = FOCUS_FOR_BOTH;
318                 } else if (param->session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
319                         /* do nothing */
320                         debug_warning("session option is UNINTERRUPTIBLE, nothing to do with focus");
321                 } else {
322                         debug_warning("need to set focus watch callback");
323                         err = mm_sound_set_focus_watch_callback_for_session((int)param->param, FOCUS_FOR_BOTH, sound_codec_focus_watch_callback, (void*)*slotid, (int *)(&param->focus_wcb_id));
324                         if (err) {
325                                 debug_error("mm_sound_set_focus_watch_callback_for_session failed [0x%x]", err);
326                                 pthread_mutex_unlock(&g_slot_mutex);
327                                 return MM_ERROR_POLICY_INTERNAL;
328                         }
329                 }
330         }
331         //
332 #endif
333
334         /* Codec id WAV or MP3 */
335         g_slots[*slotid].pluginid = count;
336         g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
337         g_slots[*slotid].session_type = param->session_type;
338         g_slots[*slotid].session_options = param->session_options;
339 #ifdef FOCUS_INTEGRATION
340         g_slots[*slotid].focus_handle = param->focus_handle;
341         g_slots[*slotid].focus_wcb_id = param->focus_wcb_id;
342 #endif
343         g_slots[*slotid].enable_session = true;
344         g_slots[*slotid].pid = (int)param->param;
345
346
347         debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
348
349         err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
350         debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
351         if (err != MM_ERROR_NONE) {
352                 debug_error("Plugin create fail : 0x%08X\n", err);
353                 g_slots[*slotid].status = STATUS_IDLE;
354                 pthread_mutex_unlock(&g_slot_mutex);
355                 debug_warning("After Slot_mutex UNLOCK\n");
356 #ifdef FOCUS_INTEGRATION
357                 if (param->focus_handle) {
358                         need_focus_unregister = 1;
359                 }
360 #endif
361                 goto cleanup;
362         }
363
364         err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
365         if (err != MM_ERROR_NONE) {
366                 debug_error("Fail to play : 0x%08X\n", err);
367                 g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
368 #ifdef FOCUS_INTEGRATION
369                 if (param->focus_handle) {
370                         need_focus_unregister = 1;
371                 }
372 #endif
373         }
374
375         pthread_mutex_unlock(&g_slot_mutex);
376 #ifdef DEBUG_DETAIL
377         debug_msg("After Slot_mutex UNLOCK\n");
378 #endif
379
380 cleanup:
381 #ifdef FOCUS_INTEGRATION
382         if(param->session_type != MM_SESSION_TYPE_CALL &&
383                 param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
384                 param->session_type != MM_SESSION_TYPE_VOIP &&
385                 param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
386                 param->enable_session &&
387                 need_focus_unregister == 1) {
388
389                 if (param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS || param->session_type == MM_SESSION_TYPE_ALARM || param->session_type == MM_SESSION_TYPE_NOTIFY || param->session_type == MM_SESSION_TYPE_EMERGENCY) {
390                         err = mm_sound_release_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
391                         if(mm_sound_unregister_focus(param->focus_handle) || err) {
392                                 debug_error("focus cleaning up failed[0x%x]", err);
393                                 return MM_ERROR_POLICY_INTERNAL;
394                         }
395                 } else if (~(param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
396                         err = mm_sound_unset_focus_watch_callback(param->focus_wcb_id);
397                         if (err) {
398                                 debug_error("focus watch cleaning up failed[0x%x]", err);
399                                 return MM_ERROR_POLICY_INTERNAL;
400                         }
401                 }
402         }
403 #endif
404
405 #ifdef DEBUG_DETAIL
406         debug_leave("\n");
407 #endif
408
409         return err;
410 }
411
412 int MMSoundMgrCodecPlayWithStreamInfo(int *slotid, const mmsound_mgr_codec_param_t *param)
413 {
414         int count = 0;
415         mmsound_codec_info_t info;
416         mmsound_codec_param_t codec_param;
417         int err = MM_ERROR_NONE;
418
419 #ifdef DEBUG_DETAIL
420         debug_enter("\n");
421 #endif
422
423         for (count = 0; g_plugins[count].GetSupportTypes; count++) {
424                 /* Find codec */
425                 if (g_plugins[count].Parse(param->source, &info) == MM_ERROR_NONE)
426                         break;
427         }
428
429         /*The count num means codec type WAV, MP3 */
430         debug_msg("Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->repeat_count, param->volume, count);
431
432         if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
433                 debug_error("unsupported file type %d\n", count);
434                 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
435                 goto cleanup;
436         }
437
438         err = _MMSoundMgrCodecGetEmptySlot(slotid);
439         if (err != MM_ERROR_NONE || *slotid < 0) {
440                 debug_error("Empty g_slot is not found\n");
441                 goto cleanup;
442         }
443
444         codec_param.volume_config = -1; //setting volume config to -1 since using stream info instead of volume type
445         codec_param.repeat_count = param->repeat_count;
446         codec_param.volume = param->volume;
447         codec_param.source = param->source;
448         codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
449         codec_param.param = *slotid;
450         codec_param.pid = (int)param->param;
451         codec_param.codec_wave_mutex = &codec_wave_mutex;
452         codec_param.stream_index = param->stream_index;
453         MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
454         pthread_mutex_lock(&g_slot_mutex);
455 #ifdef DEBUG_DETAIL
456         debug_msg("After Slot_mutex LOCK\n");
457 #endif
458
459         /* Codec id WAV or MP3 */
460         g_slots[*slotid].pluginid = count;
461         g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
462
463         debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
464
465         err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
466         debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
467         if (err != MM_ERROR_NONE) {
468                 debug_error("Plugin create fail : 0x%08X\n", err);
469                 g_slots[*slotid].status = STATUS_IDLE;
470                 pthread_mutex_unlock(&g_slot_mutex);
471                 debug_warning("After Slot_mutex UNLOCK\n");
472                 goto cleanup;
473         }
474
475         err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
476         if (err != MM_ERROR_NONE) {
477                 debug_error("Fail to play : 0x%08X\n", err);
478                 g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
479         }
480
481         pthread_mutex_unlock(&g_slot_mutex);
482 #ifdef DEBUG_DETAIL
483         debug_msg("After Slot_mutex UNLOCK\n");
484 #endif
485
486 cleanup:
487
488 #ifdef DEBUG_DETAIL
489         debug_leave("\n");
490 #endif
491
492         return err;
493
494 }
495
496 #define DTMF_PLUGIN_COUNT 2
497 int MMSoundMgrCodecPlayDtmf(int *slotid, const mmsound_mgr_codec_param_t *param)
498 {
499         int count = 0;
500         int *codec_type;
501         mmsound_codec_info_t info;
502         mmsound_codec_param_t codec_param;
503         int err = MM_ERROR_NONE;
504 #ifdef FOCUS_INTEGRATION
505         int need_focus_unregister = 0;
506 #endif
507
508 #ifdef DEBUG_DETAIL
509         debug_enter("\n");
510 #endif
511
512         for (count = 0; g_plugins[count].GetSupportTypes; count++) {
513                 /* Find codec */
514                 codec_type = g_plugins[count].GetSupportTypes();
515                 if(codec_type && (MM_SOUND_SUPPORTED_CODEC_DTMF == codec_type[0]))
516                         break;
517         }
518
519         /*The count num means codec type DTMF */
520         debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->tone, param->repeat_count, param->volume, count);
521
522         if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
523                 debug_error("unsupported file type %d\n", count);
524                 printf("unsupported file type %d\n", count);
525                 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
526                 goto cleanup;
527         }
528
529 #ifdef DEBUG_DETAIL
530         debug_msg("Get New handle\n");
531 #endif
532
533         err = _MMSoundMgrCodecGetEmptySlot(slotid);
534         if(err != MM_ERROR_NONE || *slotid < 0)
535         {
536                 debug_error("Empty g_slot is not found\n");
537                 goto cleanup;
538         }
539
540         codec_param.tone = param->tone;
541         codec_param.volume_config = param->volume_config;
542         codec_param.repeat_count = param->repeat_count;
543         codec_param.volume = param->volume;
544         codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
545         codec_param.param = *slotid;
546         codec_param.pid = (int)param->param;
547         codec_param.stream_index = param->stream_index;
548         MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
549
550         pthread_mutex_lock(&g_slot_mutex);
551 #ifdef DEBUG_DETAIL
552         debug_msg("After Slot_mutex LOCK\n");
553 #endif
554
555 #ifdef FOCUS_INTEGRATION
556         //
557         /*
558          * Register FOCUS here
559          */
560
561         if (param->session_type != MM_SESSION_TYPE_CALL &&
562                 param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
563                 param->session_type != MM_SESSION_TYPE_VOIP &&
564                 param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
565                 param->enable_session) {
566
567                 unsigned int subs_id = 0;
568
569                 err = mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG, (mm_sound_device_connected_cb)sound_codec_device_connected_callback, (void*) *slotid, &subs_id);
570                 if (err) {
571                         debug_error("mm_sound_add_device_connected_callback failed [0x%x]", err);
572                         pthread_mutex_unlock(&g_slot_mutex);
573                         return MM_ERROR_POLICY_INTERNAL;
574                 }
575                 g_slots[*slotid].subs_id = subs_id;
576
577                 if ((param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS) || param->session_type == MM_SESSION_TYPE_ALARM || param->session_type == MM_SESSION_TYPE_NOTIFY || param->session_type == MM_SESSION_TYPE_EMERGENCY) {
578                         debug_warning("session option is PAUSE_OTHERS -> acquire focus");
579                         err = mm_sound_focus_get_id((int *)(&param->focus_handle));
580                         err = mm_sound_register_focus_for_session(param->focus_handle, (int)param->param, "media", sound_codec_focus_callback, (void*)*slotid);
581                         if (err) {
582                                 debug_error("mm_sound_register_focus failed [0x%x]", err);
583                                 pthread_mutex_unlock(&g_slot_mutex);
584                                 return MM_ERROR_POLICY_INTERNAL;
585                         }
586                         err = mm_sound_acquire_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
587                         if (err) {
588                                 debug_error("mm_sound_acquire_focus failed [0x%x]", err);
589                                 err = mm_sound_unregister_focus(param->focus_handle);
590                                 pthread_mutex_unlock(&g_slot_mutex);
591                                 return MM_ERROR_POLICY_INTERNAL;
592                         }
593                         g_slots[*slotid].current_focus_type = FOCUS_FOR_BOTH;
594                 } else if (param->session_options & MM_SESSION_OPTION_UNINTERRUPTIBLE) {
595                         /* do nothing */
596                         debug_warning("session option is UNINTERRUPTIBLE, nothing to do with focus");
597                 } else {
598                         debug_warning("need to set focus watch callback");
599                         err = mm_sound_set_focus_watch_callback_for_session((int)param->param, FOCUS_FOR_BOTH, sound_codec_focus_watch_callback, (void*)*slotid, (int *)(&param->focus_wcb_id));
600                         if (err) {
601                                 debug_error("mm_sound_set_focus_watch_callback failed [0x%x]", err);
602                                 pthread_mutex_unlock(&g_slot_mutex);
603                                 return MM_ERROR_POLICY_INTERNAL;
604                         }
605                 }
606         }
607 #endif
608
609         g_slots[*slotid].pluginid = count;
610         g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
611         g_slots[*slotid].session_type = param->session_type;
612         g_slots[*slotid].session_options = param->session_options;
613 #ifdef FOCUS_INTEGRATION
614         g_slots[*slotid].focus_handle= param->focus_handle;
615         g_slots[*slotid].focus_wcb_id= param->focus_wcb_id;
616 #endif
617         g_slots[*slotid].enable_session = param->enable_session;
618         g_slots[*slotid].pid = (int)param->param;
619
620 #ifdef DEBUG_DETAIL
621         debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
622 #endif
623
624         err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
625         debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
626         if (err != MM_ERROR_NONE) {
627                 debug_error("Plugin create fail : 0x%08X\n", err);
628                 g_slots[*slotid].status = STATUS_IDLE;
629                 pthread_mutex_unlock(&g_slot_mutex);
630                 debug_warning("After Slot_mutex UNLOCK\n");
631 #ifdef FOCUS_INTEGRATION
632                 need_focus_unregister = 1;
633 #endif
634                 goto cleanup;
635         }
636
637         err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
638         if (err != MM_ERROR_NONE) {
639                 debug_error("Fail to play : 0x%08X\n", err);
640                 g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
641 #ifdef FOCUS_INTEGRATION
642                 need_focus_unregister = 1;
643 #endif
644         }
645
646         pthread_mutex_unlock(&g_slot_mutex);
647
648         debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
649 #ifdef DEBUG_DETAIL
650         debug_msg("After Slot_mutex UNLOCK\n")
651 #endif
652
653 cleanup:
654 #ifdef FOCUS_INTEGRATION
655         if (param->session_type != MM_SESSION_TYPE_CALL &&
656                 param->session_type != MM_SESSION_TYPE_VIDEOCALL &&
657                 param->session_type != MM_SESSION_TYPE_VOIP &&
658                 param->session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
659                 param->enable_session &&
660                 need_focus_unregister == 1) {
661
662                 if (param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS || param->session_type == MM_SESSION_TYPE_ALARM || param->session_type == MM_SESSION_TYPE_NOTIFY || param->session_type == MM_SESSION_TYPE_EMERGENCY) {
663                         err = mm_sound_release_focus(param->focus_handle, FOCUS_FOR_BOTH, NULL);
664                         if(mm_sound_unregister_focus(param->focus_handle) || err) {
665                                 debug_error("focus cleaning up failed[0x%x]", err);
666                                 return MM_ERROR_POLICY_INTERNAL;
667                         }
668                 } else if (~(param->session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
669                         err = mm_sound_unset_focus_watch_callback(param->focus_wcb_id);
670                         if (err) {
671                                 debug_error("focus watch cleaning up failed[0x%x]", err);
672                                 return MM_ERROR_POLICY_INTERNAL;
673                         }
674                 }
675         }
676 #endif
677
678 #ifdef DEBUG_DETAIL
679         debug_leave("\n");
680 #endif
681
682         return err;
683 }
684
685 int MMSoundMgrCodecPlayDtmfWithStreamInfo(int *slotid, const mmsound_mgr_codec_param_t *param)
686 {
687         int count = 0;
688         int *codec_type;
689         mmsound_codec_info_t info;
690         mmsound_codec_param_t codec_param;
691         int err = MM_ERROR_NONE;
692
693 #ifdef DEBUG_DETAIL
694         debug_enter("\n");
695 #endif
696
697         for (count = 0; g_plugins[count].GetSupportTypes; count++) {
698                 /* Find codec */
699                 codec_type = g_plugins[count].GetSupportTypes();
700                 if(codec_type && (MM_SOUND_SUPPORTED_CODEC_DTMF == codec_type[0]))
701                         break;
702         }
703
704         /*The count num means codec type DTMF */
705         debug_msg("DTMF[%d] Repeat[%d] Volume[%f] plugin_codec[%d]\n", param->tone, param->repeat_count, param->volume, count);
706
707         if (g_plugins[count].GetSupportTypes == NULL) { /* Codec not found */
708                 debug_error("unsupported file type %d\n", count);
709                 printf("unsupported file type %d\n", count);
710                 err = MM_ERROR_SOUND_UNSUPPORTED_MEDIA_TYPE;
711                 goto cleanup;
712         }
713
714 #ifdef DEBUG_DETAIL
715         debug_msg("Get New handle\n");
716 #endif
717
718         err = _MMSoundMgrCodecGetEmptySlot(slotid);
719         if(err != MM_ERROR_NONE || *slotid < 0)
720         {
721                 debug_error("Empty g_slot is not found\n");
722                 goto cleanup;
723         }
724
725         codec_param.tone = param->tone;
726         codec_param.repeat_count = param->repeat_count;
727         codec_param.volume = param->volume;
728         codec_param.stop_cb = _MMSoundMgrCodecStopCallback;
729         codec_param.param = *slotid;
730         codec_param.pid = (int)param->param;
731         codec_param.volume_config = -1; //setting volume config to -1 since using stream info instead of volume type
732         codec_param.stream_index = param->stream_index;
733         MMSOUND_STRNCPY(codec_param.stream_type, param->stream_type, MM_SOUND_STREAM_TYPE_LEN);
734
735         pthread_mutex_lock(&g_slot_mutex);
736 #ifdef DEBUG_DETAIL
737         debug_msg("After Slot_mutex LOCK\n");
738 #endif
739                 g_slots[*slotid].pluginid = count;
740                 g_slots[*slotid].param    = param->param;               /* This arg is used callback data */
741                 g_slots[*slotid].enable_session = param->enable_session;
742
743 #ifdef DEBUG_DETAIL
744                 debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
745 #endif
746
747                 err = g_plugins[g_slots[*slotid].pluginid].Create(&codec_param, &info, &(g_slots[*slotid].plughandle));
748                 debug_msg("Created audio handle : [%d]\n", g_slots[*slotid].plughandle);
749                 if (err != MM_ERROR_NONE) {
750                         debug_error("Plugin create fail : 0x%08X\n", err);
751                         g_slots[*slotid].status = STATUS_IDLE;
752                         pthread_mutex_unlock(&g_slot_mutex);
753                         debug_warning("After Slot_mutex UNLOCK\n");
754                         goto cleanup;
755                 }
756
757                 err = g_plugins[g_slots[*slotid].pluginid].Play(g_slots[*slotid].plughandle);
758                 if (err != MM_ERROR_NONE) {
759                         debug_error("Fail to play : 0x%08X\n", err);
760                         g_plugins[g_slots[*slotid].pluginid].Destroy(g_slots[*slotid].plughandle);
761                 }
762
763                 pthread_mutex_unlock(&g_slot_mutex);
764
765                 debug_msg("Using Slotid : [%d] Slot Status : [%d]\n", *slotid, g_slots[*slotid].status);
766 #ifdef DEBUG_DETAIL
767                 debug_msg("After Slot_mutex UNLOCK\n");
768 #endif
769
770         cleanup:
771 #ifdef DEBUG_DETAIL
772                 debug_leave("\n");
773 #endif
774
775                 return err;
776
777 }
778
779 int MMSoundMgrCodecStop(const int slotid)
780 {
781         int err = MM_ERROR_NONE;
782
783         debug_enter("(Slotid : [%d])\n", slotid);
784
785         if (slotid < 0 || MANAGER_HANDLE_MAX <= slotid) {
786                 return MM_ERROR_INVALID_ARGUMENT;
787         }
788
789         pthread_mutex_lock (&g_slot_mutex);
790 #ifdef DEBUG_DETAIL
791         debug_msg("After Slot_mutex LOCK\n");
792 #endif
793         if (g_slots[slotid].status == STATUS_IDLE) {
794                 err = MM_ERROR_SOUND_INVALID_STATE;
795                 debug_warning("The playing slots is not found, Slot ID : [%d]\n", slotid);
796                 goto cleanup;
797         }
798 #ifdef DEBUG_DETAIL
799         debug_msg("Found slot, Slotid [%d] State [%d]\n", slotid, g_slots[slotid].status);
800 #endif
801
802         err = g_plugins[g_slots[slotid].pluginid].Stop(g_slots[slotid].plughandle);
803         if (err != MM_ERROR_NONE) {
804                 debug_error("Fail to STOP Code : 0x%08X\n", err);
805         }
806         debug_msg("Found slot, Slotid [%d] State [%d]\n", slotid, g_slots[slotid].status);
807 cleanup:
808         pthread_mutex_unlock(&g_slot_mutex);
809 #ifdef DEBUG_DETAIL
810         debug_msg("After Slot_mutex UNLOCK\n");
811 #endif
812         debug_leave("(err : 0x%08X)\n", err);
813
814         return err;
815 }
816 #ifdef FOCUS_INTEGRATION
817 int MMSoundMgrCodecClearFocus(int pid)
818 {
819         int err = MM_ERROR_NONE;
820         int slotid;
821
822         debug_enter("(pid : [%d])\n", pid);
823
824         pthread_mutex_lock (&g_slot_mutex);
825
826         for (slotid = 0 ; slotid < MANAGER_HANDLE_MAX ; slotid++) {
827                 if (g_slots[slotid].pid == pid) {
828                         if (g_slots[slotid].focus_handle || g_slots[slotid].focus_wcb_id) {
829                                 if(g_slots[slotid].session_type != MM_SESSION_TYPE_CALL &&
830                                         g_slots[slotid].session_type != MM_SESSION_TYPE_VIDEOCALL &&
831                                         g_slots[slotid].session_type != MM_SESSION_TYPE_VOIP &&
832                                         g_slots[slotid].session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
833                                         g_slots[slotid].enable_session ) {
834                                         if ((g_slots[slotid].session_options & MM_SESSION_OPTION_PAUSE_OTHERS) || g_slots[slotid].session_type == MM_SESSION_TYPE_ALARM || g_slots[slotid].session_type == MM_SESSION_TYPE_NOTIFY || g_slots[slotid].session_type == MM_SESSION_TYPE_EMERGENCY) {
835                                                 err = mm_sound_release_focus(g_slots[slotid].focus_handle, FOCUS_FOR_BOTH, NULL);
836                                                 if (err) {
837                                                         debug_error("mm_sound_release_focus failed [0x%x]", err);
838                                                 }
839                                                 if(mm_sound_unregister_focus(g_slots[slotid].focus_handle) || err) {
840                                                         debug_error("Focus clean up failed [0x%x]", err);
841                                                         err = MM_ERROR_POLICY_INTERNAL;
842                                                         goto cleanup;
843                                                 }
844                                         } else if (~(g_slots[slotid].session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
845                                                 err = mm_sound_unset_focus_watch_callback(g_slots[slotid].focus_wcb_id);
846                                                 if (err) {
847                                                         debug_error("mm_sound_unset_focus_watch_callback failed [0x%x]", err);
848                                                         err = MM_ERROR_POLICY_INTERNAL;
849                                                         goto cleanup;
850                                                 }
851                                         }
852                                 }
853                                 if(mm_sound_remove_device_connected_callback(g_slots[slotid].subs_id) != MM_ERROR_NONE)
854                                         debug_error("mm_sound_remove_device_connected_callback() failed");
855                                 g_slots[slotid].focus_handle = 0;
856                                 g_slots[slotid].focus_wcb_id = 0;
857                                 g_slots[slotid].subs_id = 0;
858                         }
859                 }
860         }
861
862 cleanup:
863         pthread_mutex_unlock(&g_slot_mutex);
864         debug_leave("(err : 0x%08X)\n", err);
865
866         return err;
867 }
868 #endif
869
870 static int _MMSoundMgrCodecStopCallback(int param)
871 {
872         int err = MM_ERROR_NONE;
873
874         debug_enter("(Slot : %d)\n", param);
875
876         if (param < 0 || param >= MANAGER_HANDLE_MAX) {
877                 debug_error ("Slot index param [%d] is invalid", param);
878                 return MM_ERROR_INVALID_ARGUMENT;
879         }
880
881         pthread_mutex_lock(&g_slot_mutex);
882         debug_msg("[CODEC MGR] Slot_mutex lock done\n");
883
884 #ifdef FOCUS_INTEGRATION
885         /*
886          * Unregister FOCUS here
887          */
888         debug_msg("[CODEC MGR] enable_session %d ",g_slots[param].enable_session);
889
890         if (g_slots[param].focus_handle || g_slots[param].focus_wcb_id) {
891                 if(g_slots[param].session_type != MM_SESSION_TYPE_CALL &&
892                         g_slots[param].session_type != MM_SESSION_TYPE_VIDEOCALL &&
893                         g_slots[param].session_type != MM_SESSION_TYPE_VOIP &&
894                         g_slots[param].session_type != MM_SESSION_TYPE_VOICE_RECOGNITION &&
895                         g_slots[param].enable_session ) {
896                         if ((g_slots[param].session_options & MM_SESSION_OPTION_PAUSE_OTHERS) || g_slots[param].session_type == MM_SESSION_TYPE_ALARM || g_slots[param].session_type == MM_SESSION_TYPE_NOTIFY || g_slots[param].session_type == MM_SESSION_TYPE_EMERGENCY) {
897                                 if(g_slots[param].current_focus_type != FOCUS_NONE) {
898                                         err = mm_sound_release_focus(g_slots[param].focus_handle, g_slots[param].current_focus_type, NULL);
899                                         if (err) {
900                                                 debug_error("mm_sound_release_focus failed [0x%x]", err);
901                                         }
902                                 }
903                                 if(mm_sound_unregister_focus(g_slots[param].focus_handle) || err) {
904                                         debug_error("Focus clean up failed [0x%x]", err);
905                                         pthread_mutex_unlock(&g_slot_mutex);
906                                         return MM_ERROR_POLICY_INTERNAL;
907                                 }
908                         } else if (~(g_slots[param].session_options & MM_SESSION_OPTION_PAUSE_OTHERS)) {
909                                 err = mm_sound_unset_focus_watch_callback(g_slots[param].focus_wcb_id);
910                                 if (err) {
911                                         debug_error("mm_sound_unset_focus_watch_callback failed [0x%x]", err);
912                                         pthread_mutex_unlock(&g_slot_mutex);
913                                         return MM_ERROR_POLICY_INTERNAL;
914                                 }
915                         }
916                 }
917                 if(mm_sound_remove_device_connected_callback(g_slots[param].subs_id) != MM_ERROR_NONE)
918                         debug_error("mm_sound_remove_device_connected_callback() failed");
919         }
920 #endif
921
922         __mm_sound_mgr_ipc_notify_play_file_end(param);
923
924         debug_msg("Client callback msg_type (instance) : [%d]\n", (int)g_slots[param].param);
925         debug_msg("Handle allocated handle : [0x%08X]\n", g_slots[param].plughandle);
926         err = g_plugins[g_slots[param].pluginid].Destroy(g_slots[param].plughandle);
927         if (err < 0 ) {
928                 debug_critical("[CODEC MGR] Fail to destroy slot number : [%d] err [0x%x]\n", param, err);
929         }
930         memset(&g_slots[param], 0, sizeof(__mmsound_mgr_codec_handle_t));
931         g_slots[param].status = STATUS_IDLE;
932         pthread_mutex_unlock(&g_slot_mutex);
933         debug_msg("[CODEC MGR] Slot_mutex done\n");
934
935         return err;
936 }
937
938 static int _MMSoundMgrCodecGetEmptySlot(int *slot)
939 {
940         int count = 0;
941         int err = MM_ERROR_NONE;
942
943 #ifdef DEBUG_DETAIL
944         debug_enter("\n");
945 #endif
946         debug_msg("Codec slot ID : [%d]\n", *slot);
947         pthread_mutex_lock(&g_slot_mutex);
948 #ifdef DEBUG_DETAIL
949         debug_msg("After Slot_mutex LOCK\n");
950 #endif
951
952         for (count = SOUND_SLOT_START; count < MANAGER_HANDLE_MAX ; count++) {
953                 if (g_slots[count].status == STATUS_IDLE) {
954                         g_slots[count].status = STATUS_SOUND;
955                         break;
956                 }
957         }
958         pthread_mutex_unlock(&g_slot_mutex);
959 #ifdef DEBUG_DETAIL
960         debug_msg("After Slot_mutex UNLOCK\n");
961 #endif
962
963         if (count < MANAGER_HANDLE_MAX) {
964                 debug_msg("New handle allocated (codec slot ID : [%d])\n", count);
965                 *slot = count;
966                 err =  MM_ERROR_NONE;
967         } else {
968                 debug_warning("Handle is full handle : [%d]\n", count);
969                 *slot = -1;
970                 /* Temporal code for reset */
971                 while(count--) {
972                         g_slots[count].status = STATUS_IDLE;
973                 }
974                 err =  MM_ERROR_SOUND_INTERNAL;
975         }
976
977 #ifdef DEBUG_DETAIL
978         debug_leave("\n");
979 #endif
980
981         return err;
982 }
983
984 static int _MMSoundMgrCodecRegisterInterface(MMSoundPluginType *plugin)
985 {
986         int err = MM_ERROR_NONE;
987         int count = 0;
988         void *getinterface = NULL;
989
990 #ifdef DEBUG_DETAIL
991         debug_enter("\n");
992 #endif
993
994         /* find emptry slot */
995         for (count = 0; count < MM_SOUND_SUPPORTED_CODEC_NUM; count++) {
996                 if (g_plugins[count].GetSupportTypes == NULL)
997                         break;
998         }
999
1000         if (count == MM_SOUND_SUPPORTED_CODEC_NUM) {
1001                 debug_critical("The plugin support type is not valid\n");
1002                 return MM_ERROR_COMMON_OUT_OF_RANGE;
1003         }
1004
1005         err = MMSoundPluginGetSymbol(plugin, CODEC_GET_INTERFACE_FUNC_NAME, &getinterface);
1006         if (err != MM_ERROR_NONE) {
1007                 debug_error("Get Symbol CODEC_GET_INTERFACE_FUNC_NAME is fail : %x\n", err);
1008                 goto cleanup;
1009         }
1010         debug_msg("interface[%p] empty_slot[%d]\n", getinterface, count);
1011
1012         err = MMSoundPlugCodecCastGetInterface(getinterface)(&g_plugins[count]);
1013         if (err != MM_ERROR_NONE) {
1014                 debug_error("Get interface fail : %x\n", err);
1015
1016 cleanup:
1017                 /* If error occur, clean interface */
1018                 memset(&g_plugins[count], 0, sizeof(mmsound_codec_interface_t));
1019         } else {
1020                 if (g_plugins[count].SetThreadPool)
1021                         g_plugins[count].SetThreadPool(MMSoundThreadPoolRun);
1022         }
1023
1024 #ifdef DEBUG_DETAIL
1025         debug_leave("\n");
1026 #endif
1027
1028         return err;
1029 }
1030