tizen 2.3 release
[apps/home/b2-clocksetting.git] / src / setting-common-sound.c
1 /*
2  * Copyright (c) 2010 Samsung Electronics, Inc.
3  * All rights reserved.
4  *
5  * This software is a confidential and proprietary information
6  * of Samsung Electronics, Inc. ("Confidential Information").  You
7  * shall not disclose such Confidential Information and shall use
8  * it only in accordance with the terms of the license agreement
9  * you entered into with Samsung Electronics.
10  */
11 /*
12  * setting-common-sound.c
13  *
14  *  Created on: Oct 16, 2013
15  *      Author: min-hoyun
16  */
17
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "setting-common-sound.h"
22 #include "util.h"
23
24 static player_h player;
25 static int is_player_created = 0;
26 static sound_type_e _sound_type;
27
28 //player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type);
29
30 /**
31  * @return zero on successful
32  */
33
34 int get_sound_mode()
35 {
36         int mode = 1;
37         int is_enable_sound   = 0;
38         int is_enable_vibrate = 0;
39
40         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_enable_sound);
41         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_enable_vibrate);
42
43         if( is_enable_sound && !is_enable_vibrate )     // sound mode
44         {
45                 mode = SOUND_MODE_SOUND;
46         }
47         else if( !is_enable_sound && is_enable_vibrate ) // vibration mode
48         {
49                 mode = SOUND_MODE_VIBRATE;
50         }
51         else if( !is_enable_sound && !is_enable_vibrate ) // mute
52         {
53                 mode = SOUND_MODE_MUTE;
54         }
55         return mode;
56 }
57
58 static void _profile_player_prepare_cb(void *data)
59 {
60         DBG("Player is prepared. Start player");
61
62         if( !is_created_player() )
63         {
64                 DBG("Player start cancel!!");
65                 return;
66         }
67
68         if(player != NULL)
69         {
70                 player_state_e play_state;
71                 player_get_state(player, &play_state);
72                 if (play_state == PLAYER_STATE_PAUSED || play_state == PLAYER_STATE_READY) {
73                         int err = 0;
74                         err = player_start(player);
75                         if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */
76                                 DBG("Setting - player_start [%d]", err);
77                                 player_unprepare(player);
78                                 player_destroy(player);
79                                 is_player_created = 0;
80                                 return;
81                         }
82                 }
83                 else{
84                         DBG("Player state = %d. do not start", play_state);
85                 }
86         }
87 }
88
89 int is_player_paused()
90 {
91         int ret = FALSE;
92         player_state_e state = -1;
93         player_get_state(player, &state);
94         if (state != PLAYER_STATE_PLAYING)
95         {
96                 DBG("Setting - player is paused!!");
97                 ret = TRUE;
98         }
99         return ret;
100 }
101
102 static int _profile_restart_player(void *data, char *ringtone_file, sound_type_e sound_type, int prelistening_enable)
103 {
104         player_state_e state = -1;
105         int ret = TRUE;
106         int prev_behavior = 0;  /*  instead of do_while(0) and break */
107
108         player_get_state(player, &state);
109         if (state == PLAYER_STATE_PLAYING) {
110                 if (player_stop(player) != PLAYER_ERROR_NONE) {
111                         DBG("Setting - mm player stop failed");
112                         ret = -1;
113                         prev_behavior++;        /*  instead of do_while(0) and break */
114                 }
115         }
116
117         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
118                 DBG("Setting - mm player unrealize failed");
119                 ret = -1;
120                 prev_behavior++;        /*  instead of do_while(0) and break */
121         }
122
123         if (prev_behavior == 0) {
124                 int err= 0;
125                 err = player_set_uri(player, ringtone_file);
126                 if (err != PLAYER_ERROR_NONE) {
127                         DBG("Setting - error to set attribute---profile_uri[%d]", err);
128                         player_destroy(player);
129                         is_player_created = 0;
130                         return err;
131                 }
132
133 #if 0
134                 if( prelistening_enable && sound_type != SOUND_TYPE_SYSTEM )
135                 {
136                         DBG("Setting - profile_play_sound is sessioning..");
137
138                         player_prelistening_mode_e mode = _get_prelistening_mode(sound_type);
139
140                         err = player_set_prelistening_mode(player, mode);
141                         if (err != PLAYER_ERROR_NONE) {
142                                 DBG("Setting - error to player_set_session_prelistening[%d]", err);
143                                 player_destroy(player);
144                                 is_player_created = 0;
145                                 return err;
146                         }
147                 }
148 #endif
149                 err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
150                 if (err != PLAYER_ERROR_NONE) {
151                         DBG("Setting - realizing the player handle failed[%d]", err);
152                         player_destroy(player);
153                         is_player_created = 0;
154                         return err;
155                 }
156         }
157         return ret;
158 }
159 #if 0
160 player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type)
161 {
162         player_prelistening_mode_e mode = mode = PLAYER_PRELISTENING_MODE_RINGTONE;
163         switch(sound_type) {
164         case SOUND_TYPE_MEDIA:
165                 mode = PLAYER_PRELISTENING_MODE_MEDIA;
166                 break;
167         case SOUND_TYPE_RINGTONE:
168                 mode = PLAYER_PRELISTENING_MODE_RINGTONE;
169                 break;
170         case SOUND_TYPE_NOTIFICATION:
171                 mode = PLAYER_PRELISTENING_MODE_NOTIFICATION;
172                 break;
173         }
174
175         return mode;
176 }
177 #endif
178 int profile_play_sound(void *data, void *cb, char *ringtone_file, float vol, sound_type_e sound_type, int prelistening_enable)
179 {
180         DBG("Setting - profile_play_sound is started. path: %s", ringtone_file);
181         DBG("Setting - profile_play_sound is creating.");
182
183         int err = 0;
184         err = player_create(&player);
185         if( err != PLAYER_ERROR_NONE )
186         {
187                 DBG("Setting - creating the player handle failed[%d]", err);
188                 return NULL;
189         }
190
191         DBG("Setting - profile_play_sound is setting sound type.");
192
193         err = player_set_sound_type(player, sound_type);
194         if (err != PLAYER_ERROR_NONE) {
195                 DBG("Setting - error to set sound_type[%d]", err);
196                 player_destroy(player);
197                 is_player_created = 0;
198                 return err;
199         }
200
201         DBG("Setting - profile_play_sound is setting uri.");
202
203         err = player_set_uri(player, ringtone_file);
204         if (err != PLAYER_ERROR_NONE) {
205                 DBG("Setting - error to set attribute---profile_uri[%d]", err);
206                 player_destroy(player);
207                 is_player_created = 0;
208                 return err;
209         }
210
211         DBG("Setting - profile_play_sound is preparing.");
212
213         err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
214         if (err != PLAYER_ERROR_NONE) {
215                 DBG("Setting - realizing the player handle failed[%d]", err);
216                 player_destroy(player);
217                 is_player_created = 0;
218                 return err;
219         }
220
221         DBG("Setting - waiting..");
222
223         _sound_type = sound_type;
224
225         is_player_created = 1;
226
227         return err;
228 }
229
230 int _profile_stop_sound(void *data)
231 {
232         player_state_e state = -1;
233         int ret = TRUE;
234         int prev_behavior = 0;  /*  instead of do_while(0) and break */
235
236         player_get_state(player, &state);
237         if (state == PLAYER_STATE_PLAYING) {
238                 if (player_stop(player) != PLAYER_ERROR_NONE) {
239                         DBG("Setting - mm player stop failed");
240                         ret = -1;
241                         prev_behavior++;        /*  instead of do_while(0) and break */
242                 }
243         }
244
245         //player_unset_completed_cb(*player);
246         //player_unset_interrupted_cb(*player);
247
248         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
249                 DBG("Setting - mm player unrealize failed");
250                 ret = -1;
251                 prev_behavior++;        /*  instead of do_while(0) and break */
252         }
253
254         if ((prev_behavior == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
255                 DBG("Setting - mm player destroy failed");
256                 ret = -1;
257         }
258
259         is_player_created = 0;
260
261         return ret;
262 }
263
264 int is_created_player()
265 {
266         return is_player_created;
267 }
268
269 void set_looping(int enable)
270 {
271         if(is_created_player())
272                 player_set_looping(player, enable);
273         else
274                 ERR("player is not created!!");
275 }
276
277 int _close_player(void *data, sound_type_e type)
278 {
279         if ( is_created_player() )
280         {
281                 if (_profile_stop_sound(data))
282                 {
283                         is_player_created = 0;
284                 }
285                 else
286                 {
287                         return FALSE;
288                 }
289         }
290         return TRUE;
291 }
292
293 void play_sound(char * file_path, float volume, sound_type_e sound_type)
294 {
295         DBG("Setting - profile_play_sound function start ... : is_created_player() => (%d)", is_created_player());
296         if( !is_created_player() )
297         {
298                 DBG("Setting - profile_play_sound)");
299                 profile_play_sound(NULL, NULL, file_path, volume, sound_type, TRUE);
300                 set_looping(TRUE);
301         }
302         else
303         {
304                 _profile_restart_player(NULL, file_path, sound_type, TRUE);
305         }
306 }
307
308 void play_sound_for_sound_mode(char * file_path, float volume, sound_type_e sound_type)
309 {
310         DBG("Setting - play_sound_for_sound_mode function start ...");
311         if( !is_created_player() )
312         {
313                 DBG("Setting - play_sound_for_sound_mode)");
314                 profile_play_sound(NULL, NULL, file_path, volume, sound_type, FALSE);
315                 set_looping(TRUE);
316         }
317         else
318         {
319                 _profile_restart_player(NULL, file_path, sound_type, FALSE);
320         }
321 }