Skip to content

Commit 9c50f74

Browse files
Merge pull request #235 from codelerity/bus-message-rewrite
Bus message handling rewrite
2 parents 64e9d78 + 211946c commit 9c50f74

File tree

13 files changed

+1131
-934
lines changed

13 files changed

+1131
-934
lines changed

src/org/freedesktop/gstreamer/Bus.java

Lines changed: 514 additions & 485 deletions
Large diffs are not rendered by default.

src/org/freedesktop/gstreamer/glib/NativeEnum.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Neil C Smith
2+
* Copyright (c) 2020 Neil C Smith
33
*
44
* This file is part of gstreamer-java.
55
*
@@ -15,26 +15,57 @@
1515
* You should have received a copy of the GNU Lesser General Public License
1616
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
1918
package org.freedesktop.gstreamer.glib;
2019

2120
/**
2221
* Interface for enums that wrap a native int-based enum.
22+
*
2323
* @param <T> Java enum type
2424
*/
2525
public interface NativeEnum<T extends Enum<T>> {
2626

2727
public int intValue();
2828

29+
/**
30+
* Convert a native int value to the specified NativeEnum type.
31+
*
32+
* @param <T> enum type
33+
* @param type enum class
34+
* @param intValue native int value
35+
* @return enum value
36+
* @throws IllegalArgumentException if no enum value matches the specified
37+
* native int value
38+
*/
2939
public static <T extends Enum<T> & NativeEnum<T>> T fromInt(Class<T> type, int intValue) {
3040
for (T value : type.getEnumConstants()) {
3141
if (value.intValue() == intValue) {
3242
return value;
3343
}
3444
}
3545

36-
throw new IllegalArgumentException("Value " + intValue + " is unacceptable for " +
37-
type.getSimpleName() + " enum");
46+
throw new IllegalArgumentException("Value " + intValue + " is unacceptable for "
47+
+ type.getSimpleName() + " enum");
48+
}
49+
50+
/**
51+
* Convert a native int value to the specified NativeEnum type, allowing for
52+
* a default value (or null) to be returned instead of throwing an exception
53+
* on invalid values.
54+
*
55+
* @param <T> enum type
56+
* @param type enum class
57+
* @param defValue default value to return if no match (may be null)
58+
* @param intValue native int value
59+
* @return enum value
60+
*/
61+
public static <T extends Enum<T> & NativeEnum<T>> T fromInt(Class<T> type, T defValue, int intValue) {
62+
for (T value : type.getEnumConstants()) {
63+
if (value.intValue() == intValue) {
64+
return value;
65+
}
66+
}
67+
68+
return defValue;
3869
}
3970

4071
}

