Merge pull request #1285 from lepre-zhang/fix-content-detection
Improve tag parsing
This commit is contained in:
@@ -25,17 +25,14 @@
|
|||||||
#include "taglib/fileref.h"
|
#include "taglib/fileref.h"
|
||||||
#include "taglib/flacfile.h"
|
#include "taglib/flacfile.h"
|
||||||
#include "taglib/mp4file.h"
|
#include "taglib/mp4file.h"
|
||||||
|
#include "taglib/mp4properties.h"
|
||||||
#include "taglib/mpegfile.h"
|
#include "taglib/mpegfile.h"
|
||||||
#include "taglib/opusfile.h"
|
#include "taglib/opusfile.h"
|
||||||
#include "taglib/vorbisfile.h"
|
#include "taglib/vorbisfile.h"
|
||||||
#include "taglib/wavfile.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) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *mpegFile = dynamic_cast<TagLib::MPEG::File*>(file);
|
|
||||||
if (mpegFile == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto id3v1Tag = mpegFile->ID3v1Tag();
|
auto id3v1Tag = mpegFile->ID3v1Tag();
|
||||||
if (id3v1Tag != nullptr) {
|
if (id3v1Tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -55,12 +52,8 @@ bool parseMpeg(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseMp4(const std::string &name, TagLib::File *file,
|
bool parseMp4(const std::string &name, TagLib::MP4::File *mp4File,
|
||||||
JMetadataBuilder &jBuilder) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *mp4File = dynamic_cast<TagLib::MP4::File*>(file);
|
|
||||||
if (mp4File == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto tag = mp4File->tag();
|
auto tag = mp4File->tag();
|
||||||
if (tag != nullptr) {
|
if (tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -72,12 +65,8 @@ bool parseMp4(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseFlac(const std::string &name, TagLib::File *file,
|
bool parseFlac(const std::string &name, TagLib::FLAC::File *flacFile,
|
||||||
JMetadataBuilder &jBuilder) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *flacFile = dynamic_cast<TagLib::FLAC::File*>(file);
|
|
||||||
if (flacFile == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto id3v1Tag = flacFile->ID3v1Tag();
|
auto id3v1Tag = flacFile->ID3v1Tag();
|
||||||
if (id3v1Tag != nullptr) {
|
if (id3v1Tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -108,12 +97,8 @@ bool parseFlac(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseOpus(const std::string &name, TagLib::File *file,
|
bool parseOpus(const std::string &name, TagLib::Ogg::Opus::File *opusFile,
|
||||||
JMetadataBuilder &jBuilder) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File*>(file);
|
|
||||||
if (opusFile == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto tag = opusFile->tag();
|
auto tag = opusFile->tag();
|
||||||
if (tag != nullptr) {
|
if (tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -126,12 +111,8 @@ bool parseOpus(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseVorbis(const std::string &name, TagLib::File *file,
|
bool parseVorbis(const std::string &name, TagLib::Ogg::Vorbis::File *vorbisFile,
|
||||||
JMetadataBuilder &jBuilder) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *vorbisFile = dynamic_cast<TagLib::Ogg::Vorbis::File*>(file);
|
|
||||||
if (vorbisFile == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto tag = vorbisFile->tag();
|
auto tag = vorbisFile->tag();
|
||||||
if (tag != nullptr) {
|
if (tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -143,12 +124,8 @@ bool parseVorbis(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseWav(const std::string &name, TagLib::File *file,
|
bool parseWav(const std::string &name, TagLib::RIFF::WAV::File *wavFile,
|
||||||
JMetadataBuilder &jBuilder) {
|
JMetadataBuilder &jBuilder) {
|
||||||
auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File*>(file);
|
|
||||||
if (wavFile == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto tag = wavFile->ID3v2Tag();
|
auto tag = wavFile->ID3v2Tag();
|
||||||
if (tag != nullptr) {
|
if (tag != nullptr) {
|
||||||
try {
|
try {
|
||||||
@@ -160,43 +137,148 @@ bool parseWav(const std::string &name, TagLib::File *file,
|
|||||||
return true;
|
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
|
extern "C" JNIEXPORT jobject JNICALL
|
||||||
Java_org_oxycblt_musikr_metadata_TagLibJNI_openNative(JNIEnv *env,
|
Java_org_oxycblt_musikr_metadata_TagLibJNI_openNative(JNIEnv *env,
|
||||||
jobject /* this */,
|
jobject /* this */,
|
||||||
jobject inputStream) {
|
jobject inputStream) {
|
||||||
std::string name = "unknown file";
|
std::string name = "unknown file";
|
||||||
|
TagLib::File *overriddenFile = nullptr;
|
||||||
try {
|
try {
|
||||||
JInputStream jStream {env, inputStream};
|
JInputStream jStream {env, inputStream};
|
||||||
name = jStream.name();
|
name = jStream.name();
|
||||||
TagLib::FileRef fileRef {&jStream};
|
TagLib::FileRef fileRef {&jStream, true, TagLib::AudioProperties::Average};
|
||||||
if (fileRef.isNull()) {
|
TagLib::File *fileToUse = fileRef.file();
|
||||||
throw std::runtime_error("Invalid file");
|
bool needsOverride = (fileToUse != nullptr &&
|
||||||
}
|
fileToUse->audioProperties() != nullptr &&
|
||||||
TagLib::File *file = fileRef.file();
|
fileToUse->audioProperties()->lengthInSeconds() == 0);
|
||||||
JMetadataBuilder jBuilder {env};
|
if (needsOverride) {
|
||||||
jBuilder.setProperties(file->audioProperties());
|
LOGD("FileRef result for %s is suspicious (duration=0). Forcing content scan.", name.c_str());
|
||||||
|
jStream.seek(0, TagLib::IOStream::Beginning);
|
||||||
// TODO: Make some type of composable logger so I don't
|
overriddenFile = createFileFromContent(&jStream, true, TagLib::AudioProperties::Average);
|
||||||
// have to shoehorn this into the native code.
|
if (overriddenFile != nullptr &&
|
||||||
if (parseMpeg(name, file, jBuilder)) {
|
overriddenFile->audioProperties() != nullptr &&
|
||||||
jBuilder.setMimeType("audio/mpeg");
|
overriddenFile->audioProperties()->lengthInSeconds() > 0)
|
||||||
} else if (parseMp4(name, file, jBuilder)) {
|
{
|
||||||
jBuilder.setMimeType("audio/mp4");
|
LOGD("Content scan successful. Overriding FileRef result for %s.", name.c_str());
|
||||||
} else if (parseFlac(name, file, jBuilder)) {
|
fileToUse = overriddenFile;
|
||||||
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 {
|
} else {
|
||||||
LOGE("File format in %s is not supported", name.c_str());
|
delete overriddenFile;
|
||||||
|
overriddenFile = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fileToUse == nullptr) {
|
||||||
|
throw std::runtime_error("Invalid or unsupported file");
|
||||||
|
}
|
||||||
|
JMetadataBuilder jBuilder {env};
|
||||||
|
jBuilder.setProperties(fileToUse->audioProperties());
|
||||||
|
|
||||||
|
if (!dispatchAndParse(name, fileToUse, jBuilder)) {
|
||||||
|
LOGE("File format in %s is not supported by any parser.", name.c_str());
|
||||||
|
delete overriddenFile;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return jBuilder.build();
|
jobject result = jBuilder.build();
|
||||||
|
delete overriddenFile;
|
||||||
|
return result;
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
LOGE("Unable to parse metadata in %s: %s", name.c_str(), e.what());
|
LOGE("Unable to parse metadata in %s: %s", name.c_str(), e.what());
|
||||||
|
delete overriddenFile;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user