Modified in acordance to coding guidelines.
[glsdk/dual-decode.git] / src / gst-controller.c
1 /*                                                                              
2  *  Copyright (c) 2012-2013, 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 Imeplements GStreamer related functions                                                                      
46  */ 
47 #include <stdint.h>
48 #include <string.h>
49 #include <gst/gst.h>
51 #include "gst-controller.h"
53 gchar * fsinkArr [] = {"fsink0", "fsink1"};
54 static gint fsinkCounter = 0;
55 static void getTime (gint64 arr[], gint64 time);
57 /******************************************************************************
58                      
59                            Static Functions
61 ******************************************************************************/
62 static gboolean attachPipeElements (Pipeline *pipePtr, GstElement *sink0, 
63                              GstElement *sink1)
64 {
65     gboolean ret = TRUE;
66     GstElement *fsink0 = NULL; 
67     GstElement *fsink1 = NULL;
68     GstElement *bin = NULL;   
69     GstElement *tee = NULL;                                                     
70     GstElement *queue0 = NULL;                                                  
71     GstElement *queue1 = NULL;                                                  
72     GstPad *pad = NULL;   
73     
74     
75     
76         bin = gst_bin_new ("vsinkbin");
77         tee = gst_element_factory_make ("tee","splitter"); 
78         //sink0 = createImageSinkFromWindow (drawArea1, "sink0");
79         queue0 = gst_element_factory_make ("queue","q0");  
80         queue1 = gst_element_factory_make ("queue","q1");
82         if (NULL == sink0)
83            fsink0 = gst_element_factory_make ("fakesink", fsinkArr[fsinkCounter++]);
84         if (NULL == sink1)                         
85             fsink1 = gst_element_factory_make ("fakesink", fsinkArr[fsinkCounter++]); 
86                 
88         if ( !bin || !tee || !queue0 || !queue1) {
89             ret = FALSE;
90             goto exit;
91         }
92         if (NULL == sink0 && NULL == sink1) {           
93             gst_bin_add_many (GST_BIN (bin), tee, 
94                               fsink0, fsink1, queue0, queue1,NULL); 
95             gst_element_link (queue0, fsink0);
96             gst_element_link(queue1,fsink1);
97        }
99         else if (NULL != sink0 && NULL == sink1){
100             gst_bin_add_many (GST_BIN (bin), tee,                               
101                               sink0, fsink1, queue0, queue1,NULL);
102             gst_element_link (queue0, sink0);
103             gst_element_link(queue1,fsink1);
104         }
105         else if (NULL!= sink0 && NULL!=sink1) {
106              gst_bin_add_many (GST_BIN (bin), tee,                               
107                               sink0, sink1, queue0, queue1,NULL);              
108             gst_element_link (queue0, sink0);                                    
109             gst_element_link(queue1,sink1);                                    
110         }      
111     
112         else {
113             ret = FALSE;
114             goto exit;
115         }
116         /*Connecting the tee through queues is necessary to sync the two displays*/ 
117         gst_element_link(tee,queue0);                                               
118         gst_element_link(tee,queue1);                                               
119                                                                                 
120         pad = gst_element_get_static_pad (tee, "sink");                             
121         gst_element_add_pad (bin, gst_ghost_pad_new ("sink", pad));                 
122         gst_object_unref (GST_OBJECT (pad)); 
123         g_object_set (G_OBJECT (pipePtr->pipe), "video-sink", bin, NULL);     
124  
125 exit:    return ret;
128 gboolean busCallback(GstBus *bus, GstMessage *msg, gpointer data)
129 {    
130         gchar *name             = (gchar *)data;
131     gchar *debugMsg     = NULL;                                                 
132     GError *err         = NULL;                                                 
133     static int count = 0;
135     switch(GST_MESSAGE_TYPE(msg)){                                              
136                                                                                 
137     case GST_MESSAGE_EOS:                                                   
138         g_printerr("%s -> Message (EOS) : End of stream.\n",name);                          
139             count++;
140         break;                                                              
141                                                                                
142     case GST_MESSAGE_ERROR: {                                             
143         gst_message_parse_error(msg,&err,&debugMsg);                      
144         g_printerr("%s -> Message (ERROR) : %s : debug ( %s )\n", 
145                             name,err->message,debugMsg);                                                      
146         g_free(debugMsg);                                                   
147         g_error_free(err);                                                  
148             count++;
149         break;                                                              
150     }                                                                       
151     case GST_MESSAGE_WARNING: {                                             
152         gst_message_parse_warning(msg,&err,&debugMsg);                      
153         g_printerr("%s -> Message (WARNING) : %s : debug ( %s )\n",
154                                            name,err->message,debugMsg);                                                      
155         g_free(debugMsg);                                                   
156         g_error_free(err);                                                  
157         break;                                                              
158         }                                                                       
159                                                                                 
160         default:                                                                
161             break;                                                              
162     }                                                                           
163                                                                                 
164     return TRUE;                                                                
167 static void getTime (gint64 arr[], gint64 time)
169     gint8 i = 0;
170     time = time / (gint64) NANOSEC;
172     for (i = 0; i < 3; i++){
173         arr[i] = time % 60;
174         time = time / 60;
175     } 
176     
177
178 /*****************************************************************************
180                           Public Functions
182 ******************************************************************************/       
183 Pipeline * DualDecode_createPipeline()
185     Pipeline *pipelinePtr = (Pipeline *) malloc (sizeof(Pipeline));
186     pipelinePtr->pipe = NULL;
187                                                   
188     GstBus *bus = NULL;
189                                                                                 
190            
191     pipelinePtr->pipe = gst_element_factory_make("playbin2",NULL);                 
192     bus = gst_pipeline_get_bus (GST_PIPELINE(pipelinePtr->pipe));                    
193     pipelinePtr->busSignal = gst_bus_add_watch (bus,busCallback, NULL);                                   
194     gst_object_unref (bus);     
195     pipelinePtr->logFunc = NULL;
196                                                                           
197                                                                                 
198     return pipelinePtr;                
201 gboolean DualDecode_setSinksForPipes (Pipeline **pipes,GstElement *sink0, GstElement *sink1)
203     gboolean ret = TRUE;
204     attachPipeElements (pipes[0], sink0, NULL);
205     attachPipeElements (pipes[1], sink1, NULL);
206        
208     return ret;
210 gboolean DualDecode_seekMedia(Pipeline *pipePtr, gint64 position)
212         if(position < 0){
213                 return FALSE;
214         }
215         return gst_element_seek_simple(pipePtr->pipe,
216                         GST_FORMAT_TIME,
217                         GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
218                         position);
222 gboolean DualDecode_pauseMedia (Pipeline *pipePtr)
224     gboolean ret = TRUE;
225     GstStateChangeReturn stateReturn;
226     if (GST_STATE_CHANGE_ASYNC == gst_element_set_state (pipePtr->pipe, GST_STATE_PAUSED)){
227         g_print ("STATE CHANGE RETURNED ASYNC \n");
228         stateReturn = gst_element_get_state (pipePtr->pipe,
229                                 NULL,
230                                 NULL,
231                                 TIMEOUT);
232         g_print ("GET STATE RETURNED %s\n", gst_element_state_change_return_get_name (stateReturn));
233     
234     }    
235     return ret;
238 gboolean DualDecode_resumeMedia (Pipeline *pipePtr)                              
239 {                                                                               
240     gboolean ret = TRUE;                                                        
241     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING))                
242         ret = FALSE;                                                            
243     g_print ("IN RESUME MEDIA\n");                                                                            
244     return ret;                                                                 
245
247 gboolean DualDecode_stopMedia (Pipeline *pipePtr)                             
248 {                                                                               
249     gboolean ret = TRUE;                                                        
250     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_NULL))               
251         ret = FALSE;                                                            
252                                                                                 
253     return ret;                                                                 
254 }  
257 gboolean DualDecode_setLogFunction (Pipeline *pipePtr, LogFunc func, gpointer data)
259     gboolean ret = TRUE;
260      
262     return ret;   
265 GstState DualDecode_getMediaState(Pipeline *pipePtr)
267         GstState state, pending;
268     g_print ("IN GET MEDIA STATE ENTRY\n");
269         if(GST_STATE_CHANGE_SUCCESS != gst_element_get_state(
270                                 pipePtr->pipe,
271                                 &state,
272                                 &pending,
273                                 GST_CLOCK_TIME_NONE)){
274                 /*TODO : a bug here*/
275                 g_printerr("No state in GstElement\n");
276         }
277     g_print("EXITING FROM GET MEDIA STATE\n");
278         return state;
282 gboolean DualDecode_getMediaPosition (Pipeline *pipePtr, gdouble *seekScaleValue,
283                                       gchar **timeLabelText)
285         gboolean ret = TRUE;
286     gint64 position = 0, duration = 0;
287     gint64 pos[3], dur[3];
288     GstFormat format = GST_FORMAT_TIME; 
290     if (TRUE == gst_element_query_position (pipePtr->pipe, &format, &position) 
291     && (TRUE == gst_element_query_duration (pipePtr->pipe, &format, &duration))) {
292        
293         getTime (pos, position);
294         getTime (dur, duration); 
295         *timeLabelText = g_strdup_printf ("%ld:%ld:%ld/%ld:%ld:%ld", 
296         pos[HH], pos[MM], pos[SS], dur[HH], dur[MM], dur[SS]);
297         if (duration)
298             *seekScaleValue = (gdouble)position/ (gdouble)duration * 100;
299         
300     }    
301     else 
302         ret = FALSE;
303     
305     return ret;
310 void DualDecode_setPipelineSink (Pipeline *pipePtr, GstElement *sink0, 
311                                  GstElement *sink1)
313     g_print ("IN SET PIPE IN SINGLE MODE");
314     attachPipeElements (pipePtr, sink0, sink1);
317 gboolean DualDecode_playMedia (Pipeline *pipePtr, gchar *filename, gint64 position)            
318 {                                                                               
319     gboolean ret = TRUE;                                                        
321     g_print ("IN PLAYMEDIA2");                                                                                
322     g_object_set(G_OBJECT(pipePtr->pipe),"uri", filename,NULL);                 
323     gst_element_set_state (pipePtr->pipe, GST_STATE_NULL);                      
324     gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING);                   
325                                                                                 
326     DualDecode_getMediaState(pipePtr);                                          
327     DualDecode_seekMedia(pipePtr, position);                                    
328     
329     return ret;                                                                            
330                                                                                 
333 gboolean DualDecode_singleToDual (Pipeline **pipes,GstElement * otherWindowSink,
334                                   gint thisWindow, gint otherWindow,
335                                   gchar *filename)
337     gboolean ret = TRUE;
339     GstElement *thisbin   = NULL;
340     GstElement *otherbin  = NULL;
341     GstElement *fsink     = NULL;
342     GstElement *queue0    = NULL;                                                  
343     GstElement *queue1    = NULL;                                                  
344     GstState state0       = NULL; 
345    
346     gst_element_get_state(                                              
347                 pipes[thisWindow]->pipe,                                                  
348                 &state0,                                                         
349                 NULL,                                                        
350                 GST_CLOCK_TIME_NONE);  
352     /*Remove  otherWindowsink from this pipeline*/
353     g_object_get (G_OBJECT (pipes[thisWindow]->pipe),"video-sink", &thisbin,NULL);
354     queue1 = gst_bin_get_by_name (GST_BIN (thisbin), "q1");                   
355     gst_element_unlink (queue1, otherWindowSink);          
356     gst_object_ref (otherWindowSink);                      
357     gst_bin_remove (GST_BIN (thisbin), otherWindowSink);       
358     
359     /*Remove a fake sink from the other pipeline*/                                                                            
360     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &otherbin, NULL);
361     queue0 = gst_bin_get_by_name (GST_BIN (otherbin), "q0");                  
362     fsink = gst_bin_get_by_name (GST_BIN (otherbin), "fsink0");               
363     gst_element_unlink (queue0, fsink);                                   
364     gst_object_ref (fsink);                                               
365     gst_bin_remove (GST_BIN (otherbin), fsink);                               
366       
367     /*Add fakesink to this pipeline*/                                                                          
368     gst_bin_add (GST_BIN (thisbin), fsink);                                  
369     gst_element_sync_state_with_parent (fsink);                          
370     gst_element_link (queue1, fsink);                                    
371     
372     /* Add otherWindowSink to other pipeline */                                                                            
373     gst_element_set_state (otherWindowSink, GST_STATE_READY);
374     gst_bin_add (GST_BIN (otherbin), otherWindowSink);        
375     gst_element_link (queue0, otherWindowSink);           
376                                                                                 
377     DualDecode_playMedia (pipes[otherWindow], filename, 0);
378     gst_element_set_state (pipes[thisWindow]->pipe, state0); 
380     return ret;
382  
383 gboolean DualDecode_dualToSingle (Pipeline **pipes,GstElement * otherWindowSink,
384                                   gint thisWindow, gint otherWindow)  
386     gboolean ret = TRUE;
387     GstElement *bin    = NULL;
388     GstElement *bin2   = NULL;
389     GstElement *fsink  = NULL;
390     GstElement *fsink0 = NULL;
391     GstElement *queue0 = NULL;
392     GstElement *queue1 = NULL; 
393     gchar *name        = NULL;
394     
395     /*Remove otherWindow sink from other pipeline*/
396     gst_element_set_state (pipes[otherWindow]->pipe, GST_STATE_PAUSED);
397     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &bin2, NULL);
398     queue0 = gst_bin_get_by_name (GST_BIN (bin2), "q0");                  
399     gst_element_unlink (queue0, otherWindowSink);          
400     gst_object_ref (otherWindowSink);                      
401     gst_bin_remove (GST_BIN (bin2), otherWindowSink);       
402     
403     /*Remove fake sink from this pipeline*/                                                                            
404     g_object_get (G_OBJECT (pipes[thisWindow]->pipe), "video-sink", &bin, NULL);
405     queue1 = gst_bin_get_by_name (GST_BIN (bin), "q1");                   
406     fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink0");                
407     if (NULL == fsink)                                                    
408         fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink1");            
409     name = gst_element_get_name(fsink);                                   
410     gst_element_unlink (queue1, fsink);                                   
411     gst_object_ref (fsink);                                               
412     gst_bin_remove (GST_BIN (bin),fsink);                                 
413                                                                                 
414     gst_element_set_state (otherWindowSink, GST_STATE_READY);
415     
416     /*Add fake sink to other pipeline */                                                                            
417     gst_bin_add (GST_BIN (bin2), fsink);                                  
418     gst_element_sync_state_with_parent (fsink);                           
419                                                                                 
420     if (!g_strcmp0 (name, "fsink0")){                                   
421                                                                                 
422         g_print ("NAME IS fsink0\n");                                   
423         gst_element_link (queue0, fsink);                               
424     }                                                                     
425     else if (!g_strcmp0 (name, "fsink1")){                                
426         g_print ("NAME IS fsink1\n");                                     
427         fsink0 = gst_bin_get_by_name (GST_BIN (bin2), "fsink0");          
428         queue1 = gst_bin_get_by_name (GST_BIN (bin2), "q1");              
429         gst_element_unlink (queue1, fsink0);                              
430         gst_element_link (queue1, fsink);                                 
431         gst_element_link (queue0, fsink0);                                
432     }                                                                     
433     g_free (name);
435     /*Add otherWindowSink to this pipeline*/                                                      
436     gst_bin_add (GST_BIN (bin), otherWindowSink);         
437     gst_element_sync_state_with_parent (otherWindowSink); 
438     gst_element_link (queue1, otherWindowSink); 
441     return ret;