> types() {
return Stream.of(
+ registration(WebRTCDataChannel.class,
+ WebRTCDataChannel.GTYPE_NAME,
+ WebRTCDataChannel::new),
registration(WebRTCSessionDescription.class,
WebRTCSessionDescription.GTYPE_NAME,
WebRTCSessionDescription::new),
diff --git a/src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java b/src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java
index c3198102..b1f8fecb 100644
--- a/src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java
+++ b/src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java
@@ -18,13 +18,12 @@
*/
package org.freedesktop.gstreamer.webrtc;
-import org.freedesktop.gstreamer.Bin;
-import org.freedesktop.gstreamer.Element;
-import org.freedesktop.gstreamer.Gst;
-import org.freedesktop.gstreamer.Promise;
-import org.freedesktop.gstreamer.Structure;
+import com.sun.jna.ptr.PointerByReference;
+import org.freedesktop.gstreamer.*;
import org.freedesktop.gstreamer.glib.NativeEnum;
+import org.freedesktop.gstreamer.glib.Natives;
+import org.freedesktop.gstreamer.lowlevel.GstAPI;
import org.freedesktop.gstreamer.lowlevel.GstAPI.GstCallback;
/**
@@ -43,6 +42,8 @@ public class WebRTCBin extends Bin {
public static final String GST_NAME = "webrtcbin";
public static final String GTYPE_NAME = "GstWebRTCBin";
+ private Promise promiseCreateOffer;
+ private Promise promiseCreateAnswer;
WebRTCBin(Initializer init) {
super(init);
@@ -148,6 +149,7 @@ public void onChange(Promise promise) {
}
});
emit("create-offer", null, promise);
+ promiseCreateOffer = promise;
}
/**
@@ -172,6 +174,7 @@ public void onChange(Promise promise) {
}
});
emit("create-answer", null, promise);
+ promiseCreateAnswer = promise;
}
/**
@@ -296,4 +299,42 @@ public WebRTCSessionDescription getRemoteDescription() {
description.disown();
return description;
}
+
+ /**
+ * Creates a {@link WebRTCDataChannel} that can be used to send messages to other connected peers
+ *
+ * Should be called once this {@link WebRTCBin} is in the READY state. Otherwise an error will be thrown
+ *
+ * @return the new {@link WebRTCDataChannel}
+ */
+ public WebRTCDataChannel createDataChannel(String label) {
+ if (getState() != State.READY) {
+ throw new IllegalStateException("WebRTCBin must be in state READY");
+ }
+ PointerByReference channel = new PointerByReference();
+ emit("create-data-channel", label, null, channel);
+ return Natives.objectFor(channel.getValue(), WebRTCDataChannel.class, false, true);
+ }
+
+ /**
+ * Signal emitted when this {@link WebRTCBin} receives a {@link WebRTCDataChannel} from
+ * a remote peer.
+ */
+ public interface ON_DATA_CHANNEL {
+ void onDataChannel(WebRTCDataChannel channel);
+ }
+
+ /**
+ * Adds a listener for the on-data-channel
signal.
+ *
+ * @param listener
+ */
+ public void connect(final ON_DATA_CHANNEL listener) {
+ connect(ON_DATA_CHANNEL.class, listener, new GstAPI.GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(Element elem, WebRTCDataChannel channel) {
+ listener.onDataChannel(channel);
+ }
+ });
+ }
}
diff --git a/src/org/freedesktop/gstreamer/webrtc/WebRTCDataChannel.java b/src/org/freedesktop/gstreamer/webrtc/WebRTCDataChannel.java
new file mode 100644
index 00000000..c378e0c8
--- /dev/null
+++ b/src/org/freedesktop/gstreamer/webrtc/WebRTCDataChannel.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2018 Antonio Morales
+ *
+ * This file is part of gstreamer-java.
+ *
+ * This code is free software: you can redistribute it and/or modify it under the terms of the GNU
+ * Lesser General Public License version 3 only, as published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License version 3 for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License version 3 along with
+ * this work. If not, see .
+ */
+
+package org.freedesktop.gstreamer.webrtc;
+
+import org.freedesktop.gstreamer.Gst;
+import org.freedesktop.gstreamer.GstObject;
+import org.freedesktop.gstreamer.glib.GBytes;
+import org.freedesktop.gstreamer.lowlevel.GstAPI.GstCallback;
+
+/**
+ *
+ * @see https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/blob/master/ext/webrtc/webrtcdatachannel.h
+ * available since Gstreamer 1.15
+ */
+@Gst.Since(minor = 15)
+public class WebRTCDataChannel extends GstObject {
+ public static final String GTYPE_NAME = "GstWebRTCDataChannel";
+
+ public WebRTCDataChannel(Initializer init) {
+ super(init);
+ }
+
+ /**
+ * Signal emitted when this {@link WebRTCDataChannel} has an error.
+ */
+ public static interface ON_ERROR {
+ public void onError(WebRTCDataChannel channel);
+ }
+
+ /**
+ * Signal emitted when this {@link WebRTCDataChannel} is opened.
+ */
+ public static interface ON_OPEN {
+ public void onOpen(WebRTCDataChannel channel);
+ }
+
+ /**
+ * Signal emitted when this {@link WebRTCDataChannel} is closed.
+ */
+ public static interface ON_CLOSE {
+ public void onClose(WebRTCDataChannel channel);
+ }
+
+ /**
+ * Signal emitted when this {@link WebRTCDataChannel} receives a string message from a remote peer.
+ */
+ public static interface ON_MESSAGE_STRING {
+ public void onMessage(WebRTCDataChannel channel, String message);
+ }
+
+ public void connect(final ON_ERROR listener) {
+ connect(ON_ERROR.class, listener, new GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(GstObject channel) {
+ listener.onError((WebRTCDataChannel) channel);
+ }
+ });
+ }
+
+ public void connect(final ON_OPEN listener) {
+ connect(ON_OPEN.class, listener, new GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(GstObject channel) {
+ listener.onOpen((WebRTCDataChannel) channel);
+ }
+ });
+ }
+
+ public void connect(final ON_CLOSE listener) {
+ connect(ON_CLOSE.class, listener, new GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(GstObject channel) {
+ listener.onClose((WebRTCDataChannel) channel);
+ }
+ });
+ }
+
+ public void connect(final ON_MESSAGE_STRING listener) {
+ connect(ON_MESSAGE_STRING.class, listener, new GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(GstObject channel, String message) {
+ listener.onMessage((WebRTCDataChannel) channel, message);
+ }
+ });
+ }
+
+ /**
+ * Sends a string through this {@link WebRTCDataChannel} which will be received by connected remote peers.
+ * @param message that should be sent over the WebRTC data-channel connection.
+ */
+ public void sendMessage(String message) {
+ emit("send-string", message);
+ }
+
+
+ /**
+ * Signal emitted when this {@link WebRTCDataChannel} receives binary data from a remote peer.
+ */
+ public interface ON_MESSAGE_DATA {
+ void onMessage(WebRTCDataChannel channel, byte[] data);
+ }
+
+ public void connect(final ON_MESSAGE_DATA listener) {
+ connect(ON_MESSAGE_DATA.class, listener, new GstCallback() {
+ @SuppressWarnings("unused")
+ public void callback(GstObject channel, GBytes gbytes) {
+ listener.onMessage((WebRTCDataChannel) channel, gbytes.getBytes());
+ gbytes.dispose();
+ }
+ });
+ }
+
+ /**
+ * Sends binary data through this {@link WebRTCDataChannel} which will be received by connected remote peers.
+ * @param bytes that should be sent over the WebRTC data-channel connection.
+ */
+ public void sendMessage(byte[] bytes) {
+ GBytes gbytes = GBytes.createInstance(bytes);
+ gbytes.disown();
+ emit("send-data", gbytes);
+ }
+
+}
\ No newline at end of file