1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
#include <jni.h>
#include <gst/gst.h>
#include <gio/gio.h>
#include <android/log.h>
/* XXX: Workaround for Android <21 making signal() an inline function
* around bsd_signal(), and Android >= 21 not having any bsd_signal()
* symbol but only signal().
* See https://bugzilla.gnome.org/show_bug.cgi?id=766235
*/
__sighandler_t bsd_signal(int signum, __sighandler_t handler) __attribute__((weak));
__sighandler_t bsd_signal(int signum, __sighandler_t handler)
{
return signal(signum, handler);
}
static jobject _context = NULL;
static jobject _class_loader = NULL;
static GstClockTime _priv_gst_info_start_time;
#define GST_G_IO_MODULE_DECLARE(name) \
extern void G_PASTE(g_io_module_, G_PASTE(name, _load_static)) (void)
#define GST_G_IO_MODULE_LOAD(name) \
G_PASTE(g_io_module_, G_PASTE(name, _load_static)) ()
/* Declaration of static plugins */
@PLUGINS_DECLARATION@
/* Declaration of static gio modules */
@G_IO_MODULES_DECLARE@
/* Call this function to register static plugins */
void
gst_android_register_static_plugins (void)
{
@PLUGINS_REGISTRATION@
}
/* Call this function to load GIO modules */
void
gst_android_load_gio_modules (void)
{
@G_IO_MODULES_LOAD@
}
static void
glib_print_handler (const gchar * string)
{
__android_log_print (ANDROID_LOG_INFO, "GLib+stdout", "%s", string);
}
static void
glib_printerr_handler (const gchar * string)
{
__android_log_print (ANDROID_LOG_ERROR, "GLib+stderr", "%s", string);
}
/* Based on GLib's default handler */
#define CHAR_IS_SAFE(wc) (!((wc < 0x20 && wc != '\t' && wc != '\n' && wc != '\r') || \
(wc == 0x7f) || \
(wc >= 0x80 && wc < 0xa0)))
#define FORMAT_UNSIGNED_BUFSIZE ((GLIB_SIZEOF_LONG * 3) + 3)
#define STRING_BUFFER_SIZE (FORMAT_UNSIGNED_BUFSIZE + 32)
#define ALERT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
static void
escape_string (GString * string)
{
const char *p = string->str;
gunichar wc;
while (p < string->str + string->len) {
gboolean safe;
wc = g_utf8_get_char_validated (p, -1);
if (wc == (gunichar) - 1 || wc == (gunichar) - 2) {
gchar *tmp;
guint pos;
pos = p - string->str;
/* Emit invalid UTF-8 as hex escapes
*/
tmp = g_strdup_printf ("\\x%02x", (guint) (guchar) * p);
g_string_erase (string, pos, 1);
g_string_insert (string, pos, tmp);
p = string->str + (pos + 4); /* Skip over escape sequence */
g_free (tmp);
continue;
}
if (wc == '\r') {
safe = *(p + 1) == '\n';
} else {
safe = CHAR_IS_SAFE (wc);
}
if (!safe) {
gchar *tmp;
guint pos;
pos = p - string->str;
/* Largest char we escape is 0x0a, so we don't have to worry
* about 8-digit \Uxxxxyyyy
*/
tmp = g_strdup_printf ("\\u%04x", wc);
g_string_erase (string, pos, g_utf8_next_char (p) - p);
g_string_insert (string, pos, tmp);
g_free (tmp);
p = string->str + (pos + 6); /* Skip over escape sequence */
} else
p = g_utf8_next_char (p);
}
}
static void
glib_log_handler (const gchar * log_domain, GLogLevelFlags log_level,
const gchar * message, gpointer user_data)
{
gchar *string;
GString *gstring;
const gchar *domains;
gint android_log_level;
gchar *tag;
if ((log_level & DEFAULT_LEVELS) || (log_level >> G_LOG_LEVEL_USER_SHIFT))
goto emit;
domains = g_getenv ("G_MESSAGES_DEBUG");
if (((log_level & INFO_LEVELS) == 0) ||
domains == NULL ||
(strcmp (domains, "all") != 0 && (!log_domain
|| !strstr (domains, log_domain))))
return;
emit:
if (log_domain)
tag = g_strdup_printf ("GLib+%s", log_domain);
else
tag = g_strdup ("GLib");
switch (log_level & G_LOG_LEVEL_MASK) {
case G_LOG_LEVEL_ERROR:
android_log_level = ANDROID_LOG_ERROR;
break;
case G_LOG_LEVEL_CRITICAL:
android_log_level = ANDROID_LOG_ERROR;
break;
case G_LOG_LEVEL_WARNING:
android_log_level = ANDROID_LOG_WARN;
break;
case G_LOG_LEVEL_MESSAGE:
android_log_level = ANDROID_LOG_INFO;
break;
case G_LOG_LEVEL_INFO:
android_log_level = ANDROID_LOG_INFO;
break;
case G_LOG_LEVEL_DEBUG:
android_log_level = ANDROID_LOG_DEBUG;
break;
default:
android_log_level = ANDROID_LOG_INFO;
break;
}
gstring = g_string_new (NULL);
if (!message) {
g_string_append (gstring, "(NULL) message");
} else {
GString * msg = g_string_new (message);
escape_string (msg);
g_string_append (gstring, msg->str);
g_string_free (msg, TRUE);
}
string = g_string_free (gstring, FALSE);
__android_log_print (android_log_level, tag, "%s", string);
g_free (string);
g_free (tag);
}
static void
gst_debug_logcat (GstDebugCategory * category, GstDebugLevel level,
const gchar * file, const gchar * function, gint line,
GObject * object, GstDebugMessage * message, gpointer unused)
{
GstClockTime elapsed;
gint android_log_level;
gchar *tag;
if (level > gst_debug_category_get_threshold (category))
return;
elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
gst_util_get_timestamp ());
switch (level) {
case GST_LEVEL_ERROR:
android_log_level = ANDROID_LOG_ERROR;
break;
case GST_LEVEL_WARNING:
android_log_level = ANDROID_LOG_WARN;
break;
case GST_LEVEL_INFO:
android_log_level = ANDROID_LOG_INFO;
break;
case GST_LEVEL_DEBUG:
android_log_level = ANDROID_LOG_DEBUG;
break;
default:
android_log_level = ANDROID_LOG_VERBOSE;
break;
}
tag = g_strdup_printf ("GStreamer+%s",
gst_debug_category_get_name (category));
if (object) {
gchar *obj;
if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
obj = g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
} else if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
obj = g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
} else if (G_IS_OBJECT (object)) {
obj = g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
} else {
obj = g_strdup_printf ("<%p>", object);
}
__android_log_print (android_log_level, tag,
"%" GST_TIME_FORMAT " %p %s:%d:%s:%s %s\n",
GST_TIME_ARGS (elapsed), g_thread_self (),
file, line, function, obj, gst_debug_message_get (message));
g_free (obj);
} else {
__android_log_print (android_log_level, tag,
"%" GST_TIME_FORMAT " %p %s:%d:%s %s\n",
GST_TIME_ARGS (elapsed), g_thread_self (),
file, line, function, gst_debug_message_get (message));
}
g_free (tag);
}
static gboolean
get_application_dirs (JNIEnv * env, jobject context, gchar ** cache_dir,
gchar ** files_dir)
{
jclass context_class;
jmethodID get_cache_dir_id, get_files_dir_id;
jclass file_class;
jmethodID get_absolute_path_id;
jobject dir;
jstring abs_path;
const gchar *abs_path_str;
*cache_dir = *files_dir = NULL;
context_class = (*env)->GetObjectClass (env, context);
if (!context_class) {
return FALSE;
}
get_cache_dir_id =
(*env)->GetMethodID (env, context_class, "getCacheDir",
"()Ljava/io/File;");
get_files_dir_id =
(*env)->GetMethodID (env, context_class, "getFilesDir",
"()Ljava/io/File;");
if (!get_cache_dir_id || !get_files_dir_id) {
(*env)->DeleteLocalRef (env, context_class);
return FALSE;
}
file_class = (*env)->FindClass (env, "java/io/File");
if (!file_class) {
(*env)->DeleteLocalRef (env, context_class);
return FALSE;
}
get_absolute_path_id =
(*env)->GetMethodID (env, file_class, "getAbsolutePath",
"()Ljava/lang/String;");
if (!get_absolute_path_id) {
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
dir = (*env)->CallObjectMethod (env, context, get_cache_dir_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
if (dir) {
abs_path = (*env)->CallObjectMethod (env, dir, get_absolute_path_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, dir);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
abs_path_str = (*env)->GetStringUTFChars (env, abs_path, NULL);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
*cache_dir = abs_path ? g_strdup (abs_path_str) : NULL;
(*env)->ReleaseStringUTFChars (env, abs_path, abs_path_str);
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
}
dir = (*env)->CallObjectMethod (env, context, get_files_dir_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
if (dir) {
abs_path = (*env)->CallObjectMethod (env, dir, get_absolute_path_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, dir);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
abs_path_str = (*env)->GetStringUTFChars (env, abs_path, NULL);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
(*env)->DeleteLocalRef (env, context_class);
(*env)->DeleteLocalRef (env, file_class);
return FALSE;
}
*files_dir = files_dir ? g_strdup (abs_path_str) : NULL;
(*env)->ReleaseStringUTFChars (env, abs_path, abs_path_str);
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
}
(*env)->DeleteLocalRef (env, file_class);
(*env)->DeleteLocalRef (env, context_class);
return TRUE;
}
jobject
gst_android_get_application_context ()
{
return _context;
}
jobject
gst_android_get_application_class_loader ()
{
return _class_loader;
}
static gboolean
init (JNIEnv *env, jobject context)
{
jclass context_cls = NULL;
jmethodID get_class_loader_id = 0;
jobject class_loader = NULL;
context_cls = (*env)->GetObjectClass (env, context);
if (!context_cls) {
return FALSE;
}
get_class_loader_id = (*env)->GetMethodID (env, context_cls,
"getClassLoader", "()Ljava/lang/ClassLoader;");
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
return FALSE;
}
class_loader = (*env)->CallObjectMethod (env, context, get_class_loader_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
return FALSE;
}
if (_context) {
(*env)->DeleteGlobalRef (env, _context);
}
_context = (*env)->NewGlobalRef (env, context);
if (_class_loader) {
(*env)->DeleteGlobalRef (env, _class_loader);
}
_class_loader = (*env)->NewGlobalRef (env, class_loader);
return TRUE;
}
void
gst_android_init (JNIEnv * env, jobject context)
{
gchar *cache_dir;
gchar *files_dir;
gchar *registry;
GError *error = NULL;
if (!init (env, context)) {
__android_log_print (ANDROID_LOG_INFO, "GStreamer",
"GStreamer failed to initialize");
}
if (gst_is_initialized ()) {
__android_log_print (ANDROID_LOG_INFO, "GStreamer",
"GStreamer already initialized");
return;
}
if (!get_application_dirs (env, context, &cache_dir, &files_dir)) {
__android_log_print (ANDROID_LOG_ERROR, "GStreamer",
"Failed to get application dirs");
}
if (cache_dir) {
g_setenv ("TMP", cache_dir, TRUE);
g_setenv ("TEMP", cache_dir, TRUE);
g_setenv ("TMPDIR", cache_dir, TRUE);
g_setenv ("XDG_RUNTIME_DIR", cache_dir, TRUE);
g_setenv ("XDG_CACHE_HOME", cache_dir, TRUE);
registry = g_build_filename (cache_dir, "registry.bin", NULL);
g_setenv ("GST_REGISTRY", registry, TRUE);
g_free (registry);
g_setenv ("GST_REGISTRY_REUSE_PLUGIN_SCANNER", "no", TRUE);
/* TODO: Should probably also set GST_PLUGIN_SCANNER and GST_PLUGIN_SYSTEM_PATH */
}
if (files_dir) {
gchar *fontconfig, *certs;
g_setenv ("HOME", files_dir, TRUE);
g_setenv ("XDG_DATA_DIRS", files_dir, TRUE);
g_setenv ("XDG_CONFIG_DIRS", files_dir, TRUE);
g_setenv ("XDG_CONFIG_HOME", files_dir, TRUE);
g_setenv ("XDG_DATA_HOME", files_dir, TRUE);
fontconfig = g_build_filename (files_dir, "fontconfig", NULL);
g_setenv ("FONTCONFIG_PATH", fontconfig, TRUE);
g_free (fontconfig);
certs = g_build_filename (files_dir, "ssl", "certs", "ca-certificates.crt", NULL);
g_setenv ("CA_CERTIFICATES", certs, TRUE);
g_free (certs);
}
g_free (cache_dir);
g_free (files_dir);
/* Set GLib print handlers */
g_set_print_handler (glib_print_handler);
g_set_printerr_handler (glib_printerr_handler);
g_log_set_default_handler (glib_log_handler, NULL);
/* Disable this for releases if performance is important
* or increase the threshold to get more information */
gst_debug_set_active (TRUE);
gst_debug_set_default_threshold (GST_LEVEL_WARNING);
gst_debug_remove_log_function (gst_debug_log_default);
gst_debug_add_log_function ((GstLogFunction) gst_debug_logcat, NULL, NULL);
/* get time we started for debugging messages */
_priv_gst_info_start_time = gst_util_get_timestamp ();
if (!gst_init_check (NULL, NULL, &error)) {
gchar *message = g_strdup_printf ("GStreamer initialization failed: %s",
error && error->message ? error->message : "(no message)");
jclass exception_class = (*env)->FindClass (env, "java/lang/Exception");
__android_log_print (ANDROID_LOG_ERROR, "GStreamer", "%s", message);
(*env)->ThrowNew (env, exception_class, message);
g_free (message);
return;
}
gst_android_register_static_plugins ();
gst_android_load_gio_modules ();
__android_log_print (ANDROID_LOG_INFO, "GStreamer",
"GStreamer initialization complete");
}
static void
gst_android_init_jni (JNIEnv * env, jobject gstreamer, jobject context)
{
gst_android_init (env, context);
}
static JNINativeMethod native_methods[] = {
{"nativeInit", "(Landroid/content/Context;)V", (void *) gst_android_init_jni}
};
jint
JNI_OnLoad (JavaVM * vm, void * reserved)
{
JNIEnv *env = NULL;
GModule *module;
if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) {
__android_log_print (ANDROID_LOG_ERROR, "GStreamer",
"Could not retrieve JNIEnv");
return 0;
}
jclass klass = (*env)->FindClass (env, "org/freedesktop/gstreamer/GStreamer");
if (!klass) {
__android_log_print (ANDROID_LOG_ERROR, "GStreamer",
"Could not retrieve class org.freedesktop.gstreamer.GStreamer");
return 0;
}
if ((*env)->RegisterNatives (env, klass, native_methods,
G_N_ELEMENTS (native_methods))) {
__android_log_print (ANDROID_LOG_ERROR, "GStreamer",
"Could not register native methods for org.freedesktop.gstreamer.GStreamer");
return 0;
}
/* Tell the androidmedia plugin about the Java VM if we can */
module = g_module_open (NULL, G_MODULE_BIND_LOCAL);
if (module) {
void (*set_java_vm) (JavaVM *) = NULL;
if (g_module_symbol (module, "gst_amc_jni_set_java_vm",
(gpointer *) & set_java_vm) && set_java_vm) {
set_java_vm (vm);
}
g_module_close (module);
}
return JNI_VERSION_1_4;
}
void
JNI_OnUnload (JavaVM * vm, void * reversed)
{
JNIEnv *env = NULL;
if ((*vm)->GetEnv (vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) {
__android_log_print (ANDROID_LOG_ERROR, "GStreamer",
"Could not retrieve JNIEnv");
return;
}
if (_context) {
(*env)->DeleteGlobalRef (env, _context);
_context = NULL;
}
if (_class_loader) {
(*env)->DeleteGlobalRef (env, _class_loader);
_class_loader = NULL;
}
}
|