summaryrefslogtreecommitdiff
path: root/tests/old/examples/autoplug/autoplug.c
blob: 1f2c85c7944916d4eb5286be10ac2ce8d22d86e1 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <gst/gst.h>
#include <gnome.h>

static void
autoplug_have_size (GstElement *element, guint width, guint height,
                   GtkWidget *socket)
{
  gtk_widget_set_usize(socket,width,height);
}

static void
gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
{
  GstElement *osssink, *videosink, *videoelement, *colorspace;
  GtkWidget *appwindow;
  GstElement *new_element;
  GstAutoplug *autoplug;
  GtkWidget *socket;
  GstElement *autobin;
  GstElement *filesrc;
  GstElement *cache;

  GST_DEBUG (0,"GstPipeline: play have type\n");

  gst_element_set_state (pipeline, GST_STATE_PAUSED);

  filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
  autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
  cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");

  // disconnect the typefind from the pipeline and remove it
  gst_element_disconnect (cache, "src", typefind, "sink");
  gst_bin_remove (GST_BIN (autobin), typefind);
      
  /* and an audio sink */
  osssink = gst_elementfactory_make("osssink", "play_audio");
  g_assert(osssink != NULL);

  videosink = gst_bin_new ("videosink");
  /* and an video sink */
  videoelement = gst_elementfactory_make("xvideosink", "play_video");
  //videosink = gst_elementfactory_make("aasink", "play_video");
  g_assert(videosink != NULL);

  colorspace = gst_elementfactory_make("colorspace", "colorspace");
  g_assert(colorspace != NULL);

  gst_element_connect (colorspace, "src", videoelement, "sink");
  gst_bin_add (GST_BIN (videosink), colorspace);
  gst_bin_add (GST_BIN (videosink), videoelement);

  gst_element_add_ghost_pad (videosink, 
		  gst_element_get_pad (colorspace, "sink"), "sink");

  autoplug = gst_autoplugfactory_make ("staticrender");
  g_assert (autoplug != NULL);

  new_element = gst_autoplug_to_renderers (autoplug,
           caps,
           videosink,
           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);

  gtk_object_set (GTK_OBJECT (cache), "reset", TRUE, NULL);

  gst_element_connect (cache, "src", new_element, "sink");

  appwindow = gnome_app_new ("autoplug demo","autoplug demo");

  socket = gtk_socket_new ();
  gtk_widget_show (socket);

  gnome_app_set_contents (GNOME_APP (appwindow),
    		GTK_WIDGET (socket));

  gtk_widget_realize (socket);
  gtk_socket_steal (GTK_SOCKET (socket), 
		    gst_util_get_int_arg (GTK_OBJECT (videoelement), "xid"));
  gtk_widget_set_usize(socket,320,240);

  gtk_widget_show_all (appwindow);

  gtk_signal_connect (GTK_OBJECT (videoelement), "have_size",
                      GTK_SIGNAL_FUNC (autoplug_have_size), socket);

  gst_element_set_state (pipeline, GST_STATE_PLAYING);
      
#ifndef GST_DISABLE_LOADSAVE
  gst_xml_write_file (GST_ELEMENT (pipeline), fopen ("xmlTest.gst", "w"));
#endif
}

gboolean 
idle_func (gpointer data)
{
  return gst_bin_iterate (GST_BIN (data));
}

static void
gst_play_cache_empty (GstElement *element, GstElement *pipeline)
{
  GstElement *autobin;
  GstElement *filesrc;
  GstElement *cache;
  GstElement *new_element;

  fprintf (stderr, "have cache empty\n");

  gst_element_set_state (pipeline, GST_STATE_PAUSED);

  filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
  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");

  gst_element_disconnect (filesrc, "src", cache, "sink");
  gst_element_disconnect (cache, "src", new_element, "sink");
  gst_bin_remove (GST_BIN (autobin), cache);
  gst_element_connect (filesrc, "src", new_element, "sink");

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  fprintf (stderr, "done with cache_empty\n");
}

int main(int argc,char *argv[]) 
{
  GstElement *filesrc;
  GstElement *pipeline;
  GstElement *autobin;
  GstElement *typefind;
  GstElement *cache;

  g_thread_init(NULL);
  gst_init(&argc,&argv);
  gnome_init("autoplug","0.0.1", argc,argv);

  if (argc != 2) {
    g_print("usage: %s <filename>\n", argv[0]);
    exit(-1);
  }

  /* create a new pipeline to hold the elements */
  pipeline = gst_pipeline_new("pipeline");
  g_assert(pipeline != NULL);

  /* create a disk reader */
  filesrc = gst_elementfactory_make("filesrc", "disk_source");
  g_assert(filesrc != NULL);
  gtk_object_set(GTK_OBJECT(filesrc),"location", argv[1],NULL);
  gst_bin_add (GST_BIN (pipeline), filesrc);

  autobin = gst_bin_new ("autobin");
  cache = gst_elementfactory_make ("autoplugcache", "cache");
  gtk_signal_connect (GTK_OBJECT (cache), "cache_empty", GTK_SIGNAL_FUNC (gst_play_cache_empty), pipeline);

  typefind = gst_elementfactory_make ("typefind", "typefind");
  gtk_signal_connect (GTK_OBJECT (typefind), "have_type", GTK_SIGNAL_FUNC (gst_play_have_type), pipeline);
  gst_bin_add (GST_BIN (autobin), cache);
  gst_bin_add (GST_BIN (autobin), typefind);

  gst_element_connect (cache, "src", typefind, "sink");
  gst_element_add_ghost_pad (autobin, gst_element_get_pad (cache, "sink"), "sink");

  gst_bin_add (GST_BIN( pipeline), autobin);
  gst_element_connect (filesrc, "src", autobin, "sink");

  /* start playing */
  gst_element_set_state( GST_ELEMENT (pipeline), GST_STATE_PLAYING);

  gtk_idle_add (idle_func, pipeline);
  gst_main ();

  /* stop the pipeline */
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);

  gst_object_unref (GST_OBJECT (pipeline));

  exit(0);
}