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)
720 memset(&statbuf, 0, sizeof(struct stat));
721 fd = stat(full_path, &statbuf);
723 media_svc_sec_error("stat(%s) fails.", full_path);
724 return MS_MEDIA_ERR_INTERNAL;
727 return statbuf.st_mtime;
730 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)
732 int ret = MS_MEDIA_ERR_NONE;
733 char * media_uuid = NULL;
734 bool drm_type = false;
735 char mime_type[256] = {0, };
736 media_svc_media_type_e media_type;
738 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
739 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
741 content_info->path = g_strdup(path);
742 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
745 memset(&st, 0, sizeof(struct stat));
746 if (stat(path, &st) == 0) {
747 content_info->modified_time = st.st_mtime;
748 content_info->timeline = content_info->modified_time;
749 content_info->size = st.st_size;
751 media_svc_stderror("stat failed");
754 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
756 media_svc_debug("refresh");
757 return MS_MEDIA_ERR_NONE;
760 content_info->storage_uuid = g_strdup(storage_id);
761 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
763 content_info->storage_type = storage_type;
764 time(&content_info->added_time);
766 media_uuid = _media_info_generate_uuid();
767 if (media_uuid == NULL) {
768 _media_svc_destroy_content_info(content_info);
769 return MS_MEDIA_ERR_INTERNAL;
772 content_info->media_uuid = g_strdup(media_uuid);
773 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
775 content_info->file_name = g_path_get_basename(path);
776 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
778 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
779 if drm_contentinfo is not NULL, the file is OMA DRM.*/
780 ret = __media_svc_get_mime_type(path, mime_type);
781 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
783 media_svc_debug("mime [%s]", mime_type);
784 content_info->is_drm = drm_type;
786 ret = __media_svc_get_media_type(path, mime_type, &media_type);
787 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
789 content_info->mime_type = g_strdup(mime_type);
790 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
792 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
794 content_info->media_type = media_type;
796 return MS_MEDIA_ERR_NONE;
799 int image_360_check(char *path)
804 unsigned char exif_header[4] = {0, };
805 unsigned char exif_app1[2] = {0, };
806 unsigned char exif_app1_xmp[2] = {0, };
807 long exif_app1_xmp_size = 0;
808 unsigned char exif_app1_xmp_t[2] = {0, };
809 char *xmp_data = NULL;
815 memset(exif_header, 0x00, sizeof(exif_header));
816 memset(exif_app1, 0x00, sizeof(exif_app1));
817 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
818 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
820 fp = fopen(path, "rb");
824 size = fread(exif_header, 1, sizeof(exif_header), fp);
828 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
829 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
833 size1 = exif_app1[0];
834 size2 = exif_app1[1];
836 app1_size = size1 * 256 + size2 - 2;
838 if (fseek(fp, app1_size, SEEK_CUR) != 0)
841 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
845 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
848 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
852 size1 = exif_app1_xmp_t[0];
853 size2 = exif_app1_xmp_t[1];
855 exif_app1_xmp_size = size1 * 256 + size2 - 2;
857 if (exif_app1_xmp_size > 0) {
858 xmp_data = (char *)malloc(exif_app1_xmp_size);
859 memset(xmp_data, 0x0, exif_app1_xmp_size);
863 while (exif_app1_xmp_size >= 0) {
864 exif_app1_xmp_size--;
876 if (strstr(ptr, "UsePanoramaViewer")
877 && strstr(ptr, "True")
878 && strstr(ptr, "ProjectionType")
879 && strstr(ptr, "equirectangular"))
884 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
906 static char * __media_svc_get_title(MMHandleType tag, const char *path)
908 int ret = FILEINFO_ERROR_NONE;
914 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
915 if (ret == FILEINFO_ERROR_NONE && size > 0) {
916 while(p && isspace(*p))
923 title = __media_svc_get_title_from_filepath(path);
927 media_svc_error("Can't extract title");
929 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
932 char * _media_svc_get_title_by_path(const char *path)
934 /* No MMHandleType in media-svc.c */
935 return __media_svc_get_title(NULL, path);
938 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
941 int orient_value = 0;
945 bool has_datetaken = false;
946 double fnumber = 0.0;
950 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
952 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
953 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);
954 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
956 path = content_info->path;
957 content_info->media_meta.title = __media_svc_get_title(NULL, path);
959 /* Load an ExifData object from an EXIF file */
960 ed = exif_data_new_from_file(path);
963 media_svc_sec_debug("There is no exif data in [ %s ]", path);
964 goto GET_WIDTH_HEIGHT;
967 content_info->media_meta.is_360 = image_360_check(path);
969 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
970 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
971 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
973 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
974 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
975 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
976 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
977 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
978 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
979 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
980 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
981 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
983 memset(buf, 0x00, sizeof(buf));
984 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
985 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
986 if (!g_strcmp0(buf, "S"))
988 content_info->media_meta.latitude = value;
992 memset(buf, 0x00, sizeof(buf));
993 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
994 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
995 if (!g_strcmp0(buf, "W"))
998 content_info->media_meta.longitude = value;
1002 memset(buf, 0x00, sizeof(buf));
1003 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1004 if (strlen(buf) > 0)
1005 content_info->media_meta.description = g_strdup(buf);
1007 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1011 memset(buf, 0x00, sizeof(buf));
1012 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1013 if (strlen(buf) > 0) {
1014 has_datetaken = true;
1015 content_info->media_meta.datetaken = g_strdup(buf);
1017 /* This is same as recorded_date */
1018 content_info->media_meta.recorded_date = g_strdup(buf);
1022 memset(buf, 0x00, sizeof(buf));
1023 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1024 if (strlen(buf) > 0) {
1025 has_datetaken = true;
1026 content_info->media_meta.datetaken = g_strdup(buf);
1028 /* This is same as recorded_date */
1029 content_info->media_meta.recorded_date = g_strdup(buf);
1033 if (has_datetaken) {
1034 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1035 if (content_info->timeline == 0)
1036 content_info->timeline = content_info->modified_time;
1038 media_svc_debug("Timeline : %ld", content_info->timeline);
1041 memset(buf, 0x00, sizeof(buf));
1042 /* Get exposure_time value from exif. */
1043 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1044 if (strlen(buf) > 0)
1045 content_info->media_meta.exposure_time = g_strdup(buf);
1048 /* Get fnumber value from exif. */
1049 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1050 content_info->media_meta.fnumber = fnumber;
1052 /* Get iso value from exif. */
1053 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1054 content_info->media_meta.iso = iso;
1056 memset(buf, 0x00, sizeof(buf));
1057 /* Get model value from exif. */
1058 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1059 if (strlen(buf) > 0)
1060 content_info->media_meta.model = g_strdup(buf);
1063 /* Get orientation value from exif. */
1064 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1065 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1066 content_info->media_meta.orientation = orient_value;
1069 /* Get width value from exif. */
1070 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1072 content_info->media_meta.width = exif_width;
1075 /* Get height value from exif. */
1076 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1077 if (exif_height > 0)
1078 content_info->media_meta.height = exif_height;
1082 exif_data_unref(ed);
1086 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1087 /*Get image width, height*/
1088 unsigned int img_width = 0;
1089 unsigned int img_height = 0;
1090 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1092 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1093 if (content_info->media_meta.width == 0)
1094 content_info->media_meta.width = img_width;
1096 if (content_info->media_meta.height == 0)
1097 content_info->media_meta.height = img_height;
1100 return MS_MEDIA_ERR_NONE;
1103 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1105 int ret = FILEINFO_ERROR_NONE;
1109 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1110 if (ret == FILEINFO_ERROR_NONE && size > 0)
1113 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1116 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1118 MMHandleType tag = 0;
1119 int mmf_error = FILEINFO_ERROR_NONE;
1121 content_info->path = g_strdup(path);
1123 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1124 if (mmf_error == FILEINFO_ERROR_NONE) {
1125 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1126 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1127 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1128 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1129 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1130 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1131 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1132 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1134 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1137 mm_file_destroy_tag_attrs(tag);
1139 return MS_MEDIA_ERR_NONE;
1142 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1144 MMHandleType content = 0;
1145 MMHandleType tag = 0;
1147 unsigned char *image = NULL;
1148 unsigned int size = 0;
1149 int mmf_error = FILEINFO_ERROR_NONE;
1151 int ret = MS_MEDIA_ERR_NONE;
1152 int convert_value = 0;
1155 /*Get Content Tag attribute ===========*/
1156 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1158 if (mmf_error == FILEINFO_ERROR_NONE) {
1159 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1160 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1161 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1162 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1163 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1164 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1165 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1166 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1167 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1169 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1170 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1171 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1172 /*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*/
1173 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1174 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1177 content_info->media_meta.recorded_date = g_strdup(p);
1180 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1181 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1182 if (content_info->timeline == 0)
1183 content_info->timeline = content_info->modified_time;
1185 /* This is same as datetaken */
1186 /* Remove compensation string */
1187 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1188 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1189 g_free(content_info->media_meta.recorded_date);
1190 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1192 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1197 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1198 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1199 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1200 content_info->media_meta.year = g_strdup(p);
1203 if (!content_info->media_meta.year)
1204 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1206 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1207 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1208 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1209 content_info->media_meta.rating = convert_value;
1211 content_info->media_meta.rating = 0;
1214 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1216 /*Do not extract artwork for the USB Storage content*/
1217 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1218 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1219 if (mmf_error != FILEINFO_ERROR_NONE)
1220 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1222 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1223 if (mmf_error != FILEINFO_ERROR_NONE)
1224 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1226 if (image != NULL && size > 0) {
1227 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1228 int artwork_mime_size = -1;
1230 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1231 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1232 ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1233 if (ret != MS_MEDIA_ERR_NONE) {
1234 media_svc_error("Fail to Get Thumbnail Path");
1236 ret = __media_svc_save_image(image, size, thumb_path, uid);
1237 if (ret != MS_MEDIA_ERR_NONE) {
1238 media_svc_error("Fail to Save Image");
1240 content_info->thumbnail_path = g_strdup(thumb_path);
1241 /* NOTICE : Prevent resize for performance (2020.02.07)
1242 * In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1243 * So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1246 /* albumart resizing */
1247 ret = __media_svc_resize_artwork(thumb_path, p);
1248 if (ret != MS_MEDIA_ERR_NONE) {
1249 media_svc_error("Fail to Make Thumbnail Image");
1250 _media_svc_remove_file(thumb_path);
1253 content_info->thumbnail_path = g_strdup(thumb_path);
1262 /*Initialize album_id to 0. below code will set the album_id*/
1263 content_info->album_id = album_id;
1264 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1265 if (ret != MS_MEDIA_ERR_NONE) {
1266 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1267 media_svc_debug("album does not exist. So start to make album art");
1268 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1269 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1270 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);
1272 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1274 content_info->album_id = album_id;
1277 content_info->album_id = album_id;
1280 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1281 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1282 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1284 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1285 double longitude = 0.0;
1286 double latitude = 0.0;
1287 double altitude = 0.0;
1289 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1290 MM_FILE_TAG_LATIDUE, &latitude,
1291 MM_FILE_TAG_ALTIDUE, &altitude,
1294 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1295 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1296 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1298 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1299 if (mmf_error != FILEINFO_ERROR_NONE)
1302 media_svc_debug("CDIS : %d", cdis_value);
1304 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1305 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1306 content_info->media_meta.orientation = atoi(p);
1308 content_info->media_meta.orientation = 0;
1309 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1313 mmf_error = mm_file_destroy_tag_attrs(tag);
1314 if (mmf_error != FILEINFO_ERROR_NONE)
1315 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1317 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1318 content_info->album_id = album_id;
1321 /*Get Content attribute ===========*/
1322 if (cdis_value == 1)
1323 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1325 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1327 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1329 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1330 int audio_bitrate = 0;
1331 int video_bitrate = 0;
1333 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1334 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1335 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1336 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1337 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1340 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1342 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1343 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1344 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1345 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1346 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1350 mm_file_destroy_content_attrs(content);
1352 return MS_MEDIA_ERR_NONE;
1355 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1357 media_svc_retm_if(content_info == NULL, "content info is NULL");
1359 /* Delete media_svc_content_info_s */
1360 SAFE_FREE(content_info->media_uuid);
1361 SAFE_FREE(content_info->path);
1362 SAFE_FREE(content_info->file_name);
1363 SAFE_FREE(content_info->mime_type);
1364 SAFE_FREE(content_info->folder_uuid);
1365 SAFE_FREE(content_info->thumbnail_path);
1366 SAFE_FREE(content_info->storage_uuid);
1368 /* Delete media_svc_content_meta_s */
1369 SAFE_FREE(content_info->media_meta.title);
1370 SAFE_FREE(content_info->media_meta.album);
1371 SAFE_FREE(content_info->media_meta.artist);
1372 SAFE_FREE(content_info->media_meta.album_artist);
1373 SAFE_FREE(content_info->media_meta.genre);
1374 SAFE_FREE(content_info->media_meta.composer);
1375 SAFE_FREE(content_info->media_meta.year);
1376 SAFE_FREE(content_info->media_meta.recorded_date);
1377 SAFE_FREE(content_info->media_meta.copyright);
1378 SAFE_FREE(content_info->media_meta.track_num);
1379 SAFE_FREE(content_info->media_meta.description);
1380 SAFE_FREE(content_info->media_meta.datetaken);
1381 SAFE_FREE(content_info->media_meta.exposure_time);
1382 SAFE_FREE(content_info->media_meta.model);
1384 SAFE_FREE(content_info->file_name_pinyin);
1385 SAFE_FREE(content_info->media_meta.title_pinyin);
1386 SAFE_FREE(content_info->media_meta.album_pinyin);
1387 SAFE_FREE(content_info->media_meta.artist_pinyin);
1388 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1389 SAFE_FREE(content_info->media_meta.genre_pinyin);
1390 SAFE_FREE(content_info->media_meta.composer_pinyin);
1391 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1392 SAFE_FREE(content_info->media_meta.description_pinyin);
1395 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1397 bool portrait = false;
1400 media_svc_retm_if(!orig_w, "Invalid orig_w");
1401 media_svc_retm_if(!orig_h, "Invalid orig_h");
1402 media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1403 media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1405 if (orig_w < orig_h)
1408 /* Set smaller length to default size */
1410 if (orig_w < *thumb_w)
1412 ratio = (double)orig_h / (double)orig_w;
1413 *thumb_h = *thumb_w * ratio;
1415 if (orig_h < *thumb_h)
1417 ratio = (double)orig_w / (double)orig_h;
1418 *thumb_w = *thumb_h * ratio;
1421 media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1426 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1428 int err = MS_MEDIA_ERR_NONE;
1429 MMHandleType tag = (MMHandleType) NULL;
1432 int _cdis_value = 0;
1433 mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1435 /* Get Content Tag attribute for orientation */
1436 err = mm_file_create_tag_attrs(&tag, origin_path);
1437 if (err != FILEINFO_ERROR_NONE) {
1438 *rot_type = MM_UTIL_ROTATE_0;
1443 err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1444 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1446 _rot_type = MM_UTIL_ROTATE_0;
1448 if (strncmp(p, "90", size) == 0)
1449 _rot_type = MM_UTIL_ROTATE_90;
1450 else if (strncmp(p, "180", size) == 0)
1451 _rot_type = MM_UTIL_ROTATE_180;
1452 else if (strncmp(p, "270", size) == 0)
1453 _rot_type = MM_UTIL_ROTATE_270;
1455 _rot_type = MM_UTIL_ROTATE_0;
1457 media_svc_debug("There is tag rotate : %d", _rot_type);
1459 media_svc_debug("There is NOT tag rotate");
1460 _rot_type = MM_UTIL_ROTATE_0;
1463 err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1464 if (err != FILEINFO_ERROR_NONE)
1467 *rot_type = _rot_type;
1468 *cdis_value = _cdis_value;
1470 err = mm_file_destroy_tag_attrs(tag);
1471 if (err != FILEINFO_ERROR_NONE) {
1472 media_svc_error("fail to free tag attr - err(%x)", err);
1478 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)
1480 int err = MS_MEDIA_ERR_NONE;
1481 MMHandleType content = (MMHandleType) NULL;
1482 int _video_track_num = 0;
1486 void *_frame = NULL;
1488 if (cdis_value == 1) {
1489 media_svc_debug("This is CDIS vlaue 1");
1490 err = mm_file_create_content_attrs_safe(&content, origin_path);
1492 err = mm_file_create_content_attrs(&content, origin_path);
1495 if (err != FILEINFO_ERROR_NONE) {
1496 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1497 return MS_MEDIA_ERR_INTERNAL;
1500 err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1501 if (err != FILEINFO_ERROR_NONE) {
1502 media_svc_error("mm_file_get_attrs fails : %d", err);
1503 mm_file_destroy_content_attrs(content);
1504 return MS_MEDIA_ERR_INTERNAL;
1507 *video_track_num = _video_track_num;
1509 if (_video_track_num == 0) {
1510 mm_file_destroy_content_attrs(content);
1511 return MS_MEDIA_ERR_NONE;
1514 err = mm_file_get_attrs(content,
1515 MM_FILE_CONTENT_VIDEO_WIDTH,
1517 MM_FILE_CONTENT_VIDEO_HEIGHT,
1519 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1522 if (err != FILEINFO_ERROR_NONE) {
1523 media_svc_error("mm_file_get_attrs fails : %d", err);
1524 mm_file_destroy_content_attrs(content);
1525 return MS_MEDIA_ERR_INTERNAL;
1528 media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1529 if (!_frame || !_width || !_height) {
1530 mm_file_destroy_content_attrs(content);
1531 return MS_MEDIA_ERR_INTERNAL;
1538 *frame = calloc(1, _size);
1539 memcpy(*frame, _frame, _size);
1541 mm_file_destroy_content_attrs(content);
1543 return MS_MEDIA_ERR_NONE;
1546 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)
1548 int err = MS_MEDIA_ERR_NONE;
1549 mm_util_image_h img = NULL;
1550 mm_util_image_h resize_img = NULL;
1552 media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1553 __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1554 if (thumb_width <= 0 || thumb_height <= 0) {
1555 media_svc_error("Failed to get thumb size");
1556 return MS_MEDIA_ERR_INTERNAL;
1559 media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1561 err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1562 media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1564 if (width > thumb_width || height > thumb_height) {
1565 if (rot_type != MM_UTIL_ROTATE_0) {
1566 err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1567 if (err != MM_UTIL_ERROR_NONE)
1570 err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1572 err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1575 if (rot_type != MM_UTIL_ROTATE_0)
1576 err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1578 err = mm_util_resize_B_P(img, width, height, thumb_path);
1582 mm_image_destroy_image(img);
1583 mm_image_destroy_image(resize_img);
1584 if (err != MS_MEDIA_ERR_NONE)
1585 return MS_MEDIA_ERR_INTERNAL;
1587 return MS_MEDIA_ERR_NONE;
1590 static int __create_video_thumbnail(const char *path, char *thumb_path)
1592 int ret = MS_MEDIA_ERR_NONE;
1594 unsigned int width = 0;
1595 unsigned int height = 0;
1597 int video_track_num = 0;
1599 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1601 __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1602 ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1603 media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1604 media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1606 ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1613 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1615 int ret = MS_MEDIA_ERR_NONE;
1616 unsigned int origin_w = 0;
1617 unsigned int origin_h = 0;
1618 unsigned int thumb_w = THUMB_WIDTH;
1619 unsigned int thumb_h = THUMB_HEIGHT;
1620 mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1622 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1623 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1624 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1626 ms_user_storage_type_e store_type = -1;
1627 ret = ms_user_get_storage_type(uid, path, &store_type);
1629 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1630 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1631 return MS_MEDIA_ERR_INVALID_PARAMETER;
1634 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1637 ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1638 if (ret != MS_MEDIA_ERR_NONE) {
1639 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1640 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1645 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1646 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1647 if (ret != MM_UTIL_ERROR_NONE) {
1648 media_svc_error("Getting image info is failed err: %d", ret);
1649 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1650 return MS_MEDIA_ERR_INTERNAL;
1653 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1654 media_svc_error("Unsupported image codec");
1655 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1658 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1659 media_svc_error("This original image is too big");
1660 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1661 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1664 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1665 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1666 if (ret != MM_UTIL_ERROR_NONE) {
1667 media_svc_error("mm_util_resize_P_P err: %d", ret);
1668 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1669 return MS_MEDIA_ERR_INTERNAL;
1672 return __create_video_thumbnail(path, thumb_path);
1678 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1680 int ret = MS_MEDIA_ERR_NONE;
1682 pinyin_name_s *pinyinname = NULL;
1684 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1685 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1689 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1690 if (ret == MS_MEDIA_ERR_NONE) {
1691 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1692 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1694 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1697 _media_svc_pinyin_free(pinyinname, size);
1702 bool _media_svc_check_pinyin_support(void)
1704 int ret = SYSTEM_INFO_ERROR_NONE;
1705 bool is_supported = false;
1706 static int media_svc_pinyin_support = -1;
1708 if (media_svc_pinyin_support == -1) {
1709 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1710 if (ret != SYSTEM_INFO_ERROR_NONE) {
1711 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1715 media_svc_pinyin_support = is_supported;
1718 return media_svc_pinyin_support;
1721 int _media_svc_get_media_type(const char *path, int *mediatype)
1723 int ret = MS_MEDIA_ERR_NONE;
1724 char mime_type[256] = {0};
1725 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1727 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1729 ret = __media_svc_get_mime_type(path, mime_type);
1730 if (ret == MS_MEDIA_ERR_NONE)
1731 __media_svc_get_media_type(path, mime_type, &media_type);
1733 media_svc_error("__media_svc_get_mime_type failed");
1735 *mediatype = media_type;
1740 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1742 switch (storage_type) {
1743 case MS_USER_STORAGE_INTERNAL:
1744 case MS_USER_STORAGE_EXTERNAL:
1745 case MS_USER_STORAGE_EXTERNAL_USB:
1748 media_svc_error("storage type is incorrect[%d]", storage_type);