\hypertarget{ecore_pipe_gstreamer_example_8c-example}{\section{ecore\+\_\+pipe\+\_\+gstreamer\+\_\+example.\+c}
}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o ecore\_pipe\_gstreamer\_example ecore\_pipe\_gstreamer\_example.c `pkg-config --libs --cflags ecore
       gstreamer-1.0`}
\textcolor{comment}{// or}
\textcolor{comment}{// gcc -o ecore\_pipe\_gstreamer\_example ecore\_pipe\_gstreamer\_example.c `pkg-config --libs --cflags ecore
       gstreamer-0.10`}

\textcolor{preprocessor}{#include <gst/gst.h>}
\textcolor{preprocessor}{#include <Ecore.h>}

\textcolor{keyword}{static} \textcolor{keywordtype}{int} nbr = 0;

\textcolor{keyword}{static} GstElement *\_build\_pipeline(gchar *filename, \hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe} *pipe);

\textcolor{keyword}{static} \textcolor{keywordtype}{void}        new\_decoded\_pad\_cb(GstElement *demuxer,
                                      GstPad *new\_pad,
                                      gpointer user\_data);

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
handler(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *buf, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} len EINA\_UNUSED)
\{
   GstBuffer *buffer = *((GstBuffer **)buf);

   printf(\textcolor{stringliteral}{"handler : %p\(\backslash\)n"}, buffer);
   printf(\textcolor{stringliteral}{"frame  : %d %p %lld %p\(\backslash\)n"}, nbr++, data, (\textcolor{keywordtype}{long} \textcolor{keywordtype}{long})GST\_BUFFER\_DURATION(buffer), buffer);
   gst\_buffer\_unref(buffer);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
handoff(GstElement *\textcolor{keywordtype}{object} EINA\_UNUSED,
        GstBuffer *arg0,
        GstPad *arg1 EINA\_UNUSED,
        gpointer user\_data)
\{
   \hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe} *\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe};

   pipe = (\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe} *)user\_data;
   printf(\textcolor{stringliteral}{"handoff : %p\(\backslash\)n"}, arg0);
   gst\_buffer\_ref(arg0);
   \hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}(pipe, &arg0, \textcolor{keyword}{sizeof}(arg0));
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} *argv[])
\{
   GstElement *pipeline;
   \textcolor{keywordtype}{char} *filename;
   \hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe} *\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe};

   gst\_init(&argc, &argv);

   \textcolor{keywordflow}{if} (!\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}())
     \{
        gst\_deinit();
        \textcolor{keywordflow}{return} 0;
     \}

   pipe = \hyperlink{group__Ecore__Pipe__Group_ga3d584a8f962644f748dfd84d39d3421a}{ecore\_pipe\_add}(handler, NULL);
   \textcolor{keywordflow}{if} (!pipe)
     \{
        \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
        gst\_deinit();
        \textcolor{keywordflow}{return} 0;
     \}

   \textcolor{keywordflow}{if} (argc < 2)
     \{
        g\_print(\textcolor{stringliteral}{"usage: %s file.avi\(\backslash\)n"}, argv[0]);
        \hyperlink{group__Ecore__Pipe__Group_ga7ed5669e83a3119d2023c64288f9af60}{ecore\_pipe\_del}(pipe);
        \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
        gst\_deinit();
        \textcolor{keywordflow}{return} 0;
     \}
   filename = argv[1];

   pipeline = \_build\_pipeline(filename, pipe);
   \textcolor{keywordflow}{if} (!pipeline)
     \{
        g\_print(\textcolor{stringliteral}{"Error during the pipeline building\(\backslash\)n"});
        \hyperlink{group__Ecore__Pipe__Group_ga7ed5669e83a3119d2023c64288f9af60}{ecore\_pipe\_del}(pipe);
        \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
        gst\_deinit();
        \textcolor{keywordflow}{return} -1;
     \}

   gst\_element\_set\_state(pipeline, GST\_STATE\_PLAYING);

   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();

   \hyperlink{group__Ecore__Pipe__Group_ga7ed5669e83a3119d2023c64288f9af60}{ecore\_pipe\_del}(pipe);
   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();
   gst\_deinit();

   \textcolor{keywordflow}{return} 0;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
new\_decoded\_pad\_cb(GstElement *demuxer EINA\_UNUSED,
                   GstPad *new\_pad,
                   gpointer user\_data)
\{
   GstElement *decoder;
   GstPad *pad;
   GstCaps *caps;
   GstStructure *s;
   \textcolor{keyword}{const} gchar *str;

\textcolor{preprocessor}{#if GST\_CHECK\_VERSION(1,0,0)}
   caps = gst\_pad\_get\_current\_caps(new\_pad);
\textcolor{preprocessor}{#else}
   caps = gst\_pad\_get\_caps(new\_pad);
\textcolor{preprocessor}{#endif}
   s = gst\_caps\_get\_structure(caps, 0);
   str = gst\_structure\_get\_name(s);

   \textcolor{keywordflow}{if} (g\_str\_has\_prefix(str, \textcolor{stringliteral}{"video/"}))
     \{
        decoder = GST\_ELEMENT(user\_data);

\textcolor{preprocessor}{#if GST\_CHECK\_VERSION(1,0,0)}
        pad = gst\_element\_get\_static\_pad(decoder, \textcolor{stringliteral}{"sink"});
\textcolor{preprocessor}{#else}
        pad = gst\_element\_get\_pad(decoder, \textcolor{stringliteral}{"sink"});
\textcolor{preprocessor}{#endif}
        \textcolor{keywordflow}{if} (GST\_PAD\_LINK\_FAILED(gst\_pad\_link(new\_pad, pad)))
          \{
             g\_warning(\textcolor{stringliteral}{"Failed to link %s:%s to %s:%s"}, GST\_DEBUG\_PAD\_NAME(new\_pad),
                       GST\_DEBUG\_PAD\_NAME(pad));
          \}
        gst\_object\_unref(pad);
     \}
   gst\_caps\_unref(caps);
\}

\textcolor{keyword}{static} GstElement *
\_build\_pipeline(gchar *filename, \hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe} *pipe)
\{
   GstElement *pipeline;
   GstElement *filesrc;
   GstElement *demuxer;
   GstElement *decoder;
   GstElement *sink;
   GstStateChangeReturn res;

   pipeline = gst\_pipeline\_new(\textcolor{stringliteral}{"pipeline"});
   \textcolor{keywordflow}{if} (!pipeline)
     \textcolor{keywordflow}{return} NULL;

   filesrc = gst\_element\_factory\_make(\textcolor{stringliteral}{"filesrc"}, \textcolor{stringliteral}{"filesrc"});
   \textcolor{keywordflow}{if} (!filesrc)
     \{
        printf(\textcolor{stringliteral}{"no filesrc"});
        \textcolor{keywordflow}{goto} failure;
     \}
   g\_object\_set(G\_OBJECT(filesrc), \textcolor{stringliteral}{"location"}, filename, NULL);

   demuxer = gst\_element\_factory\_make(\textcolor{stringliteral}{"oggdemux"}, \textcolor{stringliteral}{"demuxer"});
   \textcolor{keywordflow}{if} (!demuxer)
     \{
        printf(\textcolor{stringliteral}{"no demux"});
        \textcolor{keywordflow}{goto} failure;
     \}

   decoder = gst\_element\_factory\_make(\textcolor{stringliteral}{"theoradec"}, \textcolor{stringliteral}{"decoder"});
   \textcolor{keywordflow}{if} (!decoder)
     \{
        printf(\textcolor{stringliteral}{"no dec"});
        \textcolor{keywordflow}{goto} failure;
     \}

   g\_signal\_connect(demuxer, \textcolor{stringliteral}{"pad-added"},
                    G\_CALLBACK(new\_decoded\_pad\_cb), decoder);

   sink = gst\_element\_factory\_make(\textcolor{stringliteral}{"fakesink"}, \textcolor{stringliteral}{"sink"});
   \textcolor{keywordflow}{if} (!sink)
     \{
        printf(\textcolor{stringliteral}{"no sink"});
        \textcolor{keywordflow}{goto} failure;
     \}
   g\_object\_set(G\_OBJECT(sink), \textcolor{stringliteral}{"sync"}, \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}, NULL);
   g\_object\_set(G\_OBJECT(sink), \textcolor{stringliteral}{"signal-handoffs"}, \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}, NULL);
   g\_signal\_connect(sink, \textcolor{stringliteral}{"handoff"},
                    G\_CALLBACK(handoff), pipe);

   gst\_bin\_add\_many(GST\_BIN(pipeline),
                    filesrc, demuxer, decoder, sink, NULL);

   \textcolor{keywordflow}{if} (!gst\_element\_link(filesrc, demuxer))
     \textcolor{keywordflow}{goto} failure;
   \textcolor{keywordflow}{if} (!gst\_element\_link(decoder, sink))
     \textcolor{keywordflow}{goto} failure;

   res = gst\_element\_set\_state(pipeline, GST\_STATE\_PAUSED);
   \textcolor{keywordflow}{if} (res == GST\_STATE\_CHANGE\_FAILURE)
     \textcolor{keywordflow}{goto} failure;

   res = gst\_element\_get\_state(pipeline, NULL, NULL, GST\_CLOCK\_TIME\_NONE);
   \textcolor{keywordflow}{if} (res != GST\_STATE\_CHANGE\_SUCCESS)
     \textcolor{keywordflow}{goto} failure;

   \textcolor{keywordflow}{return} pipeline;

failure:
   gst\_object\_unref(GST\_OBJECT(pipeline));
   \textcolor{keywordflow}{return} NULL;
\}

\end{DoxyCodeInclude}
 