1 /* GStreamer
2  * Copyright (C) 2011 David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <string.h>
25 
26 #include "gstintersurface.h"
27 
28 static GList *list;
29 static GMutex mutex;
30 
31 GstInterSurface *
gst_inter_surface_get(const char * name)32 gst_inter_surface_get (const char *name)
33 {
34   GList *g;
35   GstInterSurface *surface;
36 
37   g_mutex_lock (&mutex);
38   for (g = list; g; g = g_list_next (g)) {
39     surface = g->data;
40     if (strcmp (name, surface->name) == 0) {
41       surface->ref_count++;
42       g_mutex_unlock (&mutex);
43       return surface;
44     }
45   }
46 
47   surface = g_malloc0 (sizeof (GstInterSurface));
48   surface->ref_count = 1;
49   surface->name = g_strdup (name);
50   g_mutex_init (&surface->mutex);
51   surface->audio_adapter = gst_adapter_new ();
52   surface->audio_buffer_time = DEFAULT_AUDIO_BUFFER_TIME;
53   surface->audio_latency_time = DEFAULT_AUDIO_LATENCY_TIME;
54   surface->audio_period_time = DEFAULT_AUDIO_PERIOD_TIME;
55 
56   list = g_list_append (list, surface);
57   g_mutex_unlock (&mutex);
58 
59   return surface;
60 }
61 
62 void
gst_inter_surface_unref(GstInterSurface * surface)63 gst_inter_surface_unref (GstInterSurface * surface)
64 {
65   /* Mutex needed here, otherwise refcount might become 0
66    * and someone else requests the same surface again before
67    * we remove it from the list */
68   g_mutex_lock (&mutex);
69   if ((--surface->ref_count) == 0) {
70     GList *g;
71 
72     for (g = list; g; g = g_list_next (g)) {
73       GstInterSurface *tmp = g->data;
74       if (strcmp (tmp->name, surface->name) == 0) {
75         list = g_list_delete_link (list, g);
76         break;
77       }
78     }
79 
80     g_mutex_clear (&surface->mutex);
81     gst_buffer_replace (&surface->video_buffer, NULL);
82     gst_buffer_replace (&surface->sub_buffer, NULL);
83     gst_object_unref (surface->audio_adapter);
84     g_free (surface->name);
85     g_free (surface);
86   }
87   g_mutex_unlock (&mutex);
88 }
89