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;
40 int fd_read; // read commands from theads here
41 int fd_write; // write commands from threads here
42 int em_read; // read commands from emotion here
43 int em_write; // write commands to emotion here
50 static pthread_mutex_t _mutex_fd = PTHREAD_MUTEX_INITIALIZER;
53 _em_read_safe(int fd, void *buf, ssize_t size)
65 r = read(fd, p, todo);
75 if (errno == EINTR || errno == EAGAIN)
79 fprintf(stderr, "could not read from fd %d: %s",
90 _em_write_safe(int fd, const void *buf, ssize_t size)
102 r = write(fd, p, todo);
112 if (errno == EINTR || errno == EAGAIN)
116 fprintf(stderr, "could not write to fd %d: %s",
117 fd, strerror(errno));
127 _em_str_read(int fd, char **str)
133 r = _em_read_safe(fd, &size, sizeof(size));
146 r = _em_read_safe(fd, buf, size);
158 _em_cmd_read(struct _App *app)
161 _em_read_safe(app->em_read, &cmd, sizeof(cmd));
167 _send_cmd_start(struct _App *app, int cmd)
169 pthread_mutex_lock(&_mutex_fd);
170 _em_write_safe(app->em_write, &cmd, sizeof(cmd));
174 _send_cmd_finish(struct _App *app)
176 pthread_mutex_unlock(&_mutex_fd);
180 _send_cmd(struct _App *app, int cmd)
182 _send_cmd_start(app, cmd);
183 _send_cmd_finish(app);
187 _send_cmd_str(struct _App *app, const char *str)
191 len = strlen(str) + 1;
194 _em_write_safe(app->em_write, &len, sizeof(len));
195 _em_write_safe(app->em_write, str, len);
198 #define SEND_CMD_PARAM(app, i) \
199 _em_write_safe((app)->em_write, &(i), sizeof((i)));
202 _send_resize(struct _App *app, int width, int height)
204 _send_cmd_start(app, EM_RESULT_FRAME_SIZE);
205 SEND_CMD_PARAM(app, width);
206 SEND_CMD_PARAM(app, height);
207 _send_cmd_finish(app);
211 _send_length_changed(struct _App *app, const struct libvlc_event_t *ev)
213 float length = ev->u.media_player_length_changed.new_length;
216 fprintf(stderr, "length changed: %0.3f\n", length);
217 _send_cmd_start(app, EM_RESULT_LENGTH_CHANGED);
218 SEND_CMD_PARAM(app, length);
219 _send_cmd_finish(app);
223 _send_time_changed(struct _App *app, const struct libvlc_event_t *ev)
225 float new_time = ev->u.media_player_time_changed.new_time;
227 if (app->vs->frame_drop > 1)
229 _send_cmd_start(app, EM_RESULT_POSITION_CHANGED);
230 SEND_CMD_PARAM(app, new_time);
231 _send_cmd_finish(app);
235 _send_seekable_changed(struct _App *app, const struct libvlc_event_t *ev)
237 int seekable = ev->u.media_player_seekable_changed.new_seekable;
238 _send_cmd_start(app, EM_RESULT_SEEKABLE_CHANGED);
239 SEND_CMD_PARAM(app, seekable);
240 _send_cmd_finish(app);
244 _lock(void *data, void **pixels)
246 struct _App *app = data;
249 *pixels = app->vf.frames[app->vs->frame.player];
253 return NULL; // picture identifier, not needed here
257 _unlock(void *data, void *id, void *const *pixels)
262 _display(void *data, void *id)
264 struct _App *app = data;
268 sem_wait(&app->vs->lock);
269 app->vs->frame.last = app->vs->frame.player;
270 app->vs->frame.player = app->vs->frame.next;
271 app->vs->frame.next = app->vs->frame.last;
272 if (!app->vs->frame_drop++)
273 _send_cmd(app, EM_RESULT_FRAME_NEW);
274 sem_post(&app->vs->lock);
278 _tmp_lock(void *data, void **pixels)
280 struct _App *app = data;
281 *pixels = app->tmpbuffer;
286 _tmp_unlock(void *data, void *id, void *const *pixels)
291 _tmp_display(void *data, void *id)
296 _play(struct _App *app)
303 _em_read_safe(app->em_read, &pos, sizeof(pos));
307 libvlc_media_player_set_pause(app->mp, 0);
311 libvlc_time_t new_time = pos * 1000;
312 libvlc_media_player_play(app->mp);
313 libvlc_media_player_set_time(app->mp, new_time);
319 _stop(struct _App *app)
322 libvlc_media_player_set_pause(app->mp, 1);
326 _send_file_closed(struct _App *app)
329 emotion_generic_shm_free(app->vs);
330 _send_cmd(app, EM_RESULT_FILE_CLOSE);
334 _send_file_set(struct _App *app)
337 _send_cmd(app, EM_RESULT_FILE_SET);
340 _send_file_closed(app);
344 _event_cb(const struct libvlc_event_t *ev, void *data)
346 struct _App *app = data;
350 case libvlc_MediaPlayerTimeChanged:
351 _send_time_changed(app, ev);
353 case libvlc_MediaPlayerPositionChanged:
354 thread_event = EM_THREAD_POSITION_CHANGED;
355 write(app->fd_write, &thread_event, sizeof(thread_event));
357 case libvlc_MediaPlayerLengthChanged:
358 _send_length_changed(app, ev);
360 case libvlc_MediaPlayerSeekableChanged:
361 _send_seekable_changed(app, ev);
363 case libvlc_MediaPlayerPlaying:
364 _send_resize(app, app->w, app->h);
366 case libvlc_MediaPlayerStopped:
369 case libvlc_MediaPlayerEndReached:
370 _send_cmd(app, EM_RESULT_PLAYBACK_STOPPED);
376 _file_set(struct _App *app)
380 libvlc_media_release(app->m);
381 libvlc_media_player_release(app->mp);
385 _em_str_read(app->em_read, &app->filename);
387 app->m = libvlc_media_new_path(app->libvlc, app->filename);
390 fprintf(stderr, "could not open path: \"%s\"\n", app->filename);
393 app->mp = libvlc_media_player_new_from_media(app->m);
397 fprintf(stderr, "could not create new player from media.\n");
402 libvlc_video_set_format(app->mp, "RV32", DEFAULTWIDTH, DEFAULTHEIGHT, DEFAULTWIDTH * 4);
403 libvlc_video_set_callbacks(app->mp, _tmp_lock, _tmp_unlock, _tmp_display, app);
404 app->event_mgr = libvlc_media_player_event_manager(app->mp);
405 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerPositionChanged,
407 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerStopped,
410 app->mevent_mgr = libvlc_media_event_manager(app->m);
412 app->tmpbuffer = malloc(sizeof(char) * DEFAULTWIDTH * DEFAULTHEIGHT * 4);
413 libvlc_audio_set_mute(app->mp, 1);
414 libvlc_media_player_play(app->mp);
418 _position_set(struct _App *app)
424 _em_read_safe(app->em_read, &position, sizeof(position));
426 libvlc_time_t new_time = position * 1000;
427 libvlc_media_player_set_time(app->mp, new_time);
431 _speed_set(struct _App *app)
438 _em_read_safe(app->em_read, &rate, sizeof(rate));
440 libvlc_media_player_set_rate(app->mp, rate);
444 _mute_set(struct _App *app)
451 _em_read_safe(app->em_read, &mute, sizeof(mute));
453 libvlc_audio_set_mute(app->mp, mute);
457 _volume_set(struct _App *app)
465 _em_read_safe(app->em_read, &volume, sizeof(volume));
468 libvlc_audio_set_volume(app->mp, vol);
472 _audio_track_set(struct _App *app)
476 _em_read_safe(app->em_read, &track, sizeof(track));
478 libvlc_audio_set_track(app->mp, track);
482 _video_track_set(struct _App *app)
486 _em_read_safe(app->em_read, &track, sizeof(track));
488 libvlc_video_set_track(app->mp, track);
492 _file_set_done(struct _App *app)
498 r = emotion_generic_shm_get(app->shmname, &app->vs, &app->vf);
502 libvlc_media_release(app->m);
503 libvlc_media_player_release(app->mp);
504 app->filename = NULL;
507 _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
508 SEND_CMD_PARAM(app, r);
509 _send_cmd_finish(app);
511 app->w = app->vs->width;
512 app->h = app->vs->height;
513 libvlc_video_set_format(app->mp, "RV32", app->w, app->h, app->w * 4);
514 libvlc_video_set_callbacks(app->mp, _lock, _unlock, _display, app);
517 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerPlaying,
519 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerTimeChanged,
521 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerLengthChanged,
523 libvlc_event_attach(app->event_mgr, libvlc_MediaPlayerSeekableChanged,
526 libvlc_audio_set_mute(app->mp, 0);
528 _send_cmd_start(app, EM_RESULT_FILE_SET_DONE);
529 SEND_CMD_PARAM(app, r);
530 _send_cmd_finish(app);
534 _file_close(struct _App *app)
538 goto release_resources;
540 if (libvlc_media_player_get_state(app->mp) != libvlc_Playing)
542 _send_file_closed(app);
549 libvlc_media_player_stop(app->mp);
554 libvlc_media_release(app->m);
555 libvlc_media_player_release(app->mp);
556 free(app->tmpbuffer);
561 _process_emotion_commands(struct _App *app)
563 int cmd = _em_cmd_read(app);
565 case EM_CMD_FILE_SET:
568 case EM_CMD_FILE_SET_DONE:
571 case EM_CMD_FILE_CLOSE:
580 case EM_CMD_POSITION_SET:
583 case EM_CMD_SPEED_SET:
586 case EM_CMD_AUDIO_MUTE_SET:
589 case EM_CMD_VOLUME_SET:
592 case EM_CMD_AUDIO_TRACK_SET:
593 _audio_track_set(app);
595 case EM_CMD_VIDEO_TRACK_SET:
596 _video_track_set(app);
602 _send_track_info(struct _App *app, int cmd, int current, int count, libvlc_track_description_t *desc)
604 _send_cmd_start(app, cmd);
605 SEND_CMD_PARAM(app, current);
606 SEND_CMD_PARAM(app, count);
609 int tid = desc->i_id;
610 const char *name = desc->psz_name;
611 SEND_CMD_PARAM(app, tid);
612 _send_cmd_str(app, name);
615 _send_cmd_finish(app);
619 _send_all_track_info(struct _App *app)
621 int track_count, current;
622 libvlc_track_description_t *desc;
624 current = libvlc_audio_get_track(app->mp);
625 track_count = libvlc_audio_get_track_count(app->mp);
626 desc = libvlc_audio_get_track_description(app->mp);
628 _send_track_info(app, EM_RESULT_AUDIO_TRACK_INFO,
629 current, track_count, desc);
631 current = libvlc_video_get_track(app->mp);
632 track_count = libvlc_video_get_track_count(app->mp);
633 desc = libvlc_video_get_track_description(app->mp);
635 _send_track_info(app, EM_RESULT_VIDEO_TRACK_INFO,
636 current, track_count, desc);
638 current = libvlc_video_get_spu(app->mp);
639 track_count = libvlc_video_get_spu_count(app->mp);
640 desc = libvlc_video_get_spu_description(app->mp);
642 _send_track_info(app, EM_RESULT_SPU_TRACK_INFO,
643 current, track_count, desc);
647 _send_all_meta_info(struct _App *app)
651 _send_cmd_start(app, EM_RESULT_META_INFO);
654 * Will send in this order: title, artist, album, year,
655 * genre, comments, disc id and track count.
657 meta = libvlc_media_get_meta(app->m, libvlc_meta_Title);
658 _send_cmd_str(app, meta);
659 meta = libvlc_media_get_meta(app->m, libvlc_meta_Artist);
660 _send_cmd_str(app, meta);
661 meta = libvlc_media_get_meta(app->m, libvlc_meta_Album);
662 _send_cmd_str(app, meta);
663 meta = libvlc_media_get_meta(app->m, libvlc_meta_Date);
664 _send_cmd_str(app, meta);
665 meta = libvlc_media_get_meta(app->m, libvlc_meta_Genre);
666 _send_cmd_str(app, meta);
667 meta = NULL; // sending empty comments
668 _send_cmd_str(app, meta);
669 meta = NULL; // sending empty disc id
670 _send_cmd_str(app, meta);
671 meta = libvlc_media_get_meta(app->m, libvlc_meta_TrackNumber);
672 _send_cmd_str(app, meta);
673 _send_cmd_finish(app);
677 _position_changed(struct _App *app)
682 /* sending size info only once */
685 r = libvlc_video_get_size(app->mp, 0, &w, &h);
688 _send_resize(app, w, h);
690 /* sending audio track info */
691 _send_all_track_info(app);
693 /* sending meta info */
694 _send_all_meta_info(app);
696 libvlc_media_player_stop(app->mp);
700 _process_thread_events(struct _App *app)
705 size = read(app->fd_read, &event, sizeof(event));
706 if (size != sizeof(event))
708 fprintf(stderr, "player: problem when reading thread event. size = %zd\n", size);
713 case EM_THREAD_POSITION_CHANGED:
714 _position_changed(app);
720 main(int argc, const char *argv[])
723 struct pollfd fds[3];
724 int tpipe[2]; // pipe for comunicating events from threads
725 char cwidth[64], cheight[64], cpitch[64], chroma[64];
727 const char *vlc_argv[] =
744 fprintf(stderr, "player: missing paramters.\n");
745 fprintf(stderr, "syntax:\n\t%s <fd read> <fd write>\n", argv[0]);
749 app.em_read = atoi(argv[1]);
750 app.em_write = atoi(argv[2]);
752 fprintf(stderr, "reading commands from fd: %d, writing on fd: %d\n", app.em_read, app.em_write);
754 int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
755 snprintf(cwidth, sizeof(cwidth), "%d", DEFAULTWIDTH);
756 snprintf(cheight, sizeof(cheight), "%d", DEFAULTHEIGHT);
757 snprintf(cpitch, sizeof(cpitch), "%d", DEFAULTWIDTH * 4);
758 snprintf(chroma, sizeof(chroma), "RV32");
761 * Naughty xattr in emotion uses ecore_thread to run its thing, this
762 * may leave emotion's reference count high and it won't kill us...
763 * letting us play the video in the background. not good.
765 * prctl(PR_SET_PDEATHSIG) is a linux only thing. Need to find ways
766 * to do it on other platforms. Until then leave it breaking on
767 * such platforms so people port it instead of ignoring.
769 prctl(PR_SET_PDEATHSIG, SIGHUP);
771 app.libvlc = libvlc_new(vlc_argc, vlc_argv);
781 if (_em_cmd_read(&app) != EM_CMD_INIT)
783 fprintf(stderr, "player: wrong init command!\n");
788 _em_read_safe(app.em_read, &size, sizeof(size));
789 _em_read_safe(app.em_read, buf, size);
790 app.shmname = strdup(buf);
792 _send_cmd(&app, EM_RESULT_INIT);
795 app.fd_read = tpipe[0];
796 app.fd_write = tpipe[1];
797 fds[0].fd = app.em_read;
798 fds[0].events = POLLIN;
799 fds[1].fd = app.fd_read;
800 fds[1].events = POLLIN;
801 fds[2].fd = STDERR_FILENO;
808 r = poll(fds, 3, -1);
814 "emotion_generic_vlc: an error ocurred on poll(): %s\n",
819 if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
821 fputs("emotion_generic_vlc: error communicating with stdin\n",
825 if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
827 fputs("emotion_generic_vlc: error communicating with thread\n",
832 if (fds[0].revents & POLLIN)
833 _process_emotion_commands(&app);
834 if (fds[1].revents & POLLIN)
835 _process_thread_events(&app);
836 if (fds[2].revents & (POLLERR | POLLHUP | POLLNVAL))
840 libvlc_release(app.libvlc);
845 #undef SEND_CMD_PARAM