Improve tag parsing
This commit is contained in:
@@ -25,17 +25,14 @@
|
||||
#include "taglib/fileref.h"
|
||||
#include "taglib/flacfile.h"
|
||||
#include "taglib/mp4file.h"
|
||||
#include "taglib/mp4properties.h"
|
||||
#include "taglib/mpegfile.h"
|
||||
#include "taglib/opusfile.h"
|
||||
#include "taglib/vorbisfile.h"
|
||||
#include "taglib/wavfile.h"
|
||||
|
||||
bool parseMpeg(const std::string &name, TagLib::File *file,
|
||||
bool parseMpeg(const std::string &name, TagLib::MPEG::File *mpegFile,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *mpegFile = dynamic_cast<TagLib::MPEG::File*>(file);
|
||||
if (mpegFile == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto id3v1Tag = mpegFile->ID3v1Tag();
|
||||
if (id3v1Tag != nullptr) {
|
||||
try {
|
||||
@@ -55,12 +52,8 @@ bool parseMpeg(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseMp4(const std::string &name, TagLib::File *file,
|
||||
bool parseMp4(const std::string &name, TagLib::MP4::File *mp4File,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *mp4File = dynamic_cast<TagLib::MP4::File*>(file);
|
||||
if (mp4File == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto tag = mp4File->tag();
|
||||
if (tag != nullptr) {
|
||||
try {
|
||||
@@ -72,12 +65,8 @@ bool parseMp4(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseFlac(const std::string &name, TagLib::File *file,
|
||||
bool parseFlac(const std::string &name, TagLib::FLAC::File *flacFile,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *flacFile = dynamic_cast<TagLib::FLAC::File*>(file);
|
||||
if (flacFile == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto id3v1Tag = flacFile->ID3v1Tag();
|
||||
if (id3v1Tag != nullptr) {
|
||||
try {
|
||||
@@ -108,12 +97,8 @@ bool parseFlac(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseOpus(const std::string &name, TagLib::File *file,
|
||||
bool parseOpus(const std::string &name, TagLib::Ogg::Opus::File *opusFile,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File*>(file);
|
||||
if (opusFile == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto tag = opusFile->tag();
|
||||
if (tag != nullptr) {
|
||||
try {
|
||||
@@ -126,12 +111,8 @@ bool parseOpus(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseVorbis(const std::string &name, TagLib::File *file,
|
||||
bool parseVorbis(const std::string &name, TagLib::Ogg::Vorbis::File *vorbisFile,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *vorbisFile = dynamic_cast<TagLib::Ogg::Vorbis::File*>(file);
|
||||
if (vorbisFile == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto tag = vorbisFile->tag();
|
||||
if (tag != nullptr) {
|
||||
try {
|
||||
@@ -143,12 +124,8 @@ bool parseVorbis(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseWav(const std::string &name, TagLib::File *file,
|
||||
bool parseWav(const std::string &name, TagLib::RIFF::WAV::File *wavFile,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File*>(file);
|
||||
if (wavFile == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto tag = wavFile->ID3v2Tag();
|
||||
if (tag != nullptr) {
|
||||
try {
|
||||
@@ -160,43 +137,148 @@ bool parseWav(const std::string &name, TagLib::File *file,
|
||||
return true;
|
||||
}
|
||||
|
||||
// A minimal implementation of 'detectByContent()' from TagLib,
|
||||
// limited to the available 'parse..()' functions available here.
|
||||
// This fixes mostly all parsing problems for formats undetected by
|
||||
// simple extension based detection, like AAC-in-MP4, but still
|
||||
// doesn't work for Matroska, as it is not yet supported by TagLib
|
||||
// see https://github.com/taglib/taglib/pull/1149
|
||||
// Detects the file type based on the actual content of the stream.
|
||||
TagLib::File* createFileFromContent(TagLib::IOStream *stream,
|
||||
bool readAudioProperties,
|
||||
TagLib::AudioProperties::ReadStyle audioPropertiesStyle) {
|
||||
TagLib::File *file = nullptr;
|
||||
|
||||
if (TagLib::MPEG::File::isSupported(stream))
|
||||
file = new TagLib::MPEG::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
else if (TagLib::Ogg::Vorbis::File::isSupported(stream))
|
||||
file = new TagLib::Ogg::Vorbis::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
else if (TagLib::FLAC::File::isSupported(stream))
|
||||
file = new TagLib::FLAC::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
else if (TagLib::Ogg::Opus::File::isSupported(stream))
|
||||
file = new TagLib::Ogg::Opus::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
else if (TagLib::MP4::File::isSupported(stream))
|
||||
file = new TagLib::MP4::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
else if (TagLib::RIFF::WAV::File::isSupported(stream))
|
||||
file = new TagLib::RIFF::WAV::File(stream, readAudioProperties,
|
||||
audioPropertiesStyle);
|
||||
|
||||
// double-check that the potentially created file actually valid.
|
||||
if (file) {
|
||||
if (file->isValid())
|
||||
return file;
|
||||
delete file;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// This dispatcher replaces the previous, order-dependent if-else chain. It uses
|
||||
// dynamic_cast to identify the file's true type, ensuring the correct parser is
|
||||
// always called. This also allows the 'parse..()' functions to be simplified by
|
||||
// accepting a specific file type (e.g., 'MPEG::File*'), removing the need for
|
||||
// redundant internal checks.
|
||||
// Accurate container MIME type support is not added, as it did not seem
|
||||
// necessary, we can rely on SAF and MediaStore implementation for now,
|
||||
// that works on extension based detection, so we expect users to use
|
||||
// correct file extensions.
|
||||
bool dispatchAndParse(const std::string &name, TagLib::File *file,
|
||||
JMetadataBuilder &jBuilder) {
|
||||
if (auto *mpegFile = dynamic_cast<TagLib::MPEG::File*>(file)) {
|
||||
jBuilder.setMimeType("audio/mpeg");
|
||||
return parseMpeg(name, mpegFile, jBuilder);
|
||||
}
|
||||
if (auto *flacFile = dynamic_cast<TagLib::FLAC::File*>(file)) {
|
||||
jBuilder.setMimeType("audio/flac");
|
||||
return parseFlac(name, flacFile, jBuilder);
|
||||
}
|
||||
if (auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File*>(file)) {
|
||||
jBuilder.setMimeType("audio/opus");
|
||||
return parseOpus(name, opusFile, jBuilder);
|
||||
}
|
||||
if (auto *vorbisFile = dynamic_cast<TagLib::Ogg::Vorbis::File*>(file)) {
|
||||
jBuilder.setMimeType("audio/vorbis");
|
||||
return parseVorbis(name, vorbisFile, jBuilder);
|
||||
}
|
||||
if (auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File*>(file)) {
|
||||
jBuilder.setMimeType("audio/wav");
|
||||
return parseWav(name, wavFile, jBuilder);
|
||||
}
|
||||
if (auto *mp4File = dynamic_cast<TagLib::MP4::File*>(file)) {
|
||||
// Why are we setting this as default?
|
||||
// 'audio/mp4' is not even a codec MIME type.
|
||||
jBuilder.setMimeType("audio/mp4");
|
||||
if (auto *props =
|
||||
dynamic_cast<TagLib::MP4::Properties*>(mp4File->audioProperties())) {
|
||||
using Codec = TagLib::MP4::Properties::Codec;
|
||||
switch (props->codec()) {
|
||||
case Codec::AAC:
|
||||
jBuilder.setMimeType("audio/aac");
|
||||
break;
|
||||
case Codec::ALAC:
|
||||
jBuilder.setMimeType("audio/alac");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return parseMp4(name, mp4File, jBuilder);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jobject JNICALL
|
||||
Java_org_oxycblt_musikr_metadata_TagLibJNI_openNative(JNIEnv *env,
|
||||
jobject /* this */,
|
||||
jobject inputStream) {
|
||||
std::string name = "unknown file";
|
||||
TagLib::File *overriddenFile = nullptr;
|
||||
try {
|
||||
JInputStream jStream {env, inputStream};
|
||||
name = jStream.name();
|
||||
TagLib::FileRef fileRef {&jStream};
|
||||
if (fileRef.isNull()) {
|
||||
throw std::runtime_error("Invalid file");
|
||||
TagLib::FileRef fileRef {&jStream, true, TagLib::AudioProperties::Average};
|
||||
TagLib::File *fileToUse = fileRef.file();
|
||||
bool needsOverride = (fileToUse != nullptr &&
|
||||
fileToUse->audioProperties() != nullptr &&
|
||||
fileToUse->audioProperties()->lengthInSeconds() == 0);
|
||||
if (needsOverride) {
|
||||
LOGD("FileRef result for %s is suspicious (duration=0). Forcing content scan.", name.c_str());
|
||||
jStream.seek(0, TagLib::IOStream::Beginning);
|
||||
overriddenFile = createFileFromContent(&jStream, true, TagLib::AudioProperties::Average);
|
||||
if (overriddenFile != nullptr &&
|
||||
overriddenFile->audioProperties() != nullptr &&
|
||||
overriddenFile->audioProperties()->lengthInSeconds() > 0)
|
||||
{
|
||||
LOGD("Content scan successful. Overriding FileRef result for %s.", name.c_str());
|
||||
fileToUse = overriddenFile;
|
||||
} else {
|
||||
delete overriddenFile;
|
||||
overriddenFile = nullptr;
|
||||
}
|
||||
}
|
||||
if (fileToUse == nullptr) {
|
||||
throw std::runtime_error("Invalid or unsupported file");
|
||||
}
|
||||
TagLib::File *file = fileRef.file();
|
||||
JMetadataBuilder jBuilder {env};
|
||||
jBuilder.setProperties(file->audioProperties());
|
||||
jBuilder.setProperties(fileToUse->audioProperties());
|
||||
|
||||
// TODO: Make some type of composable logger so I don't
|
||||
// have to shoehorn this into the native code.
|
||||
if (parseMpeg(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/mpeg");
|
||||
} else if (parseMp4(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/mp4");
|
||||
} else if (parseFlac(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/flac");
|
||||
} else if (parseOpus(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/opus");
|
||||
} else if (parseVorbis(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/vorbis");
|
||||
} else if (parseWav(name, file, jBuilder)) {
|
||||
jBuilder.setMimeType("audio/wav");
|
||||
} else {
|
||||
LOGE("File format in %s is not supported", name.c_str());
|
||||
if (!dispatchAndParse(name, fileToUse, jBuilder)) {
|
||||
LOGE("File format in %s is not supported by any parser.", name.c_str());
|
||||
delete overriddenFile;
|
||||
return nullptr;
|
||||
}
|
||||
return jBuilder.build();
|
||||
jobject result = jBuilder.build();
|
||||
delete overriddenFile;
|
||||
return result;
|
||||
} catch (std::exception &e) {
|
||||
LOGE("Unable to parse metadata in %s: %s", name.c_str(), e.what());
|
||||
delete overriddenFile;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user