Skip to content

Commit

Permalink
tools: play: implement seeking via console in interactive mode
Browse files Browse the repository at this point in the history
Arrow left and right to seek back of forward.
  • Loading branch information
tp-m committed Nov 24, 2013
1 parent aab6875 commit 68afb29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
8 changes: 4 additions & 4 deletions tools/gst-play-kb.h
Expand Up @@ -23,10 +23,10 @@

#include <glib.h>

#define GST_PLAY_KB_KEY_UP "\033[A"
#define GST_PLAY_KB_KEY_DOWN "\033[B"
#define GST_PLAY_KB_KEY_RIGHT "\033[C"
#define GST_PLAY_KB_KEY_LEFT "\033[D"
#define GST_PLAY_KB_ARROW_UP "\033[A"
#define GST_PLAY_KB_ARROW_DOWN "\033[B"
#define GST_PLAY_KB_ARROW_RIGHT "\033[C"
#define GST_PLAY_KB_ARROW_LEFT "\033[D"

typedef void (*GstPlayKbFunc) (const gchar * kb_input, gpointer user_data);

Expand Down
58 changes: 55 additions & 3 deletions tools/gst-play.c
Expand Up @@ -495,6 +495,52 @@ toggle_paused (GstPlay * play)
}
}

static void
relative_seek (GstPlay * play, gdouble percent)
{
GstQuery *query;
gboolean seekable = FALSE;
gint64 dur = -1, pos = -1;

g_return_if_fail (percent >= -1.0 && percent <= 1.0);

if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
goto seek_failed;

query = gst_query_new_seeking (GST_FORMAT_TIME);
if (!gst_element_query (play->playbin, query)) {
gst_query_unref (query);
goto seek_failed;
}

gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
gst_query_unref (query);

if (!seekable || dur <= 0)
goto seek_failed;

pos = pos + dur * percent;
if (pos > dur) {
if (!play_next (play)) {
g_print ("\nReached end of play list.\n");
g_main_loop_quit (play->loop);
}
} else {
if (pos < 0)
pos = 0;
if (!gst_element_seek_simple (play->playbin, GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, pos))
goto seek_failed;
}

return;

seek_failed:
{
g_print ("\nCould not seek.\n");
}
}

static void
keyboard_cb (const gchar * key_input, gpointer user_data)
{
Expand All @@ -515,9 +561,15 @@ keyboard_cb (const gchar * key_input, gpointer user_data)
break;
case 27: /* ESC */
default:
GST_INFO ("keyboard input:");
for (; *key_input != '\0'; ++key_input)
GST_INFO (" code %3d", *key_input);
if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
relative_seek (play, +0.08);
} else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
relative_seek (play, -0.01);
} else {
GST_INFO ("keyboard input:");
for (; *key_input != '\0'; ++key_input)
GST_INFO (" code %3d", *key_input);
}
break;
}
}
Expand Down

0 comments on commit 68afb29

Please sign in to comment.