Blame tests/check/elements/concat.c

Packit f546b1
/* GStreamer unit tests for concat
Packit f546b1
 *
Packit f546b1
 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
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 Lesser General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2.1 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
 * Lesser General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Lesser General Public
Packit f546b1
 * License along with this library; if not, write to the Free Software
Packit f546b1
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
Packit f546b1
*/
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
Packit f546b1
#define N_BUFFERS 10
Packit f546b1
static gboolean got_eos;
Packit f546b1
static guint buffer_count;
Packit f546b1
static GstSegment current_segment;
Packit f546b1
static guint64 current_bytes;
Packit f546b1
Packit f546b1
static GstFlowReturn
Packit f546b1
output_chain_time (GstPad * pad, GstObject * parent, GstBuffer * buffer)
Packit f546b1
{
Packit f546b1
  GstClockTime timestamp;
Packit f546b1
  guint8 b;
Packit f546b1
Packit f546b1
  timestamp = GST_BUFFER_TIMESTAMP (buffer);
Packit f546b1
  fail_unless_equals_int64 (timestamp,
Packit f546b1
      (buffer_count % N_BUFFERS) * 25 * GST_MSECOND);
Packit f546b1
  timestamp =
Packit f546b1
      gst_segment_to_stream_time (&current_segment, GST_FORMAT_TIME, timestamp);
Packit f546b1
  fail_unless_equals_int64 (timestamp,
Packit f546b1
      (buffer_count % N_BUFFERS) * 25 * GST_MSECOND);
Packit f546b1
Packit f546b1
  timestamp = GST_BUFFER_TIMESTAMP (buffer);
Packit f546b1
  timestamp =
Packit f546b1
      gst_segment_to_running_time (&current_segment, GST_FORMAT_TIME,
Packit f546b1
      timestamp);
Packit f546b1
  fail_unless_equals_int64 (timestamp, buffer_count * 25 * GST_MSECOND);
Packit f546b1
Packit f546b1
  gst_buffer_extract (buffer, 0, &b, 1);
Packit f546b1
  fail_unless_equals_int (b, buffer_count % N_BUFFERS);
Packit f546b1
Packit f546b1
  buffer_count++;
Packit f546b1
  gst_buffer_unref (buffer);
Packit f546b1
  return GST_FLOW_OK;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
output_event_time (GstPad * pad, GstObject * parent, GstEvent * event)
Packit f546b1
{
Packit f546b1
  switch (GST_EVENT_TYPE (event)) {
Packit f546b1
    case GST_EVENT_FLUSH_STOP:
Packit f546b1
      gst_segment_init (&current_segment, GST_FORMAT_UNDEFINED);
Packit f546b1
      break;
Packit f546b1
    case GST_EVENT_SEGMENT:
Packit f546b1
      gst_event_copy_segment (event, &current_segment);
Packit f546b1
      break;
Packit f546b1
    case GST_EVENT_EOS:
Packit f546b1
      got_eos = TRUE;
Packit f546b1
      break;
Packit f546b1
    default:
Packit f546b1
      break;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  gst_event_unref (event);
Packit f546b1
  return TRUE;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gpointer
Packit f546b1
push_buffers_time (gpointer data)
Packit f546b1
{
Packit f546b1
  GstSegment segment;
Packit f546b1
  GstPad *pad = data;
Packit f546b1
  gint i;
Packit f546b1
  GstClockTime timestamp = 0;
Packit f546b1
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_stream_start ("test"));
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_TIME);
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_segment (&segment));
Packit f546b1
Packit f546b1
  for (i = 0; i < N_BUFFERS; i++) {
Packit f546b1
    GstBuffer *buf = gst_buffer_new_and_alloc (1000);
Packit f546b1
Packit f546b1
    gst_buffer_memset (buf, 0, i, 1);
Packit f546b1
Packit f546b1
    GST_BUFFER_TIMESTAMP (buf) = timestamp;
Packit f546b1
    timestamp += 25 * GST_MSECOND;
Packit f546b1
    GST_BUFFER_DURATION (buf) = timestamp - GST_BUFFER_TIMESTAMP (buf);
Packit f546b1
Packit f546b1
    fail_unless (gst_pad_chain (pad, buf) == GST_FLOW_OK);
Packit f546b1
  }
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_eos ());
Packit f546b1
Packit f546b1
  return NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_START_TEST (test_concat_simple_time)
Packit f546b1
{
Packit f546b1
  GstElement *concat;
Packit f546b1
  GstPad *sink1, *sink2, *sink3, *src, *output_sink;
Packit f546b1
  GThread *thread1, *thread2, *thread3;
Packit f546b1
Packit f546b1
  got_eos = FALSE;
Packit f546b1
  buffer_count = 0;
Packit f546b1
  gst_segment_init (&current_segment, GST_FORMAT_UNDEFINED);
Packit f546b1
Packit f546b1
  concat = gst_element_factory_make ("concat", NULL);
Packit f546b1
  fail_unless (concat != NULL);
Packit f546b1
Packit f546b1
  sink1 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink1 != NULL);
