Skip to content
Snippets Groups Projects
Commit e56d9673 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

doc: thumbnailer: Use new media thumbnailer API

parent d3be66ed
No related branches found
No related tags found
1 merge request!980doc: thumbnailer: Use new libvlc media thumbnailer API
Pipeline #164886 passed with stage
in 38 minutes and 31 seconds
......@@ -114,69 +114,58 @@ static bool done;
static void callback(const libvlc_event_t *ev, void *param)
{
float new_position;
(void)param;
pthread_mutex_lock(&lock);
switch (ev->type) {
case libvlc_MediaPlayerPositionChanged:
new_position = ev->u.media_player_position_changed.new_position;
if (new_position < VLC_THUMBNAIL_POSITION * .9 /* 90% margin */)
break;
case libvlc_MediaPlayerSnapshotTaken:
if (ev->type == libvlc_MediaThumbnailGenerated)
{
libvlc_picture_t** pic = param;
pthread_mutex_lock(&lock);
libvlc_picture_retain(ev->u.media_thumbnail_generated.p_thumbnail);
*pic = ev->u.media_thumbnail_generated.p_thumbnail;
done = true;
pthread_cond_signal(&wait);
break;
default:
assert(0);
pthread_mutex_unlock(&lock);
}
pthread_mutex_unlock(&lock);
}
static void event_wait(const char *error)
{
int ret;
struct timespec ts;
#define VLC_THUMBNAIL_TIMEOUT 5 /* 5 secs */
clock_gettime(CLOCK_MONOTONIC, &ts);
ts.tv_sec += VLC_THUMBNAIL_TIMEOUT;
pthread_mutex_lock(&lock);
ret = done ? 0 : pthread_cond_timedwait(&wait, &lock, &ts);
pthread_mutex_unlock(&lock);
assert(!ret || ret == ETIMEDOUT);
if (ret) {
fprintf(stderr,
"%s (timeout after %d secs!\n", error, VLC_THUMBNAIL_TIMEOUT);
exit(1);
}
}
static void set_position(libvlc_media_player_t *mp)
static void snapshot(libvlc_media_t *m, int width, char *out_with_ext)
{
libvlc_event_manager_t *em = libvlc_media_player_event_manager(mp);
libvlc_event_manager_t *em = libvlc_media_event_manager(m);
assert(em);
libvlc_event_attach(em, libvlc_MediaPlayerPositionChanged, callback, NULL);
libvlc_picture_t* pic = NULL;
libvlc_event_attach(em, libvlc_MediaThumbnailGenerated, callback, &pic);
done = false;
libvlc_media_player_set_position(mp, VLC_THUMBNAIL_POSITION);
event_wait("Couldn't set position");
libvlc_event_detach(em, libvlc_MediaPlayerPositionChanged, callback, NULL);
}
static void snapshot(libvlc_media_player_t *mp, int width, char *out_with_ext)
{
libvlc_event_manager_t *em = libvlc_media_player_event_manager(mp);
assert(em);
libvlc_media_thumbnail_request_t* req =
libvlc_media_thumbnail_request_by_pos(m, VLC_THUMBNAIL_POSITION,
libvlc_media_thumbnail_seek_fast,
width, 0, false, libvlc_picture_Png,
VLC_THUMBNAIL_TIMEOUT * 1000);
if (!req)
{
fprintf(stderr, "Failed to request thumbnail\n");
exit(1);
}
pthread_mutex_lock(&lock);
while (!done)
pthread_cond_wait(&wait, &lock);
pthread_mutex_unlock(&lock);
libvlc_media_thumbnail_request_destroy(req);
libvlc_event_detach(em, libvlc_MediaThumbnailGenerated, callback, &pic);
libvlc_event_attach(em, libvlc_MediaPlayerSnapshotTaken, callback, NULL);
done = false;
libvlc_video_take_snapshot(mp, 0, out_with_ext, width, 0);
event_wait("Snapshot has not been written");
libvlc_event_detach(em, libvlc_MediaPlayerSnapshotTaken, callback, NULL);
if (!pic)
{
fprintf(stderr, "Snapshot has not been written (timeout after %d secs!\n",
VLC_THUMBNAIL_TIMEOUT);
exit(1);
}
int res = libvlc_picture_save(pic, out_with_ext);
libvlc_picture_release(pic);
if (res)
{
fprintf(stderr, "Failed to save the thumbnail\n");
exit(res);
}
}
int main(int argc, const char **argv)
......@@ -184,9 +173,7 @@ int main(int argc, const char **argv)
const char *in;
char *out, *out_with_ext;
int width;
pthread_condattr_t attr;
libvlc_instance_t *libvlc;
libvlc_media_player_t *mp;
libvlc_media_t *m;
/* mandatory to support UTF-8 filenames (provided the locale is well set)*/
......@@ -194,10 +181,7 @@ int main(int argc, const char **argv)
cmdline(argc, argv, &in, &out, &out_with_ext, &width);
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&wait, &attr);
pthread_condattr_destroy(&attr);
pthread_cond_init(&wait, NULL);
/* starts vlc */
libvlc = create_libvlc();
......@@ -206,16 +190,8 @@ int main(int argc, const char **argv)
m = libvlc_media_new_path(libvlc, in);
assert(m);
mp = libvlc_media_player_new_from_media(m);
assert(mp);
libvlc_media_player_play(mp);
/* takes snapshot */
set_position(mp);
snapshot(mp, width, out_with_ext);
libvlc_media_player_stop_async(mp);
snapshot(m, width, out_with_ext);
/* clean up */
if (out != out_with_ext) {
......@@ -224,7 +200,6 @@ int main(int argc, const char **argv)
}
free(out);
libvlc_media_player_release(mp);
libvlc_media_release(m);
libvlc_release(libvlc);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment