4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #define __USE_XOPEN /* needed for strptime */
33 #include <system_info.h>
35 #include <glib/gstdio.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include "media-util-err.h"
46 #include "media-svc-util.h"
47 #include "media-svc-db-utils.h"
48 #include "media-svc-debug.h"
49 #include "media-svc-env.h"
50 #include "media-svc-hash.h"
51 #include "media-svc-album.h"
52 #include "media-svc-localize_ch.h"
54 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
56 /* Define data structures for media type and mime type */
57 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
58 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
59 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
60 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
61 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
62 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
63 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
64 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
65 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA category */
67 #define CONTENT_TYPE_NUM 5
68 #define MUSIC_MIME_NUM 29
69 #define SOUND_MIME_NUM 1
70 #define MIME_TYPE_LENGTH 255
71 #define MIME_LENGTH 50
72 #define _3GP_FILE ".3gp"
73 #define _MP4_FILE ".mp4"
74 #define _ASF_FILE ".asf"
75 #define MEDIA_SVC_ARTWORK_SIZE 2000
76 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
78 #define MEDIA_SVC_DEFAULT_GPS_VALUE -200 /**< Default GPS Value*/
80 static int media_svc_pinyin_support = -1;
83 char content_type[15];
85 } _media_svc_content_table_s;
87 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
88 {"audio", MEDIA_SVC_CATEGORY_SOUND},
89 {"image", MEDIA_SVC_CATEGORY_IMAGE},
90 {"video", MEDIA_SVC_CATEGORY_VIDEO},
91 {"application", MEDIA_SVC_CATEGORY_ETC},
92 {"text", MEDIA_SVC_CATEGORY_ETC},
95 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
96 /*known mime types of normal files*/
102 /* known mime types of drm files*/
104 "x-mp3", /*alias of audio/mpeg*/
105 "x-mpeg", /*alias of audio/mpeg*/
107 "x-ogg", /*alias of audio/ogg*/
108 "vnd.ms-playready.media.pya:*.pya", /*playready*/
111 "x-m4a", /*alias of audio/mp4*/
112 /* below mimes are rare*/
122 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
123 "vorbis", /*alias of audio/x-vorbis+ogg*/
125 "x-mp2", /*alias of audio/mp2*/
126 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
127 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
131 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
135 char *_media_info_generate_uuid(void)
138 static char uuid_unparsed[37];
141 uuid_generate(uuid_value);
142 uuid_unparse(uuid_value, uuid_unparsed);
144 if (strlen(uuid_unparsed) < 36) {
145 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
149 return uuid_unparsed;
152 static int __media_svc_split_to_double(char *input, double *arr)
154 char tmp_arr[255] = {0, };
155 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
157 if (!STRING_VALID(input)) {
158 media_svc_error("Invalid parameter");
159 return MS_MEDIA_ERR_INVALID_PARAMETER;
161 memset(tmp_arr, 0x0, sizeof(tmp_arr));
163 /*media_svc_debug("input: [%s]", input); */
167 for (idx = 0; idx < (len + 1); idx++) {
168 if (input[idx] == ' ') {
170 } else if ((input[idx] == ',') || (idx == len)) {
171 arr[arr_idx] = atof(tmp_arr);
174 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
175 memset(tmp_arr, 0x0, sizeof(tmp_arr));
177 tmp_arr[str_idx] = input[idx];
183 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
184 return MS_MEDIA_ERR_INTERNAL;
187 return MS_MEDIA_ERR_NONE;
190 /* GPS information is not ExifTag member */
191 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
193 int ret = MS_MEDIA_ERR_NONE;
195 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
196 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
198 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
199 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
201 entry = exif_data_get_entry(ed, tagtype);
203 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
204 gps_buf[strlen(gps_buf)] = '\0';
206 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
207 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
209 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
212 return MS_MEDIA_ERR_NONE;
215 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
219 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
220 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
222 entry = exif_data_get_entry(ed, tagtype);
224 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
225 value[strlen(value)] = '\0';
228 return MS_MEDIA_ERR_NONE;
231 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
234 ExifByteOrder mByteOrder;
235 ExifRational mRational;
236 long numerator, denominator;
238 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
240 entry = exif_data_get_entry(ed, tagtype);
241 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
244 case EXIF_TAG_ORIENTATION:
245 case EXIF_TAG_PIXEL_X_DIMENSION:
246 case EXIF_TAG_PIXEL_Y_DIMENSION:
247 case EXIF_TAG_ISO_SPEED_RATINGS:
248 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
250 mByteOrder = exif_data_get_byte_order(ed);
251 short exif_value = exif_get_short(entry->data, mByteOrder);
252 *i_value = (int)exif_value;
254 case EXIF_TAG_EXPOSURE_TIME:
255 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
257 mByteOrder = exif_data_get_byte_order(ed);
258 mRational = exif_get_rational(entry->data, mByteOrder);
259 numerator = mRational.numerator;
260 denominator = mRational.denominator;
261 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
263 case EXIF_TAG_FNUMBER:
264 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
266 mByteOrder = exif_data_get_byte_order(ed);
267 mRational = exif_get_rational(entry->data, mByteOrder);
268 numerator = mRational.numerator;
269 denominator = mRational.denominator;
271 *d_value = ((numerator*1.0)/(denominator*1.0));
274 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
276 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
277 buf[strlen(buf)] = '\0';
280 return MS_MEDIA_ERR_NONE;
283 time_t __media_svc_get_timeline_from_str(const char *timstr)
286 time_t modified_t = 0;
290 if (!STRING_VALID(timstr)) {
291 media_svc_error("Invalid Parameter");
295 /*Exif Format : %Y:%m:%d %H:%M:%S
296 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
297 memset(&t, 0x00, sizeof(struct tm));
301 localtime_r(&rawtime, &timeinfo);
303 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
304 t.tm_isdst = timeinfo.tm_isdst;
306 media_svc_debug("DST %d", t.tm_isdst);
308 /* If time string has timezone */
309 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
311 char tim_tmp_str[255] = { 0, };
313 /* ISO8601 Time string format */
314 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
315 g_time_val_from_iso8601(tim_tmp_str, &timeval);
316 modified_t = timeval.tv_sec;
317 media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
320 modified_t = mktime(&t);
326 media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
328 media_svc_error("Failed to get timeline : [%s]", timstr);
334 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
338 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
339 media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
340 media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
342 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
344 /*categorize from mimetype */
345 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
346 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
347 *category = (*category | content_category[idx].category_by_mime);
352 /*in application type, exitst sound file ex) x-smafs, asf */
353 if (*category & MEDIA_SVC_CATEGORY_ETC) {
354 int prefix_len = strlen(content_category[3].content_type) + 1;
357 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
358 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
359 *category ^= MEDIA_SVC_CATEGORY_ETC;
360 *category |= MEDIA_SVC_CATEGORY_SOUND;
365 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
366 *category ^= MEDIA_SVC_CATEGORY_ETC;
367 *category |= MEDIA_SVC_CATEGORY_SOUND;
370 /*"asf" must check video stream and then categorize in directly. */
371 ext = strrchr(path, '.');
373 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
378 err = mm_file_get_stream_info(path, &audio, &video);
380 if (audio > 0 && video == 0) {
381 *category ^= MEDIA_SVC_CATEGORY_ETC;
382 *category |= MEDIA_SVC_CATEGORY_MUSIC;
384 *category ^= MEDIA_SVC_CATEGORY_ETC;
385 *category |= MEDIA_SVC_CATEGORY_VIDEO;
392 /*check music file in sound files. */
393 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
394 int prefix_len = strlen(content_category[0].content_type) + 1;
396 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
397 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
398 *category ^= MEDIA_SVC_CATEGORY_SOUND;
399 *category |= MEDIA_SVC_CATEGORY_MUSIC;
404 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
405 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
406 *category ^= MEDIA_SVC_CATEGORY_SOUND;
407 *category |= MEDIA_SVC_CATEGORY_ETC;
409 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
410 /*some video files don't have video stream. in this case it is categorize as music. */
412 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
413 ext = strrchr(path, '.');
415 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
420 err = mm_file_get_stream_info(path, &audio, &video);
422 if (audio > 0 && video == 0) {
423 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
424 *category |= MEDIA_SVC_CATEGORY_MUSIC;
427 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
432 return MS_MEDIA_ERR_NONE;
435 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
437 int ret = MS_MEDIA_ERR_NONE;
440 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
441 if (ret != MS_MEDIA_ERR_NONE)
442 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
445 case MEDIA_SVC_CATEGORY_SOUND:
446 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
448 case MEDIA_SVC_CATEGORY_MUSIC:
449 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
451 case MEDIA_SVC_CATEGORY_IMAGE:
452 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
454 case MEDIA_SVC_CATEGORY_VIDEO:
455 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
458 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
465 drm_contentifo is not NULL, if the file is OMA DRM.
466 If the file is not OMA DRM, drm_contentinfo must be NULL.
468 static int __media_svc_get_mime_type(const char *path, char *mimetype)
470 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
472 /*in case of normal files or failure to get mime in drm */
473 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
474 media_svc_error("aul_get_mime_from_file fail");
475 return MS_MEDIA_ERR_INTERNAL;
478 return MS_MEDIA_ERR_NONE;
481 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
485 for (i = strlen(file_path); i >= 0; i--) {
486 if (file_path[i] == '.') {
487 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
491 if (file_path[i] == '/')
497 static int __media_svc_resize_artwork(const char *path, const char *img_format)
499 int ret = MS_MEDIA_ERR_NONE;
500 unsigned int width = 0;
501 unsigned int height = 0;
502 unsigned int resized_width = 0;
503 unsigned int resized_height = 0;
504 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
506 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
507 media_svc_debug("type [jpeg]");
509 mm_util_extract_image_info(path, &img_type, &width, &height);
511 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
512 media_svc_debug("No need resizing");
513 return MS_MEDIA_ERR_NONE;
517 if (width > height) {
518 resized_height = MEDIA_SVC_ARTWORK_SIZE;
519 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
521 resized_width = MEDIA_SVC_ARTWORK_SIZE;
522 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
525 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
527 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
528 media_svc_debug("type [png]");
530 media_svc_debug("Not proper img format");
536 static int __media_svc_safe_atoi(char *buffer, int *si)
540 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
542 const long sl = strtol(buffer, &end, 10);
544 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
545 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
546 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
547 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
548 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
552 return MS_MEDIA_ERR_NONE;
555 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
557 int ret = MS_MEDIA_ERR_NONE;
559 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
562 media_svc_error("invalid image..");
563 return MS_MEDIA_ERR_INVALID_PARAMETER;
567 char *thumb_path = NULL;
568 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
569 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
571 if (-1 == statfs(thumb_path, &fs)) {
572 media_svc_error("error in statfs");
573 SAFE_FREE(thumb_path);
574 return MS_MEDIA_ERR_INTERNAL;
577 SAFE_FREE(thumb_path);
579 long bsize_kbytes = fs.f_bsize >> 10;
581 if ((bsize_kbytes * fs.f_bavail) < 1024) {
582 media_svc_error("not enought space...");
583 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
588 if (image != NULL && size > 0) {
589 fp = fopen(image_path, "w");
592 media_svc_error("failed to open file");
593 return MS_MEDIA_ERR_INTERNAL;
596 nwrite = fwrite(image, 1, size, fp);
597 if (nwrite != size) {
598 media_svc_error("failed to write thumbnail");
600 return MS_MEDIA_ERR_INTERNAL;
605 return MS_MEDIA_ERR_NONE;
608 static char *__media_svc_get_title_from_filepath(const char *path)
610 char *filename = NULL;
613 int filename_len = -1;
614 int new_title_len = -1;
617 media_svc_error("path is NULL");
621 filename = g_path_get_basename(path);
622 if (!STRING_VALID(filename)) {
623 media_svc_error("wrong file name");
628 filename_len = strlen(filename);
630 ext = g_strrstr(filename, ".");
632 media_svc_error("there is no file extention");
636 new_title_len = filename_len - strlen(ext);
637 if (new_title_len < 1) {
638 media_svc_error("title length is zero");
643 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
647 media_svc_debug("extract title is [%s]", title);
652 void _media_svc_remove_file(const char *path)
654 if (!STRING_VALID(path))
657 if (remove(path) != 0)
658 media_svc_stderror("fail to remove file result");
661 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
663 int ret = MS_MEDIA_ERR_NONE;
664 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
665 char hash[255 + 1] = {0, };
666 char *thumb_dir = NULL;
667 char *thumbfile_ext = NULL;
669 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
670 if (!STRING_VALID(thumb_dir)) {
671 media_svc_error("ms_user_get_root_thumb_store_path failed");
672 return MS_MEDIA_ERR_INTERNAL;
675 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
676 media_svc_error("Wrong path[%s]", thumb_dir);
677 SAFE_FREE(thumb_dir);
678 return MS_MEDIA_ERR_INTERNAL;
681 memset(file_ext, 0, sizeof(file_ext));
682 if (!__media_svc_get_file_ext(pathname, file_ext))
683 media_svc_error("get file ext fail");
685 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
686 if (ret != MS_MEDIA_ERR_NONE) {
687 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
688 SAFE_FREE(thumb_dir);
689 return MS_MEDIA_ERR_INTERNAL;
692 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
693 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
694 thumbfile_ext = (char *)"jpg";
695 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
696 thumbfile_ext = (char *)"png";
697 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
698 thumbfile_ext = (char *)"gif";
699 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
700 thumbfile_ext = (char *)"bmp";
702 media_svc_error("Not proper img format");
703 SAFE_FREE(thumb_dir);
704 return MS_MEDIA_ERR_INTERNAL;
707 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
709 if (strcasecmp(file_ext, "PNG") == 0)
710 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
712 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
715 SAFE_FREE(thumb_dir);
717 return MS_MEDIA_ERR_NONE;
720 int _media_svc_get_file_time(const char *full_path)
725 memset(&statbuf, 0, sizeof(struct stat));
726 fd = stat(full_path, &statbuf);
728 media_svc_sec_error("stat(%s) fails.", full_path);
729 return MS_MEDIA_ERR_INTERNAL;
732 return statbuf.st_mtime;
735 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
737 /* Set default GPS value before extracting meta information */
738 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
739 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
740 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
742 /* Set filename to title for all media */
744 title = __media_svc_get_title_from_filepath(content_info->path);
746 content_info->media_meta.title = g_strdup(title);
748 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
750 media_svc_error("Can't extract title");
751 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
752 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
755 /* Set default value before extracting meta information */
756 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
757 media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
759 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
760 media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
762 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
763 media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
765 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
766 media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
768 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
769 media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
771 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
772 media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
774 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
775 media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
777 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
778 media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
780 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
781 media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
784 media_svc_debug("refresh");
785 return MS_MEDIA_ERR_NONE;
788 content_info->favourate = 0;
789 content_info->media_meta.rating = 0;
791 return MS_MEDIA_ERR_NONE;
794 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
796 int ret = MS_MEDIA_ERR_NONE;
797 char * media_uuid = NULL;
798 bool drm_type = false;
799 char mime_type[256] = {0, };
800 media_svc_media_type_e media_type;
802 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
803 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
805 content_info->path = g_strdup(path);
806 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
809 memset(&st, 0, sizeof(struct stat));
810 if (stat(path, &st) == 0) {
811 content_info->modified_time = st.st_mtime;
812 content_info->timeline = content_info->modified_time;
813 content_info->size = st.st_size;
815 media_svc_stderror("stat failed");
818 _media_svc_set_default_value(content_info, refresh);
820 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
822 media_svc_debug("refresh");
823 return MS_MEDIA_ERR_NONE;
826 content_info->storage_uuid = g_strdup(storage_id);
827 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
829 content_info->storage_type = storage_type;
830 time(&content_info->added_time);
832 media_uuid = _media_info_generate_uuid();
833 if (media_uuid == NULL) {
834 _media_svc_destroy_content_info(content_info);
835 return MS_MEDIA_ERR_INTERNAL;
838 content_info->media_uuid = g_strdup(media_uuid);
839 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
841 content_info->file_name = g_path_get_basename(path);
842 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
844 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
845 if drm_contentinfo is not NULL, the file is OMA DRM.*/
846 ret = __media_svc_get_mime_type(path, mime_type);
847 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
849 media_svc_debug("mime [%s]", mime_type);
850 content_info->is_drm = drm_type;
852 ret = __media_svc_get_media_type(path, mime_type, &media_type);
853 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
855 content_info->mime_type = g_strdup(mime_type);
856 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
858 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
860 content_info->media_type = media_type;
862 return MS_MEDIA_ERR_NONE;
865 int image_360_check(char *path)
870 unsigned char exif_header[4] = {0, };
871 unsigned char exif_app1[2] = {0, };
872 unsigned char exif_app1_xmp[2] = {0, };
873 long exif_app1_xmp_size = 0;
874 unsigned char exif_app1_xmp_t[2] = {0, };
875 char *xmp_data = NULL;
881 memset(exif_header, 0x00, sizeof(exif_header));
882 memset(exif_app1, 0x00, sizeof(exif_app1));
883 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
884 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
886 fp = fopen(path, "rb");
890 size = fread(exif_header, 1, sizeof(exif_header), fp);
894 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
895 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
899 size1 = exif_app1[0];
900 size2 = exif_app1[1];
902 app1_size = size1 * 256 + size2 - 2;
904 if (fseek(fp, app1_size, SEEK_CUR) != 0)
907 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
911 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
914 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
918 size1 = exif_app1_xmp_t[0];
919 size2 = exif_app1_xmp_t[1];
921 exif_app1_xmp_size = size1 * 256 + size2 - 2;
923 if (exif_app1_xmp_size > 0) {
924 xmp_data = (char *)malloc(exif_app1_xmp_size);
925 memset(xmp_data, 0x0, exif_app1_xmp_size);
929 while (exif_app1_xmp_size >= 0) {
930 exif_app1_xmp_size--;
942 if (strstr(ptr, "UsePanoramaViewer")
943 && strstr(ptr, "True")
944 && strstr(ptr, "ProjectionType")
945 && strstr(ptr, "equirectangular"))
950 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
972 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
975 int orient_value = 0;
979 bool has_datetaken = false;
980 double fnumber = 0.0;
984 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
986 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
987 media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
988 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
990 path = content_info->path;
992 /* Load an ExifData object from an EXIF file */
993 ed = exif_data_new_from_file(path);
996 media_svc_sec_debug("There is no exif data in [ %s ]", path);
997 goto GET_WIDTH_HEIGHT;
1000 content_info->media_meta.is_360 = image_360_check(path);
1002 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1003 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1004 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1005 content_info->media_meta.fnumber = 0.0;
1006 content_info->media_meta.iso = 0;
1007 content_info->media_meta.orientation = 0;
1008 content_info->media_meta.width = 0;
1009 content_info->media_meta.height = 0;
1011 memset(buf, 0x00, sizeof(buf));
1012 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1013 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1014 if (!g_strcmp0(buf, "S"))
1016 content_info->media_meta.latitude = value;
1020 memset(buf, 0x00, sizeof(buf));
1021 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1022 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1023 if (!g_strcmp0(buf, "W"))
1026 content_info->media_meta.longitude = value;
1030 memset(buf, 0x00, sizeof(buf));
1031 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1032 if (strlen(buf) > 0) {
1033 SAFE_FREE(content_info->media_meta.description);
1034 content_info->media_meta.description = g_strdup(buf);
1038 memset(buf, 0x00, sizeof(buf));
1039 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1040 if (strlen(buf) > 0) {
1041 has_datetaken = true;
1042 content_info->media_meta.datetaken = g_strdup(buf);
1044 /* This is same as recorded_date */
1045 content_info->media_meta.recorded_date = g_strdup(buf);
1049 memset(buf, 0x00, sizeof(buf));
1050 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1051 if (strlen(buf) > 0) {
1052 has_datetaken = true;
1053 content_info->media_meta.datetaken = g_strdup(buf);
1055 /* This is same as recorded_date */
1056 content_info->media_meta.recorded_date = g_strdup(buf);
1060 if (has_datetaken) {
1061 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1062 if (content_info->timeline == 0)
1063 content_info->timeline = content_info->modified_time;
1065 media_svc_debug("Timeline : %ld", content_info->timeline);
1068 memset(buf, 0x00, sizeof(buf));
1069 /* Get exposure_time value from exif. */
1070 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1071 if (strlen(buf) > 0)
1072 content_info->media_meta.exposure_time = g_strdup(buf);
1075 /* Get fnumber value from exif. */
1076 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1078 content_info->media_meta.fnumber = fnumber;
1081 /* Get iso value from exif. */
1082 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1084 content_info->media_meta.iso = iso;
1087 memset(buf, 0x00, sizeof(buf));
1088 /* Get model value from exif. */
1089 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1090 if (strlen(buf) > 0)
1091 content_info->media_meta.model = g_strdup(buf);
1094 /* Get orientation value from exif. */
1095 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1096 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1097 content_info->media_meta.orientation = orient_value;
1100 /* Get width value from exif. */
1101 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1103 content_info->media_meta.width = exif_width;
1106 /* Get height value from exif. */
1107 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1108 if (exif_height > 0)
1109 content_info->media_meta.height = exif_height;
1113 exif_data_unref(ed);
1117 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1118 /*Get image width, height*/
1119 unsigned int img_width = 0;
1120 unsigned int img_height = 0;
1121 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1123 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1124 if (content_info->media_meta.width == 0)
1125 content_info->media_meta.width = img_width;
1127 if (content_info->media_meta.height == 0)
1128 content_info->media_meta.height = img_height;
1131 return MS_MEDIA_ERR_NONE;
1134 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1136 MMHandleType tag = 0;
1139 int mmf_error = FILEINFO_ERROR_NONE;
1141 content_info->path = g_strdup(path);
1143 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1144 if (mmf_error != FILEINFO_ERROR_NONE)
1145 return MS_MEDIA_ERR_INTERNAL;
1147 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1148 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1149 content_info->media_meta.album = g_strdup(p);
1151 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1152 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1153 content_info->media_meta.artist = g_strdup(p);
1155 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1156 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1157 content_info->media_meta.album_artist = g_strdup(p);
1159 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1160 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1161 content_info->media_meta.genre = g_strdup(p);
1163 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1164 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1165 while(p && isspace(*p))
1168 content_info->media_meta.title = g_strdup(p);
1171 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1172 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1173 content_info->media_meta.description = g_strdup(p);
1175 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1176 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1177 content_info->media_meta.composer = g_strdup(p);
1179 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1180 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1181 content_info->media_meta.copyright = g_strdup(p);
1183 mm_file_destroy_tag_attrs(tag);
1185 return MS_MEDIA_ERR_NONE;
1188 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1190 MMHandleType content = 0;
1191 MMHandleType tag = 0;
1193 unsigned char *image = NULL;
1194 unsigned int size = 0;
1195 int mmf_error = FILEINFO_ERROR_NONE;
1197 int ret = MS_MEDIA_ERR_NONE;
1200 /*Get Content Tag attribute ===========*/
1201 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1203 if (mmf_error == FILEINFO_ERROR_NONE) {
1204 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1205 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1206 content_info->media_meta.album = g_strdup(p);
1208 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1209 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1210 content_info->media_meta.artist = g_strdup(p);
1212 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1213 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1214 content_info->media_meta.album_artist = g_strdup(p);
1216 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1217 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1218 content_info->media_meta.genre = g_strdup(p);
1220 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1221 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1222 while(p && isspace(*p))
1225 content_info->media_meta.title = g_strdup(p);
1228 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1229 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1230 content_info->media_meta.description = g_strdup(p);
1232 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1234 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1235 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1236 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1237 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00 +0000 like exif time format*/
1238 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1239 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1242 content_info->media_meta.recorded_date = g_strdup(p);
1245 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1246 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1247 if (content_info->timeline == 0)
1248 content_info->timeline = content_info->modified_time;
1250 /* This is same as datetaken */
1251 /* Remove compensation string */
1252 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1253 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1254 g_free(content_info->media_meta.recorded_date);
1255 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1257 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1262 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1263 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1264 content_info->media_meta.composer = g_strdup(p);
1266 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1267 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1268 content_info->media_meta.copyright = g_strdup(p);
1270 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1271 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1272 content_info->media_meta.track_num = g_strdup(p);
1274 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1275 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1277 if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1278 content_info->media_meta.year = g_strdup(p);
1280 media_svc_debug("Wrong Year Information [%s]", p);
1283 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1284 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1286 if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1287 content_info->media_meta.rating = rate;
1289 content_info->media_meta.rating = 0;
1292 /*Do not extract artwork for the USB Storage content*/
1293 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1294 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1295 if (mmf_error != FILEINFO_ERROR_NONE) {
1296 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1298 /*media_svc_debug("artwork size1 [%d]", size); */
1301 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1302 if (mmf_error != FILEINFO_ERROR_NONE) {
1303 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1305 /*media_svc_debug("artwork size2 [%d]", size); */
1308 if (image != NULL && size > 0) {
1309 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1310 int artwork_mime_size = -1;
1312 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1313 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1314 ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1315 if (ret != MS_MEDIA_ERR_NONE) {
1316 media_svc_error("Fail to Get Thumbnail Path");
1318 ret = __media_svc_save_image(image, size, thumb_path, uid);
1319 if (ret != MS_MEDIA_ERR_NONE) {
1320 media_svc_error("Fail to Save Image");
1322 content_info->thumbnail_path = g_strdup(thumb_path);
1323 /* NOTICE : Prevent resize for performance (2020.02.07)
1324 * In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1325 * So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1328 /* albumart resizing */
1329 ret = __media_svc_resize_artwork(thumb_path, p);
1330 if (ret != MS_MEDIA_ERR_NONE) {
1331 media_svc_error("Fail to Make Thumbnail Image");
1332 _media_svc_remove_file(thumb_path);
1335 content_info->thumbnail_path = g_strdup(thumb_path);
1344 /*Initialize album_id to 0. below code will set the album_id*/
1345 content_info->album_id = album_id;
1346 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1347 if (ret != MS_MEDIA_ERR_NONE) {
1348 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1349 media_svc_debug("album does not exist. So start to make album art");
1350 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1351 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1352 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1354 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1356 content_info->album_id = album_id;
1359 content_info->album_id = album_id;
1362 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1363 double longitude = 0.0;
1364 double latitude = 0.0;
1365 double altitude = 0.0;
1367 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1368 MM_FILE_TAG_LATIDUE, &latitude,
1369 MM_FILE_TAG_ALTIDUE, &altitude,
1372 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1373 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1374 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1376 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1377 if (mmf_error != FILEINFO_ERROR_NONE)
1380 media_svc_debug("CDIS : %d", cdis_value);
1382 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1383 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1384 content_info->media_meta.orientation = atoi(p);
1386 content_info->media_meta.orientation = 0;
1387 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1391 mmf_error = mm_file_destroy_tag_attrs(tag);
1392 if (mmf_error != FILEINFO_ERROR_NONE)
1393 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1395 content_info->album_id = album_id;
1398 /*Get Content attribute ===========*/
1399 if (cdis_value == 1)
1400 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1402 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1404 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1406 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1407 int audio_bitrate = 0;
1408 int video_bitrate = 0;
1410 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1411 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1412 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1413 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1414 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1417 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1419 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1420 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1421 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1422 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1423 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1427 mm_file_destroy_content_attrs(content);
1429 return MS_MEDIA_ERR_NONE;
1432 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1434 media_svc_retm_if(content_info == NULL, "content info is NULL");
1436 /* Delete media_svc_content_info_s */
1437 SAFE_FREE(content_info->media_uuid);
1438 SAFE_FREE(content_info->path);
1439 SAFE_FREE(content_info->file_name);
1440 SAFE_FREE(content_info->mime_type);
1441 SAFE_FREE(content_info->folder_uuid);
1442 SAFE_FREE(content_info->thumbnail_path);
1443 SAFE_FREE(content_info->storage_uuid);
1445 /* Delete media_svc_content_meta_s */
1446 SAFE_FREE(content_info->media_meta.title);
1447 SAFE_FREE(content_info->media_meta.album);
1448 SAFE_FREE(content_info->media_meta.artist);
1449 SAFE_FREE(content_info->media_meta.album_artist);
1450 SAFE_FREE(content_info->media_meta.genre);
1451 SAFE_FREE(content_info->media_meta.composer);
1452 SAFE_FREE(content_info->media_meta.year);
1453 SAFE_FREE(content_info->media_meta.recorded_date);
1454 SAFE_FREE(content_info->media_meta.copyright);
1455 SAFE_FREE(content_info->media_meta.track_num);
1456 SAFE_FREE(content_info->media_meta.description);
1457 SAFE_FREE(content_info->media_meta.datetaken);
1458 SAFE_FREE(content_info->media_meta.exposure_time);
1459 SAFE_FREE(content_info->media_meta.model);
1461 SAFE_FREE(content_info->file_name_pinyin);
1462 SAFE_FREE(content_info->media_meta.title_pinyin);
1463 SAFE_FREE(content_info->media_meta.album_pinyin);
1464 SAFE_FREE(content_info->media_meta.artist_pinyin);
1465 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1466 SAFE_FREE(content_info->media_meta.genre_pinyin);
1467 SAFE_FREE(content_info->media_meta.composer_pinyin);
1468 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1469 SAFE_FREE(content_info->media_meta.description_pinyin);
1472 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1474 bool portrait = false;
1477 media_svc_retm_if(!orig_w, "Invalid orig_w");
1478 media_svc_retm_if(!orig_h, "Invalid orig_h");
1479 media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1480 media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1482 if (orig_w < orig_h)
1485 /* Set smaller length to default size */
1487 if (orig_w < *thumb_w)
1489 ratio = (double)orig_h / (double)orig_w;
1490 *thumb_h = *thumb_w * ratio;
1492 if (orig_h < *thumb_h)
1494 ratio = (double)orig_w / (double)orig_h;
1495 *thumb_w = *thumb_h * ratio;
1498 media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1503 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1505 int err = MS_MEDIA_ERR_NONE;
1506 MMHandleType tag = (MMHandleType) NULL;
1509 int _cdis_value = 0;
1510 mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1512 /* Get Content Tag attribute for orientation */
1513 err = mm_file_create_tag_attrs(&tag, origin_path);
1514 if (err != FILEINFO_ERROR_NONE) {
1515 *rot_type = MM_UTIL_ROTATE_0;
1520 err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1521 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1523 _rot_type = MM_UTIL_ROTATE_0;
1525 if (strncmp(p, "90", size) == 0)
1526 _rot_type = MM_UTIL_ROTATE_90;
1527 else if (strncmp(p, "180", size) == 0)
1528 _rot_type = MM_UTIL_ROTATE_180;
1529 else if (strncmp(p, "270", size) == 0)
1530 _rot_type = MM_UTIL_ROTATE_270;
1532 _rot_type = MM_UTIL_ROTATE_0;
1534 media_svc_debug("There is tag rotate : %d", _rot_type);
1536 media_svc_debug("There is NOT tag rotate");
1537 _rot_type = MM_UTIL_ROTATE_0;
1540 err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1541 if (err != FILEINFO_ERROR_NONE)
1544 *rot_type = _rot_type;
1545 *cdis_value = _cdis_value;
1547 err = mm_file_destroy_tag_attrs(tag);
1548 if (err != FILEINFO_ERROR_NONE) {
1549 media_svc_error("fail to free tag attr - err(%x)", err);
1555 static int __get_video_info(int cdis_value, const char *origin_path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *size)
1557 int err = MS_MEDIA_ERR_NONE;
1558 MMHandleType content = (MMHandleType) NULL;
1559 int _video_track_num = 0;
1563 void *_frame = NULL;
1565 if (cdis_value == 1) {
1566 media_svc_debug("This is CDIS vlaue 1");
1567 err = mm_file_create_content_attrs_safe(&content, origin_path);
1569 err = mm_file_create_content_attrs(&content, origin_path);
1572 if (err != FILEINFO_ERROR_NONE) {
1573 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1574 return MS_MEDIA_ERR_INTERNAL;
1577 err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1578 if (err != FILEINFO_ERROR_NONE) {
1579 media_svc_error("mm_file_get_attrs fails : %d", err);
1580 mm_file_destroy_content_attrs(content);
1581 return MS_MEDIA_ERR_INTERNAL;
1584 *video_track_num = _video_track_num;
1586 if (_video_track_num == 0) {
1587 mm_file_destroy_content_attrs(content);
1588 return MS_MEDIA_ERR_NONE;
1591 err = mm_file_get_attrs(content,
1592 MM_FILE_CONTENT_VIDEO_WIDTH,
1594 MM_FILE_CONTENT_VIDEO_HEIGHT,
1596 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1599 if (err != FILEINFO_ERROR_NONE) {
1600 media_svc_error("mm_file_get_attrs fails : %d", err);
1601 mm_file_destroy_content_attrs(content);
1602 return MS_MEDIA_ERR_INTERNAL;
1605 media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1606 if (!_frame || !_width || !_height) {
1607 mm_file_destroy_content_attrs(content);
1608 return MS_MEDIA_ERR_INTERNAL;
1615 *frame = calloc(1, _size);
1616 memcpy(*frame, _frame, _size);
1618 mm_file_destroy_content_attrs(content);
1620 return MS_MEDIA_ERR_NONE;
1623 static int __get_video_thumb(int width, int height, void *frame, size_t size, mm_util_magick_rotate_type rot_type, const char *thumb_path, unsigned int thumb_width, unsigned int thumb_height)
1625 int err = MS_MEDIA_ERR_NONE;
1626 mm_util_image_h img = NULL;
1627 mm_util_image_h resize_img = NULL;
1629 media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1630 __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1631 if (thumb_width <= 0 || thumb_height <= 0) {
1632 media_svc_error("Failed to get thumb size");
1633 return MS_MEDIA_ERR_INTERNAL;
1636 media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1638 err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1639 media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1641 if (width > thumb_width || height > thumb_height) {
1642 if (rot_type != MM_UTIL_ROTATE_0) {
1643 err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1644 if (err != MM_UTIL_ERROR_NONE)
1647 err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1649 err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1652 if (rot_type != MM_UTIL_ROTATE_0)
1653 err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1655 err = mm_util_resize_B_P(img, width, height, thumb_path);
1659 mm_image_destroy_image(img);
1660 mm_image_destroy_image(resize_img);
1661 if (err != MS_MEDIA_ERR_NONE)
1662 return MS_MEDIA_ERR_INTERNAL;
1664 return MS_MEDIA_ERR_NONE;
1667 static int __create_video_thumbnail(const char *path, char *thumb_path)
1669 int ret = MS_MEDIA_ERR_NONE;
1671 unsigned int width = 0;
1672 unsigned int height = 0;
1674 int video_track_num = 0;
1676 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1678 __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1679 ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1680 media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1681 media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1683 ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1690 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1692 int ret = MS_MEDIA_ERR_NONE;
1693 unsigned int origin_w = 0;
1694 unsigned int origin_h = 0;
1695 unsigned int thumb_w = THUMB_WIDTH;
1696 unsigned int thumb_h = THUMB_HEIGHT;
1697 mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1699 if (path == NULL || thumb_path == NULL) {
1700 media_svc_error("Invalid parameter");
1701 return MS_MEDIA_ERR_INVALID_PARAMETER;
1704 if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1705 media_svc_error("Original path(%s) doesn't exist.", path);
1706 return MS_MEDIA_ERR_INVALID_PARAMETER;
1709 ms_user_storage_type_e store_type = -1;
1710 ret = ms_user_get_storage_type(uid, path, &store_type);
1712 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1713 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1714 return MS_MEDIA_ERR_INVALID_PARAMETER;
1717 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1720 ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, THUMB_EXT, uid);
1721 if (ret != MS_MEDIA_ERR_NONE) {
1722 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1723 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1728 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1729 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1730 if (ret != MS_MEDIA_ERR_NONE) {
1731 media_svc_error("Getting image info is failed err: %d", ret);
1732 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1733 return MS_MEDIA_ERR_INTERNAL;
1736 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1737 media_svc_error("Unsupported image codec");
1738 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1741 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1742 media_svc_error("This original image is too big");
1743 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1744 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1747 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1748 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1749 if (ret != MM_UTIL_ERROR_NONE) {
1750 media_svc_error("mm_util_resize_P_P err: %d", ret);
1751 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1752 return MS_MEDIA_ERR_INTERNAL;
1755 return __create_video_thumbnail(path, thumb_path);
1761 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1763 int ret = MS_MEDIA_ERR_NONE;
1765 pinyin_name_s *pinyinname = NULL;
1767 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1768 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1772 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1773 if (ret == MS_MEDIA_ERR_NONE) {
1774 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1775 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1777 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1780 _media_svc_pinyin_free(pinyinname, size);
1785 bool _media_svc_check_pinyin_support(void)
1787 int ret = SYSTEM_INFO_ERROR_NONE;
1788 bool is_supported = false;
1790 if (media_svc_pinyin_support == -1) {
1791 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1792 if (ret != SYSTEM_INFO_ERROR_NONE) {
1793 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1797 media_svc_pinyin_support = is_supported;
1800 return media_svc_pinyin_support;
1803 int _media_svc_get_media_type(const char *path, int *mediatype)
1805 int ret = MS_MEDIA_ERR_NONE;
1806 char mime_type[256] = {0};
1807 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1809 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1811 ret = __media_svc_get_mime_type(path, mime_type);
1812 if (ret == MS_MEDIA_ERR_NONE)
1813 __media_svc_get_media_type(path, mime_type, &media_type);
1815 media_svc_error("__media_svc_get_mime_type failed");
1817 *mediatype = media_type;
1822 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1824 switch (storage_type) {
1825 case MS_USER_STORAGE_INTERNAL:
1826 case MS_USER_STORAGE_EXTERNAL:
1827 case MS_USER_STORAGE_EXTERNAL_USB:
1830 media_svc_error("storage type is incorrect[%d]", storage_type);