Last active
October 5, 2022 04:07
-
-
Save xiaoli/31f30c8ad68c986ad039 to your computer and use it in GitHub Desktop.
Simple Video Player for FreeScale IMX53 QSB on Debian Wheezy Without X11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gst/gst.h> | |
#include <glib.h> | |
#include <string.h> | |
// Compile command: | |
// gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) test.c -o test | |
// Gstreamer test: | |
// gst-launch filesrc location=<your video file> typefind=true ! aiurdemux name=demux demux. ! queue max-size-buffers=0 max-size-time=0 ! mfw_vpudecoder ! autovideosink demux. ! queue ! mfw_aacdecoder ! autoaudiosink | |
static GstElement *source, *demuxer, *vdqueue, *adqueue, *vdconvert, *adconvert, *vdsink, *adsink, *decvd, *decad; | |
static gboolean | |
bus_call (GstBus *bus, | |
GstMessage *msg, | |
gpointer data) | |
{ | |
GMainLoop *loop = (GMainLoop *) data; | |
switch (GST_MESSAGE_TYPE (msg)) { | |
case GST_MESSAGE_EOS: | |
g_print ("End of stream\n"); | |
g_main_loop_quit (loop); | |
break; | |
case GST_MESSAGE_ERROR: { | |
gchar *debug; | |
GError *error; | |
gst_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; | |
} | |
//function to link demux and decoder | |
static void pad_added (GstElement* demux, GstPad* pPad, GstElement* decoder) | |
{ | |
/*declare a pad*/ | |
GstPad *sinkpad; | |
/*get the sink pad of the decoder*/ | |
sinkpad = gst_element_get_static_pad (decoder, "sink"); | |
/*link the sink pad of the decoder with the one of the demux*/ | |
gst_pad_link (pPad, sinkpad); | |
/*get rid of the sinkpad object*/ | |
gst_object_unref (sinkpad); | |
} | |
int | |
main (int argc, | |
char *argv[]) | |
{ | |
GMainLoop *loop; | |
GstElement *pipeline; | |
GstBus *bus; | |
gst_init (&argc, &argv); | |
loop = g_main_loop_new (NULL, FALSE); | |
if (argc != 2) { | |
g_printerr ("Usage: %s <Video H264 filename>\n", argv[0]); | |
return -1; | |
} | |
pipeline = gst_pipeline_new ("media-player"); | |
source = gst_element_factory_make ("filesrc","file-source"); | |
demuxer = gst_element_factory_make ("aiurdemux","demuxer"); | |
decvd = gst_element_factory_make ("mfw_vpudecoder", "video-decoder"); | |
decad = gst_element_factory_make ("mfw_aacdecoder", "audio-decoder"); | |
vdsink = gst_element_factory_make ("autovideosink", "video-sink"); | |
vdqueue = gst_element_factory_make ("queue", "video-queue"); | |
adqueue = gst_element_factory_make ("queue", "audio-queue"); | |
vdconvert = gst_element_factory_make ("autovideoconvert", "video-convert"); | |
adconvert = gst_element_factory_make ("audioconvert", "audio-convert"); | |
adsink = gst_element_factory_make ("autoaudiosink", "audio-sink"); | |
// g_object_set (decvd, "codec-type", "std_avc", NULL); | |
if (!pipeline || !source || !demuxer || !decvd || !decad || !vdsink || !vdqueue || !adqueue || !adsink) { | |
g_printerr ("One element could not be created. Exiting.\n"); | |
return -1; | |
} | |
g_object_set (G_OBJECT (source), "location", argv[1], NULL); | |
g_object_set(G_OBJECT (source), "typefind", TRUE, NULL); | |
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); | |
gst_bus_add_watch (bus, bus_call, loop); | |
gst_object_unref (bus); | |
gst_bin_add_many (GST_BIN (pipeline), | |
source, demuxer, decvd, decad, adqueue, vdqueue, adconvert, vdconvert, vdsink, adsink, NULL); | |
/* link source and demux*/ | |
if (!gst_element_link (source, demuxer)) { | |
g_warning ("Failed to link elements source and demux!"); | |
} | |
/*link decoder and videosink*/ | |
if (!gst_element_link_many (vdqueue, decvd, vdsink, NULL)) { | |
g_warning ("Failed to vdqueue, decvd elements!"); | |
} | |
if (!gst_element_link_many (adqueue, decad, adsink, NULL)) { | |
g_warning ("Failed to adqueue, decad elements!"); | |
} | |
/*link demux and decoder | |
Since demux has an output for audio and an output for video some special | |
handling needs to be done by using g_signal_conect function an our callback | |
function pad added explained below*/ | |
if (!g_signal_connect (demuxer, "pad-added", (GCallback)pad_added, | |
vdqueue)) | |
{ | |
g_warning ("Failed to link elements demux and vdqueue!"); | |
} | |
if (!g_signal_connect (demuxer, "pad-added", (GCallback)pad_added, | |
adqueue)) | |
{ | |
g_warning ("Failed to link elements demux and adqueue!"); | |
} | |
g_print ("Now playing: %s\n", argv[1]); | |
gst_element_set_state (pipeline, GST_STATE_PLAYING); | |
g_print ("Running...\n"); | |
g_main_loop_run (loop); | |
g_print ("Returned, stopping playback\n"); | |
gst_element_set_state (pipeline, GST_STATE_NULL); | |
g_print ("Deleting pipeline\n"); | |
gst_object_unref (GST_OBJECT (pipeline)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment