tizen 2.3.1 release
[apps/home/b2-clocksetting.git] / src / setting-common-sound.c
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *      http://floralicense.org/license/
9  *
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.
15  *
16  */
17 /*
18  * setting-common-sound.c
19  *
20  *  Created on: Oct 16, 2013
21  *      Author: min-hoyun
22  */
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26
27 #include "setting-common-sound.h"
28 #include "util.h"
29
30 static player_h player;
31 static int is_player_created = 0;
32 static sound_type_e _sound_type;
33
34 /*player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type); */
35
36 /**
37  * @return zero on successful
38  */
39
40 int get_sound_mode()
41 {
42         int mode = 1;
43         int is_enable_sound   = 0;
44         int is_enable_vibrate = 0;
45
46         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_enable_sound);
47         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_enable_vibrate);
48
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;
55         }
56         return mode;
57 }
58
59 static void _profile_player_prepare_cb(void *data)
60 {
61         DBG("Player is prepared. Start player");
62
63         if (!is_created_player()) {
64                 DBG("Player start cancel!!");
65                 return;
66         }
67
68         if (player != NULL) {
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) {
72                         int err = 0;
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;
79                                 return;
80                         }
81                 } else {
82                         DBG("Player state = %d. do not start", play_state);
83                 }
84         }
85 }
86
87 int is_player_paused()
88 {
89         int ret = FALSE;
90         player_state_e state = -1;
91         player_get_state(player, &state);
92         if (state != PLAYER_STATE_PLAYING) {
93                 DBG("Setting - player is paused!!");
94                 ret = TRUE;
95         }
96         return ret;
97 }
98
99 static int _profile_restart_player(void *data, char *ringtone_file, sound_type_e sound_type, int prelistening_enable)
100 {
101         player_state_e state = -1;
102         int ret = TRUE;
103         int prev_behavior = 0;  /*  instead of do_while(0) and break */
104
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");
109                         ret = -1;
110                         prev_behavior++;        /*  instead of do_while(0) and break */
111                 }
112         }
113
114         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
115                 DBG("Setting - mm player unrealize failed");
116                 ret = -1;
117                 prev_behavior++;        /*  instead of do_while(0) and break */
118         }
119
120         if (prev_behavior == 0) {
121                 int err = 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;
127                         return err;
128                 }
129
130 #if 0
131                 if (prelistening_enable && sound_type != SOUND_TYPE_SYSTEM) {
132                         DBG("Setting - profile_play_sound is sessioning..");
133
134                         player_prelistening_mode_e mode = _get_prelistening_mode(sound_type);
135
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;
141                                 return err;
142                         }
143                 }
144 #endif
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;
150                         return err;
151                 }
152         }
153         return ret;
154 }
155 #if 0
156 player_prelistening_mode_e _get_prelistening_mode(sound_type_e sound_type)
157 {
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;
162                 break;
163         case SOUND_TYPE_RINGTONE:
164                 mode = PLAYER_PRELISTENING_MODE_RINGTONE;
165                 break;
166         case SOUND_TYPE_NOTIFICATION:
167                 mode = PLAYER_PRELISTENING_MODE_NOTIFICATION;
168                 break;
169         }
170
171         return mode;
172 }
173 #endif
174 int profile_play_sound(void *data, void *cb, char *ringtone_file, float vol, sound_type_e sound_type, int prelistening_enable)
175 {
176         DBG("Setting - profile_play_sound is started. path: %s", ringtone_file);
177         DBG("Setting - profile_play_sound is creating.");
178
179         int err = 0;
180         err = player_create(&player);
181         if (err != PLAYER_ERROR_NONE) {
182                 DBG("Setting - creating the player handle failed[%d]", err);
183                 return NULL;
184         }
185
186         DBG("Setting - profile_play_sound is setting sound type.");
187
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;
193                 return err;
194         }
195
196         DBG("Setting - profile_play_sound is setting uri.");
197
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;
203                 return err;
204         }
205
206         DBG("Setting - profile_play_sound is preparing.");
207
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;
213                 return err;
214         }
215
216         DBG("Setting - waiting..");
217
218         _sound_type = sound_type;
219
220         is_player_created = 1;
221
222         return err;
223 }
224
225 int _profile_stop_sound(void *data)
226 {
227         player_state_e state = -1;
228         int ret = TRUE;
229         int prev_behavior = 0;  /*  instead of do_while(0) and break */
230
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");
235                         ret = -1;
236                         prev_behavior++;        /*  instead of do_while(0) and break */
237                 }
238         }
239
240         /*player_unset_completed_cb(*player); */
241         /*player_unset_interrupted_cb(*player); */
242
243         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
244                 DBG("Setting - mm player unrealize failed");
245                 ret = -1;
246                 prev_behavior++;        /*  instead of do_while(0) and break */
247         }
248
249         if ((prev_behavior == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
250                 DBG("Setting - mm player destroy failed");
251                 ret = -1;
252         }
253
254         is_player_created = 0;
255
256         return ret;
257 }
258
259 int is_created_player()
260 {
261         return is_player_created;
262 }
263
264 void set_looping(int enable)
265 {
266         if (is_created_player())
267                 player_set_looping(player, enable);
268         else
269                 ERR("player is not created!!");
270 }
271
272 int _close_player(void *data, sound_type_e type)
273 {
274         if (is_created_player()) {
275                 if (_profile_stop_sound(data)) {
276                         is_player_created = 0;
277                 } else {
278                         return FALSE;
279                 }
280         }
281         return TRUE;
282 }
283
284 void play_sound(char *file_path, float volume, sound_type_e sound_type)
285 {
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);
290                 set_looping(TRUE);
291         } else {
292                 _profile_restart_player(NULL, file_path, sound_type, TRUE);
293         }
294 }
295
296 void play_sound_for_sound_mode(char *file_path, float volume, sound_type_e sound_type)
297 {
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);
302                 set_looping(TRUE);
303         } else {
304                 _profile_restart_player(NULL, file_path, sound_type, FALSE);
305         }
306 }