2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * setting-common-sound.c
20 * Created on: Oct 16, 2013
25 #include <vconf-keys.h>
27 #include "setting-common-sound.h"
30 static player_h player;
31 static int is_player_created = 0;
32 static sound_type_e _sound_type;
34 /*player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type); */
37 * @return zero on successful
43 int is_enable_sound = 0;
44 int is_enable_vibrate = 0;
46 vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_enable_sound);
47 vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_enable_vibrate);
49 if (is_enable_sound && !is_enable_vibrate) { /* sound mode */
50 mode = SOUND_MODE_SOUND;
51 } else if (!is_enable_sound && is_enable_vibrate) { /* vibration mode */
52 mode = SOUND_MODE_VIBRATE;
53 } else if (!is_enable_sound && !is_enable_vibrate) { /* mute */
54 mode = SOUND_MODE_MUTE;
59 static void _profile_player_prepare_cb(void *data)
61 DBG("Player is prepared. Start player");
63 if (!is_created_player()) {
64 DBG("Player start cancel!!");
69 player_state_e play_state;
70 player_get_state(player, &play_state);
71 if (play_state == PLAYER_STATE_PAUSED || play_state == PLAYER_STATE_READY) {
73 err = player_start(player);
74 if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */
75 DBG("Setting - player_start [%d]", err);
76 player_unprepare(player);
77 player_destroy(player);
78 is_player_created = 0;
82 DBG("Player state = %d. do not start", play_state);
87 int is_player_paused()
90 player_state_e state = -1;
91 player_get_state(player, &state);
92 if (state != PLAYER_STATE_PLAYING) {
93 DBG("Setting - player is paused!!");
99 static int _profile_restart_player(void *data, char *ringtone_file, sound_type_e sound_type, int prelistening_enable)
101 player_state_e state = -1;
103 int prev_behavior = 0; /* instead of do_while(0) and break */
105 player_get_state(player, &state);
106 if (state == PLAYER_STATE_PLAYING) {
107 if (player_stop(player) != PLAYER_ERROR_NONE) {
108 DBG("Setting - mm player stop failed");
110 prev_behavior++; /* instead of do_while(0) and break */
114 if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
115 DBG("Setting - mm player unrealize failed");
117 prev_behavior++; /* instead of do_while(0) and break */
120 if (prev_behavior == 0) {
122 err = player_set_uri(player, ringtone_file);
123 if (err != PLAYER_ERROR_NONE) {
124 DBG("Setting - error to set attribute---profile_uri[%d]", err);
125 player_destroy(player);
126 is_player_created = 0;
131 if (prelistening_enable && sound_type != SOUND_TYPE_SYSTEM) {
132 DBG("Setting - profile_play_sound is sessioning..");
134 player_prelistening_mode_e mode = _get_prelistening_mode(sound_type);
136 err = player_set_prelistening_mode(player, mode);
137 if (err != PLAYER_ERROR_NONE) {
138 DBG("Setting - error to player_set_session_prelistening[%d]", err);
139 player_destroy(player);
140 is_player_created = 0;
145 err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
146 if (err != PLAYER_ERROR_NONE) {
147 DBG("Setting - realizing the player handle failed[%d]", err);
148 player_destroy(player);
149 is_player_created = 0;
156 player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type)
158 player_prelistening_mode_e mode = mode = PLAYER_PRELISTENING_MODE_RINGTONE;
159 switch (sound_type) {
160 case SOUND_TYPE_MEDIA:
161 mode = PLAYER_PRELISTENING_MODE_MEDIA;
163 case SOUND_TYPE_RINGTONE:
164 mode = PLAYER_PRELISTENING_MODE_RINGTONE;
166 case SOUND_TYPE_NOTIFICATION:
167 mode = PLAYER_PRELISTENING_MODE_NOTIFICATION;
174 int profile_play_sound(void *data, void *cb, char *ringtone_file, float vol, sound_type_e sound_type, int prelistening_enable)
176 DBG("Setting - profile_play_sound is started. path: %s", ringtone_file);
177 DBG("Setting - profile_play_sound is creating.");
180 err = player_create(&player);
181 if (err != PLAYER_ERROR_NONE) {
182 DBG("Setting - creating the player handle failed[%d]", err);
186 DBG("Setting - profile_play_sound is setting sound type.");
188 err = player_set_sound_type(player, sound_type);
189 if (err != PLAYER_ERROR_NONE) {
190 DBG("Setting - error to set sound_type[%d]", err);
191 player_destroy(player);
192 is_player_created = 0;
196 DBG("Setting - profile_play_sound is setting uri.");
198 err = player_set_uri(player, ringtone_file);
199 if (err != PLAYER_ERROR_NONE) {
200 DBG("Setting - error to set attribute---profile_uri[%d]", err);
201 player_destroy(player);
202 is_player_created = 0;
206 DBG("Setting - profile_play_sound is preparing.");
208 err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
209 if (err != PLAYER_ERROR_NONE) {
210 DBG("Setting - realizing the player handle failed[%d]", err);
211 player_destroy(player);
212 is_player_created = 0;
216 DBG("Setting - waiting..");
218 _sound_type = sound_type;
220 is_player_created = 1;
225 int _profile_stop_sound(void *data)
227 player_state_e state = -1;
229 int prev_behavior = 0; /* instead of do_while(0) and break */
231 player_get_state(player, &state);
232 if (state == PLAYER_STATE_PLAYING) {
233 if (player_stop(player) != PLAYER_ERROR_NONE) {
234 DBG("Setting - mm player stop failed");
236 prev_behavior++; /* instead of do_while(0) and break */
240 /*player_unset_completed_cb(*player); */
241 /*player_unset_interrupted_cb(*player); */
243 if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
244 DBG("Setting - mm player unrealize failed");
246 prev_behavior++; /* instead of do_while(0) and break */
249 if ((prev_behavior == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
250 DBG("Setting - mm player destroy failed");
254 is_player_created = 0;
259 int is_created_player()
261 return is_player_created;
264 void set_looping(int enable)
266 if (is_created_player())
267 player_set_looping(player, enable);
269 ERR("player is not created!!");
272 int _close_player(void *data, sound_type_e type)
274 if (is_created_player()) {
275 if (_profile_stop_sound(data)) {
276 is_player_created = 0;
284 void play_sound(char *file_path, float volume, sound_type_e sound_type)
286 DBG("Setting - profile_play_sound function start ... : is_created_player() => (%d)", is_created_player());
287 if (!is_created_player()) {
288 DBG("Setting - profile_play_sound)");
289 profile_play_sound(NULL, NULL, file_path, volume, sound_type, TRUE);
292 _profile_restart_player(NULL, file_path, sound_type, TRUE);
296 void play_sound_for_sound_mode(char *file_path, float volume, sound_type_e sound_type)
298 DBG("Setting - play_sound_for_sound_mode function start ...");
299 if (!is_created_player()) {
300 DBG("Setting - play_sound_for_sound_mode)");
301 profile_play_sound(NULL, NULL, file_path, volume, sound_type, FALSE);
304 _profile_restart_player(NULL, file_path, sound_type, FALSE);