Play Media function independent of the draw area.
[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 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 setSink (Pipeline *pipePtr, GstElement *sink)
128     gboolean ret = TRUE;
129    
131     return ret;
132 }  
133 gboolean busCallback(GstBus *bus, GstMessage *msg, gpointer data)
134 {    
135         gchar *name             = (gchar *)data;
136     gchar *debugMsg     = NULL;                                                 
137     GError *err         = NULL;                                                 
138     static int count = 0;
140     switch(GST_MESSAGE_TYPE(msg)){                                              
141                                                                                 
142         case GST_MESSAGE_EOS:                                                   
143             g_printerr("%s -> Message (EOS) : End of stream.\n",name);                          
144                         count++;
145             break;                                                              
146                                                                                
147          case GST_MESSAGE_ERROR: {                                             
148             gst_message_parse_error(msg,&err,&debugMsg);                      
149             g_printerr("%s -> Message (ERROR) : %s : debug ( %s )\n", name,err->message,debugMsg);                                                      
150             g_free(debugMsg);                                                   
151             g_error_free(err);                                                  
152                         count++;
153             break;                                                              
154         }                                                                       
155         case GST_MESSAGE_WARNING: {                                             
156             gst_message_parse_warning(msg,&err,&debugMsg);                      
157             g_printerr("%s -> Message (WARNING) : %s : debug ( %s )\n",
158                                            name,err->message,debugMsg);                                                      
159             g_free(debugMsg);                                                   
160             g_error_free(err);                                                  
161             break;                                                              
162         }                                                                       
163                                                                                 
164         default:                                                                
165             break;                                                              
166     }                                                                           
167                                                                                 
168     return TRUE;                                                                
169
170        
171 Pipeline * DualDecode_createPipeline()
173     Pipeline *pipelinePtr = (Pipeline *) malloc (sizeof(Pipeline));
174     pipelinePtr->pipe = NULL;
175                                                   
176     GstBus *bus = NULL;
177                                                                                 
178            
179     pipelinePtr->pipe = gst_element_factory_make("playbin2",NULL);                 
180     bus = gst_pipeline_get_bus (GST_PIPELINE(pipelinePtr->pipe));                    
181     pipelinePtr->busSignal = gst_bus_add_watch (bus,busCallback, NULL);                                   
182     gst_object_unref (bus);     
183     pipelinePtr->logFunc = NULL;
184                                                                           
185                                                                                 
186     return pipelinePtr;                
189 gboolean DualDecode_setSinksForPipes (Pipeline **pipes,GstElement *sink0, GstElement *sink1)
191     gboolean ret = TRUE;
192     attachPipeElements (pipes[0], sink0, NULL);
193     attachPipeElements (pipes[1], sink1, NULL);
194        
196     return ret;
198 gboolean DualDecode_seekMedia(Pipeline *pipePtr, gint64 position)
200         if(position < 0){
201                 return FALSE;
202         }
203         return gst_element_seek_simple(pipePtr->pipe,
204                         GST_FORMAT_TIME,
205                         GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
206                         position);
210 gboolean DualDecode_pauseMedia (Pipeline *pipePtr)
212     gboolean ret = TRUE;
213     if (GST_STATE_CHANGE_ASYNC == gst_element_set_state (pipePtr->pipe, GST_STATE_PAUSED)){
214         g_print ("SET CHANGE RETURNED ASYNC \n");
215         gst_element_get_state (pipePtr->pipe,
216                                 NULL,
217                                 NULL,
218                                 GST_CLOCK_TIME_NONE);
219     }
221     
222     
223     
224     return ret;
227 gboolean DualDecode_resumeMedia (Pipeline *pipePtr)                              
228 {                                                                               
229     gboolean ret = TRUE;                                                        
230     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING))                
231         ret = FALSE;                                                            
232     g_print ("IN RESUME MEDIA\n");                                                                            
233     return ret;                                                                 
234
236 gboolean DualDecode_stopMedia (Pipeline *pipePtr)                             
237 {                                                                               
238     gboolean ret = TRUE;                                                        
239     if(!gst_element_set_state (pipePtr->pipe, GST_STATE_NULL))               
240         ret = FALSE;                                                            
241                                                                                 
242     return ret;                                                                 
243 }  
246 gboolean DualDecode_setLogFunction (Pipeline *pipePtr, LogFunc func, gpointer data)
248     gboolean ret = TRUE;
249     
251     return ret;   
254 GstState DualDecode_getMediaState(Pipeline *pipePtr)
256         GstState state, pending;
257     g_print ("IN GET MEDIA STATE ENTRY\n");
258         if(GST_STATE_CHANGE_SUCCESS != gst_element_get_state(
259                                 pipePtr->pipe,
260                                 &state,
261                                 &pending,
262                                 GST_CLOCK_TIME_NONE)){
263                 /*TODO : a bug here*/
264                 g_printerr("No state in GstElement\n");
265         }
266     g_print("EXITING FROM GET MEDIA STATE\n");
267         return state;
271 gboolean DualDecode_getMediaPosition (Pipeline *pipePtr, gdouble *seekScaleValue,
272                                       gchar **timeLabelText)
274         gboolean ret = TRUE;
275     gint64 position = 0, duration = 0;
276     gint64 pos[3], dur[3];
277     GstFormat format = GST_FORMAT_TIME; 
279     if (TRUE == gst_element_query_position (pipePtr->pipe, &format, &position) 
280     && (TRUE == gst_element_query_duration (pipePtr->pipe, &format, &duration))) {
281        
282         getTime (pos, position);
283         getTime (dur, duration); 
284         *timeLabelText = g_strdup_printf ("%ld:%ld:%ld/%ld:%ld:%ld", 
285         pos[HH], pos[MM], pos[SS], dur[HH], dur[MM], dur[SS]);
286         if (duration)
287             *seekScaleValue = (gdouble)position/ (gdouble)duration * 100;
288         
289     }    
290     else 
291         ret = FALSE;
292     
294     return ret;
297 static void getTime (gint64 arr[], gint64 time)
299     gint8 i = 0;
300     time = time / (gint64) NANOSEC;
302     for (i = 0; i < 3; i++){
303         arr[i] = time % 60;
304         time = time / 60;
305     } 
306     
309 void DualDecode_setPipelineSink (Pipeline *pipePtr, GstElement *sink0, GstElement *sink1)
311     g_print ("IN SET PIPE IN SINGLE MODE");
312     attachPipeElements (pipePtr, sink0, sink1);
313     //gst_element_set_state (pipePtr->pipe, GST_STATE_READY);
316 gboolean DualDecode_playMedia2 (Pipeline *pipePtr, gchar *filename, gint64 position)            
317 {                                                                               
318     gboolean ret = TRUE;                                                        
320     g_print ("IN PLAYMEDIA2");                                                                                
321     g_object_set(G_OBJECT(pipePtr->pipe),"uri", filename,NULL);                 
322     gst_element_set_state (pipePtr->pipe, GST_STATE_NULL);                      
323     gst_element_set_state (pipePtr->pipe, GST_STATE_PLAYING);                   
324                                                                                 
325     DualDecode_getMediaState(pipePtr);                                          
326     DualDecode_seekMedia(pipePtr, position);                                    
327                                                                                 
328                                                                                 
329 exit:    return ret;                                                            
332 gboolean DualDecode_singleToDual (Pipeline **pipes,GstElement * otherWindowSink,
333                                   gint thisWindow, gint otherWindow,
334                                   gchar *filename)
336     gboolean ret = TRUE;
338     GstElement *thisbin = NULL;
339     GstElement *otherbin = NULL;
340     GstElement *fsink = NULL;
341     GstElement *queue0 = NULL;                                                  
342     GstElement *queue1 = NULL;                                                  
343     GstState state0 ; 
344    
345     //gst_element_set_state (pipes[thisWindow]->pipe, GST_STATE_PAUSED);
346     gst_element_get_state(                                              
347                 pipes[thisWindow]->pipe,                                                  
348                 &state0,                                                         
349                 NULL,                                                        
350                 GST_CLOCK_TIME_NONE);  
352     g_object_get (G_OBJECT (pipes[thisWindow]->pipe),"video-sink", &thisbin,NULL);
353     queue1 = gst_bin_get_by_name (GST_BIN (thisbin), "q1");                   
354     gst_element_unlink (queue1, otherWindowSink);          
355     gst_object_ref (otherWindowSink);                      
356     gst_bin_remove (GST_BIN (thisbin), otherWindowSink);       
357                                                                                 
358     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &otherbin, NULL);
359     queue0 = gst_bin_get_by_name (GST_BIN (otherbin), "q0");                  
360     fsink = gst_bin_get_by_name (GST_BIN (otherbin), "fsink0");               
361     gst_element_unlink (queue0, fsink);                                   
362     gst_object_ref (fsink);                                               
363     gst_bin_remove (GST_BIN (otherbin), fsink);                               
364                                                                                 
365     gst_bin_add (GST_BIN (thisbin), fsink);                                  
366     gst_element_sync_state_with_parent (fsink);                          
367     gst_element_link (queue1, fsink);                                    
368                                                                                 
369     gst_element_set_state (otherWindowSink, GST_STATE_READY);
370     gst_bin_add (GST_BIN (otherbin), otherWindowSink);        
371     //gst_element_sync_state_with_parent (window[otherWindow]->sink);                          
372     gst_element_link (queue0, otherWindowSink);           
373                                                                                 
374     DualDecode_playMedia2 (pipes[otherWindow], filename, 0);
375     //DualDecode_playMedia2 (pipes[thiswindow], filename[DECODER_INDEX_SINGLE], SEEK_START);
376     gst_element_set_state (pipes[thisWindow]->pipe, state0); 
378     return ret;
380  
381 gboolean DualDecode_dualToSingle (Pipeline **pipes,GstElement * otherWindowSink,
382                                   gint thisWindow, gint otherWindow)  
384     gboolean ret = TRUE;
386     GstElement *bin = NULL;
387     GstElement *bin2 = NULL;
388     GstElement *fsink = NULL;
389     GstElement *fsink0 = NULL;
390     GstElement *queue0 = NULL;
391     GstElement *queue1 = NULL; 
392     gchar *name = NULL;
393     gst_element_set_state (pipes[otherWindow]->pipe, GST_STATE_PAUSED);
394                                                                                 
395     g_object_get (G_OBJECT (pipes[otherWindow]->pipe), "video-sink", &bin2, NULL);
396     queue0 = gst_bin_get_by_name (GST_BIN (bin2), "q0");                  
397     gst_element_unlink (queue0, otherWindowSink);          
398     gst_object_ref (otherWindowSink);                      
399     gst_bin_remove (GST_BIN (bin2), otherWindowSink);       
400                                                                                 
401     g_object_get (G_OBJECT (pipes[thisWindow]->pipe), "video-sink", &bin, NULL);
402     queue1 = gst_bin_get_by_name (GST_BIN (bin), "q1");                   
403     fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink0");                
404     if (NULL == fsink)                                                    
405         fsink = gst_bin_get_by_name (GST_BIN (bin), "fsink1");            
406     name = gst_element_get_name(fsink);                                   
407                                                                                 
408     gst_element_unlink (queue1, fsink);                                   
409     gst_object_ref (fsink);                                               
410     gst_bin_remove (GST_BIN (bin),fsink);                                 
411                                                                                 
412     gst_element_set_state (otherWindowSink, GST_STATE_READY);
413                                                                                 
414     gst_bin_add (GST_BIN (bin2), fsink);                                  
415     gst_element_sync_state_with_parent (fsink);                           
416                                                                                 
417     if (!g_strcmp0 (name, "fsink0")){                                   
418                                                                                 
419         g_print ("NAME IS fsink0\n");                                   
420         gst_element_link (queue0, fsink);                               
421     }                                                                     
422     else if (!g_strcmp0 (name, "fsink1")){                                
423         g_print ("NAME IS fsink1\n");                                     
424         fsink0 = gst_bin_get_by_name (GST_BIN (bin2), "fsink0");          
425         queue1 = gst_bin_get_by_name (GST_BIN (bin2), "q1");              
426         gst_element_unlink (queue1, fsink0);                              
427         gst_element_link (queue1, fsink);                                 
428         gst_element_link (queue0, fsink0);                                
429     }                                                                     
430     g_free (name);                                                       
431     gst_bin_add (GST_BIN (bin), otherWindowSink);         
432     gst_element_sync_state_with_parent (otherWindowSink); 
433     gst_element_link (queue1, otherWindowSink); 
436     return ret;