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 */
70 #define CONTENT_TYPE_NUM 5
71 #define MUSIC_MIME_NUM 29
72 #define SOUND_MIME_NUM 1
73 #define MIME_TYPE_LENGTH 255
74 #define MIME_LENGTH 50
75 #define _3GP_FILE ".3gp"
76 #define _MP4_FILE ".mp4"
77 #define _ASF_FILE ".asf"
78 #define MEDIA_SVC_INI_GET_INT(dict, key, value, default) \
80 value = iniparser_getint(dict, key, default); \
81 media_svc_debug("get %s = %d", key, value); \
83 #define MEDIA_SVC_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
84 #define MEDIA_SVC_ARTWORK_SIZE 2000
86 static int g_ini_value = -1;
89 char content_type[15];
91 } _media_svc_content_table_s;
93 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
94 {"audio", MEDIA_SVC_CATEGORY_SOUND},
95 {"image", MEDIA_SVC_CATEGORY_IMAGE},
96 {"video", MEDIA_SVC_CATEGORY_VIDEO},
97 {"application", MEDIA_SVC_CATEGORY_ETC},
98 {"text", MEDIA_SVC_CATEGORY_ETC},
101 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
102 /*known mime types of normal files*/
108 /* known mime types of drm files*/
110 "x-mp3", /*alias of audio/mpeg*/
111 "x-mpeg", /*alias of audio/mpeg*/
113 "x-ogg", /*alias of audio/ogg*/
114 "vnd.ms-playready.media.pya:*.pya", /*playready*/
117 "x-m4a", /*alias of audio/mp4*/
118 /* below mimes are rare*/
128 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
129 "vorbis", /*alias of audio/x-vorbis+ogg*/
131 "x-mp2", /*alias of audio/mp2*/
132 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
133 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
137 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
142 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
143 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
144 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
145 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
146 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
147 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
148 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
149 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
150 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
151 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
152 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
153 MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
154 } media_svc_extracted_field_e;
156 char *_media_info_generate_uuid(void)
159 static char uuid_unparsed[37];
162 uuid_generate(uuid_value);
163 uuid_unparse(uuid_value, uuid_unparsed);
165 if (strlen(uuid_unparsed) < 36) {
166 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
170 return uuid_unparsed;
173 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
175 if (!x_src || strlen(x_src) == 0) {
176 media_svc_error("x_src is NULL");
181 media_svc_error("length is Wrong");
185 strncpy(x_dst, x_src, max_len - 1);
186 x_dst[max_len - 1] = '\0';
189 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
193 if (!STRING_VALID(src)) {
194 media_svc_error("invalid src");
195 return MS_MEDIA_ERR_INVALID_PARAMETER;
200 len = strlen(src) + 1;
204 media_svc_error("malloc failed");
205 return MS_MEDIA_ERR_INTERNAL;
208 strncpy(*dst, src, len);
212 return MS_MEDIA_ERR_NONE;
215 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
217 if (!STRING_VALID(src)) {
218 media_svc_error("invalid src");
219 return MS_MEDIA_ERR_INVALID_PARAMETER;
224 *dst = malloc(copysize + 1);
227 media_svc_error("malloc failed");
228 return MS_MEDIA_ERR_INTERNAL;
231 strncpy(*dst, src, copysize);
235 return MS_MEDIA_ERR_NONE;
238 static int __media_svc_split_to_double(char *input, double *arr)
240 char tmp_arr[255] = {0, };
241 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
243 if (!STRING_VALID(input)) {
244 media_svc_error("Invalid parameter");
245 return MS_MEDIA_ERR_INVALID_PARAMETER;
247 memset(tmp_arr, 0x0, sizeof(tmp_arr));
249 /*media_svc_debug("input: [%s]", input); */
253 for (idx = 0; idx < (len + 1); idx++) {
254 if (input[idx] == ' ') {
256 } else if ((input[idx] == ',') || (idx == len)) {
257 arr[arr_idx] = atof(tmp_arr);
260 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
261 memset(tmp_arr, 0x0, sizeof(tmp_arr));
263 tmp_arr[str_idx] = input[idx];
269 media_svc_error("Error when parsing GPS [%d]", arr_idx);
270 return MS_MEDIA_ERR_INTERNAL;
273 return MS_MEDIA_ERR_NONE;
276 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
282 return MS_MEDIA_ERR_INVALID_PARAMETER;
287 entry = exif_data_get_entry(ed, tag);
289 /* Get the contents of the tag in human-readable form */
290 if (tag == EXIF_TAG_ORIENTATION ||
291 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
292 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
293 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
295 if (i_value == NULL) {
296 media_svc_error("i_value is NULL");
297 return MS_MEDIA_ERR_INVALID_PARAMETER;
300 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
301 short exif_value = exif_get_short(entry->data, mByteOrder);
302 *i_value = (int)exif_value;
304 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
306 if (d_value == NULL) {
307 media_svc_error("d_value is NULL");
308 return MS_MEDIA_ERR_INVALID_PARAMETER;
311 /* Get the contents of the tag in human-readable form */
312 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
313 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
314 gps_buf[strlen(gps_buf)] = '\0';
315 int ret = MS_MEDIA_ERR_NONE;
317 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
319 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
320 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
322 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
323 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
326 media_svc_error("buf is NULL");
327 return MS_MEDIA_ERR_INVALID_PARAMETER;
330 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
331 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
332 long numerator = mRational.numerator;
333 long denominator = mRational.denominator;
334 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
336 } else if (tag == EXIF_TAG_FNUMBER) {
338 if (d_value == NULL) {
339 media_svc_error("d_value is NULL");
340 return MS_MEDIA_ERR_INVALID_PARAMETER;
343 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
344 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
345 long numerator = mRational.numerator;
346 long denominator = mRational.denominator;
348 *d_value = ((numerator*1.0)/(denominator*1.0));
353 media_svc_error("buf is NULL");
354 return MS_MEDIA_ERR_INVALID_PARAMETER;
357 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
358 buf[strlen(buf)] = '\0';
362 return MS_MEDIA_ERR_NONE;
365 time_t __media_svc_get_timeline_from_str(const char *timstr)
368 time_t modified_t = 0;
372 if (!STRING_VALID(timstr)) {
373 media_svc_error("Invalid Parameter");
377 /*Exif Format : %Y:%m:%d %H:%M:%S
378 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
379 memset(&t, 0x00, sizeof(struct tm));
383 localtime_r(&rawtime, &timeinfo);
385 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
386 t.tm_isdst = timeinfo.tm_isdst;
387 if (t.tm_isdst != 0) {
388 media_svc_error("DST %d", t.tm_isdst);
391 modified_t = mktime(&t);
392 if (modified_t > 0) {
395 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);
513 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
514 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
515 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
516 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
517 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
525 drm_contentifo is not NULL, if the file is OMA DRM.
526 If the file is not OMA DRM, drm_contentinfo must be NULL.
528 static int __media_svc_get_mime_type(const char *path, char *mimetype)
531 return MS_MEDIA_ERR_INVALID_PARAMETER;
533 /*in case of normal files or failure to get mime in drm */
534 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
535 media_svc_error("aul_get_mime_from_file fail");
536 return MS_MEDIA_ERR_INTERNAL;
539 return MS_MEDIA_ERR_NONE;
542 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
546 for (i = strlen(file_path); i >= 0; i--) {
547 if (file_path[i] == '.') {
548 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
552 if (file_path[i] == '/') {
559 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
561 char *err_attr_name = NULL;
562 double gps_value = 0.0;
563 int mmf_error = FILEINFO_ERROR_NONE;
565 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
566 if (mmf_error == FILEINFO_ERROR_NONE) {
567 if (longitude != NULL) {
568 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
571 SAFE_FREE(err_attr_name);
574 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
575 if (mmf_error == FILEINFO_ERROR_NONE) {
576 if (latitude != NULL) {
577 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
580 SAFE_FREE(err_attr_name);
583 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
584 if (mmf_error == FILEINFO_ERROR_NONE) {
585 if (altitude != NULL) {
586 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
589 SAFE_FREE(err_attr_name);
592 return MS_MEDIA_ERR_NONE;
595 static char *__media_svc_get_thumb_path(uid_t uid)
598 char *result_passwd = NULL;
599 struct group *grpinfo = NULL;
600 if (uid == getuid()) {
601 grpinfo = getgrnam("users");
602 if (grpinfo == NULL) {
603 media_svc_error("getgrnam(users) returns NULL !");
606 len = strlen(MEDIA_SVC_THUMB_PATH_PREFIX);
608 result_passwd = strndup(MEDIA_SVC_THUMB_PATH_PREFIX, len);
610 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
611 struct passwd *userinfo = getpwuid(uid);
612 if (userinfo == NULL) {
613 media_svc_error("getpwuid(%d) returns NULL !", uid);
616 grpinfo = getgrnam("users");
617 if (grpinfo == NULL) {
618 media_svc_error("getgrnam(users) returns NULL !");
621 /* Compare git_t type and not group name */
622 if (grpinfo->gr_gid != userinfo->pw_gid) {
623 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
626 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
628 result_passwd = strndup(passwd_str, len);
631 return result_passwd;
634 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
636 int ret = MS_MEDIA_ERR_NONE;
637 unsigned char *raw_image = NULL;
640 unsigned int raw_size = 0;
641 void *resized_raw_image = NULL;
642 int resized_width = 0;
643 int resized_height = 0;
644 unsigned int buf_size = 0;
646 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
647 media_svc_debug("type [jpeg] size [%d]", size);
649 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
650 if (ret != MS_MEDIA_ERR_NONE) {
651 media_svc_error("image_util_decode_jpeg_from_memory failed");
652 *resize_image = image;
654 return MS_MEDIA_ERR_NONE;
657 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
658 media_svc_debug("No need resizing");
659 *resize_image = image;
661 SAFE_FREE(raw_image);
662 return MS_MEDIA_ERR_NONE;
665 if (width > height) {
666 resized_height = MEDIA_SVC_ARTWORK_SIZE;
667 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
669 resized_width = MEDIA_SVC_ARTWORK_SIZE;
670 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
673 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
675 resized_raw_image = malloc(buf_size);
677 if (resized_raw_image == NULL) {
678 media_svc_error("malloc failed");
679 *resize_image = image;
681 SAFE_FREE(raw_image);
682 return MS_MEDIA_ERR_NONE;
685 memset(resized_raw_image, 0, buf_size);
687 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
688 if (ret != MS_MEDIA_ERR_NONE) {
689 media_svc_error("image_util_resize failed");
690 *resize_image = image;
692 SAFE_FREE(raw_image);
693 SAFE_FREE(resized_raw_image);
694 return MS_MEDIA_ERR_NONE;
696 SAFE_FREE(raw_image);
699 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
700 if (ret != MS_MEDIA_ERR_NONE) {
701 media_svc_error("image_util_encode_jpeg_to_memory failed");
702 *resize_image = image;
704 SAFE_FREE(resized_raw_image);
705 return MS_MEDIA_ERR_NONE;
707 SAFE_FREE(resized_raw_image);
709 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
710 media_svc_debug("type [png] size [%d]", size);
711 *resize_image = image;
715 media_svc_debug("Not proper img format");
716 *resize_image = image;
723 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
725 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
728 media_svc_error("invalid image..");
729 return MS_MEDIA_ERR_INVALID_PARAMETER;
733 char *thumb_path = __media_svc_get_thumb_path(uid);
734 if (!STRING_VALID(thumb_path)) {
735 media_svc_error("fail to get thumb_path");
736 return MS_MEDIA_ERR_INTERNAL;
739 if (-1 == statfs(thumb_path, &fs)) {
740 media_svc_error("error in statfs");
741 SAFE_FREE(thumb_path);
742 return MS_MEDIA_ERR_INTERNAL;
745 SAFE_FREE(thumb_path);
747 long bsize_kbytes = fs.f_bsize >> 10;
749 if ((bsize_kbytes * fs.f_bavail) < 1024) {
750 media_svc_error("not enought space...");
751 return MS_MEDIA_ERR_INTERNAL;
756 if (image != NULL && size > 0) {
757 fp = fopen(image_path, "w");
760 media_svc_error("failed to open file");
761 return MS_MEDIA_ERR_INTERNAL;
764 nwrite = fwrite(image, 1, size, fp);
765 if (nwrite != size) {
766 media_svc_error("failed to write thumbnail");
768 return MS_MEDIA_ERR_INTERNAL;
773 return MS_MEDIA_ERR_NONE;
776 static char *_media_svc_get_title_from_filepath(const char *path)
778 char *filename = NULL;
781 int filename_len = -1;
782 int new_title_len = -1;
785 media_svc_error("path is NULL");
789 filename = g_path_get_basename(path);
790 if (!STRING_VALID(filename)) {
791 media_svc_error("wrong file name");
796 filename_len = strlen(filename);
798 ext = g_strrstr(filename, ".");
800 media_svc_error("there is no file extention");
804 new_title_len = filename_len - strlen(ext);
805 if (new_title_len < 1) {
806 media_svc_error("title length is zero");
811 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
815 media_svc_debug("extract title is [%s]", title);
820 int _media_svc_rename_file(const char *old_name, const char *new_name)
822 if ((old_name == NULL) || (new_name == NULL)) {
823 media_svc_error("invalid file name");
824 return MS_MEDIA_ERR_INVALID_PARAMETER;
827 if (rename(old_name, new_name) < 0) {
828 media_svc_stderror(" ");
829 return MS_MEDIA_ERR_INTERNAL;
832 return MS_MEDIA_ERR_NONE;
835 int _media_svc_remove_file(const char *path)
839 result = remove(path);
841 media_svc_debug("success to remove file");
842 return MS_MEDIA_ERR_NONE;
844 media_svc_stderror("fail to remove file result");
845 return MS_MEDIA_ERR_INTERNAL;
849 int _media_svc_remove_all_files_in_dir(const char *dir_path)
852 struct dirent *result;
854 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
857 dir = opendir(dir_path);
859 media_svc_error("%s is not exist", dir_path);
860 return MS_MEDIA_ERR_INVALID_PARAMETER;
863 while (!readdir_r(dir, &entry, &result)) {
867 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
870 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
872 if (stat(filename, &st) != 0) {
875 if (S_ISDIR(st.st_mode)) {
878 if (unlink(filename) != 0) {
879 media_svc_stderror("failed to remove");
881 return MS_MEDIA_ERR_INTERNAL;
886 return MS_MEDIA_ERR_NONE;
889 char *_media_svc_get_thumb_internal_path(uid_t uid)
892 char *result_passwd = NULL;
893 struct group *grpinfo = NULL;
894 if (uid == getuid()) {
895 grpinfo = getgrnam("users");
896 if (grpinfo == NULL) {
897 media_svc_error("getgrnam(users) returns NULL !");
900 len = strlen(MEDIA_SVC_THUMB_INTERNAL_PATH);
902 result_passwd = strndup(MEDIA_SVC_THUMB_INTERNAL_PATH, len);
904 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
905 struct passwd *userinfo = getpwuid(uid);
906 if (userinfo == NULL) {
907 media_svc_error("getpwuid(%d) returns NULL !", uid);
910 grpinfo = getgrnam("users");
911 if (grpinfo == NULL) {
912 media_svc_error("getgrnam(users) returns NULL !");
915 /* Compare git_t type and not group name */
916 if (grpinfo->gr_gid != userinfo->pw_gid) {
917 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
920 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
922 result_passwd = strndup(passwd_str, len);
925 return result_passwd;
928 char *_media_svc_get_thumb_external_path(uid_t uid)
931 char *result_passwd = NULL;
932 struct group *grpinfo = NULL;
933 if (uid == getuid()) {
934 grpinfo = getgrnam("users");
935 if (grpinfo == NULL) {
936 media_svc_error("getgrnam(users) returns NULL !");
939 len = strlen(MEDIA_SVC_THUMB_EXTERNAL_PATH);
941 result_passwd = strndup(MEDIA_SVC_THUMB_EXTERNAL_PATH, len);
943 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
944 struct passwd *userinfo = getpwuid(uid);
945 if (userinfo == NULL) {
946 media_svc_error("getpwuid(%d) returns NULL !", uid);
949 grpinfo = getgrnam("users");
950 if (grpinfo == NULL) {
951 media_svc_error("getgrnam(users) returns NULL !");
954 /* Compare git_t type and not group name */
955 if (grpinfo->gr_gid != userinfo->pw_gid) {
956 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
959 len = snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
961 result_passwd = strndup(passwd_str, len);
964 return result_passwd;
967 static int __media_svc_check_thumb_dir(const char *thumb_dir)
972 dir = opendir(thumb_dir);
976 media_svc_stderror("opendir fail");
977 if (errno == ENOENT) {
978 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
979 ret = mkdir(thumb_dir, 0777);
981 media_svc_error("make fail");
988 ret = chmod(thumb_dir, 0777);
990 media_svc_stderror("chmod failed");
992 ret = chown(thumb_dir, 5000, 5000);
994 media_svc_stderror("chown failed");
998 return MS_MEDIA_ERR_NONE;
1004 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)
1006 int ret = MS_MEDIA_ERR_NONE;
1007 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1008 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1009 char *thumb_dir = NULL;
1010 char hash[255 + 1] = {0, };
1011 char *thumbfile_ext = NULL;
1012 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1013 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1015 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1016 media_svc_error("fail to get thumbnail path");
1017 SAFE_FREE(internal_thumb_path);
1018 SAFE_FREE(external_thumb_path);
1019 return MS_MEDIA_ERR_INTERNAL;
1022 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1024 ret = __media_svc_check_thumb_dir(thumb_dir);
1025 if (ret != MS_MEDIA_ERR_NONE) {
1026 media_svc_error("__media_svc_check_thumb_dir");
1027 SAFE_FREE(internal_thumb_path);
1028 SAFE_FREE(external_thumb_path);
1029 return MS_MEDIA_ERR_INTERNAL;
1032 memset(file_ext, 0, sizeof(file_ext));
1033 if (!__media_svc_get_file_ext(pathname, file_ext)) {
1034 media_svc_error("get file ext fail");
1037 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1038 if (ret != MS_MEDIA_ERR_NONE) {
1039 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1040 SAFE_FREE(internal_thumb_path);
1041 SAFE_FREE(external_thumb_path);
1042 return MS_MEDIA_ERR_INTERNAL;
1045 /*media_svc_debug("img format is [%s]", img_format); */
1047 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1048 thumbfile_ext = (char *)"jpg";
1049 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1050 thumbfile_ext = (char *)"png";
1051 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1052 thumbfile_ext = (char *)"gif";
1053 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1054 thumbfile_ext = (char *)"bmp";
1056 media_svc_error("Not proper img format");
1057 SAFE_FREE(internal_thumb_path);
1058 SAFE_FREE(external_thumb_path);
1059 return MS_MEDIA_ERR_INTERNAL;
1062 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1063 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1064 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1066 SAFE_FREE(internal_thumb_path);
1067 SAFE_FREE(external_thumb_path);
1069 return MS_MEDIA_ERR_NONE;
1072 int _media_svc_get_file_time(const char *full_path)
1074 struct stat statbuf;
1077 memset(&statbuf, 0, sizeof(struct stat));
1078 fd = stat(full_path, &statbuf);
1080 media_svc_error("stat(%s) fails.", full_path);
1081 return MS_MEDIA_ERR_INTERNAL;
1084 return statbuf.st_mtime;
1087 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1089 int ret = MS_MEDIA_ERR_NONE;
1091 /* Set default GPS value before extracting meta information */
1092 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1093 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1094 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1096 /* Set filename to title for all media */
1098 title = _media_svc_get_title_from_filepath(content_info->path);
1100 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1101 if (ret != MS_MEDIA_ERR_NONE)
1102 media_svc_error("strcpy error");
1105 media_svc_error("Can't extract title");
1106 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1107 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1110 /* Set default value before extracting meta information */
1111 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1112 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1114 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1115 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1117 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1118 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1120 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1121 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1123 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1124 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1126 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1127 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1129 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1130 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1132 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1133 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1135 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1136 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1139 media_svc_debug("refresh");
1140 return MS_MEDIA_ERR_NONE;
1143 content_info->played_count = 0;
1144 content_info->last_played_time = 0;
1145 content_info->last_played_position = 0;
1146 content_info->favourate = 0;
1147 content_info->media_meta.rating = 0;
1149 return MS_MEDIA_ERR_NONE;
1152 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1153 const char *path, media_svc_media_type_e *media_type, bool refresh)
1155 int ret = MS_MEDIA_ERR_NONE;
1156 char * media_uuid = NULL;
1157 char * file_name = NULL;
1158 bool drm_type = false;
1159 char mime_type[256] = {0, };
1161 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1162 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1164 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1166 memset(&st, 0, sizeof(struct stat));
1167 if (stat(path, &st) == 0) {
1168 content_info->modified_time = st.st_mtime;
1169 content_info->timeline = content_info->modified_time;
1170 content_info->size = st.st_size;
1171 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1173 media_svc_stderror("stat failed");
1177 _media_svc_set_default_value(content_info, refresh);
1179 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1181 media_svc_debug("refresh");
1182 return MS_MEDIA_ERR_NONE;
1185 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1186 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1188 content_info->storage_type = storage_type;
1189 time(&content_info->added_time);
1191 media_uuid = _media_info_generate_uuid();
1192 if (media_uuid == NULL) {
1193 _media_svc_destroy_content_info(content_info);
1194 return MS_MEDIA_ERR_INTERNAL;
1197 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1198 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1200 file_name = g_path_get_basename(path);
1201 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1202 SAFE_FREE(file_name);
1203 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1205 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1206 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1207 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1208 ret = __media_svc_get_mime_type(path, mime_type);
1209 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1211 media_svc_debug("mime [%s]", mime_type);
1212 content_info->is_drm = drm_type;
1214 ret = __media_svc_get_media_type(path, mime_type, media_type);
1215 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1217 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1218 media_svc_error("invalid media_type condition[%d]", *media_type);
1219 return MS_MEDIA_ERR_INVALID_PARAMETER;
1222 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1223 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1225 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1227 content_info->media_type = *media_type;
1230 return MS_MEDIA_ERR_NONE;
1233 int image_360_check(char *path)
1238 unsigned char exif_header[4];
1239 unsigned char exif_app1[2];
1240 unsigned char *exif_app1_data;
1241 unsigned char exif_app1_xmp[2];
1242 long exif_app1_xmp_size = 0;
1243 unsigned char exif_app1_xmp_t[2];
1250 fp = fopen(path, "rb");
1254 size = fread(exif_header, 1, sizeof (exif_header), fp);
1258 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1259 size = fread(exif_app1, 1, sizeof (exif_app1), fp);
1263 size1 = exif_app1[0];
1264 size2 = exif_app1[1];
1266 app1_size = size1 * 256 + size2 - 2;
1268 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1271 size = fread(exif_app1_xmp, 1, sizeof (exif_app1_xmp), fp);
1275 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1277 size = fread(exif_app1_xmp_t, 1, sizeof (exif_app1_xmp_t), fp);
1281 size1 = exif_app1_xmp_t[0];
1282 size2 = exif_app1_xmp_t[1];
1284 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1286 xmp_data = (char *)malloc(exif_app1_xmp_size);
1287 memset(xmp_data, 0x00, exif_app1_xmp_size);
1288 while (exif_app1_xmp_size >= 0) {
1289 exif_app1_xmp_size--;
1290 ch = (char)fgetc(fp);
1297 xmp_data = xmp_data - temp;
1299 if(strstr(xmp_data, "UsePanoramaViewer")
1300 && strstr(xmp_data, "True")
1301 && strstr(xmp_data, "ProjectionType")
1302 && strstr(xmp_data, "equirectangular"))
1317 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1319 int ret = MS_MEDIA_ERR_NONE;
1321 int orient_value = 0;
1323 int exif_height = 0;
1324 ExifData *ed = NULL;
1325 int has_datetaken = FALSE;
1326 int datetaken_size = 19;
1327 double fnumber = 0.0;
1331 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1332 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1333 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1334 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1336 memset(buf, 0x00, sizeof(buf));
1337 memset(description_buf, 0x00, sizeof(description_buf));
1338 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1339 memset(model_buf, 0x00, sizeof(model_buf));
1341 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1342 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1343 return MS_MEDIA_ERR_INVALID_PARAMETER;
1346 path = content_info->path;
1347 if (!STRING_VALID(path)) {
1348 media_svc_error("Invalid Path");
1349 return MS_MEDIA_ERR_INVALID_PARAMETER;
1352 /* Load an ExifData object from an EXIF file */
1353 ed = exif_data_new_from_file(path);
1356 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1357 goto GET_WIDTH_HEIGHT;
1360 content_info->media_meta.is_360 = image_360_check(path);
1362 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1363 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1364 if (strlen(buf) > 0) {
1365 if (strcmp(buf, "S") == 0) {
1369 content_info->media_meta.latitude = value;
1371 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1374 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1377 memset(buf, 0x00, sizeof(buf));
1379 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1380 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1381 if (strlen(buf) > 0) {
1382 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);
1460 /* Get exposure_time value from exif. */
1461 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1462 if (strlen(exposure_time_buf) == 0) {
1463 //media_svc_debug("exposure_time_buf is NULL");
1465 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1466 if (ret != MS_MEDIA_ERR_NONE)
1467 media_svc_error("strcpy error");
1471 /* Get fnumber value from exif. */
1472 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1473 if (fnumber > 0.0) {
1474 content_info->media_meta.fnumber = fnumber;
1476 content_info->media_meta.fnumber = 0.0;
1479 content_info->media_meta.fnumber = 0.0;
1482 /* Get iso value from exif. */
1483 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1485 content_info->media_meta.iso = iso;
1487 content_info->media_meta.iso = 0;
1490 content_info->media_meta.iso = 0;
1493 /* Get model value from exif. */
1494 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1495 if (strlen(model_buf) == 0) {
1496 //media_svc_debug("model_buf is NULL");
1498 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1499 if (ret != MS_MEDIA_ERR_NONE)
1500 media_svc_error("strcpy error");
1504 /* Get orientation value from exif. */
1505 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1506 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1507 content_info->media_meta.orientation = orient_value;
1509 content_info->media_meta.orientation = 0;
1512 content_info->media_meta.orientation = 0;
1515 /* Get width value from exif. */
1516 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1517 if (exif_width > 0) {
1518 content_info->media_meta.width = exif_width;
1520 content_info->media_meta.width = 0;
1523 content_info->media_meta.width = 0;
1526 /* Get height value from exif. */
1527 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1528 if (exif_height > 0) {
1529 content_info->media_meta.height = exif_height;
1531 content_info->media_meta.height = 0;
1534 content_info->media_meta.height = 0;
1537 if (ed != NULL) exif_data_unref(ed);
1541 if (content_info->media_meta.width == 0 ||
1542 content_info->media_meta.height == 0) {
1543 /*Get image width, height*/
1544 unsigned int img_width = 0;
1545 unsigned int img_height = 0;
1546 ImgCodecType img_type = IMG_CODEC_NONE;
1548 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1550 if (content_info->media_meta.width == 0)
1551 content_info->media_meta.width = img_width;
1553 if (content_info->media_meta.height == 0)
1554 content_info->media_meta.height = img_height;
1557 return MS_MEDIA_ERR_NONE;
1560 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1562 MMHandleType tag = 0;
1565 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1566 int mmf_error = FILEINFO_ERROR_NONE;
1567 char *err_attr_name = NULL;
1570 int ret = MS_MEDIA_ERR_NONE;
1572 /*Get Content Tag attribute ===========*/
1573 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1575 if (mmf_error == FILEINFO_ERROR_NONE) {
1576 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1577 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1578 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1579 if (ret != MS_MEDIA_ERR_NONE)
1580 media_svc_error("strcpy error");
1582 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1584 SAFE_FREE(err_attr_name);
1585 /*media_svc_debug("album - unknown"); */
1588 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1589 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1590 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1591 if (ret != MS_MEDIA_ERR_NONE)
1592 media_svc_error("strcpy error");
1593 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1595 SAFE_FREE(err_attr_name);
1596 /*media_svc_debug("artist - unknown"); */
1599 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1600 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1601 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1602 if (ret != MS_MEDIA_ERR_NONE)
1603 media_svc_error("strcpy error");
1604 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1606 SAFE_FREE(err_attr_name);
1607 /*media_svc_debug("album_artist - unknown"); */
1610 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1611 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1612 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1613 if (ret != MS_MEDIA_ERR_NONE)
1614 media_svc_error("strcpy error");
1616 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1617 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1619 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1620 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1624 SAFE_FREE(err_attr_name);
1625 /*media_svc_debug("genre - unknown"); */
1628 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1629 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1631 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1632 if (ret != MS_MEDIA_ERR_NONE)
1633 media_svc_error("strcpy error");
1635 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1639 for (idx = 0; idx < size; idx++) {
1641 media_svc_debug("SPACE [%s]", p);
1645 media_svc_debug("Not SPACE [%s]", p);
1646 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1647 if (ret != MS_MEDIA_ERR_NONE)
1648 media_svc_error("strcpy error");
1654 media_svc_debug("Can't extract title. All string is space");
1655 title = _media_svc_get_title_from_filepath(content_info->path);
1657 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1658 if (ret != MS_MEDIA_ERR_NONE)
1659 media_svc_error("strcpy error");
1662 media_svc_error("Can't extract title");
1667 SAFE_FREE(err_attr_name);
1668 title = _media_svc_get_title_from_filepath(content_info->path);
1670 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1671 if (ret != MS_MEDIA_ERR_NONE)
1672 media_svc_error("strcpy error");
1675 media_svc_error("Can't extract title");
1679 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1680 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1681 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1682 if (ret != MS_MEDIA_ERR_NONE)
1683 media_svc_error("strcpy error");
1684 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1686 SAFE_FREE(err_attr_name);
1689 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1690 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1691 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1692 if (ret != MS_MEDIA_ERR_NONE)
1693 media_svc_error("strcpy error");
1694 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1695 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1697 /*media_svc_debug("composer - unknown"); */
1698 SAFE_FREE(err_attr_name);
1701 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1702 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1703 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1704 if (ret != MS_MEDIA_ERR_NONE)
1705 media_svc_error("strcpy error");
1706 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1707 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1709 /*media_svc_debug("copyright - unknown"); */
1710 SAFE_FREE(err_attr_name);
1713 mmf_error = mm_file_destroy_tag_attrs(tag);
1714 if (mmf_error != FILEINFO_ERROR_NONE) {
1715 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1718 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1719 char *no_tag_title = NULL;
1720 media_svc_error("no tag information");
1722 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1724 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1725 if (ret != MS_MEDIA_ERR_NONE)
1726 media_svc_error("strcpy error");
1727 SAFE_FREE(no_tag_title);
1729 media_svc_error("Can't extract title");
1732 content_info->album_id = album_id;
1735 return MS_MEDIA_ERR_NONE;
1738 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1740 MMHandleType content = 0;
1741 MMHandleType tag = 0;
1743 unsigned char *image = NULL;
1744 unsigned int size = 0;
1745 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1746 int mmf_error = FILEINFO_ERROR_NONE;
1747 char *err_attr_name = NULL;
1749 bool extract_thumbnail = FALSE;
1750 bool append_album = FALSE;
1752 int ret = MS_MEDIA_ERR_NONE;
1754 unsigned int resize_size = 0;
1755 unsigned char *resize_image = NULL;
1757 /*Get Content Tag attribute ===========*/
1758 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1760 if (mmf_error == FILEINFO_ERROR_NONE) {
1761 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1762 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1763 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1764 if (ret != MS_MEDIA_ERR_NONE)
1765 media_svc_error("strcpy error");
1767 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1769 SAFE_FREE(err_attr_name);
1770 /*media_svc_debug("album - unknown"); */
1773 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1774 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1775 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1776 if (ret != MS_MEDIA_ERR_NONE)
1777 media_svc_error("strcpy error");
1778 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1780 SAFE_FREE(err_attr_name);
1781 /*media_svc_debug("artist - unknown"); */
1784 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1785 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1786 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1787 if (ret != MS_MEDIA_ERR_NONE)
1788 media_svc_error("strcpy error");
1789 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1791 SAFE_FREE(err_attr_name);
1792 /*media_svc_debug("album_artist - unknown"); */
1795 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1796 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1797 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1798 if (ret != MS_MEDIA_ERR_NONE)
1799 media_svc_error("strcpy error");
1801 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1802 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1804 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1805 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1809 SAFE_FREE(err_attr_name);
1810 /*media_svc_debug("genre - unknown"); */
1813 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1814 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1816 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1817 if (ret != MS_MEDIA_ERR_NONE)
1818 media_svc_error("strcpy error");
1820 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1824 for (idx = 0; idx < size; idx++) {
1826 media_svc_debug("SPACE [%s]", p);
1830 media_svc_debug("Not SPACE [%s]", p);
1831 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1832 if (ret != MS_MEDIA_ERR_NONE)
1833 media_svc_error("strcpy error");
1839 media_svc_debug("Can't extract title. All string is space");
1840 title = _media_svc_get_title_from_filepath(content_info->path);
1842 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1843 if (ret != MS_MEDIA_ERR_NONE)
1844 media_svc_error("strcpy error");
1847 media_svc_error("Can't extract title");
1852 SAFE_FREE(err_attr_name);
1853 title = _media_svc_get_title_from_filepath(content_info->path);
1855 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1856 if (ret != MS_MEDIA_ERR_NONE)
1857 media_svc_error("strcpy error");
1860 media_svc_error("Can't extract title");
1864 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1865 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1866 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1867 if (ret != MS_MEDIA_ERR_NONE)
1868 media_svc_error("strcpy error");
1869 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1871 SAFE_FREE(err_attr_name);
1874 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1876 if (mmf_error != FILEINFO_ERROR_NONE) {
1877 SAFE_FREE(err_attr_name);
1880 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1881 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1882 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1883 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1884 char time_info[64] = {0, };
1885 char p_value[64] = {0, };
1887 memset(time_info, 0x00, sizeof(time_info));
1888 memset(p_value, 0x00, sizeof(p_value));
1889 strncpy(p_value, p, size);
1890 for (idx = 0; idx < size; idx++) {
1891 if (p_value[idx] == '-') {
1892 time_info[idx] = ':';
1893 } else if (p_value[idx] != '\0') {
1894 time_info[idx] = p_value[idx];
1896 media_svc_error("strcpy error");
1900 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1902 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1905 if (ret != MS_MEDIA_ERR_NONE) {
1906 media_svc_error("strcpy error");
1908 /* This is same as datetaken */
1910 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1912 int datetaken_size = 19;
1913 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1915 if (ret != MS_MEDIA_ERR_NONE)
1916 media_svc_error("strcpy error");
1918 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1919 if (content_info->timeline == 0) {
1920 content_info->timeline = content_info->modified_time;
1922 media_svc_debug("Timeline : %ld", content_info->timeline);
1925 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1927 SAFE_FREE(err_attr_name);
1930 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1931 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1932 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1933 if (ret != MS_MEDIA_ERR_NONE)
1934 media_svc_error("strcpy error");
1935 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1936 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1938 /*media_svc_debug("composer - unknown"); */
1939 SAFE_FREE(err_attr_name);
1942 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1943 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1944 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1945 if (ret != MS_MEDIA_ERR_NONE)
1946 media_svc_error("strcpy error");
1947 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1948 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1950 /*media_svc_debug("copyright - unknown"); */
1951 SAFE_FREE(err_attr_name);
1954 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1955 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1956 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1957 if (ret != MS_MEDIA_ERR_NONE)
1958 media_svc_error("strcpy error");
1960 SAFE_FREE(err_attr_name);
1963 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1964 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1966 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1967 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1968 if (ret != MS_MEDIA_ERR_NONE)
1969 media_svc_error("strcpy error");
1971 media_svc_debug("Wrong Year Information [%s]", p);
1974 SAFE_FREE(err_attr_name);
1977 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1978 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1979 content_info->media_meta.rating = atoi(p);
1981 SAFE_FREE(err_attr_name);
1982 content_info->media_meta.rating = 0;
1985 /*Initialize album_id to 0. below code will set the album_id*/
1986 content_info->album_id = album_id;
1987 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1989 if (ret != MS_MEDIA_ERR_NONE) {
1990 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1991 media_svc_debug("album does not exist. So start to make album art");
1992 extract_thumbnail = TRUE;
1993 append_album = TRUE;
1995 extract_thumbnail = TRUE;
1996 append_album = FALSE;
1999 content_info->album_id = album_id;
2000 append_album = FALSE;
2002 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
2003 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
2005 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2006 extract_thumbnail = TRUE;
2008 media_svc_debug("album already exists. don't need to make album art");
2009 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2010 extract_thumbnail = TRUE;
2014 /*Do not extract artwork for the USB Storage content*/
2015 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2016 extract_thumbnail = FALSE;
2018 if (extract_thumbnail == TRUE) {
2019 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2020 if (mmf_error != FILEINFO_ERROR_NONE) {
2021 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2022 SAFE_FREE(err_attr_name);
2024 /*media_svc_debug("artwork size1 [%d]", size); */
2027 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2028 if (mmf_error != FILEINFO_ERROR_NONE) {
2029 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2030 SAFE_FREE(err_attr_name);
2032 /*media_svc_debug("artwork size2 [%d]", size); */
2034 if (image != NULL && size > 0) {
2035 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2036 int artwork_mime_size = -1;
2038 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2039 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2040 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2041 if (ret != MS_MEDIA_ERR_NONE) {
2042 media_svc_error("Fail to Get Thumbnail Path");
2044 /* albumart resizing */
2045 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2047 SAFE_FREE(err_attr_name);
2050 if (strlen(thumb_path) > 0) {
2051 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2052 if (ret != MS_MEDIA_ERR_NONE) {
2053 media_svc_error("Fail to Save Thumbnail Image");
2055 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2056 if (ret != MS_MEDIA_ERR_NONE)
2057 media_svc_error("strcpy error");
2063 if (append_album == TRUE) {
2064 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2065 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2066 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2068 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2070 content_info->album_id = album_id;
2073 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2074 double longitude = 0.0;
2075 double latitude = 0.0;
2076 double altitude = 0.0;
2078 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2079 content_info->media_meta.longitude = longitude;
2080 content_info->media_meta.latitude = latitude;
2081 content_info->media_meta.altitude = altitude;
2083 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2084 if (mmf_error != FILEINFO_ERROR_NONE) {
2086 SAFE_FREE(err_attr_name);
2089 media_svc_debug("CDIS : %d", cdis_value);
2092 mmf_error = mm_file_destroy_tag_attrs(tag);
2093 if (mmf_error != FILEINFO_ERROR_NONE) {
2094 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2097 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2098 char *no_tag_title = NULL;
2099 media_svc_error("no tag information");
2101 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2103 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2104 if (ret != MS_MEDIA_ERR_NONE)
2105 media_svc_error("strcpy error");
2106 SAFE_FREE(no_tag_title);
2108 media_svc_error("Can't extract title");
2111 content_info->album_id = album_id;
2114 /*Get Content attribute ===========*/
2115 if (cdis_value == 1) {
2116 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2118 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2121 if (mmf_error == FILEINFO_ERROR_NONE) {
2122 /*Common attribute*/
2123 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2124 if (mmf_error != FILEINFO_ERROR_NONE) {
2125 SAFE_FREE(err_attr_name);
2126 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2128 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2131 /*Sound/Music attribute*/
2132 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2134 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2135 if (mmf_error != FILEINFO_ERROR_NONE) {
2136 SAFE_FREE(err_attr_name);
2137 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2139 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2142 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2143 if (mmf_error != FILEINFO_ERROR_NONE) {
2144 SAFE_FREE(err_attr_name);
2145 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2147 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2150 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2151 if (mmf_error != FILEINFO_ERROR_NONE) {
2152 SAFE_FREE(err_attr_name);
2153 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2155 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2158 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2159 if (mmf_error != FILEINFO_ERROR_NONE) {
2160 SAFE_FREE(err_attr_name);
2161 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2163 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2165 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2166 int audio_bitrate = 0;
2167 int video_bitrate = 0;
2169 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2170 if (mmf_error != FILEINFO_ERROR_NONE) {
2171 SAFE_FREE(err_attr_name);
2172 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2174 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2177 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2178 if (mmf_error != FILEINFO_ERROR_NONE) {
2179 SAFE_FREE(err_attr_name);
2180 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2182 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2185 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2187 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2188 if (mmf_error != FILEINFO_ERROR_NONE) {
2189 SAFE_FREE(err_attr_name);
2190 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2192 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2195 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2196 if (mmf_error != FILEINFO_ERROR_NONE) {
2197 SAFE_FREE(err_attr_name);
2198 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2200 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2204 media_svc_error("Not support type");
2205 mmf_error = mm_file_destroy_content_attrs(content);
2206 if (mmf_error != FILEINFO_ERROR_NONE) {
2207 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2209 return MS_MEDIA_ERR_INVALID_PARAMETER;
2212 mmf_error = mm_file_destroy_content_attrs(content);
2213 if (mmf_error != FILEINFO_ERROR_NONE) {
2214 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2217 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2220 return MS_MEDIA_ERR_NONE;
2223 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2225 media_svc_retm_if(content_info == NULL, "content info is NULL");
2227 /* Delete media_svc_content_info_s */
2228 SAFE_FREE(content_info->media_uuid);
2229 SAFE_FREE(content_info->path);
2230 SAFE_FREE(content_info->file_name);
2231 SAFE_FREE(content_info->mime_type);
2232 SAFE_FREE(content_info->folder_uuid);
2233 SAFE_FREE(content_info->thumbnail_path);
2234 SAFE_FREE(content_info->storage_uuid);
2236 /* Delete media_svc_content_meta_s */
2237 SAFE_FREE(content_info->media_meta.title);
2238 SAFE_FREE(content_info->media_meta.album);
2239 SAFE_FREE(content_info->media_meta.artist);
2240 SAFE_FREE(content_info->media_meta.album_artist);
2241 SAFE_FREE(content_info->media_meta.genre);
2242 SAFE_FREE(content_info->media_meta.composer);
2243 SAFE_FREE(content_info->media_meta.year);
2244 SAFE_FREE(content_info->media_meta.recorded_date);
2245 SAFE_FREE(content_info->media_meta.copyright);
2246 SAFE_FREE(content_info->media_meta.track_num);
2247 SAFE_FREE(content_info->media_meta.description);
2248 SAFE_FREE(content_info->media_meta.datetaken);
2249 SAFE_FREE(content_info->media_meta.exposure_time);
2250 SAFE_FREE(content_info->media_meta.model);
2251 SAFE_FREE(content_info->media_meta.weather);
2252 SAFE_FREE(content_info->media_meta.category);
2253 SAFE_FREE(content_info->media_meta.keyword);
2254 SAFE_FREE(content_info->media_meta.location_tag);
2255 SAFE_FREE(content_info->media_meta.content_name);
2256 SAFE_FREE(content_info->media_meta.age_rating);
2257 SAFE_FREE(content_info->media_meta.author);
2258 SAFE_FREE(content_info->media_meta.provider);
2260 SAFE_FREE(content_info->media_meta.title_pinyin);
2261 SAFE_FREE(content_info->media_meta.album_pinyin);
2262 SAFE_FREE(content_info->media_meta.artist_pinyin);
2263 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2264 SAFE_FREE(content_info->media_meta.genre_pinyin);
2265 SAFE_FREE(content_info->media_meta.composer_pinyin);
2266 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2267 SAFE_FREE(content_info->media_meta.description_pinyin);
2272 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2274 if (STRING_VALID(path)) {
2275 char *internal_path = _media_svc_get_path(uid);
2276 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2277 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2278 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2279 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2280 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2281 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2283 media_svc_error("Invalid Path");
2284 SAFE_FREE(internal_path);
2285 return MS_MEDIA_ERR_INVALID_PARAMETER;
2287 SAFE_FREE(internal_path);
2289 media_svc_error("INVALID parameter");
2290 return MS_MEDIA_ERR_INVALID_PARAMETER;
2293 return MS_MEDIA_ERR_NONE;
2296 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2299 size_t i, count = 0;
2300 size_t oldlen = strlen(olds);
2301 if (oldlen < 1) return s;
2302 size_t newlen = strlen(news);
2304 if (newlen != oldlen) {
2305 for (i = 0; s[i] != '\0';) {
2306 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2309 } else i = strlen(s);
2312 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2313 if (result == NULL) return NULL;
2317 if (memcmp(s, olds, oldlen) == 0) {
2318 memcpy(sr, news, newlen);
2321 } else *sr++ = *s++;
2329 bool _media_svc_is_drm_file(const char *path)
2334 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)
2336 int ret = MS_MEDIA_ERR_NONE;
2338 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2340 if (ret != MS_MEDIA_ERR_NONE) {
2341 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2342 ret = MS_MEDIA_ERR_INTERNAL;
2344 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2350 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2352 int ret = MS_MEDIA_ERR_NONE;
2354 pinyin_name_s *pinyinname = NULL;
2358 if (!STRING_VALID(src_str)) {
2359 media_svc_debug("String is invalid");
2363 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2364 if (ret == MS_MEDIA_ERR_NONE) {
2365 if (STRING_VALID(pinyinname[0].pinyin_name))
2366 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2368 *pinyin_str = strdup(src_str); //Return Original Non China Character
2371 _media_svc_pinyin_free(pinyinname, size);
2376 bool _media_svc_check_pinyin_support(void)
2382 int _media_svc_get_ini_value()
2384 if (g_ini_value == -1) {
2385 dictionary *dict = NULL;
2387 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2389 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2393 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2394 iniparser_freedict(dict);
2396 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2401 char* _media_svc_get_title_from_path(const char *path)
2403 char *filename = NULL;
2406 int filename_len = -1;
2407 int new_title_len = -1;
2410 media_svc_error("path is NULL");
2414 filename = g_path_get_basename(path);
2415 if (!STRING_VALID(filename)) {
2416 media_svc_error("wrong file name");
2417 SAFE_FREE(filename);
2421 filename_len = strlen(filename);
2423 ext = g_strrstr(filename, ".");
2425 media_svc_error("there is no file extention");
2429 new_title_len = filename_len - strlen(ext);
2430 if (new_title_len < 1) {
2431 media_svc_error("title length is zero");
2432 SAFE_FREE(filename);
2436 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2438 SAFE_FREE(filename);
2440 media_svc_debug("extract title is [%s]", title);
2445 #define BUF_LENGHT 256
2447 void _media_svc_print_stderror(void)
2449 char buf[BUF_LENGHT] = {0,};
2451 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));