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 */
34 #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 <img-codec-parser.h>
46 #include <image_util.h>
49 #include "media-util-err.h"
50 #include "media-svc-util.h"
51 #include "media-svc-db-utils.h"
52 #include "media-svc-debug.h"
53 #include "media-svc-env.h"
54 #include "media-svc-hash.h"
55 #include "media-svc-album.h"
56 #include "media-svc-localize-utils.h"
57 #include "media-svc-localize_ch.h"
58 #include "media-svc-localize_tw.h"
60 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
62 /* Define data structures for media type and mime type */
63 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
64 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
65 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
66 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
67 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
68 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
69 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
70 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
71 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA category */
73 #define CONTENT_TYPE_NUM 5
74 #define MUSIC_MIME_NUM 29
75 #define SOUND_MIME_NUM 1
76 #define MIME_TYPE_LENGTH 255
77 #define MIME_LENGTH 50
78 #define _3GP_FILE ".3gp"
79 #define _MP4_FILE ".mp4"
80 #define _ASF_FILE ".asf"
81 #define MEDIA_SVC_INI_GET_INT(dict, key, value, default) \
83 value = iniparser_getint(dict, key, default); \
84 media_svc_debug("get %s = %d", key, value); \
86 #define MEDIA_SVC_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
87 #define MEDIA_SVC_ARTWORK_SIZE 2000
89 static int g_ini_value = -1;
92 char content_type[15];
94 } _media_svc_content_table_s;
96 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
97 {"audio", MEDIA_SVC_CATEGORY_SOUND},
98 {"image", MEDIA_SVC_CATEGORY_IMAGE},
99 {"video", MEDIA_SVC_CATEGORY_VIDEO},
100 {"application", MEDIA_SVC_CATEGORY_ETC},
101 {"text", MEDIA_SVC_CATEGORY_ETC},
104 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
105 /*known mime types of normal files*/
111 /* known mime types of drm files*/
113 "x-mp3", /*alias of audio/mpeg*/
114 "x-mpeg", /*alias of audio/mpeg*/
116 "x-ogg", /*alias of audio/ogg*/
117 "vnd.ms-playready.media.pya:*.pya", /*playready*/
120 "x-m4a", /*alias of audio/mp4*/
121 /* below mimes are rare*/
131 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
132 "vorbis", /*alias of audio/x-vorbis+ogg*/
134 "x-mp2", /*alias of audio/mp2*/
135 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
136 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
140 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
145 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
146 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
147 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
148 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
149 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
150 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
151 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
152 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
153 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
154 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
155 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
156 MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
157 } media_svc_extracted_field_e;
159 char *_media_info_generate_uuid(void)
162 static char uuid_unparsed[37];
165 uuid_generate(uuid_value);
166 uuid_unparse(uuid_value, uuid_unparsed);
168 if (strlen(uuid_unparsed) < 36) {
169 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
173 return uuid_unparsed;
176 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
178 if (!x_src || strlen(x_src) == 0) {
179 media_svc_error("x_src is NULL");
184 media_svc_error("length is Wrong");
188 strncpy(x_dst, x_src, max_len - 1);
189 x_dst[max_len - 1] = '\0';
192 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
196 if (!STRING_VALID(src)) {
197 media_svc_error("invalid src");
198 return MS_MEDIA_ERR_INVALID_PARAMETER;
203 len = strlen(src) + 1;
207 media_svc_error("malloc failed");
208 return MS_MEDIA_ERR_INTERNAL;
211 strncpy(*dst, src, len);
215 return MS_MEDIA_ERR_NONE;
218 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
220 if (!STRING_VALID(src)) {
221 media_svc_error("invalid src");
222 return MS_MEDIA_ERR_INVALID_PARAMETER;
227 *dst = malloc(copysize + 1);
230 media_svc_error("malloc failed");
231 return MS_MEDIA_ERR_INTERNAL;
234 strncpy(*dst, src, copysize);
238 return MS_MEDIA_ERR_NONE;
241 static int __media_svc_split_to_double(char *input, double *arr)
243 char tmp_arr[255] = {0, };
244 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
246 if (!STRING_VALID(input)) {
247 media_svc_error("Invalid parameter");
248 return MS_MEDIA_ERR_INVALID_PARAMETER;
250 memset(tmp_arr, 0x0, sizeof(tmp_arr));
252 /*media_svc_debug("input: [%s]", input); */
256 for (idx = 0; idx < (len + 1); idx++) {
257 if (input[idx] == ' ') {
259 } else if ((input[idx] == ',') || (idx == len)) {
260 arr[arr_idx] = atof(tmp_arr);
263 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
264 memset(tmp_arr, 0x0, sizeof(tmp_arr));
266 tmp_arr[str_idx] = input[idx];
272 media_svc_error("Error when parsing GPS [%d]", arr_idx);
273 return MS_MEDIA_ERR_INTERNAL;
276 return MS_MEDIA_ERR_NONE;
279 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
285 return MS_MEDIA_ERR_INVALID_PARAMETER;
289 entry = exif_data_get_entry(ed, tag);
291 /* Get the contents of the tag in human-readable form */
292 if (tag == EXIF_TAG_ORIENTATION ||
293 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
294 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
295 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
297 if (i_value == NULL) {
298 media_svc_error("i_value is NULL");
299 return MS_MEDIA_ERR_INVALID_PARAMETER;
302 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
303 short exif_value = exif_get_short(entry->data, mByteOrder);
304 *i_value = (int)exif_value;
306 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
308 if (d_value == NULL) {
309 media_svc_error("d_value is NULL");
310 return MS_MEDIA_ERR_INVALID_PARAMETER;
313 /* Get the contents of the tag in human-readable form */
314 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
315 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
316 gps_buf[strlen(gps_buf)] = '\0';
317 int ret = MS_MEDIA_ERR_NONE;
319 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
321 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
322 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
324 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
325 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
328 media_svc_error("buf is NULL");
329 return MS_MEDIA_ERR_INVALID_PARAMETER;
332 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
333 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
334 long numerator = mRational.numerator;
335 long denominator = mRational.denominator;
336 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
338 } else if (tag == EXIF_TAG_FNUMBER) {
340 if (d_value == NULL) {
341 media_svc_error("d_value is NULL");
342 return MS_MEDIA_ERR_INVALID_PARAMETER;
345 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
346 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
347 long numerator = mRational.numerator;
348 long denominator = mRational.denominator;
350 *d_value = ((numerator*1.0)/(denominator*1.0));
355 media_svc_error("buf is NULL");
356 return MS_MEDIA_ERR_INVALID_PARAMETER;
359 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
360 buf[strlen(buf)] = '\0';
364 return MS_MEDIA_ERR_NONE;
367 time_t __media_svc_get_timeline_from_str(const char *timstr)
370 time_t modified_t = 0;
374 if (!STRING_VALID(timstr)) {
375 media_svc_error("Invalid Parameter");
379 /*Exif Format : %Y:%m:%d %H:%M:%S
380 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
381 memset(&t, 0x00, sizeof(struct tm));
385 localtime_r(&rawtime, &timeinfo);
387 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
388 t.tm_isdst = timeinfo.tm_isdst;
390 media_svc_error("DST %d", t.tm_isdst);
392 modified_t = mktime(&t);
396 media_svc_error("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);
398 media_svc_error("Failed to get timeline : [%s]", timstr);
404 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
408 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
410 /*categorize from mimetype */
411 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
412 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
413 *category = (*category | content_category[idx].category_by_mime);
418 /*in application type, exitst sound file ex) x-smafs, asf */
419 if (*category & MEDIA_SVC_CATEGORY_ETC) {
420 int prefix_len = strlen(content_category[0].content_type);
423 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
424 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
425 *category ^= MEDIA_SVC_CATEGORY_ETC;
426 *category |= MEDIA_SVC_CATEGORY_SOUND;
431 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
432 *category ^= MEDIA_SVC_CATEGORY_ETC;
433 *category |= MEDIA_SVC_CATEGORY_SOUND;
436 /*"asf" must check video stream and then categorize in directly. */
437 ext = strrchr(path, '.');
439 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
444 err = mm_file_get_stream_info(path, &audio, &video);
446 if (audio > 0 && video == 0) {
447 *category ^= MEDIA_SVC_CATEGORY_ETC;
448 *category |= MEDIA_SVC_CATEGORY_MUSIC;
450 *category ^= MEDIA_SVC_CATEGORY_ETC;
451 *category |= MEDIA_SVC_CATEGORY_VIDEO;
458 /*check music file in soun files. */
459 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
460 int prefix_len = strlen(content_category[0].content_type) + 1;
462 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
463 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
464 *category ^= MEDIA_SVC_CATEGORY_SOUND;
465 *category |= MEDIA_SVC_CATEGORY_MUSIC;
470 /*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*/
471 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
472 *category ^= MEDIA_SVC_CATEGORY_SOUND;
473 *category |= MEDIA_SVC_CATEGORY_ETC;
475 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
476 /*some video files don't have video stream. in this case it is categorize as music. */
478 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
479 ext = strrchr(path, '.');
481 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
486 err = mm_file_get_stream_info(path, &audio, &video);
488 if (audio > 0 && video == 0) {
489 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
490 *category |= MEDIA_SVC_CATEGORY_MUSIC;
493 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
498 return MS_MEDIA_ERR_NONE;
501 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
503 int ret = MS_MEDIA_ERR_NONE;
506 media_svc_media_type_e type;
508 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
509 if (ret != MS_MEDIA_ERR_NONE)
510 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
512 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
513 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
514 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
515 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
516 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
524 drm_contentifo is not NULL, if the file is OMA DRM.
525 If the file is not OMA DRM, drm_contentinfo must be NULL.
527 static int __media_svc_get_mime_type(const char *path, char *mimetype)
530 return MS_MEDIA_ERR_INVALID_PARAMETER;
532 /*in case of normal files or failure to get mime in drm */
533 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
534 media_svc_error("aul_get_mime_from_file fail");
535 return MS_MEDIA_ERR_INTERNAL;
538 return MS_MEDIA_ERR_NONE;
541 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
545 for (i = strlen(file_path); i >= 0; i--) {
546 if (file_path[i] == '.') {
547 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
551 if (file_path[i] == '/')
557 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
559 char *err_attr_name = NULL;
560 double gps_value = 0.0;
561 int mmf_error = FILEINFO_ERROR_NONE;
563 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
564 if (mmf_error == FILEINFO_ERROR_NONE) {
565 if (longitude != NULL)
566 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
568 SAFE_FREE(err_attr_name);
571 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
572 if (mmf_error == FILEINFO_ERROR_NONE) {
573 if (latitude != NULL)
574 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
576 SAFE_FREE(err_attr_name);
579 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
580 if (mmf_error == FILEINFO_ERROR_NONE) {
581 if (altitude != NULL)
582 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
584 SAFE_FREE(err_attr_name);
587 return MS_MEDIA_ERR_NONE;
590 static char *__media_svc_get_thumb_path(uid_t uid)
593 char *result_passwd = NULL;
594 struct group *grpinfo = NULL;
595 if (uid == getuid()) {
596 grpinfo = getgrnam("users");
597 if (grpinfo == NULL) {
598 media_svc_error("getgrnam(users) returns NULL !");
601 len = strlen(MEDIA_SVC_THUMB_PATH_PREFIX);
603 result_passwd = strndup(MEDIA_SVC_THUMB_PATH_PREFIX, len);
605 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
606 struct passwd *userinfo = getpwuid(uid);
607 if (userinfo == NULL) {
608 media_svc_error("getpwuid(%d) returns NULL !", uid);
611 grpinfo = getgrnam("users");
612 if (grpinfo == NULL) {
613 media_svc_error("getgrnam(users) returns NULL !");
616 /* Compare git_t type and not group name */
617 if (grpinfo->gr_gid != userinfo->pw_gid) {
618 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
621 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
623 result_passwd = strndup(passwd_str, len);
626 return result_passwd;
629 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
631 int ret = MS_MEDIA_ERR_NONE;
632 unsigned char *raw_image = NULL;
635 unsigned int raw_size = 0;
636 void *resized_raw_image = NULL;
637 int resized_width = 0;
638 int resized_height = 0;
639 unsigned int buf_size = 0;
641 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
642 media_svc_debug("type [jpeg] size [%d]", size);
644 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
645 if (ret != MS_MEDIA_ERR_NONE) {
646 media_svc_error("image_util_decode_jpeg_from_memory failed");
647 *resize_image = image;
649 return MS_MEDIA_ERR_NONE;
652 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
653 media_svc_debug("No need resizing");
654 *resize_image = image;
656 SAFE_FREE(raw_image);
657 return MS_MEDIA_ERR_NONE;
660 if (width > height) {
661 resized_height = MEDIA_SVC_ARTWORK_SIZE;
662 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
664 resized_width = MEDIA_SVC_ARTWORK_SIZE;
665 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
668 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
670 resized_raw_image = malloc(buf_size);
672 if (resized_raw_image == NULL) {
673 media_svc_error("malloc failed");
674 *resize_image = image;
676 SAFE_FREE(raw_image);
677 return MS_MEDIA_ERR_NONE;
680 memset(resized_raw_image, 0, buf_size);
682 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
683 if (ret != MS_MEDIA_ERR_NONE) {
684 media_svc_error("image_util_resize failed");
685 *resize_image = image;
687 SAFE_FREE(raw_image);
688 SAFE_FREE(resized_raw_image);
689 return MS_MEDIA_ERR_NONE;
691 SAFE_FREE(raw_image);
694 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
695 if (ret != MS_MEDIA_ERR_NONE) {
696 media_svc_error("image_util_encode_jpeg_to_memory failed");
697 *resize_image = image;
699 SAFE_FREE(resized_raw_image);
700 return MS_MEDIA_ERR_NONE;
702 SAFE_FREE(resized_raw_image);
704 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
705 media_svc_debug("type [png] size [%d]", size);
706 *resize_image = image;
710 media_svc_debug("Not proper img format");
711 *resize_image = image;
718 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
720 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
723 media_svc_error("invalid image..");
724 return MS_MEDIA_ERR_INVALID_PARAMETER;
728 char *thumb_path = __media_svc_get_thumb_path(uid);
729 if (!STRING_VALID(thumb_path)) {
730 media_svc_error("fail to get thumb_path");
731 return MS_MEDIA_ERR_INTERNAL;
734 if (-1 == statfs(thumb_path, &fs)) {
735 media_svc_error("error in statfs");
736 SAFE_FREE(thumb_path);
737 return MS_MEDIA_ERR_INTERNAL;
740 SAFE_FREE(thumb_path);
742 long bsize_kbytes = fs.f_bsize >> 10;
744 if ((bsize_kbytes * fs.f_bavail) < 1024) {
745 media_svc_error("not enought space...");
746 return MS_MEDIA_ERR_INTERNAL;
751 if (image != NULL && size > 0) {
752 fp = fopen(image_path, "w");
755 media_svc_error("failed to open file");
756 return MS_MEDIA_ERR_INTERNAL;
759 nwrite = fwrite(image, 1, size, fp);
760 if (nwrite != size) {
761 media_svc_error("failed to write thumbnail");
763 return MS_MEDIA_ERR_INTERNAL;
768 return MS_MEDIA_ERR_NONE;
771 static char *_media_svc_get_title_from_filepath(const char *path)
773 char *filename = NULL;
776 int filename_len = -1;
777 int new_title_len = -1;
780 media_svc_error("path is NULL");
784 filename = g_path_get_basename(path);
785 if (!STRING_VALID(filename)) {
786 media_svc_error("wrong file name");
791 filename_len = strlen(filename);
793 ext = g_strrstr(filename, ".");
795 media_svc_error("there is no file extention");
799 new_title_len = filename_len - strlen(ext);
800 if (new_title_len < 1) {
801 media_svc_error("title length is zero");
806 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
810 media_svc_debug("extract title is [%s]", title);
815 int _media_svc_rename_file(const char *old_name, const char *new_name)
817 if ((old_name == NULL) || (new_name == NULL)) {
818 media_svc_error("invalid file name");
819 return MS_MEDIA_ERR_INVALID_PARAMETER;
822 if (rename(old_name, new_name) < 0) {
823 media_svc_stderror(" ");
824 return MS_MEDIA_ERR_INTERNAL;
827 return MS_MEDIA_ERR_NONE;
830 int _media_svc_remove_file(const char *path)
834 result = remove(path);
836 media_svc_debug("success to remove file");
837 return MS_MEDIA_ERR_NONE;
839 media_svc_stderror("fail to remove file result");
840 return MS_MEDIA_ERR_INTERNAL;
844 int _media_svc_remove_all_files_in_dir(const char *dir_path)
847 struct dirent *result;
849 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
852 dir = opendir(dir_path);
854 media_svc_error("%s is not exist", dir_path);
855 return MS_MEDIA_ERR_INVALID_PARAMETER;
858 while (!readdir_r(dir, &entry, &result)) {
862 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
865 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
867 if (stat(filename, &st) != 0)
870 if (S_ISDIR(st.st_mode))
873 if (unlink(filename) != 0) {
874 media_svc_stderror("failed to remove");
876 return MS_MEDIA_ERR_INTERNAL;
881 return MS_MEDIA_ERR_NONE;
884 char *_media_svc_get_thumb_internal_path(uid_t uid)
887 char *result_passwd = NULL;
888 struct group *grpinfo = NULL;
889 if (uid == getuid()) {
890 grpinfo = getgrnam("users");
891 if (grpinfo == NULL) {
892 media_svc_error("getgrnam(users) returns NULL !");
895 len = strlen(MEDIA_SVC_THUMB_INTERNAL_PATH);
897 result_passwd = strndup(MEDIA_SVC_THUMB_INTERNAL_PATH, len);
899 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
900 struct passwd *userinfo = getpwuid(uid);
901 if (userinfo == NULL) {
902 media_svc_error("getpwuid(%d) returns NULL !", uid);
905 grpinfo = getgrnam("users");
906 if (grpinfo == NULL) {
907 media_svc_error("getgrnam(users) returns NULL !");
910 /* Compare git_t type and not group name */
911 if (grpinfo->gr_gid != userinfo->pw_gid) {
912 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
915 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
917 result_passwd = strndup(passwd_str, len);
920 return result_passwd;
923 char *_media_svc_get_thumb_external_path(uid_t uid)
926 char *result_passwd = NULL;
927 struct group *grpinfo = NULL;
928 if (uid == getuid()) {
929 grpinfo = getgrnam("users");
930 if (grpinfo == NULL) {
931 media_svc_error("getgrnam(users) returns NULL !");
934 len = strlen(MEDIA_SVC_THUMB_EXTERNAL_PATH);
936 result_passwd = strndup(MEDIA_SVC_THUMB_EXTERNAL_PATH, len);
938 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
939 struct passwd *userinfo = getpwuid(uid);
940 if (userinfo == NULL) {
941 media_svc_error("getpwuid(%d) returns NULL !", uid);
944 grpinfo = getgrnam("users");
945 if (grpinfo == NULL) {
946 media_svc_error("getgrnam(users) returns NULL !");
949 /* Compare git_t type and not group name */
950 if (grpinfo->gr_gid != userinfo->pw_gid) {
951 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
954 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
956 result_passwd = strndup(passwd_str, len);
959 return result_passwd;
962 static int __media_svc_check_thumb_dir(const char *thumb_dir)
967 dir = opendir(thumb_dir);
971 media_svc_stderror("opendir fail");
972 if (errno == ENOENT) {
973 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
974 ret = mkdir(thumb_dir, 0777);
976 media_svc_error("make fail");
983 ret = chmod(thumb_dir, 0777);
985 media_svc_stderror("chmod failed");
987 ret = chown(thumb_dir, 5000, 5000);
989 media_svc_stderror("chown failed");
992 return MS_MEDIA_ERR_NONE;
998 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)
1000 int ret = MS_MEDIA_ERR_NONE;
1001 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1002 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1003 char *thumb_dir = NULL;
1004 char hash[255 + 1] = {0, };
1005 char *thumbfile_ext = NULL;
1006 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1007 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1009 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1010 media_svc_error("fail to get thumbnail path");
1011 SAFE_FREE(internal_thumb_path);
1012 SAFE_FREE(external_thumb_path);
1013 return MS_MEDIA_ERR_INTERNAL;
1016 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1018 ret = __media_svc_check_thumb_dir(thumb_dir);
1019 if (ret != MS_MEDIA_ERR_NONE) {
1020 media_svc_error("__media_svc_check_thumb_dir");
1021 SAFE_FREE(internal_thumb_path);
1022 SAFE_FREE(external_thumb_path);
1023 return MS_MEDIA_ERR_INTERNAL;
1026 memset(file_ext, 0, sizeof(file_ext));
1027 if (!__media_svc_get_file_ext(pathname, file_ext))
1028 media_svc_error("get file ext fail");
1030 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1031 if (ret != MS_MEDIA_ERR_NONE) {
1032 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1033 SAFE_FREE(internal_thumb_path);
1034 SAFE_FREE(external_thumb_path);
1035 return MS_MEDIA_ERR_INTERNAL;
1038 /*media_svc_debug("img format is [%s]", img_format); */
1040 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1041 thumbfile_ext = (char *)"jpg";
1042 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1043 thumbfile_ext = (char *)"png";
1044 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1045 thumbfile_ext = (char *)"gif";
1046 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1047 thumbfile_ext = (char *)"bmp";
1049 media_svc_error("Not proper img format");
1050 SAFE_FREE(internal_thumb_path);
1051 SAFE_FREE(external_thumb_path);
1052 return MS_MEDIA_ERR_INTERNAL;
1055 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1056 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1057 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1059 SAFE_FREE(internal_thumb_path);
1060 SAFE_FREE(external_thumb_path);
1062 return MS_MEDIA_ERR_NONE;
1065 int _media_svc_get_file_time(const char *full_path)
1067 struct stat statbuf;
1070 memset(&statbuf, 0, sizeof(struct stat));
1071 fd = stat(full_path, &statbuf);
1073 media_svc_error("stat(%s) fails.", full_path);
1074 return MS_MEDIA_ERR_INTERNAL;
1077 return statbuf.st_mtime;
1080 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1082 int ret = MS_MEDIA_ERR_NONE;
1084 /* Set default GPS value before extracting meta information */
1085 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1086 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1087 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1089 /* Set filename to title for all media */
1091 title = _media_svc_get_title_from_filepath(content_info->path);
1093 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1094 if (ret != MS_MEDIA_ERR_NONE)
1095 media_svc_error("strcpy error");
1098 media_svc_error("Can't extract title");
1099 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1100 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1103 /* Set default value before extracting meta information */
1104 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1105 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1107 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1108 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1110 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1111 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1113 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1114 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1116 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1117 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1119 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1120 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1122 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1123 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1125 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1126 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1128 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1129 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1132 media_svc_debug("refresh");
1133 return MS_MEDIA_ERR_NONE;
1136 content_info->played_count = 0;
1137 content_info->last_played_time = 0;
1138 content_info->last_played_position = 0;
1139 content_info->favourate = 0;
1140 content_info->media_meta.rating = 0;
1142 return MS_MEDIA_ERR_NONE;
1145 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1146 const char *path, media_svc_media_type_e *media_type, bool refresh)
1148 int ret = MS_MEDIA_ERR_NONE;
1149 char * media_uuid = NULL;
1150 char * file_name = NULL;
1151 bool drm_type = false;
1152 char mime_type[256] = {0, };
1154 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1155 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1157 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1159 memset(&st, 0, sizeof(struct stat));
1160 if (stat(path, &st) == 0) {
1161 content_info->modified_time = st.st_mtime;
1162 content_info->timeline = content_info->modified_time;
1163 content_info->size = st.st_size;
1164 /* media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size); */
1166 media_svc_stderror("stat failed");
1170 _media_svc_set_default_value(content_info, refresh);
1172 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1174 media_svc_debug("refresh");
1175 return MS_MEDIA_ERR_NONE;
1178 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1179 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1181 content_info->storage_type = storage_type;
1182 time(&content_info->added_time);
1184 media_uuid = _media_info_generate_uuid();
1185 if (media_uuid == NULL) {
1186 _media_svc_destroy_content_info(content_info);
1187 return MS_MEDIA_ERR_INTERNAL;
1190 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1191 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1193 file_name = g_path_get_basename(path);
1194 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1195 SAFE_FREE(file_name);
1196 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1198 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1199 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1200 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1201 ret = __media_svc_get_mime_type(path, mime_type);
1202 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1204 media_svc_debug("mime [%s]", mime_type);
1205 content_info->is_drm = drm_type;
1207 ret = __media_svc_get_media_type(path, mime_type, media_type);
1208 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1210 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1211 media_svc_error("invalid media_type condition[%d]", *media_type);
1212 return MS_MEDIA_ERR_INVALID_PARAMETER;
1215 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1216 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1218 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1220 content_info->media_type = *media_type;
1223 return MS_MEDIA_ERR_NONE;
1226 int image_360_check(char *path)
1231 unsigned char exif_header[4];
1232 unsigned char exif_app1[2];
1233 unsigned char exif_app1_xmp[2];
1234 long exif_app1_xmp_size = 0;
1235 unsigned char exif_app1_xmp_t[2];
1242 fp = fopen(path, "rb");
1246 size = fread(exif_header, 1, sizeof(exif_header), fp);
1250 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1251 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1255 size1 = exif_app1[0];
1256 size2 = exif_app1[1];
1258 app1_size = size1 * 256 + size2 - 2;
1260 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1263 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1267 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1270 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1274 size1 = exif_app1_xmp_t[0];
1275 size2 = exif_app1_xmp_t[1];
1277 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1279 xmp_data = (char *)malloc(exif_app1_xmp_size);
1280 memset(xmp_data, 0x00, exif_app1_xmp_size);
1282 while (exif_app1_xmp_size >= 0) {
1283 exif_app1_xmp_size--;
1293 if (strstr(ptr, "UsePanoramaViewer")
1294 && strstr(ptr, "True")
1295 && strstr(ptr, "ProjectionType")
1296 && strstr(ptr, "equirectangular"))
1299 SAFE_FREE(xmp_data);
1319 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1321 int ret = MS_MEDIA_ERR_NONE;
1323 int orient_value = 0;
1325 int exif_height = 0;
1326 ExifData *ed = NULL;
1327 int has_datetaken = FALSE;
1328 int datetaken_size = 19;
1329 double fnumber = 0.0;
1333 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1334 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1335 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1336 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1338 memset(buf, 0x00, sizeof(buf));
1339 memset(description_buf, 0x00, sizeof(description_buf));
1340 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1341 memset(model_buf, 0x00, sizeof(model_buf));
1343 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1344 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1345 return MS_MEDIA_ERR_INVALID_PARAMETER;
1348 path = content_info->path;
1349 if (!STRING_VALID(path)) {
1350 media_svc_error("Invalid Path");
1351 return MS_MEDIA_ERR_INVALID_PARAMETER;
1354 /* Load an ExifData object from an EXIF file */
1355 ed = exif_data_new_from_file(path);
1358 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1359 goto GET_WIDTH_HEIGHT;
1362 content_info->media_meta.is_360 = image_360_check(path);
1364 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1365 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1366 if (strlen(buf) > 0) {
1367 if (strcmp(buf, "S") == 0)
1370 content_info->media_meta.latitude = value;
1372 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1375 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1378 memset(buf, 0x00, sizeof(buf));
1380 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1381 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1382 if (strlen(buf) > 0) {
1383 if (strcmp(buf, "W") == 0)
1386 content_info->media_meta.longitude = value;
1388 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1391 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1394 memset(buf, 0x00, sizeof(buf));
1396 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1397 if (strlen(description_buf) == 0) {
1398 /*media_svc_debug("Use 'No description'"); */
1399 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1400 if (ret != MS_MEDIA_ERR_NONE)
1401 media_svc_error("strcpy error");
1403 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1404 if (ret != MS_MEDIA_ERR_NONE)
1405 media_svc_error("strcpy error");
1408 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1409 if (ret != MS_MEDIA_ERR_NONE)
1410 media_svc_error("strcpy error");
1413 memset(buf, 0x00, sizeof(buf));
1415 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1416 if (strlen(buf) == 0) {
1417 /*media_svc_debug("time is NULL"); */
1419 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1420 if (ret != MS_MEDIA_ERR_NONE) {
1421 media_svc_error("strcpy error");
1423 has_datetaken = TRUE;
1424 /* This is same as recorded_date */
1425 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1426 if (ret != MS_MEDIA_ERR_NONE)
1427 media_svc_error("strcpy error");
1432 memset(buf, 0x00, sizeof(buf));
1434 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1435 if (strlen(buf) == 0) {
1436 /*media_svc_debug("time is NULL"); */
1438 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1439 if (ret != MS_MEDIA_ERR_NONE) {
1440 media_svc_error("strcpy error");
1442 has_datetaken = TRUE;
1443 /* This is same as recorded_date */
1444 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1445 if (ret != MS_MEDIA_ERR_NONE)
1446 media_svc_error("strcpy error");
1451 if (has_datetaken) {
1452 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1453 if (content_info->timeline == 0)
1454 content_info->timeline = content_info->modified_time;
1456 media_svc_debug("Timeline : %ld", content_info->timeline);
1459 /* Get exposure_time value from exif. */
1460 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1461 if (strlen(exposure_time_buf) == 0) {
1462 /* media_svc_debug("exposure_time_buf is NULL"); */
1464 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1465 if (ret != MS_MEDIA_ERR_NONE)
1466 media_svc_error("strcpy error");
1470 /* Get fnumber value from exif. */
1471 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1473 content_info->media_meta.fnumber = fnumber;
1475 content_info->media_meta.fnumber = 0.0;
1477 content_info->media_meta.fnumber = 0.0;
1480 /* Get iso value from exif. */
1481 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1483 content_info->media_meta.iso = iso;
1485 content_info->media_meta.iso = 0;
1487 content_info->media_meta.iso = 0;
1490 /* Get model value from exif. */
1491 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1492 if (strlen(model_buf) == 0) {
1493 /* media_svc_debug("model_buf is NULL"); */
1495 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1496 if (ret != MS_MEDIA_ERR_NONE)
1497 media_svc_error("strcpy error");
1501 /* Get orientation value from exif. */
1502 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1503 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1504 content_info->media_meta.orientation = orient_value;
1506 content_info->media_meta.orientation = 0;
1508 content_info->media_meta.orientation = 0;
1511 /* Get width value from exif. */
1512 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1514 content_info->media_meta.width = exif_width;
1516 content_info->media_meta.width = 0;
1518 content_info->media_meta.width = 0;
1521 /* Get height value from exif. */
1522 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1523 if (exif_height > 0)
1524 content_info->media_meta.height = exif_height;
1526 content_info->media_meta.height = 0;
1528 content_info->media_meta.height = 0;
1531 if (ed != NULL) exif_data_unref(ed);
1535 if (content_info->media_meta.width == 0 ||
1536 content_info->media_meta.height == 0) {
1537 /*Get image width, height*/
1538 unsigned int img_width = 0;
1539 unsigned int img_height = 0;
1540 ImgCodecType img_type = IMG_CODEC_NONE;
1542 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1544 if (content_info->media_meta.width == 0)
1545 content_info->media_meta.width = img_width;
1547 if (content_info->media_meta.height == 0)
1548 content_info->media_meta.height = img_height;
1551 return MS_MEDIA_ERR_NONE;
1554 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1556 MMHandleType tag = 0;
1559 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1560 int mmf_error = FILEINFO_ERROR_NONE;
1561 char *err_attr_name = NULL;
1564 int ret = MS_MEDIA_ERR_NONE;
1566 /*Get Content Tag attribute ===========*/
1567 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1569 if (mmf_error == FILEINFO_ERROR_NONE) {
1570 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1571 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1572 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1573 if (ret != MS_MEDIA_ERR_NONE)
1574 media_svc_error("strcpy error");
1576 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1578 SAFE_FREE(err_attr_name);
1579 /*media_svc_debug("album - unknown"); */
1582 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1583 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1584 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1585 if (ret != MS_MEDIA_ERR_NONE)
1586 media_svc_error("strcpy error");
1587 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1589 SAFE_FREE(err_attr_name);
1590 /*media_svc_debug("artist - unknown"); */
1593 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1594 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1595 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1596 if (ret != MS_MEDIA_ERR_NONE)
1597 media_svc_error("strcpy error");
1598 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1600 SAFE_FREE(err_attr_name);
1601 /*media_svc_debug("album_artist - unknown"); */
1604 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1605 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1606 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1607 if (ret != MS_MEDIA_ERR_NONE)
1608 media_svc_error("strcpy error");
1610 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1611 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1613 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1614 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1618 SAFE_FREE(err_attr_name);
1619 /*media_svc_debug("genre - unknown"); */
1622 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1623 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1625 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1626 if (ret != MS_MEDIA_ERR_NONE)
1627 media_svc_error("strcpy error");
1629 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1633 for (idx = 0; idx < size; idx++) {
1635 media_svc_debug("SPACE [%s]", p);
1639 media_svc_debug("Not SPACE [%s]", p);
1640 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1641 if (ret != MS_MEDIA_ERR_NONE)
1642 media_svc_error("strcpy error");
1648 media_svc_debug("Can't extract title. All string is space");
1649 title = _media_svc_get_title_from_filepath(content_info->path);
1651 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1652 if (ret != MS_MEDIA_ERR_NONE)
1653 media_svc_error("strcpy error");
1656 media_svc_error("Can't extract title");
1661 SAFE_FREE(err_attr_name);
1662 title = _media_svc_get_title_from_filepath(content_info->path);
1664 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1665 if (ret != MS_MEDIA_ERR_NONE)
1666 media_svc_error("strcpy error");
1669 media_svc_error("Can't extract title");
1673 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1674 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1675 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1676 if (ret != MS_MEDIA_ERR_NONE)
1677 media_svc_error("strcpy error");
1678 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1680 SAFE_FREE(err_attr_name);
1683 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1684 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1685 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1686 if (ret != MS_MEDIA_ERR_NONE)
1687 media_svc_error("strcpy error");
1688 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1689 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1691 /*media_svc_debug("composer - unknown"); */
1692 SAFE_FREE(err_attr_name);
1695 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1696 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1697 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1698 if (ret != MS_MEDIA_ERR_NONE)
1699 media_svc_error("strcpy error");
1700 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1701 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1703 /*media_svc_debug("copyright - unknown"); */
1704 SAFE_FREE(err_attr_name);
1707 mmf_error = mm_file_destroy_tag_attrs(tag);
1708 if (mmf_error != FILEINFO_ERROR_NONE)
1709 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1711 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1712 char *no_tag_title = NULL;
1713 media_svc_error("no tag information");
1715 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1717 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1718 if (ret != MS_MEDIA_ERR_NONE)
1719 media_svc_error("strcpy error");
1720 SAFE_FREE(no_tag_title);
1722 media_svc_error("Can't extract title");
1725 content_info->album_id = album_id;
1728 return MS_MEDIA_ERR_NONE;
1731 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1733 MMHandleType content = 0;
1734 MMHandleType tag = 0;
1736 unsigned char *image = NULL;
1737 unsigned int size = 0;
1738 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1739 int mmf_error = FILEINFO_ERROR_NONE;
1740 char *err_attr_name = NULL;
1742 bool extract_thumbnail = FALSE;
1743 bool append_album = FALSE;
1745 int ret = MS_MEDIA_ERR_NONE;
1747 unsigned int resize_size = 0;
1748 unsigned char *resize_image = NULL;
1750 /*Get Content Tag attribute ===========*/
1751 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1753 if (mmf_error == FILEINFO_ERROR_NONE) {
1754 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1755 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1756 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1757 if (ret != MS_MEDIA_ERR_NONE)
1758 media_svc_error("strcpy error");
1760 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1762 SAFE_FREE(err_attr_name);
1763 /*media_svc_debug("album - unknown"); */
1766 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1767 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1768 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1769 if (ret != MS_MEDIA_ERR_NONE)
1770 media_svc_error("strcpy error");
1771 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1773 SAFE_FREE(err_attr_name);
1774 /*media_svc_debug("artist - unknown"); */
1777 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1778 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1779 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1780 if (ret != MS_MEDIA_ERR_NONE)
1781 media_svc_error("strcpy error");
1782 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1784 SAFE_FREE(err_attr_name);
1785 /*media_svc_debug("album_artist - unknown"); */
1788 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1789 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1790 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1791 if (ret != MS_MEDIA_ERR_NONE)
1792 media_svc_error("strcpy error");
1794 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1795 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1797 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1798 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1802 SAFE_FREE(err_attr_name);
1803 /*media_svc_debug("genre - unknown"); */
1806 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1807 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1809 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1810 if (ret != MS_MEDIA_ERR_NONE)
1811 media_svc_error("strcpy error");
1813 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1817 for (idx = 0; idx < size; idx++) {
1819 media_svc_debug("SPACE [%s]", p);
1823 media_svc_debug("Not SPACE [%s]", p);
1824 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1825 if (ret != MS_MEDIA_ERR_NONE)
1826 media_svc_error("strcpy error");
1832 media_svc_debug("Can't extract title. All string is space");
1833 title = _media_svc_get_title_from_filepath(content_info->path);
1835 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1836 if (ret != MS_MEDIA_ERR_NONE)
1837 media_svc_error("strcpy error");
1840 media_svc_error("Can't extract title");
1845 SAFE_FREE(err_attr_name);
1846 title = _media_svc_get_title_from_filepath(content_info->path);
1848 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1849 if (ret != MS_MEDIA_ERR_NONE)
1850 media_svc_error("strcpy error");
1853 media_svc_error("Can't extract title");
1857 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1858 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1859 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1860 if (ret != MS_MEDIA_ERR_NONE)
1861 media_svc_error("strcpy error");
1862 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1864 SAFE_FREE(err_attr_name);
1867 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1869 if (mmf_error != FILEINFO_ERROR_NONE)
1870 SAFE_FREE(err_attr_name);
1872 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1873 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1874 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1875 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1876 char time_info[64] = {0, };
1877 char p_value[64] = {0, };
1879 memset(time_info, 0x00, sizeof(time_info));
1880 memset(p_value, 0x00, sizeof(p_value));
1881 strncpy(p_value, p, size);
1882 for (idx = 0; idx < size; idx++) {
1883 if (p_value[idx] == '-') {
1884 time_info[idx] = ':';
1885 } else if (p_value[idx] != '\0') {
1886 time_info[idx] = p_value[idx];
1888 media_svc_error("strcpy error");
1892 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1894 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1897 if (ret != MS_MEDIA_ERR_NONE) {
1898 media_svc_error("strcpy error");
1900 /* This is same as datetaken */
1902 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1904 int datetaken_size = 19;
1905 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1907 if (ret != MS_MEDIA_ERR_NONE)
1908 media_svc_error("strcpy error");
1910 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1911 if (content_info->timeline == 0)
1912 content_info->timeline = content_info->modified_time;
1914 media_svc_debug("Timeline : %ld", content_info->timeline);
1916 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1918 SAFE_FREE(err_attr_name);
1921 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1922 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1923 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1924 if (ret != MS_MEDIA_ERR_NONE)
1925 media_svc_error("strcpy error");
1926 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1927 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1929 /*media_svc_debug("composer - unknown"); */
1930 SAFE_FREE(err_attr_name);
1933 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1934 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1935 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1936 if (ret != MS_MEDIA_ERR_NONE)
1937 media_svc_error("strcpy error");
1938 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1939 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1941 /*media_svc_debug("copyright - unknown"); */
1942 SAFE_FREE(err_attr_name);
1945 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1946 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1947 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1948 if (ret != MS_MEDIA_ERR_NONE)
1949 media_svc_error("strcpy error");
1951 SAFE_FREE(err_attr_name);
1954 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1955 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1957 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1958 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1959 if (ret != MS_MEDIA_ERR_NONE)
1960 media_svc_error("strcpy error");
1962 media_svc_debug("Wrong Year Information [%s]", p);
1965 SAFE_FREE(err_attr_name);
1968 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1969 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1970 content_info->media_meta.rating = atoi(p);
1972 SAFE_FREE(err_attr_name);
1973 content_info->media_meta.rating = 0;
1976 /*Initialize album_id to 0. below code will set the album_id*/
1977 content_info->album_id = album_id;
1978 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1980 if (ret != MS_MEDIA_ERR_NONE) {
1981 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1982 media_svc_debug("album does not exist. So start to make album art");
1983 extract_thumbnail = TRUE;
1984 append_album = TRUE;
1986 extract_thumbnail = TRUE;
1987 append_album = FALSE;
1990 content_info->album_id = album_id;
1991 append_album = FALSE;
1993 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1994 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1996 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1997 extract_thumbnail = TRUE;
1999 media_svc_debug("album already exists. don't need to make album art");
2000 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2001 extract_thumbnail = TRUE;
2005 /*Do not extract artwork for the USB Storage content*/
2006 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2007 extract_thumbnail = FALSE;
2009 if (extract_thumbnail == TRUE) {
2010 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2011 if (mmf_error != FILEINFO_ERROR_NONE) {
2012 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2013 SAFE_FREE(err_attr_name);
2015 /*media_svc_debug("artwork size1 [%d]", size); */
2018 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2019 if (mmf_error != FILEINFO_ERROR_NONE) {
2020 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2021 SAFE_FREE(err_attr_name);
2023 /*media_svc_debug("artwork size2 [%d]", size); */
2025 if (image != NULL && size > 0) {
2026 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2027 int artwork_mime_size = -1;
2029 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2030 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2031 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2032 if (ret != MS_MEDIA_ERR_NONE)
2033 media_svc_error("Fail to Get Thumbnail Path");
2034 /* albumart resizing */
2035 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2037 SAFE_FREE(err_attr_name);
2040 if (strlen(thumb_path) > 0) {
2041 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2042 if (ret != MS_MEDIA_ERR_NONE) {
2043 media_svc_error("Fail to Save Thumbnail Image");
2045 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2046 if (ret != MS_MEDIA_ERR_NONE)
2047 media_svc_error("strcpy error");
2053 if (append_album == TRUE) {
2054 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2055 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2056 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2058 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2060 content_info->album_id = album_id;
2063 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2064 double longitude = 0.0;
2065 double latitude = 0.0;
2066 double altitude = 0.0;
2068 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2069 content_info->media_meta.longitude = longitude;
2070 content_info->media_meta.latitude = latitude;
2071 content_info->media_meta.altitude = altitude;
2073 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2074 if (mmf_error != FILEINFO_ERROR_NONE) {
2076 SAFE_FREE(err_attr_name);
2079 media_svc_debug("CDIS : %d", cdis_value);
2082 mmf_error = mm_file_destroy_tag_attrs(tag);
2083 if (mmf_error != FILEINFO_ERROR_NONE)
2084 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2086 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2087 char *no_tag_title = NULL;
2088 media_svc_error("no tag information");
2090 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2092 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2093 if (ret != MS_MEDIA_ERR_NONE)
2094 media_svc_error("strcpy error");
2095 SAFE_FREE(no_tag_title);
2097 media_svc_error("Can't extract title");
2100 content_info->album_id = album_id;
2103 /*Get Content attribute ===========*/
2104 if (cdis_value == 1)
2105 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2107 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2109 if (mmf_error == FILEINFO_ERROR_NONE) {
2110 /*Common attribute*/
2111 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2112 if (mmf_error != FILEINFO_ERROR_NONE) {
2113 SAFE_FREE(err_attr_name);
2114 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2116 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2119 /*Sound/Music attribute*/
2120 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2122 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2123 if (mmf_error != FILEINFO_ERROR_NONE) {
2124 SAFE_FREE(err_attr_name);
2125 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2127 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2130 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2131 if (mmf_error != FILEINFO_ERROR_NONE) {
2132 SAFE_FREE(err_attr_name);
2133 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2135 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2138 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2139 if (mmf_error != FILEINFO_ERROR_NONE) {
2140 SAFE_FREE(err_attr_name);
2141 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2143 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2146 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2147 if (mmf_error != FILEINFO_ERROR_NONE) {
2148 SAFE_FREE(err_attr_name);
2149 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2151 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2153 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2154 int audio_bitrate = 0;
2155 int video_bitrate = 0;
2157 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2158 if (mmf_error != FILEINFO_ERROR_NONE) {
2159 SAFE_FREE(err_attr_name);
2160 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2162 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2165 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2166 if (mmf_error != FILEINFO_ERROR_NONE) {
2167 SAFE_FREE(err_attr_name);
2168 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2170 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2173 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2175 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2176 if (mmf_error != FILEINFO_ERROR_NONE) {
2177 SAFE_FREE(err_attr_name);
2178 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2180 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2183 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2184 if (mmf_error != FILEINFO_ERROR_NONE) {
2185 SAFE_FREE(err_attr_name);
2186 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2188 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2192 media_svc_error("Not support type");
2193 mmf_error = mm_file_destroy_content_attrs(content);
2194 if (mmf_error != FILEINFO_ERROR_NONE)
2195 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2197 return MS_MEDIA_ERR_INVALID_PARAMETER;
2200 mmf_error = mm_file_destroy_content_attrs(content);
2201 if (mmf_error != FILEINFO_ERROR_NONE)
2202 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2204 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2207 return MS_MEDIA_ERR_NONE;
2210 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2212 media_svc_retm_if(content_info == NULL, "content info is NULL");
2214 /* Delete media_svc_content_info_s */
2215 SAFE_FREE(content_info->media_uuid);
2216 SAFE_FREE(content_info->path);
2217 SAFE_FREE(content_info->file_name);
2218 SAFE_FREE(content_info->mime_type);
2219 SAFE_FREE(content_info->folder_uuid);
2220 SAFE_FREE(content_info->thumbnail_path);
2221 SAFE_FREE(content_info->storage_uuid);
2223 /* Delete media_svc_content_meta_s */
2224 SAFE_FREE(content_info->media_meta.title);
2225 SAFE_FREE(content_info->media_meta.album);
2226 SAFE_FREE(content_info->media_meta.artist);
2227 SAFE_FREE(content_info->media_meta.album_artist);
2228 SAFE_FREE(content_info->media_meta.genre);
2229 SAFE_FREE(content_info->media_meta.composer);
2230 SAFE_FREE(content_info->media_meta.year);
2231 SAFE_FREE(content_info->media_meta.recorded_date);
2232 SAFE_FREE(content_info->media_meta.copyright);
2233 SAFE_FREE(content_info->media_meta.track_num);
2234 SAFE_FREE(content_info->media_meta.description);
2235 SAFE_FREE(content_info->media_meta.datetaken);
2236 SAFE_FREE(content_info->media_meta.exposure_time);
2237 SAFE_FREE(content_info->media_meta.model);
2238 SAFE_FREE(content_info->media_meta.weather);
2239 SAFE_FREE(content_info->media_meta.category);
2240 SAFE_FREE(content_info->media_meta.keyword);
2241 SAFE_FREE(content_info->media_meta.location_tag);
2242 SAFE_FREE(content_info->media_meta.content_name);
2243 SAFE_FREE(content_info->media_meta.age_rating);
2244 SAFE_FREE(content_info->media_meta.author);
2245 SAFE_FREE(content_info->media_meta.provider);
2247 SAFE_FREE(content_info->media_meta.title_pinyin);
2248 SAFE_FREE(content_info->media_meta.album_pinyin);
2249 SAFE_FREE(content_info->media_meta.artist_pinyin);
2250 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2251 SAFE_FREE(content_info->media_meta.genre_pinyin);
2252 SAFE_FREE(content_info->media_meta.composer_pinyin);
2253 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2254 SAFE_FREE(content_info->media_meta.description_pinyin);
2259 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2261 if (STRING_VALID(path)) {
2262 char *internal_path = _media_svc_get_path(uid);
2263 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2264 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2265 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2266 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2267 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2268 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2270 media_svc_error("Invalid Path");
2271 SAFE_FREE(internal_path);
2272 return MS_MEDIA_ERR_INVALID_PARAMETER;
2274 SAFE_FREE(internal_path);
2276 media_svc_error("INVALID parameter");
2277 return MS_MEDIA_ERR_INVALID_PARAMETER;
2280 return MS_MEDIA_ERR_NONE;
2283 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2286 size_t i, count = 0;
2287 size_t oldlen = strlen(olds);
2288 if (oldlen < 1) return s;
2289 size_t newlen = strlen(news);
2291 if (newlen != oldlen) {
2292 for (i = 0; s[i] != '\0';) {
2293 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2296 } else i = strlen(s);
2299 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2300 if (result == NULL) return NULL;
2304 if (memcmp(s, olds, oldlen) == 0) {
2305 memcpy(sr, news, newlen);
2308 } else *sr++ = *s++;
2316 bool _media_svc_is_drm_file(const char *path)
2321 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2323 int ret = MS_MEDIA_ERR_NONE;
2325 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2327 if (ret != MS_MEDIA_ERR_NONE) {
2328 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2329 ret = MS_MEDIA_ERR_INTERNAL;
2331 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2337 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2339 int ret = MS_MEDIA_ERR_NONE;
2341 pinyin_name_s *pinyinname = NULL;
2345 if (!STRING_VALID(src_str)) {
2346 media_svc_debug("String is invalid");
2350 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2351 if (ret == MS_MEDIA_ERR_NONE) {
2352 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2353 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2355 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
2358 _media_svc_pinyin_free(pinyinname, size);
2363 bool _media_svc_check_pinyin_support(void)
2369 int _media_svc_get_ini_value()
2371 if (g_ini_value == -1) {
2372 dictionary *dict = NULL;
2374 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2376 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2380 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2381 iniparser_freedict(dict);
2383 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2388 char* _media_svc_get_title_from_path(const char *path)
2390 char *filename = NULL;
2393 int filename_len = -1;
2394 int new_title_len = -1;
2397 media_svc_error("path is NULL");
2401 filename = g_path_get_basename(path);
2402 if (!STRING_VALID(filename)) {
2403 media_svc_error("wrong file name");
2404 SAFE_FREE(filename);
2408 filename_len = strlen(filename);
2410 ext = g_strrstr(filename, ".");
2412 media_svc_error("there is no file extention");
2416 new_title_len = filename_len - strlen(ext);
2417 if (new_title_len < 1) {
2418 media_svc_error("title length is zero");
2419 SAFE_FREE(filename);
2423 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2425 SAFE_FREE(filename);
2427 media_svc_debug("extract title is [%s]", title);
2432 #define BUF_LENGHT 256
2434 void _media_svc_print_stderror(void)
2436 char buf[BUF_LENGHT] = {0,};
2438 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));