summaryrefslogtreecommitdiff
path: root/docs/manual/helloworld2.sgml
blob: 1a48f7f991383577bc293ba374c176178fc2ca60 (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
<chapter id="cha-hello2">
  <title>Your second application</title>
  <para> 
    In the previous chapter we created a first version of the helloworld
    application. We then explained a better way of creating the elements
    using factories identified by MIME types.
  </para>
  <para> 
    In this chapter we will introduce you to autoplugging. Using the MIME
    types of the elements <application>GStreamer</application> can automatically create a pipeline
    for you.
  </para>

  <sect1>
    <title>Autoplugging helloworld</title>
    <para> 
      We will create a second version of the helloworld application using
      autoplugging. Its source code is considerably easier to write and
      it can also handle many more data types.
    </para>

    <programlisting>

#include &lt;gst/gst.h&gt;

static gboolean playing;

/* eos will be called when the src element has an end of stream */
void 
eos (GstSrc *src) 
{
  g_print ("have eos, quitting\n");

  playing = FALSE;
}

int 
main (int argc, char *argv[]) 
{
  GstElement *disksrc, *audiosink;
  GstElement *pipeline;

  if (argc != 2) {
    g_print ("usage: &percnt;s &lt;filename&gt;\n", argv[0]);
    exit (-1);
  }

  gst_init (&amp;argc, &amp;argv);

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

  /* create a disk reader */
  disksrc = gst_elementfactory_make ("disksrc", "disk_source");
  gtk_object_set (GTK_OBJECT (disksrc), "location", argv[1], NULL);
  gtk_signal_connect (GTK_OBJECT (disksrc), "eos",
                      GTK_SIGNAL_FUNC (eos), NULL);

  /* and an audio sink */
  audiosink = gst_elementfactory_make ("audiosink", "play_audio");

  /* add objects to the main pipeline */
  gst_pipeline_add_src (GST_PIPELINE (pipeline), disksrc);
  gst_pipeline_add_sink (GST_PIPELINE (pipeline), audiosink);

  if (!gst_pipeline_autoplug (GST_PIPELINE (pipeline))) {
    g_print ("unable to handle stream\n");
    exit (-1);
  }

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

  playing = TRUE;

  while (playing) {
    gst_bin_iterate (GST_BIN (pipeline));
  }

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

  gst_pipeline_destroy (pipeline);

  exit (0);
}

    </programlisting>

    <para>
      First of all, we do not use any mpg123 or mp3parse element in this example.
      In fact, we only specify a source element and a sink element and add them
      to a pipeline.
    </para>

    <para>
      The most interesting change however is the following:
    </para>
    <programlisting>

  ...
  if (!gst_pipeline_autoplug (pipeline)) {
    g_print ("unable to handle stream\n");
    exit (-1);
  }
  ...

    </programlisting>

    <para>
      This piece of code does all the magic.
    </para>

    <para>
      <itemizedlist>
        <listitem>
          <para>
	    The pipeline will try to connect the src and the sink element.
          </para>
        </listitem>
        <listitem>
          <para>
            Since the source has no type, a typedetection will be started on
	    the source element.
          </para>
        </listitem>
        <listitem>
          <para>
	    The best set of elements that connect the MIME type of the source 
	    element to the MIME type of the sink are found.
          </para>
        </listitem>
        <listitem>
          <para>
	    The elements are added to the pipeline and their pads are connected.
          </para>
        </listitem>
      </itemizedlist>
    </para>

    <para>
      After this autoplugging, the pipeline is ready to play. Remember that this
      pipeline will be able to playback all of the media types for which an
      appropriate plugin exists since the autoplugging is all done using MIME
      types.
    </para>

    <para>
      If you really want, you can use the GSteamer components to do the 
      autoplugging yourself. We will cover this topic in the dynamic pipeline chapter.
    </para>

    <para>
      To compile the helloworld2 example, use: 
    </para>
    <programlisting>
       gcc -Wall `gstreamer-config --cflags --libs` helloworld2.c \
             -o helloworld2 
    </programlisting>
    <para>
      You can run the example with (substitute helloworld.mp3 with you favorite MP3 file):
    </para>
    <programlisting>
      ./helloworld2 helloworld.mp3
    </programlisting>
    <para>
      You can also try to use an AVI or MPEG file as its input. Using autoplugging,
      <application>GStreamer</application> will automatically figure out how to 
      handle the stream. Remember that only the audio part will be played because 
      we have only added an audiosink to the pipeline.
    </para>
    <programlisting>
      ./helloworld2 mymovie.mpeg
    </programlisting>

  </sect1>
</chapter>