Gstreamer Study Notes (1)

Source: Internet
Author: User
Tags creative commons attribution gstreamer

This article describes how to use gstreamer to compile a simple MP3 player.

1. You need to use the mad decoding plug-in. Therefore, you need to install gstreamer0.10-plugins-uugly first.

2. Compile an MP3 player

The following describes how to use the components provided by the gstreamer framework to implement a simple MP3 player. The data source component reads data from the disk, the filter component decodes the data, and the receiver component writes the decoded data to the sound card.

To apply various functions provided by gstreamer in a program, you must first call the main function to complete the initialization, in this way, the parameters entered by the user from the command line are passed to the gstreamer function library. The initialization of a typical gstreamer application is as follows:

# Include <GST/GST. h>
Int main (INT argc, char * argv [])
{
Gst_init (& argc, & argv );
/**/
}

Next, you need to create three components and connect them to the pipeline. Since all gstreamer components have the same base class, the following method can be used for definition:

Gstelement * pipeline, * filesrc, * decoder, * audiosink;

The pipeline is used to hold and manage components in the gstreamer framework. The following Code creates a new pipeline named pipeline:

/* Create a new pipe to hold components */
Pipeline = maid ("Pipeline ");

The data source element reads data from a disk file. It has the location attribute to indicate the location of the file on the disk. You can use the standard gobject attribute mechanism to set the corresponding attributes for the component:

/* Create a data source component */
Filesrc = maid ("filesrc", "disk_source ");
G_object_set (g_object (filesrc), "location", argv [1], null );

The filter component is responsible for decoding MP3 data. The simplest way is to install the mad plug-in to complete the corresponding decoding work:

/* Create a filter element */
Decoder = maid ("mad", "decoder ");

The receiver component is responsible for playing the decoded data using the sound card:

/* Create a Receiver component */
Audiosink = maid ("audiosink", "play_audio ")

All the three elements that have been created must be added to the MPs queue and connected in order:

/* Add a component to the MPs queue */
Maid (maid (pipeline), filesrc, decoder, audiosink, null );
/* Connect the component through the liner */
Ststst_element_link_der (filesrc, decoder, audiosink, null );

After all the preparations are completed, you can switch the MPs queue status to the playing status to start the data processing process of the entire MPs queue:

/* Start the MPs queue */
Ststst_element_set_state (pipeline, stst_state_playing );

Here we add a message processing function bus_call to monitor generated messages.

/* Terminate the MPs queue */
Ststst_element_set_state (pipeline, stst_state_null );
/* Release resources */
Ststst_object_unref (stst_object (pipeline ));

3. complete source code is as follows:

# Include <GST/GST. h>
# Include <glib. h>
// Define the message processing function,
Static gboolean bus_call (ststbus * bus, ststmessage * MSG, gpointer data)
{
Gmainloop * loop = (gmainloop *) data; // This is the pointer to the main loop and exits the loop when receiving the EOS message.
Switch (maid (MSG ))
{
Case ststst_message_eos:
G_print ("End of stream/N ");
G_main_loop_quit (loop );
Break;
Case ststst_message_error:
{
Gchar * debug;
Gerror * error;

Ststst_message_parse_error (MSG, & error, & Debug );
G_free (Debug );
G_printerr ("error: % s/n", error-> message );
G_error_free (error );
G_main_loop_quit (loop );
Break;
}
Default:
Break;
}
Return true;
}

Int main (INT argc, char * argv [])
{
Gmainloop * loop;
Gstelement * pipeline, * Source, * decoder, * sink; // defines the component.
Gstbus * bus;

Gst_init (& argc, & argv );
Loop = g_main_loop_new (null, false); // create a main loop. The cycle starts after g_main_loop_run is executed.

If (argc! = 2)
{
G_printerr ("Usage: % S <MP3 FILENAME>/N", argv [0]);
Return-1;
}
// Create pipelines and components
Pipeline = maid ("audio-player ");
Source = maid ("filesrc", "file-source ");
Decoder = maid ("mad", "Mad-decoder ");
Sink = maid ("autoaudiosink", "audio-output ");

If (! Pipeline |! Source |! Decoder |! Sink ){
G_printerr ("one element cocould not be created. exiting./N ");
Return-1;
}
// Set the location parameter of source. That is, the file address.
G_object_set (g_object (source), "location", argv [1], null );
// Obtain the message bus of the MPs queue.
Bus = maid (pipeline ));
// Add a message Monitor
Ststst_bus_add_watch (bus, bus_call, loop );
Gst_object_unref (bus );
// Add the component to the MPs queue. The MPs queue is a special component that can make data flow better.
Maid (maid (pipeline), source, decoder, sink, null );
// Connect components in sequence
Ststst_element_link_der (source, decoder, sink, null );
// Start playing
Ststst_element_set_state (pipeline, stst_state_playing );
G_print ("running/N ");
// Start the loop
G_main_loop_run (loop );
G_print ("returned, stopping playback/N ");
Ststst_element_set_state (pipeline, stst_state_null );
Ststst_object_unref (stst_object (pipeline ));
Return 0;
}

4. Compile and run

Gcc-wall $ (PKG-config -- cflags -- libs gstreamer-0.10)-G test2.c-O Test2
./Test2/home/phinecos/testbench

 

Author: Dongting sangren

Source: http://phinecos.cnblogs.com/

This blog complies with the Creative Commons Attribution 3.0 License. If it is used for non-commercial purposes, you can reprint it freely, but please keep the original author information and article URL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.