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