Skip to content

Commit e56d0af

Browse files
Merge pull request #58 from isaacrj/gsocket-api-support
Gsocket api support
2 parents b1590ca + 38f47a4 commit e56d0af

File tree

11 files changed

+326
-6
lines changed

11 files changed

+326
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.freedesktop.gstreamer;
2+
3+
import org.freedesktop.gstreamer.glib.GInetAddress;
4+
import org.freedesktop.gstreamer.glib.GSocketAddress;
5+
import org.freedesktop.gstreamer.lowlevel.GioAPI;
6+
7+
import com.sun.jna.Pointer;
8+
9+
public class GInetSocketAddress extends GSocketAddress{
10+
11+
public static final String GTYPE_NAME = "GInetSocketAddress";
12+
13+
14+
protected static Initializer createRawAddress(String address, int port) {
15+
Pointer nativePointer = GioAPI.g_inet_socket_address_new_from_string(address, port);
16+
if (nativePointer == null) {
17+
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");
18+
}
19+
return initializer(nativePointer);
20+
}
21+
22+
public GInetSocketAddress(String address, int port) {
23+
this(createRawAddress(address, port));
24+
}
25+
26+
public GInetSocketAddress(Initializer init) {
27+
super(init);
28+
}
29+
30+
public int getPort() {
31+
return (Integer) get("port");
32+
}
33+
34+
public GInetAddress getAddress() {
35+
return (GInetAddress) get("address");
36+
}
37+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.freedesktop.gstreamer;
2+
3+
import org.freedesktop.gstreamer.glib.GCancellable;
4+
import org.freedesktop.gstreamer.glib.GSocketFamily;
5+
import org.freedesktop.gstreamer.glib.GSocketProtocol;
6+
import org.freedesktop.gstreamer.glib.GSocketType;
7+
import org.freedesktop.gstreamer.lowlevel.GioAPI;
8+
import org.freedesktop.gstreamer.lowlevel.GstAPI.GErrorStruct;
9+
10+
import com.sun.jna.Pointer;
11+
12+
public class GSocket extends GObject{
13+
14+
public static final String GTYPE_NAME = "GSocket";
15+
16+
public static Initializer makeRawSocket(GSocketFamily family, GSocketType type, GSocketProtocol protocol) throws GstException {
17+
GErrorStruct reference = new GErrorStruct();
18+
GErrorStruct[] errorArray = (GErrorStruct[]) reference.toArray(1);
19+
Pointer socketPointer = GioAPI.g_socket_new(family.toGioValue(), type.toGioValue(), protocol.toGioValue(), reference.getPointer());
20+
if (socketPointer == null) {
21+
errorArray[0].read();
22+
throw new GstException(new GError(errorArray[0]));
23+
}
24+
return initializer(socketPointer);
25+
}
26+
27+
public GSocket(GSocketFamily family, GSocketType type, GSocketProtocol protocol) throws GstException {
28+
this(makeRawSocket(family, type, protocol));
29+
}
30+
31+
public GSocket(Initializer init) {
32+
super(init);
33+
}
34+
35+
public GSocket bind(String address, int port) {
36+
GInetSocketAddress boundAddress = new GInetSocketAddress(address, port);
37+
GErrorStruct reference = new GErrorStruct();
38+
GErrorStruct[] errorArray = (GErrorStruct[]) reference.toArray(1);
39+
if ( ! GioAPI.g_socket_bind(getNativeAddress(), boundAddress.getNativeAddress(), true, reference.getPointer()) ) {
40+
errorArray[0].read();
41+
throw new GstException(new GError(errorArray[0]));
42+
}
43+
return this;
44+
}
45+
46+
public void connect(String address, int port) {
47+
GInetSocketAddress connectedAddress = new GInetSocketAddress(address, port);
48+
GErrorStruct reference = new GErrorStruct();
49+
GErrorStruct[] errorArray = (GErrorStruct[]) reference.toArray(1);
50+
if ( ! GioAPI.g_socket_connect(getNativeAddress(), connectedAddress.getNativeAddress(), new GCancellable().getNativeAddress(), reference.getPointer()) ) {
51+
errorArray[0].read();
52+
throw new GstException(new GError(errorArray[0]));
53+
}
54+
}
55+
56+
public GInetSocketAddress getLocalAddress() {
57+
return (GInetSocketAddress) get("local-address");
58+
}
59+
60+
public GInetSocketAddress getRemoteAddress() {
61+
return (GInetSocketAddress) get("remote-address");
62+
}
63+
64+
public GSocketType getSocketType() {
65+
return GSocketType.fromGioValue((Integer) get("type"));
66+
}
67+
68+
public GSocketFamily getSocketFamily() {
69+
return GSocketFamily.fromGioValue((Integer) get("family"));
70+
}
71+
72+
public GSocketProtocol getSocketProtocol() {
73+
return GSocketProtocol.fromGioValue((Integer) get("protocol"));
74+
}
75+
76+
public boolean isBlocking() {
77+
return (Boolean) get("blocking");
78+
}
79+
80+
public int getFD() {
81+
return (Integer) get("fd");
82+
}
83+
84+
85+
}

src/org/freedesktop/gstreamer/Gst.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package org.freedesktop.gstreamer;
2121

22+
import static org.freedesktop.gstreamer.lowlevel.GstAPI.GST_API;
23+
2224
import java.lang.reflect.Field;
2325
import java.lang.reflect.Modifier;
2426
import java.util.ArrayList;
@@ -34,11 +36,6 @@
3436
import java.util.concurrent.atomic.AtomicInteger;
3537
import java.util.logging.Logger;
3638

37-
import com.sun.jna.Memory;
38-
import com.sun.jna.Pointer;
39-
import com.sun.jna.ptr.IntByReference;
40-
import com.sun.jna.ptr.PointerByReference;
41-
4239
import org.freedesktop.gstreamer.elements.AppSink;
4340
import org.freedesktop.gstreamer.elements.AppSrc;
4441
import org.freedesktop.gstreamer.elements.BaseSink;
@@ -48,6 +45,8 @@
4845
import org.freedesktop.gstreamer.elements.PlayBin;
4946
import org.freedesktop.gstreamer.elements.URIDecodeBin;
5047
import org.freedesktop.gstreamer.glib.GDate;
48+
import org.freedesktop.gstreamer.glib.GInetAddress;
49+
import org.freedesktop.gstreamer.glib.GSocketAddress;
5150
import org.freedesktop.gstreamer.glib.MainContextExecutorService;
5251
import org.freedesktop.gstreamer.lowlevel.GMainContext;
5352
import org.freedesktop.gstreamer.lowlevel.GValueAPI.GValue;
@@ -58,7 +57,10 @@
5857
import org.freedesktop.gstreamer.lowlevel.GstTypes;
5958
import org.freedesktop.gstreamer.lowlevel.NativeObject;
6059

61-
import static org.freedesktop.gstreamer.lowlevel.GstAPI.GST_API;
60+
import com.sun.jna.Memory;
61+
import com.sun.jna.Pointer;
62+
import com.sun.jna.ptr.IntByReference;
63+
import com.sun.jna.ptr.PointerByReference;
6264

6365
/**
6466
* Media library supporting arbitrary formats and filter graphs.
@@ -446,6 +448,10 @@ private static synchronized void loadAllClasses() {
446448
@SuppressWarnings("rawtypes")
447449
private static Class[] nativeClasses = {
448450
GDate.class,
451+
GInetAddress.class,
452+
GSocket.class,
453+
GSocketAddress.class,
454+
GInetSocketAddress.class,
449455
GValue.class,
450456
GValueArray.class,
451457
TagList.class,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import org.freedesktop.gstreamer.GObject;
4+
import org.freedesktop.gstreamer.lowlevel.GioAPI;
5+
6+
public class GCancellable extends GObject{
7+
8+
public static final String GTYPE_NAME = "GCancellable";
9+
10+
public GCancellable() {
11+
this(initializer(GioAPI.g_cancellable_new()));
12+
}
13+
14+
public GCancellable(Initializer init) {
15+
super(init);
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import org.freedesktop.gstreamer.GObject;
4+
import org.freedesktop.gstreamer.lowlevel.GioAPI;
5+
6+
public class GInetAddress extends GObject{
7+
8+
public static final String GTYPE_NAME = "GInetAddress";
9+
10+
public GInetAddress(Initializer init) {
11+
super(init);
12+
}
13+
14+
public String getAddress() {
15+
return GioAPI.g_inet_address_to_string(getNativeAddress());
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import org.freedesktop.gstreamer.GObject;
4+
5+
public class GSocketAddress extends GObject{
6+
7+
public static final String GTYPE_NAME = "GSocketAddress";
8+
9+
public GSocketAddress(Initializer init) {
10+
super(init);
11+
}
12+
13+
public GSocketFamily getFamily() {
14+
return GSocketFamily.fromGioValue((Integer) get("family"));
15+
}
16+
17+
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.freedesktop.gstreamer.lowlevel.GlibAPI;
7+
8+
public enum GSocketFamily {
9+
10+
INVALID (0x00),
11+
UNIX (GlibAPI.GLIB_SYSDEF_AF_UNIX),
12+
IPV4 (GlibAPI.GLIB_SYSDEF_AF_INET),
13+
IPV6 (GlibAPI.GLIB_SYSDEF_AF_INET6);
14+
15+
private static final Map<Integer,GSocketFamily> fastResolveMap = new HashMap<Integer,GSocketFamily>();
16+
static {
17+
for(GSocketFamily dataUnitType : values()) {
18+
fastResolveMap.put(dataUnitType.toGioValue(), dataUnitType);
19+
}
20+
}
21+
22+
public static GSocketFamily fromGioValue(int gioValue) {
23+
return fastResolveMap.get(gioValue);
24+
}
25+
26+
private int gioValue;
27+
28+
private GSocketFamily(int gioValue) {
29+
this.gioValue = gioValue;
30+
}
31+
32+
public int toGioValue() {
33+
return gioValue;
34+
}
35+
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public enum GSocketProtocol {
7+
UNKNOWN (-1),
8+
DEFAULT (0),
9+
TCP (6),
10+
UDP (17),
11+
SCTP (132);
12+
13+
private static final Map<Integer,GSocketProtocol> fastResolveMap = new HashMap<Integer,GSocketProtocol>();
14+
static {
15+
for(GSocketProtocol dataUnitType : values()) {
16+
fastResolveMap.put(dataUnitType.toGioValue(), dataUnitType);
17+
}
18+
}
19+
20+
public static GSocketProtocol fromGioValue(int gioValue) {
21+
return fastResolveMap.get(gioValue);
22+
}
23+
24+
private int gioValue;
25+
26+
private GSocketProtocol(int gioValue) {
27+
this.gioValue = gioValue;
28+
}
29+
30+
public int toGioValue() {
31+
return gioValue;
32+
}
33+
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.freedesktop.gstreamer.glib;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public enum GSocketType {
7+
INVALID (0),
8+
STREAM (1),
9+
DATAGRAM (2),
10+
SEQPACKET (3);
11+
12+
private static final Map<Integer,GSocketType> fastResolveMap = new HashMap<Integer,GSocketType>();
13+
static {
14+
for(GSocketType dataUnitType : values()) {
15+
fastResolveMap.put(dataUnitType.toGioValue(), dataUnitType);
16+
}
17+
}
18+
19+
public static GSocketType fromGioValue(int gioValue) {
20+
return fastResolveMap.get(gioValue);
21+
}
22+
23+
private int gioValue;
24+
25+
private GSocketType(int gioValue) {
26+
this.gioValue = gioValue;
27+
}
28+
29+
public int toGioValue() {
30+
return gioValue;
31+
}
32+
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.freedesktop.gstreamer.lowlevel;
2+
3+
import com.sun.jna.Native;
4+
import com.sun.jna.Pointer;
5+
6+
public class GioAPI {
7+
8+
static {
9+
Native.register("gio-2.0");
10+
}
11+
12+
// GInetAddress
13+
public static native String g_inet_address_to_string(Pointer gInetAddress);
14+
15+
// GstSocketAddress
16+
public static native Pointer g_inet_socket_address_new_from_string(String address, int port);
17+
18+
// GstSocket
19+
public static native Pointer g_socket_new(int gSocketFamilyEnumValue, int gSocketTypeEnumValue, int gSocketProtcolEnumValue, Pointer gErrorStructArrayPointer);
20+
public static native boolean g_socket_bind(Pointer gSocketPointer, Pointer gSocketAddressPointer, boolean allowReuse, Pointer gErrorStructArrayPointer);
21+
public static native boolean g_socket_connect(Pointer gSocketPointer, Pointer gSocketAddressPointer, Pointer gCancellablePointer, Pointer gErrorStructArrayPointer);
22+
23+
// GCancellable
24+
public static native Pointer g_cancellable_new();
25+
26+
27+
28+
29+
30+
}

0 commit comments

Comments
 (0)