Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <assert.h>
- #include <gst/gst.h>
- #include <glib.h>
- #include <string.h>
- typedef struct _CustomData {
- GstElement *pipeline;
- GstElement *video_sink;
- GMainLoop *loop;
- gboolean playing; /* Playing or Paused */
- gdouble rate; /* Current playback rate (can be negative) */
- int count;
- int uri_count;
- char **uris;
- } CustomData;
- /* Send seek event to change rate */
- static void send_seek_event(CustomData *data) {
- gint64 position;
- GstEvent *seek_event;
- /* Obtain the current position, needed for the seek event */
- if (!gst_element_query_position(data->pipeline, GST_FORMAT_TIME, &position)) {
- g_printerr("Unable to retrieve current position.\n");
- return;
- }
- seek_event = gst_event_new_seek(data->rate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
- GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, -1);
- /* Send the event */
- gst_element_send_event(data->pipeline, seek_event);
- g_print("Current rate: %g\n", data->rate);
- }
- static void prepare_next_stream(GstElement *obj, gpointer userdata) {
- CustomData *data = (CustomData*) userdata;
- const char* next_uri = data->uris[(data->count + 1) % data->uri_count];
- g_print("about-to-finish %4d; setting next to %s\n",
- data->count, next_uri);
- g_object_set(G_OBJECT(data->pipeline), "uri", next_uri, NULL);
- data->count++;
- }
- /* Process keyboard input */
- static gboolean handle_keyboard(GIOChannel *source, GIOCondition cond, CustomData *data) {
- gchar *str = NULL;
- if (g_io_channel_read_line(source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
- return TRUE;
- }
- switch (g_ascii_tolower(str[0])) {
- case 'p':
- data->playing = !data->playing;
- gst_element_set_state(data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
- g_print("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
- break;
- case 's':
- if (g_ascii_isupper(str[0])) {
- data->rate *= 1.1;
- } else {
- data->rate /= 1.1;
- }
- send_seek_event(data);
- break;
- case 'd':
- data->rate *= -1.0;
- send_seek_event(data);
- break;
- case 'n':
- if (data->video_sink == NULL) {
- /* If we have not done so, obtain the sink through which we will send the step events */
- g_object_get(data->pipeline, "video-sink", &data->video_sink, NULL);
- }
- gst_element_send_event(data->video_sink,
- gst_event_new_step(GST_FORMAT_BUFFERS, 1, data->rate, TRUE, FALSE));
- g_print("Stepping one frame\n");
- break;
- case 'q':
- g_main_loop_quit(data->loop);
- break;
- default:
- break;
- }
- g_free(str);
- return TRUE;
- }
- int main(int argc, char *argv[]) {
- CustomData data;
- GIOChannel *io_stdin;
- /* Initialisation */
- gst_init(&argc, &argv);
- /* Initialize our data structure */
- memset(&data, 0, sizeof (data));
- data.loop = g_main_loop_new(NULL, FALSE);
- /* Check input arguments */
- if (argc < 2) {
- g_printerr("Usage: %s <list of space-delimited filenames or URLs>\n",
- argv[0]);
- return -1;
- }
- /* Print usage map */
- g_print(
- "USAGE: Choose one of the following options, then press enter:\n"
- " 'P' to toggle between PAUSE and PLAY\n"
- " 'S' to increase playback speed, 's' to decrease playback speed\n"
- " 'D' to toggle playback direction\n"
- " 'N' to move to next frame (in the current direction, better in PAUSE)\n"
- " 'Q' to quit\n");
- data.pipeline = gst_element_factory_make("playbin", "play");
- assert(data.pipeline != NULL);
- // Add watch for keyboard input
- io_stdin = g_io_channel_unix_new(fileno(stdin));
- g_io_add_watch(io_stdin, G_IO_IN, (GIOFunc) handle_keyboard, &data);
- /* Register about-to-finish callback to re-set the URI */
- data.count = 0;
- data.uris = &(argv[1]);
- data.uri_count = argc - 1;
- g_signal_connect(data.pipeline, "about-to-finish",
- G_CALLBACK(prepare_next_stream), &data);
- /* Set initial URI */
- g_object_set(G_OBJECT(data.pipeline), "uri", argv[1], NULL);
- g_print("Now playing: %s\n", argv[1]);
- gst_element_set_state(data.pipeline, GST_STATE_PLAYING);
- data.playing = TRUE;
- data.rate = 1.0;
- /* Iterate */
- g_print("Running...\n");
- g_main_loop_run(data.loop);
- /* since we loop endlessly, the follwing is never reached */
- /* Out of the main loop, clean up nicely */
- g_print("Returned, stopping playback\n");
- gst_element_set_state(data.pipeline, GST_STATE_NULL);
- g_print("Deleting pipeline\n");
- gst_object_unref(GST_OBJECT(data.pipeline));
- g_main_loop_unref(data.loop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement