helloworld2.c 3.86 KB
Newer Older
1 2
#include <gst/gst.h>

Wim Taymans's avatar
Wim Taymans committed
3 4
static void
gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
5
{
Wim Taymans's avatar
Wim Taymans committed
6 7 8 9
  GstElement *osssink;
  GstElement *new_element;
  GstAutoplug *autoplug;
  GstElement *autobin;
10
  GstElement *filesrc;
Wim Taymans's avatar
Wim Taymans committed
11
  GstElement *cache;
12

13
  GST_DEBUG ("GstPipeline: play have type");
Wim Taymans's avatar
Wim Taymans committed
14 15 16

  gst_element_set_state (pipeline, GST_STATE_PAUSED);

17
  filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
Wim Taymans's avatar
Wim Taymans committed
18 19 20
  autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
  cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");

21 22
  /* unlink_pads the typefind from the pipeline and remove it */
  gst_element_unlink_pads (cache, "src", typefind, "sink");
Wim Taymans's avatar
Wim Taymans committed
23 24 25
  gst_bin_remove (GST_BIN (autobin), typefind);
      
  /* and an audio sink */
26
  osssink = gst_element_factory_make("osssink", "play_audio");
Wim Taymans's avatar
Wim Taymans committed
27 28
  g_assert(osssink != NULL);

29
  autoplug = gst_autoplug_factory_make ("staticrender");
Wim Taymans's avatar
Wim Taymans committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  g_assert (autoplug != NULL);

  new_element = gst_autoplug_to_renderers (autoplug,
           caps,
           osssink,
           NULL);

  if (!new_element) {
    g_print ("could not autoplug, no suitable codecs found...\n");
    exit (-1);
  }

  gst_element_set_name (new_element, "new_element");

  gst_bin_add (GST_BIN (autobin), new_element);

  g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);

48
  gst_element_link_pads (cache, "src", new_element, "sink");
Wim Taymans's avatar
Wim Taymans committed
49 50

  gst_element_set_state (pipeline, GST_STATE_PLAYING);
51 52
}

Wim Taymans's avatar
Wim Taymans committed
53 54
static void
gst_play_cache_empty (GstElement *element, GstElement *pipeline)
55
{
Wim Taymans's avatar
Wim Taymans committed
56
  GstElement *autobin;
57
  GstElement *filesrc;
Wim Taymans's avatar
Wim Taymans committed
58 59
  GstElement *cache;
  GstElement *new_element;
60

Wim Taymans's avatar
Wim Taymans committed
61
  fprintf (stderr, "have cache empty\n");
62

Wim Taymans's avatar
Wim Taymans committed
63
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
64

65
  filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
Wim Taymans's avatar
Wim Taymans committed
66 67 68
  autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
  cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
  new_element = gst_bin_get_by_name (GST_BIN (autobin), "new_element");
Wim Taymans's avatar
Wim Taymans committed
69

70 71
  gst_element_unlink_pads (filesrc, "src", cache, "sink");
  gst_element_unlink_pads (cache, "src", new_element, "sink");
Wim Taymans's avatar
Wim Taymans committed
72
  gst_bin_remove (GST_BIN (autobin), cache);
73
  gst_element_link_pads (filesrc, "src", new_element, "sink");
74

Wim Taymans's avatar
Wim Taymans committed
75
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Wim Taymans's avatar
Wim Taymans committed
76

Wim Taymans's avatar
Wim Taymans committed
77 78
  fprintf (stderr, "done with cache_empty\n");
}
79

Wim Taymans's avatar
Wim Taymans committed
80 81 82
int 
main (int argc, char *argv[]) 
{
83
  GstElement *filesrc;
Wim Taymans's avatar
Wim Taymans committed
84 85 86 87
  GstElement *pipeline;
  GstElement *autobin;
  GstElement *typefind;
  GstElement *cache;
88

Wim Taymans's avatar
Wim Taymans committed
89
  gst_init (&argc, &argv);
90

Wim Taymans's avatar
Wim Taymans committed
91 92 93
  if (argc != 2) {
    g_print ("usage: %s <filename>\n", argv[0]);
    exit (-1);
94 95
  }

Wim Taymans's avatar
Wim Taymans committed
96 97 98 99 100
  /* create a new pipeline to hold the elements */
  pipeline = gst_pipeline_new ("pipeline");
  g_assert (pipeline != NULL);

  /* create a disk reader */
101
  filesrc = gst_element_factory_make ("filesrc", "disk_source");
102 103 104
  g_assert (filesrc != NULL);
  g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
  gst_bin_add (GST_BIN (pipeline), filesrc);
Wim Taymans's avatar
Wim Taymans committed
105 106

  autobin = gst_bin_new ("autobin");
107
  cache = gst_element_factory_make ("autoplugcache", "cache");
108 109
  g_signal_connect (G_OBJECT (cache), "cache_empty", 
		     G_CALLBACK (gst_play_cache_empty), pipeline);
Wim Taymans's avatar
Wim Taymans committed
110

111
  typefind = gst_element_factory_make ("typefind", "typefind");
112 113
  g_signal_connect (G_OBJECT (typefind), "have_type", 
		     G_CALLBACK (gst_play_have_type), pipeline);
Wim Taymans's avatar
Wim Taymans committed
114 115 116
  gst_bin_add (GST_BIN (autobin), cache);
  gst_bin_add (GST_BIN (autobin), typefind);

117
  gst_element_link_pads (cache, "src", typefind, "sink");
Wim Taymans's avatar
Wim Taymans committed
118 119 120
  gst_element_add_ghost_pad (autobin, gst_element_get_pad (cache, "sink"), "sink");

  gst_bin_add (GST_BIN( pipeline), autobin);
121
  gst_element_link_pads (filesrc, "src", autobin, "sink");
122

123
  /* start playing */
Wim Taymans's avatar
Wim Taymans committed
124
  gst_element_set_state( GST_ELEMENT (pipeline), GST_STATE_PLAYING);
125

Wim Taymans's avatar
Wim Taymans committed
126
  while (gst_bin_iterate (GST_BIN (pipeline)));
127

Wim Taymans's avatar
Wim Taymans committed
128 129
  /* stop the pipeline */
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
130

Wim Taymans's avatar
Wim Taymans committed
131
  gst_object_unref (GST_OBJECT (pipeline));
132 133 134 135

  exit(0);
}