emotion/generic: Fix frame dropping.
[profile/ivi/emotion.git] / src / modules / generic / emotion_generic.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <Eina.h>
11 #include <Evas.h>
12
13 #include "Emotion.h"
14 #include "emotion_private.h"
15 #include "emotion_generic.h"
16
17 static Eina_Prefix *pfx = NULL;
18
19 static int _emotion_generic_log_domain = -1;
20 #define DBG(...) EINA_LOG_DOM_DBG(_emotion_generic_log_domain, __VA_ARGS__)
21 #define INF(...) EINA_LOG_DOM_INFO(_emotion_generic_log_domain, __VA_ARGS__)
22 #define WRN(...) EINA_LOG_DOM_WARN(_emotion_generic_log_domain, __VA_ARGS__)
23 #define ERR(...) EINA_LOG_DOM_ERR(_emotion_generic_log_domain, __VA_ARGS__)
24 #define CRITICAL(...) EINA_LOG_DOM_CRIT(_emotion_generic_log_domain, __VA_ARGS__)
25
26
27 struct _default_players {
28    const char *name;
29    const char *cmdline;
30 };
31
32 static struct _default_players players[] = {
33 #ifdef EMOTION_BUILD_GENERIC_VLC
34        { "vlc", "em_generic_vlc" },
35 #endif
36        { NULL, NULL }
37 };
38
39 static const char *
40 _get_player(const char *name)
41 {
42    const char *selected_name = NULL;
43    const char *libdir = eina_prefix_lib_get(pfx);
44    static char buf[PATH_MAX];
45    int i;
46
47    if (name)
48      {
49         for (i = 0; players[i].name; i++)
50           {
51              if (!strcmp(players[i].name, name))
52                {
53                   selected_name = players[i].cmdline;
54                   break;
55                }
56           }
57      }
58
59    if ((!selected_name) && (name))
60      selected_name = name;
61
62    if (selected_name)
63      {
64         const char *cmd;
65
66         if (selected_name[0] == '/') cmd = selected_name;
67         else
68           {
69              snprintf(buf, sizeof(buf), "%s/emotion/utils/%s",
70                       libdir, selected_name);
71              cmd = buf;
72           }
73
74         DBG("Try generic player '%s'", cmd);
75         if (access(cmd, R_OK | X_OK) == 0)
76           {
77              INF("Using generic player '%s'", cmd);
78              return cmd;
79           }
80      }
81
82    for (i = 0; players[i].name; i++)
83      {
84         snprintf(buf, sizeof(buf), "%s/emotion/utils/%s",
85                  libdir, players[i].cmdline);
86         DBG("Try generic player '%s'", buf);
87         if (access(buf, R_OK | X_OK) == 0)
88           {
89              INF("Using fallback player '%s'", buf);
90              return buf;
91           }
92      }
93
94    ERR("no generic player found, given name='%s'", name ? name : "");
95    return NULL;
96 }
97
98 static void
99 _player_send_cmd(Emotion_Generic_Video *ev, int cmd)
100 {
101    if (cmd >= EM_CMD_LAST)
102      {
103         ERR("invalid command to player.");
104         return;
105      }
106    write(ev->fd_write, &cmd, sizeof(cmd));
107 }
108
109 static void
110 _player_send_int(Emotion_Generic_Video *ev, int number)
111 {
112    write(ev->fd_write, &number, sizeof(number));
113 }
114
115 static void
116 _player_send_float(Emotion_Generic_Video *ev, float number)
117 {
118    write(ev->fd_write, &number, sizeof(number));
119 }
120
121 static void
122 _player_send_str(Emotion_Generic_Video *ev, const char *str, Eina_Bool stringshared)
123 {
124    int len;
125
126    if (stringshared)
127      len = eina_stringshare_strlen(str) + 1;
128    else
129      len = strlen(str) + 1;
130    write(ev->fd_write, &len, sizeof(len));
131    write(ev->fd_write, str, len);
132 }
133
134 static Eina_Bool
135 _create_shm_data(Emotion_Generic_Video *ev, const char *shmname)
136 {
137    int shmfd;
138    int npages;
139    size_t size;
140    Emotion_Generic_Video_Shared *vs;
141
142    shmfd = shm_open(shmname, O_CREAT | O_RDWR | O_TRUNC, 0777);
143    if (shmfd == -1)
144      {
145         ERR("player: could not open shm: %s", shmname);
146         ERR("player: %s", strerror(errno));
147         return 0;
148      }
149    size = 3 * (ev->w * ev->h * DEFAULTPITCH) + sizeof(*vs);
150
151    npages = (int)(size / getpagesize()) + 1;
152    size = npages * getpagesize();
153
154    if (ftruncate(shmfd, size))
155      {
156         ERR("error when allocating shared memory (size = %zd): "
157             "%s", size, strerror(errno));
158         shm_unlink(shmname);
159         return EINA_FALSE;
160      }
161    vs = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0);
162    if (vs == MAP_FAILED)
163      {
164         ERR("error when mapping shared memory.\n");
165         return EINA_FALSE;
166      }
167
168    vs->size = size;
169    vs->width = ev->w;
170    vs->height = ev->h;
171    vs->pitch = DEFAULTPITCH;
172    vs->frame.emotion = 0;
173    vs->frame.player = 1;
174    vs->frame.last = 2;
175    vs->frame.next = 2;
176    vs->frame_drop = 0;
177    sem_init(&vs->lock, 1, 1);
178    ev->frame.frames[0] = (unsigned char *)vs + sizeof(*vs);
179    ev->frame.frames[1] = (unsigned char *)vs + sizeof(*vs) + vs->height * vs->width * vs->pitch;
180    ev->frame.frames[2] = (unsigned char *)vs + sizeof(*vs) + 2 * vs->height * vs->width * vs->pitch;
181
182    if (ev->shared)
183      munmap(ev->shared, ev->shared->size);
184    ev->shared = vs;
185
186    return EINA_TRUE;
187 }
188
189 static void
190 _player_new_frame(Emotion_Generic_Video *ev)
191 {
192    if (ev->opening || ev->closing)
193      return;
194    _emotion_frame_new(ev->obj);
195 }
196
197 static void
198 _file_open(Emotion_Generic_Video *ev)
199 {
200    INF("Opening file: %s", ev->filename);
201    ev->drop = 0;
202
203    if (!ev->ready || !ev->filename)
204      return;
205    _player_send_cmd(ev, EM_CMD_FILE_SET);
206    _player_send_str(ev, ev->filename, EINA_TRUE);
207 }
208
209 static void
210 _player_file_set_done(Emotion_Generic_Video *ev)
211 {
212    if (ev->file_changed)
213      {
214         _file_open(ev);
215         ev->file_changed = EINA_FALSE;
216         return;
217      }
218
219    if (!_create_shm_data(ev, ev->shmname))
220      {
221         ERR("could not create shared memory.");
222         return;
223      }
224    _player_send_cmd(ev, EM_CMD_FILE_SET_DONE);
225 }
226
227 static void
228 _player_ready(Emotion_Generic_Video *ev)
229 {
230    INF("received: player ready.");
231
232    ev->initializing = EINA_FALSE;
233    ev->ready = EINA_TRUE;
234
235    if (!ev->filename)
236      return;
237
238    _file_open(ev);
239 }
240
241 static int
242 _em_read_safe(int fd, void *buf, ssize_t size)
243 {
244    ssize_t todo;
245    char *p;
246
247    todo = size;
248    p = buf;
249
250    while (todo > 0)
251      {
252         ssize_t r;
253
254         r = read(fd, p, todo);
255         if (r > 0)
256           {
257              todo -= r;
258              p += r;
259           }
260         else if (r == 0)
261           return 0;
262         else
263           {
264              if (errno == EINTR || errno == EAGAIN)
265                continue;
266              else
267                {
268                   ERR("could not read from fd %d: %s", fd, strerror(errno));
269                   return 0;
270                }
271           }
272      }
273
274    return 1;
275 }
276
277 static Eina_Bool
278 _player_int_read(Emotion_Generic_Video *ev, int *i)
279 {
280    int n;
281    n = _em_read_safe(ev->fd_read, i, sizeof(*i));
282    if (n <= 0)
283      {
284         ERR("could not read int from fd_read %d\n", ev->fd_read);
285         return EINA_FALSE;
286      }
287
288    return EINA_TRUE;
289 }
290
291 static Eina_Bool
292 _player_float_read(Emotion_Generic_Video *ev, float *f)
293 {
294    int n;
295    n = _em_read_safe(ev->fd_read, f, sizeof(*f));
296    if (n <= 0)
297      {
298         ERR("could not read float from fd_read %d\n", ev->fd_read);
299         return EINA_FALSE;
300      }
301
302    return EINA_TRUE;
303 }
304
305 static Eina_Bool
306 _player_str_read(Emotion_Generic_Video *ev, char *str, int *len)
307 {
308    int n;
309
310    if (!_player_int_read(ev, len))
311      return EINA_FALSE;
312
313    n = _em_read_safe(ev->fd_read, str, *len);
314    if (n <= 0)
315      {
316         ERR("could not read string from fd_read %d\n", ev->fd_read);
317         return EINA_FALSE;
318      }
319
320    return EINA_TRUE;
321 }
322
323 static void
324 _player_frame_resize(Emotion_Generic_Video *ev)
325 {
326    int w, h;
327    _player_int_read(ev, &w);
328    _player_int_read(ev, &h);
329
330    INF("received frame resize: %dx%d", w, h);
331    ev->w = w;
332    ev->h = h;
333    ev->ratio = (float)w / h;
334
335    if (ev->opening)
336      return;
337
338    _emotion_frame_resize(ev->obj, ev->w, ev->h, ev->ratio);
339 }
340
341 static void
342 _player_length_changed(Emotion_Generic_Video *ev)
343 {
344    float length;
345    _player_float_read(ev, &length);
346
347    INF("received length changed: %0.3f", length);
348
349    ev->len = length;
350    _emotion_video_pos_update(ev->obj, ev->pos, ev->len);
351 }
352
353 static void
354 _player_position_changed(Emotion_Generic_Video *ev)
355 {
356    float position;
357    _player_float_read(ev, &position);
358
359    INF("received position changed: %0.3f", position);
360
361    ev->pos = position;
362    _emotion_video_pos_update(ev->obj, ev->pos, ev->len);
363
364    if (ev->len == 0)
365      return;
366
367    float progress = ev->pos / ev->len;
368    char buf[16];
369    snprintf(buf, sizeof(buf), "%0.1f%%", progress * 100);
370
371    _emotion_progress_set(ev->obj, buf, progress);
372 }
373
374 static void
375 _player_seekable_changed(Emotion_Generic_Video *ev)
376 {
377    int seekable;
378    _player_int_read(ev, &seekable);
379
380    INF("received seekable changed: %d", seekable);
381
382    seekable = !!seekable;
383
384    ev->seekable = seekable;
385 }
386
387 static void
388 _player_volume(Emotion_Generic_Video *ev)
389 {
390    float vol, oldvol;
391    _player_float_read(ev, &vol);
392
393    INF("received volume: %0.3f", vol);
394
395    oldvol = ev->volume;
396    ev->volume = vol;
397    if (vol != oldvol && !ev->opening)
398      _emotion_audio_level_change(ev->obj);
399 }
400
401 static void
402 _player_audio_mute(Emotion_Generic_Video *ev)
403 {
404    int mute;
405    _player_int_read(ev, &mute);
406
407    INF("received audio mute: %d", mute);
408
409    ev->audio_mute = !!mute;
410 }
411
412 static void
413 _audio_channels_free(Emotion_Generic_Video *ev)
414 {
415    int i;
416    for (i = 0; i < ev->audio_channels_count; i++)
417      eina_stringshare_del(ev->audio_channels[i].name);
418    free(ev->audio_channels);
419    ev->audio_channels_count = 0;
420 }
421
422 static void
423 _video_channels_free(Emotion_Generic_Video *ev)
424 {
425    int i;
426    for (i = 0; i < ev->video_channels_count; i++)
427      eina_stringshare_del(ev->video_channels[i].name);
428    free(ev->video_channels);
429    ev->video_channels_count = 0;
430 }
431
432 static void
433 _spu_channels_free(Emotion_Generic_Video *ev)
434 {
435    int i;
436    for (i = 0; i < ev->spu_channels_count; i++)
437      eina_stringshare_del(ev->spu_channels[i].name);
438    free(ev->spu_channels);
439    ev->spu_channels_count = 0;
440 }
441
442 static void
443 _player_tracks_info(Emotion_Generic_Video *ev, Emotion_Generic_Channel **channels, int *count, int *current)
444 {
445    Emotion_Generic_Channel *pchannels;
446    int i;
447
448    _player_int_read(ev, current);
449    _player_int_read(ev, count);
450
451    INF("number of tracks: %d (current = %d):", *count, *current);
452    pchannels = calloc(*count, sizeof(Emotion_Generic_Channel));
453    for (i = 0; i < *count; i++)
454      {
455         int tid, len;
456         char buf[PATH_MAX];
457         _player_int_read(ev, &tid);
458         _player_str_read(ev, buf, &len);
459         pchannels[i].id = tid;
460         pchannels[i].name = eina_stringshare_add_length(buf, len);
461         INF("\tchannel %d: %s", tid, buf);
462      }
463
464    *channels = pchannels;
465 }
466
467 static void
468 _player_audio_tracks_info(Emotion_Generic_Video *ev)
469 {
470    INF("Receiving audio channels:");
471    if (ev->audio_channels_count)
472      _audio_channels_free(ev);
473
474    _player_tracks_info(ev, &ev->audio_channels, &ev->audio_channels_count,
475                        &ev->audio_channel_current);
476 }
477
478 static void
479 _player_video_tracks_info(Emotion_Generic_Video *ev)
480 {
481    INF("Receiving video channels:");
482    if (ev->video_channels_count)
483      _video_channels_free(ev);
484
485    _player_tracks_info(ev, &ev->video_channels, &ev->video_channels_count,
486                        &ev->video_channel_current);
487 }
488
489 static void
490 _player_spu_tracks_info(Emotion_Generic_Video *ev)
491 {
492    INF("Receiving spu channels:");
493    if (ev->spu_channels_count)
494      _spu_channels_free(ev);
495
496    _player_tracks_info(ev, &ev->spu_channels, &ev->spu_channels_count,
497                        &ev->spu_channel_current);
498 }
499
500 static void
501 _player_helper_str_read(Emotion_Generic_Video *ev, const char **pstr)
502 {
503    int len;
504    char buf[PATH_MAX];
505    if (_player_str_read(ev, buf, &len))
506      *pstr = eina_stringshare_add_length(buf, len);
507 }
508
509 static void
510 _player_meta_info_free(Emotion_Generic_Video *ev)
511 {
512    eina_stringshare_replace(&ev->meta.title, NULL);
513    eina_stringshare_replace(&ev->meta.artist, NULL);
514    eina_stringshare_replace(&ev->meta.album, NULL);
515    eina_stringshare_replace(&ev->meta.year, NULL);
516    eina_stringshare_replace(&ev->meta.genre, NULL);
517    eina_stringshare_replace(&ev->meta.comment, NULL);
518    eina_stringshare_replace(&ev->meta.disc_id, NULL);
519    eina_stringshare_replace(&ev->meta.count, NULL);
520 }
521
522 static void
523 _player_meta_info_read(Emotion_Generic_Video *ev)
524 {
525    INF("Receiving meta info:");
526    _player_meta_info_free(ev);
527    _player_helper_str_read(ev, &ev->meta.title);
528    _player_helper_str_read(ev, &ev->meta.artist);
529    _player_helper_str_read(ev, &ev->meta.album);
530    _player_helper_str_read(ev, &ev->meta.year);
531    _player_helper_str_read(ev, &ev->meta.genre);
532    _player_helper_str_read(ev, &ev->meta.comment);
533    _player_helper_str_read(ev, &ev->meta.disc_id);
534    _player_helper_str_read(ev, &ev->meta.count);
535    INF("title: '%s'", ev->meta.title);
536    INF("artist: '%s'", ev->meta.artist);
537    INF("album: '%s'", ev->meta.album);
538    INF("year: '%s'", ev->meta.year);
539    INF("genre: '%s'", ev->meta.genre);
540    INF("comment: '%s'", ev->meta.comment);
541    INF("disc_id: '%s'", ev->meta.disc_id);
542    INF("count: '%s'", ev->meta.count);
543 }
544
545 static void
546 _player_file_closed(Emotion_Generic_Video *ev)
547 {
548    INF("Closed previous file.");
549    sem_destroy(&ev->shared->lock);
550
551    ev->closing = EINA_FALSE;
552
553    if (ev->opening)
554      _file_open(ev);
555 }
556
557 static void
558 _player_open_done(Emotion_Generic_Video *ev)
559 {
560    int success;
561
562    _player_int_read(ev, &success);
563    shm_unlink(ev->shmname);
564
565    if (ev->file_changed)
566      {
567         _file_open(ev);
568         ev->file_changed = EINA_FALSE;
569         return;
570      }
571
572    ev->opening = EINA_FALSE;
573    if (!success)
574      {
575         ERR("Could not open file.");
576         return;
577      }
578
579    _emotion_open_done(ev->obj);
580
581    if (ev->play)
582      {
583         _player_send_cmd(ev, EM_CMD_PLAY);
584         _player_send_float(ev, ev->pos);
585      }
586
587    INF("Open done");
588 }
589
590 static void
591 _player_read_cmd(Emotion_Generic_Video *ev)
592 {
593    int type;
594
595    if (!_player_int_read(ev, &type))
596      {
597         ERR("could not read command\n");
598         return;
599      }
600
601    switch (type) {
602       case EM_RESULT_INIT:
603          _player_ready(ev);
604          break;
605       case EM_RESULT_FRAME_NEW:
606          _player_new_frame(ev);
607          break;
608       case EM_RESULT_FILE_SET:
609          _player_file_set_done(ev);
610          break;
611       case EM_RESULT_FILE_SET_DONE:
612          _player_open_done(ev);
613          break;
614       case EM_RESULT_FILE_CLOSE:
615          _player_file_closed(ev);
616          break;
617       case EM_RESULT_PLAYBACK_STOPPED:
618          _emotion_playback_finished(ev->obj);
619          break;
620       case EM_RESULT_FRAME_SIZE:
621          _player_frame_resize(ev);
622          break;
623       case EM_RESULT_LENGTH_CHANGED:
624          _player_length_changed(ev);
625          break;
626       case EM_RESULT_POSITION_CHANGED:
627          _player_position_changed(ev);
628          break;
629       case EM_RESULT_SEEKABLE_CHANGED:
630          _player_seekable_changed(ev);
631          break;
632       case EM_RESULT_AUDIO_TRACK_INFO:
633          _player_audio_tracks_info(ev);
634          break;
635       case EM_RESULT_VIDEO_TRACK_INFO:
636          _player_video_tracks_info(ev);
637          break;
638       case EM_RESULT_SPU_TRACK_INFO:
639          _player_spu_tracks_info(ev);
640          break;
641       case EM_RESULT_META_INFO:
642          _player_meta_info_read(ev);
643          break;
644       default:
645          WRN("received wrong command: %d", type);
646    };
647 }
648
649 static Eina_Bool
650 _player_cmd_handler_cb(void *data, Ecore_Fd_Handler *fd_handler)
651 {
652    Emotion_Generic_Video *ev = data;
653
654    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
655      {
656         ERR("an error occurred on fd_read %d.", ev->fd_read);
657         return ECORE_CALLBACK_CANCEL;
658      }
659
660    _player_read_cmd(ev);
661
662    return ECORE_CALLBACK_RENEW;
663 }
664
665 static Eina_Bool
666 _player_data_cb(void *data, int type __UNUSED__, void *event)
667 {
668    Ecore_Exe_Event_Data *ev = event;
669    Emotion_Generic_Video *evideo = data;
670    int i;
671
672    if (ev->exe != evideo->player.exe)
673      {
674         ERR("slave != ev->exe");
675         return ECORE_CALLBACK_DONE;
676      }
677
678    if (ev->size < 4)
679      {
680         ERR("invalid command: missing bytes.");
681         return ECORE_CALLBACK_DONE;
682      }
683
684    for (i = 0; ev->lines[i].line; i++)
685      INF("received input from player: \"%s\"", ev->lines[i].line);
686
687    return ECORE_CALLBACK_DONE;
688 }
689
690 static Eina_Bool
691 _player_add_cb(void *data, int type __UNUSED__, void *event)
692 {
693    Ecore_Exe_Event_Add *event_add = event;
694    Ecore_Exe *player = event_add->exe;
695    Emotion_Generic_Video *ev = data;
696
697    if (ev->player.exe != player)
698      {
699         ERR("ev->player != player.");
700         return ECORE_CALLBACK_DONE;
701      }
702
703    _player_send_cmd(ev, EM_CMD_INIT);
704    _player_send_str(ev, ev->shmname, EINA_TRUE);
705
706    return ECORE_CALLBACK_DONE;
707 }
708
709 static Eina_Bool
710 _player_del_cb(void *data, int type __UNUSED__, void *event __UNUSED__)
711 {
712    Emotion_Generic_Video *ev = data;
713    ERR("player died.");
714
715    ev->player.exe = NULL;
716    ev->ready = EINA_FALSE;
717    ecore_main_fd_handler_del(ev->fd_handler);
718    close(ev->fd_read);
719    close(ev->fd_write);
720    ev->fd_read = -1;
721    ev->fd_write = -1;
722    _emotion_decode_stop(ev->obj);
723
724    return ECORE_CALLBACK_DONE;
725 }
726
727 static Eina_Bool
728 _player_exec(Emotion_Generic_Video *ev)
729 {
730    int pipe_out[2];
731    int pipe_in[2];
732    char buf[PATH_MAX];
733
734    if (pipe(pipe_out) == -1)
735      {
736         ERR("could not create pipe for communication emotion -> player: %s", strerror(errno));
737         return EINA_FALSE;
738      }
739
740    if (pipe(pipe_in) == -1)
741      {
742         ERR("could not create pipe for communication player -> emotion: %s", strerror(errno));
743         close(pipe_out[0]);
744         close(pipe_out[1]);
745         return EINA_FALSE;
746      }
747
748    snprintf(buf, sizeof(buf), "%s %d %d\n", ev->cmdline, pipe_out[0], pipe_in[1]);
749
750    ev->player.exe = ecore_exe_pipe_run(
751       buf,
752       ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_WRITE |
753       ECORE_EXE_PIPE_READ_LINE_BUFFERED | ECORE_EXE_NOT_LEADER,
754       ev);
755
756    INF("created pipe emotion -> player: %d -> %d\n", pipe_out[1], pipe_out[0]);
757    INF("created pipe player -> emotion: %d -> %d\n", pipe_in[1], pipe_in[0]);
758
759    close(pipe_in[1]);
760    close(pipe_out[0]);
761
762    if (!ev->player.exe)
763      {
764         close(pipe_in[0]);
765         close(pipe_out[1]);
766         return EINA_FALSE;
767      }
768
769    ev->fd_read = pipe_in[0];
770    ev->fd_write = pipe_out[1];
771
772    ev->fd_handler = ecore_main_fd_handler_add(
773       ev->fd_read, ECORE_FD_READ | ECORE_FD_ERROR, _player_cmd_handler_cb, ev,
774       NULL, NULL);
775
776    return EINA_TRUE;
777 }
778
779 static Eina_Bool
780 _fork_and_exec(Evas_Object *obj, Emotion_Generic_Video *ev)
781 {
782    char shmname[256];
783    struct timeval tv;
784
785    gettimeofday(&tv, NULL);
786    snprintf(shmname, sizeof(shmname), "/em-generic-shm_%d_%d",
787             (int)tv.tv_sec, (int)tv.tv_usec);
788
789    ev->shmname = eina_stringshare_add(shmname);
790
791    ev->player_add = ecore_event_handler_add(
792       ECORE_EXE_EVENT_ADD, _player_add_cb, ev);
793    ev->player_del = ecore_event_handler_add(
794       ECORE_EXE_EVENT_DEL, _player_del_cb, ev);
795    ev->player_data = ecore_event_handler_add(
796       ECORE_EXE_EVENT_DATA, _player_data_cb, ev);
797
798
799    if (!_player_exec(ev))
800      {
801         ERR("could not start player.");
802         return EINA_FALSE;
803      }
804
805    ev->initializing = EINA_TRUE;
806
807    return EINA_TRUE;
808 }
809
810 static unsigned char
811 em_init(Evas_Object *obj, void **emotion_video, Emotion_Module_Options *opt)
812 {
813    Emotion_Generic_Video *ev;
814    const char *player;
815
816    if (!emotion_video) return 0;
817    player = _get_player(opt ? opt->player : NULL);
818    if (!player) return 0;
819
820    ev = (Emotion_Generic_Video *)calloc(1, sizeof(*ev));
821    if (!ev) return 0;
822
823    ev->fd_read = -1;
824    ev->fd_write = -1;
825    ev->speed = 1.0;
826    ev->volume = 0.5;
827    ev->audio_mute = EINA_FALSE;
828
829    ev->obj = obj;
830    ev->cmdline = eina_stringshare_add(player);
831    *emotion_video = ev;
832
833    return _fork_and_exec(obj, ev);
834 }
835
836 static int
837 em_shutdown(void *data)
838 {
839    Emotion_Generic_Video *ev = data;
840
841    if (!ev) return 0;
842
843    if (ev->player.exe)
844      {
845         ecore_exe_terminate(ev->player.exe);
846         ecore_exe_free(ev->player.exe);
847         ev->player.exe = NULL;
848      }
849
850    if (ev->shared)
851      munmap(ev->shared, ev->shared->size);
852
853    if (ev->fd_read >= 0)
854      close(ev->fd_read);
855    if (ev->fd_write >= 0)
856      close(ev->fd_write);
857    if (ev->fd_handler)
858      ecore_main_fd_handler_del(ev->fd_handler);
859
860    _audio_channels_free(ev);
861    _video_channels_free(ev);
862    _spu_channels_free(ev);
863    _player_meta_info_free(ev);
864
865    eina_stringshare_del(ev->cmdline);
866    eina_stringshare_del(ev->shmname);
867
868    ecore_event_handler_del(ev->player_add);
869    ecore_event_handler_del(ev->player_data);
870    ecore_event_handler_del(ev->player_del);
871
872    return 1;
873 }
874
875 static unsigned char
876 em_file_open(const char *file, Evas_Object *obj __UNUSED__, void *data)
877 {
878    Emotion_Generic_Video *ev = data;
879    INF("file set: %s", file);
880    if (!ev) return 0;
881
882    eina_stringshare_replace(&ev->filename, file);
883
884    ev->pos = 0;
885    ev->w = 0;
886    ev->h = 0;
887    ev->ratio = 1;
888    ev->speed = 1.0;
889    ev->len = 0;
890
891    if (ev->ready && ev->opening)
892      {
893         INF("file changed while opening.");
894         ev->file_changed = EINA_TRUE;
895         return 1;
896      }
897
898    ev->opening = EINA_TRUE;
899
900    if (!ev->closing)
901      _file_open(ev);
902
903    return 1;
904 }
905
906 static void
907 em_file_close(void *data)
908 {
909    Emotion_Generic_Video *ev = data;
910
911    if (!ev || !ev->filename) return;
912
913    INF("file close: %s", ev->filename);
914
915    eina_stringshare_replace(&ev->filename, NULL);
916
917    if (ev->opening)
918      return;
919
920    _player_send_cmd(ev, EM_CMD_FILE_CLOSE);
921    ev->closing = EINA_TRUE;
922 }
923
924 static Emotion_Format
925 em_format_get(void *ef __UNUSED__)
926 {
927    return EMOTION_FORMAT_BGRA;
928 }
929
930 static void
931 em_video_data_size_get(void *data, int *w, int *h)
932 {
933    Emotion_Generic_Video *ev = data;
934
935    if (!ev) return;
936    if (w) *w = ev->w;
937    if (h) *h = ev->h;
938 }
939
940 static void
941 em_play(void *data, double pos)
942 {
943    Emotion_Generic_Video *ev = data;
944
945    if (!ev)
946      return;
947
948    ev->play = EINA_TRUE;
949    INF("play: %0.3f", pos);
950
951    if (ev->initializing || ev->opening)
952      return;
953
954    if (ev->ready)
955      {
956         _player_send_cmd(ev, EM_CMD_PLAY);
957         _player_send_float(ev, ev->pos);
958         return;
959      }
960
961    if (!_player_exec(ev))
962      ERR("could not start player.");
963 }
964
965 static void
966 em_stop(void *data)
967 {
968    Emotion_Generic_Video *ev = data;
969
970    if (!ev)
971      return;
972
973    ev->play = EINA_FALSE;
974
975    if (!ev->ready)
976      return;
977
978    _player_send_cmd(ev, EM_CMD_STOP);
979    _emotion_decode_stop(ev->obj);
980 }
981
982 static void
983 em_size_get(void *data, int *w, int *h)
984 {
985    Emotion_Generic_Video *ev = data;
986    if (w) *w = ev->w;
987    if (h) *h = ev->h;
988 }
989
990 static void
991 em_pos_set(void *data, double pos)
992 {
993    Emotion_Generic_Video *ev = data;
994    float position = pos;
995    _player_send_cmd(ev, EM_CMD_POSITION_SET);
996    _player_send_float(ev, position);
997    _emotion_seek_done(ev->obj);
998 }
999
1000 static double
1001 em_len_get(void *data)
1002 {
1003    Emotion_Generic_Video *ev = data;
1004    return ev->len;
1005 }
1006
1007 static int
1008 em_fps_num_get(void *data)
1009 {
1010    Emotion_Generic_Video *ev = data;
1011    return (int)(ev->fps * 1000.0);
1012 }
1013
1014 static int
1015 em_fps_den_get(void *ef __UNUSED__)
1016 {
1017    return 1000;
1018 }
1019
1020 static double
1021 em_fps_get(void *data)
1022 {
1023    Emotion_Generic_Video *ev = data;
1024    return ev->fps;
1025 }
1026
1027 static double
1028 em_pos_get(void *data)
1029 {
1030    Emotion_Generic_Video *ev = data;
1031    return ev->pos;
1032 }
1033
1034 static void
1035 em_vis_set(void *ef __UNUSED__, Emotion_Vis vis __UNUSED__)
1036 {
1037 }
1038
1039 static Emotion_Vis
1040 em_vis_get(void *data)
1041 {
1042    Emotion_Generic_Video *ev = data;
1043    return ev->vis;
1044 }
1045
1046 static Eina_Bool
1047 em_vis_supported(void *ef __UNUSED__, Emotion_Vis vis __UNUSED__)
1048 {
1049    return EINA_FALSE;
1050 }
1051
1052 static double
1053 em_ratio_get(void *data)
1054 {
1055    Emotion_Generic_Video *ev = data;
1056    return ev->ratio;
1057 }
1058
1059 static int em_video_handled(void *ef __UNUSED__)
1060 {
1061    fprintf(stderr, "video handled!\n");
1062    return 1;
1063 }
1064
1065 static int em_audio_handled(void *ef __UNUSED__)
1066 {
1067    fprintf(stderr, "audio handled!\n");
1068    return 1;
1069 }
1070
1071 static int em_seekable(void *data)
1072 {
1073    Emotion_Generic_Video *ev = data;
1074    return ev->seekable;
1075 }
1076
1077 static void em_frame_done(void *ef __UNUSED__)
1078 {
1079 }
1080
1081 static int
1082 em_yuv_rows_get(void *data __UNUSED__, int w __UNUSED__, int h __UNUSED__, unsigned char **yrows __UNUSED__, unsigned char **urows __UNUSED__, unsigned char **vrows __UNUSED__)
1083 {
1084    return 0;
1085 }
1086
1087 static int
1088 em_bgra_data_get(void *data, unsigned char **bgra_data)
1089 {
1090    Emotion_Generic_Video *ev = data;
1091
1092    if (!ev || ev->opening || ev->closing)
1093      return 0;
1094
1095    // lock frame here
1096    sem_wait(&ev->shared->lock);
1097
1098    // send current frame to emotion
1099    if (ev->shared->frame.emotion != ev->shared->frame.last)
1100      {
1101         ev->shared->frame.next = ev->shared->frame.emotion;
1102         ev->shared->frame.emotion = ev->shared->frame.last;
1103      }
1104    *bgra_data = ev->frame.frames[ev->shared->frame.emotion];
1105
1106    if (ev->shared->frame_drop > 1)
1107      WRN("dropped frames: %d", ev->shared->frame_drop - 1);
1108    ev->shared->frame_drop = 0;
1109
1110    // unlock frame here
1111    sem_post(&ev->shared->lock);
1112    ev->drop = 0;
1113
1114    return 1;
1115 }
1116
1117 static void
1118 em_event_feed(void *ef __UNUSED__, int event __UNUSED__)
1119 {
1120 }
1121
1122 static void
1123 em_event_mouse_button_feed(void *ef __UNUSED__, int button __UNUSED__, int x __UNUSED__, int y __UNUSED__)
1124 {
1125 }
1126
1127 static void
1128 em_event_mouse_move_feed(void *ef __UNUSED__, int x __UNUSED__, int y __UNUSED__)
1129 {
1130 }
1131
1132 static int
1133 em_video_channel_count(void *data)
1134 {
1135    Emotion_Generic_Video *ev = data;
1136    return ev->video_channels_count;
1137 }
1138
1139 static void
1140 em_video_channel_set(void *data, int channel)
1141 {
1142    Emotion_Generic_Video *ev = data;
1143
1144    if (channel < 0 || channel >= ev->video_channels_count)
1145      {
1146         WRN("video channel out of range.");
1147         return;
1148      }
1149
1150    _player_send_cmd(ev, EM_CMD_VIDEO_TRACK_SET);
1151    _player_send_int(ev, ev->video_channels[channel].id);
1152    ev->video_channel_current = channel;
1153 }
1154
1155 static int
1156 em_video_channel_get(void *data)
1157 {
1158    Emotion_Generic_Video *ev = data;
1159    return ev->video_channel_current;
1160 }
1161
1162 static const char *
1163 em_video_channel_name_get(void *data, int channel)
1164 {
1165    Emotion_Generic_Video *ev = data;
1166
1167    if (channel < 0 || channel >= ev->video_channels_count)
1168      {
1169         WRN("video channel out of range.");
1170         return NULL;
1171      }
1172
1173    return ev->video_channels[channel].name;
1174 }
1175
1176 static void
1177 em_video_channel_mute_set(void *data, int mute)
1178 {
1179    Emotion_Generic_Video *ev = data;
1180    _player_send_cmd(ev, EM_CMD_VIDEO_MUTE_SET);
1181    _player_send_int(ev, mute);
1182    ev->video_mute = !!mute;
1183 }
1184
1185 static int
1186 em_video_channel_mute_get(void *data)
1187 {
1188    Emotion_Generic_Video *ev = data;
1189    return ev->video_mute;
1190 }
1191
1192 static int
1193 em_audio_channel_count(void *data)
1194 {
1195    Emotion_Generic_Video *ev = data;
1196    return ev->audio_channels_count;
1197 }
1198
1199 static void
1200 em_audio_channel_set(void *data, int channel)
1201 {
1202    Emotion_Generic_Video *ev = data;
1203
1204    if (channel < 0 || channel >= ev->audio_channels_count)
1205      {
1206         WRN("audio channel out of range.");
1207         return;
1208      }
1209
1210    _player_send_cmd(ev, EM_CMD_AUDIO_TRACK_SET);
1211    _player_send_int(ev, ev->audio_channels[channel].id);
1212    ev->audio_channel_current = channel;
1213 }
1214
1215 static int
1216 em_audio_channel_get(void *data)
1217 {
1218    Emotion_Generic_Video *ev = data;
1219    return ev->audio_channel_current;
1220 }
1221
1222 static const char *
1223 em_audio_channel_name_get(void *data, int channel)
1224 {
1225    Emotion_Generic_Video *ev = data;
1226
1227    if (channel < 0 || channel >= ev->audio_channels_count)
1228      {
1229         WRN("audio channel out of range.");
1230         return NULL;
1231      }
1232
1233    return ev->audio_channels[channel].name;
1234 }
1235
1236 static void
1237 em_audio_channel_mute_set(void *data, int mute)
1238 {
1239    Emotion_Generic_Video *ev = data;
1240    _player_send_cmd(ev, EM_CMD_AUDIO_MUTE_SET);
1241    _player_send_int(ev, mute);
1242    ev->audio_mute = !!mute;
1243 }
1244
1245 static int
1246 em_audio_channel_mute_get(void *data)
1247 {
1248    Emotion_Generic_Video *ev = data;
1249    return ev->audio_mute;
1250 }
1251
1252 static void
1253 em_audio_channel_volume_set(void *data, double vol)
1254 {
1255    Emotion_Generic_Video *ev = data;
1256    float fvol;
1257
1258    if (vol > 1.0) vol = 1.0;
1259    if (vol < 0.0) vol = 0.0;
1260
1261    fvol = vol;
1262    _player_send_cmd(ev, EM_CMD_VOLUME_SET);
1263    _player_send_float(ev, fvol);
1264
1265    ev->volume = vol;
1266 }
1267
1268 static double
1269 em_audio_channel_volume_get(void *data)
1270 {
1271    Emotion_Generic_Video *ev = data;
1272    return ev->volume;
1273 }
1274
1275 static int
1276 em_spu_channel_count(void *data)
1277 {
1278    Emotion_Generic_Video *ev = data;
1279    return ev->spu_channels_count;
1280 }
1281
1282 static void
1283 em_spu_channel_set(void *data, int channel)
1284 {
1285    Emotion_Generic_Video *ev = data;
1286
1287    if (channel < 0 || channel >= ev->spu_channels_count)
1288      {
1289         WRN("spu channel out of range.");
1290         return;
1291      }
1292
1293    _player_send_cmd(ev, EM_CMD_SPU_TRACK_SET);
1294    _player_send_int(ev, ev->spu_channels[channel].id);
1295    ev->spu_channel_current = channel;
1296 }
1297
1298 static int
1299 em_spu_channel_get(void *data)
1300 {
1301    Emotion_Generic_Video *ev = data;
1302    return ev->spu_channel_current;
1303 }
1304
1305 static const char *
1306 em_spu_channel_name_get(void *data, int channel)
1307 {
1308    Emotion_Generic_Video *ev = data;
1309
1310    if (channel < 0 || channel >= ev->spu_channels_count)
1311      {
1312         WRN("spu channel out of range.");
1313         return NULL;
1314      }
1315
1316    return ev->spu_channels[channel].name;
1317 }
1318
1319 static void
1320 em_spu_channel_mute_set(void *data, int mute)
1321 {
1322    Emotion_Generic_Video *ev = data;
1323    _player_send_cmd(ev, EM_CMD_SPU_MUTE_SET);
1324    _player_send_int(ev, mute);
1325    ev->spu_mute = !!mute;
1326 }
1327
1328 static int
1329 em_spu_channel_mute_get(void *data)
1330 {
1331    Emotion_Generic_Video *ev = data;
1332    return ev->spu_mute;
1333 }
1334
1335 static int
1336 em_chapter_count(void *ef __UNUSED__)
1337 {
1338    int num = 0;
1339    return num;
1340 }
1341
1342 static void
1343 em_chapter_set(void *ef __UNUSED__, int chapter __UNUSED__)
1344 {
1345 }
1346
1347 static int
1348 em_chapter_get(void *ef __UNUSED__)
1349 {
1350    int num = 0;
1351    return num;
1352 }
1353
1354 static const char *
1355 em_chapter_name_get(void *ef __UNUSED__, int chapter __UNUSED__)
1356 {
1357    return NULL;
1358 }
1359
1360 static void
1361 em_speed_set(void *data, double speed)
1362 {
1363    Emotion_Generic_Video *ev = data;
1364    float rate = speed;
1365
1366    _player_send_cmd(ev, EM_CMD_SPEED_SET);
1367    _player_send_float(ev, rate);
1368
1369    ev->speed = rate;
1370 }
1371
1372 static double
1373 em_speed_get(void *data)
1374 {
1375    Emotion_Generic_Video *ev = data;
1376    return (double)ev->speed;
1377 }
1378
1379 static int
1380 em_eject(void *ef __UNUSED__)
1381 {
1382    return 1;
1383 }
1384
1385 static const char *
1386 em_meta_get(void *data, int meta)
1387 {
1388    Emotion_Generic_Video *ev = data;
1389
1390    switch (meta) {
1391       case EMOTION_META_INFO_TRACK_TITLE:
1392          return ev->meta.title;
1393       case EMOTION_META_INFO_TRACK_ARTIST:
1394          return ev->meta.artist;
1395       case EMOTION_META_INFO_TRACK_ALBUM:
1396          return ev->meta.album;
1397       case EMOTION_META_INFO_TRACK_YEAR:
1398          return ev->meta.year;
1399       case EMOTION_META_INFO_TRACK_GENRE:
1400          return ev->meta.genre;
1401       case EMOTION_META_INFO_TRACK_COMMENT:
1402          return ev->meta.comment;
1403       case EMOTION_META_INFO_TRACK_DISC_ID:
1404          return ev->meta.disc_id;
1405       case EMOTION_META_INFO_TRACK_COUNT:
1406          return ev->meta.count;
1407    }
1408
1409    return NULL;
1410 }
1411
1412 static Emotion_Video_Module em_module =
1413 {
1414    em_init, /* init */
1415    em_shutdown, /* shutdown */
1416    em_file_open, /* file_open */
1417    em_file_close, /* file_close */
1418    em_play, /* play */
1419    em_stop, /* stop */
1420    em_size_get, /* size_get */
1421    em_pos_set, /* pos_set */
1422    em_len_get, /* len_get */
1423    em_fps_num_get, /* fps_num_get */
1424    em_fps_den_get, /* fps_den_get */
1425    em_fps_get, /* fps_get */
1426    em_pos_get, /* pos_get */
1427    em_vis_set, /* vis_set */
1428    em_vis_get, /* vis_get */
1429    em_vis_supported, /* vis_supported */
1430    em_ratio_get, /* ratio_get */
1431    em_video_handled, /* video_handled */
1432    em_audio_handled, /* audio_handled */
1433    em_seekable, /* seekable */
1434    em_frame_done, /* frame_done */
1435    em_format_get, /* format_get */
1436    em_video_data_size_get, /* video_data_size_get */
1437    em_yuv_rows_get, /* yuv_rows_get */
1438    em_bgra_data_get, /* bgra_data_get */
1439    em_event_feed, /* event_feed */
1440    em_event_mouse_button_feed, /* event_mouse_button_feed */
1441    em_event_mouse_move_feed, /* event_mouse_move_feed */
1442    em_video_channel_count, /* video_channel_count */
1443    em_video_channel_set, /* video_channel_set */
1444    em_video_channel_get, /* video_channel_get */
1445    em_video_channel_name_get, /* video_channel_name_get */
1446    em_video_channel_mute_set, /* video_channel_mute_set */
1447    em_video_channel_mute_get, /* video_channel_mute_get */
1448    em_audio_channel_count, /* audio_channel_count */
1449    em_audio_channel_set, /* audio_channel_set */
1450    em_audio_channel_get, /* audio_channel_get */
1451    em_audio_channel_name_get, /* audio_channel_name_get */
1452    em_audio_channel_mute_set, /* audio_channel_mute_set */
1453    em_audio_channel_mute_get, /* audio_channel_mute_get */
1454    em_audio_channel_volume_set, /* audio_channel_volume_set */
1455    em_audio_channel_volume_get, /* audio_channel_volume_get */
1456    em_spu_channel_count, /* spu_channel_count */
1457    em_spu_channel_set, /* spu_channel_set */
1458    em_spu_channel_get, /* spu_channel_get */
1459    em_spu_channel_name_get, /* spu_channel_name_get */
1460    em_spu_channel_mute_set, /* spu_channel_mute_set */
1461    em_spu_channel_mute_get, /* spu_channel_mute_get */
1462    em_chapter_count, /* chapter_count */
1463    em_chapter_set, /* chapter_set */
1464    em_chapter_get, /* chapter_get */
1465    em_chapter_name_get, /* chapter_name_get */
1466    em_speed_set, /* speed_set */
1467    em_speed_get, /* speed_get */
1468    em_eject, /* eject */
1469    em_meta_get, /* meta_get */
1470    NULL /* handle */
1471 };
1472
1473 static Eina_Bool
1474 module_open(Evas_Object *obj, const Emotion_Video_Module **module, void **video, Emotion_Module_Options *opt)
1475 {
1476    if (!module) {
1477         return EINA_FALSE;
1478    }
1479
1480    if (_emotion_generic_log_domain < 0)
1481      {
1482         eina_threads_init();
1483         eina_log_threads_enable();
1484         _emotion_generic_log_domain = eina_log_domain_register
1485           ("emotion-generic", EINA_COLOR_LIGHTCYAN);
1486         if (_emotion_generic_log_domain < 0)
1487           {
1488              EINA_LOG_CRIT("Could not register log domain 'emotion-generic'");
1489              return EINA_FALSE;
1490           }
1491      }
1492
1493
1494    if (!em_module.init(obj, video, opt))        {
1495         return EINA_FALSE;
1496    }
1497
1498    *module = &em_module;
1499
1500    return EINA_TRUE;
1501 }
1502
1503 static void module_close(Emotion_Video_Module *module __UNUSED__, void *video)
1504 {
1505         em_module.shutdown(video);
1506 }
1507
1508
1509 Eina_Bool
1510 generic_module_init(void)
1511 {
1512    if (!pfx)
1513      {
1514         pfx = eina_prefix_new(NULL, emotion_object_add,
1515                               "EMOTION", "emotion", NULL,
1516                               PACKAGE_BIN_DIR,
1517                               PACKAGE_LIB_DIR,
1518                               PACKAGE_DATA_DIR,
1519                               "");
1520         if (!pfx) return EINA_FALSE;
1521      }
1522    return _emotion_module_register("generic", module_open, module_close);
1523 }
1524
1525 static void
1526 generic_module_shutdown(void)
1527 {
1528    if (pfx)
1529      {
1530         eina_prefix_free(pfx);
1531         pfx = NULL;
1532      }
1533    _emotion_module_unregister("generic");
1534 }
1535
1536 #ifndef EMOTION_STATIC_BUILD_GENERIC
1537
1538 EINA_MODULE_INIT(generic_module_init);
1539 EINA_MODULE_SHUTDOWN(generic_module_shutdown);
1540
1541 #endif
1542