Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 3.28 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. gstreamer leaking memory when not using pulseaudio
  2. int current_sound;
  3. int current_beep_type = BEEP_CONT;
  4. int next_beep_type = -1;
  5. gboolean sound_muted = FALSE;
  6. static GstSeekFlags seek_flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT;
  7. GstElement *sound_pipeline = NULL;
  8.  
  9. int update_callback(GtkWidget *widget, gpointer data) {
  10.  
  11.     static int i = 0;
  12.     switch((i++)%2) {
  13.         case 0:
  14.             play_sound("morse_code.ogg",BEEP_CONT);
  15.             break;
  16.         case 1:
  17.             destroy_sound();
  18.             break;
  19.         default:
  20.             break;
  21.     }
  22.  
  23.     return TRUE;
  24. }
  25.  
  26.  
  27.  
  28. static gboolean bus_call(GstBus *bus, GstMessage *msg, void *user_data)
  29. {
  30.     switch (GST_MESSAGE_TYPE(msg)) {
  31.         case GST_MESSAGE_EOS: {
  32.             //g_print("End of streamn");
  33.             if(current_beep_type == BEEP_CONT) {
  34.                 gst_element_seek (sound_pipeline, 1.0,
  35.                       GST_FORMAT_TIME,
  36.                       seek_flags,
  37.                       GST_SEEK_TYPE_SET, 0,
  38.                       GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
  39.                 break;
  40.             }
  41.             else {
  42.                 //gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_NULL);
  43.             }
  44.             if(strlen(next_uri) > 0) {
  45.  
  46.                 g_object_set(G_OBJECT(sound_pipeline), "uri", next_uri, NULL);
  47.  
  48.                 current_beep_type = next_beep_type;
  49.                 memset(next_uri,0,sizeof(next_uri));
  50.                 if(!sound_muted) gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_PLAYING);
  51.                 else gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_PAUSED);
  52.             }
  53.             else {
  54.                 gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_NULL);
  55.             }
  56.  
  57.             break;
  58.         }
  59.         case GST_MESSAGE_ERROR: {
  60.             GError *err;
  61.             gst_message_parse_error(msg, &err, NULL);
  62.             g_error("%s", err->message);
  63.             g_error_free(err);
  64.             break;
  65.         }
  66.         default:
  67.             break;
  68.     }
  69.  
  70.     return TRUE;
  71. }
  72.  
  73. void play_sound(char *sound, int beep)
  74. {
  75.     strcpy(next_uri,"file://");
  76.     strcat(next_uri,SOUNDS_DIR);
  77.     strcat(next_uri,sound);
  78.  
  79.     next_beep_type = beep;
  80.  
  81.     if(sound_pipeline == NULL) {
  82.         mad_log(LOG_INFO,"creating sound_pipelinen");
  83.         GstBus *bus;
  84.         sound_pipeline = gst_element_factory_make("playbin", "play");
  85.         bus = gst_pipeline_get_bus(GST_PIPELINE(sound_pipeline));
  86.         gst_bus_add_watch(bus, bus_call, loop);
  87.         gst_object_unref(bus);
  88.     }
  89.  
  90.     GstState state;
  91.  
  92.     gst_element_get_state(sound_pipeline,&state,NULL,500000000);
  93.  
  94.     if (state == GST_STATE_NULL) {
  95.         //g_print("pipeline is in NULL staten");
  96.         g_object_set(G_OBJECT(sound_pipeline), "uri", next_uri, NULL);
  97.         current_beep_type = next_beep_type;
  98.         memset(next_uri,0,sizeof(next_uri));
  99.         if(!sound_muted) gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_PLAYING);
  100.         else gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_PAUSED);
  101.     }
  102.  
  103. }
  104.  
  105. void destroy_sound(void) {
  106.     if(sound_pipeline)
  107.     {        
  108.         current_beep_type = -1;
  109.         gst_element_set_state(GST_ELEMENT(sound_pipeline), GST_STATE_NULL);
  110.         g_object_unref(G_OBJECT(sound_pipeline));
  111.         sound_pipeline = NULL;
  112.     }
  113. }