Blame tests/check/pipelines/gio.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * unit test for GIO
Packit 971217
 *
Packit 971217
 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/check/gstcheck.h>
Packit 971217
#include <gst/check/gstbufferstraw.h>
Packit 971217
#include <gio/gio.h>
Packit 971217
Packit 971217
static gboolean got_eos = FALSE;
Packit 971217
Packit 971217
static gboolean
Packit 971217
message_handler (GstBus * bus, GstMessage * msg, gpointer data)
Packit 971217
{
Packit 971217
  GMainLoop *loop = (GMainLoop *) data;
Packit 971217
Packit 971217
  switch (GST_MESSAGE_TYPE (msg)) {
Packit 971217
    case GST_MESSAGE_EOS:
Packit 971217
      got_eos = TRUE;
Packit 971217
      g_main_loop_quit (loop);
Packit 971217
      break;
Packit 971217
    case GST_MESSAGE_ERROR:{
Packit 971217
      gchar *debug;
Packit 971217
      GError *err;
Packit 971217
Packit 971217
      gst_message_parse_error (msg, &err, &debug);
Packit 971217
      g_free (debug);
Packit 971217
Packit 971217
      /* Will abort the check */
Packit 971217
      g_warning ("Error: %s\n", err->message);
Packit 971217
      g_error_free (err);
Packit 971217
Packit 971217
      g_main_loop_quit (loop);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_memory_stream)
Packit 971217
{
Packit 971217
  GMainLoop *loop;
Packit 971217
  GstElement *bin;
Packit 971217
  GstElement *src, *sink;
Packit 971217
  GstBus *bus;
Packit 971217
  GMemoryInputStream *input;
Packit 971217
  GMemoryOutputStream *output;
Packit 971217
  guint8 *in_data;
Packit 971217
  guint8 *out_data;
Packit 971217
  gint i;
Packit 971217
  gint64 duration;
Packit 971217
  guint bus_watch = 0;
Packit 971217
Packit 971217
  got_eos = FALSE;
Packit 971217
Packit 971217
  in_data = g_new (guint8, 512);
Packit 971217
  out_data = g_new (guint8, 512);
Packit 971217
  for (i = 0; i < 512; i++)
Packit 971217
    in_data[i] = i % 256;
Packit 971217
Packit 971217
  input =
Packit 971217
      G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
Packit 971217
          (GDestroyNotify) g_free));
Packit 971217
Packit 971217
  output = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (out_data, 512,
Packit 971217
          (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
Packit 971217
  out_data = NULL;
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, FALSE);
Packit 971217
Packit 971217
  bin = gst_pipeline_new ("bin");
Packit 971217
Packit 971217
  src = gst_element_factory_make ("giostreamsrc", "src");
Packit 971217
  fail_unless (src != NULL);
Packit 971217
  g_object_set (G_OBJECT (src), "stream", input, NULL);
Packit 971217
Packit 971217
  sink = gst_element_factory_make ("giostreamsink", "sink");
Packit 971217
  fail_unless (sink != NULL);
Packit 971217
  g_object_set (G_OBJECT (sink), "stream", output, NULL);
Packit 971217
Packit 971217
  gst_bin_add_many (GST_BIN (bin), src, sink, NULL);
Packit 971217
Packit 971217
  fail_unless (gst_element_link_many (src, sink, NULL));
Packit 971217
Packit 971217
  bus = gst_element_get_bus (bin);
Packit 971217
  bus_watch = gst_bus_add_watch (bus, message_handler, loop);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_PAUSED);
Packit 971217
  gst_element_get_state (bin, NULL, NULL, -1);
Packit 971217
Packit 971217
  fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
Packit 971217
  fail_unless_equals_int (duration, 512);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_NULL);
Packit 971217
Packit 971217
  fail_unless (got_eos);
Packit 971217
  got_eos = FALSE;
Packit 971217
Packit 971217
  out_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output));
Packit 971217
Packit 971217
  for (i = 0; i < 512; i++)
Packit 971217
    fail_unless_equals_int (in_data[i], out_data[i]);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_PAUSED);
Packit 971217
  gst_element_get_state (bin, NULL, NULL, -1);
Packit 971217
Packit 971217
  fail_unless (gst_element_query_duration (bin, GST_FORMAT_BYTES, &duration));
Packit 971217
  fail_unless_equals_int (duration, 512);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  gst_element_set_state (bin, GST_STATE_NULL);
Packit 971217
  gst_object_unref (bin);
Packit 971217
Packit 971217
  fail_unless (got_eos);
Packit 971217
Packit 971217
  g_object_unref (input);
Packit 971217
  g_object_unref (output);
Packit 971217
Packit 971217
  g_main_loop_unref (loop);
Packit 971217
  g_source_remove (bus_watch);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
static Suite *
Packit 971217
gio_suite (void)
Packit 971217
{
Packit 971217
  Suite *s = suite_create ("gio");
Packit 971217
  TCase *tc_chain = tcase_create ("general");
Packit 971217
Packit 971217
  suite_add_tcase (s, tc_chain);
Packit 971217
  tcase_add_test (tc_chain, test_memory_stream);
Packit 971217
Packit 971217
  return s;
Packit 971217
}
Packit 971217
Packit 971217
GST_CHECK_MAIN (gio);