summaryrefslogtreecommitdiff
path: root/testsuite/threads/thread.c
blob: 3a1324ec3da652c716747a37c32791b78a460a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <gst/gst.h>

/*
 * FIXME:
 * these tests should have a maximum run length, so that they get killed
 * if they lock up, which they're bound to do.
 */

void
usage (void)
{
  g_print ("compile this test with TESTNUM defined.\n"
      "   available TESTNUMs:   \n"
      "          1: stress test state change      \n"
      "          2: iterate once                  \n"
      "          3: iterate twice                 \n"
      "          4: state change while running    \n"
      "          5: state change in thread context\n");
}

static void
construct_pipeline (GstElement * pipeline)
{
  GstElement *src, *sink, *queue, *identity, *thread;

  src = gst_element_factory_make ("fakesrc", NULL);
  sink = gst_element_factory_make ("fakesink", "sink");
  identity = gst_element_factory_make ("identity", NULL);
  queue = gst_element_factory_make ("queue", NULL);
  thread = gst_element_factory_make ("thread", NULL);

  gst_element_link_many (src, queue, identity, sink, NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, queue, thread, NULL);
  gst_bin_add_many (GST_BIN (thread), identity, sink, NULL);

  g_object_set (G_OBJECT (src), "num_buffers", 5, NULL);
  g_object_set (sink, "signal-handoffs", TRUE, NULL);
}

void
change_state (GstElement * element, GstBuffer * buf, GstElement * pipeline)
{
  gst_element_set_state (pipeline, GST_STATE_NULL);
}

int
main (gint argc, gchar * argv[])
{
  GstElement *pipeline;

  gst_init (&argc, &argv);

#ifndef TESTNUM
  usage ();
  return -1;
#endif

  pipeline = gst_pipeline_new ("main_pipeline");
  construct_pipeline (pipeline);

  if (TESTNUM == 1) {
    g_print ("thread test 1: stress test state changes...\n");

    g_print ("NULL\n");
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_print ("READY\n");
    gst_element_set_state (pipeline, GST_STATE_READY);
    g_print ("NULL\n");
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_print ("PAUSED\n");
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    g_print ("READY\n");
    gst_element_set_state (pipeline, GST_STATE_READY);
    g_print ("PAUSED\n");
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    g_print ("PLAYING\n");
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    /* element likely hits EOS and does a state transition to PAUSED */
    g_print ("READY\n");
    gst_element_set_state (pipeline, GST_STATE_READY);
    g_print ("NULL\n");
    gst_element_set_state (pipeline, GST_STATE_NULL);
  }

  if (TESTNUM == 2 || TESTNUM == 3) {
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_print ("running ...\n");
    while (gst_bin_iterate (GST_BIN (pipeline)));
    gst_element_set_state (pipeline, GST_STATE_NULL);
  }
  if (TESTNUM == 3) {
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_print ("running ...\n");
    while (gst_bin_iterate (GST_BIN (pipeline)));
    gst_element_set_state (pipeline, GST_STATE_NULL);
  }
  if (TESTNUM == 4) {
    gint run;

    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_print ("running ...\n");
    for (run = 0; run < 3; run++) {
      gst_bin_iterate (GST_BIN (pipeline));
    }
    gst_element_set_state (pipeline, GST_STATE_NULL);
  }
  if (TESTNUM == 5) {
    /* I don't think this test is supposed to work */
    GstElement *sink;

    sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
    g_assert (sink);

    g_signal_connect (G_OBJECT (sink), "handoff",
        G_CALLBACK (change_state), pipeline);
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_print ("running ...\n");
    while (gst_bin_iterate (GST_BIN (pipeline)));
    gst_element_set_state (pipeline, GST_STATE_NULL);
  }

  return 0;
}