#include "metadata/metadata_file_handle.h"
+#include <image_util.h>
+
#include "common/logger.h"
#include "common/platform_enum.h"
#include "common/platform_exception.h"
return PlatformResult(ErrorCode::NO_ERROR);
}
+// convert frame buffer from raw RGB888 to JPG encoding
+PlatformResult MetadataFileHandle::EncodeJPEG(unsigned char* frame, size_t frame_size,
+ std::uint8_t** uint8_frame, size_t* buffer_size) {
+ ScopeLogger();
+ image_util_type_e encoder_type = IMAGE_UTIL_JPEG;
+ image_util_encode_h encode_handle = nullptr;
+ image_util_image_h image = nullptr;
+ SCOPE_EXIT {
+ image_util_encode_destroy(encode_handle);
+ image_util_destroy_image(image);
+ };
+
+ int ret = image_util_encode_create(encoder_type, &encode_handle);
+ if (IMAGE_UTIL_ERROR_NONE != ret) {
+ LoggerE("Error during jpg encoding: %d", ret);
+ return convertErrorCode(ret);
+ }
+
+ char* value = nullptr;
+ int native_res = metadata_extractor_get_metadata(handle_, METADATA_VIDEO_WIDTH, &value);
+ if (METADATA_EXTRACTOR_ERROR_NONE != native_res) {
+ LoggerE("Error during getting metadata: %d", native_res);
+ return convertErrorCode(native_res);
+ }
+ unsigned int width = (unsigned long)atoi(value);
+ free(value);
+ value = nullptr;
+
+ native_res = metadata_extractor_get_metadata(handle_, METADATA_VIDEO_HEIGHT, &value);
+ if (METADATA_EXTRACTOR_ERROR_NONE != native_res) {
+ LoggerE("Error during getting metadata: %d", native_res);
+ return convertErrorCode(native_res);
+ }
+ unsigned int height = (unsigned long)atoi(value);
+ free(value);
+ value = nullptr;
+
+ ret = image_util_create_image(width, height, IMAGE_UTIL_COLORSPACE_RGB888, frame, frame_size,
+ &image);
+ if (IMAGE_UTIL_ERROR_NONE != ret) {
+ LoggerE("Error during jpg encoding: %d", ret);
+ return convertErrorCode(ret);
+ }
+
+ unsigned char* jpg_buffer = nullptr;
+ ret = image_util_encode_run_to_buffer(encode_handle, image, &jpg_buffer, buffer_size);
+ if (IMAGE_UTIL_ERROR_NONE != ret) {
+ LoggerE("Error during jpg encoding: %d", ret);
+ return convertErrorCode(ret);
+ }
+
+ // jpg_buffer is passed outside function and need to be released after usage
+ *uint8_frame = (std::uint8_t*)jpg_buffer;
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult MetadataFileHandle::GetFrame(std::vector<std::uint8_t>* frame_array) {
+ ScopeLogger();
+ int size = 0;
+ size_t buffer_size = 0;
+ void* frame = nullptr;
+ char* mime_type = nullptr;
+ std::uint8_t* uint8_frame = nullptr;
+ SCOPE_EXIT {
+ free(frame);
+ free(mime_type);
+ free(uint8_frame);
+ };
+
+ int native_res = metadata_extractor_get_frame(handle_, &frame, &size);
+ if (METADATA_EXTRACTOR_ERROR_NONE != native_res) {
+ LoggerE("Error during getting metadata: %d", native_res);
+ return convertErrorCode(native_res);
+ }
+ LoggerD("Frame size %d", size);
+
+ // encode as JPEG
+ auto ret = EncodeJPEG((unsigned char*)frame, (size_t)size, &uint8_frame, &buffer_size);
+ if (!ret) {
+ return ret;
+ }
+
+ // convert frame to vector
+ std::vector<std::uint8_t> out_buf(uint8_frame, uint8_frame + buffer_size);
+
+ *frame_array = std::move(out_buf);
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
} // namespace metadata
} // namespace extension
REGISTER_METHOD(MetadataManagerCreateFileHandle);
REGISTER_METHOD(MetadataFileHandleGet);
REGISTER_METHOD(MetadataFileHandleGetArtwork);
+ REGISTER_METHOD(MetadataFileHandleGetThumbnailFrame);
REGISTER_METHOD(MetadataFileHandleRelease);
#undef REGISTER_METHOD
}
ReportSuccess(result_artwork, out);
}
+void MetadataInstance::MetadataFileHandleGetThumbnailFrame(const picojson::value& args,
+ picojson::object& out) {
+ ScopeLogger();
+ const std::string& uri = args.get("uri").get<std::string>();
+ const int id = static_cast<int>(args.get("id").get<double>());
+
+ auto res = InitializeHandleIfNeeded(uri, id);
+ if (!res) {
+ LogAndReportError(res, &out);
+ return;
+ }
+
+ std::vector<std::uint8_t> data;
+ std::string mime_type;
+ try {
+ MetadataFileHandle& m = handles_map_.at(id);
+
+ auto res = m.GetFrame(&data);
+ if (!res) {
+ LogAndReportError(res, &out);
+ return;
+ }
+ } catch (...) {
+ // this should never happen
+ LogAndReportError(
+ PlatformResult(ErrorCode::ABORT_ERR, "Unknown error during accessing a file."), &out);
+ return;
+ }
+
+ std::string result_artwork;
+ encode_binary_in_string(data, result_artwork);
+
+ ReportSuccess(picojson::value(result_artwork), out);
+}
+
void MetadataInstance::MetadataFileHandleRelease(const picojson::value& args,
picojson::object& out) {
ScopeLogger();