Skip to content

Commit

Permalink
Breaking change: Remove AudioProperties from Metadata class, add new …
Browse files Browse the repository at this point in the history
…getAudioProperties method
  • Loading branch information
Kyant0 committed May 3, 2024
1 parent cdf100e commit 8c365f4
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 26 deletions.
241 changes: 241 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
namespace = "com.kyant.taglib"
compileSdk = libs.versions.android.compileSdk.get().toInt()
buildToolsVersion = "34.0.0"
ndkVersion = "27.0.11718014"
ndkVersion = "26.3.11579264"

defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[versions]
agp = "8.5.0-alpha05"
agp = "8.5.0-alpha07"
android-minSdk = "21"
android-compileSdk = "34"
lib-version = "1.0.0-alpha13"
lib-version = "1.0.0-alpha14"
kotlin = "1.9.23"
rules = "1.5.0"
runner = "1.5.2"
Expand Down
6 changes: 5 additions & 1 deletion src/androidTest/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ class Tests {
private fun read_and_write_m4a() {
getFdFromAssets(context, "Sample_BeeMoved_48kHz16bit.m4a").use { fd ->

// Read audio properties

val audioProperties = TagLib.getAudioProperties(fd.dup().detachFd())!!
Assert.assertEquals(39936, audioProperties.length)

// Read metadata

val metadata = TagLib.getMetadata(fd = fd.dup().detachFd(), readPictures = true)!!
Assert.assertEquals(39936, metadata.audioProperties.length)
Assert.assertEquals("Bee Moved", metadata.propertyMap["TITLE"]!!.single())
Assert.assertEquals(58336, metadata.pictures.single().data.size)

Expand Down
5 changes: 3 additions & 2 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ cmake_minimum_required(VERSION 3.22.1)

project(taglib)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -flto -fdata-sections -ffunction-sections -fomit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto -fdata-sections -ffunction-sections -fomit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fvisibility=hidden -flto -fdata-sections -ffunction-sections -fomit-frame-pointer")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fvisibility=hidden -flto -fdata-sections -ffunction-sections -fomit-frame-pointer")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto -Wl,--gc-sections -s")

set(VISIBILITY_HIDDEN ON)
set(BUILD_BINDINGS OFF)
set(BUILD_TESTING OFF)

add_subdirectory(taglib)

include_directories(
Expand Down
36 changes: 32 additions & 4 deletions src/main/cpp/taglib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

extern "C" {
JNIEXPORT jobject JNICALL
Java_com_kyant_taglib_TagLib_getMetadata(
Java_com_kyant_taglib_TagLib_getAudioProperties(
JNIEnv *env,
jobject,
jint fd,
jint read_style,
jboolean read_pictures
jint read_style
) {
try {
auto path = getRealPathFromFd(fd);
Expand All @@ -17,10 +16,36 @@ Java_com_kyant_taglib_TagLib_getMetadata(
TagLibExt::FileRef f(path, stream.get(), true, style);

if (f.isNull()) {
free(path);
return nullptr;
}

jobject audioProperties = getAudioProperties(env, f);
free(path);
return audioProperties;
} catch (const std::exception &e) {
throwJavaException(env, e.what());
return nullptr;
}
}

JNIEXPORT jobject JNICALL
Java_com_kyant_taglib_TagLib_getMetadata(
JNIEnv *env,
jobject,
jint fd,
jboolean read_pictures
) {
try {
auto path = getRealPathFromFd(fd);
auto stream = std::make_unique<TagLib::FileStream>(fd, true);
TagLibExt::FileRef f(path, stream.get(), false);

if (f.isNull()) {
free(path);
return nullptr;
}

jobject propertiesMap = getPropertyMap(env, f);
jobjectArray pictures;
if (read_pictures) {
Expand All @@ -31,7 +56,7 @@ Java_com_kyant_taglib_TagLib_getMetadata(

jobject metadata = env->NewObject(
metadataClass, metadataConstructor,
audioProperties, propertiesMap, pictures
propertiesMap, pictures
);
free(path);
return metadata;
Expand All @@ -53,6 +78,7 @@ Java_com_kyant_taglib_TagLib_getPictures(
TagLibExt::FileRef f(path, stream.get(), false);

if (f.isNull()) {
free(path);
return emptyPictureArray(env);
}

Expand All @@ -78,6 +104,7 @@ Java_com_kyant_taglib_TagLib_savePropertyMap(
TagLibExt::FileRef f(path, stream.get(), false);

if (f.isNull()) {
free(path);
return false;
}

Expand Down Expand Up @@ -105,6 +132,7 @@ Java_com_kyant_taglib_TagLib_savePictures(
TagLibExt::FileRef f(path, stream.get(), false);

if (f.isNull()) {
free(path);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
metadataClass = reinterpret_cast<jclass>(env->NewGlobalRef(_metadataClass));
env->DeleteLocalRef(_metadataClass);
metadataConstructor = env->GetMethodID(metadataClass, "<init>",
"(Lcom/kyant/taglib/AudioProperties;Ljava/util/HashMap;[Lcom/kyant/taglib/Picture;)V");
"(Ljava/util/HashMap;[Lcom/kyant/taglib/Picture;)V");

jclass _audioPropertiesClass = env->FindClass("com/kyant/taglib/AudioProperties");
audioPropertiesClass = reinterpret_cast<jclass>(env->NewGlobalRef(_audioPropertiesClass));
Expand Down Expand Up @@ -318,7 +318,7 @@ jobjectArray emptyPictureArray(JNIEnv *env) {
return env->NewObjectArray(0, pictureClass, nullptr);
}

char *getRealPathFromFd(int fd) {
char *getRealPathFromFd(const int fd) {
const std::string path = "/proc/self/fd/" + std::to_string(fd);
const char *pathStr = path.c_str();

Expand Down
Loading

0 comments on commit 8c365f4

Please sign in to comment.