Handle asynchronous get state return using timeout.
[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                                                                       
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 static gboolean attachPipeElements (Pipeline *pipePtr, GstElement *sink0, 
58                              GstElement *sink1)
59 {
60     gboolean ret = TRUE;
61     GstElement *fsink0 = NULL; 
62     GstElement *fsink1 = NULL;
63     GstElement *bin = NULL;   
64     GstElement *tee = NULL;                                                     
65     GstElement *queue0 = NULL;                                                  
66     GstElement *queue1 = NULL;                                                  
67     GstPad *pad = NULL;   
68     
69     
70     
71         bin = gst_bin_new ("vsinkbin");
72         tee = gst_element_factory_make ("tee","splitter"); 
73         //sink0 = createImageSinkFromWindow (drawArea1, "sink0");
74         queue0 = gst_element_factory_make ("queue","q0");  
75         queue1 = gst_element_factory_make ("queue","q1");
77         if (NULL == sink0)
78            fsink0 = gst_element_factory_make ("fakesink", fsinkArr[fsinkCounter++]);
79         if (NULL == sink1)                         
80             fsink1 = gst_element_factory_make ("fakesink", fsinkArr[fsinkCounter++]); 
81                 
83         if ( !bin || !tee || !queue0 || !queue1) {
84             ret = FALSE;
85             goto exit;
86         }
87         if (NULL == sink0 && NULL == sink1) {           
88             gst_bin_add_many (GST_BIN (bin), tee, 
89                               fsink0, fsink1, queue0, queue1,NULL); 
90             gst_element_link (queue0, fsink0);
91             gst_element_link(queue1,fsink1);
92        }
94         else if (NULL != sink0 && NULL == sink1){
95             gst_bin_add_many (GST_BIN (bin), tee,                               
96                               sink0, fsink1, queue0, queue1,NULL);
97             gst_element_link (queue0, sink0);
98             gst_element_link(queue1,fsink1);
99         }
100         else if (NULL!= sink0 && NULL!=sink1) {
101              gst_bin_add_many (GST_BIN (bin), tee,                               
102                               sink0, sink1, queue0, queue1,NULL);              
103             gst_element_link (queue0, sink0);                                    
104             gst_element_link(queue1,sink1);                                    
105         }      
106     
107         else {
108             ret = FALSE;
109             goto exit;
110         }
111         /*Connecting the tee through queues is necessary to sync the two displays*/ 
112         gst_element_link(tee,queue0);                                               
113         gst_element_link(tee,queue1);                                               
114         //gst_element_link(queue0,sink0);                                             
115         //gst_element_link(queue1,sink1);                                             
116                                                                                 
117         pad = gst_element_get_static_pad (tee, "sink");                             
118         gst_element_add_pad (bin, gst_ghost_pad_new ("sink", pad));                 
119         gst_object_unref (GST_OBJECT (pad)); 
120         g_object_set (G_OBJECT (pipePtr->pipe), "video-sink", bin, NULL);     
121  
122                 
123     
124 exit:    return ret;
126 gboolean busCallback(GstBus *bus, GstMessage *msg, gpointer data)
127 {    
128         gchar *name             = (gchar *)data;
129     gchar *debugMsg     = NULL;                                                 
130     GError *err         = NULL;                                                 
131     static int count = 0;
133     switch(GST_MESSAGE_TYPE(msg)){                                              
134                                                                                 
135         case GST_MESSAGE_EOS:                                                   
136             g_printerr("%s -> Message (EOS) : End of stream.\n",name);                          
137                         count++;
138             break;                                                              
139                                                                                
140          case GST_MESSAGE_ERROR: {                                             
141             gst_message_parse_error(msg,&err,&debugMsg);                      
142             g_printerr("%s -> Message (ERROR) : %s : debug ( %s )\n", name,err->message,debugMsg);                                                      
143             g_free(debugMsg);                                                   
144             g_error_free(err);                                                  
145                         count++;
146             break;                                                              
147         }                                                                       
148         case GST_MESSAGE_WARNING: {                                             
149             gst_message_parse_warning(msg,&err,&debugMsg);                      
150             g_printerr("%s -> Message (WARNING) : %s : debug ( %s )\n",
151                                            name,err->message,debugMsg);                                                      
152             g_free(debugMsg);                                                   
153             g_error_free(err);                                                  
154             break;                                                              
155         }                                                                       
156                                                                                 
157         default:                                                                
158             break;                                                              
159     }                                                                           
160                                                                                 
161     return TRUE;                                                                
162
163        
164 Pipeline * DualDecode_createPipeline()
166     Pipeline *pipelinePtr = (Pipeline *) malloc (sizeof(Pipeline));
167     pipelinePtr->pipe = NULL;
168                                                   
169     GstBus *bus = NULL;
170                                                                                 
171            
172     pipelinePtr->pipe = gst_element_factory_make("playbin2",NULL);                 
173     bus = gst_pipeline_get_bus (GST_PIPELINE(pipelinePtr->pipe));                    
174     pipelinePtr->busSignal = gst_bus_add_watch (bus,busCallback, NULL);                                   
175     gst_object_unref (bus);     
176     pipelinePtr->logFunc = NULL;
177                                                                           
178                                                                                 
179     return pipelinePtr;                
182 gboolean DualDecode_setSinksForPipes (Pipeline **pipes,GstElement *sink0, GstElement *sink1)
184     gboolean ret = TRUE;
185     attachPipeElements (pipes[0], sink0, NULL);
186     attachPipeElements (pipes[1], sink1, NULL);
187        
189     return ret;
191 gboolean DualDecode_seekMedia(Pipeline *pipePtr, gint64 position)
193         if(position < 0){
194                 return FALSE;
195         }
196         return gst_element_seek_simple(pipePtr->pipe,
197                         GST_FORMAT_TIME,
198                         GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
199                         position);
203 gboolean DualDecode_pauseMedia (Pipeline *pipePtr)
205     gboolean ret = TRUE;
206     GstStateChangeReturn stateReturn;
207     if (GST_STATE_CHANGE_ASYNC == gst_element_set_state (pipePtr->pipe, GST_STATE_PAUSED)){
208         g_print ("STATE CHANGE RETURNED ASYNC \n");
209         stateReturn = gst_element_get_state (pipePtr->pipe,
210                                 NULL,
211                                 NULL,
212                                 TIMEOUT);
213         g_print ("GET STATE RETURNED %s\n", gst_element_state_change_return_get_name (stateReturn));
214     
215     }    
216     return ret;
219 gboolean DualDecode_resumeMedia (Pipeline *pipePtr)                              
220 {                                                                               
221     gboolean ret = TRUE;                                                        
222     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING))                
223         ret = FALSE;                                                            
224     g_print ("IN RESUME MEDIA\n");                                                                            
225     return ret;                                                                 
226
228 gboolean DualDecode_stopMedia (Pipeline *pipePtr)                             
229 {                                                                               
230     gboolean ret = TRUE;                                                        
231     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_NULL))               
232         ret = FALSE;                                                            
233                                                                                 
234     return ret;                                                                 
235 }  
238 gboolean DualDecode_setLogFunction (Pipeline *pipePtr, LogFunc func, gpointer data)
240     gboolean ret = TRUE;
241      
243     return ret;   
246 GstState DualDecode_getMediaState(Pipeline *pipePtr)
248         GstState state, pending;
249     g_print ("IN GET MEDIA STATE ENTRY\n");
250         if(GST_STATE_CHANGE_SUCCESS != gst_element_get_state(
251                                 pipePtr->pipe,
252                                 &state,
253                                 &pending,
254                                 GST_CLOCK_TIME_NONE)){
255                 /*TODO : a bug here*/
256                 g_printerr("No state in GstElement\n");
257         }
258     g_print("EXITING FROM GET MEDIA STATE\n");
259         return state;
263 gboolean DualDecode_getMediaPosition (Pipeline *pipePtr, gdouble *seekScaleValue,
264                                       gchar **timeLabelText)
266         gboolean ret = TRUE;
267     gint64 position = 0, duration = 0;
268     gint64 pos[3], dur[3];
269     GstFormat format = GST_FORMAT_TIME; 
271     if (TRUE == gst_element_query_position (pipePtr->pipe, &format, &position) 
272     && (TRUE == gst_element_query_duration (pipePtr->pipe, &format, &duration))) {
273        
274         getTime (pos, position);
275         getTime (dur, duration); 
276         *timeLabelText = g_strdup_printf ("%ld:%ld:%ld/%ld:%ld:%ld", 
277         pos[HH], pos[MM], pos[SS], dur[HH], dur[MM], dur[SS]);
278         if (duration)
279             *seekScaleValue = (gdouble)position/ (gdouble)duration * 100;
280         
281     }    
282     else 
283         ret = FALSE;
284     
286     return ret;
289 static void getTime (gint64 arr[], gint64 time)
291     gint8 i = 0;
292     time = time / (gint64) NANOSEC;
294     for (i = 0; i < 3; i++){
295         arr[i] = time % 60;
296         time = time / 60;
297     } 
298     
301 void DualDecode_setPipelineSink (Pipeline *pipePtr, GstElement *sink0, GstElement *sink1)
303     g_print ("IN SET PIPE IN SINGLE MODE");
304     attachPipeElements (pipePtr, sink0, sink1);
305     //gst_element_set_state (pipePtr->pipe, GST_STATE_READY);
308 gboolean DualDecode_playMedia (Pipeline *pipePtr, gchar *filename, gint64 position)            
309 {                                                                               
310     gboolean ret = TRUE;                                                        
312     g_print ("IN PLAYMEDIA2");                                                                                
313     g_object_set(G_OBJECT(pipePtr->pipe),"uri", filename,NULL);                 
314     gst_element_set_state (pipePtr->pipe, GST_STATE_NULL);                      
315     gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING);                   
316                                                                                 
317     DualDecode_getMediaState(pipePtr);                                          
318     DualDecode_seekMedia(pipePtr, position);                                    
319     
320     return ret;                                                                            
321                                                                                 
324 gboolean DualDecode_singleToDual (Pipeline **pipes,GstElement * otherWindowSink,
325                                   gint thisWindow, gint otherWindow,
326                                   gchar *filename)
328     gboolean ret = TRUE;
330     GstElement *thisbin = NULL;
331     GstElement *otherbin = NULL;
332     GstElement *fsink = NULL;
333     GstElement *queue0 = NULL;                                                  
334     GstElement *queue1 = NULL;                                                  
335     GstState state0 ; 
336    
337     //gst_element_set_state (pipes[thisWindow]->pipe, GST_STATE_PAUSED);
338     gst_element_get_state(                                              
339                 pipes[thisWindow]->pipe,                                                  
340                 &state0,                                                         
341                 NULL,                                                        
342                 GST_CLOCK_TIME_NONE);  
344     g_object_get (G_OBJECT (pipes[thisWindow]->pipe),"video-sink", &thisbin,NULL);
345     queue1 = gst_bin_get_by_name (GST_BIN (thisbin), "q1");                   
346     gst_element_unlink (queue1, otherWindowSink);          
347     gst_object_ref (otherWindowSink);                      
348     gst_bin_remove (GST_BIN (thisbin), otherWindowSink);       
349                                                                                 
350     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &otherbin, NULL);
351     queue0 = gst_bin_get_by_name (GST_BIN (otherbin), "q0");                  
352     fsink = gst_bin_get_by_name (GST_BIN (otherbin), "fsink0");               
353     gst_element_unlink (queue0, fsink);                                   
354     gst_object_ref (fsink);                                               
355     gst_bin_remove (GST_BIN (otherbin), fsink);                               
356                                                                                 
357     gst_bin_add (GST_BIN (thisbin), fsink);                                  
358     gst_element_sync_state_with_parent (fsink);                          
359     gst_element_link (queue1, fsink);                                    
360                                                                                 
361     gst_element_set_state (otherWindowSink, GST_STATE_READY);
362     gst_bin_add (GST_BIN (otherbin), otherWindowSink);        
363     //gst_element_sync_state_with_parent (window[otherWindow]->sink);                          
364     gst_element_link (queue0, otherWindowSink);           
365                                                                                 
366     DualDecode_playMedia (pipes[otherWindow], filename, 0);
367     //DualDecode_playMedia (pipes[thiswindow], filename[DECODER_INDEX_SINGLE], SEEK_START);
368     gst_element_set_state (pipes[thisWindow]->pipe, state0); 
370     return ret;
372  
373 gboolean DualDecode_dualToSingle (Pipeline **pipes,GstElement * otherWindowSink,
374                                   gint thisWindow, gint otherWindow)  
376     gboolean ret = TRUE;
378     GstElement *bin = NULL;
379     GstElement *bin2 = NULL;
380     GstElement *fsink = NULL;
381     GstElement *fsink0 = NULL;
382     GstElement *queue0 = NULL;
383     GstElement *queue1 = NULL; 
384     gchar *name = NULL;
385     gst_element_set_state (pipes[otherWindow]->pipe, GST_STATE_PAUSED);
386                                                                                 
387     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &bin2, NULL);
388     queue0 = gst_bin_get_by_name (GST_BIN (bin2), "q0");                  
389     gst_element_unlink (queue0, otherWindowSink);          
390     gst_object_ref (otherWindowSink);                      
391     gst_bin_remove (GST_BIN (bin2), otherWindowSink);       
392                                                                                 
393     g_object_get (G_OBJECT (pipes[thisWindow]->pipe), "video-sink", &bin, NULL);
394     queue1 = gst_bin_get_by_name (GST_BIN (bin), "q1");                   
395     fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink0");                
396     if (NULL == fsink)                                                    
397         fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink1");            
398     name = gst_element_get_name(fsink);                                   
399                                                                                 
400     gst_element_unlink (queue1, fsink);                                   
401     gst_object_ref (fsink);                                               
402     gst_bin_remove (GST_BIN (bin),fsink);                                 
403                                                                                 
404     gst_element_set_state (otherWindowSink, GST_STATE_READY);
405                                                                                 
406     gst_bin_add (GST_BIN (bin2), fsink);                                  
407     gst_element_sync_state_with_parent (fsink);                           
408                                                                                 
409     if (!g_strcmp0 (name, "fsink0")){                                   
410                                                                                 
411         g_print ("NAME IS fsink0\n");                                   
412         gst_element_link (queue0, fsink);                               
413     }                                                                     
414     else if (!g_strcmp0 (name, "fsink1")){                                
415         g_print ("NAME IS fsink1\n");                                     
416         fsink0 = gst_bin_get_by_name (GST_BIN (bin2), "fsink0");          
417         queue1 = gst_bin_get_by_name (GST_BIN (bin2), "q1");              
418         gst_element_unlink (queue1, fsink0);                              
419         gst_element_link (queue1, fsink);                                 
420         gst_element_link (queue0, fsink0);                                
421     }                                                                     
422     g_free (name);                                                       
423     gst_bin_add (GST_BIN (bin), otherWindowSink);         
424     gst_element_sync_state_with_parent (otherWindowSink); 
425     gst_element_link (queue1, otherWindowSink); 
428     return ret;