Packit f546b1
Packit f546b1
  sink2 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink2 != NULL);
Packit f546b1
Packit f546b1
  sink3 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink3 != NULL);
Packit f546b1
Packit f546b1
  src = gst_element_get_static_pad (concat, "src");
Packit f546b1
  output_sink = gst_pad_new ("sink", GST_PAD_SINK);
Packit f546b1
  fail_unless (output_sink != NULL);
Packit f546b1
  fail_unless (gst_pad_link (src, output_sink) == GST_PAD_LINK_OK);
Packit f546b1
Packit f546b1
  gst_pad_set_chain_function (output_sink, output_chain_time);
Packit f546b1
  gst_pad_set_event_function (output_sink, output_event_time);
Packit f546b1
Packit f546b1
  gst_pad_set_active (output_sink, TRUE);
Packit f546b1
  fail_unless (gst_element_set_state (concat,
Packit f546b1
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
Packit f546b1
Packit f546b1
  thread1 = g_thread_new ("thread1", (GThreadFunc) push_buffers_time, sink1);
Packit f546b1
  thread2 = g_thread_new ("thread2", (GThreadFunc) push_buffers_time, sink2);
Packit f546b1
  thread3 = g_thread_new ("thread3", (GThreadFunc) push_buffers_time, sink3);
Packit f546b1
Packit f546b1
  g_thread_join (thread1);
Packit f546b1
  g_thread_join (thread2);
Packit f546b1
  g_thread_join (thread3);
Packit f546b1
Packit f546b1
  fail_unless (got_eos);
Packit f546b1
  fail_unless_equals_int (buffer_count, 3 * N_BUFFERS);
Packit f546b1
Packit f546b1
  gst_element_set_state (concat, GST_STATE_NULL);
Packit f546b1
  gst_pad_unlink (src, output_sink);
Packit f546b1
  gst_object_unref (src);
Packit f546b1
  gst_element_release_request_pad (concat, sink1);
Packit f546b1
  gst_object_unref (sink1);
Packit f546b1
  gst_element_release_request_pad (concat, sink2);
Packit f546b1
  gst_object_unref (sink2);
Packit f546b1
  gst_element_release_request_pad (concat, sink3);
Packit f546b1
  gst_object_unref (sink3);
Packit f546b1
  gst_pad_set_active (output_sink, FALSE);
Packit f546b1
  gst_object_unref (output_sink);
Packit f546b1
  gst_object_unref (concat);
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_END_TEST;
Packit f546b1
Packit f546b1
static GstFlowReturn
Packit f546b1
output_chain_bytes (GstPad * pad, GstObject * parent, GstBuffer * buffer)
Packit f546b1
{
Packit f546b1
  guint8 b;
Packit f546b1
Packit f546b1
  fail_unless (current_bytes >= current_segment.start);
Packit f546b1
  fail_unless_equals_int64 (current_segment.start,
Packit f546b1
      (buffer_count / N_BUFFERS) * 1000 * N_BUFFERS);
Packit f546b1
Packit f546b1
  gst_buffer_extract (buffer, 0, &b, 1);
Packit f546b1
  fail_unless_equals_int (b, buffer_count % N_BUFFERS);
Packit f546b1
Packit f546b1
  current_bytes += gst_buffer_get_size (buffer), buffer_count++;
Packit f546b1
  gst_buffer_unref (buffer);
Packit f546b1
  return GST_FLOW_OK;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
output_event_bytes (GstPad * pad, GstObject * parent, GstEvent * event)
Packit f546b1
{
Packit f546b1
  switch (GST_EVENT_TYPE (event)) {
Packit f546b1
    case GST_EVENT_FLUSH_STOP:
Packit f546b1
      gst_segment_init (&current_segment, GST_FORMAT_UNDEFINED);
Packit f546b1
      break;
Packit f546b1
    case GST_EVENT_SEGMENT:
Packit f546b1
      gst_event_copy_segment (event, &current_segment);
Packit f546b1
      break;
Packit f546b1
    case GST_EVENT_EOS:
Packit f546b1
      got_eos = TRUE;
Packit f546b1
      break;
Packit f546b1
    default:
Packit f546b1
      break;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  gst_event_unref (event);
Packit f546b1
  return TRUE;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gpointer
Packit f546b1
push_buffers_bytes (gpointer data)
Packit f546b1
{
Packit f546b1
  GstSegment segment;
Packit f546b1
  GstPad *pad = data;
Packit f546b1
  gint i;
Packit f546b1
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_stream_start ("test"));
Packit f546b1
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_segment (&segment));
Packit f546b1
Packit f546b1
  for (i = 0; i < N_BUFFERS; i++) {
Packit f546b1
    GstBuffer *buf = gst_buffer_new_and_alloc (1000);
Packit f546b1
    gst_buffer_memset (buf, 0, i, 1);
Packit f546b1
Packit f546b1
    fail_unless (gst_pad_chain (pad, buf) == GST_FLOW_OK);
Packit f546b1
  }
Packit f546b1
  gst_pad_send_event (pad, gst_event_new_eos ());
Packit f546b1
Packit f546b1
  return NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_START_TEST (test_concat_simple_bytes)
Packit f546b1
{
Packit f546b1
  GstElement *concat;
Packit f546b1
  GstPad *sink1, *sink2, *sink3, *src, *output_sink;
Packit f546b1
  GThread *thread1, *thread2, *thread3;
Packit f546b1
Packit f546b1
  got_eos = FALSE;
Packit f546b1
  buffer_count = 0;
Packit f546b1
  current_bytes = 0;
Packit f546b1
  gst_segment_init (&current_segment, GST_FORMAT_UNDEFINED);
Packit f546b1
Packit f546b1
  concat = gst_element_factory_make ("concat", NULL);
Packit f546b1
  fail_unless (concat != NULL);
Packit f546b1
Packit f546b1
  sink1 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink1 != NULL);
Packit f546b1
Packit f546b1
  sink2 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink2 != NULL);
Packit f546b1
Packit f546b1
  sink3 = gst_element_get_request_pad (concat, "sink_%u");
Packit f546b1
  fail_unless (sink3 != NULL);
Packit f546b1
Packit f546b1
  src = gst_element_get_static_pad (concat, "src");
Packit f546b1
  output_sink = gst_pad_new ("sink", GST_PAD_SINK);
Packit f546b1
  fail_unless (output_sink != NULL);
Packit f546b1
  fail_unless (gst_pad_link (src, output_sink) == GST_PAD_LINK_OK);
Packit f546b1
Packit f546b1
  gst_pad_set_chain_function (output_sink, output_chain_bytes);
Packit f546b1
  gst_pad_set_event_function (output_sink, output_event_bytes);
Packit f546b1
Packit f546b1
  gst_pad_set_active (output_sink, TRUE);
Packit f546b1
  fail_unless (gst_element_set_state (concat,
Packit f546b1
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
Packit f546b1
Packit f546b1
  thread1 = g_thread_new ("thread1", (GThreadFunc) push_buffers_bytes, sink1);
Packit f546b1
  thread2 = g_thread_new ("thread2", (GThreadFunc) push_buffers_bytes, sink2);
Packit f546b1
  thread3 = g_thread_new ("thread3", (GThreadFunc) push_buffers_bytes, sink3);
Packit f546b1
Packit f546b1
  g_thread_join (thread1);
Packit f546b1
  g_thread_join (thread2);
Packit f546b1
  g_thread_join (thread3);
Packit f546b1
Packit f546b1
  fail_unless (got_eos);
Packit f546b1
  fail_unless_equals_int (buffer_count, 3 * N_BUFFERS);
Packit f546b1
  fail_unless_equals_int64 (current_bytes, 3 * N_BUFFERS * 1000);
Packit f546b1
Packit f546b1
  gst_element_set_state (concat, GST_STATE_NULL);
Packit f546b1
  gst_pad_unlink (src, output_sink);
Packit f546b1
  gst_object_unref (src);
Packit f546b1
  gst_element_release_request_pad (concat, sink1);
Packit f546b1
  gst_object_unref (sink1);
Packit f546b1
  gst_element_release_request_pad (concat, sink2);
Packit f546b1
  gst_object_unref (sink2);
Packit f546b1
  gst_element_release_request_pad (concat, sink3);
Packit f546b1
  gst_object_unref (sink3);
Packit f546b1
  gst_pad_set_active (output_sink, FALSE);
Packit f546b1
  gst_object_unref (output_sink);
Packit f546b1
  gst_object_unref (concat);
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_END_TEST;
Packit f546b1
Packit f546b1
static Suite *
Packit f546b1
concat_suite (void)
Packit f546b1
{
Packit f546b1
  Suite *s = suite_create ("concat");
Packit f546b1
  TCase *tc_chain;
Packit f546b1
Packit f546b1
  tc_chain = tcase_create ("concat");
Packit f546b1
  tcase_add_test (tc_chain, test_concat_simple_time);
Packit f546b1
  tcase_add_test (tc_chain, test_concat_simple_bytes);
Packit f546b1
  suite_add_tcase (s, tc_chain);
Packit f546b1
Packit f546b1
  return s;
Packit f546b1
}
Packit f546b1
Packit f546b1
GST_CHECK_MAIN (concat);