Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix WebRTCBin.createOffer/createAnswer; Support for WebRTC DataChannel (string/data) #271

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/org/freedesktop/gstreamer/glib/GBytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023 Jan Weber
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.freedesktop.gstreamer.glib;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.freedesktop.gstreamer.lowlevel.GPointer;

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

/**
* Wrapper to the GBytes data structure,
* see <a href="https://docs.gtk.org/glib/struct.Bytes.html">https://docs.gtk.org/glib/struct.Bytes.html</a>
*/
public class GBytes extends RefCountedObject {

public static final String GTYPE_NAME = "GBytes";

GBytes(Initializer init) {
super(new GBytes.Handle(init.ptr, init.ownsHandle), init.needRef);
}

GBytes(Handle handle) {
super(handle);
}

public int getSize() {
return GLIB_API.g_bytes_get_size(getRawPointer());
}

public byte[] getBytes() {
IntByReference size = new IntByReference();
Pointer bytes = GLIB_API.g_bytes_get_data(getRawPointer(), size);
return bytes != null ? bytes.getByteArray(0, size.getValue()) : new byte[0];
}

public static GBytes createInstance(byte[] bytes) {
Pointer source = new Memory(bytes.length);
source.write(0, bytes, 0, bytes.length);
Pointer ptr = GLIB_API.g_bytes_new(source, bytes.length);
return new GBytes(new Handle(new GPointer(ptr), true));
}

private static final class Handle extends RefCountedObject.Handle {

Handle(GPointer ptr, boolean ownsHandle) {
super(ptr, ownsHandle);
}

@Override
protected void disposeNativeHandle(GPointer ptr) {
GLIB_API.g_bytes_unref(ptr.getPointer());
}

@Override
protected void ref() {
GLIB_API.g_bytes_ref(getPointer().getPointer());
}

@Override
protected void unref() {
GLIB_API.g_bytes_unref(getPointer().getPointer());
}

}

}
1 change: 1 addition & 0 deletions src/org/freedesktop/gstreamer/glib/GLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public static class Types implements NativeObject.TypeProvider {
@Override
public Stream<NativeObject.TypeRegistration<?>> types() {
return Stream.of(
registration(GBytes.class, GBytes.GTYPE_NAME, GBytes::new),
registration(GDate.class, GDate.GTYPE_NAME, GDate::new),
registration(GInetAddress.class, GInetAddress.GTYPE_NAME, GInetAddress::new),
registration(GSocket.class, GSocket.GTYPE_NAME, GSocket::new),
Expand Down
8 changes: 8 additions & 0 deletions src/org/freedesktop/gstreamer/lowlevel/GlibAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;

import com.sun.jna.ptr.IntByReference;
import org.freedesktop.gstreamer.lowlevel.annotations.CallerOwnsReturn;

import com.sun.jna.Callback;
Expand Down Expand Up @@ -122,6 +123,13 @@ int g_timeout_add_full(int priority, int interval, GSourceFunc function,
int g_date_get_day(Pointer date);
void g_date_free(Pointer date);

Pointer g_bytes_new(Pointer source, int size);
Pointer g_bytes_get_data(Pointer bytes, IntByReference size);
int g_bytes_get_size(Pointer bytes);
Pointer g_bytes_ref(Pointer bytes);
void g_bytes_unref(Pointer bytes);


GList g_list_append(GList list, Pointer data);

public static final class GList extends com.sun.jna.Structure {
Expand Down
3 changes: 3 additions & 0 deletions src/org/freedesktop/gstreamer/webrtc/WebRTC.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static class Types implements NativeObject.TypeProvider {
@Override
public Stream<NativeObject.TypeRegistration<?>> types() {
return Stream.of(
registration(WebRTCDataChannel.class,
WebRTCDataChannel.GTYPE_NAME,
WebRTCDataChannel::new),
registration(WebRTCSessionDescription.class,
WebRTCSessionDescription.GTYPE_NAME,
WebRTCSessionDescription::new),
Expand Down
51 changes: 46 additions & 5 deletions src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand Down Expand Up @@ -148,6 +149,7 @@ public void onChange(Promise promise) {
}
});
emit("create-offer", null, promise);
promiseCreateOffer = promise;
}

/**
Expand All @@ -172,6 +174,7 @@ public void onChange(Promise promise) {
}
});
emit("create-answer", null, promise);
promiseCreateAnswer = promise;
}

/**
Expand Down Expand Up @@ -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
* <p>
* Should be called once this {@link WebRTCBin} is in the READY state. Otherwise an error will be thrown
* <p>
* @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 <code>on-data-channel</code> 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);
}
});
}
}
137 changes: 137 additions & 0 deletions src/org/freedesktop/gstreamer/webrtc/WebRTCDataChannel.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}

}