2010-10-17 72 views
3

我想製作一個GStreamer應用程序,它需要一個xml文件處理其內容,該內容提供諸如圖像url,其重量以及它將在屏幕上顯示的持續時間等信息。Gstreamer中的圖像幻燈片

可以使用libxml在C中清楚地處理xml文件。但是,我們如何使用GStreamer庫創建圖像的幻燈片。我想使用GStreamer,因爲我使用的硬件和SDK爲GStreamer提供本機支持。

是否有任何GStreamer插件可以做同樣的事情?或者還有什麼可以做的。我知道GStreamer的基礎知識,並且可以在C語言中爲GStreamer應用程序編寫代碼。

回答

1

嘗試下面的一段的C代碼:

#include <gst/gst.h> 
#include <glib.h> 

char *strcpy(char *dest, const xmlChar *src) 
{ 
    unsigned i; 
    for (i=0; src[i] != '\0'; ++i) 
    dest[i] = src[i]; 
     dest[i] = '\0'; 
    return dest; 
} 


int main (int argc, char *argv[]) 

{ 
char *docname; 
char *image1 = malloc(1000); 
char *image2 = malloc(1000); 

docname = "input.xml"; 

//parse the document 
xmlDocPtr doc; 
xmlNodePtr cur1, cur2, cur3; 

doc = xmlParseFile(docname); 

if (doc == NULL) { 
    fprintf(stderr,"Document not parsed successfully. \n"); 
    return -1; 
} 

cur1 = xmlDocGetRootElement(doc); 

if (cur1 == NULL) { 
    fprintf(stderr,"Empty Document\n"); 
    xmlFreeDoc(doc); 
    return -1; 
} 

if (xmlStrcmp(cur1->name, (const xmlChar *) "start")) { 
    fprintf(stderr,"document of the wrong type, root node is supposed to be start"); 
    xmlFreeDoc(doc); 
    return -1; 
} 

int flag=0; 
cur2 = cur1->xmlChildrenNode; 
while (cur2 != NULL) 
{ 
    if ((!xmlStrcmp(cur2->name, (const xmlChar *)"image"))) 
    { 
     //parseVideo (doc, cur2); 
     xmlChar *key; 
     cur3 = cur2->xmlChildrenNode; 
     while (cur3 != NULL) 
     { 
      if ((!xmlStrcmp(cur3->name, (const xmlChar *)"filename"))) 
      { 
       key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1); 
       //printf("Name of the file is: %s\n", key); 
       xmlFree(key); 
      } 

      if ((!xmlStrcmp(cur3->name, (const xmlChar *)"src"))) 
      { 
       key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1); 
       if (flag == 1) 
       { 
         strcpy(image2, key); 
         printf("the image 2 is %s \n", image2); 
         flag = 2; 
       } 
       if(flag == 0) 
       { 
         strcpy(image1, key); 
         printf("the image 1 is %s \n", image1); 
         flag = 1; 
       } 
        //printf("SRC of the file is: %s\n", key); 
       xmlFree(key); 
      } 

      if ((!xmlStrcmp(cur3->name, (const xmlChar *)"duration"))) 
      { 
       key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1); 
       //printf("No. of seconds the image should be displayed is: %s\n", key); 
       xmlFree(key); 
      } 

      if ((!xmlStrcmp(cur3->name, (const xmlChar *)"weight"))) 
      { 
       key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1); 
       //printf("The weight of the image is: %s\n", key); 
       xmlFree(key); 
      } 
     cur3 = cur3->next; 
     } 
    } 
cur2 = cur2->next; 
} 

xmlFreeDoc(doc); 

GstElement *pipeline, *source, *jpg_decoder, *freeze, *colorspace, *sink; 

    /* Initialisation */ 
gst_init (&argc, &argv); 

/* Create gstreamer elements */ 
pipeline = gst_pipeline_new ("image-player"); 
if(!pipeline) 
{ 
    g_printerr ("Pipeline could not be created. Exiting.\n"); 
    return -1; 
} 

    source = gst_element_factory_make ("filesrc",  "file-source"); 
    //set the location of the file to the argv[1] 
    g_object_set (G_OBJECT (source), "location", image1, NULL); 
