4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #define __USE_XOPEN /* needed for strptime */
33 #include <system_info.h>
35 #include <glib/gstdio.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include "media-util-err.h"
46 #include "media-svc-util.h"
47 #include "media-svc-db-utils.h"
48 #include "media-svc-debug.h"
49 #include "media-svc-env.h"
50 #include "media-svc-hash.h"
51 #include "media-svc-album.h"
52 #include "media-svc-localize_ch.h"
54 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
56 /* Define data structures for media type and mime type */
57 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
58 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
59 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
60 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
61 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
62 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
63 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
64 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
65 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA category */
67 #define CONTENT_TYPE_NUM 5
68 #define MUSIC_MIME_NUM 29
69 #define SOUND_MIME_NUM 1
70 #define MIME_TYPE_LENGTH 255
71 #define MIME_LENGTH 50
72 #define _3GP_FILE ".3gp"
73 #define _MP4_FILE ".mp4"
74 #define _ASF_FILE ".asf"
75 #define MEDIA_SVC_ARTWORK_SIZE 2000
76 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
78 #define MEDIA_SVC_DEFAULT_GPS_VALUE -200 /**< Default GPS Value*/
80 static int media_svc_pinyin_support = -1;
83 char content_type[15];
85 } _media_svc_content_table_s;
87 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
88 {"audio", MEDIA_SVC_CATEGORY_SOUND},
89 {"image", MEDIA_SVC_CATEGORY_IMAGE},
90 {"video", MEDIA_SVC_CATEGORY_VIDEO},
91 {"application", MEDIA_SVC_CATEGORY_ETC},
92 {"text", MEDIA_SVC_CATEGORY_ETC},
95 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
96 /*known mime types of normal files*/
102 /* known mime types of drm files*/
104 "x-mp3", /*alias of audio/mpeg*/
105 "x-mpeg", /*alias of audio/mpeg*/
107 "x-ogg", /*alias of audio/ogg*/
108 "vnd.ms-playready.media.pya:*.pya", /*playready*/
111 "x-m4a", /*alias of audio/mp4*/
112 /* below mimes are rare*/
122 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
123 "vorbis", /*alias of audio/x-vorbis+ogg*/
125 "x-mp2", /*alias of audio/mp2*/
126 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
127 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
131 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
135 char *_media_info_generate_uuid(void)
138 static char uuid_unparsed[37];
141 uuid_generate(uuid_value);
142 uuid_unparse(uuid_value, uuid_unparsed);
144 if (strlen(uuid_unparsed) < 36) {
145 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
149 return uuid_unparsed;
152 static int __media_svc_split_to_double(char *input, double *arr)
154 char tmp_arr[255] = {0, };
155 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
157 if (!STRING_VALID(input)) {
158 media_svc_error("Invalid parameter");
159 return MS_MEDIA_ERR_INVALID_PARAMETER;
161 memset(tmp_arr, 0x0, sizeof(tmp_arr));
163 /*media_svc_debug("input: [%s]", input); */
167 for (idx = 0; idx < (len + 1); idx++) {
168 if (input[idx] == ' ') {
170 } else if ((input[idx] == ',') || (idx == len)) {
171 arr[arr_idx] = atof(tmp_arr);
174 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
175 memset(tmp_arr, 0x0, sizeof(tmp_arr));
177 tmp_arr[str_idx] = input[idx];
183 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
184 return MS_MEDIA_ERR_INTERNAL;
187 return MS_MEDIA_ERR_NONE;
190 /* GPS information is not ExifTag member */
191 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
193 int ret = MS_MEDIA_ERR_NONE;
195 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
196 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
198 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
199 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
201 entry = exif_data_get_entry(ed, tagtype);
203 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
204 gps_buf[strlen(gps_buf)] = '\0';
206 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
207 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
209 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
212 return MS_MEDIA_ERR_NONE;
215 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
219 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
220 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
222 entry = exif_data_get_entry(ed, tagtype);
224 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
225 value[strlen(value)] = '\0';
228 return MS_MEDIA_ERR_NONE;
231 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
234 ExifByteOrder mByteOrder;
235 ExifRational mRational;
236 long numerator, denominator;
238 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
240 entry = exif_data_get_entry(ed, tagtype);
241 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
244 case EXIF_TAG_ORIENTATION:
245 case EXIF_TAG_PIXEL_X_DIMENSION:
246 case EXIF_TAG_PIXEL_Y_DIMENSION:
247 case EXIF_TAG_ISO_SPEED_RATINGS:
248 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
250 mByteOrder = exif_data_get_byte_order(ed);
251 short exif_value = exif_get_short(entry->data, mByteOrder);
252 *i_value = (int)exif_value;
254 case EXIF_TAG_EXPOSURE_TIME:
255 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
257 mByteOrder = exif_data_get_byte_order(ed);
258 mRational = exif_get_rational(entry->data, mByteOrder);
259 numerator = mRational.numerator;
260 denominator = mRational.denominator;
261 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
263 case EXIF_TAG_FNUMBER:
264 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
266 mByteOrder = exif_data_get_byte_order(ed);
267 mRational = exif_get_rational(entry->data, mByteOrder);
268 numerator = mRational.numerator;
269 denominator = mRational.denominator;
271 *d_value = ((numerator*1.0)/(denominator*1.0));
274 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
276 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
277 buf[strlen(buf)] = '\0';
280 return MS_MEDIA_ERR_NONE;
283 static time_t __media_svc_get_timeline_from_str(const char *timstr)
286 time_t modified_t = 0;
290 if (!STRING_VALID(timstr)) {
291 media_svc_error("Invalid Parameter");
295 /*Exif Format : %Y:%m:%d %H:%M:%S
296 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
297 memset(&t, 0x00, sizeof(struct tm));
301 localtime_r(&rawtime, &timeinfo);
303 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
304 t.tm_isdst = timeinfo.tm_isdst;
306 media_svc_debug("DST %d", t.tm_isdst);
308 /* If time string has timezone */
309 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
311 char tim_tmp_str[255] = { 0, };
313 /* ISO8601 Time string format */
314 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
315 g_time_val_from_iso8601(tim_tmp_str, &timeval);
316 modified_t = timeval.tv_sec;
317 media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
320 modified_t = mktime(&t);
326 media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
328 media_svc_error("Failed to get timeline : [%s]", timstr);
334 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
338 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
339 media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
340 media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
342 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
344 /*categorize from mimetype */
345 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
346 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
347 *category = (*category | content_category[idx].category_by_mime);
352 /*in application type, exitst sound file ex) x-smafs, asf */
353 if (*category & MEDIA_SVC_CATEGORY_ETC) {
354 int prefix_len = strlen(content_category[3].content_type) + 1;
357 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
358 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
359 *category ^= MEDIA_SVC_CATEGORY_ETC;
360 *category |= MEDIA_SVC_CATEGORY_SOUND;
365 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
366 *category ^= MEDIA_SVC_CATEGORY_ETC;
367 *category |= MEDIA_SVC_CATEGORY_SOUND;
370 /*"asf" must check video stream and then categorize in directly. */
371 ext = strrchr(path, '.');
373 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
378 err = mm_file_get_stream_info(path, &audio, &video);
380 if (audio > 0 && video == 0) {
381 *category ^= MEDIA_SVC_CATEGORY_ETC;
382 *category |= MEDIA_SVC_CATEGORY_MUSIC;
384 *category ^= MEDIA_SVC_CATEGORY_ETC;
385 *category |= MEDIA_SVC_CATEGORY_VIDEO;
392 /*check music file in sound files. */
393 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
394 int prefix_len = strlen(content_category[0].content_type) + 1;
396 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
397 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
398 *category ^= MEDIA_SVC_CATEGORY_SOUND;
399 *category |= MEDIA_SVC_CATEGORY_MUSIC;
404 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
405 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
406 *category ^= MEDIA_SVC_CATEGORY_SOUND;
407 *category |= MEDIA_SVC_CATEGORY_ETC;
409 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
410 /*some video files don't have video stream. in this case it is categorize as music. */
412 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
413 ext = strrchr(path, '.');
415 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
420 err = mm_file_get_stream_info(path, &audio, &video);
422 if (audio > 0 && video == 0) {
423 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
424 *category |= MEDIA_SVC_CATEGORY_MUSIC;
427 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
432 return MS_MEDIA_ERR_NONE;
435 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
437 int ret = MS_MEDIA_ERR_NONE;
440 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
441 if (ret != MS_MEDIA_ERR_NONE)
442 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
445 case MEDIA_SVC_CATEGORY_SOUND:
446 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
448 case MEDIA_SVC_CATEGORY_MUSIC:
449 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
451 case MEDIA_SVC_CATEGORY_IMAGE:
452 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
454 case MEDIA_SVC_CATEGORY_VIDEO:
455 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
458 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
465 drm_contentifo is not NULL, if the file is OMA DRM.
466 If the file is not OMA DRM, drm_contentinfo must be NULL.
468 static int __media_svc_get_mime_type(const char *path, char *mimetype)
470 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
472 /*in case of normal files or failure to get mime in drm */
473 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
474 media_svc_error("aul_get_mime_from_file fail");
475 return MS_MEDIA_ERR_INTERNAL;
478 return MS_MEDIA_ERR_NONE;
481 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
485 for (i = strlen(file_path); i >= 0; i--) {
486 if (file_path[i] == '.') {
487 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
491 if (file_path[i] == '/')
497 static int __media_svc_resize_artwork(const char *path, const char *img_format)
499 int ret = MS_MEDIA_ERR_NONE;
500 unsigned int width = 0;
501 unsigned int height = 0;
502 unsigned int resized_width = 0;
503 unsigned int resized_height = 0;
504 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
506 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
507 media_svc_debug("type [jpeg]");
509 mm_util_extract_image_info(path, &img_type, &width, &height);
511 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
512 media_svc_debug("No need resizing");
513 return MS_MEDIA_ERR_NONE;
517 if (width > height) {
518 resized_height = MEDIA_SVC_ARTWORK_SIZE;
519 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
521 resized_width = MEDIA_SVC_ARTWORK_SIZE;
522 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
525 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
527 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
528 media_svc_debug("type [png]");
530 media_svc_debug("Not proper img format");
536 static int __media_svc_safe_atoi(char *buffer, int *si)
540 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
542 const long sl = strtol(buffer, &end, 10);
544 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
545 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
546 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
547 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
548 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
552 return MS_MEDIA_ERR_NONE;
555 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
557 int ret = MS_MEDIA_ERR_NONE;
559 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
562 media_svc_error("invalid image..");
563 return MS_MEDIA_ERR_INVALID_PARAMETER;
567 char *thumb_path = NULL;
568 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
569 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
571 if (-1 == statfs(thumb_path, &fs)) {
572 media_svc_error("error in statfs");
573 SAFE_FREE(thumb_path);
574 return MS_MEDIA_ERR_INTERNAL;
577 SAFE_FREE(thumb_path);
579 long bsize_kbytes = fs.f_bsize >> 10;
581 if ((bsize_kbytes * fs.f_bavail) < 1024) {
582 media_svc_error("not enought space...");
583 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
588 if (image != NULL && size > 0) {
589 fp = fopen(image_path, "w");
592 media_svc_error("failed to open file");
593 return MS_MEDIA_ERR_INTERNAL;
596 nwrite = fwrite(image, 1, size, fp);
597 if (nwrite != size) {
598 media_svc_error("failed to write thumbnail");
600 return MS_MEDIA_ERR_INTERNAL;
605 return MS_MEDIA_ERR_NONE;
608 static char *__media_svc_get_title_from_filepath(const char *path)
610 char *filename = NULL;
613 int filename_len = -1;
614 int new_title_len = -1;
617 media_svc_error("path is NULL");
621 filename = g_path_get_basename(path);
622 if (!STRING_VALID(filename)) {
623 media_svc_error("wrong file name");
628 filename_len = strlen(filename);
630 ext = g_strrstr(filename, ".");
632 media_svc_error("there is no file extention");
636 new_title_len = filename_len - strlen(ext);
637 if (new_title_len < 1) {
638 media_svc_error("title length is zero");
643 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
647 media_svc_debug("extract title is [%s]", title);
652 void _media_svc_remove_file(const char *path)
654 if (!STRING_VALID(path))
657 if (remove(path) != 0)
658 media_svc_stderror("fail to remove file result");
661 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
663 int ret = MS_MEDIA_ERR_NONE;
664 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
665 char hash[255 + 1] = {0, };
666 char *thumb_dir = NULL;
667 char *thumbfile_ext = NULL;
669 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
670 if (!STRING_VALID(thumb_dir)) {
671 media_svc_error("ms_user_get_root_thumb_store_path failed");
672 return MS_MEDIA_ERR_INTERNAL;
675 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
676 media_svc_error("Wrong path[%s]", thumb_dir);
677 SAFE_FREE(thumb_dir);
678 return MS_MEDIA_ERR_INTERNAL;
681 memset(file_ext, 0, sizeof(file_ext));
682 if (!__media_svc_get_file_ext(pathname, file_ext))
683 media_svc_error("get file ext fail");
685 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
686 if (ret != MS_MEDIA_ERR_NONE) {
687 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
688 SAFE_FREE(thumb_dir);
689 return MS_MEDIA_ERR_INTERNAL;
692 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
693 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
694 thumbfile_ext = (char *)"jpg";
695 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
696 thumbfile_ext = (char *)"png";
697 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
698 thumbfile_ext = (char *)"gif";
699 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
700 thumbfile_ext = (char *)"bmp";
702 media_svc_error("Not proper img format");
703 SAFE_FREE(thumb_dir);
704 return MS_MEDIA_ERR_INTERNAL;
707 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
709 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
712 SAFE_FREE(thumb_dir);
714 return MS_MEDIA_ERR_NONE;
717 int _media_svc_get_file_time(const char *full_path)
722 memset(&statbuf, 0, sizeof(struct stat));
723 fd = stat(full_path, &statbuf);
725 media_svc_sec_error("stat(%s) fails.", full_path);
726 return MS_MEDIA_ERR_INTERNAL;
729 return statbuf.st_mtime;
732 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)
734 int ret = MS_MEDIA_ERR_NONE;
735 char * media_uuid = NULL;
736 bool drm_type = false;
737 char mime_type[256] = {0, };
738 media_svc_media_type_e media_type;
740 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
741 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
743 content_info->path = g_strdup(path);
744 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
747 memset(&st, 0, sizeof(struct stat));
748 if (stat(path, &st) == 0) {
749 content_info->modified_time = st.st_mtime;
750 content_info->timeline = content_info->modified_time;
751 content_info->size = st.st_size;
753 media_svc_stderror("stat failed");
756 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
758 media_svc_debug("refresh");
759 return MS_MEDIA_ERR_NONE;
762 content_info->storage_uuid = g_strdup(storage_id);
763 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
765 content_info->storage_type = storage_type;
766 time(&content_info->added_time);
768 media_uuid = _media_info_generate_uuid();
769 if (media_uuid == NULL) {
770 _media_svc_destroy_content_info(content_info);
771 return MS_MEDIA_ERR_INTERNAL;
774 content_info->media_uuid = g_strdup(media_uuid);
775 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
777 content_info->file_name = g_path_get_basename(path);
778 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
780 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
781 if drm_contentinfo is not NULL, the file is OMA DRM.*/
782 ret = __media_svc_get_mime_type(path, mime_type);
783 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
785 media_svc_debug("mime [%s]", mime_type);
786 content_info->is_drm = drm_type;
788 ret = __media_svc_get_media_type(path, mime_type, &media_type);
789 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
791 content_info->mime_type = g_strdup(mime_type);
792 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
794 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
796 content_info->media_type = media_type;
798 return MS_MEDIA_ERR_NONE;
801 int image_360_check(char *path)
806 unsigned char exif_header[4] = {0, };
807 unsigned char exif_app1[2] = {0, };
808 unsigned char exif_app1_xmp[2] = {0, };
809 long exif_app1_xmp_size = 0;
810 unsigned char exif_app1_xmp_t[2] = {0, };
811 char *xmp_data = NULL;
817 memset(exif_header, 0x00, sizeof(exif_header));
818 memset(exif_app1, 0x00, sizeof(exif_app1));
819 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
820 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
822 fp = fopen(path, "rb");
826 size = fread(exif_header, 1, sizeof(exif_header), fp);
830 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
831 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
835 size1 = exif_app1[0];
836 size2 = exif_app1[1];
838 app1_size = size1 * 256 + size2 - 2;
840 if (fseek(fp, app1_size, SEEK_CUR) != 0)
843 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
847 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
850 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
854 size1 = exif_app1_xmp_t[0];
855 size2 = exif_app1_xmp_t[1];
857 exif_app1_xmp_size = size1 * 256 + size2 - 2;
859 if (exif_app1_xmp_size > 0) {
860 xmp_data = (char *)malloc(exif_app1_xmp_size);
861 memset(xmp_data, 0x0, exif_app1_xmp_size);
865 while (exif_app1_xmp_size >= 0) {
866 exif_app1_xmp_size--;
878 if (strstr(ptr, "UsePanoramaViewer")
879 && strstr(ptr, "True")
880 && strstr(ptr, "ProjectionType")
881 && strstr(ptr, "equirectangular"))
886 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
908 static char * __media_svc_get_title(MMHandleType tag, const char *path)
910 int ret = FILEINFO_ERROR_NONE;
916 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
917 if (ret == FILEINFO_ERROR_NONE && size > 0) {
918 while(p && isspace(*p))
925 title = __media_svc_get_title_from_filepath(path);
929 media_svc_error("Can't extract title");
931 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
934 char * _media_svc_get_title_by_path(const char *path)
936 /* No MMHandleType in media-svc.c */
937 return __media_svc_get_title(NULL, path);
940 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
943 int orient_value = 0;
947 bool has_datetaken = false;
948 double fnumber = 0.0;
952 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
954 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
955 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);
956 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
958 path = content_info->path;
959 content_info->media_meta.title = __media_svc_get_title(NULL, path);
961 /* Load an ExifData object from an EXIF file */
962 ed = exif_data_new_from_file(path);
965 media_svc_sec_debug("There is no exif data in [ %s ]", path);
966 goto GET_WIDTH_HEIGHT;
969 content_info->media_meta.is_360 = image_360_check(path);
971 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
972 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
973 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
975 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
976 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
977 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
978 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
979 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
980 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
981 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
982 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
983 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
985 memset(buf, 0x00, sizeof(buf));
986 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
987 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
988 if (!g_strcmp0(buf, "S"))
990 content_info->media_meta.latitude = value;
994 memset(buf, 0x00, sizeof(buf));
995 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
996 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
997 if (!g_strcmp0(buf, "W"))
1000 content_info->media_meta.longitude = value;
1004 memset(buf, 0x00, sizeof(buf));
1005 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1006 if (strlen(buf) > 0)
1007 content_info->media_meta.description = g_strdup(buf);
1009 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1013 memset(buf, 0x00, sizeof(buf));
1014 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1015 if (strlen(buf) > 0) {
1016 has_datetaken = true;
1017 content_info->media_meta.datetaken = g_strdup(buf);
1019 /* This is same as recorded_date */
1020 content_info->media_meta.recorded_date = g_strdup(buf);
1024 memset(buf, 0x00, sizeof(buf));
1025 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1026 if (strlen(buf) > 0) {
1027 has_datetaken = true;
1028 content_info->media_meta.datetaken = g_strdup(buf);
1030 /* This is same as recorded_date */
1031 content_info->media_meta.recorded_date = g_strdup(buf);
1035 if (has_datetaken) {
1036 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1037 if (content_info->timeline == 0)
1038 content_info->timeline = content_info->modified_time;
1040 media_svc_debug("Timeline : %ld", content_info->timeline);
1043 memset(buf, 0x00, sizeof(buf));
1044 /* Get exposure_time value from exif. */
1045 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1046 if (strlen(buf) > 0)
1047 content_info->media_meta.exposure_time = g_strdup(buf);
1050 /* Get fnumber value from exif. */
1051 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1052 content_info->media_meta.fnumber = fnumber;
1054 /* Get iso value from exif. */
1055 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1056 content_info->media_meta.iso = iso;
1058 memset(buf, 0x00, sizeof(buf));
1059 /* Get model value from exif. */
1060 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1061 if (strlen(buf) > 0)
1062 content_info->media_meta.model = g_strdup(buf);
1065 /* Get orientation value from exif. */
1066 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1067 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1068 content_info->media_meta.orientation = orient_value;
1071 /* Get width value from exif. */
1072 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1074 content_info->media_meta.width = exif_width;
1077 /* Get height value from exif. */
1078 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1079 if (exif_height > 0)
1080 content_info->media_meta.height = exif_height;
1084 exif_data_unref(ed);
1088 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1089 /*Get image width, height*/
1090 unsigned int img_width = 0;
1091 unsigned int img_height = 0;
1092 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1094 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1095 if (content_info->media_meta.width == 0)
1096 content_info->media_meta.width = img_width;
1098 if (content_info->media_meta.height == 0)
1099 content_info->media_meta.height = img_height;
1102 return MS_MEDIA_ERR_NONE;
1105 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1107 int ret = FILEINFO_ERROR_NONE;
1111 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1112 if (ret == FILEINFO_ERROR_NONE && size > 0)
1115 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1118 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1120 MMHandleType tag = 0;
1121 int mmf_error = FILEINFO_ERROR_NONE;
1123 content_info->path = g_strdup(path);
1125 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1126 if (mmf_error == FILEINFO_ERROR_NONE) {
1127 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1128 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1129 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1130 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1131 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1132 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1133 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1134 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1136 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1139 mm_file_destroy_tag_attrs(tag);
1141 return MS_MEDIA_ERR_NONE;
1144 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1146 MMHandleType content = 0;
1147 MMHandleType tag = 0;
1149 unsigned char *image = NULL;
1150 unsigned int size = 0;
1151 int mmf_error = FILEINFO_ERROR_NONE;
1153 int ret = MS_MEDIA_ERR_NONE;
1154 int convert_value = 0;
1157 /*Get Content Tag attribute ===========*/
1158 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1160 if (mmf_error == FILEINFO_ERROR_NONE) {
1161 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1162 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1163 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1164 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1165 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1166 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1167 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1168 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1169 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1171 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1172 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1173 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1174 /*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*/
1175 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1176 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1179 content_info->media_meta.recorded_date = g_strdup(p);
1182 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1183 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1184 if (content_info->timeline == 0)
1185 content_info->timeline = content_info->modified_time;
1187 /* This is same as datetaken */
1188 /* Remove compensation string */
1189 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1190 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1191 g_free(content_info->media_meta.recorded_date);
1192 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1194 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1199 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1200 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1201 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1202 content_info->media_meta.year = g_strdup(p);
1205 if (!content_info->media_meta.year)
1206 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1208 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1209 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1210 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1211 content_info->media_meta.rating = convert_value;
1213 content_info->media_meta.rating = 0;
1216 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1218 /*Do not extract artwork for the USB Storage content*/
1219 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1220 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1221 if (mmf_error != FILEINFO_ERROR_NONE)
1222 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1224 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1225 if (mmf_error != FILEINFO_ERROR_NONE)
1226 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1228 if (image != NULL && size > 0) {
1229 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1230 int artwork_mime_size = -1;
1232 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1233 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1234 ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1235 if (ret != MS_MEDIA_ERR_NONE) {
1236 media_svc_error("Fail to Get Thumbnail Path");
1238 ret = __media_svc_save_image(image, size, thumb_path, uid);
1239 if (ret != MS_MEDIA_ERR_NONE) {
1240 media_svc_error("Fail to Save Image");
1242 content_info->thumbnail_path = g_strdup(thumb_path);
1243 /* NOTICE : Prevent resize for performance (2020.02.07)
1244 * In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1245 * So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1248 /* albumart resizing */
1249 ret = __media_svc_resize_artwork(thumb_path, p);
1250 if (ret != MS_MEDIA_ERR_NONE) {
1251 media_svc_error("Fail to Make Thumbnail Image");
1252 _media_svc_remove_file(thumb_path);
1255 content_info->thumbnail_path = g_strdup(thumb_path);
1264 /*Initialize album_id to 0. below code will set the album_id*/
1265 content_info->album_id = album_id;
1266 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1267 if (ret != MS_MEDIA_ERR_NONE) {
1268 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1269 media_svc_debug("album does not exist. So start to make album art");
1270 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1271 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1272 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);
1274 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1276 content_info->album_id = album_id;
1279 content_info->album_id = album_id;
1282 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1283 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1284 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1286 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1287 double longitude = 0.0;
1288 double latitude = 0.0;
1289 double altitude = 0.0;
1291 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1292 MM_FILE_TAG_LATIDUE, &latitude,
1293 MM_FILE_TAG_ALTIDUE, &altitude,
1296 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1297 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1298 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1300 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1301 if (mmf_error != FILEINFO_ERROR_NONE)
1304 media_svc_debug("CDIS : %d", cdis_value);
1306 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1307 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1308 content_info->media_meta.orientation = atoi(p);
1310 content_info->media_meta.orientation = 0;
1311 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1315 mmf_error = mm_file_destroy_tag_attrs(tag);
1316 if (mmf_error != FILEINFO_ERROR_NONE)
1317 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1319 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1320 content_info->album_id = album_id;
1323 /*Get Content attribute ===========*/
1324 if (cdis_value == 1)
1325 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1327 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1329 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1331 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1332 int audio_bitrate = 0;
1333 int video_bitrate = 0;
1335 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1336 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1337 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1338 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1339 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1342 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1344 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1345 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1346 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1347 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1348 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1352 mm_file_destroy_content_attrs(content);
1354 return MS_MEDIA_ERR_NONE;
1357 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1359 media_svc_retm_if(content_info == NULL, "content info is NULL");
1361 /* Delete media_svc_content_info_s */
1362 SAFE_FREE(content_info->media_uuid);
1363 SAFE_FREE(content_info->path);
1364 SAFE_FREE(content_info->file_name);
1365 SAFE_FREE(content_info->mime_type);
1366 SAFE_FREE(content_info->folder_uuid);
1367 SAFE_FREE(content_info->thumbnail_path);
1368 SAFE_FREE(content_info->storage_uuid);
1370 /* Delete media_svc_content_meta_s */
1371 SAFE_FREE(content_info->media_meta.title);
1372 SAFE_FREE(content_info->media_meta.album);
1373 SAFE_FREE(content_info->media_meta.artist);
1374 SAFE_FREE(content_info->media_meta.album_artist);
1375 SAFE_FREE(content_info->media_meta.genre);
1376 SAFE_FREE(content_info->media_meta.composer);
1377 SAFE_FREE(content_info->media_meta.year);
1378 SAFE_FREE(content_info->media_meta.recorded_date);
1379 SAFE_FREE(content_info->media_meta.copyright);
1380 SAFE_FREE(content_info->media_meta.track_num);
1381 SAFE_FREE(content_info->media_meta.description);
1382 SAFE_FREE(content_info->media_meta.datetaken);
1383 SAFE_FREE(content_info->media_meta.exposure_time);
1384 SAFE_FREE(content_info->media_meta.model);
1386 SAFE_FREE(content_info->file_name_pinyin);
1387 SAFE_FREE(content_info->media_meta.title_pinyin);
1388 SAFE_FREE(content_info->media_meta.album_pinyin);
1389 SAFE_FREE(content_info->media_meta.artist_pinyin);
1390 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1391 SAFE_FREE(content_info->media_meta.genre_pinyin);
1392 SAFE_FREE(content_info->media_meta.composer_pinyin);
1393 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1394 SAFE_FREE(content_info->media_meta.description_pinyin);
1397 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1399 bool portrait = false;
1402 media_svc_retm_if(!orig_w, "Invalid orig_w");
1403 media_svc_retm_if(!orig_h, "Invalid orig_h");
1404 media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1405 media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1407 if (orig_w < orig_h)
1410 /* Set smaller length to default size */
1412 if (orig_w < *thumb_w)
1414 ratio = (double)orig_h / (double)orig_w;
1415 *thumb_h = *thumb_w * ratio;
1417 if (orig_h < *thumb_h)
1419 ratio = (double)orig_w / (double)orig_h;
1420 *thumb_w = *thumb_h * ratio;
1423 media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1428 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1430 int err = MS_MEDIA_ERR_NONE;
1431 MMHandleType tag = (MMHandleType) NULL;
1434 int _cdis_value = 0;
1435 mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1437 /* Get Content Tag attribute for orientation */
1438 err = mm_file_create_tag_attrs(&tag, origin_path);
1439 if (err != FILEINFO_ERROR_NONE) {
1440 *rot_type = MM_UTIL_ROTATE_0;
1445 err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1446 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1448 _rot_type = MM_UTIL_ROTATE_0;
1450 if (strncmp(p, "90", size) == 0)
1451 _rot_type = MM_UTIL_ROTATE_90;
1452 else if (strncmp(p, "180", size) == 0)
1453 _rot_type = MM_UTIL_ROTATE_180;
1454 else if (strncmp(p, "270", size) == 0)
1455 _rot_type = MM_UTIL_ROTATE_270;
1457 _rot_type = MM_UTIL_ROTATE_0;
1459 media_svc_debug("There is tag rotate : %d", _rot_type);
1461 media_svc_debug("There is NOT tag rotate");
1462 _rot_type = MM_UTIL_ROTATE_0;
1465 err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1466 if (err != FILEINFO_ERROR_NONE)
1469 *rot_type = _rot_type;
1470 *cdis_value = _cdis_value;
1472 err = mm_file_destroy_tag_attrs(tag);
1473 if (err != FILEINFO_ERROR_NONE) {
1474 media_svc_error("fail to free tag attr - err(%x)", err);
1480 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)
1482 int err = MS_MEDIA_ERR_NONE;
1483 MMHandleType content = (MMHandleType) NULL;
1484 int _video_track_num = 0;
1488 void *_frame = NULL;
1490 if (cdis_value == 1) {
1491 media_svc_debug("This is CDIS vlaue 1");
1492 err = mm_file_create_content_attrs_safe(&content, origin_path);
1494 err = mm_file_create_content_attrs(&content, origin_path);
1497 if (err != FILEINFO_ERROR_NONE) {
1498 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1499 return MS_MEDIA_ERR_INTERNAL;
1502 err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1503 if (err != FILEINFO_ERROR_NONE) {
1504 media_svc_error("mm_file_get_attrs fails : %d", err);
1505 mm_file_destroy_content_attrs(content);
1506 return MS_MEDIA_ERR_INTERNAL;
1509 *video_track_num = _video_track_num;
1511 if (_video_track_num == 0) {
1512 mm_file_destroy_content_attrs(content);
1513 return MS_MEDIA_ERR_NONE;
1516 err = mm_file_get_attrs(content,
1517 MM_FILE_CONTENT_VIDEO_WIDTH,
1519 MM_FILE_CONTENT_VIDEO_HEIGHT,
1521 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1524 if (err != FILEINFO_ERROR_NONE) {
1525 media_svc_error("mm_file_get_attrs fails : %d", err);
1526 mm_file_destroy_content_attrs(content);
1527 return MS_MEDIA_ERR_INTERNAL;
1530 media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1531 if (!_frame || !_width || !_height) {
1532 mm_file_destroy_content_attrs(content);
1533 return MS_MEDIA_ERR_INTERNAL;
1540 *frame = calloc(1, _size);
1541 memcpy(*frame, _frame, _size);
1543 mm_file_destroy_content_attrs(content);
1545 return MS_MEDIA_ERR_NONE;
1548 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)
1550 int err = MS_MEDIA_ERR_NONE;
1551 mm_util_image_h img = NULL;
1552 mm_util_image_h resize_img = NULL;
1554 media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1555 __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1556 if (thumb_width <= 0 || thumb_height <= 0) {
1557 media_svc_error("Failed to get thumb size");
1558 return MS_MEDIA_ERR_INTERNAL;
1561 media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1563 err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1564 media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1566 if (width > thumb_width || height > thumb_height) {
1567 if (rot_type != MM_UTIL_ROTATE_0) {
1568 err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1569 if (err != MM_UTIL_ERROR_NONE)
1572 err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1574 err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1577 if (rot_type != MM_UTIL_ROTATE_0)
1578 err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1580 err = mm_util_resize_B_P(img, width, height, thumb_path);
1584 mm_image_destroy_image(img);
1585 mm_image_destroy_image(resize_img);
1586 if (err != MS_MEDIA_ERR_NONE)
1587 return MS_MEDIA_ERR_INTERNAL;
1589 return MS_MEDIA_ERR_NONE;
1592 static int __create_video_thumbnail(const char *path, char *thumb_path)
1594 int ret = MS_MEDIA_ERR_NONE;
1596 unsigned int width = 0;
1597 unsigned int height = 0;
1599 int video_track_num = 0;
1601 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1603 __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1604 ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1605 media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1606 media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1608 ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1615 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1617 int ret = MS_MEDIA_ERR_NONE;
1618 unsigned int origin_w = 0;
1619 unsigned int origin_h = 0;
1620 unsigned int thumb_w = THUMB_WIDTH;
1621 unsigned int thumb_h = THUMB_HEIGHT;
1622 mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1624 if (path == NULL || thumb_path == NULL) {
1625 media_svc_error("Invalid parameter");
1626 return MS_MEDIA_ERR_INVALID_PARAMETER;
1629 if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1630 media_svc_error("Original path(%s) doesn't exist.", path);
1631 return MS_MEDIA_ERR_INVALID_PARAMETER;
1634 ms_user_storage_type_e store_type = -1;
1635 ret = ms_user_get_storage_type(uid, path, &store_type);
1637 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1638 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1639 return MS_MEDIA_ERR_INVALID_PARAMETER;
1642 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1645 ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1646 if (ret != MS_MEDIA_ERR_NONE) {
1647 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1648 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1653 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1654 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1655 if (ret != MS_MEDIA_ERR_NONE) {
1656 media_svc_error("Getting image info is failed err: %d", ret);
1657 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1658 return MS_MEDIA_ERR_INTERNAL;
1661 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1662 media_svc_error("Unsupported image codec");
1663 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1666 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1667 media_svc_error("This original image is too big");
1668 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1669 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1672 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1673 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1674 if (ret != MM_UTIL_ERROR_NONE) {
1675 media_svc_error("mm_util_resize_P_P err: %d", ret);
1676 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1677 return MS_MEDIA_ERR_INTERNAL;
1680 return __create_video_thumbnail(path, thumb_path);
1686 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1688 int ret = MS_MEDIA_ERR_NONE;
1690 pinyin_name_s *pinyinname = NULL;
1692 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1693 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1697 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1698 if (ret == MS_MEDIA_ERR_NONE) {
1699 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1700 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1702 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1705 _media_svc_pinyin_free(pinyinname, size);
1710 bool _media_svc_check_pinyin_support(void)
1712 int ret = SYSTEM_INFO_ERROR_NONE;
1713 bool is_supported = false;
1715 if (media_svc_pinyin_support == -1) {
1716 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1717 if (ret != SYSTEM_INFO_ERROR_NONE) {
1718 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1722 media_svc_pinyin_support = is_supported;
1725 return media_svc_pinyin_support;
1728 int _media_svc_get_media_type(const char *path, int *mediatype)
1730 int ret = MS_MEDIA_ERR_NONE;
1731 char mime_type[256] = {0};
1732 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1734 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1736 ret = __media_svc_get_mime_type(path, mime_type);
1737 if (ret == MS_MEDIA_ERR_NONE)
1738 __media_svc_get_media_type(path, mime_type, &media_type);
1740 media_svc_error("__media_svc_get_mime_type failed");
1742 *mediatype = media_type;
1747 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1749 switch (storage_type) {
1750 case MS_USER_STORAGE_INTERNAL:
1751 case MS_USER_STORAGE_EXTERNAL:
1752 case MS_USER_STORAGE_EXTERNAL_USB:
1755 media_svc_error("storage type is incorrect[%d]", storage_type);