Skip to content

Commit

Permalink
Add native metadata extra API
Browse files Browse the repository at this point in the history
  • Loading branch information
caprica committed Feb 2, 2024
1 parent e4db863 commit 91abecf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 25 deletions.
72 changes: 61 additions & 11 deletions src/main/java/uk/co/caprica/vlcj/media/MetaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@
package uk.co.caprica.vlcj.media;

import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import uk.co.caprica.vlcj.binding.support.strings.NativeString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_get_meta;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_get_meta_extra;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_get_meta_extra_names;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_meta_extra_names_release;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_save_meta;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_set_meta;
import static uk.co.caprica.vlcj.binding.lib.LibVlc.libvlc_media_set_meta_extra;

/**
* Behaviour pertaining to media meta data.
* Behaviour pertaining to media metadata.
*/
public final class MetaApi extends BaseApi {

Expand All @@ -39,27 +47,64 @@ public final class MetaApi extends BaseApi {
}

/**
* Get the value for a particular type of meta data.
* Get the value for a particular type of metadata.
*
* @param meta type of meta data
* @return meta data value
* @param meta type of metadata
* @return meta metadata value
*/
public String get(Meta meta) {
return getMetaValue(libvlc_media_get_meta(mediaInstance, meta.intValue()));
}

/**
* Set the value for a particular type of meta data.
* Set the value for a particular type of metadata.
*
* @param meta type of meta data
* @param meta type of metadata
* @param value meta data value
*/
public void set(Meta meta, String value) {
libvlc_media_set_meta(mediaInstance, meta.intValue(), value);
}

/**
* Save the meta data to the underlying media.
* Get the names of the available extra metadata fields.
*
* @return list of extra metadata field names
*/
public List<String> getExtraNames() {
PointerByReference namesPointer = new PointerByReference();
int namesCount = libvlc_media_get_meta_extra_names(mediaInstance, namesPointer);
List<String> result = new ArrayList<>(namesCount);
Pointer[] namePointers = namesPointer.getPointer().getPointerArray(0L, namesCount);
for (Pointer namePointer : namePointers) {
String name = NativeString.copyNativeString(namePointer);
result.add(getExtra(name));
}
libvlc_media_meta_extra_names_release(namesPointer.getValue(), namesCount);
return result;
}

/**
* Get extra meta data.
*
* @param name name of the extra metadata field to get
* @return metadata value, may be <code>NULL</code>
*/
public String getExtra(String name) {
return getMetaValue(libvlc_media_get_meta_extra(mediaInstance, name));
}

/**
* Set extra meta data.
*
* @param name name of the extra metadata field to set
*/
public void setExtra(String name, String value) {
libvlc_media_set_meta_extra(mediaInstance, name, value);
}

/**
* Save the metadata to the underlying media.
*
* @return <code>true</code> if successful; <code>false</code> on error
*/
Expand All @@ -68,25 +113,30 @@ public boolean save() {
}

/**
* Get all of the meta data values as a {@link MetaData} value lobject.
* Get all the metadata values as a {@link MetaData} value object.
* <p>
* The returned object is immutable and "disconnected" from the underlying media.
*
* @return meta data
*/
public MetaData asMetaData() {
Map<Meta,String> values = new HashMap<Meta,String>(26);
Map<Meta,String> values = new HashMap<Meta,String>(Meta.values().length);
for (Meta meta : Meta.values()) {
String value = get(meta);
if (value != null) {
values.put(meta, value);
}
}
return new MetaData(values);
List<String> extraNames = getExtraNames();
Map<String, String> extraMeta = new TreeMap<>();
for (String extraName : extraNames) {
String extraValue = getExtra((extraName));
extraMeta.put(extraName, extraValue);
}
return new MetaData(values, extraMeta);
}

private String getMetaValue(Pointer pointer) {
return NativeString.copyAndFreeNativeString(pointer);
}

}
55 changes: 41 additions & 14 deletions src/main/java/uk/co/caprica/vlcj/media/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,75 @@
import java.util.Map;

/**
* Immutable meta data value object.
* Immutable metadata value object.
*/
public final class MetaData {

/**
* Collection of meta data values.
* Collection of metadata values.
*/
private final Map<Meta,String> values;
private final Map<Meta, String> values;

/**
* Create a meta data value object.
* Collection of metadata extra values.
*/
private final Map<String, String> extraValues;

/**
* Create a metadata value object.
*
* @param values meta data values
* @param values metadata values
* @param extraValues metadata extra values
*/
public MetaData(Map<Meta,String> values) {
public MetaData(Map<Meta, String> values, Map<String, String> extraValues) {
this.values = Collections.unmodifiableMap(values);
this.extraValues = Collections.unmodifiableMap(extraValues);
}

/**
* Get a particular meta data value.
* Get a particular metadata value.
*
* @param meta meta data type
* @return value meta data value
* @param meta metadata type
* @return value metadata value
*/
public String get(Meta meta) {
return values.get(meta);
}

/**
* Get all of the meta data values.
* Get a particular metadata extra value.
*
* @param name metadata extra name
* @return value metadata extra value
*/
public String get(String name) {
return extraValues.get(name);
}

/**
* Get all the metadata values.
*
* @return copy of the metadata values collection
*/
public Map<Meta, String> values() {
return new HashMap<>(values);
}

/**
* Get all the metadata extra values.
*
* @return copy of the meta data values collection
* @return copy of the metadata exta values collection
*/
public Map<Meta,String> values() {
return new HashMap<Meta,String>(values);
public Map<String, String> extraValues() {
return new HashMap<>(extraValues);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(300);
sb.append(getClass().getSimpleName()).append('[');
sb.append("values=").append(values).append(']');
sb.append("values=").append(values).append(',');
sb.append("extraValues").append(extraValues).append(']');
return sb.toString();
}

Expand Down

0 comments on commit 91abecf

Please sign in to comment.