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-thumbnail.h>
43 #include <media-util.h>
44 #include <uuid/uuid.h>
45 #include <mm_util_magick.h>
46 #include "media-util-err.h"
47 #include "media-svc-util.h"
48 #include "media-svc-db-utils.h"
49 #include "media-svc-debug.h"
50 #include "media-svc-env.h"
51 #include "media-svc-hash.h"
52 #include "media-svc-album.h"
53 #include "media-svc-localize_ch.h"
55 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
57 /* Define data structures for media type and mime type */
58 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
59 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
60 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
61 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
62 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
63 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
64 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
65 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
66 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA category */
68 #define CONTENT_TYPE_NUM 5
69 #define MUSIC_MIME_NUM 29
70 #define SOUND_MIME_NUM 1
71 #define MIME_TYPE_LENGTH 255
72 #define MIME_LENGTH 50
73 #define _3GP_FILE ".3gp"
74 #define _MP4_FILE ".mp4"
75 #define _ASF_FILE ".asf"
76 #define MEDIA_SVC_ARTWORK_SIZE 2000
77 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
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 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
196 return MS_MEDIA_ERR_INVALID_PARAMETER;
200 entry = exif_data_get_entry(ed, tag);
202 /* Get the contents of the tag in human-readable form */
203 if (tag == EXIF_TAG_ORIENTATION ||
204 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
205 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
206 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
208 if (i_value == NULL) {
209 media_svc_debug("i_value is NULL");
210 return MS_MEDIA_ERR_INVALID_PARAMETER;
213 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
214 short exif_value = exif_get_short(entry->data, mByteOrder);
215 *i_value = (int)exif_value;
217 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
219 if (d_value == NULL) {
220 media_svc_debug("d_value is NULL");
221 return MS_MEDIA_ERR_INVALID_PARAMETER;
224 /* Get the contents of the tag in human-readable form */
225 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
226 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
227 gps_buf[strlen(gps_buf)] = '\0';
228 int ret = MS_MEDIA_ERR_NONE;
230 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
232 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
233 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
235 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
236 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
239 media_svc_debug("buf is NULL");
240 return MS_MEDIA_ERR_INVALID_PARAMETER;
243 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
244 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
245 long numerator = mRational.numerator;
246 long denominator = mRational.denominator;
247 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
249 } else if (tag == EXIF_TAG_FNUMBER) {
251 if (d_value == NULL) {
252 media_svc_debug("d_value is NULL");
253 return MS_MEDIA_ERR_INVALID_PARAMETER;
256 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
257 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
258 long numerator = mRational.numerator;
259 long denominator = mRational.denominator;
261 *d_value = ((numerator*1.0)/(denominator*1.0));
266 media_svc_debug("buf is NULL");
267 return MS_MEDIA_ERR_INVALID_PARAMETER;
270 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
271 buf[strlen(buf)] = '\0';
275 return MS_MEDIA_ERR_NONE;
278 time_t __media_svc_get_timeline_from_str(const char *timstr)
281 time_t modified_t = 0;
285 if (!STRING_VALID(timstr)) {
286 media_svc_error("Invalid Parameter");
290 /*Exif Format : %Y:%m:%d %H:%M:%S
291 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
292 memset(&t, 0x00, sizeof(struct tm));
296 localtime_r(&rawtime, &timeinfo);
298 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
299 t.tm_isdst = timeinfo.tm_isdst;
301 media_svc_debug("DST %d", t.tm_isdst);
303 /* If time string has timezone */
304 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
306 char tim_tmp_str[255] = { 0, };
308 /* ISO8601 Time string format */
309 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
310 g_time_val_from_iso8601(tim_tmp_str, &timeval);
311 modified_t = timeval.tv_sec;
312 media_svc_debug("Calibrated timeval : [%d][%s]", modified_t, tim_tmp_str);
315 modified_t = mktime(&t);
321 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);
323 media_svc_error("Failed to get timeline : [%s]", timstr);
329 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
333 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
335 /*categorize from mimetype */
336 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
337 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
338 *category = (*category | content_category[idx].category_by_mime);
343 /*in application type, exitst sound file ex) x-smafs, asf */
344 if (*category & MEDIA_SVC_CATEGORY_ETC) {
345 int prefix_len = strlen(content_category[0].content_type);
348 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
349 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
350 *category ^= MEDIA_SVC_CATEGORY_ETC;
351 *category |= MEDIA_SVC_CATEGORY_SOUND;
356 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
357 *category ^= MEDIA_SVC_CATEGORY_ETC;
358 *category |= MEDIA_SVC_CATEGORY_SOUND;
361 /*"asf" must check video stream and then categorize in directly. */
362 ext = strrchr(path, '.');
364 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
369 err = mm_file_get_stream_info(path, &audio, &video);
371 if (audio > 0 && video == 0) {
372 *category ^= MEDIA_SVC_CATEGORY_ETC;
373 *category |= MEDIA_SVC_CATEGORY_MUSIC;
375 *category ^= MEDIA_SVC_CATEGORY_ETC;
376 *category |= MEDIA_SVC_CATEGORY_VIDEO;
383 /*check music file in sound files. */
384 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
385 int prefix_len = strlen(content_category[0].content_type) + 1;
387 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
388 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
389 *category ^= MEDIA_SVC_CATEGORY_SOUND;
390 *category |= MEDIA_SVC_CATEGORY_MUSIC;
395 /*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*/
396 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
397 *category ^= MEDIA_SVC_CATEGORY_SOUND;
398 *category |= MEDIA_SVC_CATEGORY_ETC;
400 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
401 /*some video files don't have video stream. in this case it is categorize as music. */
403 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
404 ext = strrchr(path, '.');
406 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
411 err = mm_file_get_stream_info(path, &audio, &video);
413 if (audio > 0 && video == 0) {
414 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
415 *category |= MEDIA_SVC_CATEGORY_MUSIC;
418 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
423 return MS_MEDIA_ERR_NONE;
426 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
428 int ret = MS_MEDIA_ERR_NONE;
431 media_svc_media_type_e type;
433 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
434 if (ret != MS_MEDIA_ERR_NONE)
435 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
437 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
438 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
439 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
440 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
441 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
449 drm_contentifo is not NULL, if the file is OMA DRM.
450 If the file is not OMA DRM, drm_contentinfo must be NULL.
452 static int __media_svc_get_mime_type(const char *path, char *mimetype)
455 return MS_MEDIA_ERR_INVALID_PARAMETER;
457 /*in case of normal files or failure to get mime in drm */
458 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
459 media_svc_error("aul_get_mime_from_file fail");
460 return MS_MEDIA_ERR_INTERNAL;
463 return MS_MEDIA_ERR_NONE;
466 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
470 for (i = strlen(file_path); i >= 0; i--) {
471 if (file_path[i] == '.') {
472 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
476 if (file_path[i] == '/')
482 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
484 char *err_attr_name = NULL;
485 double gps_value = 0.0;
486 int mmf_error = FILEINFO_ERROR_NONE;
488 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
489 if (mmf_error == FILEINFO_ERROR_NONE) {
490 if (longitude != NULL)
491 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
493 SAFE_FREE(err_attr_name);
496 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
497 if (mmf_error == FILEINFO_ERROR_NONE) {
498 if (latitude != NULL)
499 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
501 SAFE_FREE(err_attr_name);
504 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
505 if (mmf_error == FILEINFO_ERROR_NONE) {
506 if (altitude != NULL)
507 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
509 SAFE_FREE(err_attr_name);
512 return MS_MEDIA_ERR_NONE;
515 static int __media_svc_resize_artwork(const char *path, const char *img_format)
517 int ret = MS_MEDIA_ERR_NONE;
518 unsigned int width = 0;
519 unsigned int height = 0;
520 unsigned int resized_width = 0;
521 unsigned int resized_height = 0;
522 mm_util_img_code_type img_type = IMG_CODEC_NONE;
524 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
525 media_svc_debug("type [jpeg]");
527 mm_util_extract_image_info(path, &img_type, &width, &height);
529 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
530 media_svc_debug("No need resizing");
531 return MS_MEDIA_ERR_NONE;
535 if (width > height) {
536 resized_height = MEDIA_SVC_ARTWORK_SIZE;
537 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
539 resized_width = MEDIA_SVC_ARTWORK_SIZE;
540 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
543 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
545 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
546 media_svc_debug("type [png]");
548 media_svc_debug("Not proper img format");
554 static int __media_svc_safe_atoi(char *buffer, int *si)
558 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
560 const long sl = strtol(buffer, &end, 10);
562 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
563 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
564 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
565 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
566 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
570 return MS_MEDIA_ERR_NONE;
573 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
575 int ret = MS_MEDIA_ERR_NONE;
577 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
580 media_svc_error("invalid image..");
581 return MS_MEDIA_ERR_INVALID_PARAMETER;
585 char *thumb_path = NULL;
586 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
587 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
589 if (-1 == statfs(thumb_path, &fs)) {
590 media_svc_error("error in statfs");
591 SAFE_FREE(thumb_path);
592 return MS_MEDIA_ERR_INTERNAL;
595 SAFE_FREE(thumb_path);
597 long bsize_kbytes = fs.f_bsize >> 10;
599 if ((bsize_kbytes * fs.f_bavail) < 1024) {
600 media_svc_error("not enought space...");
601 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
606 if (image != NULL && size > 0) {
607 fp = fopen(image_path, "w");
610 media_svc_error("failed to open file");
611 return MS_MEDIA_ERR_INTERNAL;
614 nwrite = fwrite(image, 1, size, fp);
615 if (nwrite != size) {
616 media_svc_error("failed to write thumbnail");
618 return MS_MEDIA_ERR_INTERNAL;
623 return MS_MEDIA_ERR_NONE;
626 static char *__media_svc_get_title_from_filepath(const char *path)
628 char *filename = NULL;
631 int filename_len = -1;
632 int new_title_len = -1;
635 media_svc_error("path is NULL");
639 filename = g_path_get_basename(path);
640 if (!STRING_VALID(filename)) {
641 media_svc_error("wrong file name");
646 filename_len = strlen(filename);
648 ext = g_strrstr(filename, ".");
650 media_svc_error("there is no file extention");
654 new_title_len = filename_len - strlen(ext);
655 if (new_title_len < 1) {
656 media_svc_error("title length is zero");
661 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
665 media_svc_debug("extract title is [%s]", title);
670 bool __media_svc_check_support_pinyin()
672 int ret = SYSTEM_INFO_ERROR_NONE;
673 bool is_supported = false;
675 if (media_svc_pinyin_support == -1) {
676 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
677 if (ret != SYSTEM_INFO_ERROR_NONE) {
678 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
682 media_svc_pinyin_support = is_supported;
685 return media_svc_pinyin_support;
687 int _media_svc_rename_file(const char *old_name, const char *new_name)
689 if ((old_name == NULL) || (new_name == NULL)) {
690 media_svc_error("invalid file name");
691 return MS_MEDIA_ERR_INVALID_PARAMETER;
694 if (rename(old_name, new_name) < 0) {
695 media_svc_stderror(" ");
696 return MS_MEDIA_ERR_INTERNAL;
699 return MS_MEDIA_ERR_NONE;
702 int _media_svc_remove_file(const char *path)
706 result = remove(path);
708 media_svc_debug("success to remove file");
709 return MS_MEDIA_ERR_NONE;
711 media_svc_stderror("fail to remove file result");
712 return MS_MEDIA_ERR_INTERNAL;
716 int _media_svc_remove_all_files_in_dir(const char *dir_path)
718 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
720 GError *error = NULL;
723 dir = g_dir_open(dir_path, 0, &error);
724 if (dir != NULL && error == NULL) {
725 while ((name = g_dir_read_name(dir))) {
726 memset(filename, 0, sizeof(filename));
727 snprintf(filename, sizeof(filename), "%s/%s", dir_path, name);
729 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
730 if (unlink(filename) != 0)
731 media_svc_stderror("failed to remove");
735 media_svc_error("%s is not exist", dir_path);
736 return MS_MEDIA_ERR_INVALID_PARAMETER;
741 return MS_MEDIA_ERR_NONE;
744 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
746 int ret = MS_MEDIA_ERR_NONE;
747 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
748 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
749 char hash[255 + 1] = {0, };
750 char *thumbfile_ext = NULL;
751 char *thumb_dir = NULL;
753 ret = ms_user_get_thumb_store_path(uid, storage_type, &thumb_dir);
754 if (!STRING_VALID(thumb_dir)) {
755 media_svc_error("ms_user_get_thumb_store_path failed");
756 return MS_MEDIA_ERR_INTERNAL;
759 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
760 media_svc_error("Wrong path[%s]", thumb_dir);
761 SAFE_FREE(thumb_dir);
762 return MS_MEDIA_ERR_INTERNAL;
765 memset(file_ext, 0, sizeof(file_ext));
766 if (!__media_svc_get_file_ext(pathname, file_ext))
767 media_svc_error("get file ext fail");
769 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
770 if (ret != MS_MEDIA_ERR_NONE) {
771 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
772 SAFE_FREE(thumb_dir);
773 return MS_MEDIA_ERR_INTERNAL;
776 /*media_svc_debug("img format is [%s]", img_format); */
778 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
779 thumbfile_ext = (char *)"jpg";
780 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
781 thumbfile_ext = (char *)"png";
782 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
783 thumbfile_ext = (char *)"gif";
784 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
785 thumbfile_ext = (char *)"bmp";
787 media_svc_error("Not proper img format");
788 SAFE_FREE(thumb_dir);
789 return MS_MEDIA_ERR_INTERNAL;
792 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
793 SAFE_STRLCPY(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
794 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
796 SAFE_FREE(thumb_dir);
798 return MS_MEDIA_ERR_NONE;
801 int _media_svc_get_file_time(const char *full_path)
806 memset(&statbuf, 0, sizeof(struct stat));
807 fd = stat(full_path, &statbuf);
809 media_svc_error("stat(%s) fails.", full_path);
810 return MS_MEDIA_ERR_INTERNAL;
813 return statbuf.st_mtime;
816 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
818 /* Set default GPS value before extracting meta information */
819 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
820 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
821 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
823 /* Set filename to title for all media */
825 title = __media_svc_get_title_from_filepath(content_info->path);
827 content_info->media_meta.title = g_strdup(title);
829 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
831 media_svc_error("Can't extract title");
832 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
833 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
836 /* Set default value before extracting meta information */
837 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
838 media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
840 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
841 media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
843 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
844 media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
846 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
847 media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
849 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
850 media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
852 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
853 media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
855 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
856 media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
858 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
859 media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
861 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
862 media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
865 media_svc_debug("refresh");
866 return MS_MEDIA_ERR_NONE;
869 content_info->played_count = 0;
870 content_info->last_played_time = 0;
871 content_info->last_played_position = 0;
872 content_info->favourate = 0;
873 content_info->media_meta.rating = 0;
875 return MS_MEDIA_ERR_NONE;
878 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
879 const char *path, media_svc_media_type_e *media_type, bool refresh)
881 int ret = MS_MEDIA_ERR_NONE;
882 char * media_uuid = NULL;
883 bool drm_type = false;
884 char mime_type[256] = {0, };
886 content_info->path = g_strdup(path);
887 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
890 memset(&st, 0, sizeof(struct stat));
891 if (stat(path, &st) == 0) {
892 content_info->modified_time = st.st_mtime;
893 content_info->timeline = content_info->modified_time;
894 content_info->size = st.st_size;
896 media_svc_stderror("stat failed");
899 _media_svc_set_default_value(content_info, refresh);
901 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
903 media_svc_debug("refresh");
904 return MS_MEDIA_ERR_NONE;
907 content_info->storage_uuid = g_strdup(storage_id);
908 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
910 content_info->storage_type = storage_type;
911 time(&content_info->added_time);
913 media_uuid = _media_info_generate_uuid();
914 if (media_uuid == NULL) {
915 _media_svc_destroy_content_info(content_info);
916 return MS_MEDIA_ERR_INTERNAL;
919 content_info->media_uuid = g_strdup(media_uuid);
920 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
922 content_info->file_name = g_path_get_basename(path);
923 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
925 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
926 if drm_contentinfo is not NULL, the file is OMA DRM.*/
927 ret = __media_svc_get_mime_type(path, mime_type);
928 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
930 media_svc_debug("mime [%s]", mime_type);
931 content_info->is_drm = drm_type;
933 ret = __media_svc_get_media_type(path, mime_type, media_type);
934 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
935 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
936 media_svc_error("invalid media_type condition[%d]", *media_type);
937 return MS_MEDIA_ERR_INVALID_PARAMETER;
940 content_info->mime_type = g_strdup(mime_type);
941 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
943 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
945 content_info->media_type = *media_type;
947 return MS_MEDIA_ERR_NONE;
950 int image_360_check(char *path)
955 unsigned char exif_header[4];
956 unsigned char exif_app1[2];
957 unsigned char exif_app1_xmp[2];
958 long exif_app1_xmp_size = 0;
959 unsigned char exif_app1_xmp_t[2];
960 char *xmp_data = NULL;
966 fp = fopen(path, "rb");
970 size = fread(exif_header, 1, sizeof(exif_header), fp);
974 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
975 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
979 size1 = exif_app1[0];
980 size2 = exif_app1[1];
982 app1_size = size1 * 256 + size2 - 2;
984 if (fseek(fp, app1_size, SEEK_CUR) != 0)
987 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
991 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
994 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
998 size1 = exif_app1_xmp_t[0];
999 size2 = exif_app1_xmp_t[1];
1001 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1003 xmp_data = (char *)malloc(exif_app1_xmp_size);
1004 if (xmp_data != NULL) {
1005 memset(xmp_data, 0x0, exif_app1_xmp_size);
1008 while (exif_app1_xmp_size >= 0) {
1009 exif_app1_xmp_size--;
1021 if (strstr(ptr, "UsePanoramaViewer")
1022 && strstr(ptr, "True")
1023 && strstr(ptr, "ProjectionType")
1024 && strstr(ptr, "equirectangular"))
1027 SAFE_FREE(xmp_data);
1029 media_svc_error("malloc failed");
1051 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1054 int orient_value = 0;
1056 int exif_height = 0;
1057 ExifData *ed = NULL;
1058 int has_datetaken = FALSE;
1059 double fnumber = 0.0;
1063 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1065 memset(buf, 0x00, sizeof(buf));
1067 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1068 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1069 return MS_MEDIA_ERR_INVALID_PARAMETER;
1072 path = content_info->path;
1073 if (!STRING_VALID(path)) {
1074 media_svc_error("Invalid Path");
1075 return MS_MEDIA_ERR_INVALID_PARAMETER;
1078 /* Load an ExifData object from an EXIF file */
1079 ed = exif_data_new_from_file(path);
1082 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1083 goto GET_WIDTH_HEIGHT;
1086 content_info->media_meta.is_360 = image_360_check(path);
1088 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1089 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1090 if (strlen(buf) > 0) {
1091 if (strcmp(buf, "S") == 0)
1094 content_info->media_meta.latitude = value;
1096 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1099 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1102 memset(buf, 0x00, sizeof(buf));
1104 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1105 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1106 if (strlen(buf) > 0) {
1107 if (strcmp(buf, "W") == 0)
1110 content_info->media_meta.longitude = value;
1112 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1115 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1118 memset(buf, 0x00, sizeof(buf));
1120 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1121 if (strlen(buf) == 0)
1122 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1124 content_info->media_meta.description = g_strdup(buf);
1126 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1129 memset(buf, 0x00, sizeof(buf));
1131 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1132 if (strlen(buf) > 0) {
1133 has_datetaken = TRUE;
1134 content_info->media_meta.datetaken = g_strdup(buf);
1136 /* This is same as recorded_date */
1137 content_info->media_meta.recorded_date = g_strdup(buf);
1141 memset(buf, 0x00, sizeof(buf));
1143 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1144 if (strlen(buf) > 0) {
1145 has_datetaken = TRUE;
1146 content_info->media_meta.datetaken = g_strdup(buf);
1148 /* This is same as recorded_date */
1149 content_info->media_meta.recorded_date = g_strdup(buf);
1153 if (has_datetaken) {
1154 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1155 if (content_info->timeline == 0)
1156 content_info->timeline = content_info->modified_time;
1158 media_svc_debug("Timeline : %ld", content_info->timeline);
1161 memset(buf, 0x00, sizeof(buf));
1163 /* Get exposure_time value from exif. */
1164 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1165 if (strlen(buf) > 0)
1166 content_info->media_meta.exposure_time = g_strdup(buf);
1169 /* Get fnumber value from exif. */
1170 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1172 content_info->media_meta.fnumber = fnumber;
1174 content_info->media_meta.fnumber = 0.0;
1176 content_info->media_meta.fnumber = 0.0;
1179 /* Get iso value from exif. */
1180 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1182 content_info->media_meta.iso = iso;
1184 content_info->media_meta.iso = 0;
1186 content_info->media_meta.iso = 0;
1189 memset(buf, 0x00, sizeof(buf));
1191 /* Get model value from exif. */
1192 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1193 if (strlen(buf) > 0)
1194 content_info->media_meta.model = g_strdup(buf);
1197 /* Get orientation value from exif. */
1198 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1199 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1200 content_info->media_meta.orientation = orient_value;
1202 content_info->media_meta.orientation = 0;
1204 content_info->media_meta.orientation = 0;
1207 /* Get width value from exif. */
1208 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1210 content_info->media_meta.width = exif_width;
1212 content_info->media_meta.width = 0;
1214 content_info->media_meta.width = 0;
1217 /* Get height value from exif. */
1218 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1219 if (exif_height > 0)
1220 content_info->media_meta.height = exif_height;
1222 content_info->media_meta.height = 0;
1224 content_info->media_meta.height = 0;
1227 if (ed != NULL) exif_data_unref(ed);
1231 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1232 /*Get image width, height*/
1233 unsigned int img_width = 0;
1234 unsigned int img_height = 0;
1235 mm_util_img_code_type img_type = IMG_CODEC_NONE;
1237 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1238 if (content_info->media_meta.width == 0)
1239 content_info->media_meta.width = img_width;
1241 if (content_info->media_meta.height == 0)
1242 content_info->media_meta.height = img_height;
1245 return MS_MEDIA_ERR_NONE;
1248 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1250 MMHandleType tag = 0;
1253 int mmf_error = FILEINFO_ERROR_NONE;
1254 char *err_attr_name = NULL;
1257 /*Get Content Tag attribute ===========*/
1258 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1260 if (mmf_error == FILEINFO_ERROR_NONE) {
1261 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1262 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1263 content_info->media_meta.album = g_strdup(p);
1265 SAFE_FREE(err_attr_name);
1267 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1268 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1269 content_info->media_meta.artist = g_strdup(p);
1271 SAFE_FREE(err_attr_name);
1273 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1274 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1275 content_info->media_meta.album_artist = g_strdup(p);
1277 SAFE_FREE(err_attr_name);
1279 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1280 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1281 content_info->media_meta.genre = g_strdup(p);
1283 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1285 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1286 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1290 SAFE_FREE(err_attr_name);
1293 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1294 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1296 content_info->media_meta.title = g_strdup(p);
1300 for (idx = 0; idx < size; idx++) {
1302 media_svc_debug("SPACE [%s]", p);
1306 media_svc_debug("Not SPACE [%s]", p);
1307 content_info->media_meta.title = g_strdup(p);
1314 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1315 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1316 content_info->media_meta.description = g_strdup(p);
1318 SAFE_FREE(err_attr_name);
1320 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1321 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1322 content_info->media_meta.composer = g_strdup(p);
1324 SAFE_FREE(err_attr_name);
1326 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1327 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1328 content_info->media_meta.copyright = g_strdup(p);
1330 SAFE_FREE(err_attr_name);
1332 mmf_error = mm_file_destroy_tag_attrs(tag);
1333 if (mmf_error != FILEINFO_ERROR_NONE)
1334 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1336 content_info->album_id = album_id;
1339 return MS_MEDIA_ERR_NONE;
1342 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1344 MMHandleType content = 0;
1345 MMHandleType tag = 0;
1347 unsigned char *image = NULL;
1348 unsigned int size = 0;
1349 int mmf_error = FILEINFO_ERROR_NONE;
1350 char *err_attr_name = NULL;
1352 int ret = MS_MEDIA_ERR_NONE;
1355 /*Get Content Tag attribute ===========*/
1356 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1358 if (mmf_error == FILEINFO_ERROR_NONE) {
1359 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1360 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1361 content_info->media_meta.album = g_strdup(p);
1363 SAFE_FREE(err_attr_name);
1365 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1366 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1367 content_info->media_meta.artist = g_strdup(p);
1369 SAFE_FREE(err_attr_name);
1371 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1372 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1373 content_info->media_meta.album_artist = g_strdup(p);
1375 SAFE_FREE(err_attr_name);
1377 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1378 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1379 content_info->media_meta.genre = g_strdup(p);
1381 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1383 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1384 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1388 SAFE_FREE(err_attr_name);
1391 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1392 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1394 content_info->media_meta.title = g_strdup(p);
1398 for (idx = 0; idx < size; idx++) {
1400 media_svc_debug("SPACE [%s]", p);
1404 media_svc_debug("Not SPACE [%s]", p);
1405 content_info->media_meta.title = g_strdup(p);
1412 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1413 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1414 content_info->media_meta.description = g_strdup(p);
1416 SAFE_FREE(err_attr_name);
1418 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1419 if (mmf_error != FILEINFO_ERROR_NONE)
1420 SAFE_FREE(err_attr_name);
1422 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1423 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1424 char mime_type[255] = {0, };
1425 ret = __media_svc_get_mime_type(content_info->path, mime_type);
1426 /*if 3gp that audio only, media_type is music */
1427 if ((ret == MS_MEDIA_ERR_NONE) &&
1428 ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
1429 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
1430 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
1431 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
1432 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1433 char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
1435 char *time_value = time_info;
1436 if (time_info != NULL) {
1437 while (*p_value != '\0') {
1438 if (*p_value == '-')
1441 *time_value = *p_value;
1445 content_info->media_meta.recorded_date = g_strdup(time_info);
1446 SAFE_FREE(time_info);
1448 media_svc_error("memory allocation error");
1449 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1452 content_info->media_meta.recorded_date = g_strdup(p);
1455 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1456 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1457 if (content_info->timeline == 0)
1458 content_info->timeline = content_info->modified_time;
1460 media_svc_debug("Timeline : %ld", content_info->timeline);
1462 /* This is same as datetaken */
1463 /* Remove compensation string */
1464 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1465 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1466 G_SAFE_FREE(content_info->media_meta.recorded_date);
1467 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1469 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1473 SAFE_FREE(err_attr_name);
1476 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1477 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1478 content_info->media_meta.composer = g_strdup(p);
1480 SAFE_FREE(err_attr_name);
1482 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1483 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1484 content_info->media_meta.copyright = g_strdup(p);
1486 SAFE_FREE(err_attr_name);
1488 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1489 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1490 content_info->media_meta.track_num = g_strdup(p);
1492 SAFE_FREE(err_attr_name);
1494 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1495 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1497 if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1498 content_info->media_meta.year = g_strdup(p);
1500 media_svc_debug("Wrong Year Information [%s]", p);
1502 SAFE_FREE(err_attr_name);
1505 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1506 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1508 if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1509 content_info->media_meta.rating = rate;
1511 SAFE_FREE(err_attr_name);
1512 content_info->media_meta.rating = 0;
1515 /*Do not extract artwork for the USB Storage content*/
1516 if (content_info->storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1517 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1518 if (mmf_error != FILEINFO_ERROR_NONE) {
1519 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1520 SAFE_FREE(err_attr_name);
1522 /*media_svc_debug("artwork size1 [%d]", size); */
1525 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1526 if (mmf_error != FILEINFO_ERROR_NONE) {
1527 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1528 SAFE_FREE(err_attr_name);
1530 /*media_svc_debug("artwork size2 [%d]", size); */
1533 if (image != NULL && size > 0) {
1534 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1535 int artwork_mime_size = -1;
1537 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1538 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1539 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1540 if (ret != MS_MEDIA_ERR_NONE)
1541 media_svc_error("Fail to Get Thumbnail Path");
1543 if (strlen(thumb_path) > 0) {
1544 ret = __media_svc_save_image(image, size, thumb_path, uid);
1545 if (ret != MS_MEDIA_ERR_NONE) {
1546 media_svc_error("Fail to Save Image");
1548 /* albumart resizing */
1549 ret = __media_svc_resize_artwork(thumb_path, p);
1550 if (ret != MS_MEDIA_ERR_NONE) {
1551 media_svc_error("Fail to Make Thumbnail Image");
1552 _media_svc_remove_file(thumb_path);
1555 content_info->thumbnail_path = g_strdup(thumb_path);
1560 SAFE_FREE(err_attr_name);
1565 /*Initialize album_id to 0. below code will set the album_id*/
1566 content_info->album_id = album_id;
1567 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1568 if (ret != MS_MEDIA_ERR_NONE) {
1569 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1570 media_svc_debug("album does not exist. So start to make album art");
1571 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1572 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1573 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1575 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1577 content_info->album_id = album_id;
1580 content_info->album_id = album_id;
1583 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1584 double longitude = 0.0;
1585 double latitude = 0.0;
1586 double altitude = 0.0;
1588 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1589 content_info->media_meta.longitude = longitude;
1590 content_info->media_meta.latitude = latitude;
1591 content_info->media_meta.altitude = altitude;
1593 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1594 if (mmf_error != FILEINFO_ERROR_NONE) {
1596 SAFE_FREE(err_attr_name);
1599 media_svc_debug("CDIS : %d", cdis_value);
1601 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1602 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1603 content_info->media_meta.orientation = atoi(p);
1605 SAFE_FREE(err_attr_name);
1606 content_info->media_meta.orientation = 0;
1607 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1611 mmf_error = mm_file_destroy_tag_attrs(tag);
1612 if (mmf_error != FILEINFO_ERROR_NONE)
1613 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1615 content_info->album_id = album_id;
1618 /*Get Content attribute ===========*/
1619 if (cdis_value == 1)
1620 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1622 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1624 if (mmf_error == FILEINFO_ERROR_NONE) {
1625 /*Common attribute*/
1626 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1627 if (mmf_error != FILEINFO_ERROR_NONE) {
1628 SAFE_FREE(err_attr_name);
1629 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1631 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1634 /*Sound/Music attribute*/
1635 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1637 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1638 if (mmf_error != FILEINFO_ERROR_NONE) {
1639 SAFE_FREE(err_attr_name);
1640 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1642 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1645 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1646 if (mmf_error != FILEINFO_ERROR_NONE) {
1647 SAFE_FREE(err_attr_name);
1648 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1650 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1653 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1654 if (mmf_error != FILEINFO_ERROR_NONE) {
1655 SAFE_FREE(err_attr_name);
1656 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1658 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1661 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1662 if (mmf_error != FILEINFO_ERROR_NONE) {
1663 SAFE_FREE(err_attr_name);
1664 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1666 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1668 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
1669 int audio_bitrate = 0;
1670 int video_bitrate = 0;
1672 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1673 if (mmf_error != FILEINFO_ERROR_NONE) {
1674 SAFE_FREE(err_attr_name);
1675 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1677 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1680 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1681 if (mmf_error != FILEINFO_ERROR_NONE) {
1682 SAFE_FREE(err_attr_name);
1683 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1685 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1688 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1690 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1691 if (mmf_error != FILEINFO_ERROR_NONE) {
1692 SAFE_FREE(err_attr_name);
1693 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1695 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1698 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1699 if (mmf_error != FILEINFO_ERROR_NONE) {
1700 SAFE_FREE(err_attr_name);
1701 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1703 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1706 media_svc_error("Not support type");
1707 mmf_error = mm_file_destroy_content_attrs(content);
1708 if (mmf_error != FILEINFO_ERROR_NONE)
1709 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1711 return MS_MEDIA_ERR_INVALID_PARAMETER;
1714 mmf_error = mm_file_destroy_content_attrs(content);
1715 if (mmf_error != FILEINFO_ERROR_NONE)
1716 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1718 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1721 return MS_MEDIA_ERR_NONE;
1724 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1726 media_svc_retm_if(content_info == NULL, "content info is NULL");
1728 /* Delete media_svc_content_info_s */
1729 SAFE_FREE(content_info->media_uuid);
1730 SAFE_FREE(content_info->path);
1731 SAFE_FREE(content_info->file_name);
1732 SAFE_FREE(content_info->mime_type);
1733 SAFE_FREE(content_info->folder_uuid);
1734 SAFE_FREE(content_info->thumbnail_path);
1735 SAFE_FREE(content_info->storage_uuid);
1737 /* Delete media_svc_content_meta_s */
1738 SAFE_FREE(content_info->media_meta.title);
1739 SAFE_FREE(content_info->media_meta.album);
1740 SAFE_FREE(content_info->media_meta.artist);
1741 SAFE_FREE(content_info->media_meta.album_artist);
1742 SAFE_FREE(content_info->media_meta.genre);
1743 SAFE_FREE(content_info->media_meta.composer);
1744 SAFE_FREE(content_info->media_meta.year);
1745 SAFE_FREE(content_info->media_meta.recorded_date);
1746 SAFE_FREE(content_info->media_meta.copyright);
1747 SAFE_FREE(content_info->media_meta.track_num);
1748 SAFE_FREE(content_info->media_meta.description);
1749 SAFE_FREE(content_info->media_meta.datetaken);
1750 SAFE_FREE(content_info->media_meta.exposure_time);
1751 SAFE_FREE(content_info->media_meta.model);
1752 SAFE_FREE(content_info->media_meta.weather);
1753 SAFE_FREE(content_info->media_meta.category);
1754 SAFE_FREE(content_info->media_meta.keyword);
1755 SAFE_FREE(content_info->media_meta.location_tag);
1756 SAFE_FREE(content_info->media_meta.content_name);
1757 SAFE_FREE(content_info->media_meta.age_rating);
1758 SAFE_FREE(content_info->media_meta.author);
1759 SAFE_FREE(content_info->media_meta.provider);
1761 SAFE_FREE(content_info->media_meta.title_pinyin);
1762 SAFE_FREE(content_info->media_meta.album_pinyin);
1763 SAFE_FREE(content_info->media_meta.artist_pinyin);
1764 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1765 SAFE_FREE(content_info->media_meta.genre_pinyin);
1766 SAFE_FREE(content_info->media_meta.composer_pinyin);
1767 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1768 SAFE_FREE(content_info->media_meta.description_pinyin);
1773 int _media_svc_request_thumbnail(const char *path, char *thumb_path, int max_length, uid_t uid)
1775 int ret = MS_MEDIA_ERR_NONE;
1777 ret = thumbnail_request_from_db(path, thumb_path, max_length, uid);
1778 if (ret != MS_MEDIA_ERR_NONE) {
1779 media_svc_error("thumbnail_request_from_db failed: %d", ret);
1780 ret = MS_MEDIA_ERR_INTERNAL;
1782 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
1788 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1790 int ret = MS_MEDIA_ERR_NONE;
1792 pinyin_name_s *pinyinname = NULL;
1796 if (!STRING_VALID(src_str)) {
1797 media_svc_debug("String is invalid");
1801 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1802 if (ret == MS_MEDIA_ERR_NONE) {
1803 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1804 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1806 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
1809 _media_svc_pinyin_free(pinyinname, size);
1814 bool _media_svc_check_pinyin_support(void)
1816 /* Check CSC : TODO : need to check CSC */
1818 /* Check content.filter.pinyin feature */
1819 return __media_svc_check_support_pinyin();
1822 int _media_svc_get_media_type(const char *path, int *mediatype)
1824 int ret = MS_MEDIA_ERR_NONE;
1825 char mime_type[256] = {0};
1826 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1828 ret = __media_svc_get_mime_type(path, mime_type);
1829 if (ret == MS_MEDIA_ERR_NONE)
1830 __media_svc_get_media_type(path, mime_type, &media_type);
1832 media_svc_error("__media_svc_get_mime_type failed");
1834 *mediatype = media_type;