17 #include <sys/prctl.h>
21 #include <Emotion_Generic_Plugin.h>
24 EM_THREAD_POSITION_CHANGED,
29 Emotion_Generic_Video_Shared *vs;
30 Emotion_Generic_Video_Frame vf;
31 libvlc_instance_t *libvlc;
33 libvlc_media_player_t *mp;
34 libvlc_event_manager_t *event_mgr;
35 libvlc_event_manager_t *mevent_mgr;
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
49 static pthread_mutex_t _mutex_fd = PTHREAD_MUTEX_INITIALIZER;
52 _em_read_safe(int fd, void *buf, ssize_t size)
64 r = read(fd, p, todo);
74 if (errno == EINTR || errno == EAGAIN)
78 fprintf(stderr, "could not read from fd %d: %s",
89 _em_write_safe(int fd, const void *buf, ssize_t size)
101 r = write(fd, p, todo);
111 if (errno == EINTR || errno == EAGAIN)
115 fprintf(stderr, "could not write to fd %d: %s",
116 fd, strerror(errno));
126 _em_str_read(int fd, char **str)
132 r = _em_read_safe(fd, &size, sizeof(size));
145 r = _em_read_safe(fd, buf, size);
157 _em_cmd_read(struct _App *app)
160 _em_read_safe(app->em_read, &cmd, sizeof(cmd));
166 _send_cmd_start(struct _App *app, int cmd)
168 pthread_mutex_lock(&_mutex_fd);
169 _em_write_safe(app->em_write, &cmd, sizeof(cmd));
173 _send_cmd_finish(struct _App *app)
175 pthread_mutex_unlock(&_mutex_fd);
179 _send_cmd(struct _App *app, int cmd)
181 _send_cmd_start(app, cmd);
182 _send_cmd_finish(app);
186 _send_cmd_str(struct _App *app, const char *str)
190 len = strlen(str) + 1;
193 _em_write_safe(app->em_write, &len, sizeof(len));
194 _em_write_safe(app->em_write, str, len);
197 #define SEND_CMD_PARAM(app, i) \
198 _em_write_safe((app)->em_write, &(i), sizeof((i)));
201 _send_resize(struct _App *app, int width, int height)
203 _send_cmd_start(app, EM_RESULT_FRAME_SIZE);
204 SEND_CMD_PARAM(app, width);
205 SEND_CMD_PARAM(app, height);
206 _send_cmd_finish(app);
210 _send_length_changed(struct _App *app, const struct libvlc_event_t *ev)
212 float length = ev->u.media_player_length_changed.new_length;
215 fprintf(stderr, "length changed: %0.3f\n", length);
216 _send_cmd_start(app, EM_RESULT_LENGTH_CHANGED);
217 SEND_CMD_PARAM(app, length);
218 _send_cmd_finish(app);
222 _send_time_changed(struct _App *app, const struct libvlc_event_t *ev)
224 float new_time = ev->u.media_player_time_changed.new_time;
226 if (app->vs->frame_drop > 1)
228 _send_cmd_start(app, EM_RESULT_POSITION_CHANGED);
229 SEND_CMD_PARAM(app, new_time);
230 _send_cmd_finish(app);
234 _send_seekable_changed(struct _App *app, const struct libvlc_event_t *ev)
236 int seekable = ev->u.media_player_seekable_changed.new_seekable;
237 _send_cmd_start(app, EM_RESULT_SEEKABLE_CHANGED);
238 SEND_CMD_PARAM(app, seekable);
239 _send_cmd_finish(app);
243 _lock(void *data, void **pixels)
245 struct _App *app = data;
248 *pixels = app->vf.frames[app->vs->frame.player];
252 return NULL; // picture identifier, not needed here
256 _unlock(void *data, void *id, void *const *pixels)
261 _display(void *data, void *id)
263 struct _App *app = data;
267 sem_wait(&app->vs->lock);
268 app->vs->frame.last = app->vs->frame.player;
269 app->vs->frame.player = app->vs->frame.next;
270 app->vs->frame.next = app->vs->frame.last;
271 if (!app->vs->frame_drop++)
272 _send_cmd(app, EM_RESULT_FRAME_NEW);
273 sem_post(&app->vs->lock);
277 _tmp_lock(void *data, void **pixels)
284 _tmp_unlock(void *data, void *id, void *const *pixels)
289 _tmp_display(void *data, void *id)
294 _play(struct _App *app)
301 _em_read_safe(app->em_read, &pos, sizeof(pos));
305 libvlc_media_player_set_pause(app->mp, 0);
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);
317 _stop(struct _App *app)
320 libvlc_media_player_set_pause(app->mp, 1);
324 _send_file_closed(struct _App *app)
327 emotion_generic_shm_free(app->vs);
328 _send_cmd(app, EM_RESULT_FILE_CLOSE);
332 _send_file_set(struct _App *app)
335 _send_cmd(app, EM_RESULT_FILE_SET);
338 _send_file_closed(app);
342 _event_cb(const struct libvlc_event_t *ev, void *data)
344 struct _App *app = data;
348 case libvlc_MediaPlayerTimeChanged:
349 _send_time_changed(app, ev);
351 case libvlc_MediaPlayerPositionChanged:
352 thread_event = EM_THREAD_POSITION_CHANGED;
353 write(app->fd_write, &thread_event, sizeof(thread_event));
355 case libvlc_MediaPlayerLengthChanged:
356 _send_length_changed(app, ev);
358 case libvlc_MediaPlayerSeekableChanged:
359 _send_seekable_changed(app, ev);
361 case libvlc_MediaPlayerPlaying:
362 _send_resize(app, app->w, app->h);
364 case libvlc_MediaPlayerStopped:
367 case libvlc_MediaPlayerEndReached:
368 _send_cmd(app, EM_RESULT_PLAYBACK_STOPPED);
374 _file_set(struct _App *app)
378 libvlc_media_release(app->m);
379 libvlc_media_player_release(app->mp);
383 _em_str_read(app->em_read, &app->filename);
385 app->m = libvlc_media_new_path(app->libvlc, app->filename);
388 fprintf(stderr, "could not open path: \"%s\"\n", app->filename);
391 app->mp = libvlc_media_player_new_from_media(app->m);
395 fprintf(stderr, "could not create new player from media.\n");
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,
405 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerStopped,
408 app->mevent_mgr = libvlc_media_event_manager(app->m);
410 libvlc_audio_set_mute(app->mp, 1);
411 libvlc_media_player_play(app->mp);
415 _position_set(struct _App *app)
421 _em_read_safe(app->em_read, &position, sizeof(position));
423 libvlc_time_t new_time = position * 1000;
424 libvlc_media_player_set_time(app->mp, new_time);
428 _speed_set(struct _App *app)
435 _em_read_safe(app->em_read, &rate, sizeof(rate));
437 libvlc_media_player_set_rate(app->mp, rate);
441 _mute_set(struct _App *app)
448 _em_read_safe(app->em_read, &mute, sizeof(mute));
450 libvlc_audio_set_mute(app->mp, mute);
454 _volume_set(struct _App *app)
462 _em_read_safe(app->em_read, &volume, sizeof(volume));
465 libvlc_audio_set_volume(app->mp, vol);
469 _audio_track_set(struct _App *app)
473 _em_read_safe(app->em_read, &track, sizeof(track));
475 libvlc_audio_set_track(app->mp, track);
479 _video_track_set(struct _App *app)
483 _em_read_safe(app->em_read, &track, sizeof(track));
485 libvlc_video_set_track(app->mp, track);
489 _spu_track_set(struct _App *app)
493 _em_read_safe(app->em_read, &track, sizeof(track));
495 libvlc_video_set_spu(app->mp, track);
499 _file_set_done(struct _App *app)
505 r = emotion_generic_shm_get(app->shmname, &app->vs, &app->vf);
509 libvlc_media_release(app->m);
510 libvlc_media_player_release(app->mp);
511 app->filename = NULL;
514 _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
515 SEND_CMD_PARAM(app, r);
516 _send_cmd_finish(app);
518 app->w = app->vs->width;
519 app->h = app->vs->height;
520 libvlc_video_set_format(app->mp, "RV32", app->w, app->h, app->w * 4);
521 libvlc_video_set_callbacks(app->mp, _lock, _unlock, _display, app);
524 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerPlaying,
526 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerTimeChanged,
528 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerLengthChanged,
530 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerSeekableChanged,
533 libvlc_audio_set_mute(app->mp, 0);
535 _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
536 SEND_CMD_PARAM(app, r);
537 _send_cmd_finish(app);
541 _file_close(struct _App *app)
545 goto release_resources;
547 if (libvlc_media_player_get_state(app->mp) != libvlc_Playing)
549 _send_file_closed(app);
556 libvlc_media_player_stop(app->mp);
561 libvlc_media_release(app->m);
562 libvlc_media_player_release(app->mp);
567 _process_emotion_commands(struct _App *app)
569 int cmd = _em_cmd_read(app);
571 case EM_CMD_FILE_SET:
574 case EM_CMD_FILE_SET_DONE:
577 case EM_CMD_FILE_CLOSE:
586 case EM_CMD_POSITION_SET:
589 case EM_CMD_SPEED_SET:
592 case EM_CMD_AUDIO_MUTE_SET:
595 case EM_CMD_VOLUME_SET:
598 case EM_CMD_AUDIO_TRACK_SET:
599 _audio_track_set(app);
601 case EM_CMD_VIDEO_TRACK_SET:
602 _video_track_set(app);
608 _send_track_info(struct _App *app, int cmd, int current, int count, libvlc_track_description_t *desc)
610 _send_cmd_start(app, cmd);
611 SEND_CMD_PARAM(app, current);
612 SEND_CMD_PARAM(app, count);
615 int tid = desc->i_id;
616 const char *name = desc->psz_name;
617 SEND_CMD_PARAM(app, tid);
618 _send_cmd_str(app, name);
621 _send_cmd_finish(app);
625 _send_all_track_info(struct _App *app)
627 int track_count, current;
628 libvlc_track_description_t *desc;
630 current = libvlc_audio_get_track(app->mp);
631 track_count = libvlc_audio_get_track_count(app->mp);
632 desc = libvlc_audio_get_track_description(app->mp);
634 _send_track_info(app, EM_RESULT_AUDIO_TRACK_INFO,
635 current, track_count, desc);
637 current = libvlc_video_get_track(app->mp);
638 track_count = libvlc_video_get_track_count(app->mp);
639 desc = libvlc_video_get_track_description(app->mp);
641 _send_track_info(app, EM_RESULT_VIDEO_TRACK_INFO,
642 current, track_count, desc);
644 current = libvlc_video_get_spu(app->mp);
645 track_count = libvlc_video_get_spu_count(app->mp);
646 desc = libvlc_video_get_spu_description(app->mp);
648 _send_track_info(app, EM_RESULT_SPU_TRACK_INFO,
649 current, track_count, desc);
653 _send_all_meta_info(struct _App *app)
657 _send_cmd_start(app, EM_RESULT_META_INFO);
660 * Will send in this order: title, artist, album, year,
661 * genre, comments, disc id and track count.
663 meta = libvlc_media_get_meta(app->m, libvlc_meta_Title);
664 _send_cmd_str(app, meta);
665 meta = libvlc_media_get_meta(app->m, libvlc_meta_Artist);
666 _send_cmd_str(app, meta);
667 meta = libvlc_media_get_meta(app->m, libvlc_meta_Album);
668 _send_cmd_str(app, meta);
669 meta = libvlc_media_get_meta(app->m, libvlc_meta_Date);
670 _send_cmd_str(app, meta);
671 meta = libvlc_media_get_meta(app->m, libvlc_meta_Genre);
672 _send_cmd_str(app, meta);
673 meta = NULL; // sending empty comments
674 _send_cmd_str(app, meta);
675 meta = NULL; // sending empty disc id
676 _send_cmd_str(app, meta);
677 meta = libvlc_media_get_meta(app->m, libvlc_meta_TrackNumber);
678 _send_cmd_str(app, meta);
679 _send_cmd_finish(app);
683 _position_changed(struct _App *app)
688 /* sending size info only once */
690 r = libvlc_video_get_size(app->mp, 0, &w, &h);
693 _send_resize(app, w, h);
695 /* sending audio track info */
696 // _send_all_track_info(app);
698 /* sending meta info */
699 // _send_all_meta_info(app);
701 libvlc_media_player_stop(app->mp);
705 _process_thread_events(struct _App *app)
710 size = read(app->fd_read, &event, sizeof(event));
711 if (size != sizeof(event))
713 fprintf(stderr, "player: problem when reading thread event. size = %zd\n", size);
718 case EM_THREAD_POSITION_CHANGED:
719 _position_changed(app);
725 main(int argc, const char *argv[])
728 Emotion_Generic_Video_Shared *vs;
729 struct pollfd fds[3];
730 int tpipe[2]; // pipe for comunicating events from threads
732 char cwidth[64], cheight[64], cpitch[64], chroma[64];
734 const char *vlc_argv[] =
751 fprintf(stderr, "player: missing paramters.\n");
752 fprintf(stderr, "syntax:\n\t%s <fd read> <fd write>\n", argv[0]);
756 app.em_read = atoi(argv[1]);
757 app.em_write = atoi(argv[2]);
759 fprintf(stderr, "reading commands from fd: %d, writing on fd: %d\n", app.em_read, app.em_write);
761 int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
762 snprintf(cwidth, sizeof(cwidth), "%d", DEFAULTWIDTH);
763 snprintf(cheight, sizeof(cheight), "%d", DEFAULTHEIGHT);
764 snprintf(cpitch, sizeof(cpitch), "%d", DEFAULTWIDTH * 4);
765 snprintf(chroma, sizeof(chroma), "RV32");
768 * Naughty xattr in emotion uses ecore_thread to run its thing, this
769 * may leave emotion's reference count high and it won't kill us...
770 * letting us play the video in the background. not good.
772 * prctl(PR_SET_PDEATHSIG) is a linux only thing. Need to find ways
773 * to do it on other platforms. Until then leave it breaking on
774 * such platforms so people port it instead of ignoring.
776 prctl(PR_SET_PDEATHSIG, SIGHUP);
778 app.libvlc = libvlc_new(vlc_argc, vlc_argv);
788 if (_em_cmd_read(&app) != EM_CMD_INIT)
790 fprintf(stderr, "player: wrong init command!\n");
795 _em_read_safe(app.em_read, &size, sizeof(size));
796 _em_read_safe(app.em_read, buf, size);
797 app.shmname = strdup(buf);
799 _send_cmd(&app, EM_RESULT_INIT);
802 app.fd_read = tpipe[0];
803 app.fd_write = tpipe[1];
804 fds[0].fd = app.em_read;
805 fds[0].events = POLLIN;
806 fds[1].fd = app.fd_read;
807 fds[1].events = POLLIN;
808 fds[2].fd = STDERR_FILENO;
815 r = poll(fds, 3, -1);
821 "emotion_generic_vlc: an error ocurred on poll(): %s\n",
826 if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
828 fputs("emotion_generic_vlc: error communicating with stdin\n",
832 if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
834 fputs("emotion_generic_vlc: error communicating with thread\n",
839 if (fds[0].revents & POLLIN)
840 _process_emotion_commands(&app);
841 if (fds[1].revents & POLLIN)
842 _process_thread_events(&app);
843 if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))
847 libvlc_release(app.libvlc);
852 #undef SEND_CMD_PARAM