From eb7da85f959318f6e0f57000c3798beca09aedd4 Mon Sep 17 00:00:00 2001 From: hj kim Date: Thu, 7 May 2020 18:38:27 +0900 Subject: [PATCH] Improve metadata_editor_append_picture() API Change-Id: I030ebb2539b49eb9a1748cd9e529dd967432d45f --- src/metadata_editor.cpp | 207 ++++++++++++++++++++---------------------------- 1 file changed, 87 insertions(+), 120 deletions(-) diff --git a/src/metadata_editor.cpp b/src/metadata_editor.cpp index 9a60fcb..83c250e 100755 --- a/src/metadata_editor.cpp +++ b/src/metadata_editor.cpp @@ -734,7 +734,7 @@ static int __metadata_editor_get_picture_type(const char *path, char **type) { return METADATA_EDITOR_ERROR_NOT_SUPPORTED; } -int __metadata_editor_get_picture_info(const char *path, void **picture, int *size, char **type) { +int __metadata_editor_get_picture_info(const char *path, void **picture, size_t *size, char **type) { int ret; ret = __metadata_editor_get_picture_type(path, type); @@ -1423,16 +1423,12 @@ static int __get_mp4_picture(metadata_editor_s* metadata, int index, void **pict metadata_editor_retvm_if(!tag, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Tag does not exist"); - // Get map of items directly from tag and launch a search of specific item - TagLib::MP4::ItemListMap& itemMap = tag->itemListMap(); - TagLib::MP4::ItemListMap::ConstIterator it = itemMap.find("covr"); - - if (it == itemMap.end()) { + if (!(tag->contains("covr"))) { metadata_editor_error("No item in file. No pictures in file"); return METADATA_EDITOR_ERROR_OPERATION_FAILED; } - TagLib::MP4::CoverArtList lst = it->second.toCoverArtList(); + TagLib::MP4::CoverArtList lst = tag->item("covr").toCoverArtList(); // Check if the index is in range of CoverArtList Item if ((index < 0) || ((uint)index >= lst.size())) { @@ -1519,12 +1515,81 @@ extern "C" int metadata_editor_get_picture(metadata_editor_h metadata, int index } } +static int __append_APIC(TagLib::ID3v2::Tag* tag, void *picture, size_t size, const char *mime_type) +{ + metadata_editor_retvm_if(!tag, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. No ID3v2 tag in file."); + + auto pictureFrame = new TagLib::ID3v2::AttachedPictureFrame(); + + metadata_editor_info("New APIC frame will be added to the ID3v2 tag"); + + pictureFrame->setPicture(TagLib::ByteVector((char*)picture, size)); + pictureFrame->setType(TagLib::ID3v2::AttachedPictureFrame::FrontCover); + pictureFrame->setMimeType(mime_type); + + tag->addFrame(pictureFrame); + + return METADATA_EDITOR_ERROR_NONE; + +} +static int __append_mp3_picture(metadata_editor_s* metadata, void *picture ,size_t size, const char *mime_type) +{ + return __append_APIC(dynamic_cast(metadata->file)->ID3v2Tag(true), picture, size, mime_type); +} + +static int __append_mp4_picture(metadata_editor_s* metadata, void *picture, size_t size, const char *mime_type) +{ + auto tag = dynamic_cast(metadata->file->tag()); + + metadata_editor_retvm_if(!tag, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Tag was not created"); + + TagLib::MP4::CoverArtList lst; + TagLib::MP4::CoverArt::Format format = TagLib::MP4::CoverArt::Unknown; + if (strncmp(mime_type, MIME_TYPE_JPEG, strlen(MIME_TYPE_JPEG)) == 0) + format = TagLib::MP4::CoverArt::JPEG; + else if (strncmp(mime_type, MIME_TYPE_PNG, strlen(MIME_TYPE_PNG)) == 0) + format = TagLib::MP4::CoverArt::PNG; + + if (tag->contains("covr")) { + metadata_editor_info("The item exists. Adding picture"); + lst = tag->item("covr").toCoverArtList(); + } + + lst.append(TagLib::MP4::CoverArt(format, TagLib::ByteVector((char*)picture, size))); + tag->setItem("covr", lst); + + return METADATA_EDITOR_ERROR_NONE; +} + +#if 0 +static int __append_flac_picture(metadata_editor_s* metadata, void *picture, size_t size, const char *mime_type) +{ + TagLib::FLAC::File* _file = (TagLib::FLAC::File*) metadata->file; + auto frontCover = new TagLib::FLAC::Picture; + + metadata_editor_debug_fenter(); + + frontCover->setData(TagLib::ByteVector((char*)picture, size)); + frontCover->setType(TagLib::FLAC::Picture::FrontCover); + frontCover->setMimeType(mime_type); + + _file->addPicture(frontCover); + + return METADATA_EDITOR_ERROR_NONE; +} + +static int __append_wav_picture(metadata_editor_s* metadata, void *picture, size_t size, const char *mime_type) +{ + return __append_APIC(dynamic_cast(metadata->file->tag()), picture, size, mime_type); +} +#endif + // *** This function appends a cover art picture to the file *** // extern "C" int metadata_editor_append_picture(metadata_editor_h metadata, const char *path) { int ret = METADATA_EDITOR_ERROR_NONE; void *picture = NULL; - int size = 0; - char *type = NULL; + size_t size = 0; + char *mime_type = NULL; metadata_editor_s* _metadata = (metadata_editor_s*) metadata; @@ -1532,124 +1597,26 @@ extern "C" int metadata_editor_append_picture(metadata_editor_h metadata, const metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_set_parameter() [%d]", ret); metadata_editor_retvm_if(!path, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid path"); - ret = __metadata_editor_get_picture_info(path, &picture, &size, &type); + ret = __metadata_editor_get_picture_info(path, &picture, &size, &mime_type); metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, METADATA_EDITOR_ERROR_PERMISSION_DENIED, "File does not exist or you have no rights to open it"); - switch (_metadata->filetype) { // Process the file according to the specified file type - case METADATA_EDITOR_FORMAT_MP3: { - // Bring the pointer to actual file type and make tags pointers - TagLib::MPEG::File* _file = (TagLib::MPEG::File*)_metadata->file; - TagLib::ID3v2::Tag* tag2 = _file->ID3v2Tag(true); - // Check if the valid tag pointer exists - if (tag2 == NULL) { - metadata_editor_error("Error. ID3v2 tag was not created. Can not proceed metadata updating"); - ret = METADATA_EDITOR_ERROR_OPERATION_FAILED; - break; - } - TagLib::ID3v2::AttachedPictureFrame* pictureFrame = new TagLib::ID3v2::AttachedPictureFrame(); - if (pictureFrame == NULL) { - metadata_editor_error("OUT_OF_MEMORY"); - ret = METADATA_EDITOR_ERROR_OUT_OF_MEMORY; - break; - } - metadata_editor_info("New APIC frame will be added to the ID3v2 tag"); - pictureFrame->setPicture(TagLib::ByteVector((char*)picture, size)); - pictureFrame->setType(TagLib::ID3v2::AttachedPictureFrame::FrontCover); - pictureFrame->setMimeType(type); - - tag2->addFrame(pictureFrame); + switch (_metadata->filetype) { + case METADATA_EDITOR_FORMAT_MP3: + ret = __append_mp3_picture(_metadata, picture, size, mime_type); + break; - ret = METADATA_EDITOR_ERROR_NONE; + case METADATA_EDITOR_FORMAT_MP4: + ret = __append_mp4_picture(_metadata, picture, size, mime_type); break; - } - case METADATA_EDITOR_FORMAT_MP4: { - TagLib::MP4::File* _file = (TagLib::MP4::File*) _metadata->file; - TagLib::MP4::Tag* tag = _file->tag(); - if (!tag) { - metadata_editor_error("Tag was not created"); - ret = METADATA_EDITOR_ERROR_OPERATION_FAILED; - break; - } - // Get map of items directly from tag and launch a search of specific item - TagLib::MP4::ItemListMap& itemMap = tag->itemListMap(); - TagLib::MP4::ItemListMap::ConstIterator it = itemMap.find("covr"); - if (it != itemMap.end()) { // Item was found - metadata_editor_info("The item exists. Adding picture"); - TagLib::MP4::CoverArtList lst = it->second.toCoverArtList(); - TagLib::MP4::CoverArt::Format format; - if (strncmp(type, MIME_TYPE_JPEG, strlen(MIME_TYPE_JPEG)) == 0) - format = TagLib::MP4::CoverArt::JPEG; - else if (strncmp(type, MIME_TYPE_PNG, strlen(MIME_TYPE_PNG)) == 0) - format = TagLib::MP4::CoverArt::PNG; - else - format = (TagLib::MP4::CoverArt::Format)0xFFFF; - TagLib::MP4::CoverArt cover(format, TagLib::ByteVector((char*)picture, size)); - lst.append(cover); - itemMap.insert("covr", TagLib::MP4::Item(lst)); - - ret = METADATA_EDITOR_ERROR_NONE; - break; - } else { // Item was not found - metadata_editor_info("The item does not exist. Adding picture"); - TagLib::MP4::CoverArt::Format format; - if (strncmp(type, MIME_TYPE_JPEG, strlen(MIME_TYPE_JPEG)) == 0) - format = TagLib::MP4::CoverArt::JPEG; - else if (strncmp(type, MIME_TYPE_PNG, strlen(MIME_TYPE_PNG)) == 0) - format = TagLib::MP4::CoverArt::PNG; - else - format = (TagLib::MP4::CoverArt::Format)0xFFFF; - TagLib::MP4::CoverArt cover(format, TagLib::ByteVector((char*)picture, size)); - TagLib::MP4::CoverArtList lst; - lst.append(cover); - itemMap.insert("covr", TagLib::MP4::Item(lst)); - - ret = METADATA_EDITOR_ERROR_NONE; - break; - } - } #if 0 - case METADATA_EDITOR_FORMAT_FLAC: { - TagLib::FLAC::File* _file = (TagLib::FLAC::File*) _metadata->file; - TagLib::FLAC::Picture* frontCover = new TagLib::FLAC::Picture; - if (frontCover == NULL) { - metadata_editor_error("OUT_OF_MEMORY"); - ret = METADATA_EDITOR_ERROR_OUT_OF_MEMORY; - break; - } - frontCover->setData(TagLib::ByteVector((char*)picture, size)); - frontCover->setType(TagLib::FLAC::Picture::FrontCover); - frontCover->setMimeType(type); - - metadata_editor_info("Picture will be added to FLAC file"); - _file->addPicture(frontCover); - ret = METADATA_EDITOR_ERROR_NONE; + case METADATA_EDITOR_FORMAT_FLAC: + ret = __append_flac_picture(_metadata, picture, size, mime_type); break; - } - case METADATA_EDITOR_FORMAT_WAV: { - // Bring the pointer to actual file type and make tags pointers - TagLib::RIFF::WAV::File* _file = (TagLib::RIFF::WAV::File*)_metadata->file; - TagLib::ID3v2::Tag* tag2 = _file->tag(); - // Check if the valid tag pointer exists - if (tag2 == NULL) { - metadata_editor_error("Error. ID3v2 tag was not created. Can not proceed metadata updating"); - ret = METADATA_EDITOR_ERROR_OPERATION_FAILED; - break; - } - TagLib::ID3v2::AttachedPictureFrame* pictureFrame = new TagLib::ID3v2::AttachedPictureFrame(); - if (pictureFrame == NULL) { - metadata_editor_error("OUT_OF_MEMORY"); - ret = METADATA_EDITOR_ERROR_OUT_OF_MEMORY; - break; - } - metadata_editor_info("New APIC frame will be added to the ID3v2 tag"); - pictureFrame->setPicture(TagLib::ByteVector((char*)picture, size)); - pictureFrame->setType(TagLib::ID3v2::AttachedPictureFrame::FrontCover); - pictureFrame->setMimeType(type); - tag2->addFrame(pictureFrame); - ret = METADATA_EDITOR_ERROR_NONE; + + case METADATA_EDITOR_FORMAT_WAV: + ret = __append_wav_picture(_metadata, picture, size, mime_type); break; - } #endif default: { metadata_editor_error("Wrong file type"); @@ -1659,7 +1626,7 @@ extern "C" int metadata_editor_append_picture(metadata_editor_h metadata, const } META_SAFE_FREE(picture); - g_free(type); + g_free(mime_type); return ret; } -- 2.7.4