Skip to content

Structure: Add Accessor for GstValueList Fields #162

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

Merged
merged 4 commits into from
Jun 10, 2019
Merged
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
42 changes: 32 additions & 10 deletions src/org/freedesktop/gstreamer/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@

import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.freedesktop.gstreamer.lowlevel.GType;
import org.freedesktop.gstreamer.lowlevel.GValueAPI;
import org.freedesktop.gstreamer.glib.NativeObject;
import org.freedesktop.gstreamer.glib.Natives;
import org.freedesktop.gstreamer.lowlevel.GPointer;
import org.freedesktop.gstreamer.lowlevel.GType;
import org.freedesktop.gstreamer.lowlevel.GValueAPI;
import org.freedesktop.gstreamer.lowlevel.GValueAPI.GValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.freedesktop.gstreamer.lowlevel.GValueAPI.GVALUE_API;
import static org.freedesktop.gstreamer.lowlevel.GstStructureAPI.GSTSTRUCTURE_API;
import static org.freedesktop.gstreamer.lowlevel.GstValueAPI.GSTVALUE_API;

Expand Down Expand Up @@ -210,7 +211,7 @@ public double[] getDoubles(String fieldName, double[] array) {
}
}
}

/**
* Get the number of fields in the {@link Structure}.
*
Expand Down Expand Up @@ -435,16 +436,37 @@ public Object getValue(String fieldName) {
* Throws {@link InvalidFieldException} if the field does not exist, or the
* field values cannot be converted to type T.
* <p>
* This method only currently supports lists of values inside a GValueArray
* - other native list types will be supported in future.
* This method currently supports lists of values inside a GValueArray or
* GstValueList.
*
* @param <T>
* @param type type of values
* @param fieldName name of field
* @return List of values from the named field
*/
public <T> List<T> getValues(Class<T> type, String fieldName) {
Object val = getValue(fieldName);
GValue gValue = GSTSTRUCTURE_API.gst_structure_get_value(this, fieldName);
if (gValue == null) {
throw new InvalidFieldException(type.getSimpleName(), fieldName);
}

GType gType = gValue.getType();
if (gType.equals(GSTVALUE_API.gst_value_list_get_type())) {
int size = GSTVALUE_API.gst_value_list_get_size(gValue);
ArrayList<T> values = new ArrayList<>(size);
for (int i = 0; i <size; i++) {
Object o = GSTVALUE_API.gst_value_list_get_value(gValue, i).getValue();
if (type.isInstance(o)) {
values.add(type.cast(o));
} else {
throw new InvalidFieldException(type.getSimpleName(), fieldName);
}
}

return values;
}

Object val = gValue.getValue();
if (val instanceof GValueAPI.GValueArray) {
GValueAPI.GValueArray arr = (GValueAPI.GValueArray) val;
int count = arr.getNValues();
Expand Down
28 changes: 25 additions & 3 deletions test/org/freedesktop/gstreamer/StructureTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.freedesktop.gstreamer;

import java.util.List;
import static org.junit.Assert.*;

import org.freedesktop.gstreamer.lowlevel.GType;
import org.freedesktop.gstreamer.lowlevel.GValueAPI;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

public class StructureTest {
private Structure structure;

Expand Down Expand Up @@ -151,4 +153,24 @@ public void testFraction() {
assertEquals(17, structure.getFraction("fraction").getNumerator());
assertEquals(10, structure.getFraction("fraction").getDenominator());
}

@Test
public void testValueListInteger() {
Caps caps = Caps.fromString("audio/x-raw,rate={44100,48000}");
List<Integer> rates = caps.getStructure(0).getValues(Integer.class, "rate");
assertEquals(Arrays.asList(44100, 48000), rates);
}

@Test
public void testValueListStrings() {
Caps caps = Caps.fromString("video/x-raw,format={RGB, BGR, RGBx, BGRx}");
List<String> formats = caps.getStructure(0).getValues(String.class, "format");
assertEquals(Arrays.asList("RGB", "BGR", "RGBx","BGRx"), formats);
}

@Test(expected = Structure.InvalidFieldException.class)
public void testValueListChecksType() {
Caps caps = Caps.fromString("video/x-raw,format={RGB, BGR, RGBx, BGRx}");
caps.getStructure(0).getValues(Integer.class, "format");
}
}