Blame tests/check/libs/flowcombiner.c

Packit f546b1
/* GStreamer
Packit f546b1
 *
Packit f546b1
 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
Packit f546b1
 *   Author: Thiago Santos <ts.santos@sisa.samsung.com>
Packit f546b1
 *
Packit f546b1
 * flowcombiner.c: Unit test for GstFlowCombiner
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
#ifdef HAVE_CONFIG_H
Packit f546b1
#include "config.h"
Packit f546b1
#endif
Packit f546b1
Packit f546b1
#include <gst/check/gstcheck.h>
Packit f546b1
#include <gst/base/gstflowcombiner.h>
Packit f546b1
Packit f546b1
static GstFlowReturn sink_flowret = GST_FLOW_OK;
Packit f546b1
Packit f546b1
#define CHECK_COMBINED_FLOWS(f1, f2, f3, expected) \
Packit f546b1
G_STMT_START { \
Packit f546b1
  combiner = gst_flow_combiner_new (); \
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad1); \
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad2); \
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad3); \
Packit f546b1
  sink_flowret = f1; \
Packit f546b1
  gst_pad_push (pad1, gst_buffer_new ()); \
Packit f546b1
  gst_flow_combiner_update_flow (combiner, f1); \
Packit f546b1
  sink_flowret = f2; \
Packit f546b1
  gst_pad_push (pad2, gst_buffer_new ()); \
Packit f546b1
  gst_flow_combiner_update_flow (combiner, f2); \
Packit f546b1
  sink_flowret = f3; \
Packit f546b1
  gst_pad_push (pad3, gst_buffer_new ()); \
Packit f546b1
  ret = gst_flow_combiner_update_flow (combiner, f3); \
Packit f546b1
  gst_flow_combiner_free (combiner); \
Packit f546b1
  fail_unless_equals_int (ret, expected); \
Packit f546b1
} G_STMT_END
Packit f546b1
Packit f546b1
static GstFlowReturn
Packit f546b1
_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
Packit f546b1
{
Packit f546b1
  gst_buffer_unref (buf);
Packit f546b1
  return sink_flowret;
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_START_TEST (test_combined_flows)
Packit f546b1
{
Packit f546b1
  GstFlowReturn ret;
Packit f546b1
  GstFlowCombiner *combiner;
Packit f546b1
  GstPad *pad1, *pad2, *pad3;
Packit f546b1
  GstPad *peer1, *peer2, *peer3;
Packit f546b1
  GstSegment segment;
Packit f546b1
Packit f546b1
  pad1 = gst_pad_new ("src1", GST_PAD_SRC);
Packit f546b1
  pad2 = gst_pad_new ("src2", GST_PAD_SRC);
Packit f546b1
  pad3 = gst_pad_new ("src3", GST_PAD_SRC);
Packit f546b1
Packit f546b1
  peer1 = gst_pad_new ("sink1", GST_PAD_SINK);
Packit f546b1
  peer2 = gst_pad_new ("sink2", GST_PAD_SINK);
Packit f546b1
  peer3 = gst_pad_new ("sink3", GST_PAD_SINK);
Packit f546b1
Packit f546b1
  gst_pad_set_chain_function (peer1, _sink_chain);
Packit f546b1
  gst_pad_set_chain_function (peer2, _sink_chain);
Packit f546b1
  gst_pad_set_chain_function (peer3, _sink_chain);
Packit f546b1
Packit f546b1
  gst_pad_link (pad1, peer1);
Packit f546b1
  gst_pad_link (pad2, peer2);
Packit f546b1
  gst_pad_link (pad3, peer3);
Packit f546b1
Packit f546b1
  gst_pad_set_active (peer1, TRUE);
Packit f546b1
  gst_pad_set_active (peer2, TRUE);
Packit f546b1
  gst_pad_set_active (peer3, TRUE);
Packit f546b1
  gst_pad_set_active (pad1, TRUE);
Packit f546b1
  gst_pad_set_active (pad2, TRUE);
Packit f546b1
  gst_pad_set_active (pad3, TRUE);
Packit f546b1
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit f546b1
  gst_pad_push_event (pad1, gst_event_new_stream_start ("p1"));
Packit f546b1
  gst_pad_push_event (pad2, gst_event_new_stream_start ("p2"));
Packit f546b1
  gst_pad_push_event (pad3, gst_event_new_stream_start ("p3"));
Packit f546b1
  gst_pad_push_event (pad1, gst_event_new_segment (&segment));
Packit f546b1
  gst_pad_push_event (pad2, gst_event_new_segment (&segment));
Packit f546b1
  gst_pad_push_event (pad3, gst_event_new_segment (&segment));
Packit f546b1
Packit f546b1
  /* ok */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_OK);
