Goal
For a software library, there is no more intuitive first impression than printing a Hello world on the screen. Because we're dealing with a multimedia framework, we're going to play a video instead of Hello World. Don't be intimidated by the code below--it really works on four lines. The rest is the resource management code, C language, is the trouble. Not much to say, prepare your first GStreamer application ...
Hello World
Copy the following code to a text file and rename it to basic-tutorial-1.c
#include <gst/gst.h> int main (int argc, char *argv[]) {gstelement *pipeline;
Gstbus *bus;
Gstmessage *msg;
/* Initialize GStreamer */Gst_init (&ARGC, &ARGV); /* Build the pipeline */pipeline = Gst_parse_launch ("Playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p
. WebM ", NULL);
/* Start playing */Gst_element_set_state (pipeline, gst_state_playing);
/* Wait until Error or EOS */bus = Gst_element_get_bus (pipeline); msg = gst_bus_timed_pop_filtered (bus, Gst_clock_time_none, Gst_message_error |
Gst_message_eos);
/* FREE Resources */if (msg! = NULL) gst_message_unref (msg);
Gst_object_unref (bus);
Gst_element_set_state (pipeline, Gst_state_null);
Gst_object_unref (pipeline);
return 0; }
The compilation aspect is also introduced according to your platform, Linux Platform HTTP://DOCS.GSTREAMER.COM/DISPLAY/GSTSDK/INSTALLING+ON+LINUX,MAC platform/HTTP/ Docs.gstreamer.com/display/gstsdk/installing+on+mac+os+x,windows is http://docs.gstreamer.com/display/GstSDK/. Installing+on+windows. If the compilation error, please check the error place statement, if compiled through, then can run, will pop up a window, play a video on the network. Congratulations, the first step was successful.
Code Analysis
Let's look at the code and analyze the workflow.
/* Initialize GStreamer *
/Gst_init (&ARGC, &ARGV);
This is the first sentence of all GStreamer applications, and it's done in Gst_init.
+ Initialization of all internal data structures
+ Check all available plugins
+ Run all command-line options
If you pass argc and argv into Gst_init, it is advantageous to handle the command line (which is also covered in the GStreamer tool).
/* Build the pipeline *
/pipeline = Gst_parse_launch ("Playbin2 uri=http://docs.gstreamer.com/media/sintel_ TRAILER-480P.WEBM ", NULL);
This line is the most important part of the tutorial, with two very important points: Gst_parse_launch and Playbin2
Gst_parse_launch
GStreamer is a framework designed to handle multimedia streaming. The media stream passes through a series of intermediate element flows from the source element to the sink element. These elements of interaction form an entire pipeline.
When using GStreamer, you often need to use a separate elements to manually build a pipeline, but in a relatively simple case, we can also use Gst_parse_launch (). This function was originally described as a pipeline, but it can also be conveniently used to create a pipeline.
Playbin2
We let the Gst_parse_launch () function set up a kind of pipeline. This is the use of playbin2, and we have created a pipeline that contains only the element of playbin2.
Playbin2 is a special element that is both a source and a sink, and can handle the entire pipeline transaction. Internally, he creates and links all the elements necessary to play your media, and you don't have to worry about it.
This element is less granular than the purely hand-built pipeline, but there are plenty of customizable controls.
In this example, we just parse the playbin2 parameter-the URI we want to play. Try other addresses, such as http://or the uri,playbin2 at the beginning of file://to work well.
If you type the wrong URI, or the URI does not exist, or you miss a plugin, GStreamer provides a notification mechanism, but this example simply exits when an error occurs, so it does not unfold.
/* Start playing *
/gst_element_set_state (pipeline, gst_state_playing);
This line of code shows another point to focus on: state. Each GStreamer element has a status that you can interpret as a common DVD player on the play/pause button. The player must set pipeline to playing status to actually start playing, and this line of code is doing it.
/* Wait until error or EOS *
/bus = Gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, Gst_clock_time_none, Gst_message_error | Gst_message_eos);
These lines are waiting for an error to occur or the stream has been played over. Gst_element_get_bus () will get pipeline bus, and then Gst_bus_timed_pop_filtered () will block until you encounter an error or the stream plays the end. This will continue to be introduced in the next lecture, which is introduced here first.
That's it, GStreamer handles everything, and this example stops at the end of the stream or when the play goes wrong, and, of course, you can use CTRL + C to terminate it at any time.
Clean
We have a few things to do before the app terminates.
/* FREE RESOURCES *
/if (msg! = NULL)
gst_message_unref (msg);
Gst_object_unref (bus);
Gst_element_set_state (pipeline, gst_state_null);
Gst_object_unref (pipeline);
After you have used a function, be sure to review the documentation to determine if you need to release the resources. In this example, gst_bus_timed_pop_filtered () returns a message that needs to be called GST_MESSAGE_UNREF () to release (the next one will continue).
Gst_element_get_bus () Adds a reference to the bus, so you need to call Get_object_unref () to release it. Setting pipeline to NULL causes it to release all resources, and finally, releases the pipeline itself.