Improve metadata_editor_get_picture() API 66/231666/2
authorhj kim <backto.kim@samsung.com>
Thu, 23 Apr 2020 09:38:37 +0000 (18:38 +0900)
committerhj kim <backto.kim@samsung.com>
Fri, 24 Apr 2020 08:38:03 +0000 (17:38 +0900)
Change-Id: Ib88f27133bd411181698150036737cfb52f37808

include/metadata_editor_private.h
src/metadata_editor.cpp

index 767fdbb..eafbc2a 100755 (executable)
@@ -54,7 +54,6 @@ extern "C" {
 #endif
 #define LOG_TAG "CAPI_MEDIA_METADATA_EDITOR"
 #define META_SAFE_FREE(src)      { if (src) {free(src); src = NULL; } }
-#define META_MALLOC(src, size) { if (size <= 0) { src = NULL; } else { src = (char *)malloc(size); if (src) memset(src, 0x0, size); } }
 
 #define META_MAX_BUF_LEN 20
 
index 33f86ac..f424edc 100755 (executable)
@@ -1533,190 +1533,204 @@ extern "C" int metadata_editor_update_metadata(metadata_editor_h metadata) {
 
 // *** This function returns buffer with picture under the specified index and buffer's (picture's) size *** //
 extern "C" int metadata_editor_get_picture(metadata_editor_h metadata, int index, void **picture, int *size, char **mime_type) {
-       const char *TYPE_JPEG = "image/jpeg";
-       const char *TYPE_PNG = "image/png";
-
        int ret = METADATA_EDITOR_ERROR_NONE;
-
        metadata_editor_s* _metadata = (metadata_editor_s*) metadata;
+       const char *TYPE_JPEG = "image/jpeg";
+       const char *TYPE_PNG = "image/png";
+       int i = 0;
 
        ret = __check_metadata_get_parameter(_metadata, mime_type);
        metadata_editor_retvm_if(ret != METADATA_EDITOR_ERROR_NONE, ret, "fail to __check_metadata_get_parameter() [%d]", ret);
        metadata_editor_retvm_if(!picture, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid picture");
        metadata_editor_retvm_if(!size, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid size");
 
-       *picture = NULL;
-       *size = 0;
-       *mime_type = NULL;
-
        switch (_metadata->filetype) {                                                                  // Process the file according to the specified file type
-               case METADATA_EDITOR_FORMAT_MP3: {
-                       TagLib::MPEG::File* _file = (TagLib::MPEG::File*)_metadata->file;               // Bring the pointer to actual file type
-                       TagLib::ID3v2::Tag* tag2 = _file->ID3v2Tag();
-                       metadata_editor_retvm_if(tag2 == NULL, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. No ID3v2 tag in file.");
+       case METADATA_EDITOR_FORMAT_MP3: {
+               TagLib::MPEG::File* _file = (TagLib::MPEG::File*)_metadata->file;               // Bring the pointer to actual file type
+               TagLib::ID3v2::Tag* tag2 = _file->ID3v2Tag();
+               metadata_editor_retvm_if(!tag2, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Error. No ID3v2 tag in file.");
+
+               TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
+               // Check if there are pictures in the tag
+               if (lst.isEmpty()) {
+                       metadata_editor_error("No pictures in file");
+                       return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+               }
 
-                       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
-                       // Check if there are pictures in the tag
-                       if (lst.isEmpty()) {
-                               metadata_editor_error("No pictures in file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       } else {                // pictures exist in file
-                               // Check if index is correct or not
-                               if ((index < 0) || (lst.size() <= (uint)index)) {
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                // everything is correct - begin extraction
-                                       metadata_editor_info("There are %u pictures in file. Start of picture number %d extraction", lst.size(), index);
-                                       int i = 0;
-                                       // Among all frames we must choose that one with specified index. "i" will be counter
-                                       for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
-                                               if (i != index) continue;
-                                               TagLib::ID3v2::AttachedPictureFrame* pictureFrame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(*it);
-                                               uint pictureSize = pictureFrame->picture().size();
-                                               metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
-                                               META_MALLOC(*picture, pictureSize);
-                                               metadata_editor_retvm_if(*picture == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                                               memcpy(*picture, pictureFrame->picture().data(), pictureSize);
-                                               *size = pictureSize;
-                                               TagLib::String mime = pictureFrame->mimeType();
-                                               if (!strcmp(mime.toCString(), "image/jpeg"))
-                                                       *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
-                                               else if (!strcmp(mime.toCString(), "image/png"))
-                                                       *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
-                                               else
-                                                       *mime_type = NULL;
-                                               break;
-                                       }
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       }
+               // Check if index is correct or not
+               if ((index < 0) || (lst.size() <= (uint)index)) {
+                       metadata_editor_error("Index of picture is out of range");
+                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
                }
-               case METADATA_EDITOR_FORMAT_MP4: {
-                       TagLib::MP4::File* _file = (TagLib::MP4::File*) _metadata->file;
-                       TagLib::MP4::Tag* tag = _file->tag();
-                       metadata_editor_retvm_if(tag == NULL, 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()) {                                                              // Item was found
-                               TagLib::MP4::CoverArtList lst = it->second.toCoverArtList();
-                               // Check if the index is in range of CoverArtList Item
-                               if ((index < 0) || ((uint)index >= lst.size())) {                               // it is not
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                                                                        // index is in range
-                                       int i = 0;
-                                       for (TagLib::MP4::CoverArtList::ConstIterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
-                                               if (i != index) continue;
-                                               int pictureSize = picIt->data().size();
-                                               metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
-                                               META_MALLOC(*picture, pictureSize);
-                                               metadata_editor_retvm_if(*picture == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                                               memcpy(*picture, picIt->data().data(), pictureSize);
-                                               *size = pictureSize;
-                                               if (picIt->format() == TagLib::MP4::CoverArt::JPEG)     *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
-                                               else if (picIt->format() == TagLib::MP4::CoverArt::PNG) *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
-                                               else                                                    *mime_type = NULL;
-                                               break;
-                                       }
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       } else {                                                                                // Item was not found - no pictures in file
-                               metadata_editor_error("No item <covr> in file. No pictures in file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       }
+               metadata_editor_info("There are %u pictures in file. Start of picture number %d extraction", lst.size(), index);
+
+               i = 0;
+               // Among all frames we must choose that one with specified index. "i" will be counter
+               for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
+                       if (i != index)
+                               continue;
+
+                       TagLib::ID3v2::AttachedPictureFrame* pictureFrame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(*it);
+                       uint pictureSize = pictureFrame->picture().size();
+                       metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
+
+                       *picture = g_memdup(pictureFrame->picture().data(), pictureSize);
+                       *size = pictureSize;
+
+                       TagLib::String mime = pictureFrame->mimeType();
+                       if (mime == "image/jpeg")
+                               *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
+                       else if (mime == "image/png")
+                               *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
+                       else
+                               *mime_type = NULL;
+                       break;
+               }
+
+               return METADATA_EDITOR_ERROR_NONE;
+       }
+
+       case METADATA_EDITOR_FORMAT_MP4: {
+               TagLib::MP4::File* _file = (TagLib::MP4::File*) _metadata->file;
+               TagLib::MP4::Tag* tag = _file->tag();
+               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()) {
+                       metadata_editor_error("No item <covr> in file. No pictures in file");
+                       return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+               }
+
+               TagLib::MP4::CoverArtList lst = it->second.toCoverArtList();
+
+               // Check if the index is in range of CoverArtList Item
+               if ((index < 0) || ((uint)index >= lst.size())) {                               // it is not
+                       metadata_editor_error("Index of picture is out of range");
+                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
+               }
+
+               i = 0;
+               for (TagLib::MP4::CoverArtList::ConstIterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
+                       if (i != index)
+                               continue;
+
+                       int pictureSize = picIt->data().size();
+                       metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
+
+                       *picture = g_memdup(picIt->data().data(), pictureSize);
+                       *size = pictureSize;
+
+                       if (picIt->format() == TagLib::MP4::CoverArt::JPEG)
+                               *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
+                       else if (picIt->format() == TagLib::MP4::CoverArt::PNG)
+                               *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
+                       else
+                               *mime_type = NULL;
+                       break;
                }
+
+               return METADATA_EDITOR_ERROR_NONE;
+
+       }
 #if 0
-               case METADATA_EDITOR_FORMAT_FLAC: {
-                       TagLib::FLAC::File* _file = (TagLib::FLAC::File*) _metadata->file;
-                       TagLib::List<TagLib::FLAC::Picture*> lst = _file->pictureList();
-                       if (lst.isEmpty()) {
-                               metadata_editor_error("No pictures in FLAC file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       } else {
-                               // Check if the index is in range of CoverArtList Item
-                               if ((index < 0) || ((uint)index >= lst.size())) {                       // it is not
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                                                                                                        // index is in range
-                                       // Consecutive check of all pictures until the desired one is found
-                                       int i = 0;
-                                       for (TagLib::List<TagLib::FLAC::Picture*>::ConstIterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
-                                               if (i != index) continue;
-                                               // picture can be received as ByteVector (picIt->data()).
-                                               // ByteVector has data() - picture itself and size() - the size of picture in data() method
-                                               int pictureSize = (*picIt)->data().size();
-                                               metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
-                                               META_MALLOC(*picture, pictureSize);
-                                               metadata_editor_retvm_if(*picture == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                                               memcpy(*picture, (*picIt)->data().data(), pictureSize);
-                                               *size = pictureSize;
-                                               TagLib::String mime = (*picIt)->mimeType();
-                                               if (!strcmp(mime.toCString(), "image/jpeg"))
-                                                       *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
-                                               else if (!strcmp(mime.toCString(), "image/png"))
-                                                       *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
-                                               else
-                                                       *mime_type = NULL;
-                                               break;
-                                       }
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       }
+       case METADATA_EDITOR_FORMAT_FLAC: {
+               TagLib::FLAC::File* _file = (TagLib::FLAC::File*) _metadata->file;
+               TagLib::List<TagLib::FLAC::Picture*> lst = _file->pictureList();
+
+               if (lst.isEmpty()) {
+                       metadata_editor_error("No pictures in FLAC file");
+                       return METADATA_EDITOR_ERROR_OPERATION_FAILED;
                }
-               case METADATA_EDITOR_FORMAT_WAV: {
-                       TagLib::RIFF::WAV::File* _file = (TagLib::RIFF::WAV::File*)_metadata->file;             // Bring the pointer to actual file type
-                       TagLib::ID3v2::Tag* tag2 = _file->tag();
-                       if (!tag2) {
-                               metadata_editor_error("No ID3v2 tag in file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       }
-                       TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
-                       // Check if there are pictures in the tag
-                       if (lst.isEmpty()) {
-                               metadata_editor_error("No pictures in file");
-                               return METADATA_EDITOR_ERROR_OPERATION_FAILED;
-                       } else {                                                // pictures exist in file
-                               // Check if index is correct or not
-                               if ((index < 0) || (lst.size() <= (uint)index)) {
-                                       metadata_editor_error("Index of picture is out of range");
-                                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
-                               } else {                                        // everything is correct - begin extraction
-                                       metadata_editor_info("There are %u pictures in file. Start of picture number %d extraction", lst.size(), index);
-                                       int i = 0;
-                                       // Among all frames we must choose that one with specified index. "i" will be counter
-                                       for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
-                                               if (i != index) continue;
-                                               TagLib::ID3v2::AttachedPictureFrame* pictureFrame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(*it);
-                                               uint pictureSize = pictureFrame->picture().size();
-                                               metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
-
-                                               META_MALLOC(*picture, pictureSize);
-                                               metadata_editor_retvm_if(*picture == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                                               memcpy(*picture, pictureFrame->picture().data(), pictureSize);
-                                               *size = pictureSize;
-                                               TagLib::String mime = pictureFrame->mimeType();
-                                               if (!strcmp(mime.toCString(), "image/jpeg"))
-                                                       *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
-                                               else if (!strcmp(mime.toCString(), "image/png"))
-                                                       *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
-                                               else
-                                                       *mime_type = NULL;
-                                               break;
-                                       }
-                                       return METADATA_EDITOR_ERROR_NONE;
-                               }
-                       }
+
+               // Check if the index is in range of CoverArtList Item
+               if ((index < 0) || ((uint)index >= lst.size())) {                       // it is not
+                       metadata_editor_error("Index of picture is out of range");
+                       return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
                }
-#endif
-               default:
-                       metadata_editor_error("Wrong file type");
+
+               // Consecutive check of all pictures until the desired one is found
+               i = 0;
+               for (TagLib::List<TagLib::FLAC::Picture*>::ConstIterator picIt = lst.begin(); picIt != lst.end(); ++picIt, ++i) {
+                       if (i != index)
+                               continue;
+
+                       // picture can be received as ByteVector (picIt->data()).
+                       // ByteVector has data() - picture itself and size() - the size of picture in data() method
+                       int pictureSize = (*picIt)->data().size();
+                       metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
+
+                       *picture = g_memdup((*picIt)->data().data(), pictureSize);
+                       *size = pictureSize;
+
+                       TagLib::String mime = (*picIt)->mimeType();
+                       if (mime == "image/jpeg")
+                               *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
+                       else if (mime == "image/png")
+                               *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
+                       else
+                               *mime_type = NULL;
+                       break;
+               }
+
+               return METADATA_EDITOR_ERROR_NONE;
+       }
+
+       case METADATA_EDITOR_FORMAT_WAV: {
+               TagLib::RIFF::WAV::File* _file = (TagLib::RIFF::WAV::File*)_metadata->file;             // Bring the pointer to actual file type
+               TagLib::ID3v2::Tag* tag2 = _file->tag();
+
+               if (!tag2) {
+                       metadata_editor_error("No ID3v2 tag in file");
+                       return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+               }
+
+               TagLib::ID3v2::FrameList lst = tag2->frameListMap()["APIC"];
+               // Check if there are pictures in the tag
+               if (lst.isEmpty()) {
+                       metadata_editor_error("No pictures in file");
+                       return METADATA_EDITOR_ERROR_OPERATION_FAILED;
+               }
+
+               // Check if index is correct or not
+               if ((index < 0) || (lst.size() <= (uint)index)) {
+                       metadata_editor_error("Index of picture is out of range");
                        return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
+               }
+
+               metadata_editor_info("There are %u pictures in file. Start of picture number %d extraction", lst.size(), index);
+               i = 0;
+               // Among all frames we must choose that one with specified index. "i" will be counter
+               for (TagLib::ID3v2::FrameList::Iterator it = lst.begin(); it != lst.end(); ++it, ++i) {
+                       if (i != index)
+                               continue;
+
+                       TagLib::ID3v2::AttachedPictureFrame* pictureFrame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(*it);
+                       uint pictureSize = pictureFrame->picture().size();
+                       metadata_editor_retvm_if(pictureSize == 0, METADATA_EDITOR_ERROR_OPERATION_FAILED, "Size of picture is 0");
+
+                       *picture = g_memdup(pictureFrame->picture().data(), pictureSize);
+                       *size = pictureSize;
+
+                       TagLib::String mime = pictureFrame->mimeType();
+                       if (mime == "image/jpeg")
+                               *mime_type = strndup(TYPE_JPEG, strlen(TYPE_JPEG));
+                       else if (mime == "image/png")
+                               *mime_type = strndup(TYPE_PNG, strlen(TYPE_PNG));
+                       else
+                               *mime_type = NULL;
+                       break;
+               }
+
+               return METADATA_EDITOR_ERROR_NONE;
+       }
+#endif
+       default:
+               metadata_editor_error("Wrong file type");
+               return METADATA_EDITOR_ERROR_INVALID_PARAMETER;
        }
 }