change feedback type
[apps/core/preloaded/quickpanel.git] / daemon / media.c
1 /*
2  * Copyright (c) 2009-2015 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 #include <stdio.h>
19 #include <glib.h>
20 #include <vconf.h>
21 #include <metadata_extractor.h>
22 #include "common.h"
23 #include "quickpanel-ui.h"
24
25 #define NEED_TO_DEBUG_LOCKUP_ISSUE
26
27 static struct info {
28         int id;
29         int is_feedback_initialized;
30         player_h player;
31         Ecore_Timer *playing_timer;
32 } s_info = {
33         .player = NULL,
34         .playing_timer = NULL,
35         .id = 0,
36         .is_feedback_initialized = 0,
37 };
38
39 static void _quickpanel_player_free(player_h *sound_player);
40
41 static void
42 _quickpanel_player_del_timeout_timer(void)
43 {
44         if (s_info.playing_timer) {
45                 ecore_timer_del(s_info.playing_timer);
46                 s_info.playing_timer = NULL;
47         }
48 }
49
50 static Eina_Bool _quickpanel_player_timeout_cb(void *data)
51 {
52         s_info.playing_timer = NULL;
53
54         retif(data == NULL, ECORE_CALLBACK_CANCEL, "invalid parameter");
55         player_h *sound_player = data;
56
57         _quickpanel_player_free(sound_player);
58         s_info.playing_timer = NULL;
59
60         return ECORE_CALLBACK_CANCEL;
61 }
62
63 static void _quickpanel_player_free_job_cb(void *data)
64 {
65         player_h sound_player = data;
66         player_state_e state = PLAYER_STATE_NONE;
67
68         retif(sound_player == NULL, , "invalid parameter");
69
70 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
71         SERR("before stopping media");
72 #endif
73         if (player_get_state(sound_player, &state) == PLAYER_ERROR_NONE) {
74
75                 INFO("the state of sound player %d", state);
76
77                 if (state == PLAYER_STATE_PLAYING) {
78                         player_stop(sound_player);
79                         player_unprepare(sound_player);
80                 }
81                 if (state == PLAYER_STATE_READY) {
82                         player_unprepare(sound_player);
83                 }
84         }
85         player_destroy(sound_player);
86 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
87         SERR("after stopping media");
88 #endif
89 }
90
91 static void _quickpanel_player_free(player_h *sound_player)
92 {
93         retif(sound_player == NULL, , "invalid parameter");
94         retif(*sound_player == NULL, , "invalid parameter");
95
96         ecore_job_add(_quickpanel_player_free_job_cb, *sound_player);
97         *sound_player = NULL;
98 }
99
100 static void _quickpanel_player_start_job_cb(void *data)
101 {
102         int ret = PLAYER_ERROR_NONE;
103         player_h *sound_player = data;
104
105 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
106         SERR("before playing media");
107 #endif
108         ret = player_start(*sound_player);
109         if (ret != PLAYER_ERROR_NONE) { /* if directly return retor.. */
110                 ERR("player_start [%d]", ret);
111                 _quickpanel_player_free(sound_player);
112                 return;
113         }
114         s_info.playing_timer = ecore_timer_add(QP_PLAY_DURATION_LIMIT,
115                         _quickpanel_player_timeout_cb, sound_player);
116 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
117         SERR("after playing media");
118 #endif
119 }
120
121 static void
122 _quickpanel_player_completed_cb(void *user_data)
123 {
124         retif(user_data == NULL, , "invalid parameter");
125         player_h *sound_player = user_data;
126
127         DBG("Media player completed");
128
129         _quickpanel_player_del_timeout_timer();
130         _quickpanel_player_free(sound_player);
131 }
132
133 static void
134 _quickpanel_player_interrupted_cb(player_interrupted_code_e code, void *user_data)
135 {
136         retif(user_data == NULL, , "invalid parameter");
137         player_h *sound_player = user_data;
138
139         ERR("interrupt code [%d]", (int)code);
140
141         _quickpanel_player_del_timeout_timer();
142         _quickpanel_player_free(sound_player);
143 }
144
145 static void
146 _quickpanel_player_error_cb(int error_code, void *user_data)
147 {
148         retif(user_data == NULL, , "invalid parameter");
149         player_h *sound_player = user_data;
150
151         ERR("Error code [%d]", (int)error_code);
152
153         _quickpanel_player_del_timeout_timer();
154         _quickpanel_player_free(sound_player);
155 }
156
157 HAPI int quickpanel_media_player_is_drm_error(int error_code)
158 {
159         if (error_code == PLAYER_ERROR_DRM_EXPIRED
160                 || error_code == PLAYER_ERROR_DRM_NO_LICENSE
161                 || error_code == PLAYER_ERROR_DRM_FUTURE_USE
162                 || error_code == PLAYER_ERROR_DRM_NOT_PERMITTED) {
163                 return 1;
164         }
165
166         return 0;
167 }
168
169 HAPI int quickpanel_media_player_play(sound_type_e sound_type, const char *sound_file)
170 {
171         player_h *sound_player = &s_info.player;
172         sound_session_type_e type = 1;
173
174         int ret = PLAYER_ERROR_NONE;
175         int sndRet = SOUND_MANAGER_ERROR_NONE;
176         player_state_e state = PLAYER_STATE_NONE;
177
178 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
179         SERR("Start player");
180 #endif
181         _quickpanel_player_del_timeout_timer();
182
183         if (*sound_player != NULL) {
184                 _quickpanel_player_free(sound_player);
185         }
186
187 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
188         SERR("setting sound session start");
189 #endif
190         if (sound_type == SOUND_TYPE_NOTIFICATION) {
191                 sound_manager_get_session_type(&type);
192                 if (type != SOUND_SESSION_TYPE_NOTIFICATION) {
193                         sndRet = sound_manager_set_session_type(SOUND_SESSION_TYPE_NOTIFICATION);
194                         if (sndRet != SOUND_MANAGER_ERROR_NONE) {
195                                 ERR("sound_manager_set_session_type fail sndRet :%x",sndRet);
196                                 return PLAYER_ERROR_INVALID_PARAMETER;
197                         }
198                 }
199         }
200 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
201         SERR("setting sound session finished");
202 #endif
203
204 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
205         SERR("player_create start");
206 #endif
207         ret = player_create(sound_player);
208         if (ret != PLAYER_ERROR_NONE) {
209                 ERR("creating the player handle failed[%d]", ret);
210                 *sound_player = NULL;
211                 return ret;
212         }
213 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
214         SERR("player_create finished");
215 #endif
216
217         ret = player_set_sound_type(*sound_player, sound_type);
218         if (ret != PLAYER_ERROR_NONE) {
219                 ERR("player_set_sound_type() ERR: %x!!!!", ret);
220                 _quickpanel_player_free(sound_player);
221                 return ret;
222         }
223
224         player_get_state(*sound_player, &state);
225         if (state > PLAYER_STATE_READY) {
226                 _quickpanel_player_free(sound_player);
227                 return ret;
228         }
229
230         ret = player_set_uri(*sound_player, sound_file);
231         if (ret != PLAYER_ERROR_NONE) {
232                 ERR("set attribute---profile_uri[%d]", ret);
233                 _quickpanel_player_free(sound_player);
234                 return ret;
235         }
236
237         ret = player_prepare(*sound_player);
238         if (ret != PLAYER_ERROR_NONE) {
239                 ERR("realizing the player handle failed[%d]", ret);
240                 _quickpanel_player_free(sound_player);
241                 return ret;
242         }
243
244         player_get_state(*sound_player, &state);
245         if (state != PLAYER_STATE_READY) {
246                 ERR("state of player is invalid %d", state);
247                 _quickpanel_player_free(sound_player);
248                 return ret;
249         }
250
251         /* register callback */
252         ret = player_set_completed_cb(*sound_player, _quickpanel_player_completed_cb, sound_player);
253         if (ret != PLAYER_ERROR_NONE) {
254                 ERR("player_set_completed_cb() ERR: %x!!!!", ret);
255                 _quickpanel_player_free(sound_player);
256                 return ret;
257         }
258
259         ret = player_set_interrupted_cb(*sound_player, _quickpanel_player_interrupted_cb, sound_player);
260         if (ret != PLAYER_ERROR_NONE) {
261                 _quickpanel_player_free(sound_player);
262                 return ret;
263         }
264
265         ret = player_set_error_cb(*sound_player, _quickpanel_player_error_cb, sound_player);
266         if (ret != PLAYER_ERROR_NONE) {
267                 _quickpanel_player_free(sound_player);
268                 return ret;
269         }
270
271         ecore_job_add(_quickpanel_player_start_job_cb, sound_player);
272 #ifdef NEED_TO_DEBUG_LOCKUP_ISSUE
273         SERR("playing request");
274 #endif
275
276         return ret;
277 }
278
279 static Eina_Bool _playable_check(const char *file_path)
280 {
281         char *value = NULL;
282         int ret_meta =  METADATA_EXTRACTOR_ERROR_NONE;
283         metadata_extractor_h metadata = NULL;
284         Eina_Bool ret = EINA_FALSE;
285
286         ret_meta = metadata_extractor_create(&metadata);
287         if (ret_meta != METADATA_EXTRACTOR_ERROR_NONE) {
288                 ERR("Failed to create metadata extractor:%d", ret_meta);
289                 return ret;
290         }
291
292         if (metadata == NULL) {
293                 ERR("Failed to create metadata extractor:%d", ret_meta);
294                 return ret;
295         }
296
297         ret_meta = metadata_extractor_set_path(metadata, file_path);
298         if (ret_meta != METADATA_EXTRACTOR_ERROR_NONE) {
299                 ERR("Failed to set path to meta extractor:%d", ret_meta);
300                 metadata_extractor_destroy(metadata);
301                 return ret;
302         }
303         ret_meta = metadata_extractor_get_metadata(metadata, METADATA_HAS_AUDIO, &value);
304         if (ret_meta != METADATA_EXTRACTOR_ERROR_NONE) {
305                 ERR("Failed to get metadata:%d", ret_meta);
306                 metadata_extractor_destroy(metadata);
307                 return ret;
308         }
309
310         if(value && g_strcmp0(value, "0")) {
311                 ret = EINA_TRUE;
312         }
313
314         if (value != NULL) {
315                 free(value);
316         }
317         DBG("%s :: playable[%d]", file_path, ret);
318         metadata_extractor_destroy(metadata);
319         return ret;
320 }
321
322 HAPI Eina_Bool quickpanel_media_playable_check(const char *file_path)
323 {
324         Eina_Bool ret = EINA_FALSE;
325
326         /* Check file exist or not */
327         ret = ecore_file_exists(file_path);
328         if (ret == EINA_FALSE) {
329                 ERR("%s file does not exist", file_path);
330                 return ret;
331         }
332
333         /* Check file playable or not */
334         ret = _playable_check(file_path);
335         if (ret == EINA_FALSE) {
336                 ERR("%s file does not playable", file_path);
337                 return ret;
338         }
339
340         return ret;
341 }
342
343
344 HAPI void quickpanel_media_player_stop(void)
345 {
346         _quickpanel_player_del_timeout_timer();
347
348         if (s_info.player != NULL) {
349                 _quickpanel_player_free(&s_info.player);
350         }
351
352         quickpanel_media_player_id_set(0);
353 }
354
355 HAPI void quickpanel_media_player_id_set(int id)
356 {
357         s_info.id = id;
358 }
359
360 HAPI int quickpanel_media_player_id_get(void)
361 {
362         return s_info.id;
363 }
364
365 HAPI int quickpanel_media_is_sound_enabled(void)
366 {
367         int snd_status = 0, ret = -1;
368
369 #ifdef VCONFKEY_SETAPPL_ACCESSIBILITY_TURN_OFF_ALL_SOUNDS
370         int snd_disabled_status = 0;
371
372         ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TURN_OFF_ALL_SOUNDS, &snd_disabled_status);
373         msgif(ret != 0, "failed to get VCONFKEY_SETAPPL_ACCESSIBILITY_TURN_OFF_ALL_SOUNDS");
374         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status);
375         msgif(ret != 0, "failed to get VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
376
377         if (snd_disabled_status == 0 && snd_status == 1) {
378                 return 1;
379         }
380 #else
381         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status);
382         msgif(ret != 0, "failed to get VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
383
384         if (snd_status == 1) {
385                 return 1;
386         }
387 #endif
388
389         return 0;
390 }
391
392 HAPI int quickpanel_media_is_vib_enabled(void)
393 {
394         int vib_status = 0, ret = -1;
395
396         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vib_status);
397         if (ret == 0) {
398                 if (vib_status == 1)
399                         return 1;
400         } else {
401                 ERR("failed to get a value of VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
402         }
403
404         return 0;
405 }
406
407 HAPI void quickpanel_media_play_feedback(void)
408 {
409         int snd_enabled = quickpanel_media_is_sound_enabled();
410         int vib_enabled = quickpanel_media_is_vib_enabled();
411
412         quickpanel_media_init();
413
414         if (snd_enabled == 1) {
415                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
416         } else  if (vib_enabled == 1) {
417                 feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_TAP);
418         }
419 }
420
421 HAPI int quickpanel_media_set_mute_toggle(void)
422 {
423         int ret = -1;
424
425         if (quickpanel_media_is_sound_enabled() == 1 ||
426                         quickpanel_media_is_vib_enabled() == 1) {
427                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 0);
428                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
429
430                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 0);
431                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
432
433                 return 0;
434         } else {
435                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 1);
436                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
437
438                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 0);
439                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
440
441                 return 1;
442         }
443 }
444
445 HAPI void quickpanel_media_init(void)
446 {
447         if (s_info.is_feedback_initialized == 0) {
448                 if (feedback_initialize() == FEEDBACK_ERROR_NONE) {
449                         s_info.is_feedback_initialized = 1;
450                 } else {
451                         ERR("failed to init feedback API");
452                 }
453         }
454 }
455
456 HAPI void quickpanel_media_fini(void)
457 {
458         if (s_info.is_feedback_initialized == 1) {
459                 if (feedback_deinitialize() == FEEDBACK_ERROR_NONE) {
460                         s_info.is_feedback_initialized = 0;
461                 } else {
462                         ERR("failed to deinit feedback API");
463                 }
464         }
465 }