if(!source) 
{ 
    g_printerr ("File could not be created. Exiting.\n"); 
    return -1; 
} 

jpg_decoder = gst_element_factory_make ("jpegdec", "jpg-decoder"); 
if(!jpg_decoder) 
{ 
    g_printerr ("Jpg Decoder could not be created. Exiting.\n"); 
    return -1; 
} 

freeze = gst_element_factory_make("imagefreeze", "freeze"); 
if(!freeze) 
{ 
    g_printerr ("ImageFreeze could not be created. Exiting.\n"); 
    return -1; 
} 

colorspace = gst_element_factory_make("ffmpegcolorspace", "colorspace"); 
if(!colorspace) 
{ 
    g_printerr ("Colorspace could not be created. Exiting.\n"); 
    return -1; 
} 

sink  = gst_element_factory_make ("ximagesink", "imagesink"); 
if(!sink) 
{ 
    g_printerr ("Image sink could not be created. Exiting.\n"); 
    return -1; 
} 

/* file-source | jpg-decoder | image-freeze | colorspace | sink */ 
gst_bin_add_many (GST_BIN (pipeline), source, jpg_decoder, freeze, colorspace, sink, NULL); 
gst_element_link_many (source, jpg_decoder, freeze, colorspace, sink, NULL); 

/* Set the pipeline to "playing" state*/ 
g_print ("Now playing: %s\n", image1); 
gst_element_set_state (pipeline, GST_STATE_PLAYING); 

getchar(); 
gst_element_set_state (pipeline, GST_STATE_READY); 
g_object_set (G_OBJECT (source), "location", image2, NULL); 
gst_element_set_state (pipeline, GST_STATE_PLAYING); 
getchar(); 

/* Out of the main loop, clean up nicely */ 
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; 
} 
+0

感謝您計算出錯誤Abhishek(我們共享相同的名稱:P)。這個腳本工作正常,但沒有給我任何輸出在屏幕上。我使用gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10)app.c -o app -I/usr/include/libxml2 -lxml2編譯它。該應用程序文件中生成並給了我下面的輸出:圖像1 1.png 圖像2 2.png 現在播放:1.png – w2lame 2010-10-31 13:20:06

+0

它正在我的筆記本電腦的Ubuntu 8.10。 – 2010-10-31 13:40:49

3

您必須創建一個帶有imagefreeze元素的管道,並在必須更改圖像時更改源位置。

一個示例應用(在Python)示出兩個靜止從硬盤上圖像:

import pygst 
pygst.require("0.10") 
import gst 

player = gst.Pipeline("player") 
source = gst.element_factory_make("filesrc", "file-source") 
source.set_property("location", "image1.jpeg") 
jpg_decoder = gst.element_factory_make("jpegdec", "jpg-decoder") 
freeze = gst.element_factory_make("imagefreeze", "freeze") 
colorspace = gst.element_factory_make("ffmpegcolorspace", "colorspace") 
sink = gst.element_factory_make("ximagesink", "imagesink") 

player.add(source, jpg_decoder, freeze, colorspace, sink) 
gst.element_link_many(source, jpg_decoder, freeze, colorspace, sink) 
player.set_state(gst.STATE_PLAYING) 

inp = raw_input("Press enter:") 
player.set_state(gst.STATE_READY) 
source.set_property("location", "image2.jpeg") 
player.set_state(gst.STATE_PLAYING) 
inp = raw_input("Press enter again:") 
+0

當我寫「GST-發射 - v filesrc location = some.png!decodebin2!imagefreeze!autovideosink「,它給了我一個錯誤警告:錯誤的管道:沒有元素」imagefreeze「 。雖然我已經安裝了gstreamer和所有可用的插件。 – w2lame 2010-10-29 09:54:54

+0

你的命令至少在Ubuntu 10.10中有效。 Imagefreeze是「好」的插件,也許你使用的是舊版本。你可以嘗試「凍結」元素,而不是「壞」插件。 – sast 2010-10-29 10:32:15

+0

我在Ubuntu 10.04和Ubuntu 10.04中也試過這個。另外,我安裝了gstremear0.10-plugins包,所以我不認爲我的包已經過時。 – w2lame 2010-10-30 09:18:55