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