all: break off musikr
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
# For more information about using CMake with Android Studio, read the
|
||||
# documentation: https://d.android.com/studio/projects/add-native-code.html.
|
||||
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
|
||||
|
||||
# Sets the minimum CMake version required for this project.
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
|
||||
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
|
||||
# Since this is the top level CMakeLists.txt, the project name is also accessible
|
||||
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
|
||||
# build script scope).
|
||||
project("ktaglib")
|
||||
|
||||
# Creates and names a library, sets it as either STATIC
|
||||
# or SHARED, and provides the relative paths to its source code.
|
||||
# You can define multiple libraries, and CMake builds them for you.
|
||||
# Gradle automatically packages shared libraries with your APK.
|
||||
#
|
||||
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
|
||||
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
|
||||
# is preferred for the same purpose.
|
||||
#
|
||||
# In order to load a library into your app from Java/Kotlin, you must call
|
||||
# System.loadLibrary() and pass the name of the library defined here;
|
||||
# for GameActivity/NativeActivity derived applications, the same library name must be
|
||||
# used in the AndroidManifest.xml file.
|
||||
set(taglib_location "${CMAKE_CURRENT_SOURCE_DIR}/taglib")
|
||||
set(taglib_pkg "${taglib_location}/pkg/${ANDROID_ABI}")
|
||||
set(taglib_lib "${taglib_pkg}/lib")
|
||||
set(taglib_include "${taglib_pkg}/include")
|
||||
|
||||
set(taglib_file_name libtag.a)
|
||||
set(taglib_file_path ${taglib_lib}/${taglib_file_name})
|
||||
set(taglib_lib_name, "taglib")
|
||||
add_library(
|
||||
"taglib"
|
||||
STATIC
|
||||
IMPORTED)
|
||||
set_target_properties(
|
||||
"taglib" PROPERTIES
|
||||
IMPORTED_LOCATION
|
||||
${taglib_file_path}
|
||||
INTERFACE_INCLUDE_DIRECTORIES
|
||||
${taglib_include})
|
||||
|
||||
add_library(${CMAKE_PROJECT_NAME} SHARED
|
||||
# List C/C++ source files with relative paths to this CMakeLists.txt.
|
||||
ktaglib.cpp
|
||||
JVMInputStream.cpp
|
||||
JVMTagMap.cpp
|
||||
JVMMetadataBuilder.cpp
|
||||
)
|
||||
|
||||
# Specifies libraries CMake should link to your target library. You
|
||||
# can link libraries from various origins, such as libraries defined in this
|
||||
# build script, prebuilt third-party libraries, or Android system libraries.
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
# List libraries link to the target library
|
||||
android
|
||||
log
|
||||
taglib)
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#include "JVMInputStream.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
// TODO: Handle stream exceptions
|
||||
|
||||
JVMInputStream::JVMInputStream(JNIEnv *env, jobject inputStream) :
|
||||
env(env), inputStream(inputStream) {
|
||||
if (!env->IsInstanceOf(inputStream, env->FindClass("org/oxycblt/ktaglib/NativeInputStream"))) {
|
||||
throw std::runtime_error("oStream is not an instance of TagLibOStream");
|
||||
}
|
||||
jclass inputStreamClass = env->FindClass("org/oxycblt/ktaglib/NativeInputStream");
|
||||
inputStreamNameMethod = env->GetMethodID(inputStreamClass, "name", "()Ljava/lang/String;");
|
||||
inputStreamReadBlockMethod = env->GetMethodID(inputStreamClass, "readBlock", "(J)[B");
|
||||
inputStreamIsOpenMethod = env->GetMethodID(inputStreamClass, "isOpen", "()Z");
|
||||
inputStreamSeekFromBeginningMethod = env->GetMethodID(inputStreamClass, "seekFromBeginning",
|
||||
"(J)V");
|
||||
inputStreamSeekFromCurrentMethod = env->GetMethodID(inputStreamClass, "seekFromCurrent",
|
||||
"(J)V");
|
||||
inputStreamSeekFromEndMethod = env->GetMethodID(inputStreamClass, "seekFromEnd", "(J)V");
|
||||
inputStreamClearMethod = env->GetMethodID(inputStreamClass, "clear", "()V");
|
||||
inputStreamTellMethod = env->GetMethodID(inputStreamClass, "tell", "()J");
|
||||
inputStreamLengthMethod = env->GetMethodID(inputStreamClass, "length", "()J");
|
||||
env->DeleteLocalRef(inputStreamClass);
|
||||
}
|
||||
|
||||
JVMInputStream::~JVMInputStream() {
|
||||
// The implicit assumption is that inputStream is managed by the owner,
|
||||
// so we don't need to delete any references here
|
||||
}
|
||||
|
||||
TagLib::FileName JVMInputStream::name() const {
|
||||
auto name = (jstring) env->CallObjectMethod(inputStream, inputStreamNameMethod);
|
||||
const char *nameChars = env->GetStringUTFChars(name, nullptr);
|
||||
auto fileName = TagLib::FileName(nameChars);
|
||||
env->ReleaseStringUTFChars(name, nameChars);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
TagLib::ByteVector JVMInputStream::readBlock(size_t length) {
|
||||
auto data = (jbyteArray) env->CallObjectMethod(inputStream, inputStreamReadBlockMethod, length);
|
||||
jsize dataLength = env->GetArrayLength(data);
|
||||
auto dataBytes = env->GetByteArrayElements(data, nullptr);
|
||||
TagLib::ByteVector byteVector(reinterpret_cast<const char *>(dataBytes), dataLength);
|
||||
env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
|
||||
return byteVector;
|
||||
}
|
||||
|
||||
void JVMInputStream::writeBlock(const TagLib::ByteVector &data) {
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
void JVMInputStream::insert(const TagLib::ByteVector &data,
|
||||
TagLib::offset_t start, size_t replace) {
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
void JVMInputStream::removeBlock(TagLib::offset_t start, size_t length) {
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
|
||||
bool JVMInputStream::readOnly() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JVMInputStream::isOpen() const {
|
||||
return env->CallBooleanMethod(inputStream, inputStreamIsOpenMethod);
|
||||
}
|
||||
|
||||
void JVMInputStream::seek(TagLib::offset_t offset, Position p) {
|
||||
auto joffset = static_cast<jlong>(std::llround(offset));
|
||||
switch (p) {
|
||||
case Beginning:
|
||||
env->CallVoidMethod(inputStream, inputStreamSeekFromBeginningMethod, joffset);
|
||||
break;
|
||||
case Current:
|
||||
env->CallVoidMethod(inputStream, inputStreamSeekFromCurrentMethod, joffset);
|
||||
break;
|
||||
case End:
|
||||
env->CallVoidMethod(inputStream, inputStreamSeekFromEndMethod, joffset);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void JVMInputStream::clear() {
|
||||
env->CallVoidMethod(inputStream, inputStreamClearMethod);
|
||||
}
|
||||
|
||||
TagLib::offset_t JVMInputStream::tell() const {
|
||||
jlong jposition = env->CallLongMethod(inputStream, inputStreamTellMethod);
|
||||
return static_cast<TagLib::offset_t>(jposition);
|
||||
}
|
||||
|
||||
TagLib::offset_t JVMInputStream::length() {
|
||||
jlong jlength = env->CallLongMethod(inputStream, inputStreamLengthMethod);
|
||||
return static_cast<TagLib::offset_t>(jlength);
|
||||
}
|
||||
|
||||
void JVMInputStream::truncate(TagLib::offset_t length) {
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#ifndef AUXIO_JVMINPUTSTREAM_H
|
||||
#define AUXIO_JVMINPUTSTREAM_H
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "taglib/tiostream.h"
|
||||
|
||||
class JVMInputStream : public TagLib::IOStream {
|
||||
public:
|
||||
JVMInputStream(JNIEnv *env, jobject inputStream);
|
||||
|
||||
~JVMInputStream();
|
||||
|
||||
JVMInputStream(const JVMInputStream &) = delete;
|
||||
JVMInputStream &operator=(const JVMInputStream &) = delete;
|
||||
|
||||
/*!
|
||||
* Returns the stream name in the local file system encoding.
|
||||
*/
|
||||
TagLib::FileName name() const override;
|
||||
|
||||
/*!
|
||||
* Reads a block of size \a length at the current get pointer.
|
||||
*/
|
||||
TagLib::ByteVector readBlock(size_t length) override;
|
||||
|
||||
/*!
|
||||
* Attempts to write the block \a data at the current get pointer. If the
|
||||
* file is currently only opened read only -- i.e. readOnly() returns \c true --
|
||||
* this attempts to reopen the file in read/write mode.
|
||||
*
|
||||
* \note This should be used instead of using the streaming output operator
|
||||
* for a ByteVector. And even this function is significantly slower than
|
||||
* doing output with a char[].
|
||||
*/
|
||||
void writeBlock(const TagLib::ByteVector &data) override;
|
||||
|
||||
/*!
|
||||
* Insert \a data at position \a start in the file overwriting \a replace
|
||||
* bytes of the original content.
|
||||
*
|
||||
* \note This method is slow since it requires rewriting all of the file
|
||||
* after the insertion point.
|
||||
*/
|
||||
void insert(const TagLib::ByteVector &data,
|
||||
TagLib::offset_t start = 0, size_t replace = 0) override;
|
||||
|
||||
/*!
|
||||
* Removes a block of the file starting a \a start and continuing for
|
||||
* \a length bytes.
|
||||
*
|
||||
* \note This method is slow since it involves rewriting all of the file
|
||||
* after the removed portion.
|
||||
*/
|
||||
void removeBlock(TagLib::offset_t start = 0, size_t length = 0) override;
|
||||
|
||||
/*!
|
||||
* Returns \c true if the file is read only (or if the file can not be opened).
|
||||
*/
|
||||
bool readOnly() const override;
|
||||
|
||||
/*!
|
||||
* Since the file can currently only be opened as an argument to the
|
||||
* constructor (sort-of by design), this returns if that open succeeded.
|
||||
*/
|
||||
bool isOpen() const override;
|
||||
|
||||
/*!
|
||||
* Move the I/O pointer to \a offset in the stream from position \a p. This
|
||||
* defaults to seeking from the beginning of the stream.
|
||||
*
|
||||
* \see Position
|
||||
*/
|
||||
void seek(TagLib::offset_t offset, Position p = Beginning) override;
|
||||
|
||||
/*!
|
||||
* Reset the end-of-stream and error flags on the stream.
|
||||
*/
|
||||
void clear() override;
|
||||
|
||||
/*!
|
||||
* Returns the current offset within the stream.
|
||||
*/
|
||||
TagLib::offset_t tell() const override;
|
||||
|
||||
/*!
|
||||
* Returns the length of the stream.
|
||||
*/
|
||||
TagLib::offset_t length() override;
|
||||
|
||||
/*!
|
||||
* Truncates the stream to a \a length.
|
||||
*/
|
||||
void truncate(TagLib::offset_t length) override;
|
||||
|
||||
private:
|
||||
JNIEnv *env;
|
||||
jobject inputStream;
|
||||
jmethodID inputStreamNameMethod;
|
||||
jmethodID inputStreamReadBlockMethod;
|
||||
jmethodID inputStreamIsOpenMethod;
|
||||
jmethodID inputStreamSeekFromBeginningMethod;
|
||||
jmethodID inputStreamSeekFromCurrentMethod;
|
||||
jmethodID inputStreamSeekFromEndMethod;
|
||||
jmethodID inputStreamClearMethod;
|
||||
jmethodID inputStreamTellMethod;
|
||||
jmethodID inputStreamLengthMethod;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //AUXIO_JVMINPUTSTREAM_H
|
||||
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#include "JVMMetadataBuilder.h"
|
||||
|
||||
#include <taglib/mp4tag.h>
|
||||
#include <taglib/textidentificationframe.h>
|
||||
|
||||
JVMMetadataBuilder::JVMMetadataBuilder(JNIEnv *env) : env(env), id3v2(env), xiph(env), mp4(env),
|
||||
cover(), properties(nullptr) {}
|
||||
|
||||
void JVMMetadataBuilder::setMimeType(const std::string_view type) {
|
||||
this->mimeType = type;
|
||||
}
|
||||
|
||||
void JVMMetadataBuilder::setId3v2(const TagLib::ID3v2::Tag &tag) {
|
||||
for (auto frame: tag.frameList()) {
|
||||
if (auto txxxFrame = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame *>(frame)) {
|
||||
TagLib::StringList frameText = txxxFrame->fieldList();
|
||||
// Frame text starts with the description then the remaining values
|
||||
auto begin = frameText.begin();
|
||||
TagLib::String key = TagLib::String(frame->frameID()) + ":" + begin->upper();
|
||||
frameText.erase(begin);
|
||||
id3v2.add(key, frameText);
|
||||
} else if (auto textFrame = dynamic_cast<TagLib::ID3v2::TextIdentificationFrame *>(frame)) {
|
||||
TagLib::String key = frame->frameID();
|
||||
TagLib::StringList frameText = textFrame->fieldList();
|
||||
id3v2.add(key, frameText);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JVMMetadataBuilder::setXiph(const TagLib::Ogg::XiphComment &tag) {
|
||||
for (auto field: tag.fieldListMap()) {
|
||||
auto key = field.first.upper();
|
||||
auto values = field.second;
|
||||
xiph.add(key, values);
|
||||
}
|
||||
}
|
||||
|
||||
void JVMMetadataBuilder::setMp4(const TagLib::MP4::Tag &tag) {
|
||||
for (auto item: tag.itemMap()) {
|
||||
auto itemName = TagLib::String(item.first);
|
||||
auto itemValue = item.second;
|
||||
auto type = itemValue.type();
|
||||
|
||||
// TODO: Handle internal atoms
|
||||
|
||||
// Only read out the atoms for the reasonable tags we are expecting.
|
||||
// None of the crazy binary atoms.
|
||||
if (type == TagLib::MP4::Item::Type::StringList) {
|
||||
auto value = itemValue.toStringList();
|
||||
mp4.add(itemName, value);
|
||||
return;
|
||||
}
|
||||
|
||||
// Assume that taggers will be unhinged and store track numbers
|
||||
// as ints, uints, or longs.
|
||||
if (type == TagLib::MP4::Item::Type::Int) {
|
||||
auto value = std::to_string(itemValue.toInt());
|
||||
id3v2.add(itemName, value);
|
||||
return;
|
||||
}
|
||||
if (type == TagLib::MP4::Item::Type::UInt) {
|
||||
auto value = std::to_string(itemValue.toUInt());
|
||||
id3v2.add(itemName, value);
|
||||
return;
|
||||
}
|
||||
if (type == TagLib::MP4::Item::Type::LongLong) {
|
||||
auto value = std::to_string(itemValue.toLongLong());
|
||||
id3v2.add(itemName, value);
|
||||
return;
|
||||
}
|
||||
if (type == TagLib::MP4::Item::Type::IntPair) {
|
||||
// It's inefficient going from the integer representation back into
|
||||
// a string, but I fully expect taggers to just write "NN/TT" strings
|
||||
// anyway, and musikr doesn't have to do as much fiddly variant handling.
|
||||
auto value = std::to_string(itemValue.toIntPair().first) + "/" +
|
||||
std::to_string(itemValue.toIntPair().second);
|
||||
id3v2.add(itemName, value);
|
||||
return;
|
||||
}
|
||||
// Nothing else makes sense to handle as far as I can tell.
|
||||
}
|
||||
}
|
||||
|
||||
void JVMMetadataBuilder::setCover(const TagLib::List<TagLib::VariantMap> covers) {
|
||||
if (covers.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Find the cover with a "front cover" type
|
||||
for (auto cover: covers) {
|
||||
auto type = cover["pictureType"].toString();
|
||||
if (type == "Front Cover") {
|
||||
this->cover = cover["data"].toByteVector();
|
||||
return;
|
||||
}
|
||||
}
|
||||
// No front cover, just pick first.
|
||||
// TODO: Consider having cascading fallbacks to increasingly less
|
||||
// relevant covers perhaps
|
||||
this->cover = covers.front()["data"].toByteVector();
|
||||
}
|
||||
|
||||
void JVMMetadataBuilder::setProperties(TagLib::AudioProperties *properties) {
|
||||
this->properties = properties;
|
||||
}
|
||||
|
||||
jobject JVMMetadataBuilder::build() {
|
||||
jclass propertiesClass = env->FindClass("org/oxycblt/ktaglib/Properties");
|
||||
jmethodID propertiesInit = env->GetMethodID(propertiesClass, "<init>", "(Ljava/lang/String;JII)V");
|
||||
jobject propertiesObj = env->NewObject(propertiesClass, propertiesInit,
|
||||
env->NewStringUTF(mimeType.data()), (jlong) properties->lengthInMilliseconds(),
|
||||
properties->bitrate(), properties->sampleRate());
|
||||
env->DeleteLocalRef(propertiesClass);
|
||||
|
||||
jclass metadataClass = env->FindClass("org/oxycblt/ktaglib/Metadata");
|
||||
jmethodID metadataInit = env->GetMethodID(metadataClass, "<init>", "(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;[BLorg/oxycblt/ktaglib/Properties;)V");
|
||||
jobject id3v2Map = id3v2.getObject();
|
||||
jobject xiphMap = xiph.getObject();
|
||||
jobject mp4Map = mp4.getObject();
|
||||
jbyteArray coverArray = nullptr;
|
||||
if (cover.has_value()) {
|
||||
auto coverSize = static_cast<jsize>(cover->size());
|
||||
coverArray = env->NewByteArray(coverSize);
|
||||
env->SetByteArrayRegion(coverArray, 0, coverSize, reinterpret_cast<const jbyte *>(cover->data()));
|
||||
}
|
||||
jobject metadataObj = env->NewObject(metadataClass, metadataInit, id3v2Map, xiphMap, mp4Map, coverArray, propertiesObj);
|
||||
env->DeleteLocalRef(metadataClass);
|
||||
return metadataObj;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#ifndef AUXIO_JVMMETADATABUILDER_H
|
||||
#define AUXIO_JVMMETADATABUILDER_H
|
||||
|
||||
#include <jni.h>
|
||||
#include <string_view>
|
||||
#include <optional>
|
||||
|
||||
#include "taglib/id3v2tag.h"
|
||||
#include "taglib/xiphcomment.h"
|
||||
#include "taglib/mp4tag.h"
|
||||
#include "taglib/audioproperties.h"
|
||||
|
||||
#include "JVMTagMap.h"
|
||||
|
||||
class JVMMetadataBuilder {
|
||||
public:
|
||||
JVMMetadataBuilder(JNIEnv *env);
|
||||
|
||||
void setMimeType(const std::string_view type);
|
||||
void setId3v2(const TagLib::ID3v2::Tag &tag);
|
||||
void setXiph(const TagLib::Ogg::XiphComment &tag);
|
||||
void setMp4(const TagLib::MP4::Tag &tag);
|
||||
void setCover(const TagLib::List<TagLib::VariantMap> covers);
|
||||
void setProperties(TagLib::AudioProperties *properties);
|
||||
|
||||
jobject build();
|
||||
|
||||
private:
|
||||
JNIEnv *env;
|
||||
|
||||
std::string_view mimeType;
|
||||
|
||||
std::optional<TagLib::ByteVector> cover;
|
||||
TagLib::AudioProperties *properties;
|
||||
|
||||
JVMTagMap id3v2;
|
||||
JVMTagMap xiph;
|
||||
JVMTagMap mp4;
|
||||
};
|
||||
|
||||
#endif //AUXIO_JVMMETADATABUILDER_H
|
||||
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#include "JVMTagMap.h"
|
||||
|
||||
JVMTagMap::JVMTagMap(JNIEnv *env) : env(env) {
|
||||
jclass hashMapClass = env->FindClass("java/util/HashMap");
|
||||
jmethodID init = env->GetMethodID(hashMapClass, "<init>", "()V");
|
||||
hashMap = env->NewObject(hashMapClass, init);
|
||||
hashMapGetMethod = env->GetMethodID(hashMapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
|
||||
hashMapPutMethod = env->GetMethodID(hashMapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
||||
env->DeleteLocalRef(hashMapClass);
|
||||
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
arrayListInitMethod = env->GetMethodID(arrayListClass, "<init>", "()V");
|
||||
arrayListAddMethod = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
|
||||
env->DeleteLocalRef(arrayListClass);
|
||||
}
|
||||
|
||||
JVMTagMap::~JVMTagMap() {
|
||||
env->DeleteLocalRef(hashMap);
|
||||
}
|
||||
|
||||
void JVMTagMap::add(TagLib::String &key, std::string_view value) {
|
||||
jstring jKey = env->NewStringUTF(key.toCString(true));
|
||||
jstring jValue = env->NewStringUTF(value.data());
|
||||
|
||||
// check if theres already a value arraylist in the map
|
||||
jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod, jKey);
|
||||
// if there is, add to the value to the existing arraylist
|
||||
if (existingValue != nullptr) {
|
||||
env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue);
|
||||
} else {
|
||||
// if there isn't, create a new arraylist and add the value to it
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod);
|
||||
env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue);
|
||||
env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList);
|
||||
env->DeleteLocalRef(arrayListClass);
|
||||
}
|
||||
}
|
||||
|
||||
void JVMTagMap::add(TagLib::String &key, TagLib::StringList &value) {
|
||||
jstring jKey = env->NewStringUTF(key.toCString(true));
|
||||
|
||||
// check if theres already a value arraylist in the map
|
||||
jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod, jKey);
|
||||
// if there is, add to the value to the existing arraylist
|
||||
if (existingValue != nullptr) {
|
||||
for (auto &val : value) {
|
||||
jstring jValue = env->NewStringUTF(val.toCString(true));
|
||||
env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue);
|
||||
}
|
||||
} else {
|
||||
// if there isn't, create a new arraylist and add the value to it
|
||||
jclass arrayListClass = env->FindClass("java/util/ArrayList");
|
||||
jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod);
|
||||
for (auto &val : value) {
|
||||
jstring jValue = env->NewStringUTF(val.toCString(true));
|
||||
env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue);
|
||||
}
|
||||
env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList);
|
||||
env->DeleteLocalRef(arrayListClass);
|
||||
}
|
||||
}
|
||||
|
||||
jobject JVMTagMap::getObject() {
|
||||
return hashMap;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by oxycblt on 12/12/24.
|
||||
//
|
||||
|
||||
#ifndef AUXIO_JVMTAGMAP_H
|
||||
#define AUXIO_JVMTAGMAP_H
|
||||
|
||||
#include <jni.h>
|
||||
#include <string_view>
|
||||
#include <taglib/tstring.h>
|
||||
#include <taglib/tstringlist.h>
|
||||
|
||||
class JVMTagMap {
|
||||
public:
|
||||
JVMTagMap(JNIEnv *env);
|
||||
~JVMTagMap();
|
||||
|
||||
JVMTagMap(const JVMTagMap &) = delete;
|
||||
JVMTagMap &operator=(const JVMTagMap &) = delete;
|
||||
|
||||
void add(TagLib::String &key, std::string_view value);
|
||||
void add(TagLib::String &key, TagLib::StringList &value);
|
||||
|
||||
jobject getObject();
|
||||
|
||||
private:
|
||||
JNIEnv *env;
|
||||
jobject hashMap;
|
||||
jmethodID hashMapGetMethod;
|
||||
jmethodID hashMapPutMethod;
|
||||
jmethodID arrayListInitMethod;
|
||||
jmethodID arrayListAddMethod;
|
||||
};
|
||||
|
||||
|
||||
#endif //AUXIO_JVMTAGMAP_H
|
||||
@@ -0,0 +1,15 @@
|
||||
# Define the minimum CMake version and project name
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
|
||||
# Set the Android NDK path
|
||||
option(ANDROID_NDK_PATH "Path to Android NDK Install. Should be same version specified in gradle." REQUIRED)
|
||||
|
||||
# Specify the target Android API level
|
||||
set(ANDROID_PLATFORM android-24)
|
||||
|
||||
# Define the toolchain
|
||||
set(CMAKE_SYSTEM_NAME Android)
|
||||
set(CMAKE_ANDROID_ARCH_ABI ${ANDROID_ABI})
|
||||
set(CMAKE_ANDROID_NDK ${ANDROID_NDK_PATH})
|
||||
set(CMAKE_ANDROID_STL_TYPE c++_static)
|
||||
set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
set -e
|
||||
WORKING_DIR=$1
|
||||
echo "Working directory is at $WORKING_DIR"
|
||||
cd "$WORKING_DIR"
|
||||
|
||||
TAGLIB_SRC_DIR=${WORKING_DIR}/taglib
|
||||
TAGLIB_DST_DIR=${WORKING_DIR}/taglib/build
|
||||
TAGLIB_PKG_DIR=${WORKING_DIR}/taglib/pkg
|
||||
NDK_TOOLCHAIN=${WORKING_DIR}/android.toolchain.cmake
|
||||
NDK_PATH=$2
|
||||
echo "Taglib source is at $TAGLIB_SRC_DIR"
|
||||
echo "Taglib build is at $TAGLIB_DST_DIR"
|
||||
echo "Taglib package is at $TAGLIB_PKG_DIR"
|
||||
echo "NDK toolchain is at $NDK_TOOLCHAIN"
|
||||
echo "NDK path is at $NDK_PATH"
|
||||
|
||||
X86_ARCH=x86
|
||||
X86_64_ARCH=x86_64
|
||||
ARMV7_ARCH=armeabi-v7a
|
||||
ARMV8_ARCH=arm64-v8a
|
||||
|
||||
build_for_arch() {
|
||||
local ARCH=$1
|
||||
local DST_DIR=$TAGLIB_DST_DIR/$ARCH
|
||||
local PKG_DIR=$TAGLIB_PKG_DIR/$ARCH
|
||||
|
||||
cd $TAGLIB_SRC_DIR
|
||||
cmake -B $DST_DIR -DANDROID_NDK_PATH=${NDK_PATH} -DCMAKE_TOOLCHAIN_FILE=${NDK_TOOLCHAIN} \
|
||||
-DANDROID_ABI=$ARCH -DBUILD_SHARED_LIBS=OFF -DVISIBILITY_HIDDEN=ON -DBUILD_TESTING=OFF \
|
||||
-DBUILD_EXAMPLES=OFF -DBUILD_BINDINGS=OFF -DWITH_ZLIB=OFF -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_CXX_FLAGS="-fPIC"
|
||||
cmake --build $DST_DIR --config Release -j$(nproc)
|
||||
cd $WORKING_DIR
|
||||
|
||||
cmake --install $DST_DIR --config Release --prefix $PKG_DIR --strip
|
||||
}
|
||||
|
||||
build_for_arch $X86_ARCH&
|
||||
build_for_arch $X86_64_ARCH&
|
||||
build_for_arch $ARMV7_ARCH&
|
||||
build_for_arch $ARMV8_ARCH&
|
||||
wait
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
|
||||
#include "JVMInputStream.h"
|
||||
#include "JVMMetadataBuilder.h"
|
||||
|
||||
#include "taglib/fileref.h"
|
||||
#include "taglib/mpegfile.h"
|
||||
#include "taglib/mp4file.h"
|
||||
#include "taglib/flacfile.h"
|
||||
#include "taglib/opusfile.h"
|
||||
#include "taglib/vorbisfile.h"
|
||||
#include "taglib/wavfile.h"
|
||||
|
||||
extern "C" JNIEXPORT jobject JNICALL
|
||||
Java_org_oxycblt_ktaglib_KTagLib_openNative(
|
||||
JNIEnv* env,
|
||||
jobject /* this */,
|
||||
jobject inputStream) {
|
||||
JVMInputStream stream { env, inputStream };
|
||||
TagLib::FileRef fileRef { &stream };
|
||||
if (fileRef.isNull()) {
|
||||
return nullptr;
|
||||
}
|
||||
TagLib::File *file = fileRef.file();
|
||||
JVMMetadataBuilder builder { env };
|
||||
|
||||
if (auto *mpegFile = dynamic_cast<TagLib::MPEG::File *>(file)) {
|
||||
builder.setMimeType("audio/mpeg");
|
||||
builder.setId3v2(*mpegFile->ID3v2Tag());
|
||||
} else if (auto *mp4File = dynamic_cast<TagLib::MP4::File *>(file)) {
|
||||
builder.setMimeType("audio/mp4");
|
||||
builder.setMp4(*mp4File->tag());
|
||||
} else if (auto *flacFile = dynamic_cast<TagLib::FLAC::File *>(file)) {
|
||||
builder.setMimeType("audio/flac");
|
||||
builder.setId3v2(*flacFile->ID3v2Tag());
|
||||
builder.setXiph(*flacFile->xiphComment());
|
||||
} else if (auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File *>(file)) {
|
||||
builder.setMimeType("audio/opus");
|
||||
builder.setXiph(*opusFile->tag());
|
||||
} else if (auto *vorbisFile = dynamic_cast<TagLib::Ogg::Vorbis::File *>(file)) {
|
||||
builder.setMimeType("audio/vorbis");
|
||||
builder.setXiph(*vorbisFile->tag());
|
||||
} else if (auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File *>(file)) {
|
||||
builder.setMimeType("audio/wav");
|
||||
builder.setId3v2(*wavFile->ID3v2Tag());
|
||||
} else {
|
||||
// While taglib supports other formats, ExoPlayer does not. Ignore them.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
builder.setProperties(file->audioProperties());
|
||||
builder.setCover(file->tag()->complexProperties("PICTURE"));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
Submodule
+1
Submodule musikr/src/main/cpp/taglib added at 225c73e181
Reference in New Issue
Block a user