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*/
81 char content_type[15];
83 } _media_svc_content_table_s;
85 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
86 {"audio", MEDIA_SVC_CATEGORY_SOUND},
87 {"image", MEDIA_SVC_CATEGORY_IMAGE},
88 {"video", MEDIA_SVC_CATEGORY_VIDEO},
89 {"application", MEDIA_SVC_CATEGORY_ETC},
90 {"text", MEDIA_SVC_CATEGORY_ETC},
93 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
94 /*known mime types of normal files*/
100 /* known mime types of drm files*/
102 "x-mp3", /*alias of audio/mpeg*/
103 "x-mpeg", /*alias of audio/mpeg*/
105 "x-ogg", /*alias of audio/ogg*/
106 "vnd.ms-playready.media.pya:*.pya", /*playready*/
109 "x-m4a", /*alias of audio/mp4*/
110 /* below mimes are rare*/
120 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
121 "vorbis", /*alias of audio/x-vorbis+ogg*/
123 "x-mp2", /*alias of audio/mp2*/
124 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
125 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
129 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
133 char *_media_info_generate_uuid(void)
136 static char uuid_unparsed[37];
139 uuid_generate(uuid_value);
140 uuid_unparse(uuid_value, uuid_unparsed);
142 if (strlen(uuid_unparsed) < 36) {
143 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
147 return uuid_unparsed;
150 static int __media_svc_split_to_double(char *input, double *arr)
152 char tmp_arr[255] = {0, };
153 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
155 if (!STRING_VALID(input)) {
156 media_svc_error("Invalid parameter");
157 return MS_MEDIA_ERR_INVALID_PARAMETER;
159 memset(tmp_arr, 0x0, sizeof(tmp_arr));
161 /*media_svc_debug("input: [%s]", input); */
165 for (idx = 0; idx < (len + 1); idx++) {
166 if (input[idx] == ' ') {
168 } else if ((input[idx] == ',') || (idx == len)) {
169 arr[arr_idx] = atof(tmp_arr);
172 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
173 memset(tmp_arr, 0x0, sizeof(tmp_arr));
175 tmp_arr[str_idx] = input[idx];
181 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
182 return MS_MEDIA_ERR_INTERNAL;
185 return MS_MEDIA_ERR_NONE;
188 /* GPS information is not ExifTag member */
189 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
191 int ret = MS_MEDIA_ERR_NONE;
193 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
194 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
196 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
197 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
199 entry = exif_data_get_entry(ed, tagtype);
201 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
202 gps_buf[strlen(gps_buf)] = '\0';
204 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
205 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
207 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
210 return MS_MEDIA_ERR_NONE;
213 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
217 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
218 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
220 entry = exif_data_get_entry(ed, tagtype);
222 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
223 value[strlen(value)] = '\0';
226 return MS_MEDIA_ERR_NONE;
229 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
232 ExifByteOrder mByteOrder;
233 ExifRational mRational;
234 long numerator, denominator;
236 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
238 entry = exif_data_get_entry(ed, tagtype);
239 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
242 case EXIF_TAG_ORIENTATION:
243 case EXIF_TAG_PIXEL_X_DIMENSION:
244 case EXIF_TAG_PIXEL_Y_DIMENSION:
245 case EXIF_TAG_ISO_SPEED_RATINGS:
246 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
248 mByteOrder = exif_data_get_byte_order(ed);
249 short exif_value = exif_get_short(entry->data, mByteOrder);
250 *i_value = (int)exif_value;
252 case EXIF_TAG_EXPOSURE_TIME:
253 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
255 mByteOrder = exif_data_get_byte_order(ed);
256 mRational = exif_get_rational(entry->data, mByteOrder);
257 numerator = mRational.numerator;
258 denominator = mRational.denominator;
259 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
261 case EXIF_TAG_FNUMBER:
262 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
264 mByteOrder = exif_data_get_byte_order(ed);
265 mRational = exif_get_rational(entry->data, mByteOrder);
266 numerator = mRational.numerator;
267 denominator = mRational.denominator;
269 *d_value = ((numerator*1.0)/(denominator*1.0));
272 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
274 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
275 buf[strlen(buf)] = '\0';
278 return MS_MEDIA_ERR_NONE;
281 static time_t __media_svc_get_timeline_from_str(const char *timstr)
284 time_t modified_t = 0;
288 if (!STRING_VALID(timstr)) {
289 media_svc_error("Invalid Parameter");
293 /*Exif Format : %Y:%m:%d %H:%M:%S
294 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
295 memset(&t, 0x00, sizeof(struct tm));
299 localtime_r(&rawtime, &timeinfo);
301 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
302 t.tm_isdst = timeinfo.tm_isdst;
304 media_svc_debug("DST %d", t.tm_isdst);
306 /* If time string has timezone */
307 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
309 char tim_tmp_str[255] = { 0, };
311 /* ISO8601 Time string format */
312 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
313 g_time_val_from_iso8601(tim_tmp_str, &timeval);
314 modified_t = timeval.tv_sec;
315 media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
318 modified_t = mktime(&t);
324 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);
326 media_svc_error("Failed to get timeline : [%s]", timstr);
332 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
336 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
337 media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
338 media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
340 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
342 /*categorize from mimetype */
343 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
344 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
345 *category = (*category | content_category[idx].category_by_mime);
350 /*in application type, exitst sound file ex) x-smafs, asf */
351 if (*category & MEDIA_SVC_CATEGORY_ETC) {
352 int prefix_len = strlen(content_category[3].content_type) + 1;
355 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
356 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
357 *category ^= MEDIA_SVC_CATEGORY_ETC;
358 *category |= MEDIA_SVC_CATEGORY_SOUND;
363 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
364 *category ^= MEDIA_SVC_CATEGORY_ETC;
365 *category |= MEDIA_SVC_CATEGORY_SOUND;
368 /*"asf" must check video stream and then categorize in directly. */
369 ext = strrchr(path, '.');
371 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
376 err = mm_file_get_stream_info(path, &audio, &video);
378 if (audio > 0 && video == 0) {
379 *category ^= MEDIA_SVC_CATEGORY_ETC;
380 *category |= MEDIA_SVC_CATEGORY_MUSIC;
382 *category ^= MEDIA_SVC_CATEGORY_ETC;
383 *category |= MEDIA_SVC_CATEGORY_VIDEO;
390 /*check music file in sound files. */
391 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
392 int prefix_len = strlen(content_category[0].content_type) + 1;
394 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
395 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
396 *category ^= MEDIA_SVC_CATEGORY_SOUND;
397 *category |= MEDIA_SVC_CATEGORY_MUSIC;
402 /*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*/
403 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
404 *category ^= MEDIA_SVC_CATEGORY_SOUND;
405 *category |= MEDIA_SVC_CATEGORY_ETC;
407 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
408 /*some video files don't have video stream. in this case it is categorize as music. */
410 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
411 ext = strrchr(path, '.');
413 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
418 err = mm_file_get_stream_info(path, &audio, &video);
420 if (audio > 0 && video == 0) {
421 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
422 *category |= MEDIA_SVC_CATEGORY_MUSIC;
425 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
430 return MS_MEDIA_ERR_NONE;
433 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
435 int ret = MS_MEDIA_ERR_NONE;
438 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
439 if (ret != MS_MEDIA_ERR_NONE)
440 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
443 case MEDIA_SVC_CATEGORY_SOUND:
444 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
446 case MEDIA_SVC_CATEGORY_MUSIC:
447 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
449 case MEDIA_SVC_CATEGORY_IMAGE:
450 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
452 case MEDIA_SVC_CATEGORY_VIDEO:
453 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
456 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
463 drm_contentifo is not NULL, if the file is OMA DRM.
464 If the file is not OMA DRM, drm_contentinfo must be NULL.
466 static int __media_svc_get_mime_type(const char *path, char *mimetype)
468 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
470 /*in case of normal files or failure to get mime in drm */
471 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
472 media_svc_error("aul_get_mime_from_file fail");
473 return MS_MEDIA_ERR_INTERNAL;
476 return MS_MEDIA_ERR_NONE;
479 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
483 for (i = strlen(file_path); i >= 0; i--) {
484 if (file_path[i] == '.') {
485 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
489 if (file_path[i] == '/')
495 static int __media_svc_resize_artwork(const char *path, const char *img_format)
497 int ret = MS_MEDIA_ERR_NONE;
498 unsigned int width = 0;
499 unsigned int height = 0;
500 unsigned int resized_width = 0;
501 unsigned int resized_height = 0;
502 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
504 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
505 media_svc_debug("type [jpeg]");
507 mm_util_extract_image_info(path, &img_type, &width, &height);
509 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
510 media_svc_debug("No need resizing");
511 return MS_MEDIA_ERR_NONE;
515 if (width > height) {
516 resized_height = MEDIA_SVC_ARTWORK_SIZE;
517 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
519 resized_width = MEDIA_SVC_ARTWORK_SIZE;
520 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
523 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
525 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
526 media_svc_debug("type [png]");
528 media_svc_debug("Not proper img format");
534 static int __media_svc_safe_atoi(char *buffer, int *si)
538 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
540 const long sl = strtol(buffer, &end, 10);
542 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
543 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
544 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
545 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
546 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
550 return MS_MEDIA_ERR_NONE;
553 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
555 int ret = MS_MEDIA_ERR_NONE;
557 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
560 media_svc_error("invalid image..");
561 return MS_MEDIA_ERR_INVALID_PARAMETER;
565 char *thumb_path = NULL;
566 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
567 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
569 if (-1 == statfs(thumb_path, &fs)) {
570 media_svc_error("error in statfs");
571 SAFE_FREE(thumb_path);
572 return MS_MEDIA_ERR_INTERNAL;
575 SAFE_FREE(thumb_path);
577 long bsize_kbytes = fs.f_bsize >> 10;
579 if ((bsize_kbytes * fs.f_bavail) < 1024) {
580 media_svc_error("not enought space...");
581 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
586 if (image != NULL && size > 0) {
587 fp = fopen(image_path, "w");
590 media_svc_error("failed to open file");
591 return MS_MEDIA_ERR_INTERNAL;
594 nwrite = fwrite(image, 1, size, fp);
595 if (nwrite != size) {
596 media_svc_error("failed to write thumbnail");
598 return MS_MEDIA_ERR_INTERNAL;
603 return MS_MEDIA_ERR_NONE;
606 static char *__media_svc_get_title_from_filepath(const char *path)
608 char *filename = NULL;
611 int filename_len = -1;
612 int new_title_len = -1;
615 media_svc_error("path is NULL");
619 filename = g_path_get_basename(path);
620 if (!STRING_VALID(filename)) {
621 media_svc_error("wrong file name");
626 filename_len = strlen(filename);
628 ext = g_strrstr(filename, ".");
630 media_svc_error("there is no file extention");
634 new_title_len = filename_len - strlen(ext);
635 if (new_title_len < 1) {
636 media_svc_error("title length is zero");
641 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
645 media_svc_debug("extract title is [%s]", title);
650 void _media_svc_remove_file(const char *path)
652 if (!STRING_VALID(path))
655 if (remove(path) != 0)
656 media_svc_stderror("fail to remove file result");
659 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)
661 int ret = MS_MEDIA_ERR_NONE;
662 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
663 char hash[255 + 1] = {0, };
664 char *thumb_dir = NULL;
665 char *thumbfile_ext = NULL;
667 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
668 if (!STRING_VALID(thumb_dir)) {
669 media_svc_error("ms_user_get_root_thumb_store_path failed");
670 return MS_MEDIA_ERR_INTERNAL;
673 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
674 media_svc_error("Wrong path[%s]", thumb_dir);
675 SAFE_FREE(thumb_dir);
676 return MS_MEDIA_ERR_INTERNAL;
679 memset(file_ext, 0, sizeof(file_ext));
680 if (!__media_svc_get_file_ext(pathname, file_ext))
681 media_svc_error("get file ext fail");
683 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
684 if (ret != MS_MEDIA_ERR_NONE) {
685 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
686 SAFE_FREE(thumb_dir);
687 return MS_MEDIA_ERR_INTERNAL;
690 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
691 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
692 thumbfile_ext = (char *)"jpg";
693 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
694 thumbfile_ext = (char *)"png";
695 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
696 thumbfile_ext = (char *)"gif";
697 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
698 thumbfile_ext = (char *)"bmp";
700 media_svc_error("Not proper img format");
701 SAFE_FREE(thumb_dir);
702 return MS_MEDIA_ERR_INTERNAL;
705 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
707 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
710 SAFE_FREE(thumb_dir);
712 return MS_MEDIA_ERR_NONE;
715 int _media_svc_get_file_time(const char *full_path)
717 struct stat statbuf = { 0, };
719 if (stat(full_path, &statbuf) == -1) {
720 media_svc_stderror("stat fails.");
724 return statbuf.st_mtime;
727 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)
729 int ret = MS_MEDIA_ERR_NONE;
730 char * media_uuid = NULL;
731 bool drm_type = false;
732 char mime_type[256] = {0, };
733 media_svc_media_type_e media_type;
735 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
736 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
738 content_info->path = g_strdup(path);
739 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
742 memset(&st, 0, sizeof(struct stat));
743 if (stat(path, &st) == 0) {
744 content_info->modified_time = st.st_mtime;
745 content_info->timeline = content_info->modified_time;
746 content_info->size = st.st_size;
748 media_svc_stderror("stat failed");
751 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
753 media_svc_debug("refresh");
754 return MS_MEDIA_ERR_NONE;
757 content_info->storage_uuid = g_strdup(storage_id);
758 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
760 content_info->storage_type = storage_type;
761 time(&content_info->added_time);
763 media_uuid = _media_info_generate_uuid();
764 if (media_uuid == NULL) {
765 _media_svc_destroy_content_info(content_info);
766 return MS_MEDIA_ERR_INTERNAL;
769 content_info->media_uuid = g_strdup(media_uuid);
770 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
772 content_info->file_name = g_path_get_basename(path);
773 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
775 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
776 if drm_contentinfo is not NULL, the file is OMA DRM.*/
777 ret = __media_svc_get_mime_type(path, mime_type);
778 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
780 media_svc_debug("mime [%s]", mime_type);
781 content_info->is_drm = drm_type;
783 ret = __media_svc_get_media_type(path, mime_type, &media_type);
784 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
786 content_info->mime_type = g_strdup(mime_type);
787 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
789 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
791 content_info->media_type = media_type;
793 return MS_MEDIA_ERR_NONE;
796 int image_360_check(char *path)
801 unsigned char exif_header[4] = {0, };
802 unsigned char exif_app1[2] = {0, };
803 unsigned char exif_app1_xmp[2] = {0, };
804 long exif_app1_xmp_size = 0;
805 unsigned char exif_app1_xmp_t[2] = {0, };
806 char *xmp_data = NULL;
812 memset(exif_header, 0x00, sizeof(exif_header));
813 memset(exif_app1, 0x00, sizeof(exif_app1));
814 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
815 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
817 fp = fopen(path, "rb");
821 size = fread(exif_header, 1, sizeof(exif_header), fp);
825 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
826 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
830 size1 = exif_app1[0];
831 size2 = exif_app1[1];
833 app1_size = size1 * 256 + size2 - 2;
835 if (fseek(fp, app1_size, SEEK_CUR) != 0)
838 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
842 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
845 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
849 size1 = exif_app1_xmp_t[0];
850 size2 = exif_app1_xmp_t[1];
852 exif_app1_xmp_size = size1 * 256 + size2 - 2;
854 if (exif_app1_xmp_size > 0) {
855 xmp_data = (char *)malloc(exif_app1_xmp_size);
856 memset(xmp_data, 0x0, exif_app1_xmp_size);
860 while (exif_app1_xmp_size >= 0) {
861 exif_app1_xmp_size--;
873 if (strstr(ptr, "UsePanoramaViewer")
874 && strstr(ptr, "True")
875 && strstr(ptr, "ProjectionType")
876 && strstr(ptr, "equirectangular"))
881 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
903 static char * __media_svc_get_title(MMHandleType tag, const char *path)
905 int ret = FILEINFO_ERROR_NONE;
911 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
912 if (ret == FILEINFO_ERROR_NONE && size > 0) {
913 while(p && isspace(*p))
920 title = __media_svc_get_title_from_filepath(path);
924 media_svc_error("Can't extract title");
926 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
929 char * _media_svc_get_title_by_path(const char *path)
931 /* No MMHandleType in media-svc.c */
932 return __media_svc_get_title(NULL, path);
935 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
938 int orient_value = 0;
942 bool has_datetaken = false;
943 double fnumber = 0.0;
947 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
949 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
950 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);
951 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
953 path = content_info->path;
954 content_info->media_meta.title = __media_svc_get_title(NULL, path);
956 /* Load an ExifData object from an EXIF file */
957 ed = exif_data_new_from_file(path);
960 media_svc_sec_debug("There is no exif data in [ %s ]", path);
961 goto GET_WIDTH_HEIGHT;
964 content_info->media_meta.is_360 = image_360_check(path);
966 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
967 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
968 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
970 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
971 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
972 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
973 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
974 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
975 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
976 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
977 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
978 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
980 memset(buf, 0x00, sizeof(buf));
981 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
982 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
983 if (!g_strcmp0(buf, "S"))
985 content_info->media_meta.latitude = value;
989 memset(buf, 0x00, sizeof(buf));
990 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
991 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
992 if (!g_strcmp0(buf, "W"))
995 content_info->media_meta.longitude = value;
999 memset(buf, 0x00, sizeof(buf));
1000 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1001 if (strlen(buf) > 0)
1002 content_info->media_meta.description = g_strdup(buf);
1004 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1008 memset(buf, 0x00, sizeof(buf));
1009 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1010 if (strlen(buf) > 0) {
1011 has_datetaken = true;
1012 content_info->media_meta.datetaken = g_strdup(buf);
1014 /* This is same as recorded_date */
1015 content_info->media_meta.recorded_date = g_strdup(buf);
1019 memset(buf, 0x00, sizeof(buf));
1020 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1021 if (strlen(buf) > 0) {
1022 has_datetaken = true;
1023 content_info->media_meta.datetaken = g_strdup(buf);
1025 /* This is same as recorded_date */
1026 content_info->media_meta.recorded_date = g_strdup(buf);
1030 if (has_datetaken) {
1031 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1032 if (content_info->timeline == 0)
1033 content_info->timeline = content_info->modified_time;
1035 media_svc_debug("Timeline : %ld", content_info->timeline);
1038 memset(buf, 0x00, sizeof(buf));
1039 /* Get exposure_time value from exif. */
1040 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1041 if (strlen(buf) > 0)
1042 content_info->media_meta.exposure_time = g_strdup(buf);
1045 /* Get fnumber value from exif. */
1046 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1047 content_info->media_meta.fnumber = fnumber;
1049 /* Get iso value from exif. */
1050 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1051 content_info->media_meta.iso = iso;
1053 memset(buf, 0x00, sizeof(buf));
1054 /* Get model value from exif. */
1055 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1056 if (strlen(buf) > 0)
1057 content_info->media_meta.model = g_strdup(buf);
1060 /* Get orientation value from exif. */
1061 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1062 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1063 content_info->media_meta.orientation = orient_value;
1066 /* Get width value from exif. */
1067 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1069 content_info->media_meta.width = exif_width;
1072 /* Get height value from exif. */
1073 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1074 if (exif_height > 0)
1075 content_info->media_meta.height = exif_height;
1079 exif_data_unref(ed);
1083 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1084 /*Get image width, height*/
1085 unsigned int img_width = 0;
1086 unsigned int img_height = 0;
1087 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1089 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1090 if (content_info->media_meta.width == 0)
1091 content_info->media_meta.width = img_width;
1093 if (content_info->media_meta.height == 0)
1094 content_info->media_meta.height = img_height;
1097 return MS_MEDIA_ERR_NONE;
1100 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1102 int ret = FILEINFO_ERROR_NONE;
1106 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1107 if (ret == FILEINFO_ERROR_NONE && size > 0)
1110 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1113 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1115 MMHandleType tag = 0;
1116 int mmf_error = FILEINFO_ERROR_NONE;
1118 content_info->path = g_strdup(path);
1120 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1121 if (mmf_error == FILEINFO_ERROR_NONE) {
1122 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1123 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1124 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1125 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1126 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1127 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1128 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1129 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1131 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1134 mm_file_destroy_tag_attrs(tag);
1136 return MS_MEDIA_ERR_NONE;
1139 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1141 MMHandleType content = 0;
1142 MMHandleType tag = 0;
1144 unsigned char *image = NULL;
1145 unsigned int size = 0;
1146 int mmf_error = FILEINFO_ERROR_NONE;
1148 int ret = MS_MEDIA_ERR_NONE;
1149 int convert_value = 0;
1152 /*Get Content Tag attribute ===========*/
1153 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1155 if (mmf_error == FILEINFO_ERROR_NONE) {
1156 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1157 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1158 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1159 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1160 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1161 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1162 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1163 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1164 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1166 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1167 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1168 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1169 /*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*/
1170 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1171 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1174 content_info->media_meta.recorded_date = g_strdup(p);
1177 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1178 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1179 if (content_info->timeline == 0)
1180 content_info->timeline = content_info->modified_time;
1182 /* This is same as datetaken */
1183 /* Remove compensation string */
1184 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1185 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1186 g_free(content_info->media_meta.recorded_date);
1187 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1189 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1194 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1195 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1196 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1197 content_info->media_meta.year = g_strdup(p);
1200 if (!content_info->media_meta.year)
1201 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1203 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1204 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1205 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1206 content_info->media_meta.rating = convert_value;
1208 content_info->media_meta.rating = 0;
1211 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1213 /*Do not extract artwork for the USB Storage content*/
1214 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1215 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1216 if (mmf_error != FILEINFO_ERROR_NONE)
1217 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1219 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1220 if (mmf_error != FILEINFO_ERROR_NONE)
1221 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1223 if (image != NULL && size > 0) {
1224 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1225 int artwork_mime_size = -1;
1227 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1228 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1229 ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1230 if (ret != MS_MEDIA_ERR_NONE) {
1231 media_svc_error("Fail to Get Thumbnail Path");
1233 ret = __media_svc_save_image(image, size, thumb_path, uid);
1234 if (ret != MS_MEDIA_ERR_NONE) {
1235 media_svc_error("Fail to Save Image");
1237 content_info->thumbnail_path = g_strdup(thumb_path);
1238 /* NOTICE : Prevent resize for performance (2020.02.07)
1239 * In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1240 * So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1243 /* albumart resizing */
1244 ret = __media_svc_resize_artwork(thumb_path, p);
1245 if (ret != MS_MEDIA_ERR_NONE) {
1246 media_svc_error("Fail to Make Thumbnail Image");
1247 _media_svc_remove_file(thumb_path);
1250 content_info->thumbnail_path = g_strdup(thumb_path);
1259 /*Initialize album_id to 0. below code will set the album_id*/
1260 content_info->album_id = album_id;
1261 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1262 if (ret != MS_MEDIA_ERR_NONE) {
1263 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1264 media_svc_debug("album does not exist. So start to make album art");
1265 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1266 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1267 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);
1269 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1271 content_info->album_id = album_id;
1274 content_info->album_id = album_id;
1277 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1278 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1279 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1281 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1282 double longitude = 0.0;
1283 double latitude = 0.0;
1284 double altitude = 0.0;
1286 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1287 MM_FILE_TAG_LATIDUE, &latitude,
1288 MM_FILE_TAG_ALTIDUE, &altitude,
1291 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1292 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1293 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1295 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1296 if (mmf_error != FILEINFO_ERROR_NONE)
1299 media_svc_debug("CDIS : %d", cdis_value);
1301 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1302 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1303 content_info->media_meta.orientation = atoi(p);
1305 content_info->media_meta.orientation = 0;
1306 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1310 mmf_error = mm_file_destroy_tag_attrs(tag);
1311 if (mmf_error != FILEINFO_ERROR_NONE)
1312 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1314 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1315 content_info->album_id = album_id;
1318 /*Get Content attribute ===========*/
1319 if (cdis_value == 1)
1320 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1322 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1324 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1326 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1327 int audio_bitrate = 0;
1328 int video_bitrate = 0;
1330 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1331 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1332 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1333 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1334 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1337 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1339 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1340 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1341 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1342 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1343 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1347 mm_file_destroy_content_attrs(content);
1349 return MS_MEDIA_ERR_NONE;
1352 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1354 media_svc_retm_if(content_info == NULL, "content info is NULL");
1356 /* Delete media_svc_content_info_s */
1357 SAFE_FREE(content_info->media_uuid);
1358 SAFE_FREE(content_info->path);
1359 SAFE_FREE(content_info->file_name);
1360 SAFE_FREE(content_info->mime_type);
1361 SAFE_FREE(content_info->folder_uuid);
1362 SAFE_FREE(content_info->thumbnail_path);
1363 SAFE_FREE(content_info->storage_uuid);
1365 /* Delete media_svc_content_meta_s */
1366 SAFE_FREE(content_info->media_meta.title);
1367 SAFE_FREE(content_info->media_meta.album);
1368 SAFE_FREE(content_info->media_meta.artist);
1369 SAFE_FREE(content_info->media_meta.album_artist);
1370 SAFE_FREE(content_info->media_meta.genre);
1371 SAFE_FREE(content_info->media_meta.composer);
1372 SAFE_FREE(content_info->media_meta.year);
1373 SAFE_FREE(content_info->media_meta.recorded_date);
1374 SAFE_FREE(content_info->media_meta.copyright);
1375 SAFE_FREE(content_info->media_meta.track_num);
1376 SAFE_FREE(content_info->media_meta.description);
1377 SAFE_FREE(content_info->media_meta.datetaken);
1378 SAFE_FREE(content_info->media_meta.exposure_time);
1379 SAFE_FREE(content_info->media_meta.model);
1381 SAFE_FREE(content_info->file_name_pinyin);
1382 SAFE_FREE(content_info->media_meta.title_pinyin);
1383 SAFE_FREE(content_info->media_meta.album_pinyin);
1384 SAFE_FREE(content_info->media_meta.artist_pinyin);
1385 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1386 SAFE_FREE(content_info->media_meta.genre_pinyin);
1387 SAFE_FREE(content_info->media_meta.composer_pinyin);
1388 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1389 SAFE_FREE(content_info->media_meta.description_pinyin);
1392 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1394 bool portrait = false;
1397 media_svc_retm_if(!orig_w, "Invalid orig_w");
1398 media_svc_retm_if(!orig_h, "Invalid orig_h");
1399 media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1400 media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1402 if (orig_w < orig_h)
1405 /* Set smaller length to default size */
1407 if (orig_w < *thumb_w)
1409 ratio = (double)orig_h / (double)orig_w;
1410 *thumb_h = *thumb_w * ratio;
1412 if (orig_h < *thumb_h)
1414 ratio = (double)orig_w / (double)orig_h;
1415 *thumb_w = *thumb_h * ratio;
1418 media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1423 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1425 int err = MS_MEDIA_ERR_NONE;
1426 MMHandleType tag = (MMHandleType) NULL;
1429 int _cdis_value = 0;
1430 mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1432 /* Get Content Tag attribute for orientation */
1433 err = mm_file_create_tag_attrs(&tag, origin_path);
1434 if (err != FILEINFO_ERROR_NONE) {
1435 *rot_type = MM_UTIL_ROTATE_0;
1440 err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1441 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1443 _rot_type = MM_UTIL_ROTATE_0;
1445 if (strncmp(p, "90", size) == 0)
1446 _rot_type = MM_UTIL_ROTATE_90;
1447 else if (strncmp(p, "180", size) == 0)
1448 _rot_type = MM_UTIL_ROTATE_180;
1449 else if (strncmp(p, "270", size) == 0)
1450 _rot_type = MM_UTIL_ROTATE_270;
1452 _rot_type = MM_UTIL_ROTATE_0;
1454 media_svc_debug("There is tag rotate : %d", _rot_type);
1456 media_svc_debug("There is NOT tag rotate");
1457 _rot_type = MM_UTIL_ROTATE_0;
1460 err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1461 if (err != FILEINFO_ERROR_NONE)
1464 *rot_type = _rot_type;
1465 *cdis_value = _cdis_value;
1467 err = mm_file_destroy_tag_attrs(tag);
1468 if (err != FILEINFO_ERROR_NONE) {
1469 media_svc_error("fail to free tag attr - err(%x)", err);
1475 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)
1477 int err = MS_MEDIA_ERR_NONE;
1478 MMHandleType content = (MMHandleType) NULL;
1479 int _video_track_num = 0;
1483 void *_frame = NULL;
1485 if (cdis_value == 1) {
1486 media_svc_debug("This is CDIS vlaue 1");
1487 err = mm_file_create_content_attrs_safe(&content, origin_path);
1489 err = mm_file_create_content_attrs(&content, origin_path);
1492 if (err != FILEINFO_ERROR_NONE) {
1493 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1494 return MS_MEDIA_ERR_INTERNAL;
1497 err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1498 if (err != FILEINFO_ERROR_NONE) {
1499 media_svc_error("mm_file_get_attrs fails : %d", err);
1500 mm_file_destroy_content_attrs(content);
1501 return MS_MEDIA_ERR_INTERNAL;
1504 *video_track_num = _video_track_num;
1506 if (_video_track_num == 0) {
1507 mm_file_destroy_content_attrs(content);
1508 return MS_MEDIA_ERR_NONE;
1511 err = mm_file_get_attrs(content,
1512 MM_FILE_CONTENT_VIDEO_WIDTH,
1514 MM_FILE_CONTENT_VIDEO_HEIGHT,
1516 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1519 if (err != FILEINFO_ERROR_NONE) {
1520 media_svc_error("mm_file_get_attrs fails : %d", err);
1521 mm_file_destroy_content_attrs(content);
1522 return MS_MEDIA_ERR_INTERNAL;
1525 media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1526 if (!_frame || !_width || !_height) {
1527 mm_file_destroy_content_attrs(content);
1528 return MS_MEDIA_ERR_INTERNAL;
1535 *frame = calloc(1, _size);
1536 memcpy(*frame, _frame, _size);
1538 mm_file_destroy_content_attrs(content);
1540 return MS_MEDIA_ERR_NONE;
1543 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)
1545 int err = MS_MEDIA_ERR_NONE;
1546 mm_util_image_h img = NULL;
1547 mm_util_image_h resize_img = NULL;
1549 media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1550 __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1551 if (thumb_width <= 0 || thumb_height <= 0) {
1552 media_svc_error("Failed to get thumb size");
1553 return MS_MEDIA_ERR_INTERNAL;
1556 media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1558 err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1559 media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1561 if (width > thumb_width || height > thumb_height) {
1562 if (rot_type != MM_UTIL_ROTATE_0) {
1563 err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1564 if (err != MM_UTIL_ERROR_NONE)
1567 err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1569 err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1572 if (rot_type != MM_UTIL_ROTATE_0)
1573 err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1575 err = mm_util_resize_B_P(img, width, height, thumb_path);
1579 mm_image_destroy_image(img);
1580 mm_image_destroy_image(resize_img);
1581 if (err != MS_MEDIA_ERR_NONE)
1582 return MS_MEDIA_ERR_INTERNAL;
1584 return MS_MEDIA_ERR_NONE;
1587 static int __create_video_thumbnail(const char *path, char *thumb_path)
1589 int ret = MS_MEDIA_ERR_NONE;
1591 unsigned int width = 0;
1592 unsigned int height = 0;
1594 int video_track_num = 0;
1596 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1598 __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1599 ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1600 media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1601 media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1603 ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1610 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1612 int ret = MS_MEDIA_ERR_NONE;
1613 unsigned int origin_w = 0;
1614 unsigned int origin_h = 0;
1615 unsigned int thumb_w = THUMB_WIDTH;
1616 unsigned int thumb_h = THUMB_HEIGHT;
1617 mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1619 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1620 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1621 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1623 ms_user_storage_type_e store_type = -1;
1624 ret = ms_user_get_storage_type(uid, path, &store_type);
1626 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1627 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1628 return MS_MEDIA_ERR_INVALID_PARAMETER;
1631 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1634 ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1635 if (ret != MS_MEDIA_ERR_NONE) {
1636 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1637 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1642 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1643 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1644 if (ret != MM_UTIL_ERROR_NONE) {
1645 media_svc_error("Getting image info is failed err: %d", ret);
1646 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1647 return MS_MEDIA_ERR_INTERNAL;
1650 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1651 media_svc_error("Unsupported image codec");
1652 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1655 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1656 media_svc_error("This original image is too big");
1657 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1658 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1661 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1662 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1663 if (ret != MM_UTIL_ERROR_NONE) {
1664 media_svc_error("mm_util_resize_P_P err: %d", ret);
1665 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1666 return MS_MEDIA_ERR_INTERNAL;
1669 return __create_video_thumbnail(path, thumb_path);
1675 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1677 int ret = MS_MEDIA_ERR_NONE;
1679 pinyin_name_s *pinyinname = NULL;
1681 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1682 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1686 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1687 if (ret == MS_MEDIA_ERR_NONE) {
1688 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1689 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1691 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1694 _media_svc_pinyin_free(pinyinname, size);
1699 bool _media_svc_check_pinyin_support(void)
1701 int ret = SYSTEM_INFO_ERROR_NONE;
1702 bool is_supported = false;
1703 static int media_svc_pinyin_support = -1;
1705 if (media_svc_pinyin_support == -1) {
1706 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1707 if (ret != SYSTEM_INFO_ERROR_NONE) {
1708 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1712 media_svc_pinyin_support = is_supported;
1715 return media_svc_pinyin_support;
1718 int _media_svc_get_media_type(const char *path, int *mediatype)
1720 int ret = MS_MEDIA_ERR_NONE;
1721 char mime_type[256] = {0};
1722 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1724 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1726 ret = __media_svc_get_mime_type(path, mime_type);
1727 if (ret == MS_MEDIA_ERR_NONE)
1728 __media_svc_get_media_type(path, mime_type, &media_type);
1730 media_svc_error("__media_svc_get_mime_type failed");
1732 *mediatype = media_type;
1737 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1739 switch (storage_type) {
1740 case MS_USER_STORAGE_INTERNAL:
1741 case MS_USER_STORAGE_EXTERNAL:
1742 case MS_USER_STORAGE_EXTERNAL_USB:
1745 media_svc_error("storage type is incorrect[%d]", storage_type);