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 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 g_strdup(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 bool drm_type = false;
731 char mime_type[256] = {0, };
732 media_svc_media_type_e media_type;
734 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
735 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
737 content_info->path = g_strdup(path);
738 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
741 memset(&st, 0, sizeof(struct stat));
742 if (stat(path, &st) == 0) {
743 content_info->modified_time = st.st_mtime;
744 content_info->timeline = content_info->modified_time;
745 content_info->size = st.st_size;
747 media_svc_stderror("stat failed");
750 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
752 media_svc_debug("refresh");
753 return MS_MEDIA_ERR_NONE;
756 content_info->storage_uuid = g_strdup(storage_id);
757 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
759 content_info->storage_type = storage_type;
760 time(&content_info->added_time);
762 content_info->media_uuid = _media_info_generate_uuid();
763 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
765 content_info->file_name = g_path_get_basename(path);
766 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
768 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
769 if drm_contentinfo is not NULL, the file is OMA DRM.*/
770 ret = __media_svc_get_mime_type(path, mime_type);
771 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
773 media_svc_debug("mime [%s]", mime_type);
774 content_info->is_drm = drm_type;
776 ret = __media_svc_get_media_type(path, mime_type, &media_type);
777 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
779 content_info->mime_type = g_strdup(mime_type);
780 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
782 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
784 content_info->media_type = media_type;
786 return MS_MEDIA_ERR_NONE;
789 int image_360_check(char *path)
794 unsigned char exif_header[4] = {0, };
795 unsigned char exif_app1[2] = {0, };
796 unsigned char exif_app1_xmp[2] = {0, };
797 long exif_app1_xmp_size = 0;
798 unsigned char exif_app1_xmp_t[2] = {0, };
799 char *xmp_data = NULL;
805 memset(exif_header, 0x00, sizeof(exif_header));
806 memset(exif_app1, 0x00, sizeof(exif_app1));
807 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
808 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
810 fp = fopen(path, "rb");
814 size = fread(exif_header, 1, sizeof(exif_header), fp);
818 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
819 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
823 size1 = exif_app1[0];
824 size2 = exif_app1[1];
826 app1_size = size1 * 256 + size2 - 2;
828 if (fseek(fp, app1_size, SEEK_CUR) != 0)
831 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
835 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
838 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
842 size1 = exif_app1_xmp_t[0];
843 size2 = exif_app1_xmp_t[1];
845 exif_app1_xmp_size = size1 * 256 + size2 - 2;
847 if (exif_app1_xmp_size > 0) {
848 xmp_data = (char *)malloc(exif_app1_xmp_size);
849 memset(xmp_data, 0x0, exif_app1_xmp_size);
853 while (exif_app1_xmp_size >= 0) {
854 exif_app1_xmp_size--;
866 if (strstr(ptr, "UsePanoramaViewer")
867 && strstr(ptr, "True")
868 && strstr(ptr, "ProjectionType")
869 && strstr(ptr, "equirectangular"))
874 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
896 static char * __media_svc_get_title(MMHandleType tag, const char *path)
898 int ret = FILEINFO_ERROR_NONE;
904 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
905 if (ret == FILEINFO_ERROR_NONE && size > 0) {
906 while(p && isspace(*p))
913 title = __media_svc_get_title_from_filepath(path);
917 media_svc_error("Can't extract title");
919 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
922 char * _media_svc_get_title_by_path(const char *path)
924 /* No MMHandleType in media-svc.c */
925 return __media_svc_get_title(NULL, path);
928 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
931 int orient_value = 0;
935 bool has_datetaken = false;
936 double fnumber = 0.0;
940 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
942 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
943 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);
944 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
946 path = content_info->path;
947 content_info->media_meta.title = __media_svc_get_title(NULL, path);
949 /* Load an ExifData object from an EXIF file */
950 ed = exif_data_new_from_file(path);
953 media_svc_sec_debug("There is no exif data in [ %s ]", path);
954 goto GET_WIDTH_HEIGHT;
957 content_info->media_meta.is_360 = image_360_check(path);
959 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
960 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
961 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
963 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
964 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
965 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
966 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
967 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
968 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
969 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
970 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
971 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
973 memset(buf, 0x00, sizeof(buf));
974 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
975 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
976 if (!g_strcmp0(buf, "S"))
978 content_info->media_meta.latitude = value;
982 memset(buf, 0x00, sizeof(buf));
983 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
984 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
985 if (!g_strcmp0(buf, "W"))
988 content_info->media_meta.longitude = value;
992 memset(buf, 0x00, sizeof(buf));
993 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
995 content_info->media_meta.description = g_strdup(buf);
997 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1001 memset(buf, 0x00, sizeof(buf));
1002 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1003 if (strlen(buf) > 0) {
1004 has_datetaken = true;
1005 content_info->media_meta.datetaken = g_strdup(buf);
1007 /* This is same as recorded_date */
1008 content_info->media_meta.recorded_date = g_strdup(buf);
1012 memset(buf, 0x00, sizeof(buf));
1013 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1014 if (strlen(buf) > 0) {
1015 has_datetaken = true;
1016 content_info->media_meta.datetaken = g_strdup(buf);
1018 /* This is same as recorded_date */
1019 content_info->media_meta.recorded_date = g_strdup(buf);
1023 if (has_datetaken) {
1024 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1025 if (content_info->timeline == 0)
1026 content_info->timeline = content_info->modified_time;
1028 media_svc_debug("Timeline : %ld", content_info->timeline);
1031 memset(buf, 0x00, sizeof(buf));
1032 /* Get exposure_time value from exif. */
1033 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1034 if (strlen(buf) > 0)
1035 content_info->media_meta.exposure_time = g_strdup(buf);
1038 /* Get fnumber value from exif. */
1039 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1040 content_info->media_meta.fnumber = fnumber;
1042 /* Get iso value from exif. */
1043 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1044 content_info->media_meta.iso = iso;
1046 memset(buf, 0x00, sizeof(buf));
1047 /* Get model value from exif. */
1048 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1049 if (strlen(buf) > 0)
1050 content_info->media_meta.model = g_strdup(buf);
1053 /* Get orientation value from exif. */
1054 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1055 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1056 content_info->media_meta.orientation = orient_value;
1059 /* Get width value from exif. */
1060 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1062 content_info->media_meta.width = exif_width;
1065 /* Get height value from exif. */
1066 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1067 if (exif_height > 0)
1068 content_info->media_meta.height = exif_height;
1072 exif_data_unref(ed);
1076 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1077 /*Get image width, height*/
1078 unsigned int img_width = 0;
1079 unsigned int img_height = 0;
1080 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1082 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1083 if (content_info->media_meta.width == 0)
1084 content_info->media_meta.width = img_width;
1086 if (content_info->media_meta.height == 0)
1087 content_info->media_meta.height = img_height;
1090 return MS_MEDIA_ERR_NONE;
1093 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1095 int ret = FILEINFO_ERROR_NONE;
1099 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1100 if (ret == FILEINFO_ERROR_NONE && size > 0)
1103 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1106 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1108 MMHandleType tag = 0;
1109 int mmf_error = FILEINFO_ERROR_NONE;
1111 content_info->path = g_strdup(path);
1113 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1114 if (mmf_error == FILEINFO_ERROR_NONE) {
1115 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1116 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1117 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1118 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1119 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1120 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1121 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1122 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1124 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1127 mm_file_destroy_tag_attrs(tag);
1129 return MS_MEDIA_ERR_NONE;
1132 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1134 MMHandleType content = 0;
1135 MMHandleType tag = 0;
1137 unsigned char *image = NULL;
1138 unsigned int size = 0;
1139 int mmf_error = FILEINFO_ERROR_NONE;
1141 int ret = MS_MEDIA_ERR_NONE;
1142 int convert_value = 0;
1145 /*Get Content Tag attribute ===========*/
1146 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1148 if (mmf_error == FILEINFO_ERROR_NONE) {
1149 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1150 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1151 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1152 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1153 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1154 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1155 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1156 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1157 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1159 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1160 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1161 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1162 /*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*/
1163 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1164 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1167 content_info->media_meta.recorded_date = g_strdup(p);
1170 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1171 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1172 if (content_info->timeline == 0)
1173 content_info->timeline = content_info->modified_time;
1175 /* This is same as datetaken */
1176 /* Remove compensation string */
1177 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1178 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1179 g_free(content_info->media_meta.recorded_date);
1180 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1182 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1187 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1188 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1189 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1190 content_info->media_meta.year = g_strdup(p);
1193 if (!content_info->media_meta.year)
1194 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1196 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1197 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1198 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1199 content_info->media_meta.rating = convert_value;
1201 content_info->media_meta.rating = 0;
1204 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1206 /*Do not extract artwork for the USB Storage content*/
1207 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1208 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1209 if (mmf_error != FILEINFO_ERROR_NONE)
1210 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1212 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1213 if (mmf_error != FILEINFO_ERROR_NONE)
1214 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1216 if (image != NULL && size > 0) {
1217 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1218 int artwork_mime_size = -1;
1220 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1221 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1222 ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1223 if (ret != MS_MEDIA_ERR_NONE) {
1224 media_svc_error("Fail to Get Thumbnail Path");
1226 ret = __media_svc_save_image(image, size, thumb_path, uid);
1227 if (ret != MS_MEDIA_ERR_NONE) {
1228 media_svc_error("Fail to Save Image");
1230 content_info->thumbnail_path = g_strdup(thumb_path);
1231 /* NOTICE : Prevent resize for performance (2020.02.07)
1232 * In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1233 * So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1236 /* albumart resizing */
1237 ret = __media_svc_resize_artwork(thumb_path, p);
1238 if (ret != MS_MEDIA_ERR_NONE) {
1239 media_svc_error("Fail to Make Thumbnail Image");
1240 _media_svc_remove_file(thumb_path);
1243 content_info->thumbnail_path = g_strdup(thumb_path);
1252 /*Initialize album_id to 0. below code will set the album_id*/
1253 content_info->album_id = album_id;
1254 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1255 if (ret != MS_MEDIA_ERR_NONE) {
1256 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1257 media_svc_debug("album does not exist. So start to make album art");
1258 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1259 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1260 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);
1262 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1264 content_info->album_id = album_id;
1267 content_info->album_id = album_id;
1270 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1271 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1272 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1274 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1275 double longitude = 0.0;
1276 double latitude = 0.0;
1277 double altitude = 0.0;
1279 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1280 MM_FILE_TAG_LATIDUE, &latitude,
1281 MM_FILE_TAG_ALTIDUE, &altitude,
1284 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1285 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1286 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1288 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1289 if (mmf_error != FILEINFO_ERROR_NONE)
1292 media_svc_debug("CDIS : %d", cdis_value);
1294 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1295 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1296 content_info->media_meta.orientation = atoi(p);
1298 content_info->media_meta.orientation = 0;
1299 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1303 mmf_error = mm_file_destroy_tag_attrs(tag);
1304 if (mmf_error != FILEINFO_ERROR_NONE)
1305 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1307 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1308 content_info->album_id = album_id;
1311 /*Get Content attribute ===========*/
1312 if (cdis_value == 1)
1313 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1315 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1317 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1319 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1320 int audio_bitrate = 0;
1321 int video_bitrate = 0;
1323 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1324 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1325 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1326 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1327 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1330 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1332 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1333 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1334 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1335 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1336 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1340 mm_file_destroy_content_attrs(content);
1342 return MS_MEDIA_ERR_NONE;
1345 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1347 media_svc_retm_if(content_info == NULL, "content info is NULL");
1349 /* Delete media_svc_content_info_s */
1350 SAFE_FREE(content_info->media_uuid);
1351 SAFE_FREE(content_info->path);
1352 SAFE_FREE(content_info->file_name);
1353 SAFE_FREE(content_info->mime_type);
1354 SAFE_FREE(content_info->folder_uuid);
1355 SAFE_FREE(content_info->thumbnail_path);
1356 SAFE_FREE(content_info->storage_uuid);
1358 /* Delete media_svc_content_meta_s */
1359 SAFE_FREE(content_info->media_meta.title);
1360 SAFE_FREE(content_info->media_meta.album);
1361 SAFE_FREE(content_info->media_meta.artist);
1362 SAFE_FREE(content_info->media_meta.album_artist);
1363 SAFE_FREE(content_info->media_meta.genre);
1364 SAFE_FREE(content_info->media_meta.composer);
1365 SAFE_FREE(content_info->media_meta.year);
1366 SAFE_FREE(content_info->media_meta.recorded_date);
1367 SAFE_FREE(content_info->media_meta.copyright);
1368 SAFE_FREE(content_info->media_meta.track_num);
1369 SAFE_FREE(content_info->media_meta.description);
1370 SAFE_FREE(content_info->media_meta.datetaken);
1371 SAFE_FREE(content_info->media_meta.exposure_time);
1372 SAFE_FREE(content_info->media_meta.model);
1374 SAFE_FREE(content_info->file_name_pinyin);
1375 SAFE_FREE(content_info->media_meta.title_pinyin);
1376 SAFE_FREE(content_info->media_meta.album_pinyin);
1377 SAFE_FREE(content_info->media_meta.artist_pinyin);
1378 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1379 SAFE_FREE(content_info->media_meta.genre_pinyin);
1380 SAFE_FREE(content_info->media_meta.composer_pinyin);
1381 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1382 SAFE_FREE(content_info->media_meta.description_pinyin);
1385 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1387 bool portrait = false;
1390 media_svc_retm_if(!orig_w, "Invalid orig_w");
1391 media_svc_retm_if(!orig_h, "Invalid orig_h");
1392 media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1393 media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1395 if (orig_w < orig_h)
1398 /* Set smaller length to default size */
1400 if (orig_w < *thumb_w)
1402 ratio = (double)orig_h / (double)orig_w;
1403 *thumb_h = *thumb_w * ratio;
1405 if (orig_h < *thumb_h)
1407 ratio = (double)orig_w / (double)orig_h;
1408 *thumb_w = *thumb_h * ratio;
1411 media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1416 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1418 int err = MS_MEDIA_ERR_NONE;
1419 MMHandleType tag = (MMHandleType) NULL;
1422 int _cdis_value = 0;
1423 mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1425 /* Get Content Tag attribute for orientation */
1426 err = mm_file_create_tag_attrs(&tag, origin_path);
1427 if (err != FILEINFO_ERROR_NONE) {
1428 *rot_type = MM_UTIL_ROTATE_0;
1433 err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1434 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1436 _rot_type = MM_UTIL_ROTATE_0;
1438 if (strncmp(p, "90", size) == 0)
1439 _rot_type = MM_UTIL_ROTATE_90;
1440 else if (strncmp(p, "180", size) == 0)
1441 _rot_type = MM_UTIL_ROTATE_180;
1442 else if (strncmp(p, "270", size) == 0)
1443 _rot_type = MM_UTIL_ROTATE_270;
1445 _rot_type = MM_UTIL_ROTATE_0;
1447 media_svc_debug("There is tag rotate : %d", _rot_type);
1449 media_svc_debug("There is NOT tag rotate");
1450 _rot_type = MM_UTIL_ROTATE_0;
1453 err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1454 if (err != FILEINFO_ERROR_NONE)
1457 *rot_type = _rot_type;
1458 *cdis_value = _cdis_value;
1460 err = mm_file_destroy_tag_attrs(tag);
1461 if (err != FILEINFO_ERROR_NONE) {
1462 media_svc_error("fail to free tag attr - err(%x)", err);
1468 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)
1470 int err = MS_MEDIA_ERR_NONE;
1471 MMHandleType content = (MMHandleType) NULL;
1472 int _video_track_num = 0;
1476 void *_frame = NULL;
1478 if (cdis_value == 1) {
1479 media_svc_debug("This is CDIS vlaue 1");
1480 err = mm_file_create_content_attrs_safe(&content, origin_path);
1482 err = mm_file_create_content_attrs(&content, origin_path);
1485 if (err != FILEINFO_ERROR_NONE) {
1486 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1487 return MS_MEDIA_ERR_INTERNAL;
1490 err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1491 if (err != FILEINFO_ERROR_NONE) {
1492 media_svc_error("mm_file_get_attrs fails : %d", err);
1493 mm_file_destroy_content_attrs(content);
1494 return MS_MEDIA_ERR_INTERNAL;
1497 *video_track_num = _video_track_num;
1499 if (_video_track_num == 0) {
1500 mm_file_destroy_content_attrs(content);
1501 return MS_MEDIA_ERR_NONE;
1504 err = mm_file_get_attrs(content,
1505 MM_FILE_CONTENT_VIDEO_WIDTH,
1507 MM_FILE_CONTENT_VIDEO_HEIGHT,
1509 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1512 if (err != FILEINFO_ERROR_NONE) {
1513 media_svc_error("mm_file_get_attrs fails : %d", err);
1514 mm_file_destroy_content_attrs(content);
1515 return MS_MEDIA_ERR_INTERNAL;
1518 media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1519 if (!_frame || !_width || !_height) {
1520 mm_file_destroy_content_attrs(content);
1521 return MS_MEDIA_ERR_INTERNAL;
1528 *frame = calloc(1, _size);
1529 memcpy(*frame, _frame, _size);
1531 mm_file_destroy_content_attrs(content);
1533 return MS_MEDIA_ERR_NONE;
1536 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)
1538 int err = MS_MEDIA_ERR_NONE;
1539 mm_util_image_h img = NULL;
1540 mm_util_image_h resize_img = NULL;
1542 media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1543 __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1544 if (thumb_width <= 0 || thumb_height <= 0) {
1545 media_svc_error("Failed to get thumb size");
1546 return MS_MEDIA_ERR_INTERNAL;
1549 media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1551 err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1552 media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1554 if (width > thumb_width || height > thumb_height) {
1555 if (rot_type != MM_UTIL_ROTATE_0) {
1556 err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1557 if (err != MM_UTIL_ERROR_NONE)
1560 err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1562 err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1565 if (rot_type != MM_UTIL_ROTATE_0)
1566 err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1568 err = mm_util_resize_B_P(img, width, height, thumb_path);
1572 mm_image_destroy_image(img);
1573 mm_image_destroy_image(resize_img);
1574 if (err != MS_MEDIA_ERR_NONE)
1575 return MS_MEDIA_ERR_INTERNAL;
1577 return MS_MEDIA_ERR_NONE;
1580 static int __create_video_thumbnail(const char *path, char *thumb_path)
1582 int ret = MS_MEDIA_ERR_NONE;
1584 unsigned int width = 0;
1585 unsigned int height = 0;
1587 int video_track_num = 0;
1589 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1591 __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1592 ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1593 media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1594 media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1596 ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1603 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1605 int ret = MS_MEDIA_ERR_NONE;
1606 unsigned int origin_w = 0;
1607 unsigned int origin_h = 0;
1608 unsigned int thumb_w = THUMB_WIDTH;
1609 unsigned int thumb_h = THUMB_HEIGHT;
1610 mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1612 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1613 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1614 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1616 ms_user_storage_type_e store_type = -1;
1617 ret = ms_user_get_storage_type(uid, path, &store_type);
1619 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1620 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1621 return MS_MEDIA_ERR_INVALID_PARAMETER;
1624 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1627 ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1628 if (ret != MS_MEDIA_ERR_NONE) {
1629 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1630 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1635 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1636 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1637 if (ret != MM_UTIL_ERROR_NONE) {
1638 media_svc_error("Getting image info is failed err: %d", ret);
1639 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1640 return MS_MEDIA_ERR_INTERNAL;
1643 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1644 media_svc_error("Unsupported image codec");
1645 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1648 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1649 media_svc_error("This original image is too big");
1650 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1651 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1654 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1655 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1656 if (ret != MM_UTIL_ERROR_NONE) {
1657 media_svc_error("mm_util_resize_P_P err: %d", ret);
1658 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1659 return MS_MEDIA_ERR_INTERNAL;
1662 return __create_video_thumbnail(path, thumb_path);
1668 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1670 int ret = MS_MEDIA_ERR_NONE;
1672 pinyin_name_s *pinyinname = NULL;
1674 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1675 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1679 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1680 if (ret == MS_MEDIA_ERR_NONE) {
1681 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1682 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1684 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1687 _media_svc_pinyin_free(pinyinname, size);
1692 bool _media_svc_check_pinyin_support(void)
1694 int ret = SYSTEM_INFO_ERROR_NONE;
1695 bool is_supported = false;
1696 static int media_svc_pinyin_support = -1;
1698 if (media_svc_pinyin_support == -1) {
1699 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1700 if (ret != SYSTEM_INFO_ERROR_NONE) {
1701 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1705 media_svc_pinyin_support = is_supported;
1708 return media_svc_pinyin_support;
1711 int _media_svc_get_media_type(const char *path, int *mediatype)
1713 int ret = MS_MEDIA_ERR_NONE;
1714 char mime_type[256] = {0};
1715 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1717 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1719 ret = __media_svc_get_mime_type(path, mime_type);
1720 if (ret == MS_MEDIA_ERR_NONE)
1721 __media_svc_get_media_type(path, mime_type, &media_type);
1723 media_svc_error("__media_svc_get_mime_type failed");
1725 *mediatype = media_type;
1730 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1732 switch (storage_type) {
1733 case MS_USER_STORAGE_INTERNAL:
1734 case MS_USER_STORAGE_EXTERNAL:
1735 case MS_USER_STORAGE_EXTERNAL_USB:
1738 media_svc_error("storage type is incorrect[%d]", storage_type);