general-test.c 67 KB
Newer Older
Brendan Long's avatar
Brendan Long committed
1
/* Copyright (C) 2013 Cable Television Laboratories, Inc.
2
 *
Brendan Long's avatar
Brendan Long committed
3 4 5
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
6
 *
Brendan Long's avatar
Brendan Long committed
7 8 9 10 11
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
12
 *
Brendan Long's avatar
Brendan Long committed
13 14 15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 17 18 19 20 21 22 23
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CABLE TELEVISION LABS INC. OR ITS
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 */

#include <gst/gst.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// Global vars for cmd line args
//
static int g_wait_secs = 0;
static int g_state_change_timeout_secs = 45;
static gfloat g_requested_rate = 1;
static int g_rrid = 2;
static int g_rate_change_cnt = 1;
static char g_host[256];
static char g_uri[256];

static gboolean g_use_manual_bin_pipeline = FALSE;
static gboolean g_use_manual_elements_pipeline = FALSE;
static gboolean g_use_simple_pipeline = FALSE;
45 46
static gboolean g_use_two_pipelines = FALSE;
static gboolean g_use_auto_audio = FALSE;
47 48
static gboolean g_use_mpeg_audio = FALSE;
static gboolean g_use_ac3_audio = TRUE;
49
static gboolean g_download_buffering = FALSE;
50
static gboolean g_handle_buffering = FALSE;
51 52 53

static gboolean g_use_dtcp = FALSE;
static gboolean g_format_bytes = FALSE;
54
static gint g_seek_secs = 0;
55
static gint g_seek_percent = 0;
56
static gboolean g_seek_source = FALSE;
57 58 59

static gboolean g_zero_position = FALSE;
static gboolean g_positions = FALSE;
60
static gboolean g_states = FALSE;
61
static gboolean g_test_uri_switch = FALSE;
62 63
static gboolean g_playspeeds = FALSE;

64 65 66 67 68 69 70 71 72 73 74 75
static int g_eos_max_cnt = 1;
static int g_eos_cnt = 0;
//static const guint64 NANOS_PER_SECOND = 1000000000L;
static const guint64 NANOS_PER_MINUTE = 60000000000L;

static gboolean g_use_file = FALSE;
static char g_file_name[256];
static gchar* g_file_path = NULL;
static gchar* TEST_FILE_URL_PREFIX_ENV = "TEST_FILE_URL_PREFIX";
static gboolean g_create_dot = FALSE;

static GstElement* g_sink = NULL;
76
static GstElement* g_source = NULL;
77
static GstElement* g_pipeline = NULL;
78
static GstElement* g_identity = NULL;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static GstElement* g_tee = NULL;
static GstElement* g_queue = NULL;
static GstElement* g_mpeg2dec = NULL;
static GstElement* g_aparse = NULL;
static GstElement* g_avdec = NULL;


/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {
    GstElement *pipeline;  /* Our pipeline element */
    gboolean playing;      /* Are we in the PLAYING state? */
    gboolean terminate;    /* Should we terminate execution? */
    gboolean seek_enabled; /* Is seeking enabled for this media? */
    gboolean seek_done;    /* Have we performed the seek already? */
    gint64 duration;       /* How long does this media last, in nanoseconds */
    gdouble rate;		   /* current playspeed */
95
    GstElement *pipeline_2;  /* Optional second pipeline element */
96
    GMainLoop *loop;        /* Main loop for testing with keyboard input */
97
    gboolean buffering;     /* Indicates if buffering data */
98 99 100 101 102 103
} CustomData;

// Local method declarations
//
static gboolean process_cmd_line_args(int argc, char*argv[]);

104
static GstElement* create_playbin_pipeline(CustomData* data);
105 106 107 108 109 110 111 112
static GstElement* create_manual_pipeline(gchar* pipeline_name, gboolean use_bin);
static gboolean create_manual_bin_pipeline(GstElement* pipeline);
static gboolean create_manual_elements_pipeline(GstElement* pipeline);
static void bin_cb_pad_added (GstElement *dec, GstPad *pad, gpointer data);
static void tsdemux_cb_pad_added (GstElement *tsdemux, GstPad *pad, gpointer data);
static gboolean link_video_elements(gchar* name, GstPad* tsdemux_src_pad, GstCaps* caps);
static gboolean link_audio_elements(gchar* name, GstPad* tsdemux_src_pad, GstCaps* caps);
static GstElement* create_simple_pipeline();
113 114
static gboolean create_auto_audio_sink(GstElement* pipeline);
static gboolean enable_download_buffering(GstElement* pipeline);
115 116 117 118 119 120 121 122

static void on_source_changed(GstElement* element, GParamSpec* param, gpointer data);

static void perform_test(CustomData* data);
static gboolean perform_test_rate_change(CustomData* data);
static gboolean perform_test_position(CustomData* data);
static gboolean perform_test_query(CustomData* data, gint64* position, GstFormat format);
static gboolean perform_test_seek(CustomData* data, gint64 position, GstFormat format, gfloat rate);
123
static gboolean perform_test_states(CustomData* data);
124
static gboolean perform_test_playspeeds(CustomData* data);
125
static gboolean perform_test_seek_byte_percent(CustomData* data);
126

127
static gboolean set_pipeline_state(GstElement* pipeline, GstState desired_state, gint timeoutSecs);
128 129
static gboolean set_new_uri(CustomData* data);
static void handle_message (CustomData *data, GstMessage *msg);
130 131
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data);
static gboolean send_seek_event (CustomData *data);
132
static gboolean handle_buffering_message(CustomData *data, GstMessage *msg);
133 134 135 136 137 138

static void log_bin_elements(GstBin* bin);
static GstElement* log_element_links(GstElement* elem);


/**
139
 * Test program for testing gstreamer playback
140 141 142
 */
