2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <Elementary.h>
21 #include "playermgr.h"
29 #define TIME_INTERVAL 1.0
31 static int _speed[] = {
41 Eina_List *media_list;
52 Ecore_Timer *ffrew_timer;
55 static void _destroy_list(Eina_List *list)
63 EINA_LIST_FREE(list, o) {
69 static Eina_List *_create_list(const char *playlist)
71 Eina_List *list = NULL;
78 fp = fopen(playlist, "r");
82 while (fscanf(fp, "%s\n", buf) != EOF)
83 list = eina_list_append(list, strdup(buf));
90 static int _get_current(Eina_List *list, const char *path)
102 EINA_LIST_FOREACH(list, l, o) {
103 filepath = (const char *)o;
104 if (!strcmp(filepath, path))
112 struct playermgr *playermgr_init(Evas_Object *win,
113 const char *path, const char *playlist)
115 struct playermgr *mgr;
117 mgr = (playermgr *)calloc(1, sizeof(*mgr));
119 _ERR("Allocation fail");
124 mgr->display_mode = E_FULL_SCREEN;
125 mgr->audio_effect = 0;
129 mgr->media_list = _create_list(playlist);
130 if (!mgr->media_list) {
131 _ERR("play list is null");
136 mgr->current = _get_current(mgr->media_list, path);
137 mgr->total = eina_list_count(mgr->media_list);
139 if (player_create(&mgr->player) != PLAYER_ERROR_NONE) {
140 _ERR("PLAYER CREATION ERROR");
149 static int _play_video(struct playermgr *mgr, const char *path)
153 if (player_set_uri(mgr->player, path) != PLAYER_ERROR_NONE) {
154 _ERR("PLAYER SET URI ERROR");
158 r = player_set_display(mgr->player, PLAYER_DISPLAY_TYPE_OVERLAY,
159 GET_DISPLAY(mgr->win));
160 if (r != PLAYER_ERROR_NONE) {
161 _ERR("SETTING DISPLAY ERROR: %d", r);
165 playermgr_set_display_mode(mgr, mgr->display_mode);
167 r = player_prepare(mgr->player);
168 if (r != PLAYER_ERROR_NONE) {
169 if (r == PLAYER_ERROR_NOT_SUPPORTED_FILE)
170 _ERR("PLAYER PREPARE ERROR NOT SUPPORTED FILE %d", r);
171 else if (r == PLAYER_ERROR_INVALID_URI)
172 _ERR("PLAYER PREPARE ERROR INVALID URI %d", r);
174 _ERR("PLAYER PREPARE ERROR");
179 r = player_start(mgr->player);
180 if (r != PLAYER_ERROR_NONE) {
181 _ERR("PLAYER START ERROR");
185 playermgr_set_audio_effect(mgr, mgr->audio_effect);
190 int playermgr_stop_video(struct playermgr *mgr)
192 if (!mgr || !mgr->player) {
193 _ERR("invalid parameter");
197 player_pause(mgr->player);
198 player_stop(mgr->player);
199 player_unprepare(mgr->player);
204 int playermgr_play_video(struct playermgr *mgr)
209 if (!mgr || !mgr->media_list) {
210 _ERR("invalid parameter");
214 r = playermgr_stop_video(mgr);
218 path = playermgr_get_video_path(mgr);
219 r = _play_video(mgr, path);
224 void playermgr_set_prev_video(struct playermgr *mgr)
227 _ERR("invalid parameter");
231 if (mgr->current == 0)
232 mgr->current = mgr->total - 1;
237 Eina_Bool playermgr_set_next_video(struct playermgr *mgr, Eina_Bool repeat)
240 _ERR("invalid parameter");
244 if (mgr->current == mgr->total - 1) {
256 const char *playermgr_get_video_path(struct playermgr *mgr)
260 if (!mgr || !mgr->media_list) {
261 _ERR("invalid parameter");
265 path = (const char *)eina_list_nth(mgr->media_list, mgr->current);
270 void playermgr_fini(struct playermgr *mgr)
275 if (mgr->ffrew_timer)
276 ecore_timer_del(mgr->ffrew_timer);
279 _destroy_list(mgr->media_list);
282 player_pause(mgr->player);
283 player_stop(mgr->player);
284 player_unprepare(mgr->player);
285 player_destroy(mgr->player);
291 int playermgr_pause(struct playermgr *mgr)
293 if (!mgr || !mgr->player) {
294 _ERR("invalid parameter");
298 if (player_pause(mgr->player) != PLAYER_ERROR_NONE) {
299 _ERR("Player pause error");
306 int playermgr_resume(struct playermgr *mgr)
308 if (!mgr || !mgr->player) {
309 _ERR("invalid parameter");
313 if (player_start(mgr->player) != PLAYER_ERROR_NONE) {
314 _ERR("Player resume error");
321 int playermgr_get_state(struct playermgr *mgr, player_state_e *state)
325 if (!mgr || !mgr->player) {
326 _ERR("invalid parameter");
330 r = player_get_state(mgr->player, state);
331 if (r != PLAYER_ERROR_NONE) {
332 _ERR("Player get state error");
339 int playermgr_get_position(struct playermgr *mgr, int *position)
343 if (!mgr || !mgr->player) {
344 _ERR("invalid parameter");
348 r = player_get_play_position(mgr->player, position);
349 if (r != PLAYER_ERROR_NONE) {
350 _ERR("Player get position error");
357 int playermgr_set_position(struct playermgr *mgr,
358 int sec, player_seek_completed_cb cb, void *data)
362 if (!mgr || !mgr->player) {
363 _ERR("invalid parameter");
367 r = player_set_play_position(mgr->player, sec, false, cb, data);
368 if (r != PLAYER_ERROR_NONE) {
369 _ERR("Player set position error");
376 int playermgr_set_playback_rate(struct playermgr *mgr, float speed)
380 if (!mgr || !mgr->player) {
381 _ERR("invalid parameter");
385 if (speed > RATE_MAX)
387 else if (speed < RATE_MIN)
390 r = player_set_playback_rate(mgr->player, speed);
391 if (r != PLAYER_ERROR_NONE) {
392 _ERR("Player set playback error");
401 int playermgr_get_playback_rate(struct playermgr *mgr, float *speed)
404 _ERR("invalid parameter");
413 int playermgr_set_looping(struct playermgr *mgr, bool value)
417 if (!mgr || !mgr->player) {
418 _ERR("invalid parameter");
422 r = player_set_looping(mgr->player, value);
423 if (r != PLAYER_ERROR_NONE) {
424 _ERR("Player set looping error");
431 int playermgr_set_audio_effect(struct playermgr *mgr, int value)
433 /* audio_effect not supported, just return 0 */
434 mgr->audio_effect = value;
439 int playermgr_set_display_mode(struct playermgr *mgr, int value)
442 player_display_mode_e mode;
444 if (!mgr || !mgr->player) {
445 _ERR("invalid parameter");
451 mode = PLAYER_DISPLAY_MODE_FULL_SCREEN;
454 mode = PLAYER_DISPLAY_MODE_ORIGIN_SIZE;
457 _ERR("Invalid value");
461 r = player_set_display_mode(mgr->player, mode);
462 if (r != PLAYER_ERROR_NONE) {
463 _ERR("Player display mode set error");
467 mgr->display_mode = value;
472 int playermgr_set_completed_cb(struct playermgr *mgr,
473 player_completed_cb cb, void *data)
477 if (!mgr || !mgr->player) {
478 _ERR("invalid parameter");
482 r = player_set_completed_cb(mgr->player, cb, data);
483 if (r != PLAYER_ERROR_NONE) {
484 _ERR("Player set completed cb error");
491 void playermgr_get_play_count(struct playermgr *mgr, char *str, int len)
494 _ERR("invalid parameter");
498 snprintf(str, len, "%d/%d", mgr->current + 1, mgr->total);
501 static Eina_Bool _ffrew_timer_cb(void *dt)
503 struct playermgr *mgr;
508 return ECORE_CALLBACK_CANCEL;
510 mgr = (struct playermgr *)dt;
513 return ECORE_CALLBACK_CANCEL;
515 playermgr_get_position(mgr, &pos);
518 pos = pos + _speed[mgr->ff_speed];
519 if (pos > mgr->duration)
521 } else if (mgr->rew_speed) {
522 pos = pos - _speed[mgr->rew_speed];
526 r = playermgr_resume(mgr);
528 return ECORE_CALLBACK_RENEW;
530 return ECORE_CALLBACK_CANCEL;
533 playermgr_set_position(mgr, pos, NULL, NULL);
535 if (mgr->rew_speed && pos <= 0) {
536 mgr->rew_speed = SPEED_MIN;
537 return ECORE_CALLBACK_CANCEL;
540 if (mgr->ff_speed && pos >= mgr->duration) {
541 mgr->ff_speed = SPEED_MIN;
542 return ECORE_CALLBACK_CANCEL;
545 if (ecore_timer_interval_get(mgr->ffrew_timer) != TIME_INTERVAL)
546 ecore_timer_interval_set(mgr->ffrew_timer, TIME_INTERVAL);
548 return ECORE_CALLBACK_RENEW;
551 static void _start_ffrew_timer(struct playermgr *mgr)
553 playermgr_pause(mgr);
555 if (!mgr->ffrew_timer) {
556 mgr->ffrew_timer = ecore_timer_add(0.0,
557 _ffrew_timer_cb, mgr);
558 if (!mgr->ffrew_timer)
561 ecore_timer_interval_set(mgr->ffrew_timer, 0.0);
562 ecore_timer_reset(mgr->ffrew_timer);
566 int playermgr_set_ff(struct playermgr *mgr, int duration)
571 _ERR("invalid parameter");
576 mgr->duration = duration;
577 mgr->rew_speed = SPEED_MIN;
579 if (mgr->ff_speed == SPEED_MAX)
582 if (mgr->ff_speed == SPEED_MIN) {
583 _start_ffrew_timer(mgr);
592 int playermgr_set_rew(struct playermgr *mgr, int duration)
597 _ERR("invalid parameter");
602 mgr->duration = duration;
603 mgr->ff_speed = SPEED_MIN;
605 if (mgr->rew_speed == SPEED_MAX)
608 if (mgr->rew_speed == SPEED_MIN) {
609 _start_ffrew_timer(mgr);
618 bool playermgr_get_ffrew(struct playermgr *mgr)
620 if (mgr->ff_speed || mgr->rew_speed)
626 void playermgr_stop_ffrew(struct playermgr *mgr)
629 _ERR("invalid parameter");
631 mgr->ff_speed = SPEED_MIN;
632 mgr->rew_speed = SPEED_MIN;
634 if (mgr->ffrew_timer) {
635 ecore_timer_del(mgr->ffrew_timer);
636 mgr->ffrew_timer = NULL;