mozgovipc

Cluttersink - ClutterTexture memory leak

Dec 5th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. #include <gst/gst.h>
  2. #include <clutter-gst/clutter-gst.h>
  3.  
  4. GMainLoop *loop;
  5. ClutterActor *stage, *texture;
  6. GstElement *pipeline = NULL, *appsrc, *ffcl, *sinkel;
  7. gfloat width = 1024;
  8. gfloat height = 768;
  9.  
  10. static void
  11. texture_destroy (ClutterActor *texture, gpointer user_data) {
  12.     g_message("*** texture_destroy ***");
  13. }
  14.  
  15. static void
  16. size_change(ClutterActor *tex, gint width, gint height, gpointer user_data) {
  17.     g_message("*** Size change ***");
  18.     clutter_actor_set_size(tex, width, height);
  19. }
  20.  
  21. static void
  22. create_texture () {
  23.     stage = clutter_stage_get_default();
  24.     clutter_actor_set_size (CLUTTER_ACTOR (stage), width, height);
  25.     texture = CLUTTER_ACTOR (g_object_new(CLUTTER_TYPE_TEXTURE, "sync-size", FALSE, "disable-slicing", TRUE, NULL));
  26.     g_signal_connect (CLUTTER_TEXTURE (texture), "size-change", G_CALLBACK (size_change), NULL);
  27.     g_signal_connect (CLUTTER_TEXTURE (texture), "destroy", G_CALLBACK (texture_destroy), NULL);
  28.     clutter_group_add (CLUTTER_GROUP (stage), texture);
  29.     clutter_actor_show_all(stage);
  30. }
  31.  
  32. static gboolean
  33. stop_pipe () {
  34.     // should, and does call the destroy for texture
  35.     clutter_group_remove_all(CLUTTER_GROUP (stage));
  36.  
  37.     gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
  38.     // refcount is 1, unref should free pipeline resources
  39.     g_message ("pipeline refcount=%d", GST_OBJECT_REFCOUNT_VALUE (pipeline));
  40.     gst_object_unref (pipeline);
  41.  
  42. //  clutter_actor_destroy (texture);
  43.     pipeline = NULL;
  44.     return FALSE;
  45. }
  46.  
  47. static gboolean
  48. bus_call (GstBus *bus, GstMessage *msg, gpointer user_data) {
  49.     switch (GST_MESSAGE_TYPE (msg)) {
  50.     case GST_MESSAGE_EOS:
  51.         g_message ("End of display pipe");
  52.         stop_pipe ();
  53.         break;
  54.  
  55.     case GST_MESSAGE_ERROR: {
  56.         gchar *debug;
  57.         GError *error = NULL;
  58.  
  59.         gst_message_parse_error (msg, &error, &debug);
  60.         g_free (debug);
  61.  
  62.         g_message ("Error: %s", error->message);
  63.         g_error_free (error);
  64.         stop_pipe ();
  65.     } break;
  66.  
  67.     default:
  68. //      g_message ("Unhandled message %d", GST_MESSAGE_TYPE (msg));
  69.         break;
  70.     }
  71.  
  72.     return TRUE;
  73. }
  74.  
  75. static void
  76. create_and_start_pipe () {
  77.     GstBus *bus;
  78.  
  79.     appsrc = gst_element_factory_make ("videotestsrc", NULL);
  80.     gchar *caps_str = g_strdup_printf ("video/x-raw-yuv,framerate=25/1,width=\"%d\",height=\"%d\"", (gint)width, (gint)height);
  81.     GstCaps *caps = gst_caps_from_string (caps_str);
  82.     g_free (caps_str);
  83.  
  84.     ffcl = gst_element_factory_make ("ffmpegcolorspace", NULL);
  85.  
  86.     sinkel = gst_element_factory_make ("cluttersink", NULL);
  87.     g_object_set (sinkel, "texture", CLUTTER_TEXTURE (texture), NULL);
  88. //  g_object_set (sinkel, "sync", FALSE, NULL);
  89.  
  90.     pipeline = gst_pipeline_new (NULL);
  91.     gst_bin_add_many (GST_BIN (pipeline), appsrc, ffcl, sinkel, NULL);
  92.     gst_element_link_filtered (appsrc, ffcl, caps);
  93.     gst_caps_unref (caps);
  94.     gst_element_link_many (ffcl, sinkel, NULL);
  95.  
  96.     bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  97.     gst_bus_add_watch (bus, (GstBusFunc) bus_call, NULL);
  98.     gst_object_unref (bus);
  99.  
  100.     GstStateChangeReturn stateret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  101.     g_message ("Done set to playing: %d", stateret);
  102. }
  103.  
  104. static gboolean
  105. restart () {
  106.     g_message ("\n Restarting pipe!");
  107.     if (pipeline != NULL) {
  108.         stop_pipe ();
  109.     }
  110.     create_texture ();
  111.     create_and_start_pipe ();
  112.     return TRUE;
  113. }
  114.  
  115. int
  116. main (int argc, char *argv[]) {
  117.     if (clutter_gst_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) {
  118.         g_message ("Failed to initialize clutter");
  119.         return -1;
  120.     }
  121.  
  122.     gchar *version = gst_version_string ();
  123.     g_message ("GStreamer version: %s", version);
  124.     g_free (version);
  125.  
  126.     g_message ("ClutterGst version: %s", CLUTTER_GST_VERSION_S);
  127.  
  128.     restart();
  129.     g_timeout_add_seconds(5, (GSourceFunc) restart, NULL);
  130.  
  131.     loop = g_main_loop_new (NULL, FALSE);
  132.     g_main_loop_run (loop);
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment