Added files gst-controller.h and gst-controller.c
[glsdk/dual-decode.git] / src / gst-controller.c
1 /*                                                                              
2  *  Copyright (c) 2010-2011, Texas Instruments Incorporated                     
3  *                                                                              
4  *  Redistribution and use in source and binary forms, with or without          
5  *  modification, are permitted provided that the following conditions          
6  *  are met:                                                                    
7  *                                                                              
8  *  *  Redistributions of source code must retain the above copyright           
9  *   notice, this list of conditions and the following disclaimer.              
10  *                                                                              
11  *  *  Redistributions in binary form must reproduce the above copyright        
12  *   notice, this list of conditions and the following disclaimer in the        
13  *   documentation and/or other materials provided with the distribution.       
14  *                                                                              
15  *  *  Neither the name of Texas Instruments Incorporated nor the names of      
16  *   its contributors may be used to endorse or promote products derived        
17  *   from this software without specific prior written permission.              
18  *                                                                              
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,       
21  *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      
22  *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            
23  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,       
24  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,         
25  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
26  *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,    
27  *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR     
28  *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,              
29  *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                          
30  *                                                                              
31  *  Contact information for paper mail:                                         
32  *  Texas Instruments                                                           
33  *  Post Office Box 655303                                                      
34  *  Dallas, Texas 75265                                                         
35  *  Contact information:                                                        
36  *  http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
37  *  DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact                             
38  *  ============================================================================
39  *                                                                              
40  */                                                                             
41                                                                                 
42 /**                                                                             
43  * @file  gst-controller.c                                                                 
44  *                                                                              
45  * @brief                                                                       
46  */ 
47 #include <stdint.h>
48 #include <string.h>
49 #include <gst/gst.h>
50 #include "gst-controller.h"
51 #include "cli.h"
53 GMainLoop *loop;
54 int processDecode (gchar ** files)
55 {
56     gint i;
57     gint fileCount = 0;
58     gint retVal = 1;
59     g_print("In process decode\n");
60     if (NULL == files) {
61         g_print("Input files not specified\n");
62         exit(1);
63     }
64     for(i = 0; files[i] != NULL; i++){
65         g_print("In init decode %s\n", files[i]);
66     }    
67     
68     /*Count files*/
69     for (i = 0; NULL != files[i++]; fileCount++);
71     switch (fileCount) {
72     case 1:
73          g_print ("Single Decode\n");
74          setupGst (files, SINGLE);
75          g_main_loop_run(loop);
76          break;
77     case 2:
78          g_print ("Dual Decode\n");
79          setupGst (files, DUAL);
80          g_main_loop_run(loop);
81          break;
82     }
83     return retVal;
84     
85 }   
88 int initGstController ()
89 {
90     gint retVal = 1;
91     gst_init(NULL,NULL);
92     loop = g_main_loop_new(NULL,FALSE);
93     return retVal;
94 }    
96 GstElement *createPipe()
97 {                                                       
98     GstElement  *pipeline = NULL;                                         
99     
100     /*create the pipeline. This will be returned to the caller function*/       
101     pipeline = gst_element_factory_make("playbin2","pipeline");                 
102     if(!pipeline){                                                              
103         g_print("Playbin Element Creation Failed.\n");                            
104     }                                                                           
105                                                                                 
106     return pipeline;                                                            
107 }        
109 int setupGst (gchar **files, int decode)
111     gint i = 0;   
112     gint retVal = 1;
113     GstElement *bin = NULL;
114     GstElement *tee = NULL;
115     GstElement *queue0 = NULL;
116     GstElement *queue1 = NULL;
117     GstElement *sink0 = NULL;
118     GstElement *sink1 = NULL;
119     GstPad *pad = NULL;       
120                                                        
121     for ( i = 0; i < decode; i++)                                                
122     {                                                                           
123         pipelines[i] = createPipe();                                            
124     /**                                                                          
125     * Create a message handler to the bus of the pipeline.                        
126     * messages will be emitted to the application from the bus when there is an   
127     * error or warning, or when the stream is complete. The application can then  
128     * be closed if the message is handled.                                      
129     */                                                                          
130     g_object_set(G_OBJECT(pipelines[i]),"uri", *files,NULL);                     
131                                                                                 
132                            
133     if (SINGLE == decode) {  
134     /*The two xvimagesinks are put inside a bin. They are connected to a    */  
135     /*tee via two queues. The tee is connected to a ghost sink pad which    */  
136     /*makes the bin a sink element. The bin can then serve as a video-sink  */  
137     /*element for the pipeline                                              */  
138     
139     bin = gst_bin_new ("vsinkbin");      
140     sink0 = gst_element_factory_make ("xvimagesink","disp0");                                        
141     sink1 = gst_element_factory_make ("xvimagesink","disp1");   
142     tee = gst_element_factory_make ("tee","splitter");                             
143     queue0 = gst_element_factory_make ("queue","q0");                              
144     queue1 = gst_element_factory_make ("queue","q1");                              
145     gst_bin_add_many (GST_BIN (bin), tee, sink0, sink1, queue0, queue1,NULL);          
146                                                                                 
147     /*connecting the tee through queues is necessary to sync the two displays*/ 
148     gst_element_link(tee,queue0);                                               
149     gst_element_link(tee,queue1);                                               
150     gst_element_link(queue0,sink0);                                             
151     gst_element_link(queue1,sink1);                                             
152                                                                                 
153     pad = gst_element_get_static_pad (tee, "sink");                             
154     gst_element_add_pad (bin, gst_ghost_pad_new ("sink", pad));                 
155     gst_object_unref (GST_OBJECT (pad));        
156     
157     g_object_set(G_OBJECT(pipelines[i]), "video-sink", bin, NULL); 
158     }                                                  
159     else if (DUAL == decode) {                                                         
160         *files++;                                                              
161     }
162     gst_element_set_state (pipelines[i], GST_STATE_PLAYING);                    
163                                                                                 
164                                                                                 
165     bus = gst_pipeline_get_bus (GST_PIPELINE(pipelines[i]));                    
166     gst_bus_add_watch (bus,busCallback,loop);                                   
167     gst_object_unref (bus);    
168     }
169     return retVal;                
170 }     
171  
172 gboolean busCallback(GstBus *bus, GstMessage *msg, gpointer data)
173 {    
174         gchar *name             = (gchar *)data;
175     gchar *debugMsg     = NULL;                                                 
176     GError *err         = NULL;                                                 
177     static int count = 0;
179     switch(GST_MESSAGE_TYPE(msg)){                                              
180                                                                                 
181         case GST_MESSAGE_EOS:                                                   
182             g_printerr("%s -> Message (EOS) : End of stream.\n",name);                          
183                         count++;
184             break;                                                              
185                                                                                
186          case GST_MESSAGE_ERROR: {                                             
187             gst_message_parse_error(msg,&err,&debugMsg);                      
188             g_printerr("%s -> Message (ERROR) : %s : debug ( %s )\n", name,err->message,debugMsg);                                                      
189             g_free(debugMsg);                                                   
190             g_error_free(err);                                                  
191                         count++;
192             break;                                                              
193         }                                                                       
194         case GST_MESSAGE_WARNING: {                                             
195             gst_message_parse_warning(msg,&err,&debugMsg);                      
196             g_printerr("%s -> Message (WARNING) : %s : debug ( %s )\n",
197                                            name,err->message,debugMsg);                                                      
198             g_free(debugMsg);                                                   
199             g_error_free(err);                                                  
200             break;                                                              
201         }                                                                       
202                                                                                 
203         default:                                                                
204             break;                                                              
205     }                                                                           
206                                                                                 
207     return TRUE;                                                                
208
209        
210