Skip to content

Commit 43be942

Browse files
Merge pull request #220 from codelerity/structure-register
Register Structure like other types.
2 parents 3a2d685 + d65e67b commit 43be942

File tree

6 files changed

+149
-68
lines changed

6 files changed

+149
-68
lines changed

src/org/freedesktop/gstreamer/Gst.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ public Stream<NativeObject.TypeRegistration<?>> types() {
693693
registration(Registry.class, Registry.GTYPE_NAME, Registry::new),
694694
registration(SDPMessage.class, SDPMessage.GTYPE_NAME, SDPMessage::new),
695695
registration(Sample.class, Sample.GTYPE_NAME, Sample::new),
696+
registration(Structure.class, Structure.GTYPE_NAME, Structure::new),
696697
registration(TagList.class, TagList.GTYPE_NAME, TagList::new)
697698
);
698699
}

src/org/freedesktop/gstreamer/Structure.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
* @see Event
6969
*/
7070
public class Structure extends NativeObject {
71-
71+
72+
public static final String GTYPE_NAME = "GstStructure";
7273

7374
/**
7475
* Creates a new, empty #GstStructure with the given name.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ public Object get(String property) {
201201
Class<? extends NativeObject> cls = GstTypes.classFor(propType);
202202
if (cls != null) {
203203
Pointer ptr = GVALUE_API.g_value_get_boxed(propValue);
204-
final GPointer gptr = GObject.class.isAssignableFrom(cls) ? new GObjectPtr(ptr)
205-
: MiniObject.class.isAssignableFrom(cls) ? new GstMiniObjectPtr(ptr)
206-
: new GPointer(ptr);
207-
return objectFor(gptr, cls, -1, true);
204+
return Natives.objectFor(ptr, cls, true, true);
208205
}
209206
}
210207
throw new IllegalArgumentException("Unknown conversion from GType=" + propType);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.ServiceLoader;
2020
import java.util.function.Function;
2121
import org.freedesktop.gstreamer.MiniObject;
22+
import org.freedesktop.gstreamer.Structure;
2223
import org.freedesktop.gstreamer.lowlevel.GObjectPtr;
2324
import org.freedesktop.gstreamer.lowlevel.GPointer;
2425
import org.freedesktop.gstreamer.lowlevel.GstMiniObjectPtr;
26+
import org.freedesktop.gstreamer.lowlevel.GstStructurePtr;
2527

2628
/**
2729
* <b>Here be Dragons!</b>
@@ -159,6 +161,7 @@ public static <T extends NativeObject> T callerOwnsReturn(GPointer ptr, Class<T>
159161
private static <T extends NativeObject> T objectFor(Pointer ptr, Class<T> cls, int refAdjust, boolean ownsHandle) {
160162
final GPointer gptr = GObject.class.isAssignableFrom(cls) ? new GObjectPtr(ptr)
161163
: MiniObject.class.isAssignableFrom(cls) ? new GstMiniObjectPtr(ptr)
164+
: Structure.class.isAssignableFrom(cls) ? new GstStructurePtr(ptr)
162165
: new GPointer(ptr);
163166
return NativeObject.objectFor(gptr, cls, refAdjust, ownsHandle);
164167
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Native;
19+
import com.sun.jna.Pointer;
20+
21+
/**
22+
* GstStructure pointer
23+
*/
24+
public class GstStructurePtr extends GTypedPtr {
25+
26+
public GstStructurePtr() {
27+
}
28+
29+
public GstStructurePtr(Pointer ptr) {
30+
super(ptr);
31+
}
32+
33+
@Override
34+
public GType getGType() {
35+
// Quick getter for GType without allocation
36+
if (Native.SIZE_T_SIZE == 8) {
37+
return GType.valueOf(getPointer().getLong(0));
38+
} else if (Native.SIZE_T_SIZE == 4) {
39+
return GType.valueOf( ((long) getPointer().getInt(0)) & 0xffffffffL );
40+
} else {
41+
throw new IllegalStateException("SIZE_T size not supported: " + Native.SIZE_T_SIZE);
42+
}
43+
}
44+
45+
}
Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Copyright (c) 2020 Neil C Smith
3+
* Copyright (c) 2009 Levente Farkas
4+
* Copyright (C) 2009 Tamas Korodi <[email protected]>
5+
* Copyright (C) 2007 Wayne Meissner
6+
*
7+
* This code is free software: you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3 only, as
9+
* published by the Free Software Foundation.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14+
* version 3 for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
18+
*/
119
package org.freedesktop.gstreamer;
220

321
import org.freedesktop.gstreamer.glib.GCancellable;
@@ -11,50 +29,53 @@
1129

1230
import java.util.Arrays;
1331
import java.util.List;
32+
import org.freedesktop.gstreamer.util.TestAssumptions;
1433

1534
import static org.junit.Assert.*;
1635

1736
public class StructureTest {
18-
private Structure structure;
19-
37+
38+
private Structure structure;
39+
2040
@BeforeClass
2141
public static void setUpClass() throws Exception {
22-
Gst.init("StructureTest", new String[] {});
42+
Gst.init(Gst.getVersion(), "StructureTest");
2343
}
24-
44+
2545
@AfterClass
2646
public static void tearDownClass() throws Exception {
2747
Gst.deinit();
2848
}
2949

3050
@Before
3151
public void setUp() {
32-
structure = new Structure("nazgul");
52+
structure = new Structure("nazgul");
3353
}
34-
35-
@Test
36-
public void testGetName() {
37-
assertEquals("nazgul", structure.getName());
38-
}
39-
40-
@Test
41-
public void testGetValue() {
42-
structure.setValue("uint", GType.UINT, 9);
43-
assertEquals(9, structure.getValue("uint"));
44-
45-
try {
46-
structure.getValue("noexist");
47-
fail("Structure.InvalidFieldException should have been thrown");
48-
} catch (Structure.InvalidFieldException e) {}
49-
50-
structure.setDouble("double", 9.0);
51-
assertEquals(9.0, structure.getValue("double"));
52-
53-
structure.setValue("bool", GType.BOOLEAN, true);
54-
assertEquals(true, structure.getValue("bool"));
55-
56-
}
57-
54+
55+
@Test
56+
public void testGetName() {
57+
assertEquals("nazgul", structure.getName());
58+
}
59+
60+
@Test
61+
public void testGetValue() {
62+
structure.setValue("uint", GType.UINT, 9);
63+
assertEquals(9, structure.getValue("uint"));
64+
65+
try {
66+
structure.getValue("noexist");
67+
fail("Structure.InvalidFieldException should have been thrown");
68+
} catch (Structure.InvalidFieldException e) {
69+
}
70+
71+
structure.setDouble("double", 9.0);
72+
assertEquals(9.0, structure.getValue("double"));
73+
74+
structure.setValue("bool", GType.BOOLEAN, true);
75+
assertEquals(true, structure.getValue("bool"));
76+
77+
}
78+
5879
@Test
5980
public void testGetValues() {
6081
GValueAPI.GValueArray ar = new GValueAPI.GValueArray(2);
@@ -75,16 +96,16 @@ public void testGetValues() {
7596
} catch (Structure.InvalidFieldException ex) {
7697
}
7798
}
78-
79-
@Test
80-
public void testGetInteger() {
81-
structure.setInteger("int", 9);
82-
assertEquals(9, structure.getInteger("int"));
83-
84-
structure.setInteger("int", -9);
85-
assertEquals(-9, structure.getInteger("int"));
86-
}
87-
99+
100+
@Test
101+
public void testGetInteger() {
102+
structure.setInteger("int", 9);
103+
assertEquals(9, structure.getInteger("int"));
104+
105+
structure.setInteger("int", -9);
106+
assertEquals(-9, structure.getInteger("int"));
107+
}
108+
88109
@Test
89110
public void testGetIntegers() {
90111
GValueAPI.GValueArray ar = new GValueAPI.GValueArray(2);
@@ -96,28 +117,28 @@ public void testGetIntegers() {
96117
assertTrue(in == ints);
97118
assertEquals(32, ints[0]);
98119
assertEquals(-49, ints[1]);
99-
120+
100121
in = new int[1];
101122
ints = structure.getIntegers("integers", in);
102123
assertFalse(in == ints);
103124
assertEquals(32, ints[0]);
104125
assertEquals(-49, ints[1]);
105-
126+
106127
structure.setInteger("single_integer", 18);
107128
int[] single = structure.getIntegers("single_integer", in);
108129
assertTrue(in == single);
109130
assertEquals(18, single[0]);
110131
}
111132

112-
@Test
113-
public void testGetDouble() {
114-
structure.setDouble("double", 9.0);
115-
assertEquals(9.0, structure.getDouble("double"), 0);
116-
117-
structure.setDouble("double", -9.0);
118-
assertEquals(-9.0, structure.getDouble("double"), 0);
119-
}
120-
133+
@Test
134+
public void testGetDouble() {
135+
structure.setDouble("double", 9.0);
136+
assertEquals(9.0, structure.getDouble("double"), 0);
137+
138+
structure.setDouble("double", -9.0);
139+
assertEquals(-9.0, structure.getDouble("double"), 0);
140+
}
141+
121142
@Test
122143
public void testGetDoubles() {
123144
GValueAPI.GValueArray ar = new GValueAPI.GValueArray(2);
@@ -129,32 +150,32 @@ public void testGetDoubles() {
129150
assertTrue(in == doubles);
130151
assertEquals(3.25, doubles[0], 0.001);
131152
assertEquals(79.6, doubles[1], 0.001);
132-
153+
133154
in = new double[1];
134155
doubles = structure.getDoubles("doubles", in);
135156
assertFalse(in == doubles);
136157
assertEquals(3.25, doubles[0], 0.001);
137158
assertEquals(79.6, doubles[1], 0.001);
138-
159+
139160
structure.setDouble("single_double", 18.2);
140161
double[] single = structure.getDoubles("single_double", in);
141162
assertTrue(in == single);
142163
assertEquals(18.2, single[0], 0.001);
143164
}
144165

145-
@Test
146-
public void testFraction() {
147-
structure.setFraction("fraction", 10, 1);
166+
@Test
167+
public void testFraction() {
168+
structure.setFraction("fraction", 10, 1);
148169

149-
assertEquals(true, structure.hasField("fraction"));
170+
assertEquals(true, structure.hasField("fraction"));
150171

151-
assertEquals(10, structure.getFraction("fraction").getNumerator());
152-
assertEquals(1, structure.getFraction("fraction").getDenominator());
172+
assertEquals(10, structure.getFraction("fraction").getNumerator());
173+
assertEquals(1, structure.getFraction("fraction").getDenominator());
153174

154-
structure.setFraction("fraction", 17, 10);
155-
assertEquals(17, structure.getFraction("fraction").getNumerator());
156-
assertEquals(10, structure.getFraction("fraction").getDenominator());
157-
}
175+
structure.setFraction("fraction", 17, 10);
176+
assertEquals(17, structure.getFraction("fraction").getNumerator());
177+
assertEquals(10, structure.getFraction("fraction").getDenominator());
178+
}
158179

159180
@Test
160181
public void testValueListInteger() {
@@ -167,7 +188,7 @@ public void testValueListInteger() {
167188
public void testValueListStrings() {
168189
Caps caps = Caps.fromString("video/x-raw,format={RGB, BGR, RGBx, BGRx}");
169190
List<String> formats = caps.getStructure(0).getValues(String.class, "format");
170-
assertEquals(Arrays.asList("RGB", "BGR", "RGBx","BGRx"), formats);
191+
assertEquals(Arrays.asList("RGB", "BGR", "RGBx", "BGRx"), formats);
171192
}
172193

173194
@Test(expected = Structure.InvalidFieldException.class)
@@ -182,13 +203,15 @@ public void testSetMistypedObject() {
182203
structure.setObject("whatever", Caps.GTYPE_NAME, notACapsInstance);
183204
}
184205

206+
@Test
185207
public void testSetUntypedObject() {
186208
GCancellable anyKindOfObject = new GCancellable();
187209
structure.setObject("whatever", GType.OBJECT.getTypeName(), anyKindOfObject);
188210
Object value = structure.getValue("whatever");
189211
Assert.assertSame(anyKindOfObject, value);
190212
}
191213

214+
@Test
192215
public void testSetObject() {
193216
GCancellable anyKindOfObject = new GCancellable();
194217
structure.setObject("whatever", GCancellable.GTYPE_NAME, anyKindOfObject);
@@ -202,4 +225,15 @@ public void testSetNullObject() {
202225
Object value = structure.getValue("whatever");
203226
Assert.assertNull(value);
204227
}
228+
229+
@Test
230+
public void testIssue173() {
231+
TestAssumptions.requireGstVersion(1, 16);
232+
TestAssumptions.requireElement("srtsink");
233+
234+
Element srtsink = ElementFactory.make("srtsink", "srtsink");
235+
srtsink.set("uri", "srt://:8888/");
236+
Object stats = srtsink.get("stats");
237+
assertTrue(stats instanceof Structure);
238+
}
205239
}

0 commit comments

Comments
 (0)