emotion/generic - Don't start opening new file when another one is being open.
[profile/ivi/emotion.git] / src / generic_players / vlc / emotion_generic_vlc.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <errno.h>
6 #include <limits.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/mman.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <pthread.h>
15 #include <poll.h>
16
17 #include <sys/prctl.h>
18 #include <signal.h>
19
20 #include <vlc/vlc.h>
21 #include <Emotion_Generic_Plugin.h>
22
23 enum _Thread_Events {
24      EM_THREAD_POSITION_CHANGED,
25      EM_THREAD_LAST
26 };
27
28 struct _App {
29      Emotion_Generic_Video_Shared *vs;
30      Emotion_Generic_Video_Frame vf;
31      libvlc_instance_t *libvlc;
32      libvlc_media_t *m;
33      libvlc_media_player_t *mp;
34      libvlc_event_manager_t *event_mgr;
35      libvlc_event_manager_t *mevent_mgr;
36      char *filename;
37      char *shmname;
38      int w, h;
39      int fd_read; // read commands from theads here
40      int fd_write; // write commands from threads here
41      int em_read; // read commands from emotion here
42      int em_write; // write commands to emotion here
43      int size_sent;
44      int opening;
45      int closing;
46      int playing;
47 };
48
49 static pthread_mutex_t _mutex_fd = PTHREAD_MUTEX_INITIALIZER;
50
51 int
52 _em_read_safe(int fd, void *buf, ssize_t size)
53 {
54    ssize_t todo;
55    char *p;
56
57    todo = size;
58    p = buf;
59
60    while (todo > 0)
61      {
62         ssize_t r;
63
64         r = read(fd, p, todo);
65         if (r > 0)
66           {
67              todo -= r;
68              p += r;
69           }
70         else if (r == 0)
71           return 0;
72         else
73           {
74              if (errno == EINTR || errno == EAGAIN)
75                continue;
76              else
77                {
78                   fprintf(stderr, "could not read from fd %d: %s",
79                           fd, strerror(errno));
80                   return 0;
81                }
82           }
83      }
84
85    return 1;
86 }
87
88 int
89 _em_write_safe(int fd, const void *buf, ssize_t size)
90 {
91    ssize_t todo;
92    const char *p;
93
94    todo = size;
95    p = buf;
96
97    while (todo > 0)
98      {
99         ssize_t r;
100
101         r = write(fd, p, todo);
102         if (r > 0)
103           {
104              todo -= r;
105              p += r;
106           }
107         else if (r == 0)
108           return 0;
109         else
110           {
111              if (errno == EINTR || errno == EAGAIN)
112                continue;
113              else
114                {
115                   fprintf(stderr, "could not write to fd %d: %s",
116                           fd, strerror(errno));
117                   return 0;
118                }
119           }
120      }
121
122    return 1;
123 }
124
125 static int
126 _em_str_read(int fd, char **str)
127 {
128    int size;
129    int r;
130    char buf[PATH_MAX];
131
132    r = _em_read_safe(fd, &size, sizeof(size));
133    if (!r)
134      {
135         *str = NULL;
136         return 0;
137      }
138
139    if (!size)
140      {
141         *str = NULL;
142         return 1;
143      }
144
145    r = _em_read_safe(fd, buf, size);
146    if (!r)
147      {
148         *str = NULL;
149         return 0;
150      }
151
152    *str = strdup(buf);
153    return 1;
154 }
155
156 static int
157 _em_cmd_read(struct _App *app)
158 {
159    int cmd;
160    _em_read_safe(app->em_read, &cmd, sizeof(cmd));
161
162    return cmd;
163 }
164
165 static void
166 _send_cmd_start(struct _App *app, int cmd)
167 {
168    pthread_mutex_lock(&_mutex_fd);
169    _em_write_safe(app->em_write, &cmd, sizeof(cmd));
170 }
171
172 static void
173 _send_cmd_finish(struct _App *app)
174 {
175    pthread_mutex_unlock(&_mutex_fd);
176 }
177
178 static void
179 _send_cmd(struct _App *app, int cmd)
180 {
181    _send_cmd_start(app, cmd);
182    _send_cmd_finish(app);
183 }
184
185 static void
186 _send_cmd_str(struct _App *app, const char *str)
187 {
188    int len;
189    len = strlen(str) + 1;
190    _em_write_safe(app->em_write, &len, sizeof(len));
191    _em_write_safe(app->em_write, str, len);
192 }
193
194 #define SEND_CMD_PARAM(app, i) \
195    _em_write_safe((app)->em_write, &(i), sizeof((i)));
196
197 static void
198 _send_resize(struct _App *app, int width, int height)
199 {
200    _send_cmd_start(app, EM_RESULT_FRAME_SIZE);
201    SEND_CMD_PARAM(app, width);
202    SEND_CMD_PARAM(app, height);
203    _send_cmd_finish(app);
204 }
205
206 static void
207 _send_length_changed(struct _App *app, const struct libvlc_event_t *ev)
208 {
209    float length = ev->u.media_player_length_changed.new_length;
210    length /= 1000;
211
212    fprintf(stderr, "length changed: %0.3f\n", length);
213    _send_cmd_start(app, EM_RESULT_LENGTH_CHANGED);
214    SEND_CMD_PARAM(app, length);
215    _send_cmd_finish(app);
216 }
217
218 static void
219 _send_time_changed(struct _App *app, const struct libvlc_event_t *ev)
220 {
221    float new_time = ev->u.media_player_time_changed.new_time;
222    new_time /= 1000;
223    _send_cmd_start(app, EM_RESULT_POSITION_CHANGED);
224    SEND_CMD_PARAM(app, new_time);
225    _send_cmd_finish(app);
226 }
227
228 static void
229 _send_seekable_changed(struct _App *app, const struct libvlc_event_t *ev)
230 {
231    int seekable = ev->u.media_player_seekable_changed.new_seekable;
232    _send_cmd_start(app, EM_RESULT_SEEKABLE_CHANGED);
233    SEND_CMD_PARAM(app, seekable);
234    _send_cmd_finish(app);
235 }
236
237 static void *
238 _lock(void *data, void **pixels)
239 {
240    struct _App *app = data;
241
242    if (app->playing)
243      *pixels = app->vf.frames[app->vs->frame.player];
244    else
245      *pixels = NULL;
246
247    return NULL; // picture identifier, not needed here
248 }
249
250 static void
251 _unlock(void *data, void *id, void *const *pixels)
252 {
253    struct _App *app = data;
254
255    if (!app->playing)
256      return;
257
258    sem_wait(&app->vs->lock);
259    app->vs->frame.last = app->vs->frame.player;
260    app->vs->frame.player = app->vs->frame.next;
261    app->vs->frame.next = app->vs->frame.last;
262
263    sem_post(&app->vs->lock);
264 }
265
266 static void
267 _display(void *data, void *id)
268 {
269    struct _App *app = data;
270    if (!app->playing)
271      return;
272
273    _send_cmd(app, EM_RESULT_FRAME_NEW);
274 }
275
276 static void *
277 _tmp_lock(void *data, void **pixels)
278 {
279    *pixels = NULL;
280    return NULL;
281 }
282
283 static void
284 _tmp_unlock(void *data, void *id, void *const *pixels)
285 {
286 }
287
288 static void
289 _tmp_display(void *data, void *id)
290 {
291 }
292
293 static void
294 _play(struct _App *app)
295 {
296    float pos;
297
298    if (!app->mp)
299      return;
300
301    _em_read_safe(app->em_read, &pos, sizeof(pos));
302
303    if (app->playing)
304      {
305         libvlc_media_player_set_pause(app->mp, 0);
306      }
307    else
308      {
309         libvlc_time_t new_time = pos * 1000;
310         libvlc_media_player_play(app->mp);
311         libvlc_media_player_set_time(app->mp, new_time);
312         app->playing = 1;
313      }
314 }
315
316 static void
317 _stop(struct _App *app)
318 {
319    if (app->mp)
320      libvlc_media_player_set_pause(app->mp, 1);
321 }
322
323 static void
324 _send_file_closed(struct _App *app)
325 {
326    app->closing = 0;
327    emotion_generic_shm_free(app->vs);
328    _send_cmd(app, EM_RESULT_FILE_CLOSE);
329 }
330
331 static void
332 _send_file_set(struct _App *app)
333 {
334    if (app->opening)
335       _send_cmd(app, EM_RESULT_FILE_SET);
336
337    if (app->closing)
338      _send_file_closed(app);
339 }
340
341 static void
342 _event_cb(const struct libvlc_event_t *ev, void *data)
343 {
344    struct _App *app = data;
345    int thread_event;
346
347    switch (ev->type) {
348       case libvlc_MediaPlayerTimeChanged:
349          _send_time_changed(app, ev);
350          break;
351       case libvlc_MediaPlayerPositionChanged:
352          thread_event = EM_THREAD_POSITION_CHANGED;
353          write(app->fd_write, &thread_event, sizeof(thread_event));
354          break;
355       case libvlc_MediaPlayerLengthChanged:
356          _send_length_changed(app, ev);
357          break;
358       case libvlc_MediaPlayerSeekableChanged:
359          _send_seekable_changed(app, ev);
360          break;
361       case libvlc_MediaPlayerPlaying:
362          _send_resize(app, app->w, app->h);
363          break;
364       case libvlc_MediaPlayerStopped:
365          _send_file_set(app);
366          break;
367       case libvlc_MediaPlayerEndReached:
368          _send_cmd(app, EM_RESULT_PLAYBACK_STOPPED);
369          break;
370    }
371 }
372
373 static void
374 _file_set(struct _App *app)
375 {
376    if (app->opening)
377      {
378         libvlc_media_release(app->m);
379         libvlc_media_player_release(app->mp);
380         free(app->filename);
381      }
382
383    _em_str_read(app->em_read, &app->filename);
384
385    app->m = libvlc_media_new_path(app->libvlc, app->filename);
386    if (!app->m)
387      {
388         fprintf(stderr, "could not open path: \"%s\"\n", app->filename);
389         return;
390      }
391    app->mp = libvlc_media_player_new_from_media(app->m);
392
393    if (!app->mp)
394      {
395         fprintf(stderr, "could not create new player from media.\n");
396         return;
397      }
398
399    app->opening = 1;
400    libvlc_video_set_format(app->mp, "RV32", DEFAULTWIDTH, DEFAULTHEIGHT, DEFAULTWIDTH * 4);
401    libvlc_video_set_callbacks(app->mp, _tmp_lock, _tmp_unlock, _tmp_display, NULL);
402    app->event_mgr = libvlc_media_player_event_manager(app->mp);
403    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerPositionChanged,
404                        _event_cb, app);
405    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerStopped,
406                        _event_cb, app);
407
408    app->mevent_mgr = libvlc_media_event_manager(app->m);
409
410    libvlc_audio_set_mute(app->mp, 1);
411    libvlc_media_player_play(app->mp);
412 }
413
414 static void
415 _position_set(struct _App *app)
416 {
417    if (!app->mp)
418      return;
419
420    float position;
421    _em_read_safe(app->em_read, &position, sizeof(position));
422
423    libvlc_time_t new_time = position * 1000;
424    libvlc_media_player_set_time(app->mp, new_time);
425 }
426
427 static void
428 _speed_set(struct _App *app)
429 {
430    float rate;
431
432    if (!app->mp)
433      return;
434
435    _em_read_safe(app->em_read, &rate, sizeof(rate));
436
437    libvlc_media_player_set_rate(app->mp, rate);
438 }
439
440 static void
441 _mute_set(struct _App *app)
442 {
443    int mute;
444
445    if (!app->mp)
446      return;
447
448    _em_read_safe(app->em_read, &mute, sizeof(mute));
449
450    libvlc_audio_set_mute(app->mp, mute);
451 }
452
453 static void
454 _volume_set(struct _App *app)
455 {
456    float volume;
457    int vol;
458
459    if (!app->mp)
460      return;
461
462    _em_read_safe(app->em_read, &volume, sizeof(volume));
463    vol = volume * 100;
464
465    libvlc_audio_set_volume(app->mp, vol);
466 }
467
468 static void
469 _audio_track_set(struct _App *app)
470 {
471    int track;
472
473    _em_read_safe(app->em_read, &track, sizeof(track));
474
475    libvlc_audio_set_track(app->mp, track);
476 }
477
478 static void
479 _file_set_done(struct _App *app)
480 {
481    int r;
482
483    app->opening = 0;
484
485    r = emotion_generic_shm_get(app->shmname, &app->vs, &app->vf);
486    if (!r)
487      {
488         free(app->filename);
489         libvlc_media_release(app->m);
490         libvlc_media_player_release(app->mp);
491         app->filename = NULL;
492         app->m = NULL;
493         app->mp = NULL;
494         _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
495         SEND_CMD_PARAM(app, r);
496         _send_cmd_finish(app);
497      }
498    app->w = app->vs->width;
499    app->h = app->vs->height;
500    libvlc_video_set_format(app->mp, "RV32", app->w, app->h, app->w * 4);
501    libvlc_video_set_callbacks(app->mp, _lock, _unlock, _display, app);
502
503
504    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerPlaying,
505                        _event_cb, app);
506    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerTimeChanged,
507                        _event_cb, app);
508    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerLengthChanged,
509                        _event_cb, app);
510    libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerSeekableChanged,
511                        _event_cb, app);
512
513    libvlc_audio_set_mute(app->mp, 0);
514
515    _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
516    SEND_CMD_PARAM(app, r);
517    _send_cmd_finish(app);
518 }
519
520 static void
521 _file_close(struct _App *app)
522 {
523    app->playing = 0;
524    if (app->opening)
525      goto release_resources;
526
527    if (libvlc_media_player_get_state(app->mp) != libvlc_Playing)
528      {
529         _send_file_closed(app);
530         return;
531      }
532
533    app->closing = 1;
534
535 release_resources:
536    libvlc_media_player_stop(app->mp);
537    if (app->filename)
538      free(app->filename);
539    if (app->mp)
540      {
541         libvlc_media_release(app->m);
542         libvlc_media_player_release(app->mp);
543      }
544 }
545
546 static void
547 _process_emotion_commands(struct _App *app)
548 {
549    int cmd = _em_cmd_read(app);
550    switch (cmd) {
551       case EM_CMD_FILE_SET:
552          _file_set(app);
553          break;
554       case EM_CMD_FILE_SET_DONE:
555          _file_set_done(app);
556          break;
557       case EM_CMD_FILE_CLOSE:
558          _file_close(app);
559          break;
560       case EM_CMD_PLAY:
561          _play(app);
562          break;
563       case EM_CMD_STOP:
564          _stop(app);
565          break;
566       case EM_CMD_POSITION_SET:
567          _position_set(app);
568          break;
569       case EM_CMD_SPEED_SET:
570          _speed_set(app);
571          break;
572       case EM_CMD_AUDIO_MUTE_SET:
573          _mute_set(app);
574          break;
575       case EM_CMD_VOLUME_SET:
576          _volume_set(app);
577          break;
578       case EM_CMD_AUDIO_TRACK_SET:
579          _audio_track_set(app);
580          break;
581    };
582 }
583
584 static void
585 _send_track_info(struct _App *app)
586 {
587    int track_count, current;
588    libvlc_track_description_t *desc;
589
590    current = libvlc_audio_get_track(app->mp);
591    track_count = libvlc_audio_get_track_count(app->mp);
592    desc = libvlc_audio_get_track_description(app->mp);
593
594    _send_cmd_start(app, EM_RESULT_AUDIO_TRACK_INFO);
595    SEND_CMD_PARAM(app, current);
596    SEND_CMD_PARAM(app, track_count);
597    while (desc)
598      {
599         int tid = desc->i_id;
600         const char *name = desc->psz_name;
601         SEND_CMD_PARAM(app, tid);
602         _send_cmd_str(app, name);
603         desc = desc->p_next;
604      }
605    _send_cmd_finish(app);
606 }
607
608 static void
609 _position_changed(struct _App *app)
610 {
611    if (!app->opening)
612      return;
613
614    /* sending size info only once */
615    int r, w, h;
616    r = libvlc_video_get_size(app->mp, 0, &w, &h);
617    if (r < 0)
618      return;
619    _send_resize(app, w, h);
620
621    /* sending audio track info */
622    // _send_track_info(app);
623
624    libvlc_media_player_stop(app->mp);
625 }
626
627 static void
628 _process_thread_events(struct _App *app)
629 {
630    int event;
631    size_t size;
632
633    size = read(app->fd_read, &event, sizeof(event));
634    if (size != sizeof(event))
635      {
636         fprintf(stderr, "player: problem when reading thread event. size = %zd\n", size);
637         return;
638      }
639
640    switch (event) {
641       case EM_THREAD_POSITION_CHANGED:
642          _position_changed(app);
643          break;
644    }
645 }
646
647 int
648 main(int argc, const char *argv[])
649 {
650    struct _App app;
651    Emotion_Generic_Video_Shared *vs;
652    struct pollfd fds[3];
653    int tpipe[2]; // pipe for comunicating events from threads
654    char shmname[256];
655    char cwidth[64], cheight[64], cpitch[64], chroma[64];
656    char buf[64];
657    const char *vlc_argv[] =
658      {
659         "--quiet",
660         "--vout",
661         "vmem",
662         "--vmem-width",
663         cwidth,
664         "--vmem-height",
665         cheight,
666         "--vmem-pitch",
667         cpitch,
668         "--vmem-chroma",
669         chroma
670      };
671
672    if (argc < 3)
673      {
674         fprintf(stderr, "player: missing paramters.\n");
675         fprintf(stderr, "syntax:\n\t%s <fd read> <fd write>\n", argv[0]);
676         return -1;
677      }
678
679    app.em_read = atoi(argv[1]);
680    app.em_write = atoi(argv[2]);
681
682    fprintf(stderr, "reading commands from fd: %d, writing on fd: %d\n", app.em_read, app.em_write);
683
684    int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
685    snprintf(cwidth, sizeof(cwidth), "%d", DEFAULTWIDTH);
686    snprintf(cheight, sizeof(cheight), "%d", DEFAULTHEIGHT);
687    snprintf(cpitch, sizeof(cpitch), "%d", DEFAULTWIDTH * 4);
688    snprintf(chroma, sizeof(chroma), "RV32");
689
690    /*
691     * Naughty xattr in emotion uses ecore_thread to run its thing, this
692     * may leave emotion's reference count high and it won't kill us...
693     * letting us play the video in the background.  not good.
694     *
695     * prctl(PR_SET_PDEATHSIG) is a linux only thing. Need to find ways
696     * to do it on other platforms. Until then leave it breaking on
697     * such platforms so people port it instead of ignoring.
698     */
699    prctl(PR_SET_PDEATHSIG, SIGHUP);
700
701    app.libvlc = libvlc_new(vlc_argc, vlc_argv);
702    app.mp = NULL;
703    app.filename = NULL;
704    app.w = 0;
705    app.h = 0;
706    app.size_sent = 0;
707    app.opening = 0;
708    app.playing = 0;
709    app.closing = 0;
710
711    if (_em_cmd_read(&app) != EM_CMD_INIT)
712      {
713         fprintf(stderr, "player: wrong init command!\n");
714         return -1;
715      }
716
717    int size;
718    _em_read_safe(app.em_read, &size, sizeof(size));
719    _em_read_safe(app.em_read, buf, size);
720    app.shmname = strdup(buf);
721
722    _send_cmd(&app, EM_RESULT_INIT);
723
724    pipe(tpipe);
725    app.fd_read = tpipe[0];
726    app.fd_write = tpipe[1];
727    fds[0].fd = app.em_read;
728    fds[0].events = POLLIN;
729    fds[1].fd = app.fd_read;
730    fds[1].events = POLLIN;
731    fds[2].fd = STDERR_FILENO;
732    fds[2].events = 0;
733
734    while (1)
735      {
736         int r;
737
738         r = poll(fds, 3, -1);
739         if (r == 0)
740           continue;
741         else if (r < 0)
742           {
743              fprintf(stderr,
744                      "emotion_generic_vlc: an error ocurred on poll(): %s\n",
745                      strerror(errno));
746              break;
747           }
748
749         if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
750           {
751              fputs("emotion_generic_vlc: error communicating with stdin\n",
752                    stderr);
753              break;
754           }
755         if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
756           {
757              fputs("emotion_generic_vlc: error communicating with thread\n",
758                    stderr);
759              break;
760           }
761
762         if (fds[0].revents & POLLIN)
763           _process_emotion_commands(&app);
764         if (fds[1].revents & POLLIN)
765           _process_thread_events(&app);
766         if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))
767           break;
768      }
769
770    libvlc_release(app.libvlc);
771
772
773    return 0;
774 }
775 #undef SEND_CMD_PARAM