Skip to content

Commit ad60d02

Browse files
Update Message and lowlevel Message API with GstMessagePtr.
Add GstMessagePtr and direct access to source obj and type without instantiation. Add parse methods in GstMessageAPI using typed pointers. Update Message with Handle subclass and use of new API for Message construction. Add method to return a default value (or null) from NativeEnum conversion rather than throw exception.
1 parent e38ac1e commit ad60d02

File tree

4 files changed

+146
-30
lines changed

4 files changed

+146
-30
lines changed

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/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/message/Message.java

Lines changed: 32 additions & 12 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
* Copyright (c) 2007, 2008 Wayne Meissner
44
* Copyright (C) 2004 Wim Taymans <[email protected]>
55
*
@@ -26,8 +26,9 @@
2626
import org.freedesktop.gstreamer.GstObject;
2727
import org.freedesktop.gstreamer.MiniObject;
2828
import org.freedesktop.gstreamer.Structure;
29+
import org.freedesktop.gstreamer.glib.NativeEnum;
2930
import org.freedesktop.gstreamer.glib.Natives;
30-
import org.freedesktop.gstreamer.lowlevel.GstMessageAPI;
31+
import org.freedesktop.gstreamer.lowlevel.GstMessagePtr;
3132
import org.freedesktop.gstreamer.lowlevel.ReferenceManager;
3233
import org.freedesktop.gstreamer.lowlevel.annotations.HasSubtype;
3334

@@ -82,16 +83,20 @@ public class Message extends MiniObject {
8283
TYPE_MAP.put(MessageType.NEED_CONTEXT, NeedContextMessage::new);
8384
}
8485

85-
private final GstMessageAPI.MessageStruct messageStruct;
86+
private final Handle handle;
8687

8788
/**
8889
* Creates a new instance of Message.
8990
*
9091
* @param init internal initialization data.
9192
*/
9293
Message(Initializer init) {
93-
super(init);
94-
messageStruct = new GstMessageAPI.MessageStruct(init.ptr.getPointer());
94+
this(new Handle(init.ptr.as(GstMessagePtr.class, GstMessagePtr::new), init.ownsHandle), init.needRef);
95+
}
96+
97+
Message(Handle handle, boolean needRef) {
98+
super(handle, needRef);
99+
this.handle = handle;
95100
}
96101

97102
/**
@@ -100,7 +105,7 @@ public class Message extends MiniObject {
100105
* @return the element that posted the message.
101106
*/
102107
public GstObject getSource() {
103-
return (GstObject) messageStruct.readField("src");
108+
return Natives.objectFor(handle.getPointer().getSource(), GstObject.class, true, true);
104109
}
105110

106111
/**
@@ -118,16 +123,18 @@ public Structure getStructure() {
118123
* @return the message type.
119124
*/
120125
public MessageType getType() {
121-
MessageType type = (MessageType) messageStruct.readField("type");
122-
return type;
126+
return NativeEnum.fromInt(MessageType.class, MessageType.UNKNOWN,
127+
handle.getPointer().getMessageType());
123128
}
124129

125130
private static Message create(Initializer init) {
126-
GstMessageAPI.MessageStruct struct = new GstMessageAPI.MessageStruct(init.ptr.getPointer());
127-
MessageType type = (MessageType) struct.readField("type");
131+
MessageType type = NativeEnum.fromInt(MessageType.class,
132+
MessageType.UNKNOWN,
133+
init.ptr.as(GstMessagePtr.class, GstMessagePtr::new).getMessageType()
134+
);
128135
return TYPE_MAP.getOrDefault(type, Message::new).apply(init);
129136
}
130-
137+
131138
public static class Types implements TypeProvider {
132139

133140
@Override
@@ -136,7 +143,20 @@ public Stream<TypeRegistration<?>> types() {
136143
Natives.registration(Message.class, GTYPE_NAME, Message::create)
137144
);
138145
}
139-
146+
147+
}
148+
149+
static class Handle extends MiniObject.Handle {
150+
151+
Handle(GstMessagePtr ptr, boolean ownsHandle) {
152+
super(ptr, ownsHandle);
153+
}
154+
155+
@Override
156+
protected GstMessagePtr getPointer() {
157+
return (GstMessagePtr) super.getPointer();
158+
}
159+
140160
}
141161

142162
}

0 commit comments

Comments
 (0)