Initialize Tizen 2.3
[apps/home/b2-clocksetting.git] / src / setting-common-sound.c
1 /*
2  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
35 /**
36  * @return zero on successful
37  */
38
39 int get_sound_mode()
40 {
41         int mode = 1;
42         int is_enable_sound   = 0;
43         int is_enable_vibrate = 0;
44
45         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &is_enable_sound);
46         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &is_enable_vibrate);
47
48         if( is_enable_sound && !is_enable_vibrate )     // sound mode
49         {
50                 mode = SOUND_MODE_SOUND;
51         }
52         else if( !is_enable_sound && is_enable_vibrate ) // vibration mode
53         {
54                 mode = SOUND_MODE_VIBRATE;
55         }
56         else if( !is_enable_sound && !is_enable_vibrate ) // mute
57         {
58                 mode = SOUND_MODE_MUTE;
59         }
60         return mode;
61 }
62
63 static void _profile_player_prepare_cb(void *data)
64 {
65         DBG("Player is prepared. Start player");
66
67         if( !is_created_player() )
68         {
69                 DBG("Player start cancel!!");
70                 return;
71         }
72
73         if(player != NULL)
74         {
75                 player_state_e play_state;
76                 player_get_state(player, &play_state);
77                 if (play_state == PLAYER_STATE_PAUSED || play_state == PLAYER_STATE_READY) {
78                         int err = 0;
79                         err = player_start(player);
80                         if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */
81                                 DBG("Setting - player_start [%d]", err);
82                                 player_unprepare(player);
83                                 player_destroy(player);
84                                 is_player_created = 0;
85                                 return;
86                         }
87                 }
88                 else{
89                         DBG("Player state = %d. do not start", play_state);
90                 }
91         }
92 }
93
94 int is_player_paused()
95 {
96         int ret = FALSE;
97         player_state_e state = -1;
98         player_get_state(player, &state);
99         if (state != PLAYER_STATE_PLAYING)
100         {
101                 DBG("Setting - player is paused!!");
102                 ret = TRUE;
103         }
104         return ret;
105 }
106
107 static int _profile_restart_player(void *data, char *ringtone_file, sound_type_e sound_type, int prelistening_enable)
108 {
109         player_state_e state = -1;
110         int ret = TRUE;
111         int prev_behavior = 0;  /*  instead of do_while(0) and break */
112
113         player_get_state(player, &state);
114         if (state == PLAYER_STATE_PLAYING) {
115                 if (player_stop(player) != PLAYER_ERROR_NONE) {
116                         DBG("Setting - mm player stop failed");
117                         ret = -1;
118                         prev_behavior++;        /*  instead of do_while(0) and break */
119                 }
120         }
121
122         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
123                 DBG("Setting - mm player unrealize failed");
124                 ret = -1;
125                 prev_behavior++;        /*  instead of do_while(0) and break */
126         }
127
128         if (prev_behavior == 0) {
129                 int err= 0;
130                 err = player_set_uri(player, ringtone_file);
131                 if (err != PLAYER_ERROR_NONE) {
132                         DBG("Setting - error to set attribute---profile_uri[%d]", err);
133                         player_destroy(player);
134                         is_player_created = 0;
135                         return err;
136                 }
137
138                 err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
139                 if (err != PLAYER_ERROR_NONE) {
140                         DBG("Setting - realizing the player handle failed[%d]", err);
141                         player_destroy(player);
142                         is_player_created = 0;
143                         return err;
144                 }
145         }
146         return ret;
147 }
148
149 int profile_play_sound(void *data, void *cb, char *ringtone_file, float vol, sound_type_e sound_type, int prelistening_enable)
150 {
151         DBG("Setting - profile_play_sound is started. path: %s", ringtone_file);
152         DBG("Setting - profile_play_sound is creating.");
153
154         int err = 0;
155         err = player_create(&player);
156         if( err != PLAYER_ERROR_NONE )
157         {
158                 DBG("Setting - creating the player handle failed[%d]", err);
159                 return NULL;
160         }
161
162         DBG("Setting - profile_play_sound is setting sound type.");
163
164         err = player_set_sound_type(player, sound_type);
165         if (err != PLAYER_ERROR_NONE) {
166                 DBG("Setting - error to set sound_type[%d]", err);
167                 player_destroy(player);
168                 is_player_created = 0;
169                 return err;
170         }
171
172         DBG("Setting - profile_play_sound is setting uri.");
173
174         err = player_set_uri(player, ringtone_file);
175         if (err != PLAYER_ERROR_NONE) {
176                 DBG("Setting - error to set attribute---profile_uri[%d]", err);
177                 player_destroy(player);
178                 is_player_created = 0;
179                 return err;
180         }
181
182         DBG("Setting - profile_play_sound is preparing.");
183
184         //err = player_prepare(player);
185         err = player_prepare_async(player, _profile_player_prepare_cb, NULL);
186         if (err != PLAYER_ERROR_NONE) {
187                 DBG("Setting - realizing the player handle failed[%d]", err);
188                 player_destroy(player);
189                 is_player_created = 0;
190                 return err;
191         }
192
193         DBG("Setting - waiting..");
194
195         _sound_type = sound_type;
196
197         is_player_created = 1;
198
199         return err;
200 }
201
202 int _profile_stop_sound(void *data)
203 {
204         player_state_e state = -1;
205         int ret = TRUE;
206         int prev_behavior = 0;  /*  instead of do_while(0) and break */
207
208         player_get_state(player, &state);
209         if (state == PLAYER_STATE_PLAYING) {
210                 if (player_stop(player) != PLAYER_ERROR_NONE) {
211                         DBG("Setting - mm player stop failed");
212                         ret = -1;
213                         prev_behavior++;        /*  instead of do_while(0) and break */
214                 }
215         }
216
217         //player_unset_completed_cb(*player);
218         //player_unset_interrupted_cb(*player);
219
220         if ((prev_behavior == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
221                 DBG("Setting - mm player unrealize failed");
222                 ret = -1;
223                 prev_behavior++;        /*  instead of do_while(0) and break */
224         }
225
226         if ((prev_behavior == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
227                 DBG("Setting - mm player destroy failed");
228                 ret = -1;
229         }
230
231         is_player_created = 0;
232
233         return ret;
234 }
235
236 int is_created_player()
237 {
238         return is_player_created;
239 }
240
241 void set_looping(int enable)
242 {
243         if(is_created_player())
244                 player_set_looping(player, enable);
245         else
246                 ERR("player is not created!!");
247 }
248
249 int _close_player(void *data, sound_type_e type)
250 {
251         if ( is_created_player() )
252         {
253                 if (_profile_stop_sound(data))
254                 {
255                         is_player_created = 0;
256                 }
257                 else
258                 {
259                         return FALSE;
260                 }
261         }
262         return TRUE;
263 }
264
265 void play_sound(char * file_path, float volume, sound_type_e sound_type)
266 {
267         DBG("Setting - profile_play_sound function start ...");
268         if( !is_created_player() )
269         {
270                 DBG("Setting - profile_play_sound)");
271                 profile_play_sound(NULL, NULL, file_path, volume, sound_type, TRUE);
272                 set_looping(TRUE);
273         }
274         else
275         {
276                 _profile_restart_player(NULL, file_path, sound_type, TRUE);
277         }
278 }
279
280 void play_sound_for_sound_mode(char * file_path, float volume, sound_type_e sound_type)
281 {
282         DBG("Setting - play_sound_for_sound_mode function start ...");
283         if( !is_created_player() )
284         {
285                 DBG("Setting - play_sound_for_sound_mode)");
286                 profile_play_sound(NULL, NULL, file_path, volume, sound_type, FALSE);
287                 set_looping(TRUE);
288         }
289         else
290         {
291                 _profile_restart_player(NULL, file_path, sound_type, FALSE);
292         }
293 }