|
| 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 GSocket create(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 new GSocket(initializer(socketPointer)); |
| 25 | + } |
| 26 | + |
| 27 | + public static GSocket createDefaultUdpSocket() { |
| 28 | + return create(GSocketFamily.IPV4, GSocketType.DATAGRAM, GSocketProtocol.UDP); |
| 29 | + } |
| 30 | + |
| 31 | + public static GSocket createDefaultUdpSocket(String bindAddress, int bindPort) { |
| 32 | + return create(GSocketFamily.IPV4, GSocketType.DATAGRAM, GSocketProtocol.UDP).bind(bindAddress, bindPort); |
| 33 | + } |
| 34 | + |
| 35 | + public GSocket(Initializer init) { |
| 36 | + super(init); |
| 37 | + } |
| 38 | + |
| 39 | + public GSocket bind(String address, int port) { |
| 40 | + GInetSocketAddress boundAddress = GInetSocketAddress.create(address, port); |
| 41 | + GErrorStruct reference = new GErrorStruct(); |
| 42 | + GErrorStruct[] errorArray = (GErrorStruct[]) reference.toArray(1); |
| 43 | + if ( ! GioAPI.g_socket_bind(getNativeAddress(), boundAddress.getNativeAddress(), true, reference.getPointer()) ) { |
| 44 | + errorArray[0].read(); |
| 45 | + throw new GstException(new GError(errorArray[0])); |
| 46 | + } |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public void connect(String address, int port) { |
| 51 | + GInetSocketAddress connectedAddress = GInetSocketAddress.create(address, port); |
| 52 | + GErrorStruct reference = new GErrorStruct(); |
| 53 | + GErrorStruct[] errorArray = (GErrorStruct[]) reference.toArray(1); |
| 54 | + if ( ! GioAPI.g_socket_connect(getNativeAddress(), connectedAddress.getNativeAddress(), new GCancellable().getNativeAddress(), reference.getPointer()) ) { |
| 55 | + errorArray[0].read(); |
| 56 | + throw new GstException(new GError(errorArray[0])); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public GInetSocketAddress getLocalAddress() { |
| 61 | + return (GInetSocketAddress) get("local-address"); |
| 62 | + } |
| 63 | + |
| 64 | + public GInetSocketAddress getRemoteAddress() { |
| 65 | + return (GInetSocketAddress) get("remote-address"); |
| 66 | + } |
| 67 | + |
| 68 | + public GSocketType getSocketType() { |
| 69 | + return GSocketType.fromGioValue((Integer) get("type")); |
| 70 | + } |
| 71 | + |
| 72 | + public GSocketFamily getSocketFamily() { |
| 73 | + return GSocketFamily.fromGioValue((Integer) get("family")); |
| 74 | + } |
| 75 | + |
| 76 | + public GSocketProtocol getSocketProtocol() { |
| 77 | + return GSocketProtocol.fromGioValue((Integer) get("protocol")); |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isBlocking() { |
| 81 | + return (Boolean) get("blocking"); |
| 82 | + } |
| 83 | + |
| 84 | + public Integer getFD() { |
| 85 | + return (Integer) get("fd"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
0 commit comments