src/org/freedesktop/gstreamer/lowlevel/GstBusAPI.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2020 Neil C Smith
23
* Copyright (c) 2014 Tom Greenwood <[email protected]>
34
* Copyright (c) 2009 Levente Farkas
45
* Copyright (c) 2007, 2008 Wayne Meissner
@@ -54,10 +55,10 @@ public interface GstBusAPI extends com.sun.jna.Library {
5455

5556
void gst_bus_set_flushing(Bus ptr, int flushing);
5657
interface BusCallback extends GstCallback {
57-
boolean callback(Bus bus, Message msg, Pointer data);
58+
boolean callback(GstBusPtr bus, GstMessagePtr msg, Pointer data);
5859
}
5960
public interface BusSyncHandler extends GstCallback {
60-
BusSyncReply callback(Bus bus, Message msg, Pointer userData);
61+
BusSyncReply callback(GstBusPtr bus, GstMessagePtr msg, Pointer userData);
6162
}
6263
NativeLong gst_bus_add_watch(Bus bus, BusCallback function, Pointer data);
6364
void gst_bus_set_sync_handler(Bus bus, BusSyncHandler function, Pointer data, GDestroyNotify destroyCallback);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2020 Neil C Smith
3+
*
4+
* This file is part of gstreamer-java.
5+
*
6+
* This code is free software: you can redistribute it and/or modify it under the terms of the GNU
7+
* Lesser General Public License version 3 only, as published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License version 3 for more details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public License version 3 along with
14+
* this work. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
package org.freedesktop.gstreamer.lowlevel;
17+
18+
import com.sun.jna.Pointer;
19+
20+
/**
21+
* GstBus pointer.
22+
*/
23+
public class GstBusPtr extends GstObjectPtr {
24+
25+
public GstBusPtr() {
26+
}
27+
28+
public GstBusPtr(Pointer ptr) {
29+
super(ptr);
30+
}
31+
32+
}

src/org/freedesktop/gstreamer/lowlevel/GstMessageAPI.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2020 Neil C Smith
23
* Copyright (c) 2014 Tom Greenwood <[email protected]>
34
* Copyright (c) 2009 Levente Farkas
45
* Copyright (c) 2007, 2008 Wayne Meissner
@@ -35,21 +36,24 @@
3536
import org.freedesktop.gstreamer.lowlevel.annotations.Invalidate;
3637

3738
import com.sun.jna.Pointer;
39+
import com.sun.jna.Structure.FieldOrder;
40+
import com.sun.jna.ptr.IntByReference;
41+
import com.sun.jna.ptr.LongByReference;
3842
import com.sun.jna.ptr.PointerByReference;
39-
import java.util.Arrays;
40-
import java.util.List;
4143

4244
/*
4345
* GstMessage functions
4446
*/
4547
public interface GstMessageAPI extends com.sun.jna.Library {
4648
GstMessageAPI GSTMESSAGE_API = GstNative.load(GstMessageAPI.class);
4749

50+
@FieldOrder({"mini_object", "type", "timestamp", "src",
51+
"seqnum", "lock", "cond"})
4852
public static final class MessageStruct extends com.sun.jna.Structure {
4953
public volatile MiniObjectStruct mini_object;
50-
public volatile MessageType type;
54+
public volatile int type;
5155
public volatile long timestamp;
52-
public volatile GstObject src;
56+
public volatile GstObjectPtr src;
5357
public volatile int seqnum;
5458

5559
public volatile Pointer lock;
@@ -63,39 +67,48 @@ public MessageStruct() {
6367
public MessageStruct(Pointer ptr) {
6468
useMemory(ptr);
6569
}
66-
67-
@Override
68-
protected List<String> getFieldOrder() {
69-
return Arrays.asList(new String[]{
70-
"mini_object",
71-
"type", "timestamp", "src",
72-
"seqnum",
73-
"lock", "cond"
74-
});
70+
71+
int typeOffset() {
72+
return fieldOffset("type");
73+
}
74+
75+
int srcOffset() {
76+
return fieldOffset("src");
7577
}
7678
}
7779

7880
GType gst_message_get_type();
7981
String gst_message_type_get_name(MessageType type);
8082
void gst_message_parse_state_changed(Message msg, State[] old, State[] current, State[] pending);
83+
void gst_message_parse_state_changed(GstMessagePtr msg, IntByReference old, IntByReference current, IntByReference pending);
8184
void gst_message_parse_tag(Message msg, PointerByReference tagList);
85+
void gst_message_parse_tag(GstMessagePtr msg, PointerByReference tagList);
8286
void gst_message_parse_clock_provide(Message msg, PointerByReference clock, int[] reader);
8387
void gst_message_parse_new_clock(Message msg, PointerByReference clock);
8488
void gst_message_parse_error(Message msg, PointerByReference err, PointerByReference debug);
89+
void gst_message_parse_error(GstMessagePtr msg, PointerByReference err, PointerByReference debug);
8590
void gst_message_parse_error(Message msg, GErrorStruct[] err, Pointer[] debug);
8691
void gst_message_parse_warning(Message msg, PointerByReference err, PointerByReference debug);
92+
void gst_message_parse_warning(GstMessagePtr msg, PointerByReference err, PointerByReference debug);
8793
void gst_message_parse_warning(Message msg, GErrorStruct[] err, Pointer[] debug);
8894
void gst_message_parse_info(Message msg, PointerByReference err, PointerByReference debug);
95+
void gst_message_parse_info(GstMessagePtr msg, PointerByReference err, PointerByReference debug);
8996
void gst_message_parse_info(Message msg, GErrorStruct[] err, Pointer[] debug);
9097
void gst_message_parse_buffering(Message msg, int[] percent);
98+
void gst_message_parse_buffering(GstMessagePtr msg, IntByReference percent);
9199
void gst_message_parse_segment_start(Message message, Format[] format, long[] position);
100+
void gst_message_parse_segment_start(GstMessagePtr msg, IntByReference format, LongByReference position);
92101
void gst_message_parse_segment_done(Message message, Format[] format, long[] position);
102+
void gst_message_parse_segment_done(GstMessagePtr msg, IntByReference format, LongByReference position);
93103
void gst_message_parse_duration(Message message, Format[] format, long[] position);
104+
void gst_message_parse_duration(GstMessagePtr msg, IntByReference format, LongByReference position);
94105
void gst_message_parse_async_start(Message message, boolean[] new_base_time);
95-
96106
boolean gst_message_parse_context_type(Message message, String[] context_type);
107+
boolean gst_message_parse_context_type(GstMessagePtr msg, PointerByReference /*String*/ context_type);
108+
97109
@CallerOwnsReturn Message gst_message_new_eos(GstObject src);
98110
Pointer ptr_gst_message_new_eos(GstObject src);
111+
@CallerOwnsReturn GstMessagePtr gst_message_new_eos(GstObjectPtr src);
99112
@CallerOwnsReturn Message gst_message_new_error(GstObject src, GErrorStruct error, String debug);
100113
@CallerOwnsReturn Message gst_message_new_warning(GstObject src, GErrorStruct error, String debug);
101114
@CallerOwnsReturn Message gst_message_new_info(GstObject src, GErrorStruct error, String debug);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2020 Neil C Smith
3+
*
4+
* This file is part of gstreamer-java.
5+
*
6+
* This code is free software: you can redistribute it and/or modify it under the terms of the GNU
7+
* Lesser General Public License version 3 only, as published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License version 3 for more details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public License version 3 along with
14+
* this work. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
package org.freedesktop.gstreamer.lowlevel;
17+
18+
import com.sun.jna.Pointer;
19+
20+
/**
21+
* GstMessage pointer.
22+
*/
23+
public class GstMessagePtr extends GstMiniObjectPtr {
24+
25+
private static final int TYPE_OFFSET;
26+
private static final int SRC_OFFSET;
27+
28+
static {
29+
GstMessageAPI.MessageStruct struct = new GstMessageAPI.MessageStruct();
30+
TYPE_OFFSET = struct.typeOffset();
31+
SRC_OFFSET = struct.srcOffset();
32+
}
33+
34+
35+
public GstMessagePtr() {
36+
}
37+
38+
public GstMessagePtr(Pointer ptr) {
39+
super(ptr);
40+
}
41+
42+
public int getMessageType() {
43+
return getPointer().getInt(TYPE_OFFSET);
44+
}
45+
46+
public GstObjectPtr getSource() {
47+
Pointer raw = getPointer().getPointer(SRC_OFFSET);
48+
return raw == null ? null : new GstObjectPtr(raw);
49+
}
50+
51+
52+
}

src/org/freedesktop/gstreamer/lowlevel/GstMiniObjectAPI.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package org.freedesktop.gstreamer.lowlevel;
2323

24+
import com.sun.jna.Callback;
2425
import java.util.Arrays;
2526
import java.util.List;
2627

@@ -41,6 +42,10 @@ public interface GstMiniObjectAPI extends com.sun.jna.Library {
4142
void gst_mini_object_ref(GstMiniObjectPtr ptr);
4243
void gst_mini_object_unref(GstMiniObjectPtr ptr);
4344
void gst_mini_object_unref(Pointer ptr);
45+
void gst_mini_object_weak_ref(GstMiniObjectPtr ptr, GstMiniObjectNotify notify,
46+
IntPtr data);
47+
void gst_mini_object_weak_unref(GstMiniObjectPtr ptr, GstMiniObjectNotify notify,
48+
IntPtr data);
4449

4550
@CallerOwnsReturn MiniObject gst_mini_object_make_writable(@Invalidate MiniObject mini_object);
4651
@CallerOwnsReturn MiniObject gst_mini_object_copy(MiniObject mini_object);
@@ -50,6 +55,12 @@ public interface GstMiniObjectAPI extends com.sun.jna.Library {
5055
Pointer gst_mini_object_get_qdata(MiniObject mini_object, GQuark quark);
5156
Pointer gst_mini_object_steal_qdata(MiniObject mini_object, GQuark quark);
5257

58+
public static interface GstMiniObjectNotify extends Callback {
59+
60+
public void callback(IntPtr userData, Pointer obj);
61+
62+
}
63+
5364
public static final class MiniObjectStruct extends com.sun.jna.Structure {
5465
public volatile GType type;
5566
public volatile int refcount;

0 commit comments

Comments
 (0)