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 <sys/types.h>
37 #include <mm_message.h>
39 #include "include/mm_sound_private.h"
40 #include "include/mm_sound.h"
41 #include "include/mm_sound_utils.h"
42 #include "include/mm_sound_client.h"
43 #include "include/mm_ipc.h"
44 #include "include/mm_sound_common.h"
47 #include <audio-session-manager.h>
48 #include <mm_session.h>
49 #include <mm_session_private.h>
51 #define MAX_FILE_LENGTH 256
52 #define MAX_MEMORY_SIZE 1048576 /* Max memory size 1024*1024 (1MB) */
53 #define _MIN_SYSTEM_SAMPLERATE 8000
54 #define _MAX_SYSTEM_SAMPLERATE 48000
55 #define MIN_TONE_PLAY_TIME 300
58 volume_callback_fn func;
63 volume_cb_param g_volume_param[VOLUME_TYPE_MAX];
65 static pthread_mutex_t _volume_mutex = PTHREAD_MUTEX_INITIALIZER;
67 #define PCM_LOCK_INTERNAL(LOCK) do { pthread_mutex_lock(LOCK); } while (0)
68 #define PCM_UNLOCK_INTERNAL(LOCK) do { pthread_mutex_unlock(LOCK); } while (0)
69 #define PCM_LOCK_DESTROY_INTERNAL(LOCK) do { pthread_mutex_destroy(LOCK); } while (0)
72 avsys_handle_t audio_handle;
74 ASM_sound_events_t asm_event;
79 pthread_mutex_t pcm_mutex_internal;
80 MMMessageCallback msg_cb;
86 static int _pcm_sound_start (MMSoundPcmHandle_t handle);
87 static int _pcm_sound_stop_internal (MMSoundPcmHandle_t handle);
88 static int _pcm_sound_stop(MMSoundPcmHandle_t handle);
90 static void __sound_pcm_send_message (mm_sound_pcm_t *pcmHandle, int message, int code);
92 static int __validate_volume(volume_type_t type, int value)
99 case VOLUME_TYPE_CALL:
100 case VOLUME_TYPE_VOIP:
101 if (value >= AVSYS_AUDIO_VOLUME_MAX_BASIC) {
105 case VOLUME_TYPE_SYSTEM:
106 case VOLUME_TYPE_MEDIA:
107 case VOLUME_TYPE_ALARM:
108 case VOLUME_TYPE_EXT_JAVA:
109 case VOLUME_TYPE_NOTIFICATION:
110 case VOLUME_TYPE_RINGTONE:
111 if (value >= AVSYS_AUDIO_VOLUME_MAX_MULTIMEDIA) {
115 case VOLUME_TYPE_EXT_ANDROID:
116 if (value >= AVSYS_AUDIO_VOLUME_MAX_SINGLE) {
127 static void volume_changed_cb(keynode_t* node, void* data)
129 volume_cb_param* param = (volume_cb_param*) data;
131 debug_msg("%s changed callback called\n",vconf_keynode_get_name(node));
133 MMSOUND_ENTER_CRITICAL_SECTION( &_volume_mutex )
135 if(param && (param->func != NULL)) {
136 debug_msg("funcion 0x%x\n", param->func);
137 ((volume_callback_fn)param->func)(param->data);
140 MMSOUND_LEAVE_CRITICAL_SECTION( &_volume_mutex )
144 int mm_sound_volume_add_callback(volume_type_t type, volume_callback_fn func, void* user_data)
148 /* Check input param */
149 if (type < 0 || type >= VOLUME_TYPE_MAX) {
150 return MM_ERROR_INVALID_ARGUMENT;
153 debug_warning("callback function is null\n");
154 return MM_ERROR_INVALID_ARGUMENT;
157 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN( &_volume_mutex, MM_ERROR_SOUND_INTERNAL );
159 g_volume_param[type].func = func;
160 g_volume_param[type].data = user_data;
161 g_volume_param[type].type = type;
163 MMSOUND_LEAVE_CRITICAL_SECTION( &_volume_mutex );
165 return _mm_sound_volume_add_callback(type, volume_changed_cb, (void*)&g_volume_param[type]);
169 int mm_sound_volume_remove_callback(volume_type_t type)
173 if(type < 0 || type >=VOLUME_TYPE_MAX) {
174 return MM_ERROR_INVALID_ARGUMENT;
177 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN( &_volume_mutex, MM_ERROR_SOUND_INTERNAL );
179 g_volume_param[type].func = NULL;
180 g_volume_param[type].data = NULL;
181 g_volume_param[type].type = type;
183 MMSOUND_LEAVE_CRITICAL_SECTION( &_volume_mutex );
185 return _mm_sound_volume_remove_callback(type, volume_changed_cb);
189 int mm_sound_get_volume_step(volume_type_t type, int *step)
191 printf("\n**********\n\nTHIS FUNCTION HAS DEFPRECATED [%s]\n\n \
192 use mm_sound_volume_get_step() instead\n\n**********\n", __func__);
193 return mm_sound_volume_get_step(type, step);
197 int mm_sound_volume_get_step(volume_type_t type, int *step)
203 /* Check input param */
205 debug_error("second parameter is null\n");
206 return MM_ERROR_INVALID_ARGUMENT;
208 if (type < 0 || type >= VOLUME_TYPE_MAX) {
209 debug_error("Invalid type value %d\n", (int)type);
210 return MM_ERROR_INVALID_ARGUMENT;
213 err = avsys_audio_get_volume_max_ex((int)type, step);
214 if (AVSYS_FAIL(err)) {
215 err = MM_ERROR_INVALID_ARGUMENT;
219 return MM_ERROR_NONE;
223 int mm_sound_volume_set_value(volume_type_t type, const unsigned int value)
225 int ret = MM_ERROR_NONE;
229 /* Check input param */
230 if (0 > __validate_volume(type, (int)value)) {
231 debug_error("invalid volume type %d, value %u\n", type, value);
232 return MM_ERROR_INVALID_ARGUMENT;
235 ret = _mm_sound_volume_set_value_by_type(type, value);
236 if (ret == MM_ERROR_NONE) {
237 /* update shared memory value */
238 if (AVSYS_FAIL(avsys_audio_set_volume_by_type(type, (int)value))) {
239 debug_error("Can not set volume to shared memory 0x%x\n", ret);
248 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
250 int ret = MM_ERROR_NONE;
254 /* Check input param */
256 return MM_ERROR_INVALID_ARGUMENT;
257 if (type < 0 || type >= VOLUME_TYPE_MAX) {
258 debug_error("invalid volume type value %d\n", type);
259 return MM_ERROR_INVALID_ARGUMENT;
262 ret = _mm_sound_volume_get_value_by_type(type, value);
269 int mm_sound_volume_primary_type_set(volume_type_t type)
272 int ret = MM_ERROR_NONE;
274 /* Check input param */
275 if(type < 0 || type >= VOLUME_TYPE_MAX)
276 return MM_ERROR_INVALID_ARGUMENT;
281 if(AVSYS_FAIL(avsys_audio_set_primary_volume((int)mypid, type))) {
282 debug_error("Can not set primary volume [%d, %d]\n", mypid, type);
283 ret = MM_ERROR_SOUND_INTERNAL;
291 int mm_sound_volume_primary_type_clear()
294 int ret = MM_ERROR_NONE;
299 if(AVSYS_FAIL(avsys_audio_clear_primary_volume((int)mypid))) {
300 debug_error("Can not clear primary volume [%d]\n", mypid);
301 ret = MM_ERROR_SOUND_INTERNAL;
309 int mm_sound_volume_get_current_playing_type(volume_type_t *type)
311 int result = AVSYS_STATE_SUCCESS;
312 int voltype = AVSYS_AUDIO_VOLUME_TYPE_RINGTONE;
313 int ret = MM_ERROR_NONE;
317 /* Check input param */
319 return MM_ERROR_INVALID_ARGUMENT;
322 result = avsys_audio_get_current_playing_volume_type(&voltype);
325 case AVSYS_STATE_SUCCESS:
328 case AVSYS_STATE_ERR_ALLOCATION:
329 ret = MM_ERROR_SOUND_VOLUME_NO_INSTANCE;
331 case AVSYS_STATE_ERR_INVALID_MODE:
332 ret = MM_ERROR_SOUND_VOLUME_CAPTURE_ONLY;
335 ret = MM_ERROR_SOUND_INTERNAL;
338 debug_log("avsys_audio_get_current_playing_volume_type() = %x, ret = %x, type = %d\n", result, ret, *type);
344 ///////////////////////////////////
345 //// MMSOUND PCM APIs
346 ///////////////////////////////////
349 int _get_asm_event_type(ASM_sound_events_t *type)
351 int sessionType = MM_SESSION_TYPE_SHARE;
352 ASM_sound_events_t asm_event;
355 return MM_ERROR_SOUND_INVALID_POINTER;
357 /* read session type */
358 if(_mm_session_util_read_type(-1, &sessionType) < 0) {
359 debug_log("Read Session Type failed. Set default \"Share\" type\n");
360 sessionType = MM_SESSION_TYPE_SHARE;
361 if(mm_session_init(sessionType) < 0) {
362 debug_error("mm_session_init() failed\n");
363 return MM_ERROR_SOUND_INTERNAL;
367 /* convert MM_SESSION_TYPE to ASM_EVENT_TYPE */
370 case MM_SESSION_TYPE_SHARE:
371 asm_event = ASM_EVENT_SHARE_MMSOUND;
373 case MM_SESSION_TYPE_EXCLUSIVE:
374 asm_event = ASM_EVENT_EXCLUSIVE_MMSOUND;
376 case MM_SESSION_TYPE_NOTIFY:
377 asm_event = ASM_EVENT_NOTIFY;
379 case MM_SESSION_TYPE_ALARM:
380 asm_event = ASM_EVENT_ALARM;
382 case MM_SESSION_TYPE_CALL:
383 asm_event = ASM_EVENT_CALL;
385 case MM_SESSION_TYPE_VIDEOCALL:
386 asm_event = ASM_EVENT_VIDEOCALL;
388 case MM_SESSION_TYPE_RICH_CALL:
389 asm_event = ASM_EVENT_RICH_CALL;
391 case MM_SESSION_TYPE_EMERGENCY:
392 asm_event = ASM_EVENT_EMERGENCY;
395 debug_error("Unexpected %d\n", sessionType);
396 return MM_ERROR_SOUND_INTERNAL;
400 return MM_ERROR_NONE;
403 static void __sound_pcm_send_message (mm_sound_pcm_t *pcmHandle, int message, int code)
406 if (pcmHandle->msg_cb) {
407 MMMessageParamType msg;
408 msg.union_type = MM_MSG_UNION_CODE;
411 debug_log ("calling msg callback(%p) with message(%d), code(%d), msg callback param(%p)\n",
412 pcmHandle->msg_cb, message, msg.code, pcmHandle->msg_cb_param);
413 ret = pcmHandle->msg_cb(message, &msg, pcmHandle->msg_cb_param);
414 debug_log ("msg callback returned (%d)\n", ret);
416 debug_log ("No pcm msg callback\n");
420 ASM_cb_result_t sound_pcm_asm_callback(int handle, ASM_event_sources_t event_src, ASM_sound_commands_t command, unsigned int sound_status, void *cb_data)
422 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t *)cb_data;
423 ASM_cb_result_t cb_res = ASM_CB_RES_IGNORE;
425 /* Check input param */
426 if(pcmHandle == NULL) {
427 debug_error("sound_pcm_asm_callback cb_data is null\n");
431 debug_log ("command = %d, handle = %p, is_started = %d\n",command, pcmHandle, pcmHandle->is_started);
434 case ASM_COMMAND_STOP:
436 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
437 _pcm_sound_stop_internal (pcmHandle);
438 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
439 cb_res = ASM_CB_RES_PAUSE;
442 case ASM_COMMAND_RESUME:
443 cb_res = ASM_CB_RES_IGNORE;
446 case ASM_COMMAND_PAUSE:
447 case ASM_COMMAND_PLAY:
448 case ASM_COMMAND_NONE:
449 debug_error ("Not an expected case!!!!\n");
453 /* execute user callback if callback available */
454 __sound_pcm_send_message (pcmHandle, MM_MESSAGE_SOUND_PCM_INTERRUPTED, event_src);
460 int mm_sound_pcm_capture_open(MMSoundPcmHandle_t *handle, const unsigned int rate, MMSoundPcmChannel_t channel, MMSoundPcmFormat_t format)
462 avsys_audio_param_t param;
463 mm_sound_pcm_t *pcmHandle = NULL;
465 int result = AVSYS_STATE_SUCCESS;
469 memset(¶m, 0, sizeof(avsys_audio_param_t));
472 if (rate < _MIN_SYSTEM_SAMPLERATE || rate > _MAX_SYSTEM_SAMPLERATE) {
473 debug_error("unsupported sample rate %u", rate);
474 return MM_ERROR_SOUND_DEVICE_INVALID_SAMPLERATE;
476 param.samplerate = rate;
481 case MMSOUND_PCM_MONO:
484 case MMSOUND_PCM_STEREO:
489 debug_error("Unsupported channel type\n");
490 return MM_ERROR_SOUND_DEVICE_INVALID_CHANNEL;
496 param.format = AVSYS_AUDIO_FORMAT_8BIT;
498 case MMSOUND_PCM_S16_LE:
499 param.format = AVSYS_AUDIO_FORMAT_16BIT;
502 debug_error("Unsupported format type\n");
503 return MM_ERROR_SOUND_DEVICE_INVALID_FORMAT;
506 pcmHandle = calloc(sizeof(mm_sound_pcm_t), 1);
507 if(pcmHandle == NULL)
508 return MM_ERROR_OUT_OF_MEMORY;
510 ret_mutex = pthread_mutex_init(&pcmHandle->pcm_mutex_internal, NULL);
514 return MM_ERROR_OUT_OF_MEMORY;
518 /* get session type */
519 if(MM_ERROR_NONE != _get_asm_event_type(&pcmHandle->asm_event)) {
520 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
522 return MM_ERROR_POLICY_INTERNAL;
525 if(pcmHandle->asm_event != ASM_EVENT_CALL && pcmHandle->asm_event != ASM_EVENT_VIDEOCALL) {
526 if(!ASM_register_sound(-1, &pcmHandle->asm_handle, pcmHandle->asm_event,
527 /* ASM_STATE_PLAYING */ ASM_STATE_NONE, sound_pcm_asm_callback, (void*)pcmHandle, ASM_RESOURCE_NONE, &errorcode))
529 debug_error("ASM_register_sound() failed 0x%x\n", errorcode);
530 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
532 return MM_ERROR_POLICY_BLOCKED;
537 param.mode = AVSYS_AUDIO_MODE_INPUT;
538 param.vol_type = AVSYS_AUDIO_VOLUME_TYPE_SYSTEM; //dose not effect at capture mode
539 param.priority = AVSYS_AUDIO_PRIORITY_0; //This does not affect anymore.
540 result = avsys_audio_open(¶m, &pcmHandle->audio_handle, &size);
541 if(AVSYS_FAIL(result)) {
542 debug_error("Device Open Error 0x%x\n", result);
543 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
545 return MM_ERROR_SOUND_DEVICE_NOT_OPENED;
548 pcmHandle->is_playback = false;
550 /* Set handle to return */
551 *handle = (MMSoundPcmHandle_t)pcmHandle;
556 static int _pcm_sound_start (MMSoundPcmHandle_t handle)
558 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
564 /* Check input param */
565 if(pcmHandle == NULL) {
566 debug_error ("Handle is null, return Invalid Argument\n");
567 ret = MM_ERROR_INVALID_ARGUMENT;
571 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
573 if (pcmHandle->skip_session == false) {
574 /* ASM set state to PLAYING */
575 if (!ASM_set_sound_state(pcmHandle->asm_handle, pcmHandle->asm_event, ASM_STATE_PLAYING, ASM_RESOURCE_NONE, &errorcode)) {
576 debug_error("ASM_set_sound_state(PLAYING) failed 0x%x\n", errorcode);
577 ret = MM_ERROR_POLICY_BLOCKED;
583 pcmHandle->is_started = true;
586 ret = avsys_audio_cork(pcmHandle->audio_handle, 0);
589 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
598 int mm_sound_pcm_capture_start(MMSoundPcmHandle_t handle)
600 int ret = MM_ERROR_NONE;
604 ret = _pcm_sound_start (handle);
605 if (ret != MM_ERROR_NONE) {
606 debug_error ("_pcm_sound_start() failed (%x)\n", ret);
615 static int _pcm_sound_stop_internal (MMSoundPcmHandle_t handle)
617 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
619 /* Check input param */
620 if(pcmHandle == NULL)
621 return MM_ERROR_INVALID_ARGUMENT;
624 if (pcmHandle->is_started == false) {
625 debug_warning ("Can't stop because not started\n");
626 return MM_ERROR_SOUND_INVALID_STATE;
629 /* Drain if playback mode */
630 if (pcmHandle->is_playback) {
631 if(AVSYS_FAIL(avsys_audio_drain(pcmHandle->audio_handle))) {
632 debug_error("drain failed\n");
637 pcmHandle->is_started = false;
640 return avsys_audio_cork(pcmHandle->audio_handle, 1);
643 static int _pcm_sound_stop(MMSoundPcmHandle_t handle)
645 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
647 int ret = MM_ERROR_NONE;
651 /* Check input param */
652 if(pcmHandle == NULL) {
653 debug_error ("Handle is null, return Invalid Argument\n");
654 ret = MM_ERROR_INVALID_ARGUMENT;
658 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
660 /* Do stop procedure */
661 ret = _pcm_sound_stop_internal(handle);
662 if (ret == MM_ERROR_NONE) {
663 /* Set ASM State to STOP */
664 if (pcmHandle->skip_session == false) {
665 if (!ASM_set_sound_state(pcmHandle->asm_handle, pcmHandle->asm_event, ASM_STATE_STOP, ASM_RESOURCE_NONE, &errorcode)) {
666 debug_error("ASM_set_sound_state(STOP) failed 0x%x\n", errorcode);
667 ret = MM_ERROR_POLICY_BLOCKED;
673 debug_log ("pcm sound [%p] stopped success!!!\n", handle);
676 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
684 int mm_sound_pcm_capture_stop(MMSoundPcmHandle_t handle)
689 ret = _pcm_sound_stop(handle);
697 int mm_sound_pcm_capture_read(MMSoundPcmHandle_t handle, void *buffer, const unsigned int length )
700 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
702 /* Check input param */
703 if(pcmHandle == NULL) {
704 debug_error ("Handle is null, return Invalid Argument\n");
705 ret = MM_ERROR_INVALID_ARGUMENT;
708 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
711 debug_error("Invalid buffer pointer\n");
712 ret = MM_ERROR_SOUND_INVALID_POINTER;
716 debug_error ("length is 0, return 0\n");
721 /* Check State : return fail if not started */
722 if (!pcmHandle->is_started) {
723 /* not started, return fail */
724 debug_error ("Not started yet, return Invalid State \n");
725 ret = MM_ERROR_SOUND_INVALID_STATE;
730 ret = avsys_audio_read(pcmHandle->audio_handle, buffer, length);
733 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
739 int mm_sound_pcm_capture_close(MMSoundPcmHandle_t handle)
741 int result = MM_ERROR_NONE;
742 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
746 /* Check input param */
747 if(pcmHandle == NULL) {
748 debug_error ("Handle is null, return Invalid Argument\n");
749 result = MM_ERROR_INVALID_ARGUMENT;
752 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
754 result = avsys_audio_close(pcmHandle->audio_handle);
755 if(AVSYS_FAIL(result)) {
756 debug_error("handle close failed 0x%X", result);
757 result = MM_ERROR_SOUND_INTERNAL;
762 if(pcmHandle->asm_event != ASM_EVENT_CALL && pcmHandle->asm_event != ASM_EVENT_VIDEOCALL) {
763 if(!ASM_unregister_sound(pcmHandle->asm_handle, pcmHandle->asm_event, &errorcode)) {
764 debug_error("ASM_unregister failed in %s with 0x%x\n", __func__, errorcode);
765 result = MM_ERROR_SOUND_INTERNAL;
770 debug_log ("pcm capture sound [%p] closed success!!!\n", handle);
773 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
777 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
786 int mm_sound_pcm_set_message_callback (MMSoundPcmHandle_t handle, MMMessageCallback callback, void *user_param)
788 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
790 if(pcmHandle == NULL || callback == NULL)
791 return MM_ERROR_INVALID_ARGUMENT;
793 pcmHandle->msg_cb = callback;
794 pcmHandle->msg_cb_param = user_param;
796 debug_log ("set pcm message callback (%p,%p)\n", callback, user_param);
798 return MM_ERROR_NONE;
803 int mm_sound_pcm_play_open_ex (MMSoundPcmHandle_t *handle, const unsigned int rate, MMSoundPcmChannel_t channel, MMSoundPcmFormat_t format, int volume_config, ASM_sound_events_t asm_event)
805 avsys_audio_param_t param;
806 mm_sound_pcm_t *pcmHandle = NULL;
808 int result = AVSYS_STATE_SUCCESS;
810 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
814 memset(¶m, 0, sizeof(avsys_audio_param_t));
816 /* Check input param */
817 if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
818 debug_error("Volume type is invalid %d\n", volume_type);
819 return MM_ERROR_INVALID_ARGUMENT;
821 if (rate < _MIN_SYSTEM_SAMPLERATE || rate > _MAX_SYSTEM_SAMPLERATE) {
822 debug_error("unsupported sample rate %u", rate);
823 return MM_ERROR_SOUND_DEVICE_INVALID_SAMPLERATE;
825 param.samplerate = rate;
830 case MMSOUND_PCM_MONO:
833 case MMSOUND_PCM_STEREO:
837 debug_error("Unsupported channel type\n");
838 return MM_ERROR_SOUND_DEVICE_INVALID_CHANNEL;
844 param.format = AVSYS_AUDIO_FORMAT_8BIT;
846 case MMSOUND_PCM_S16_LE:
847 param.format = AVSYS_AUDIO_FORMAT_16BIT;
850 debug_error("Unsupported format type\n");
851 return MM_ERROR_SOUND_DEVICE_INVALID_FORMAT;
854 pcmHandle = calloc(sizeof(mm_sound_pcm_t),1);
855 if(pcmHandle == NULL)
856 return MM_ERROR_OUT_OF_MEMORY;
858 ret_mutex = pthread_mutex_init(&pcmHandle->pcm_mutex_internal, NULL);
862 return MM_ERROR_OUT_OF_MEMORY;
866 debug_log ("session start : input asm_event = %d-------------\n", asm_event);
867 if (asm_event == ASM_EVENT_MONITOR) {
868 debug_log ("Skip SESSION for event (%d)\n", asm_event);
869 pcmHandle->skip_session = true;
870 } else if (asm_event == ASM_EVENT_NONE) {
871 /* get session type */
872 if(MM_ERROR_NONE != _get_asm_event_type(&pcmHandle->asm_event)) {
873 debug_error ("_get_asm_event_type failed....\n");
874 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
876 return MM_ERROR_POLICY_INTERNAL;
879 if(pcmHandle->asm_event != ASM_EVENT_CALL && pcmHandle->asm_event != ASM_EVENT_VIDEOCALL) {
881 if(!ASM_register_sound(-1, &pcmHandle->asm_handle, pcmHandle->asm_event,
882 ASM_STATE_NONE, sound_pcm_asm_callback, (void*)pcmHandle, ASM_RESOURCE_NONE, &errorcode)) {
883 debug_error("ASM_register_sound() failed 0x%x\n", errorcode);
884 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
886 return MM_ERROR_POLICY_BLOCKED;
890 /* register asm using asm_event input */
891 if(!ASM_register_sound(-1, &pcmHandle->asm_handle, asm_event,
892 ASM_STATE_NONE, NULL, (void*)pcmHandle, ASM_RESOURCE_NONE, &errorcode)) {
893 debug_error("ASM_register_sound() failed 0x%x\n", errorcode);
894 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
896 return MM_ERROR_POLICY_BLOCKED;
900 param.mode = AVSYS_AUDIO_MODE_OUTPUT;
901 param.vol_type = volume_config;
902 param.priority = AVSYS_AUDIO_PRIORITY_0;
904 // avsys_audio_ampon();
907 debug_log ("avsys open -------------\n");
908 result = avsys_audio_open(¶m, &pcmHandle->audio_handle, &size);
909 if(AVSYS_FAIL(result)) {
910 debug_error("Device Open Error 0x%x\n", result);
911 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
913 return MM_ERROR_SOUND_DEVICE_NOT_OPENED;
916 pcmHandle->is_playback = true;
918 /* Set handle to return */
919 *handle = (MMSoundPcmHandle_t)pcmHandle;
925 int mm_sound_pcm_play_open_no_session(MMSoundPcmHandle_t *handle, const unsigned int rate, MMSoundPcmChannel_t channel, MMSoundPcmFormat_t format)
927 return mm_sound_pcm_play_open_ex (handle, rate, channel, format, VOLUME_TYPE_SYSTEM, ASM_EVENT_MONITOR);
931 int mm_sound_pcm_play_open(MMSoundPcmHandle_t *handle, const unsigned int rate, MMSoundPcmChannel_t channel, MMSoundPcmFormat_t format, const volume_type_t vol_type)
933 return mm_sound_pcm_play_open_ex (handle, rate, channel, format, vol_type, ASM_EVENT_NONE);
937 int mm_sound_pcm_play_start(MMSoundPcmHandle_t handle)
942 ret = _pcm_sound_start (handle);
951 int mm_sound_pcm_play_stop(MMSoundPcmHandle_t handle)
956 ret = _pcm_sound_stop(handle);
964 int mm_sound_pcm_play_write(MMSoundPcmHandle_t handle, void* ptr, unsigned int length_byte)
967 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
969 /* Check input param */
970 if(pcmHandle == NULL) {
971 debug_error ("Handle is null, return Invalid Argument\n");
972 ret = MM_ERROR_INVALID_ARGUMENT;
976 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
979 debug_error("Invalid buffer pointer\n");
980 ret = MM_ERROR_SOUND_INVALID_POINTER;
983 if(length_byte == 0 ) {
984 debug_error ("length is 0, return 0\n");
989 /* Check State : return fail if not started */
990 if (!pcmHandle->is_started) {
991 /* not started, return fail */
992 debug_error ("Not started yet, return Invalid State \n");
993 ret = MM_ERROR_SOUND_INVALID_STATE;
998 ret = avsys_audio_write(pcmHandle->audio_handle, ptr, length_byte);
1001 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
1007 int mm_sound_pcm_play_close(MMSoundPcmHandle_t handle)
1009 int result = MM_ERROR_NONE;
1010 mm_sound_pcm_t *pcmHandle = (mm_sound_pcm_t*)handle;
1014 /* Check input param */
1015 if(pcmHandle == NULL) {
1016 debug_error ("Handle is null, return Invalid Argument\n");
1017 result = MM_ERROR_INVALID_ARGUMENT;
1020 PCM_LOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
1021 /* Drain if needed */
1022 if (pcmHandle->is_started) {
1023 /* stop() is not called before close(), drain is needed */
1024 if(AVSYS_FAIL(avsys_audio_drain(pcmHandle->audio_handle))) {
1025 debug_error("drain failed\n");
1026 result = MM_ERROR_SOUND_INTERNAL;
1030 pcmHandle->is_started = false;
1032 result = avsys_audio_close(pcmHandle->audio_handle);
1033 if(AVSYS_FAIL(result)) {
1034 debug_error("handle close failed 0x%X", result);
1035 result = MM_ERROR_SOUND_INTERNAL;
1039 if (pcmHandle->skip_session == false) {
1040 /* Unregister ASM */
1041 if(pcmHandle->asm_event != ASM_EVENT_CALL && pcmHandle->asm_event != ASM_EVENT_VIDEOCALL) {
1042 if(!ASM_unregister_sound(pcmHandle->asm_handle, pcmHandle->asm_event, &errorcode)) {
1043 debug_error("ASM_unregister failed in %s with 0x%x\n",__func__, errorcode);
1044 result = MM_ERROR_SOUND_INTERNAL;
1050 debug_log ("pcm play sound [%p] closed success!!!\n", handle);
1054 PCM_UNLOCK_INTERNAL(&pcmHandle->pcm_mutex_internal);
1058 PCM_LOCK_DESTROY_INTERNAL(&pcmHandle->pcm_mutex_internal);
1066 ///////////////////////////////////
1067 //// MMSOUND PLAY APIs
1068 ///////////////////////////////////
1069 static inline void _mm_sound_fill_play_param(MMSoundPlayParam *param, const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int priority, int handle_route)
1071 param->filename = filename;
1072 param->volume = 0; //volume value dose not effect anymore
1073 param->callback = callback;
1076 param->volume_config = volume_config;
1077 param->priority = priority;
1078 param->handle_route = handle_route;
1082 int mm_sound_play_loud_solo_sound_no_restore(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
1084 MMSoundPlayParam param = { 0, };
1086 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, AVSYS_AUDIO_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_SPEAKER_NO_RESTORE);
1087 return mm_sound_play_sound_ex(¶m, handle);
1091 int mm_sound_play_loud_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
1093 MMSoundPlayParam param = { 0, };
1095 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, AVSYS_AUDIO_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_SPEAKER);
1096 return mm_sound_play_sound_ex(¶m, handle);
1100 int mm_sound_play_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
1102 MMSoundPlayParam param = { 0, };
1104 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, AVSYS_AUDIO_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
1105 return mm_sound_play_sound_ex(¶m, handle);
1109 int mm_sound_play_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
1111 MMSoundPlayParam param = { 0, };
1113 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, AVSYS_AUDIO_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
1114 return mm_sound_play_sound_ex(¶m, handle);
1118 int mm_sound_play_sound_ex(MMSoundPlayParam *param, int *handle)
1122 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
1126 /* Check input param */
1127 if (param == NULL) {
1128 debug_error("param is null\n");
1129 return MM_ERROR_INVALID_ARGUMENT;
1131 if (param->filename == NULL) {
1132 debug_error("filename is NULL\n");
1133 return MM_ERROR_SOUND_FILE_NOT_FOUND;
1135 if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
1136 debug_error("Volume type is invalid %d\n", volume_type);
1137 return MM_ERROR_INVALID_ARGUMENT;
1141 err = MMSoundClientPlaySound(param, 0, 0, &lhandle);
1143 debug_error("Failed to play sound\n");
1147 /* Set handle to return */
1151 debug_critical("The sound hadle cannot be get [%d]\n", lhandle);
1155 return MM_ERROR_NONE;
1160 int mm_sound_stop_sound(int handle)
1167 err = MMSoundClientStopSound(handle);
1169 debug_error("Fail to stop sound\n");
1174 return MM_ERROR_NONE;
1177 ///////////////////////////////////
1178 //// MMSOUND TONE APIs
1179 ///////////////////////////////////
1181 int mm_sound_play_tone (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle)
1184 int err = MM_ERROR_NONE;
1185 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
1189 /* Check input param */
1190 if (duration < -1) {
1191 debug_error("number is invalid %d\n", duration);
1192 return MM_ERROR_INVALID_ARGUMENT;
1194 if (num < MM_SOUND_TONE_DTMF_0 || num >= MM_SOUND_TONE_NUM) {
1195 debug_error("TONE Value is invalid %d\n", num);
1196 return MM_ERROR_INVALID_ARGUMENT;
1198 if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
1199 debug_error("Volume type is invalid %d\n", volume_type);
1200 return MM_ERROR_INVALID_ARGUMENT;
1202 if (volume < 0.0 || volume > 1.0) {
1203 debug_error("Volume Value is invalid %d\n", volume);
1204 return MM_ERROR_INVALID_ARGUMENT;
1208 debug_msg("Call MMSoundClientPlayTone\n");
1209 err = MMSoundClientPlayTone(num, volume_config, volume, duration, &lhandle);
1211 debug_error("Failed to play sound\n");
1215 /* Set handle to return */
1219 debug_critical("The sound handle cannot be get [%d]\n", lhandle);
1222 return MM_ERROR_NONE;
1225 ///////////////////////////////////
1226 //// MMSOUND ROUTING APIs
1227 ///////////////////////////////////
1229 int mm_sound_set_path(int gain, int output, int input, int option)
1235 debug_msg("gain: 0x%02X, output: %d, input: %d, option: 0x%x\n", gain, output, input, option);
1237 err = avsys_audio_set_path_ex( gain, output, input, option);
1239 debug_error("avsys_audio_set_path() failed\n");
1240 return MM_ERROR_SOUND_INTERNAL;
1244 return MM_ERROR_NONE;
1248 int mm_sound_get_path(int *gain, int *output, int *input, int *option)
1254 err = avsys_audio_get_path_ex( gain, output, input, option);
1256 debug_error("SoundCtlPathGet() failed\n");
1257 return MM_ERROR_SOUND_INTERNAL;
1260 debug_msg("gain: 0x%02X, output: %d, input: %d, option: 0x%x\n", *gain, *output, *input, *option);
1263 return MM_ERROR_NONE;
1268 USE_PA_SINK_ALSA = 0,
1272 int mm_sound_route_set_system_policy (system_audio_route_t route)
1274 return MM_ERROR_NONE;
1279 int mm_sound_route_get_system_policy (system_audio_route_t *route)
1281 return MM_ERROR_NONE;
1286 int mm_sound_route_get_a2dp_status (bool *connected, char **bt_name)
1288 int ret = MM_ERROR_NONE;
1292 if (connected == NULL || bt_name == NULL) {
1293 debug_error ("argument is not valid\n");
1294 return MM_ERROR_INVALID_ARGUMENT;
1297 ret = MMSoundClientIsBtA2dpOn (connected, bt_name);
1298 debug_msg ("connected=[%d] bt_name[%s]\n", *connected, *bt_name);
1300 debug_error("MMSoundClientIsBtA2dpOn() Failed\n");
1310 int mm_sound_route_get_playing_device(system_audio_route_device_t *dev)
1312 mm_sound_device_in device_in = MM_SOUND_DEVICE_IN_NONE;
1313 mm_sound_device_out device_out = MM_SOUND_DEVICE_OUT_NONE;
1316 return MM_ERROR_INVALID_ARGUMENT;
1318 if(MM_ERROR_NONE != mm_sound_get_active_device(&device_in, &device_out)) {
1319 debug_error("Can not get active device info\n");
1320 return MM_ERROR_SOUND_INTERNAL;
1325 case MM_SOUND_DEVICE_OUT_SPEAKER:
1326 *dev = SYSTEM_AUDIO_ROUTE_PLAYBACK_DEVICE_HANDSET;
1328 case MM_SOUND_DEVICE_OUT_BT_A2DP:
1329 *dev = SYSTEM_AUDIO_ROUTE_PLAYBACK_DEVICE_BLUETOOTH;
1331 case MM_SOUND_DEVICE_OUT_WIRED_ACCESSORY:
1332 *dev = SYSTEM_AUDIO_ROUTE_PLAYBACK_DEVICE_EARPHONE;
1335 *dev = SYSTEM_AUDIO_ROUTE_PLAYBACK_DEVICE_NONE;
1339 return MM_ERROR_NONE;
1343 int mm_sound_route_add_change_callback(audio_route_policy_changed_callback_fn func, void *user_data)
1346 return MM_ERROR_NONE;
1350 int mm_sound_route_remove_change_callback()
1353 return MM_ERROR_NONE;
1356 #endif /* PULSE_CLIENT */
1359 int mm_sound_system_get_capture_status(system_audio_capture_status_t *status)
1361 int err = AVSYS_STATE_SUCCESS;
1365 return MM_ERROR_INVALID_ARGUMENT;
1367 err = avsys_audio_get_capture_status(&on_capture);
1369 debug_error("Can not get capture status with 0x%x\n", err);
1370 return MM_ERROR_SOUND_INTERNAL;
1374 *status = SYSTEM_AUDIO_CAPTURE_ACTIVE;
1376 *status = SYSTEM_AUDIO_CAPTURE_NONE;
1378 return MM_ERROR_NONE;
1382 int mm_sound_is_route_available(mm_sound_route route, bool *is_available)
1384 int ret = MM_ERROR_NONE;
1388 if (!_mm_sound_is_route_valid(route)) {
1389 debug_error("route is invalid %d\n", route);
1390 return MM_ERROR_INVALID_ARGUMENT;
1394 debug_error("is_available is invalid\n");
1395 return MM_ERROR_INVALID_ARGUMENT;
1398 ret = _mm_sound_client_is_route_available(route, is_available);
1400 debug_error("Can not check given route is available\n");
1408 int mm_sound_foreach_available_route_cb(mm_sound_available_route_cb available_route_cb, void *user_data)
1410 int ret = MM_ERROR_NONE;
1414 if (!available_route_cb)
1416 debug_error("available_route_cb is invalid\n");
1417 return MM_ERROR_INVALID_ARGUMENT;
1420 ret = _mm_sound_client_foreach_available_route_cb(available_route_cb, user_data);
1422 debug_error("Can not set foreach available route callback\n");
1430 int mm_sound_set_active_route(mm_sound_route route)
1432 int ret = MM_ERROR_NONE;
1436 if (!_mm_sound_is_route_valid(route)) {
1437 debug_error("route is invalid %d\n", route);
1438 return MM_ERROR_INVALID_ARGUMENT;
1441 ret = _mm_sound_client_set_active_route(route);
1443 debug_error("Can not set active route\n");
1452 int mm_sound_get_active_device(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
1454 int ret = MM_ERROR_NONE;
1458 if (device_in == NULL || device_out == NULL) {
1459 debug_error("argument is not valid\n");
1460 return MM_ERROR_INVALID_ARGUMENT;
1463 ret = _mm_sound_client_get_active_device(device_in, device_out);
1465 debug_error("Can not add active device callback\n");
1473 int mm_sound_add_active_device_changed_callback(mm_sound_active_device_changed_cb func, void *user_data)
1475 int ret = MM_ERROR_NONE;
1480 debug_error("argument is not valid\n");
1481 return MM_ERROR_INVALID_ARGUMENT;
1484 ret = _mm_sound_client_add_active_device_changed_callback(func, user_data);
1486 debug_error("Can not add active device changed callback\n");
1494 int mm_sound_remove_active_device_changed_callback(void)
1496 int ret = MM_ERROR_NONE;
1500 ret = _mm_sound_client_remove_active_device_changed_callback();
1502 debug_error("Can not remove active device changed callback\n");
1510 int mm_sound_add_available_route_changed_callback(mm_sound_available_route_changed_cb func, void *user_data)
1512 int ret = MM_ERROR_NONE;
1517 debug_error("argument is not valid\n");
1518 return MM_ERROR_INVALID_ARGUMENT;
1521 ret = _mm_sound_client_add_available_route_changed_callback(func, user_data);
1523 debug_error("Can not add available route changed callback\n");
1531 int mm_sound_remove_available_route_changed_callback(void)
1533 int ret = MM_ERROR_NONE;
1537 ret = _mm_sound_client_remove_available_route_changed_callback();
1539 debug_error("Can not remove available route changed callback\n");
1546 __attribute__ ((destructor))
1547 void __mmfsnd_finalize(void)
1551 MMSoundClientCallbackFini();
1556 __attribute__ ((constructor))
1557 void __mmfsnd_initialize(void)