gst_plugins_base/tests/check/pipelines/gio.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer
       
     2  *
       
     3  * unit test for GIO
       
     4  *
       
     5  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
       
     6  *
       
     7  * This library is free software; you can redistribute it and/or
       
     8  * modify it under the terms of the GNU Library General Public
       
     9  * License as published by the Free Software Foundation; either
       
    10  * version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This library is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  * Library General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU Library General Public
       
    18  * License along with this library; if not, write to the
       
    19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    20  * Boston, MA 02111-1307, USA.
       
    21  */
       
    22 
       
    23 #include <gst/check/gstcheck.h>
       
    24 #include <gst/check/gstbufferstraw.h>
       
    25 #include <gio/gio.h>
       
    26 
       
    27 static gboolean got_eos = FALSE;
       
    28 
       
    29 static gboolean
       
    30 message_handler (GstBus * bus, GstMessage * msg, gpointer data)
       
    31 {
       
    32   GMainLoop *loop = (GMainLoop *) data;
       
    33 
       
    34   switch (GST_MESSAGE_TYPE (msg)) {
       
    35     case GST_MESSAGE_EOS:
       
    36       got_eos = TRUE;
       
    37       g_main_loop_quit (loop);
       
    38       break;
       
    39     case GST_MESSAGE_ERROR:{
       
    40       gchar *debug;
       
    41       GError *err;
       
    42 
       
    43       gst_message_parse_error (msg, &err, &debug);
       
    44       g_free (debug);
       
    45 
       
    46       /* Will abort the check */
       
    47       g_warning ("Error: %s\n", err->message);
       
    48       g_error_free (err);
       
    49 
       
    50       g_main_loop_quit (loop);
       
    51       break;
       
    52     }
       
    53     default:
       
    54       break;
       
    55   }
       
    56 
       
    57   return TRUE;
       
    58 }
       
    59 
       
    60 GST_START_TEST (test_memory_stream)
       
    61 {
       
    62   GMainLoop *loop;
       
    63   GstElement *bin;
       
    64   GstElement *src, *sink;
       
    65   GstBus *bus;
       
    66 
       
    67   GMemoryInputStream *input;
       
    68   GMemoryOutputStream *output;
       
    69 
       
    70   guint8 *in_data;
       
    71   guint8 *out_data;
       
    72   gint i;
       
    73   GstFormat fmt = GST_FORMAT_BYTES;
       
    74   gint64 duration;
       
    75 
       
    76   got_eos = FALSE;
       
    77 
       
    78   in_data = g_new (guint8, 512);
       
    79   out_data = g_new (guint8, 512);
       
    80   for (i = 0; i < 512; i++)
       
    81     in_data[i] = i % 256;
       
    82 
       
    83   input =
       
    84       G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
       
    85           (GDestroyNotify) g_free));
       
    86 
       
    87   output = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (out_data, 512,
       
    88           (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
       
    89   out_data = NULL;
       
    90 
       
    91   loop = g_main_loop_new (NULL, FALSE);
       
    92 
       
    93   bin = gst_pipeline_new ("bin");
       
    94 
       
    95   src = gst_element_factory_make ("giostreamsrc", "src");
       
    96   fail_unless (src != NULL);
       
    97   g_object_set (G_OBJECT (src), "stream", input, NULL);
       
    98 
       
    99   sink = gst_element_factory_make ("giostreamsink", "sink");
       
   100   fail_unless (sink != NULL);
       
   101   g_object_set (G_OBJECT (sink), "stream", output, NULL);
       
   102 
       
   103   gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
       
   104 
       
   105   fail_unless (gst_element_link_many (src, sink, NULL));
       
   106 
       
   107   bus = gst_element_get_bus (bin);
       
   108   gst_bus_add_watch (bus, message_handler, loop);
       
   109   gst_object_unref (bus);
       
   110 
       
   111   gst_element_set_state (bin, GST_STATE_PAUSED);
       
   112 
       
   113   fail_unless (gst_element_query_duration (bin, &fmt, &duration));
       
   114   fail_unless_equals_int (duration, 512);
       
   115 
       
   116   gst_element_set_state (bin, GST_STATE_PLAYING);
       
   117 
       
   118   g_main_loop_run (loop);
       
   119 
       
   120   gst_element_set_state (bin, GST_STATE_NULL);
       
   121   gst_object_unref (bin);
       
   122 
       
   123   fail_unless (got_eos);
       
   124 
       
   125   out_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output));
       
   126 
       
   127   for (i = 0; i < 512; i++)
       
   128     fail_unless_equals_int (in_data[i], out_data[i]);
       
   129 
       
   130   g_object_unref (input);
       
   131   g_object_unref (output);
       
   132 
       
   133   g_main_loop_unref (loop);
       
   134 }
       
   135 
       
   136 GST_END_TEST;
       
   137 
       
   138 static Suite *
       
   139 gio_testsuite (void)
       
   140 {
       
   141   Suite *s = suite_create ("gio");
       
   142   TCase *tc_chain = tcase_create ("general");
       
   143 
       
   144   suite_add_tcase (s, tc_chain);
       
   145   tcase_add_test (tc_chain, test_memory_stream);
       
   146 
       
   147   return s;
       
   148 }
       
   149 
       
   150 int
       
   151 main (int argc, char **argv)
       
   152 {
       
   153   int nf;
       
   154 
       
   155   Suite *s = gio_testsuite ();
       
   156   SRunner *sr = srunner_create (s);
       
   157 
       
   158   gst_check_init (&argc, &argv);
       
   159 
       
   160   srunner_run_all (sr, CK_NORMAL);
       
   161   nf = srunner_ntests_failed (sr);
       
   162   srunner_free (sr);
       
   163 
       
   164   return nf;
       
   165 }