[gst-devel] I tried to replace audio sink at runtime, but failed

Antonio Marqués amm at tid.es
Mon Mar 2 11:42:49 CET 2009


I think you should first block the src pad of the previous element, and
then unlink the element. 
It's explained in: 
http://www.sfr-fresh.com/unix/privat/gstreamer-0.10.22.tar.gz:a/gstreamer-0.10.22/docs/design/part-block.txt
Regards
On Mon, 2009-03-02 at 10:13 +0100, Zhao, Halley wrote:
> Hi all:
> 
> I try to replace audio sink as following, but failed, could you give
> me some advice
> 
>  
> 
> /* pause */
> 
>         gst_element_set_state (pipeline, GST_STATE_PAUSED);
> 
> /* unlink and remove former alsa sink */
> 
>         gst_element_unlink(decoder, alsaaudiosink);      
> 
>         gst_bin_remove (GST_BIN (pipeline), alsaaudiosink);
> 
> /* link to pulse audio sink */
> 
>         pulseaudiosink = gst_element_factory_make ("pulsesink",
> "pulse_play_audio");
> 
>         gst_bin_add (GST_BIN (pipeline), pulseaudiosink);
> 
>         gst_element_link(decoder, pulseaudiosink);
> 
> /* start playing */
> 
>         gst_element_set_state (pipeline, GST_STATE_PLAYING);
> 
>  
> 
>  
> 
>  
> 
> ====complete source code====
> 
> /* example-begin helloworld.c */      
> 
> #include <gst/gst.h>
> 
>  
> 
> int 
> 
> main (int argc, char *argv[]) 
> 
> {
> 
>   GstElement *pipeline, *filesrc, *decoder, *alsaaudiosink = NULL,
> *pulseaudiosink = NULL;
> 
>  
> 
>   gst_init(&argc, &argv);
> 
>  
> 
>   if (argc != 2) {
> 
>     g_print ("usage: %s <mp3 filename>\n", argv[0]);
> 
>     exit (-1);
> 
>   }
> 
>  
> 
>   /* create a new pipeline to hold the elements */
> 
>   pipeline = gst_pipeline_new ("pipeline");
> 
>  
> 
>   /* create a disk reader */
> 
>   filesrc = gst_element_factory_make ("filesrc", "disk_source");
> 
>   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
> 
>  
> 
>   /* now it's time to get the decoder */
> 
>   decoder = gst_element_factory_make ("mad", "decoder");
> 
>   
> 
>  
> 
>   /* and an audio sink */
> 
>   alsaaudiosink = gst_element_factory_make ("alsasink",
> "alsa_play_audio");
> 
>  
> 
>   /* add objects to the main pipeline */
> 
>   gst_bin_add_many (GST_BIN (pipeline), filesrc, decoder,
> alsaaudiosink, NULL);
> 
>  
> 
>   /* link src to sink */
> 
>   gst_element_link(filesrc, decoder);
> 
>   gst_element_link(decoder,alsaaudiosink);
> 
>  
> 
>   /* start playing */
> 
>   gst_element_set_state (pipeline, GST_STATE_PLAYING);
> 
>  
> 
>   static int is_playing = 1;
> 
>   static int is_quiting = 0;
> 
>   static int is_alsasink = 1;
> 
>  
> 
>     while (1) {
> 
>       if(!(gst_bin_iterate_elements (GST_BIN (pipeline)))) break;
> 
>  
> 
>     printf("    q:Quit, p:Pause/Play, t:Test: ");
> 
>     char ch =0 ;
> 
>     ch=getchar();
> 
>  
> 
>     switch (ch) {
> 
>     case 'p':
> 
>       if(is_playing)  {
> 
>         /* pause */
> 
>         gst_element_set_state (pipeline, GST_STATE_PAUSED);
> 
>  
> 
>       }
> 
>       else {
> 
>         /* start playing */
> 
>         gst_element_set_state (pipeline, GST_STATE_PLAYING);
> 
>  
> 
>       }
> 
>       is_playing = !is_playing;
> 
>     break;
> 
>     case 't':
> 
> 
> printf("================================================================\n");
> 
>       if(is_alsasink) {
> 
>         /* pause */
> 
>         gst_element_set_state (pipeline, GST_STATE_PAUSED);
> 
>         // gst_element_set_state (pipeline, GST_STATE_NULL);
> 
>         sleep(1);
> 
>  
> 
>         gst_element_unlink(decoder, alsaaudiosink);      
> 
>         gst_bin_remove (GST_BIN (pipeline), alsaaudiosink);
> 
>         gst_element_set_state (alsaaudiosink, GST_STATE_NULL);
> 
>         gst_object_unref (GST_OBJECT (alsaaudiosink));        
> 
>         
> 
>         pulseaudiosink = gst_element_factory_make ("pulsesink",
> "pulse_play_audio");
> 
>         gst_bin_add (GST_BIN (pipeline), pulseaudiosink);
> 
>         gst_element_link(decoder, pulseaudiosink);
> 
>         sleep(1);
> 
>  
> 
>         /* start playing */
> 
>         printf("pulse sink prepare to play:\n");
> 
>         gst_element_set_state (pipeline, GST_STATE_PLAYING);
> 
>         }
> 
>       else {
> 
>         /* pause */
> 
>         gst_element_set_state (pipeline, GST_STATE_PAUSED);
> 
>         // gst_element_set_state (pipeline, GST_STATE_NULL);
> 
>         sleep(1);
> 
>  
> 
>         gst_element_unlink(decoder, pulseaudiosink);      
> 
>         gst_bin_remove (GST_BIN (pipeline), pulseaudiosink);
> 
>         gst_element_set_state (pulseaudiosink, GST_STATE_NULL);
> 
>         gst_object_unref (GST_OBJECT (pulseaudiosink));        
> 
>         
> 
>         alsaaudiosink = gst_element_factory_make ("alsasink",
> "alsa_play_audio");
> 
>         gst_bin_add (GST_BIN (pipeline), alsaaudiosink);
> 
>         gst_element_link(decoder, alsaaudiosink);
> 
>         sleep(1);
> 
>  
> 
>         /* start playing */
> 
>         printf("alsa sink prepare to play:\n");
> 
>         gst_element_set_state (pipeline, GST_STATE_PLAYING);
> 
>       }
> 
>  
> 
>       is_alsasink = !is_alsasink;
> 
>     
> 
>     break;
> 
>     case 'q':
> 
>       is_quiting = 1;
> 
>     break;
> 
>     default:
> 
>     break;
> 
>     }
> 
>  
> 
>     if(is_quiting) break;
> 
>     
> 
>     
> 
>       GstFormat fmt = GST_FORMAT_TIME;
> 
>       gint64 pos, len;
> 
>       
> 
>       if (gst_element_query_position (pipeline, &fmt, &pos)
> 
>         && gst_element_query_duration (pipeline, &fmt, &len)) {
> 
>         g_print ("Time: %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT
> "\n",
> 
>            GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
> 
>       }
> 
>  
> 
>     sleep(2);
> 
>   }
> 
>   
> 
>  
> 
>   /* stop the pipeline */
> 
>   gst_element_set_state (pipeline, GST_STATE_NULL);
> 
>  
> 
>   /* we don't need a reference to these objects anymore */
> 
>   gst_object_unref (GST_OBJECT (pipeline));
> 
>   /* unreffing the pipeline unrefs the contained elements as well */
> 
>  
> 
>   exit (0);
> 
> }
> 
> /* example-end helloworld.c */      
> 
> ZHAO, Halley (Aihua)
> 
> Email: halley.zhao at intel.com
> 
> Tel: +86(21)61166476
> 
> iNet: 8821-6476
> 
> SSG/OTC/UMD 3W033
> 
> 
>  
> 
> 
-- 
Toni Marqués Marqués
Telefónica I+D
División de Tecnologías de Video
amm at tid.es
933653188








More information about the gstreamer-devel mailing list