Skip to content

Commit 9ee6cb7

Browse files
Refactor GLib package; remove some old code from GObject API (eg. Closure) and deprecate other things planned for removal by v1; add GLibException to remove dependency on main gstreamer package; add code to help remove GErrorStruct from GError API.
1 parent 9f72f68 commit 9ee6cb7

15 files changed

+841
-722
lines changed

src/org/freedesktop/gstreamer/glib/GCancellable.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Copyright (c) 2019 Neil C Smith
3+
* Copyright (c) 2016 Isaac Raño Jares
4+
*
5+
* This file is part of gstreamer-java.
6+
*
7+
* This code is free software: you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3 only, as
9+
* published by the Free Software Foundation.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14+
* version 3 for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
120
package org.freedesktop.gstreamer.glib;
221

322
import org.freedesktop.gstreamer.lowlevel.GioAPI;
@@ -10,7 +29,7 @@ public GCancellable() {
1029
this(initializer(GioAPI.g_cancellable_new()));
1130
}
1231

13-
public GCancellable(Initializer init) {
32+
private GCancellable(Initializer init) {
1433
super(init);
1534
}
1635

src/org/freedesktop/gstreamer/glib/GDate.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2019 Neil C Smith
23
* Copyright (c) 2016 Christophe Lafolet
34
* Copyright (c) 2007 Wayne Meissner
45
*
@@ -25,43 +26,51 @@
2526

2627
import com.sun.jna.Pointer;
2728

29+
/**
30+
* Wrapper to the GDate data structure.
31+
*
32+
* See upstream documentation at <a href="https://developer.gnome.org/glib/stable/glib-Date-and-Time-Functions.html"
33+
* >https://developer.gnome.org/glib/stable/glib-Date-and-Time-Functions.html</a>
34+
*/
2835
public class GDate extends NativeObject {
2936
public static final String GTYPE_NAME = "GDate";
3037
public static final GType GTYPE = GType.valueOf(GTYPE_NAME);
3138

32-
public static GDate createInstance(int day, int month, int year) {
33-
return new GDate(GlibAPI.GLIB_API.g_date_new_dmy(day, month , year), false, true);
34-
}
35-
public static GDate createInstance(int julian_day) {
36-
return new GDate(GlibAPI.GLIB_API.g_date_new_julian(julian_day), false, true);
37-
}
38-
39+
@Deprecated
3940
public GDate(Initializer init) {
4041
super(init);
4142
}
43+
44+
@Deprecated
4245
public GDate(Pointer ptr, boolean needRef, boolean ownsHandle) {
4346
this(initializer(ptr, needRef, ownsHandle));
4447
}
45-
46-
@Override
47-
protected void disposeNativeHandle(Pointer ptr) {
48-
GlibAPI.GLIB_API.g_date_free(ptr);
49-
}
50-
51-
public int getYear() {
52-
return GlibAPI.GLIB_API.g_date_get_year(handle());
48+
public int getDay() {
49+
return GlibAPI.GLIB_API.g_date_get_day(handle());
5350
}
5451

5552
public int getMonth() {
5653
return GlibAPI.GLIB_API.g_date_get_month(handle());
5754
}
58-
59-
public int getDay() {
60-
return GlibAPI.GLIB_API.g_date_get_day(handle());
55+
public int getYear() {
56+
return GlibAPI.GLIB_API.g_date_get_year(handle());
6157
}
6258

6359
@Override
6460
public String toString() {
6561
return "" + getYear() + "-" + getMonth() + "-" + getDay();
6662
}
63+
64+
@Override
65+
protected void disposeNativeHandle(Pointer ptr) {
66+
GlibAPI.GLIB_API.g_date_free(ptr);
67+
}
68+
69+
public static GDate createInstance(int day, int month, int year) {
70+
return new GDate(GlibAPI.GLIB_API.g_date_new_dmy(day, month , year), false, true);
71+
}
72+
73+
public static GDate createInstance(int julian_day) {
74+
return new GDate(GlibAPI.GLIB_API.g_date_new_julian(julian_day), false, true);
75+
}
6776
}
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2019 Neil C Smith
23
* Copyright (c) 2007 Wayne Meissner
34
*
45
* This file is part of gstreamer-java.
@@ -18,36 +19,37 @@
1819

1920
package org.freedesktop.gstreamer.glib;
2021

22+
import java.util.Objects;
2123
import org.freedesktop.gstreamer.lowlevel.GstAPI.GErrorStruct;
2224

2325
import static org.freedesktop.gstreamer.lowlevel.GlibAPI.GLIB_API;
2426

2527
/**
26-
* Base gstreamer error type.
28+
* Base GLib error type.
2729
*/
2830
public class GError {
29-
/**
31+
32+
private final int code;
33+
private final String message;
34+
35+
/**
3036
* Creates a new instance of GError
31-
* <p>
32-
* <b> Note: </b> This takes ownership of the passed in GErrorStruct.
33-
* @param error
37+
*
38+
* @param code native int error code
39+
* @param message error message text
3440
*/
41+
public GError(int code, String message) {
42+
this.code = code;
43+
this.message = Objects.requireNonNull(message);
44+
}
45+
3546
// rewrite to accept code and message - handle struct elsewhere
3647
@Deprecated
3748
public GError(GErrorStruct error) {
3849
code = error.getCode();
3950
message = error.getMessage();
4051
GLIB_API.g_error_free(error);
4152
}
42-
43-
/**
44-
* Gets a string representation of this error.
45-
*
46-
* @return a string representing the error.
47-
*/
48-
public String getMessage() {
49-
return message;
50-
}
5153
/**
5254
* Gets a numeric code representing this error.
5355
*
@@ -56,7 +58,12 @@ public String getMessage() {
5658
public final int getCode() {
5759
return code;
5860
}
59-
60-
private final int code;
61-
private final String message;
61+
/**
62+
* Gets a string representation of this error.
63+
*
64+
* @return a string representing the error.
65+
*/
66+
public String getMessage() {
67+
return message;
68+
}
6269
}

src/org/freedesktop/gstreamer/glib/GInetAddress.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Copyright (c) 2019 Neil C Smith
3+
* Copyright (c) 2016 Isaac Raño Jares
4+
*
5+
* This file is part of gstreamer-java.
6+
*
7+
* This code is free software: you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3 only, as
9+
* published by the Free Software Foundation.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14+
* version 3 for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
120
package org.freedesktop.gstreamer.glib;
221

322
import org.freedesktop.gstreamer.lowlevel.GioAPI;
@@ -6,6 +25,7 @@ public class GInetAddress extends GObject{
625

726
public static final String GTYPE_NAME = "GInetAddress";
827

28+
@Deprecated
929
public GInetAddress(Initializer init) {
1030
super(init);
1131
}
Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1+
/*
2+
* Copyright (c) 2019 Neil C Smith
3+
* Copyright (c) 2016 Isaac Raño Jares
4+
*
5+
* This file is part of gstreamer-java.
6+
*
7+
* This code is free software: you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3 only, as
9+
* published by the Free Software Foundation.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14+
* version 3 for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
120
package org.freedesktop.gstreamer.glib;
221

3-
import org.freedesktop.gstreamer.glib.GInetAddress;
4-
import org.freedesktop.gstreamer.glib.GSocketAddress;
522
import org.freedesktop.gstreamer.lowlevel.GioAPI;
623

724
import com.sun.jna.Pointer;
8-
import org.freedesktop.gstreamer.GstException;
925

10-
public class GInetSocketAddress extends GSocketAddress{
26+
public class GInetSocketAddress extends GSocketAddress {
1127

1228
public static final String GTYPE_NAME = "GInetSocketAddress";
1329

14-
15-
protected static Initializer createRawAddress(String address, int port) {
16-
Pointer nativePointer = GioAPI.g_inet_socket_address_new_from_string(address, port);
17-
if (nativePointer == null) {
18-
throw new GstException("Can not create "+GInetSocketAddress.class.getSimpleName()+" for "+address+":"+port+", please check that the IP address is valid, with format x.x.x.x");
19-
}
20-
return initializer(nativePointer);
21-
}
22-
2330
public GInetSocketAddress(String address, int port) {
2431
this(createRawAddress(address, port));
2532
}
2633

34+
@Deprecated
2735
public GInetSocketAddress(Initializer init) {
2836
super(init);
2937
}
38+
39+
public GInetAddress getAddress() {
40+
return (GInetAddress) get("address");
41+
}
3042

3143
public int getPort() {
3244
return (Integer) get("port");
3345
}
34-
35-
public GInetAddress getAddress() {
36-
return (GInetAddress) get("address");
37-
}
46+
47+
private static Initializer createRawAddress(String address, int port) {
48+
Pointer nativePointer = GioAPI.g_inet_socket_address_new_from_string(address, port);
49+
if (nativePointer == null) {
50+
throw new GLibException("Can not create "+GInetSocketAddress.class.getSimpleName()+" for "+address+":"+port+", please check that the IP address is valid, with format x.x.x.x");
51+
}
52+
return initializer(nativePointer);
53+
}
3854
}

src/org/freedesktop/gstreamer/glib/GLib.java

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/*
2+
*
3+
* Copyright (c) 2019 Neil C Smith
4+
* Copyright (c) 2018 Ingo Randalf
5+
*
26
* This file is part of gstreamer-java.
37
*
48
* gstreamer-java is free software: you can redistribute it and/or modify
@@ -18,17 +22,71 @@
1822

1923
import org.freedesktop.gstreamer.lowlevel.GlibAPI;
2024

25+
/**
26+
* Miscellaneous Utility Functions — a selection of portable utility functions from GLib
27+
*
28+
* Documentation derived from https://developer.gnome.org/glib/stable/glib-Miscellaneous-Utility-Functions.html#g-setenv
29+
*/
2130
public class GLib {
22-
23-
public static boolean setEnv(String variable, final String value, boolean overwrite) {
24-
return GlibAPI.GLIB_API.g_setenv(variable, value, overwrite);
31+
32+
/**
33+
* Sets an environment variable. On UNIX, both the variable's name and value
34+
* can be arbitrary byte strings, except that the variable's name cannot
35+
* contain '='. On Windows, they should be in UTF-8.
36+
*
37+
* Note that on some systems, when variables are overwritten, the memory
38+
* used for the previous variables and its value isn't reclaimed.
39+
*
40+
* You should be mindful of the fact that environment variable handling in
41+
* UNIX is not thread-safe, and your program may crash if one thread calls
42+
* g_setenv() while another thread is calling getenv(). (And note that many
43+
* functions, such as gettext(), call getenv() internally.) This function is
44+
* only safe to use at the very start of your program, before creating any
45+
* other threads (or creating objects that create worker threads of their
46+
* own).
47+
*
48+
* @param variable the environment variable to set, must not contain '='.
49+
* @param value the value to set the variable to.
50+
* @param overwrite whether to change the variable if it already exists.
51+
* @return FALSE if the environment variable couldn't be set.
52+
*/
53+
public static boolean setEnv(String variable, final String value, boolean overwrite) {
54+
return GlibAPI.GLIB_API.g_setenv(variable, value, overwrite);
2555
}
26-
27-
public static String getEnv(String variable) {
28-
return GlibAPI.GLIB_API.g_getenv(variable);
29-
}
30-
56+
57+
/**
58+
* Returns the value of an environment variable.
59+
*
60+
* On UNIX, the name and value are byte strings which might or might not be
61+
* in some consistent character set and encoding. On Windows, they are in
62+
* UTF-8. On Windows, in case the environment variable's value contains
63+
* references to other environment variables, they are expanded.
64+
*
65+
* @param variable the environment variable to get.
66+
* @return the value of the environment variable, or NULL if the environment
67+
* variable is not found.
68+
*/
69+
public static String getEnv(String variable) {
70+
return GlibAPI.GLIB_API.g_getenv(variable);
71+
}
72+
73+
/**
74+
* Removes an environment variable from the environment.
75+
*
76+
* Note that on some systems, when variables are overwritten, the memory
77+
* used for the previous variables and its value isn't reclaimed.
78+
*
79+
* You should be mindful of the fact that environment variable handling in
80+
* UNIX is not thread-safe, and your program may crash if one thread calls
81+
* g_unsetenv() while another thread is calling getenv(). (And note that
82+
* many functions, such as gettext(), call getenv() internally.) This
83+
* function is only safe to use at the very start of your program, before
84+
* creating any other threads (or creating objects that create worker
85+
* threads of their own).
86+
*
87+
* @param variable the environment variable to remove, must not contain '='.
88+
*/
3189
public static void unsetEnv(String variable) {
32-
GlibAPI.GLIB_API.g_unsetenv(variable);
90+
GlibAPI.GLIB_API.g_unsetenv(variable);
3391
}
3492
}

0 commit comments

Comments
 (0)