int main(int argc, char *argv[]) 
{
143

144 145 146 147 148 149
    CustomData data;
    data.playing = FALSE;
    data.terminate = FALSE;
    data.seek_enabled = FALSE;
    data.seek_done = FALSE;
    data.duration = GST_CLOCK_TIME_NONE;
150
    data.pipeline_2 = NULL;
151
    data.buffering = FALSE;
152 153 154 155 156 157 158 159 160 161 162 163 164 165

    // Assign default values
    strcpy(g_host, "192.168.2.2");
    g_uri[0] = '\0';
    g_file_name[0] = '\0';
    if (!process_cmd_line_args(argc, argv))
    {
        g_printerr("Exit due to problems with cmd line args\n");
        return -1;
    }

    // Build default URI if one was not specified
    if (g_uri[0] == '\0')
    {
smaynard's avatar
smaynard committed
166
        char* line2 = "dlna+http://";
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        char* line3 = ":8008/ocaphn/recording?rrid=";
        char* line4 = "&profile=MPEG_TS_SD_NA_ISO&mime=video/mpeg";
        if (g_use_dtcp)
        {
            line4 = "&profile=DTCP_MPEG_TS_SD_NA_ISO&mime=video/mpeg";
        }
        sprintf(g_uri, "%s%s%s%d%s", line2, g_host, line3, g_rrid, line4);
    }

    // Initialize GStreamer
    gst_init (&argc, &argv);

    // Build the pipeline
    if (g_use_manual_bin_pipeline)
    {
        g_print("Creating pipeline by manually linking uri decode bin\n");
        data.pipeline = create_manual_pipeline("manual-bin-pipeline", TRUE);
    }
    else if (g_use_manual_elements_pipeline)
    {
        g_print("Creating pipeline by manually linking decode elements\n");
        data.pipeline = create_manual_pipeline("manual-elements-pipeline", FALSE);
    }
    else if (g_use_simple_pipeline)
    {
        g_print("Creating simple pipeline\n");
        data.pipeline = create_simple_pipeline();
    }
195 196 197
    else if (g_use_two_pipelines)
    {
        g_print("Creating first of two pipelines\n");
198
        data.pipeline = create_playbin_pipeline(&data);
199
        g_print("Creating second of two pipelines\n");
200
        data.pipeline_2 = create_playbin_pipeline(&data);
201
    }
202 203 204
    else
    {
        g_print("Creating pipeline using playbin\n");
205
        data.pipeline = create_playbin_pipeline(&data);
206 207 208 209 210 211 212 213 214 215 216 217
    }

    // Check that pipeline was properly created
    if (data.pipeline == NULL)
    {
        g_printerr("Problems creating pipeline\n");
        return -1;
    }
    g_pipeline = data.pipeline;

    // Start playing
    g_print("Pipeline created, start playing\n");
218
    if (!set_pipeline_state(data.pipeline, GST_STATE_PLAYING, g_state_change_timeout_secs))
219 220 221 222 223 224 225 226
    {
        g_printerr ("Unable to set the pipeline to the playing state.\n");
        return -1;
    }
    else
    {
        g_print("Pipeline in playing state\n");

227 228 229
        // Print out elements in playbin
        g_print("Elements in pipeline 1\n");
        log_bin_elements(GST_BIN(data.pipeline));
230

231 232 233 234 235 236 237 238
        // Create pipeline diagram
        if (g_create_dot)
        {
            g_print("Creating pipeline.dot file\n");
            GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(data.pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
        }
    }
    if (data.pipeline_2)
239
    {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        if (g_wait_secs > 0)
        {
            g_print("Sleeping %d secs prior to setting pipeline 2 in playing state\n", g_wait_secs);
            g_usleep(g_wait_secs * 1000000L);
        }
        if (!set_pipeline_state(data.pipeline_2, GST_STATE_PLAYING, g_state_change_timeout_secs))
        {
            g_printerr ("Unable to set pipeline 2 to the playing state.\n");
            return -1;
        }
        else
        {
            g_print("Pipeline 2 in playing state\n");
            if (data.pipeline_2)
            {
                g_print("Elements in pipeline 2\n");
                log_bin_elements(GST_BIN(data.pipeline_2));
            }
        }
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    }

    // Perform requested testing
    g_print("Begin pipeline test\n");
    perform_test(&data);

    return 0;
}

/**
 *
 */
static void handle_message (CustomData *data, GstMessage *msg)
{
    GError *err;
    gchar *debug_info;
    GstState old_state, new_state, pending_state;
276

277
    //g_print("Got message type: %s\n", GST_MESSAGE_TYPE_NAME (msg));
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

    switch (GST_MESSAGE_TYPE (msg))
    {
    case GST_MESSAGE_ERROR:
        err = NULL;
        debug_info = NULL;
        gst_message_parse_error (msg, &err, &debug_info);
        g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
        g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
        g_clear_error (&err);
        g_free (debug_info);
        data->terminate = TRUE;
        break;

    case GST_MESSAGE_EOS:
        g_print ("End-Of-Stream reached.\n");
        g_eos_cnt++;
        if (g_eos_cnt >= g_eos_max_cnt)
        {
            data->terminate = TRUE;
        }
        break;

301
    case GST_MESSAGE_BUFFERING:
302 303 304 305 306 307 308
        if (g_handle_buffering)
        {
            if (!handle_buffering_message(data, msg)) {
                g_printerr ("Error handling buffering message");
                data->terminate = TRUE;
            }
        }
309 310
        break;

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    case GST_MESSAGE_DURATION:
        // The duration has changed, mark thdata.pipeline,e current one as invalid
        data->duration = GST_CLOCK_TIME_NONE;
        break;

    case GST_MESSAGE_STATE_CHANGED:
        gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
        if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline))
        {
            g_print ("Pipeline state changed from %s to %s:\n",
                    gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));

            // Remember whether we are in the PLAYING state or not
            data->playing = (new_state == GST_STATE_PLAYING);

        }
        break;

    default:
        // We should not reach here
        g_printerr ("Unexpected message received.\n");
        break;
    }
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
static gboolean handle_buffering_message(CustomData *data, GstMessage *msg)
{
    int buffering_percentage;
    const GstStructure *structure;

    structure = gst_message_get_structure(msg);
    gst_structure_get_int(structure, "buffer-percent", &buffering_percentage);

    g_print("[Buffering] Buffering: %d%%.\n", buffering_percentage);

    if ((!data->buffering) && (buffering_percentage < 10)) {
        g_print("[Buffering] Pausing playback to give buffering chance to catch\n");
        // Change state to paused to give buffering a chance
        if (!set_pipeline_state(data->pipeline, GST_STATE_PAUSED, g_state_change_timeout_secs))
        {
            g_printerr ("Unable to set the pipeline to the paused state.\n");
            return FALSE;
        }
        data->buffering = TRUE;

    } else if ((data->buffering) && (buffering_percentage > 90)) {
        // Resume playback
        g_print("[Buffering] Resuming playback since buffering caught up\n");
        if (!set_pipeline_state(data->pipeline, GST_STATE_PLAYING, g_state_change_timeout_secs))
        {
            g_printerr ("Unable to set the pipeline to the playing state.\n");
            return FALSE;
        }
        data->buffering = FALSE;
    }
    return TRUE;
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/**
 * Handle command line args
 */
static gboolean process_cmd_line_args(int argc, char *argv[])
{
    int i = 0;
    for (i = 1; i < argc; i++)
    {
        if (strstr(argv[i], "rate=") != NULL)
        {
            if (sscanf(argv[i], "rate=%f", &g_requested_rate) != 1)
            {
                g_printerr("Invalid rate arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested rate change to %4.1f\n", g_requested_rate);
            }
        }
        else if (strstr(argv[i], "wait=") != NULL)
        {
            if (sscanf(argv[i], "wait=%d", &g_wait_secs) != 1)
            {
                g_printerr("Invalid wait arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested wait secs to %d\n", g_wait_secs);
            }
        }
        else if (strstr(argv[i], "uri=") != NULL)
        {
            if (sscanf(argv[i], "uri=%s\n", &g_uri[0]) != 1)
            {
                g_printerr("Invalid uri arg specified: %s\n", argv[i]);
                return FALSE;

            }
            else
            {
                g_print("Set requested URI to %s\n", g_uri);
            }
        }
        else if (strstr(argv[i], "rrid=") != NULL)
        {
            if (sscanf(argv[i], "rrid=%d", &g_rrid) != 1)
            {
                g_printerr("Invalid rrid specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested rrid to %d\n", g_rrid);
            }
        }
        else if (strstr(argv[i], "host=") != NULL)
        {
            if (sscanf(argv[i], "host=%s\n", &g_host[0]) != 1)
            {
                g_printerr("Invalid host arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested host ip to %s\n", g_host);
            }
        }
        else if (strstr(argv[i], "manual_bin") != NULL)
        {
            g_use_manual_bin_pipeline = TRUE;
            g_print("Set to manually build pipeline using uridecode bin\n");
        }
        else if (strstr(argv[i], "manual_elements") != NULL)
        {
            g_use_manual_elements_pipeline = TRUE;
            g_print("Set to manually build pipeline using individual elements\n");
        }
        else if (strstr(argv[i], "simple") != NULL)
        {
            g_use_simple_pipeline = TRUE;
            g_print("Set to build simplest pipeline\n");
        }
453 454 455 456 457 458 459 460 461 462
        else if (strstr(argv[i], "two") != NULL)
        {
            g_use_two_pipelines = TRUE;
            g_print("Set to build two pipelines\n");
        }
        else if (strstr(argv[i], "auto_audio") != NULL)
        {
            g_use_auto_audio = TRUE;
            g_print("Set to use auto audio sink\n");
        }
463 464 465 466 467 468 469 470 471 472 473 474
        else if (strstr(argv[i], "mpeg_audio") != NULL)
        {
            g_use_mpeg_audio = TRUE;
            g_use_ac3_audio = FALSE;
            g_print("Set to use mpeg audio\n");
        }
        else if (strstr(argv[i], "ac3_audio") != NULL)
        {
            g_use_ac3_audio = TRUE;
            g_use_mpeg_audio = FALSE;
            g_print("Set to use ac3 audio\n");
        }
475 476 477 478 479
        else if (strstr(argv[i], "download_buffering") != NULL)
        {
            g_download_buffering = TRUE;
            g_print("Set to playbin to buffer download\n");
        }
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        else if (strstr(argv[i], "switch") != NULL)
        {
            g_test_uri_switch = TRUE;
            g_print("Set to test uri switching\n");
        }
        else if (strstr(argv[i], "zero") != NULL)
        {
            g_zero_position = TRUE;
            g_print("Set to position to zero\n");
        }
        else if (strstr(argv[i], "file=") != NULL)
        {
            if (sscanf(argv[i], "file=%s\n", &g_file_name[0]) != 1)
            {
                g_printerr("Invalid file name specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_use_file = TRUE;
                g_print("Test using local file %s rather than URI\n", g_file_name);
            }
        }
        else if (strstr(argv[i], "dtcp") != NULL)
        {
            g_use_dtcp = TRUE;
            g_print("Set to use dtcp URI\n");
        }
        else if (strstr(argv[i], "byte") != NULL)
        {
            g_format_bytes = TRUE;
            g_print("Set to use byte format for query and seek\n");
        }
        else if (strstr(argv[i], "dot") != NULL)
        {
            g_create_dot = TRUE;
            g_print("Set to generate dot file for pipeline diagram\n");
        }
        else if (strstr(argv[i], "changes=") != NULL)
        {
            if (sscanf(argv[i], "changes=%d", &g_rate_change_cnt) != 1)
            {
                g_printerr("Invalid rate change count arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested rate change count to %d\n", g_rate_change_cnt);
            }
        }
        else if (strstr(argv[i], "position") != NULL)
        {
            g_positions = TRUE;
            g_print("Set to test positioning\n");
        }
535 536 537 538 539
        else if (strstr(argv[i], "states") != NULL)
        {
            g_states = TRUE;
            g_print("Set to test state changes\n");
        }
540 541 542 543 544
        else if (strstr(argv[i], "playspeeds") != NULL)
        {
            g_playspeeds = TRUE;
            g_print("Set to change playspeeds from keyboard\n");
        }
545 546 547 548 549
        else if (strstr(argv[i], "handle_buffering") != NULL)
        {
            g_handle_buffering = TRUE;
            g_print("Set to handle buffering\n");
        }
550 551 552 553 554 555 556 557 558 559 560 561
        else if (strstr(argv[i], "seek_secs=") != NULL)
        {
            if (sscanf(argv[i], "seek_secs=%d", &g_seek_secs) != 1)
            {
                g_printerr("Invalid seek secs arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested seek secs to %d\n", g_seek_secs);
            }
        }
562 563 564 565 566 567 568 569 570 571 572 573
        else if (strstr(argv[i], "seek_percent=") != NULL)
        {
            if (sscanf(argv[i], "seek_percent=%d", &g_seek_percent) != 1)
            {
                g_printerr("Invalid seek percent arg specified: %s\n", argv[i]);
                return FALSE;
            }
            else
            {
                g_print("Set requested seek percent to %d\n", g_seek_percent);
            }
        }
574 575 576 577 578
        else if (strstr(argv[i], "seek_source") != NULL)
        {
            g_seek_source = TRUE;
            g_print("Set to seek on source of pipeline rather than on pipeline itself\n");
        }
579 580 581 582
        else
        {
            g_printerr("Invalid option: %s\n", argv[i]);
            g_printerr("Usage:\n");
583
            g_printerr("\t auto_audio \t\t create and use auto audio sink in playbin pipeline\n");
584 585
            g_printerr("\t ac3_audio \t\t use ac3parse and avdec_ac3 in manual pipeline\n");
            g_printerr("\t mpeg_audio \t\t use mpegaudioparse and avdec_mp2float in manual pipeline\n");
586 587 588
            g_printerr("\t byte \t\t use byte format for query and seek\n");
            g_printerr("\t changes=x \t\t where x is number of 2x increase rate changes\n");
            g_printerr("\t dot \t\t generate dot file of pipeline diagram\n");
589
            g_printerr("\t download_buffering \t\t enable playbin pipeline to buffer download\n");
590 591
            g_printerr("\t dtcp \t\t indicates content is dtcp/ip encrypted\n");
            g_printerr("\t file=name \t\twhere name indicates file name using path from env var\n");
592
            g_printerr("\t handle_buffering \t\t listen for buffering messages, changing playback state accordingly\n");
593 594 595 596
            g_printerr("\t host=ip \t\t addr of server\n");
            g_printerr("\t manual_uri_bin \t\t build manual pipeline using uri decode bin\n");
            g_printerr("\t manual_decode_bin \t\t build manual pipeline using decode bin\n");
            g_printerr("\t manual_elements \t\t build manual pipeline using decode elements\n");
597
            g_printerr("\t playspeeds \t\t allow changing of playspeeds from keyboard\n");
598 599 600
            g_printerr("\t position \t\t test positioning of seeks\n");
            g_printerr("\t rate=y \t\t where y is desired rate\n");
            g_printerr("\t rrid=i \t\t where i is cds recording id\n");
601
            g_printerr("\t seek_percent \t\t perform byte based seek to specified percent of total byte size\n");
602
            g_printerr("\t seek_secs \t\t perform seek to specified seconds\n");
603
            g_printerr("\t seek_source \t\t issue seek on pipeline's source rather than pipeline itself\n");
604
            g_printerr("\t simple \t\t create simple pipeline rather than playbin\n");
605
            g_printerr("\t states \t\t test state changes\n");
606
            g_printerr("\t switch \t\t play for wait secs then switch uri\n");
607
            g_printerr("\t two \t\t create two playbin pipelines\n");
608 609 610 611 612 613 614 615 616 617 618 619
            g_printerr("\t uri=l \t\t where l is uri of desired content\n");
            g_printerr("\t wait=x \t\t where x is secs\n");
            g_printerr("\t zero \t\t set start position of seek to zero\n");
            return FALSE;
        }
    }
    return TRUE;
}

/**
 * Create playbin pipeline
 */
620
static GstElement* create_playbin_pipeline(CustomData* data)
621 622
{
    GstElement* pipeline = NULL;
623
    GstBus* bus = NULL;
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    char launchLine[256];
    char* line1 = "playbin flags=0x3 uri=";
    if (!g_use_file)
    {
        sprintf(launchLine, "%s%s", line1, g_uri);
    }
    else
    {
        g_file_path = getenv(TEST_FILE_URL_PREFIX_ENV);
        if (g_file_path == NULL)
        {
            g_printerr ("Could not get env var %s value\n", TEST_FILE_URL_PREFIX_ENV);
            return NULL;
        }
        else
        {
            sprintf(launchLine, "%s%s%s", line1, g_file_path, g_file_name);
        }
    }

    g_print("Starting up playbin using line: %s\n", launchLine);
    pipeline = gst_parse_launch(launchLine, NULL);

647
    // Register to receive playbin source signal & call get property on source
648 649
    // to get supported playspeeds just like webkit would do
    g_signal_connect(pipeline, "notify::source", G_CALLBACK(on_source_changed), pipeline);
650 651
    bus = gst_element_get_bus (pipeline);
    g_signal_connect(bus, "message", G_CALLBACK(handle_message), data);
652 653 654 655 656 657 658 659 660 661

    // Uncomment this line to limit the amount of downloaded data
    // NOTE: Can't set ring buffer to anything besides zero otherwise queue will not forward events
    //g_object_set (pipeline, "ring-buffer-max-size", (guint64)4000, NULL);

    // Tried to force audio to fake sink since it complained about audio decoder
    // when trying to change URIs
    //GstElement* fake_sink = gst_element_factory_make ("fakesink", "fakesink");
    //g_object_set (pipeline, "audio-sink", fake_sink, NULL);

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    if (g_use_auto_audio)
    {
        if (!create_auto_audio_sink(pipeline))
        {
            g_printerr ("Problems creating auto audio sink\n");
            return NULL;
        }
        else
        {
            g_print("Created auto audio sink\n");
        }
    }

    if (g_download_buffering)
    {
        if (!enable_download_buffering(pipeline))
        {
            g_printerr ("Problems enabling download buffering\n");
            return NULL;
        }
        else
        {
            g_print("Playbin pipeline download buffering enabled\n");
        }
    }

688 689 690 691 692 693 694 695
    return pipeline;
}

/**
 * Create really simple pipeline with just src and sink
 */
static GstElement* create_simple_pipeline()
{
696
    GstElement* identity;
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

    // Create gstreamer elements
    GstElement* pipeline = gst_pipeline_new("simple-pipeline");
    if (!pipeline)
    {
        g_printerr ("Pipeline element could not be created.\n");
        return NULL;
    }
    GstElement* src = NULL;
    if (g_use_file)
    {
        g_print("%s() - using file source\n", __FUNCTION__);
        src = gst_element_factory_make ("filesrc", "file-source");
        if (!src)
        {
            g_printerr ("Filesrc element could not be created.\n");
            return NULL;
        }
        g_object_set(G_OBJECT(src), "location", "clock.mpg", NULL);
    }
    else
    {
        g_print("%s() - using dlnasrc\n", __FUNCTION__);
        src = gst_element_factory_make ("dlnasrc", "dlna http source");
        if (!src)
        {
            g_printerr ("dlnasrc element could not be created.\n");
            return NULL;
        }
        g_object_set(G_OBJECT(src), "uri", g_uri, NULL);
    }

729 730 731
    // Create identity element
    identity  = gst_element_factory_make ("identity", "identity");
    if (!identity)
732
    {
733
        g_printerr ("identity element could not be created.\n");
734 735 736 737 738 739 740 741 742 743 744 745 746
        return NULL;
    }

    GstElement* sink = gst_element_factory_make ("filesink", "file-sink");
    if (!sink)
    {
        g_printerr ("Filesink element could not be created.\n");
        return NULL;
    }
    g_object_set(G_OBJECT(sink), "location", "simple.out", NULL);

    // Add all elements into the pipeline
    gst_bin_add_many (GST_BIN (pipeline),
747
            src, identity, sink,
748 749 750
            NULL);

    // Link the elements together
751
    if (!gst_element_link_many (src, identity, sink, NULL))
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    {
        g_printerr ("Problems linking elements together\n");
        return NULL;
    }

    return pipeline;
}

/**
 * Create pipeline manually based on pipeline which
 * playbin creates.
 */
static GstElement* create_manual_pipeline(gchar* pipeline_name, gboolean use_bin)
{
    // Create gstreamer elements
    GstElement* pipeline = gst_pipeline_new(pipeline_name);
    if (!pipeline)
    {
        g_printerr ("Pipeline element could not be created.\n");
        return NULL;
    }

    // Check if local file uri should be used
    if (g_use_file)
    {
        g_file_path = getenv(TEST_FILE_URL_PREFIX_ENV);
        if (g_file_path == NULL)
        {
            g_printerr ("Could not get env var %s value\n", TEST_FILE_URL_PREFIX_ENV);
            return NULL;
        }
        else
        {
            sprintf(g_uri, "%s%s", g_file_path, g_file_name);
        }
    }

    // Create playsink
    g_sink  = gst_element_factory_make ("playsink", "playsink");
    if (!g_sink)
    {
        g_printerr ("playsink element could not be created.\n");
        return NULL;
    }
    gst_util_set_object_arg (G_OBJECT(g_sink), "flags",
            "soft-colorbalance+soft-volume+vis+text+audio+video");

    if (use_bin)
    {
        if (!create_manual_bin_pipeline(pipeline))
        {
            g_printerr ("%s() - problems creating decode bin based pipeline.\n",
                    __FUNCTION__);
            return NULL;
        }
    }
    else
    {
        if (!create_manual_elements_pipeline(pipeline))
        {
            g_printerr ("%s() - problems creating pipeline built from decode elements.\n",
                    __FUNCTION__);
            return NULL;
        }
    }

    g_print("%s() - creation of manual pipeline complete\n", __FUNCTION__);
    return pipeline;
}

/**
 * Create pipeline manually by creating a decode bin and linking in callback function.
 */
static gboolean create_manual_bin_pipeline(GstElement* pipeline)
{
    // Create decode elements
    GstElement* dbin = NULL;

    // Create URI decode bin
    g_print("%s() - creating uridecodebin element\n", __FUNCTION__);

    dbin  = gst_element_factory_make ("uridecodebin", "uridecodebin");
    if (!dbin)
    {
        g_printerr ("%s() - uridecodebin element could not be created.\n", __FUNCTION__);
        return FALSE;
    }
    g_object_set(G_OBJECT(dbin), "uri", g_uri, NULL);
    g_print("%s() - set uri decode bin uri: %s\n", __FUNCTION__, g_uri);

    // Create diagnostic element
843 844
    g_identity  = gst_element_factory_make ("identity", "identity");
    if (!g_identity)
845
    {
846
        g_printerr ("%s() - identity element could not be created.\n", __FUNCTION__);
847 848 849 850
        return FALSE;
    }

    gst_bin_add_many (GST_BIN (pipeline),
851
            dbin, g_identity, g_sink,
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
            NULL);

    // Add callback to link after source has been selected
    g_print("%s() - add callback on decode bin\n", __FUNCTION__);
    g_signal_connect (dbin, "pad-added", G_CALLBACK (bin_cb_pad_added), NULL);
    g_print("%s() - setup for decode bin callback\n", __FUNCTION__);

    g_print("%s() - creation of manual uri decode bin pipeline complete\n", __FUNCTION__);
    return TRUE;
}

/**
 * Callback function to link decode bin to playsink when building a manual pipeline
 * using a uri decode bin.
 */
static void
bin_cb_pad_added (GstElement *dec,
        GstPad     *srcpad,
        gpointer    data)
{
    g_print("%s() - called\n", __FUNCTION__);

    GstCaps *caps;
    GstStructure *str;
    const gchar *name;
    GstPadTemplate *templ;
    GstElementClass *klass;
    GstPad* d_src_pad;
    GstPad* d_sink_pad;

882 883
    d_src_pad = gst_element_get_static_pad(g_identity, "src");
    d_sink_pad = gst_element_get_static_pad(g_identity, "sink");
884 885
    if (!d_src_pad || !d_sink_pad)
    {
886
        g_printerr ("identity element pads could not be retrieved.\n");
887 888 889 890
        return;
    }
    if ((gst_pad_is_linked(d_sink_pad)) || (gst_pad_is_linked(d_src_pad)))
    {
891
        g_printerr ("identity element pads already linked\n");
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
        return;
    }

    /* check media type */
    caps = gst_pad_query_caps (srcpad, NULL);
    str = gst_caps_get_structure (caps, 0);
    name = gst_structure_get_name (str);

    klass = GST_ELEMENT_GET_CLASS (g_sink);

    if (g_str_has_prefix (name, "audio")) {
        templ = gst_element_class_get_pad_template (klass, "audio_sink");
    } else if (g_str_has_prefix (name, "video")) {
        templ = gst_element_class_get_pad_template (klass, "video_sink");
    } else if (g_str_has_prefix (name, "text")) {
        templ = gst_element_class_get_pad_template (klass, "text_sink");
    } else {
        templ = NULL;
    }

    if (templ)
    {
        g_print("%s() - got sink pad template\n", __FUNCTION__);
        GstPad *sinkpad;

        sinkpad = gst_element_request_pad (g_sink, templ, NULL, NULL);
        g_print("%s() - got sink pad from sink element\n", __FUNCTION__);

        if (!gst_pad_is_linked (sinkpad))
        {
            gst_pad_link (srcpad, d_sink_pad);
            gst_pad_link (d_src_pad, sinkpad);
            g_print("%s() - sink pad of sink element is now linked\n", __FUNCTION__);
        }
        else
        {
            g_print("%s() - sink pad of sink element was already linked\n", __FUNCTION__);
        }
        gst_object_unref (sinkpad);
    }
}

/**
 * Create pipeline manually by creating individual elements based on pipeline which
 * playbin creates.
 */
static gboolean create_manual_elements_pipeline(GstElement* pipeline)
{
    GstElement* tsdemux = NULL;
    GstElement* src = NULL;
    GstElement* mqueue = NULL;

    g_print("%s() - creating elements manually\n", __FUNCTION__);

    // Create either a file or dlna source
    if (g_use_file)
    {
        src = gst_element_factory_make ("filesrc", "file-source");
        g_object_set(G_OBJECT(src), "location", g_file_name, NULL);
    }
    else
    {
        src = gst_element_factory_make ("dlnasrc", "dlna-source");
        g_object_set(G_OBJECT(src), "uri", g_uri, NULL);
    }
    if (!src)
    {
        g_printerr ("%s() - src element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

    g_tee = gst_element_factory_make ("tee", "tee");
    if (!g_tee)
    {
        g_printerr ("%s() - tee element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

    mqueue  = gst_element_factory_make ("queue2", "mqueue");
    if (!mqueue)
    {
        g_printerr ("%s() - mpeg queue element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

    tsdemux = gst_element_factory_make ("tsdemux", "tsdemux");
    if (!tsdemux)
    {
        g_printerr ("%s() - tsdemux element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

    g_queue  = gst_element_factory_make ("multiqueue", "multiqueue");
    if (!g_queue)
    {
        g_printerr ("%s() - queue element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

    g_mpeg2dec  = gst_element_factory_make ("mpeg2dec", "mpeg2dec");
    if (!g_mpeg2dec)
    {
        g_printerr ("%s() - mpeg2dec element could not be created.\n", __FUNCTION__);
        return FALSE;
    }

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    if (g_use_ac3_audio == TRUE)
    {
        g_aparse = gst_element_factory_make ("ac3parse", "audioparser");
        g_avdec = gst_element_factory_make ("avdec_ac3", "audiodecoder");
    }
    else if (g_use_mpeg_audio == TRUE)
    {
        g_aparse = gst_element_factory_make ("mpegaudioparse", "audioparser");
        g_avdec = gst_element_factory_make ("avdec_mp2float", "audiodecoder");
    }
    else
    {
        g_aparse = NULL;
        g_avdec = NULL;
    }

1014 1015
    if (!g_aparse)
    {
1016
        g_printerr ("%s() - audioparse element could not be created.\n", __FUNCTION__);
1017 1018 1019 1020 1021
        return FALSE;
    }

    if (!g_avdec)
    {
1022
        g_printerr ("%s() - audiodecoder element could not be created.\n", __FUNCTION__);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        return FALSE;
    }

    gst_bin_add_many (GST_BIN (pipeline),
            src, g_tee, mqueue, tsdemux, g_queue, g_mpeg2dec,
            g_aparse, g_avdec,
            g_sink,
            NULL);

    // Can only link source to demux for now, rest is done in callback
    if (!gst_element_link_many (
            src, g_tee, mqueue, tsdemux,
            NULL))
    {
        g_printerr ("%s() - Problems linking filesrc to tsdemux\n", __FUNCTION__);
        return FALSE;
    }
    g_print("%s() - linked filesrc to tsdemux complete\n", __FUNCTION__);

    // Add callback to link after source has been selected
    g_print("%s() - registering tsdemux for callback\n", __FUNCTION__);
    g_signal_connect (tsdemux, "pad-added", G_CALLBACK (tsdemux_cb_pad_added), NULL);
    g_print("%s() - setup for tsdemux callback\n", __FUNCTION__);

    g_print("%s() - creation of manual pipeline complete\n", __FUNCTION__);
    return TRUE;
}

/**
 * Callback function to manually link up decode elements
 */
static void
tsdemux_cb_pad_added (GstElement *tsdemux,
        GstPad     *tsdemux_src_pad,
        gpointer    data)
{
    g_print("%s() - called\n", __FUNCTION__);

    gchar *name;
    GstCaps *caps;
    gchar *caps_string;

    name = gst_pad_get_name (tsdemux_src_pad);
    g_print ("%s() - new pad %s was created\n", __FUNCTION__, name);

    caps = gst_pad_query_caps (tsdemux_src_pad, NULL);
    caps_string = gst_caps_to_string (caps);
    gst_caps_unref (caps);
    g_print ("%s() - Pad Capability:  %s\n", __FUNCTION__, caps_string);

    if (strncmp (caps_string, "video/mpeg", 10) == 0)
    {
        if (link_video_elements(name, tsdemux_src_pad, caps))
        {
            g_print("%s() - setup of video elements complete\n", __FUNCTION__);
        }
        else
        {
            g_printerr("%s() - problems in setup of video elements\n", __FUNCTION__);
        }
    }
1084
    else if (strncmp (caps_string, "audio/", 6) == 0)
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    {
        if (link_audio_elements(name, tsdemux_src_pad, caps))
        {
            g_print("%s() - setup of audio elements complete\n", __FUNCTION__);
        }
        else
        {
            g_printerr("%s() - problems in setup of audio elements\n", __FUNCTION__);
        }
    }
    else
    {
        g_print ("%s() - Pad was not an audio or video pad\n", __FUNCTION__);
    }
    g_free (name);
    g_free (caps_string);
}

static gboolean
link_video_elements(gchar* name, GstPad* tsdemux_src_pad, GstCaps* caps)
{
    GstPad* queue_in_pad = NULL;
    GstPad* mpeg2dec_src_pad = NULL;
    GstPad* sink_in_pad = NULL;
    GstPadTemplate *templ;
    GstElementClass *klass;
    gboolean is_linked = FALSE;

    g_print("%s() - dynamic Video-pad created, linking %s to queue/Mpegparser\n", __FUNCTION__, name);

    // Link tsdemux src pad to queue
    queue_in_pad = gst_element_get_compatible_pad(g_queue, tsdemux_src_pad, caps);
    if (queue_in_pad != NULL)
    {
        g_print("%s() - got sink pad: %s ", __FUNCTION__, gst_pad_get_name (queue_in_pad));

        if (gst_pad_link (tsdemux_src_pad, queue_in_pad) == GST_PAD_LINK_OK)
        {
            g_print ("%s() - linked tdemux src pad to queue sink pad\n", __FUNCTION__);
        }
        else
        {
            g_print ("%s() - unable to link src pad to sink\n", __FUNCTION__);
            return FALSE;
        }
        gst_object_unref (queue_in_pad);
    }
    else
    {
        g_print ("%s() - Unable to get sink pad\n", __FUNCTION__);
        return FALSE;
    }

    // Link queue to mpegvparse just using default element links
    if (!gst_element_link_many (
            g_queue, g_mpeg2dec,
            NULL))
    {
        g_printerr ("%s() - problems linking video decode elements\n", __FUNCTION__);
        return FALSE;
    }

    // Link mpeg2dec video src pad to playsink
    // Get static video src pad of mpeg2dec to hook up to sink pad of playsink
    mpeg2dec_src_pad = gst_element_get_static_pad(g_mpeg2dec, "src");
    if (mpeg2dec_src_pad == NULL)
    {
        g_printerr ("%s() - unable to get video src output pad from mpeg2dec\n", __FUNCTION__);
        return FALSE;
    }

    // Get playsink's video sink pad template
    klass = GST_ELEMENT_GET_CLASS (g_sink);
    templ = gst_element_class_get_pad_template (klass, "video_sink");
    if (templ)
    {
        g_print("%s() - got sink video pad template\n", __FUNCTION__);

        sink_in_pad = gst_element_request_pad (g_sink, templ, NULL, NULL);
        if (sink_in_pad != NULL)
        {
            g_print("%s() - got sink pad from sink element\n", __FUNCTION__);

            if (gst_pad_link (mpeg2dec_src_pad, sink_in_pad) != GST_PAD_LINK_OK)
            {
                g_printerr ("%s() - unable to link mpeg2dec src pad to playsink sink pad\n", __FUNCTION__);
            }
            else
            {
                g_print("%s() - mpeg2dec and playsink are now linked\n", __FUNCTION__);
                is_linked = TRUE;
            }
            gst_object_unref (sink_in_pad);
        }
        else
        {
            g_printerr("%s() - Unable to get sink pad of playsink\n", __FUNCTION__);
        }
    }
    else
    {
        g_printerr("%s() - Unable to get template of video sink pad of playsink\n", __FUNCTION__);
    }

    return is_linked;
}

static gboolean
link_audio_elements(gchar* name, GstPad* tsdemux_src_pad, GstCaps* caps)
{
    GstPad* queue_in_pad = NULL;
    GstPad* avdec_src_pad = NULL;
    GstPad* sink_in_pad = NULL;
    GstPadTemplate *templ;
    GstElementClass *klass;
    gboolean is_linked = FALSE;

1202
    g_print("%s() - dynamic Audio-pad created, linking %s to queue/audioparser\n", __FUNCTION__, name);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

    // Link tsdemux src pad to queue
    queue_in_pad = gst_element_get_compatible_pad(g_queue, tsdemux_src_pad, caps);
    if (queue_in_pad != NULL)
    {
        g_print("%s() - got sink pad: %s ", __FUNCTION__, gst_pad_get_name (queue_in_pad));

        if (gst_pad_link (tsdemux_src_pad, queue_in_pad) == GST_PAD_LINK_OK)
        {
            g_print ("%s() - linked tdemux src pad to queue sink pad\n", __FUNCTION__);
        }
        else
        {
            g_printerr ("%s() - unable to link src pad to sink\n", __FUNCTION__);
            return FALSE;
        }
        gst_object_unref (queue_in_pad);
    }
    else
    {
        g_printerr ("%s() - Unable to get sink pad\n", __FUNCTION__);
        return FALSE;
    }

1227 1228 1229
    // Link queue to audioparser just using default element links
    g_print("%s() - audioparser(%p), audiodecoder(%p)\n",
        __FUNCTION__, g_aparse, g_avdec);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    if (!gst_element_link_many (
            g_queue, g_aparse, g_avdec,
            NULL))
    {
        g_printerr ("%s() - problems linking audio decode elements\n", __FUNCTION__);
        return FALSE;
    }

    // Get static audio src pad of avdec to hook up to sink pad of playsink
    avdec_src_pad = gst_element_get_static_pad(g_avdec, "src");
    if (avdec_src_pad == NULL)
    {
        g_printerr ("%s() - unable to get audio src output pad from avdec\n", __FUNCTION__);
        return FALSE;
    }

    // Get playsink's audio sink pad template
    klass = GST_ELEMENT_GET_CLASS (g_sink);
    templ = gst_element_class_get_pad_template (klass, "audio_sink");
    if (templ)
    {
        g_print("%s() - got sink audio pad template\n", __FUNCTION__);

        sink_in_pad = gst_element_request_pad (g_sink, templ, NULL, NULL);
        if (sink_in_pad != NULL)
        {
            g_print("%s() - got sink pad from sink element\n", __FUNCTION__);

            if (gst_pad_link (avdec_src_pad, sink_in_pad) != GST_PAD_LINK_OK)
            {
                g_printerr ("%s() - unable to link avdec src pad to playsink sink pad\n", __FUNCTION__);
            }
            else
            {
                is_linked = TRUE;
                g_print("%s() - avdec and playsink are now linked\n", __FUNCTION__);
            }
            gst_object_unref (sink_in_pad);
        }
        else
        {
            g_printerr("%s() - Unable to get sink pad of playsink\n", __FUNCTION__);
        }
    }
    else
    {
        g_printerr("%s() - Unable to get template of audio sink pad of playsink\n", __FUNCTION__);
    }
    return is_linked;
}

/**
 * Callback when playbin's source element changes
 */
static void on_source_changed(GstElement* element, GParamSpec* param, gpointer data)
{
    g_print("Notified of source change, gather supported rates\n");

    int i = 0;
    GstElement* src = NULL;
1290
    const gchar *src_type_name = NULL;
1291 1292 1293 1294 1295

    g_object_get(element, "source", &src, NULL);
    if (src != NULL)
    {
        g_print("Got src from callback, determine if dlnasrc\n");
1296
        g_source = src;
1297

1298 1299
        src_type_name = g_type_name(G_OBJECT_TYPE (src));
        if (src_type_name != NULL)
1300
        {
1301 1302
            g_print("type name of source for pipeline: %s\n", src_type_name);
            if (strcmp(src_type_name, "GstDlnaSrc") == 0)
1303
            {
1304 1305
                // Get supported rates property value which is a GArray
                g_print("Getting supported rates\n");
1306
                GArray* rates_array = NULL;
1307
                g_object_get(src, "supported-rates", &rates_array, NULL);
1308
                if (rates_array != NULL)
1309
                {
1310 1311
                    g_print("Supported rates cnt: %d\n", rates_array->len);
                    for (i = 0; i < rates_array->len; i++)
1312
                    {
1313
                        g_print("Retrieved rate %d: %f\n", (i+1), g_array_index(rates_array, gfloat, i));
1314 1315 1316 1317 1318
                    }
                }
                else
                {
                    g_printerr("Got null value for supported rates property\n");
1319 1320 1321 1322
                }
            }
            else
            {
1323
                g_print("dlnasrc is not source of pipeline\n");
1324 1325 1326 1327
            }
        }
        else
        {
1328
            g_print("Unable to get type name of source\n");
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        }
    }
    else
    {
        g_printerr("Unable to get src from callback\n");
    }
}

static void log_bin_elements(GstBin* bin)
{
    GstIterator* it = gst_bin_iterate_elements(bin);
    gboolean done = FALSE;
    GstElement *elem = NULL;
    GValue value = { 0 };

    while (!done)
    {
        switch (gst_iterator_next (it, &value))
        {
        case GST_ITERATOR_OK:
            elem = (GstElement *) g_value_get_object (&value);
            g_print("Bin %s has element: %s\n",
                    GST_ELEMENT_NAME(GST_ELEMENT(bin)),
                    GST_ELEMENT_NAME(elem));

            // If this is a bin, log its elements
            if (GST_IS_BIN(elem))
            {
                log_bin_elements(GST_BIN(elem));
            }
            else
            {
                g_print("Element: %s is linked to: ", GST_ELEMENT_NAME(elem));
                elem = log_element_links(elem);
            }
            g_value_unset (&value);
            break;
        case GST_ITERATOR_RESYNC:
            gst_iterator_resync (it);
            break;
        case GST_ITERATOR_ERROR:
            g_printerr("Unable to iterate through elements\n");
            done = TRUE;
            break;
        case GST_ITERATOR_DONE:
            done = TRUE;
            break;
        }
    }
    gst_iterator_free (it);
}

static GstElement* log_element_links(GstElement* elem)
{
    // Get sink pads of this element
    GstIterator* it = gst_element_iterate_src_pads(elem);
    gboolean done = FALSE;
    GstPad *pad = NULL;
    GstPad* peer = NULL;
    GstElement* ds_elem = NULL;
    int cnt = 0;
    GValue value = { 0 };
    while (!done)
    {
        switch (gst_iterator_next (it, &value))
        {
        case GST_ITERATOR_OK:
            pad = (GstPad *) g_value_get_object (&value);
            // Got a pad, check if its linked
            if (gst_pad_is_linked(pad))
            {
                cnt++;

                // Get peer of linkage
                peer = gst_pad_get_peer(pad);
                if (peer)
                {
                    // Get parent element
                    ds_elem = gst_pad_get_parent_element(peer);
                    if (ds_elem)
                    {
                        g_print(" %s\n", GST_ELEMENT_NAME(ds_elem));
                    }
                    else
                    {
                        g_print(" NULL Parent\n");
                    }
                }
            }
            g_value_unset (&value);
            break;
        case GST_ITERATOR_RESYNC:
            gst_iterator_resync (it);
            break;
        case GST_ITERATOR_ERROR:
            g_printerr("Unable to iterate through elements\n");
            done = TRUE;
            break;
        case GST_ITERATOR_DONE:
            done = TRUE;
            break;
        default:
            done = TRUE;
            break;
        }
    }
    gst_iterator_free (it);
    if (cnt == 0)
    {
        g_print(" Nothing\n");
    }

    return ds_elem;
}

static void perform_test(CustomData* data)
{
    GstBus *bus;
    GstMessage *msg;

    if (g_requested_rate >= 2)
    {
        perform_test_rate_change(data);
    }
    else if (g_requested_rate == 0)
    {
        g_usleep(g_wait_secs * 1000000L);

        g_print("%s - Pausing pipeline for %d secs\n", __FUNCTION__, g_wait_secs);
1458
        set_pipeline_state (data->pipeline, GST_STATE_PAUSED, g_state_change_timeout_secs);
1459 1460 1461 1462

        g_usleep(g_wait_secs * 1000000L);

        g_print("%s - Resuming pipeline after %d sec pause\n", __FUNCTION__, g_wait_secs);
1463
        set_pipeline_state (data->pipeline, GST_STATE_PLAYING, g_state_change_timeout_secs);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
    }
    else if (g_test_uri_switch)
    {
        // Set eos count to two since one will be received for each EOS
        g_eos_max_cnt = 2;
        if (!set_new_uri(data))
        {
            g_print("Problems setting new URI\n");
            return;
        }
    }
    else if (g_positions)
    {
        perform_test_position(data);
    }
1479 1480 1481 1482
    else if (g_states)
    {
        perform_test_states(data);
    }
1483 1484 1485 1486
    else if (g_playspeeds)
    {
        perform_test_playspeeds(data);
    }
1487 1488 1489 1490 1491
    else if (g_seek_secs != 0)
    {
        g_usleep(g_wait_secs * 1000000L);
        perform_test_seek(data, g_seek_secs*GST_SECOND, GST_FORMAT_TIME, 1);
    }
1492 1493 1494 1495
    else if (g_seek_percent != 0)
    {
        perform_test_seek_byte_percent(data);
    }
1496 1497 1498 1499 1500 1501 1502 1503

    // Wait until error or EOS
    bus = gst_element_get_bus (data->pipeline);
    g_print("%s - Waiting for EOS or ERROR\n", __FUNCTION__);
    gboolean done = FALSE;
    while (!done)
    {
        msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
1504
                GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_BUFFERING);
1505 1506
        if (msg != NULL)
        {
1507
            //g_print("%s - Received msg type: %s\n", __FUNCTION__, GST_MESSAGE_TYPE_NAME(msg));
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
            handle_message(data, msg);
            gst_message_unref (msg);

            if (data->terminate)
            {
                done = TRUE;
            }
        }
    }
}

static gboolean perform_test_rate_change(CustomData* data)
{
    int i = 0;
    gfloat rate = 0;
    long secs = g_wait_secs;
    gint64 position = -1;

    for (i = 0; i < g_rate_change_cnt; i++)
    {
        rate = pow(g_requested_rate, i+1);

        g_print("%s - Waiting %ld secs prior to change %d of %d using rate %0.2f\n",
                __FUNCTION__, secs, (i+1), g_rate_change_cnt, rate);
        g_usleep(secs * 1000000L);

        // Determine what format to use for query and seek
        GstFormat format = GST_FORMAT_TIME;
        if (g_format_bytes)
        {
            format = GST_FORMAT_BYTES;
        }
        g_print("%s - Test pipeline using format: %s\n",
                __FUNCTION__, gst_format_get_name(format));

        // Query current position, duration and rate
1544
        if (!perform_test_query(data, &position, format))
1545
        {
1546
            g_printerr("%s - Problems with query associated with test.\n",
1547
                        __FUNCTION__);
1548
            return FALSE;
1549 1550 1551
        }

        // Initiate seek to perform test
1552
        if (!perform_test_seek(data, position, format, rate))
1553
        {
1554
            g_printerr("%s - Problems with seek associated with test.\n",
1555
                        __FUNCTION__);
1556
            return FALSE;
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
        }
    }

    return TRUE;
}

static gboolean perform_test_position(CustomData* data)
{
    int i = 0;
    gfloat rate = 1.0;
    long secs = g_wait_secs;
    guint64 position = 0L;

1570
    g_print("%s - Performing position changes at 2, 1, 3, and 1 mins\n",  __FUNCTION__);
1571

1572
    guint64 positions[] = {2L,1L,3L,1L};
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    for (i = 0; i < 4; i++)
    {
        g_print("%s - Waiting %ld secs prior to seeking to %" G_GUINT64_FORMAT " minutes\n",
                __FUNCTION__, secs, positions[i]);
        g_usleep(secs * 1000000L);

        position = positions[i] * NANOS_PER_MINUTE;
        g_print("%s - Seeking to time position: %" GST_TIME_FORMAT "\n",
                __FUNCTION__, GST_TIME_ARGS (position));

        // Initiate seek to perform test
        if (!perform_test_seek(data, position, GST_FORMAT_TIME, rate))
        {
            g_printerr("%s - Problems with seek associated with test.\n",
                    __FUNCTION__);
            return FALSE;
        }
    }

    return TRUE;
}

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
static gboolean perform_test_states(CustomData* data)
{
    int i = 0;
    GstState state = GST_STATE_PAUSED;
    GstState current_state = GST_STATE_PAUSED;
    GstState pending_state = GST_STATE_PAUSED;
    g_print("%s - Performing 5 state changes\n",  __FUNCTION__);
    GstStateChangeReturn ret;

    for (i = 0; i < 30; i++)
    {
        g_print("%s - Waiting %d secs prior to changing state to %s\n",
                __FUNCTION__, g_wait_secs, gst_element_state_get_name(state));
        g_usleep(g_wait_secs * 1000000L);

        g_print("%s - Setting pipeline state to %s\n",  __FUNCTION__, gst_element_state_get_name(state));
        ret = gst_element_set_state(data->pipeline, state);
        //ret = gst_element_set_state(data->pipeline_2, state);

        if (ret == GST_STATE_CHANGE_ASYNC)
        {
            g_print("%s - Waiting for async state change\n",  __FUNCTION__);
            ret = gst_element_get_state(data->pipeline, &current_state, &pending_state, (10 * 1000000L));
            g_print("%s - Got state for async pipeline state, status: %s, current: %s, pending: %s\n",  __FUNCTION__,
                    gst_element_state_change_return_get_name(ret), gst_element_state_get_name(current_state),
                    gst_element_state_get_name(pending_state));
            if (ret != GST_STATE_CHANGE_SUCCESS)
            {
                g_printerr("%s - Async state change failed to complete\n", __FUNCTION__);
                return FALSE;
            }
        }
        else if (ret == GST_STATE_CHANGE_FAILURE)
        {
            g_printerr("%s - Failed to change to state %s\n",
                    __FUNCTION__, gst_element_state_get_name(state));
            return FALSE;
        }
        else
        {
            g_print("%s - Got successful pipeline state change\n",  __FUNCTION__);
        }

        if (state == GST_STATE_PAUSED)
        {
            state = GST_STATE_PLAYING;
        }
        else
        {
            state = GST_STATE_PAUSED;
        }
    }

    return TRUE;
}

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
static gboolean perform_test_query(CustomData* data, gint64* position, GstFormat format)
{
    g_print("%s - Query position in format: %s\n",
            __FUNCTION__, gst_format_get_name(format));

    // Query current time
    if (!gst_element_query_position(data->pipeline, format, position))
    {
        g_printerr("%s - Unable to retrieve current position.\n",
                __FUNCTION__);
        return FALSE;
    }
    if (format == GST_FORMAT_TIME)
    {
        g_print("%s - Got current time position: %" GST_TIME_FORMAT "\n",
                __FUNCTION__, GST_TIME_ARGS (*position));
    }

    // Query duration
    if (!GST_CLOCK_TIME_IS_VALID (data->duration))
    {
        if (!gst_element_query_duration (data->pipeline, GST_FORMAT_TIME, &data->duration))
        {
            g_printerr ("Could not query current duration.\n");
            return FALSE;
        }
        else
        {
            if (format == GST_FORMAT_TIME)
            {
                g_print("%s - Got time duration: %" GST_TIME_FORMAT "\n",
                        __FUNCTION__, GST_TIME_ARGS (data->duration));
            }
        }
    }

    // Get the current playback rate
    GstQuery* query = NULL;
    GstFormat fmt;
    gint64 start;
    gint64 stop;
    query = gst_query_new_segment(GST_FORMAT_TIME);
    if (query != NULL)
    {
        gst_element_query(data->pipeline, query);
        gst_query_parse_segment(query, &data->rate, &fmt, &start, &stop);
        gst_query_unref(query);
        //g_print ("\nCurrent playspeed: %3.1f\n\n", data.rate);
    }
    else
    {
        g_printerr ("Could not query segment to get playback rate\n");
        return FALSE;
    }

    // Print current position and total duration
    if (!g_format_bytes)
    {
        g_print ("Current Time Position %" GST_TIME_FORMAT " / Total Duration %" GST_TIME_FORMAT "\r",
                GST_TIME_ARGS (*position), GST_TIME_ARGS (data->duration));
    }

    return TRUE;
}

static gboolean perform_test_seek(CustomData* data, gint64 start_position, GstFormat format, gfloat rate)
{
    gint64 stop_position = GST_CLOCK_TIME_NONE;

    // Determine if seek is necessary for requested test
    if ((rate == 0) || (g_test_uri_switch))
    {
        g_print("%s - No seeking necessary for requested test\n", __FUNCTION__);
        return TRUE;
    }

    // If requested, set position to zero
    if (g_zero_position)
    {
        start_position = 0;
    }

    // If reverse playspeed, set stop to zero
    if (rate < 0)
    {
        stop_position = 0;
    }

    if (format == GST_FORMAT_TIME)
    {
        g_print("%s - Seeking to time position: %" GST_TIME_FORMAT " at rate %3.1f\n",
                __FUNCTION__, GST_TIME_ARGS (start_position), rate);
    }
    else
    {
        g_print("%s - Requesting rate of %4.1f at position %" G_GUINT64_FORMAT " using format %s\n",
                __FUNCTION__, rate, start_position, gst_format_get_name(format));
    }

    // Initiate seek
1751
    if (g_seek_source)
1752
    {
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
        if (g_source)
        {
            g_print("%s - Sending seek to source\n", __FUNCTION__);
            if (!gst_element_seek (g_source, rate,
                    format, GST_SEEK_FLAG_FLUSH,
                    GST_SEEK_TYPE_SET, start_position,
                    GST_SEEK_TYPE_NONE, stop_position))
            {
                g_printerr("%s - Problems seeking.\n", __FUNCTION__);
                return FALSE;
            }
        }
        else
        {
            g_printerr("%s - Unable to seek on NULL source\n", __FUNCTION__);
            return FALSE;
        }
    }
    else
    {
        g_print("%s - Sending seek to pipeline\n", __FUNCTION__);
        if (!gst_element_seek (data->pipeline, rate,
                format, GST_SEEK_FLAG_FLUSH,
                GST_SEEK_TYPE_SET, start_position,
                GST_SEEK_TYPE_NONE, stop_position))
        {
            g_printerr("%s - Problems seeking.\n", __FUNCTION__);
            return FALSE;
        }
1782 1783 1784 1785 1786 1787
    }
    g_print("%s - Seeking on pipeline element complete\n", __FUNCTION__);

    return TRUE;
}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
static gboolean perform_test_seek_byte_percent(CustomData* data)
{
    gint64 total_bytes;
    guint64 position;
    gfloat percent;

    g_print("%s - Seek to byte percent: %d\n", __FUNCTION__, g_seek_percent);

    g_usleep(g_wait_secs * 1000000L);

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
    if (g_seek_source) {
        if (g_source) {
            if (!gst_element_query_duration (g_source, GST_FORMAT_BYTES, &total_bytes))
            {
                g_printerr ("Could not query source for current duration.\n");
                return FALSE;
            }
            else
            {
                g_print("%s - Got total bytes: %" G_GINT64_FORMAT " \n", __FUNCTION__, total_bytes);
            }
        } else {
            g_printerr ("No source could not query current duration.\n");
            return FALSE;
        }
1813
    }
1814
    else // query pipeline for duration
1815
    {
1816 1817 1818 1819 1820 1821 1822 1823 1824
        if (!gst_element_query_duration (data->pipeline, GST_FORMAT_BYTES, &total_bytes))
        {
            g_printerr ("Could not query pipeline for current duration.\n");
            return FALSE;
        }
        else
        {
            g_print("%s - Got total bytes: %" G_GINT64_FORMAT " \n", __FUNCTION__, total_bytes);
        }
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
    }

    // Calculate byte for percent requested
    percent = g_seek_percent / 100.0;
    position = (guint64)(percent * total_bytes);
    g_print("%s - Byte position is %" G_GUINT64_FORMAT " based on %d percent\n",
            __FUNCTION__, position, g_seek_percent);

    return perform_test_seek(data, position, GST_FORMAT_BYTES, 1);
}

1836 1837 1838 1839
static gboolean perform_test_playspeeds(CustomData* data)
{
    GIOChannel *io_stdin;

1840
    g_print("%s - Performing playspeeds test\n",  __FUNCTION__);
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914

    /* 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");

    /* Add a keyboard watch so we get notified of keystrokes */
    g_print ("Setting up keyboard watch\n");
  #ifdef _WIN32
    io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
  #else
    io_stdin = g_io_channel_unix_new (fileno (stdin));
  #endif
    g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, data);

    g_print ("Setting rate to 1.0\n");
    data->rate = 1.0;

    // Create a GLib Main Loop and set it to run
    g_print ("Entering main loop\n");
    data->loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (data->loop);

    /* Free resources */
    g_print ("Exiting\n");
    g_main_loop_unref (data->loop);
    g_io_channel_unref (io_stdin);

    return TRUE;
}

/* Process keyboard input */
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
  gchar *str = NULL;

  g_print ("Called to handle keyboard input\n");

  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;
    g_print ("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
    gst_element_set_state (data->pipeline, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
    break;

  case 's':
    g_print ("Current rate: %2.0f\n", data->rate);
    if (g_ascii_isupper (str[0])) {
      data->rate *= 2.0;
    } else {
      data->rate /= 2.0;
    }
    g_print ("Adjusting rate to: %2.0f\n", data->rate);
    if (send_seek_event (data)) {
      g_print ("Rate changed to: %2.0f\n", data->rate);
    } else {
      g_print ("Problems changing rate to: %2.0f\n", data->rate);
      return FALSE;
    }
    break;

  case 'd':
    data->rate *= -1.0;
    g_print ("Adjusting rate to: %2.0f\n", data->rate);
    send_seek_event (data);
    break;

1915 1916 1917 1918 1919 1920
  case 'f':
    data->rate *= 0.5;
    g_print ("Adjusting rate to: %2.0f\n", data->rate);
    send_seek_event (data);
    break;

1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
  case 'n':
    gst_element_send_event (data->pipeline,
        gst_event_new_step (GST_FORMAT_BUFFERS, 1, data->rate, TRUE, FALSE));
    g_print ("Stepping one frame\n");
    break;

  case 'q':
    g_print ("Quitting\n");
    g_main_loop_quit (data->loop);
    break;

  default:
    break;
  }

  g_free (str);

  return TRUE;
}

static gboolean send_seek_event (CustomData *data) {
  gint64 position;
  GstFormat format = GST_FORMAT_TIME;

  g_print ("Sending Seek Event\n");

  if (!gst_element_query_position(data->pipeline, format, &position))
  {
      g_printerr("%s - Unable to retrieve current position.\n",
              __FUNCTION__);
      return FALSE;
  }

  /* Create the seek event */
  if (!gst_element_seek (data->pipeline, data->rate,
          format, GST_SEEK_FLAG_FLUSH,
          GST_SEEK_TYPE_SET, position,
          GST_SEEK_TYPE_SET, -1))
      //GST_SEEK_TYPE_NONE, 0))
  {
      g_printerr("%s - Problems seeking.\n", __FUNCTION__);
      return FALSE;
  }
  else
  {
      printf("%s - seek was successful\n", __FUNCTION__);
  }

  return TRUE;
}

1972
static gboolean set_pipeline_state(GstElement* pipeline, GstState desired_state, gint timeoutSecs)
1973
{
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
    GstState state = GST_STATE_PAUSED;
    GstState current_state = GST_STATE_PAUSED;
    GstState pending_state = GST_STATE_PAUSED;

    printf("set_pipeline_state() called with state: %s\n",
            gst_element_state_get_name(desired_state));

    // Determine current state
    GstStateChangeReturn ret = gst_element_get_state(pipeline, &current_state, &pending_state, (10 * 1000000L));

    if ((desired_state == current_state ) || (desired_state == pending_state)) {
        // Nothing to do already in desired state, just return
        // *TODO* - what about async change???
        printf("already in desired state: %s\n", gst_element_state_get_name(desired_state));
        return TRUE;
    }

    ret = gst_element_set_state(pipeline, desired_state);
1992 1993 1994 1995 1996 1997 1998

    if (ret == GST_STATE_CHANGE_FAILURE)
    {
        g_printerr ("Unable to set playbin to desired state: %s\n",
                gst_element_state_get_name(desired_state));
        return FALSE;
    }
1999 2000 2001 2002 2003 2004
    else if (ret == GST_STATE_CHANGE_SUCCESS)
    {
        printf("Successfully changed to state: %s\n", gst_element_state_get_name(desired_state));
        return TRUE;
    }
    else if (ret == GST_STATE_CHANGE_ASYNC)
2005 2006 2007 2008 2009
    {
        printf("State change is async, calling get state to wait %d secs for state\n",
                timeoutSecs);
        int maxCnt = timeoutSecs;
        int curCnt = 0;
2010
        state = GST_STATE_NULL;
2011 2012
        do
        {
2013
            ret = gst_element_get_state(pipeline, // element
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
                    &state, // state
                    NULL, // pending
                    100000000LL); // timeout(1 second = 10^9 nanoseconds)

            if (ret == GST_STATE_CHANGE_SUCCESS)
            {
                printf("State change succeeded, now in desired state: %s\n",
                        gst_element_state_get_name(desired_state));
                return TRUE;
            }
            else if (ret == GST_STATE_CHANGE_FAILURE)
            {
                g_printerr ("State change failed\n");
                return FALSE;
            }
            else if (ret == GST_STATE_CHANGE_ASYNC)
            {
                g_printerr ("State change time out: %d secs\n", curCnt);
            }
            else
            {
                g_printerr ("Unknown state change return value: %d\n", ret);
                return FALSE;
            }
            curCnt++;

            // Sleep for a short time
            g_usleep(1000000L);
        }
        while ((desired_state != state) && (curCnt < maxCnt));
    }
    g_printerr("Timed out waiting %d secs for desired state: %s\n",
            timeoutSecs, gst_element_state_get_name(desired_state));
    return FALSE;
}

static gboolean set_new_uri(CustomData* data)
{
    // Formulate a new URI
    gchar new_uri[256];
    new_uri[0] = '\0';