|
| 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 | +} |
0 commit comments