/* * Copyright (c) 2010 Samsung Electronics, Inc. * All rights reserved. * * This software is a confidential and proprietary information * of Samsung Electronics, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Samsung Electronics. */ /* * setting-common-sound.c * * Created on: Oct 16, 2013 * Author: min-hoyun */ #include #include #include "setting-common-sound.h" #include "util.h" static player_h player; static int is_player_created = 0; static sound_stream_info_h g_stream_info = NULL; /** * @return zero on successful */ int get_sound_mode() { bool is_silent_sound = false; bool is_enable_vibrate = false; system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_SOUND_SILENT_MODE, &is_silent_sound); system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_VIBRATION, &is_enable_vibrate); if (is_silent_sound) { /* mute */ return SOUND_MODE_MUTE; } if (is_enable_vibrate) { /* vibration mode */ return SOUND_MODE_VIBRATE; } return SOUND_MODE_SOUND; } int get_sound_type(sound_stream_type_e stream_type) { sound_stream_info_h stream_info = NULL; int ret = sound_manager_create_stream_information(stream_type, NULL, NULL, &stream_info); if (ret != SOUND_MANAGER_ERROR_NONE) { ERR("sound_manager_create_stream_information() get failed with err[%d, 0x%x]", ret, ret); return ret; } sound_type_e type; sound_manager_get_sound_type(stream_info, &type); sound_manager_destroy_stream_information(stream_info); return type; } int get_sound_internal_type(sound_stream_type_internal_e stream_type) { sound_stream_info_h stream_info = NULL; int ret = sound_manager_create_stream_information_internal(stream_type, NULL, NULL, &stream_info); if (ret != SOUND_MANAGER_ERROR_NONE) { ERR("sound_manager_create_stream_information_internal() get failed with err[%d, 0x%x]", ret, ret); return ret; } sound_type_e type; sound_manager_get_sound_type(stream_info, &type); sound_manager_destroy_stream_information(stream_info); return type; } static void _profile_player_prepare_cb(void *data) { DBG("Player is prepared. Start player"); if (!is_created_player()) { DBG("Player start cancel!!"); return; } if (player != NULL) { player_state_e play_state; player_get_state(player, &play_state); if (play_state == PLAYER_STATE_PAUSED || play_state == PLAYER_STATE_READY) { int err = 0; err = player_start(player); if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */ DBG("Setting - player_start [%d]", err); player_unprepare(player); player_destroy(player); is_player_created = 0; return; } } else { DBG("Player state = %d. do not start", play_state); } } } int is_player_paused() { int ret = FALSE; player_state_e state = -1; player_get_state(player, &state); if (state != PLAYER_STATE_PLAYING) { DBG("Setting - player is paused!!"); ret = TRUE; } return ret; } static int _profile_restart_player(void *data, char *ringtone_file, sound_stream_type_e stream_type, int prelistening_enable) { player_state_e state = -1; int ret = TRUE; player_get_state(player, &state); if (state == PLAYER_STATE_PLAYING || state == PLAYER_STATE_PAUSED) { player_set_play_position(player, 0, true, NULL, NULL); } return ret; } int profile_play_sound(void *data, void *cb, char *ringtone_file, float vol, int stream_type, int prelistening_enable) { DBG("Setting - profile_play_sound is started. path: %s", ringtone_file); DBG("Setting - profile_play_sound is creating."); sound_stream_info_h stream_info = NULL; if (stream_type == SOUND_STREAM_TYPE_RINGTONE_CALL) { int ret = sound_manager_create_stream_information_internal(stream_type, NULL, NULL, &stream_info); if (ret != SOUND_MANAGER_ERROR_NONE) { ERR("sound_manager_create_stream_information_internal() get failed with err[%d, 0x%x]", ret, ret); return ret; } } else { int ret = sound_manager_create_stream_information(stream_type, NULL, NULL, &stream_info); if (ret != SOUND_MANAGER_ERROR_NONE) { ERR("sound_manager_create_stream_information() get failed with err[%d, 0x%x]", ret, ret); return ret; } } int err = 0; err = player_create(&player); if (err != PLAYER_ERROR_NONE) { ERR("Setting - creating the player handle failed[%d]", err); return 0; } DBG("Setting - profile_play_sound is setting sound type."); err = player_set_sound_stream_info(player, stream_info); if (err != PLAYER_ERROR_NONE) { ERR("Setting - error to set stream_info[%d]", err); player_destroy(player); is_player_created = 0; sound_manager_destroy_stream_information(stream_info); return err; } DBG("Setting - profile_play_sound is setting uri."); err = player_set_uri(player, ringtone_file); if (err != PLAYER_ERROR_NONE) { ERR("Setting - error to set attribute---profile_uri[%d]", err); player_destroy(player); is_player_created = 0; sound_manager_destroy_stream_information(stream_info); return err; } DBG("Setting - profile_play_sound is preparing."); err = player_prepare_async(player, _profile_player_prepare_cb, NULL); if (err != PLAYER_ERROR_NONE) { ERR("Setting - realizing the player handle failed[%d]", err); player_destroy(player); is_player_created = 0; sound_manager_destroy_stream_information(stream_info); return err; } DBG("Setting - waiting.."); g_stream_info = stream_info; is_player_created = 1; return err; } int _profile_stop_sound(void *data) { player_state_e state = -1; int ret = TRUE; int prev_behavior = 0; /* instead of do_while(0) and break */ player_get_state(player, &state); if (state == PLAYER_STATE_PLAYING) { if (player_stop(player) != PLAYER_ERROR_NONE) { DBG("Setting - mm player stop failed"); ret = -1; prev_behavior++; /* instead of do_while(0) and break */ } } /*player_unset_completed_cb(*player); */ /*player_unset_interrupted_cb(*player); */ if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) { DBG("Setting - mm player unrealize failed"); ret = -1; prev_behavior++; /* instead of do_while(0) and break */ } if ((prev_behavior == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) { DBG("Setting - mm player destroy failed"); ret = -1; } is_player_created = 0; if (g_stream_info) { sound_manager_destroy_stream_information(g_stream_info); g_stream_info = NULL; } return ret; } int is_created_player() { return is_player_created; } void set_looping(int enable) { if (is_created_player()) player_set_looping(player, enable); else ERR("player is not created!!"); } int _close_player(void *data) { if (is_created_player()) { _profile_stop_sound(data); } return TRUE; } void play_sound(char *file_path, float volume, sound_stream_type_e stream_type) { DBG("Setting - profile_play_sound function start ... : is_created_player() => (%d)", is_created_player()); if (!is_created_player()) { DBG("Setting - profile_play_sound)"); profile_play_sound(NULL, NULL, file_path, volume, stream_type, TRUE); set_looping(TRUE); } } void play_sound_for_sound_mode_setting(char *file_path, float volume, sound_stream_type_e stream_type) { DBG("Setting - play_sound_for_sound_mode_setting function start ..."); if (!is_created_player()) { DBG("Setting - play_sound_for_sound_mode_setting)"); profile_play_sound(NULL, NULL, file_path, volume, stream_type, FALSE); set_looping(TRUE); } else { _profile_restart_player(NULL, file_path, stream_type, FALSE); } }