1 /*
2 * Copyright (C) 2013, Fluendo S.A.
3 * Author: Andoni Morales <amorales@fluendo.com>
4 *
5 * Copyright (C) 2015, Collabora Ltd.
6 * Author: Matthieu Bouron <matthieu.bouron@collabora.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "gstjniutils.h"
29 #include "gstamcsurface.h"
30
31 G_DEFINE_TYPE (GstAmcSurface, gst_amc_surface, G_TYPE_OBJECT);
32
33 static gpointer parent_class = NULL;
34 static void gst_amc_surface_dispose (GObject * object);
35
36 static gboolean
_cache_java_class(GstAmcSurfaceClass * klass,GError ** err)37 _cache_java_class (GstAmcSurfaceClass * klass, GError ** err)
38 {
39 JNIEnv *env;
40
41 gst_amc_jni_initialize ();
42
43 env = gst_amc_jni_get_env ();
44
45 klass->jklass = gst_amc_jni_get_class (env, err, "android/view/Surface");
46 if (!klass->jklass) {
47 return FALSE;
48 }
49
50 klass->constructor =
51 gst_amc_jni_get_method_id (env, err, klass->jklass, "<init>",
52 "(Landroid/graphics/SurfaceTexture;)V");
53 if (!klass->constructor) {
54 goto error;
55 }
56
57 klass->is_valid =
58 gst_amc_jni_get_method_id (env, err, klass->jklass, "isValid", "()Z");
59 if (!klass->is_valid) {
60 goto error;
61 }
62
63 klass->release =
64 gst_amc_jni_get_method_id (env, err, klass->jklass, "release", "()V");
65 if (!klass->release) {
66 goto error;
67 }
68
69 klass->describe_contents =
70 gst_amc_jni_get_method_id (env, err, klass->jklass, "describeContents",
71 "()I");
72 if (!klass->describe_contents) {
73 goto error;
74 }
75
76 return TRUE;
77
78 error:
79 gst_amc_jni_object_unref (env, klass->jklass);
80 return FALSE;
81 }
82
83 static void
gst_amc_surface_class_init(GstAmcSurfaceClass * klass)84 gst_amc_surface_class_init (GstAmcSurfaceClass * klass)
85 {
86 GError *err = NULL;
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88
89 parent_class = g_type_class_peek_parent (klass);
90 gobject_class->dispose = gst_amc_surface_dispose;
91
92 if (!_cache_java_class (klass, &err)) {
93 GST_ERROR ("Could not cache java class android/view/Surface: %s",
94 err->message);
95 g_clear_error (&err);
96 }
97 }
98
99 static void
gst_amc_surface_init(GstAmcSurface * self)100 gst_amc_surface_init (GstAmcSurface * self)
101 {
102 /* initialize the object */
103 }
104
105 static void
gst_amc_surface_dispose(GObject * object)106 gst_amc_surface_dispose (GObject * object)
107 {
108 GstAmcSurface *self;
109 JNIEnv *env;
110 GError *err = NULL;
111
112 self = GST_AMC_SURFACE (object);
113 env = gst_amc_jni_get_env ();
114
115 if (!gst_amc_surface_release (self, &err)) {
116 if (err) {
117 GST_ERROR ("Error: %s", err->message);
118 }
119 }
120
121 if (self->jobject) {
122 gst_amc_jni_object_unref (env, self->jobject);
123 }
124
125 if (self->texture != NULL) {
126 g_object_unref (self->texture);
127 self->texture = NULL;
128 }
129
130 G_OBJECT_CLASS (parent_class)->dispose (object);
131 }
132
133 GstAmcSurface *
gst_amc_surface_new(GstAmcSurfaceTextureJNI * texture,GError ** err)134 gst_amc_surface_new (GstAmcSurfaceTextureJNI * texture, GError ** err)
135 {
136 GstAmcSurface *surface;
137 GstAmcSurfaceClass *klass;
138 JNIEnv *env;
139
140 surface = g_object_new (GST_TYPE_AMC_SURFACE, NULL);
141 env = gst_amc_jni_get_env ();
142 klass = GST_AMC_SURFACE_GET_CLASS (surface);
143
144 surface->jobject = gst_amc_jni_new_object (env, err, TRUE, klass->jklass,
145 klass->constructor, gst_amc_surface_texture_jni_get_jobject (texture));
146 if (surface->jobject == NULL) {
147 g_object_unref (surface);
148 return NULL;
149 }
150
151 surface->texture = g_object_ref (texture);
152 return surface;
153 }
154
155 gboolean
gst_amc_surface_is_valid(GstAmcSurface * self,gboolean * result,GError ** err)156 gst_amc_surface_is_valid (GstAmcSurface * self, gboolean * result,
157 GError ** err)
158 {
159 JNIEnv *env;
160 GstAmcSurfaceClass *klass;
161
162 env = gst_amc_jni_get_env ();
163 klass = GST_AMC_SURFACE_GET_CLASS (self);
164
165 return gst_amc_jni_call_boolean_method (env, err, self->jobject,
166 klass->is_valid, result);
167 }
168
169 gboolean
gst_amc_surface_release(GstAmcSurface * self,GError ** err)170 gst_amc_surface_release (GstAmcSurface * self, GError ** err)
171 {
172 JNIEnv *env;
173 GstAmcSurfaceClass *klass;
174
175 env = gst_amc_jni_get_env ();
176 klass = GST_AMC_SURFACE_GET_CLASS (self);
177
178 return gst_amc_jni_call_void_method (env, err, self->jobject, klass->release);
179 }
180
181 gboolean
gst_amc_surface_describe_contents(GstAmcSurface * self,gint * result,GError ** err)182 gst_amc_surface_describe_contents (GstAmcSurface * self, gint * result,
183 GError ** err)
184 {
185 JNIEnv *env;
186 GstAmcSurfaceClass *klass;
187
188 env = gst_amc_jni_get_env ();
189 klass = GST_AMC_SURFACE_GET_CLASS (self);
190
191 return gst_amc_jni_call_int_method (env, err, self->jobject,
192 klass->describe_contents, result);
193 }
194