gst_plugins_base/tests/check/elements/decodebin.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer unit tests for decodebin
       
     2  *
       
     3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 #ifdef HAVE_CONFIG_H
       
    22 # include <config.h>
       
    23 #endif
       
    24 
       
    25 #include <gst/check/gstcheck.h>
       
    26 #include <unistd.h>
       
    27 
       
    28 static const gchar dummytext[] =
       
    29     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
       
    30     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
       
    31     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
       
    32     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
       
    33     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
       
    34     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
       
    35     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox "
       
    36     "jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown "
       
    37     "Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick "
       
    38     "Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog "
       
    39     "Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a Lazy "
       
    40     "Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps over a "
       
    41     "Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox Jumps "
       
    42     "over a Lazy Frog Quick Brown Fox Jumps over a Lazy Frog Quick Brown Fox ";
       
    43 
       
    44 static void
       
    45 src_handoff_cb (GstElement * src, GstBuffer * buf, GstPad * pad, gpointer data)
       
    46 {
       
    47   GST_BUFFER_DATA (buf) = (guint8 *) dummytext;
       
    48   GST_BUFFER_SIZE (buf) = sizeof (dummytext);
       
    49   GST_BUFFER_OFFSET (buf) = 0;
       
    50   GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY);
       
    51 }
       
    52 
       
    53 static void
       
    54 decodebin_new_decoded_pad_cb (GstElement * decodebin, GstPad * pad,
       
    55     gboolean last, gboolean * p_flag)
       
    56 {
       
    57   /* we should not be reached */
       
    58   fail_unless (decodebin == NULL, "new-decoded-pad should not be emitted");
       
    59 }
       
    60 
       
    61 /* make sure that decodebin errors out instead of creating a new decoded pad
       
    62  * if the entire stream is a plain text file */
       
    63 GST_START_TEST (test_text_plain_streams)
       
    64 {
       
    65   GstElement *pipe, *src, *decodebin;
       
    66   GstMessage *msg;
       
    67 
       
    68   pipe = gst_pipeline_new (NULL);
       
    69   fail_unless (pipe != NULL, "failed to create pipeline");
       
    70 
       
    71   src = gst_element_factory_make ("fakesrc", "src");
       
    72   fail_unless (src != NULL, "Failed to create fakesrc element");
       
    73 
       
    74   g_object_set (src, "signal-handoffs", TRUE, NULL);
       
    75   g_object_set (src, "num-buffers", 1, NULL);
       
    76   g_object_set (src, "can-activate-pull", FALSE, NULL);
       
    77   g_signal_connect (src, "handoff", G_CALLBACK (src_handoff_cb), NULL);
       
    78 
       
    79   decodebin = gst_element_factory_make ("decodebin", "decodebin");
       
    80   fail_unless (decodebin != NULL, "Failed to create decodebin element");
       
    81 
       
    82   g_signal_connect (decodebin, "new-decoded-pad",
       
    83       G_CALLBACK (decodebin_new_decoded_pad_cb), NULL);
       
    84 
       
    85   fail_unless (gst_bin_add (GST_BIN (pipe), src));
       
    86   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
       
    87   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
       
    88 
       
    89   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
       
    90       GST_STATE_CHANGE_SUCCESS);
       
    91   /* it's push-based, so should be async */
       
    92   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
       
    93       GST_STATE_CHANGE_ASYNC);
       
    94 
       
    95   /* it should error out at some point */
       
    96   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, -1);
       
    97   fail_unless (msg != NULL);
       
    98   fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
       
    99   gst_message_unref (msg);
       
   100 
       
   101   gst_element_set_state (pipe, GST_STATE_NULL);
       
   102   gst_object_unref (pipe);
       
   103 }
       
   104 
       
   105 GST_END_TEST;
       
   106 
       
   107 static void
       
   108 new_decoded_pad_plug_fakesink_cb (GstElement * decodebin, GstPad * srcpad,
       
   109     gboolean last, GstElement * pipeline)
       
   110 {
       
   111   GstElement *sink;
       
   112   GstPad *sinkpad;
       
   113 
       
   114   sink = gst_element_factory_make ("fakesink", "sink");
       
   115   fail_unless (sink != NULL, "Failed to create fakesink element");
       
   116 
       
   117   gst_bin_add (GST_BIN (pipeline), sink);
       
   118 
       
   119   sinkpad = gst_element_get_static_pad (sink, "sink");
       
   120   fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);
       
   121   gst_object_unref (sinkpad);
       
   122 
       
   123   gst_element_set_state (sink, GST_STATE_PLAYING);
       
   124 }
       
   125 
       
   126 GST_START_TEST (test_reuse_without_decoders)
       
   127 {
       
   128   GstElement *pipe, *src, *decodebin, *sink;
       
   129 
       
   130   pipe = gst_pipeline_new (NULL);
       
   131   fail_unless (pipe != NULL, "failed to create pipeline");
       
   132 
       
   133   src = gst_element_factory_make ("audiotestsrc", "src");
       
   134   fail_unless (src != NULL, "Failed to create audiotestsrc element");
       
   135 
       
   136   decodebin = gst_element_factory_make ("decodebin", "decodebin");
       
   137   fail_unless (decodebin != NULL, "Failed to create decodebin element");
       
   138 
       
   139   g_signal_connect (decodebin, "new-decoded-pad",
       
   140       G_CALLBACK (new_decoded_pad_plug_fakesink_cb), pipe);
       
   141 
       
   142   fail_unless (gst_bin_add (GST_BIN (pipe), src));
       
   143   fail_unless (gst_bin_add (GST_BIN (pipe), decodebin));
       
   144   fail_unless (gst_element_link (src, decodebin), "can't link src<->decodebin");
       
   145 
       
   146   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
       
   147       GST_STATE_CHANGE_SUCCESS);
       
   148   /* it's push-based, so should be async */
       
   149   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
       
   150       GST_STATE_CHANGE_ASYNC);
       
   151 
       
   152   /* wait for state change to complete */
       
   153   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
       
   154       GST_STATE_CHANGE_SUCCESS);
       
   155 
       
   156   /* there shouldn't be any errors */
       
   157   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
       
   158 
       
   159   /* reset */
       
   160   gst_element_set_state (pipe, GST_STATE_NULL);
       
   161 
       
   162   sink = gst_bin_get_by_name (GST_BIN (pipe), "sink");
       
   163   gst_bin_remove (GST_BIN (pipe), sink);
       
   164   gst_object_unref (sink);
       
   165 
       
   166   GST_LOG ("second try");
       
   167 
       
   168   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_READY),
       
   169       GST_STATE_CHANGE_SUCCESS);
       
   170   /* it's push-based, so should be async */
       
   171   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PAUSED),
       
   172       GST_STATE_CHANGE_ASYNC);
       
   173 
       
   174   /* wait for state change to complete */
       
   175   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
       
   176       GST_STATE_CHANGE_SUCCESS);
       
   177 
       
   178   /* there shouldn't be any errors */
       
   179   fail_if (gst_bus_poll (GST_ELEMENT_BUS (pipe), GST_MESSAGE_ERROR, 0) != NULL);
       
   180 
       
   181   gst_element_set_state (pipe, GST_STATE_NULL);
       
   182   gst_object_unref (pipe);
       
   183 }
       
   184 
       
   185 GST_END_TEST;
       
   186 
       
   187 static Suite *
       
   188 decodebin_suite (void)
       
   189 {
       
   190   Suite *s = suite_create ("decodebin");
       
   191   TCase *tc_chain = tcase_create ("general");
       
   192 
       
   193   suite_add_tcase (s, tc_chain);
       
   194   tcase_add_test (tc_chain, test_text_plain_streams);
       
   195   tcase_add_test (tc_chain, test_reuse_without_decoders);
       
   196 
       
   197   return s;
       
   198 }
       
   199 
       
   200 GST_CHECK_MAIN (decodebin);