Packit f546b1
Packit f546b1
  /* not linked */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_NOT_LINKED, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_OK);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_EOS, GST_FLOW_OK, GST_FLOW_OK);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_NOT_LINKED, GST_FLOW_NOT_LINKED,
Packit f546b1
      GST_FLOW_OK);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_NOT_LINKED, GST_FLOW_NOT_LINKED,
Packit f546b1
      GST_FLOW_NOT_LINKED, GST_FLOW_NOT_LINKED);
Packit f546b1
Packit f546b1
  /* errors */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_ERROR, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_ERROR);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_CUSTOM_ERROR, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_CUSTOM_ERROR);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_NOT_NEGOTIATED, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_NOT_NEGOTIATED);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_NOT_NEGOTIATED,
Packit f546b1
      GST_FLOW_NOT_NEGOTIATED);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_NOT_LINKED, GST_FLOW_ERROR, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_ERROR);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_ERROR,
Packit f546b1
      GST_FLOW_ERROR);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_CUSTOM_ERROR,
Packit f546b1
      GST_FLOW_CUSTOM_ERROR);
Packit f546b1
Packit f546b1
  /* flushing */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_OK, GST_FLOW_FLUSHING,
Packit f546b1
      GST_FLOW_FLUSHING);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_FLUSHING, GST_FLOW_OK,
Packit f546b1
      GST_FLOW_FLUSHING);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_FLUSHING, GST_FLOW_FLUSHING, GST_FLOW_FLUSHING,
Packit f546b1
      GST_FLOW_FLUSHING);
Packit f546b1
Packit f546b1
  /* eos */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_OK, GST_FLOW_NOT_LINKED, GST_FLOW_EOS,
Packit f546b1
      GST_FLOW_OK);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_EOS, GST_FLOW_OK, GST_FLOW_EOS, GST_FLOW_OK);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_EOS, GST_FLOW_EOS, GST_FLOW_EOS, GST_FLOW_EOS);
Packit f546b1
Packit f546b1
  /* eos + not-linked */
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_NOT_LINKED, GST_FLOW_EOS, GST_FLOW_EOS,
Packit f546b1
      GST_FLOW_EOS);
Packit f546b1
  CHECK_COMBINED_FLOWS (GST_FLOW_NOT_LINKED, GST_FLOW_NOT_LINKED, GST_FLOW_EOS,
Packit f546b1
      GST_FLOW_EOS);
Packit f546b1
Packit f546b1
  gst_object_unref (pad1);
Packit f546b1
  gst_object_unref (pad2);
Packit f546b1
  gst_object_unref (pad3);
Packit f546b1
  gst_object_unref (peer1);
Packit f546b1
  gst_object_unref (peer2);
Packit f546b1
  gst_object_unref (peer3);
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_END_TEST;
Packit f546b1
Packit f546b1
GST_START_TEST (test_clear)
Packit f546b1
{
Packit f546b1
  GstFlowCombiner *combiner;
Packit f546b1
  GstPad *pad;
Packit f546b1
  GstPad *peer;
Packit f546b1
  GstSegment segment;
Packit f546b1
  GstFlowReturn ret;
Packit f546b1
Packit f546b1
  combiner = gst_flow_combiner_new ();
Packit f546b1
Packit f546b1
  /* add a pad and make it return _FLUSHING */
Packit f546b1
  pad = gst_pad_new ("src1", GST_PAD_SRC);
Packit f546b1
  peer = gst_pad_new ("sink1", GST_PAD_SINK);
Packit f546b1
  gst_pad_set_chain_function (peer, _sink_chain);
Packit f546b1
  gst_pad_link (pad, peer);
Packit f546b1
  gst_pad_set_active (peer, TRUE);
Packit f546b1
  gst_pad_set_active (pad, TRUE);
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_stream_start ("test1"));
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_segment (&segment));
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad);
Packit f546b1
  sink_flowret = GST_FLOW_FLUSHING;
Packit f546b1
  fail_unless_equals_int (gst_pad_push (pad, gst_buffer_new ()),
Packit f546b1
      GST_FLOW_FLUSHING);
Packit f546b1
Packit f546b1
  /* the combined flow is _FLUSHING */
Packit f546b1
  ret = gst_flow_combiner_update_flow (combiner, GST_FLOW_FLUSHING);
Packit f546b1
  fail_unless_equals_int (ret, GST_FLOW_FLUSHING);
Packit f546b1
  gst_object_unref (pad);
Packit f546b1
  gst_object_unref (peer);
Packit f546b1
Packit f546b1
  /* add one more pad and make it return _OK */
Packit f546b1
  pad = gst_pad_new ("src2", GST_PAD_SRC);
Packit f546b1
  peer = gst_pad_new ("sink2", GST_PAD_SINK);
Packit f546b1
  gst_pad_set_chain_function (peer, _sink_chain);
Packit f546b1
  gst_pad_link (pad, peer);
Packit f546b1
  gst_pad_set_active (peer, TRUE);
Packit f546b1
  gst_pad_set_active (pad, TRUE);
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_stream_start ("test2"));
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_segment (&segment));
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad);
Packit f546b1
  sink_flowret = GST_FLOW_OK;
Packit f546b1
  fail_unless_equals_int (gst_pad_push (pad, gst_buffer_new ()), GST_FLOW_OK);
Packit f546b1
Packit f546b1
  /* the combined flow is _FLUSHING because of the first pad */
Packit f546b1
  ret = gst_flow_combiner_update_flow (combiner, GST_FLOW_OK);
Packit f546b1
  fail_unless_equals_int (ret, GST_FLOW_FLUSHING);
Packit f546b1
  gst_object_unref (pad);
Packit f546b1
  gst_object_unref (peer);
Packit f546b1
Packit f546b1
  /* clear the combiner */
Packit f546b1
  gst_flow_combiner_clear (combiner);
Packit f546b1
Packit f546b1
  /* add a pad and make it return _OK */
Packit f546b1
  pad = gst_pad_new ("src3", GST_PAD_SRC);
Packit f546b1
  peer = gst_pad_new ("sink3", GST_PAD_SINK);
Packit f546b1
  gst_pad_set_chain_function (peer, _sink_chain);
Packit f546b1
  gst_pad_link (pad, peer);
Packit f546b1
  gst_pad_set_active (peer, TRUE);
Packit f546b1
  gst_pad_set_active (pad, TRUE);
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_stream_start ("test3"));
Packit f546b1
  gst_pad_push_event (pad, gst_event_new_segment (&segment));
Packit f546b1
  gst_flow_combiner_add_pad (combiner, pad);
Packit f546b1
  sink_flowret = GST_FLOW_OK;
Packit f546b1
  fail_unless_equals_int (gst_pad_push (pad, gst_buffer_new ()), GST_FLOW_OK);
Packit f546b1
Packit f546b1
  /* the combined flow is _OK since the other pads have been removed */
Packit f546b1
  ret = gst_flow_combiner_update_flow (combiner, GST_FLOW_OK);
Packit f546b1
  fail_unless_equals_int (ret, GST_FLOW_OK);
Packit f546b1
  gst_object_unref (pad);
Packit f546b1
  gst_object_unref (peer);
Packit f546b1
Packit f546b1
  gst_flow_combiner_free (combiner);
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_END_TEST;
Packit f546b1
Packit f546b1
static Suite *
Packit f546b1
flow_combiner_suite (void)
Packit f546b1
{
Packit f546b1
  Suite *s = suite_create ("GstFlowCombiner");
Packit f546b1
  TCase *tc_chain = tcase_create ("general");
Packit f546b1
Packit f546b1
  suite_add_tcase (s, tc_chain);
Packit f546b1
  tcase_add_test (tc_chain, test_combined_flows);
Packit f546b1
  tcase_add_test (tc_chain, test_clear);
Packit f546b1
Packit f546b1
  return s;
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_CHECK_MAIN (flow_combiner);