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)) {
1278 size = fread(exif_app1_xmp_t, 1, sizeof (exif_app1_xmp_t), fp);
1282 size1 = exif_app1_xmp_t[0];
1283 size2 = exif_app1_xmp_t[1];
1285 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1287 xmp_data = (char *)malloc(exif_app1_xmp_size);
1288 memset(xmp_data, 0x00, exif_app1_xmp_size);
1290 while (exif_app1_xmp_size >= 0) {
1291 exif_app1_xmp_size--;
1301 if(strstr(ptr, "UsePanoramaViewer")
1302 && strstr(ptr, "True")
1303 && strstr(ptr, "ProjectionType")
1304 && strstr(ptr, "equirectangular"))
1307 SAFE_FREE(xmp_data);
1327 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1329 int ret = MS_MEDIA_ERR_NONE;
1331 int orient_value = 0;
1333 int exif_height = 0;
1334 ExifData *ed = NULL;
1335 int has_datetaken = FALSE;
1336 int datetaken_size = 19;
1337 double fnumber = 0.0;
1341 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1342 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1343 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1344 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1346 memset(buf, 0x00, sizeof(buf));
1347 memset(description_buf, 0x00, sizeof(description_buf));
1348 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1349 memset(model_buf, 0x00, sizeof(model_buf));
1351 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1352 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1353 return MS_MEDIA_ERR_INVALID_PARAMETER;
1356 path = content_info->path;
1357 if (!STRING_VALID(path)) {
1358 media_svc_error("Invalid Path");
1359 return MS_MEDIA_ERR_INVALID_PARAMETER;
1362 /* Load an ExifData object from an EXIF file */
1363 ed = exif_data_new_from_file(path);
1366 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1367 goto GET_WIDTH_HEIGHT;
1370 content_info->media_meta.is_360 = image_360_check(path);
1372 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1373 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1374 if (strlen(buf) > 0) {
1375 if (strcmp(buf, "S") == 0) {
1379 content_info->media_meta.latitude = value;
1381 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1384 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1387 memset(buf, 0x00, sizeof(buf));
1389 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1390 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1391 if (strlen(buf) > 0) {
1392 if (strcmp(buf, "W") == 0) {
1396 content_info->media_meta.longitude = value;
1398 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1401 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1404 memset(buf, 0x00, sizeof(buf));
1406 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1407 if (strlen(description_buf) == 0) {
1408 /*media_svc_debug("Use 'No description'"); */
1409 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1410 if (ret != MS_MEDIA_ERR_NONE)
1411 media_svc_error("strcpy error");
1413 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1414 if (ret != MS_MEDIA_ERR_NONE)
1415 media_svc_error("strcpy error");
1418 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1419 if (ret != MS_MEDIA_ERR_NONE)
1420 media_svc_error("strcpy error");
1423 memset(buf, 0x00, sizeof(buf));
1425 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1426 if (strlen(buf) == 0) {
1427 /*media_svc_debug("time is NULL"); */
1429 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1430 if (ret != MS_MEDIA_ERR_NONE) {
1431 media_svc_error("strcpy error");
1433 has_datetaken = TRUE;
1434 /* This is same as recorded_date */
1435 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1436 if (ret != MS_MEDIA_ERR_NONE)
1437 media_svc_error("strcpy error");
1442 memset(buf, 0x00, sizeof(buf));
1444 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1445 if (strlen(buf) == 0) {
1446 /*media_svc_debug("time is NULL"); */
1448 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1449 if (ret != MS_MEDIA_ERR_NONE) {
1450 media_svc_error("strcpy error");
1452 has_datetaken = TRUE;
1453 /* This is same as recorded_date */
1454 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1455 if (ret != MS_MEDIA_ERR_NONE)
1456 media_svc_error("strcpy error");
1461 if (has_datetaken) {
1462 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1463 if (content_info->timeline == 0) {
1464 content_info->timeline = content_info->modified_time;
1466 media_svc_debug("Timeline : %ld", content_info->timeline);
1470 /* Get exposure_time value from exif. */
1471 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1472 if (strlen(exposure_time_buf) == 0) {
1473 //media_svc_debug("exposure_time_buf is NULL");
1475 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1476 if (ret != MS_MEDIA_ERR_NONE)
1477 media_svc_error("strcpy error");
1481 /* Get fnumber value from exif. */
1482 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1483 if (fnumber > 0.0) {
1484 content_info->media_meta.fnumber = fnumber;
1486 content_info->media_meta.fnumber = 0.0;
1489 content_info->media_meta.fnumber = 0.0;
1492 /* Get iso value from exif. */
1493 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1495 content_info->media_meta.iso = iso;
1497 content_info->media_meta.iso = 0;
1500 content_info->media_meta.iso = 0;
1503 /* Get model value from exif. */
1504 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1505 if (strlen(model_buf) == 0) {
1506 //media_svc_debug("model_buf is NULL");
1508 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1509 if (ret != MS_MEDIA_ERR_NONE)
1510 media_svc_error("strcpy error");
1514 /* Get orientation value from exif. */
1515 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1516 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1517 content_info->media_meta.orientation = orient_value;
1519 content_info->media_meta.orientation = 0;
1522 content_info->media_meta.orientation = 0;
1525 /* Get width value from exif. */
1526 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1527 if (exif_width > 0) {
1528 content_info->media_meta.width = exif_width;
1530 content_info->media_meta.width = 0;
1533 content_info->media_meta.width = 0;
1536 /* Get height value from exif. */
1537 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1538 if (exif_height > 0) {
1539 content_info->media_meta.height = exif_height;
1541 content_info->media_meta.height = 0;
1544 content_info->media_meta.height = 0;
1547 if (ed != NULL) exif_data_unref(ed);
1551 if (content_info->media_meta.width == 0 ||
1552 content_info->media_meta.height == 0) {
1553 /*Get image width, height*/
1554 unsigned int img_width = 0;
1555 unsigned int img_height = 0;
1556 ImgCodecType img_type = IMG_CODEC_NONE;
1558 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1560 if (content_info->media_meta.width == 0)
1561 content_info->media_meta.width = img_width;
1563 if (content_info->media_meta.height == 0)
1564 content_info->media_meta.height = img_height;
1567 return MS_MEDIA_ERR_NONE;
1570 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1572 MMHandleType tag = 0;
1575 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1576 int mmf_error = FILEINFO_ERROR_NONE;
1577 char *err_attr_name = NULL;
1580 int ret = MS_MEDIA_ERR_NONE;
1582 /*Get Content Tag attribute ===========*/
1583 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1585 if (mmf_error == FILEINFO_ERROR_NONE) {
1586 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1587 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1588 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1589 if (ret != MS_MEDIA_ERR_NONE)
1590 media_svc_error("strcpy error");
1592 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1594 SAFE_FREE(err_attr_name);
1595 /*media_svc_debug("album - unknown"); */
1598 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1599 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1600 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1601 if (ret != MS_MEDIA_ERR_NONE)
1602 media_svc_error("strcpy error");
1603 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1605 SAFE_FREE(err_attr_name);
1606 /*media_svc_debug("artist - unknown"); */
1609 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1610 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1611 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1612 if (ret != MS_MEDIA_ERR_NONE)
1613 media_svc_error("strcpy error");
1614 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1616 SAFE_FREE(err_attr_name);
1617 /*media_svc_debug("album_artist - unknown"); */
1620 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1621 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1622 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1623 if (ret != MS_MEDIA_ERR_NONE)
1624 media_svc_error("strcpy error");
1626 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1627 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1629 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1630 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1634 SAFE_FREE(err_attr_name);
1635 /*media_svc_debug("genre - unknown"); */
1638 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1639 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1641 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1642 if (ret != MS_MEDIA_ERR_NONE)
1643 media_svc_error("strcpy error");
1645 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1649 for (idx = 0; idx < size; idx++) {
1651 media_svc_debug("SPACE [%s]", p);
1655 media_svc_debug("Not SPACE [%s]", p);
1656 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1657 if (ret != MS_MEDIA_ERR_NONE)
1658 media_svc_error("strcpy error");
1664 media_svc_debug("Can't extract title. All string is space");
1665 title = _media_svc_get_title_from_filepath(content_info->path);
1667 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1668 if (ret != MS_MEDIA_ERR_NONE)
1669 media_svc_error("strcpy error");
1672 media_svc_error("Can't extract title");
1677 SAFE_FREE(err_attr_name);
1678 title = _media_svc_get_title_from_filepath(content_info->path);
1680 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1681 if (ret != MS_MEDIA_ERR_NONE)
1682 media_svc_error("strcpy error");
1685 media_svc_error("Can't extract title");
1689 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1690 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1691 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1692 if (ret != MS_MEDIA_ERR_NONE)
1693 media_svc_error("strcpy error");
1694 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1696 SAFE_FREE(err_attr_name);
1699 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1700 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1701 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1702 if (ret != MS_MEDIA_ERR_NONE)
1703 media_svc_error("strcpy error");
1704 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1705 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1707 /*media_svc_debug("composer - unknown"); */
1708 SAFE_FREE(err_attr_name);
1711 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1712 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1713 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1714 if (ret != MS_MEDIA_ERR_NONE)
1715 media_svc_error("strcpy error");
1716 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1717 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1719 /*media_svc_debug("copyright - unknown"); */
1720 SAFE_FREE(err_attr_name);
1723 mmf_error = mm_file_destroy_tag_attrs(tag);
1724 if (mmf_error != FILEINFO_ERROR_NONE) {
1725 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1728 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1729 char *no_tag_title = NULL;
1730 media_svc_error("no tag information");
1732 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1734 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1735 if (ret != MS_MEDIA_ERR_NONE)
1736 media_svc_error("strcpy error");
1737 SAFE_FREE(no_tag_title);
1739 media_svc_error("Can't extract title");
1742 content_info->album_id = album_id;
1745 return MS_MEDIA_ERR_NONE;
1748 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1750 MMHandleType content = 0;
1751 MMHandleType tag = 0;
1753 unsigned char *image = NULL;
1754 unsigned int size = 0;
1755 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1756 int mmf_error = FILEINFO_ERROR_NONE;
1757 char *err_attr_name = NULL;
1759 bool extract_thumbnail = FALSE;
1760 bool append_album = FALSE;
1762 int ret = MS_MEDIA_ERR_NONE;
1764 unsigned int resize_size = 0;
1765 unsigned char *resize_image = NULL;
1767 /*Get Content Tag attribute ===========*/
1768 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1770 if (mmf_error == FILEINFO_ERROR_NONE) {
1771 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1772 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1773 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1774 if (ret != MS_MEDIA_ERR_NONE)
1775 media_svc_error("strcpy error");
1777 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1779 SAFE_FREE(err_attr_name);
1780 /*media_svc_debug("album - unknown"); */
1783 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1784 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1785 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1786 if (ret != MS_MEDIA_ERR_NONE)
1787 media_svc_error("strcpy error");
1788 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1790 SAFE_FREE(err_attr_name);
1791 /*media_svc_debug("artist - unknown"); */
1794 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1795 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1796 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1797 if (ret != MS_MEDIA_ERR_NONE)
1798 media_svc_error("strcpy error");
1799 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1801 SAFE_FREE(err_attr_name);
1802 /*media_svc_debug("album_artist - unknown"); */
1805 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1806 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1807 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1808 if (ret != MS_MEDIA_ERR_NONE)
1809 media_svc_error("strcpy error");
1811 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1812 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1814 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1815 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1819 SAFE_FREE(err_attr_name);
1820 /*media_svc_debug("genre - unknown"); */
1823 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1824 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1826 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1827 if (ret != MS_MEDIA_ERR_NONE)
1828 media_svc_error("strcpy error");
1830 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1834 for (idx = 0; idx < size; idx++) {
1836 media_svc_debug("SPACE [%s]", p);
1840 media_svc_debug("Not SPACE [%s]", p);
1841 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1842 if (ret != MS_MEDIA_ERR_NONE)
1843 media_svc_error("strcpy error");
1849 media_svc_debug("Can't extract title. All string is space");
1850 title = _media_svc_get_title_from_filepath(content_info->path);
1852 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1853 if (ret != MS_MEDIA_ERR_NONE)
1854 media_svc_error("strcpy error");
1857 media_svc_error("Can't extract title");
1862 SAFE_FREE(err_attr_name);
1863 title = _media_svc_get_title_from_filepath(content_info->path);
1865 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1866 if (ret != MS_MEDIA_ERR_NONE)
1867 media_svc_error("strcpy error");
1870 media_svc_error("Can't extract title");
1874 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1875 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1876 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1877 if (ret != MS_MEDIA_ERR_NONE)
1878 media_svc_error("strcpy error");
1879 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1881 SAFE_FREE(err_attr_name);
1884 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1886 if (mmf_error != FILEINFO_ERROR_NONE) {
1887 SAFE_FREE(err_attr_name);
1890 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1891 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1892 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1893 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1894 char time_info[64] = {0, };
1895 char p_value[64] = {0, };
1897 memset(time_info, 0x00, sizeof(time_info));
1898 memset(p_value, 0x00, sizeof(p_value));
1899 strncpy(p_value, p, size);
1900 for (idx = 0; idx < size; idx++) {
1901 if (p_value[idx] == '-') {
1902 time_info[idx] = ':';
1903 } else if (p_value[idx] != '\0') {
1904 time_info[idx] = p_value[idx];
1906 media_svc_error("strcpy error");
1910 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1912 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1915 if (ret != MS_MEDIA_ERR_NONE) {
1916 media_svc_error("strcpy error");
1918 /* This is same as datetaken */
1920 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1922 int datetaken_size = 19;
1923 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1925 if (ret != MS_MEDIA_ERR_NONE)
1926 media_svc_error("strcpy error");
1928 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1929 if (content_info->timeline == 0) {
1930 content_info->timeline = content_info->modified_time;
1932 media_svc_debug("Timeline : %ld", content_info->timeline);
1935 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1937 SAFE_FREE(err_attr_name);
1940 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1941 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1942 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1943 if (ret != MS_MEDIA_ERR_NONE)
1944 media_svc_error("strcpy error");
1945 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1946 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1948 /*media_svc_debug("composer - unknown"); */
1949 SAFE_FREE(err_attr_name);
1952 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1953 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1954 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1955 if (ret != MS_MEDIA_ERR_NONE)
1956 media_svc_error("strcpy error");
1957 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1958 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1960 /*media_svc_debug("copyright - unknown"); */
1961 SAFE_FREE(err_attr_name);
1964 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1965 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1966 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1967 if (ret != MS_MEDIA_ERR_NONE)
1968 media_svc_error("strcpy error");
1970 SAFE_FREE(err_attr_name);
1973 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1974 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1976 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1977 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1978 if (ret != MS_MEDIA_ERR_NONE)
1979 media_svc_error("strcpy error");
1981 media_svc_debug("Wrong Year Information [%s]", p);
1984 SAFE_FREE(err_attr_name);
1987 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1988 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1989 content_info->media_meta.rating = atoi(p);
1991 SAFE_FREE(err_attr_name);
1992 content_info->media_meta.rating = 0;
1995 /*Initialize album_id to 0. below code will set the album_id*/
1996 content_info->album_id = album_id;
1997 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1999 if (ret != MS_MEDIA_ERR_NONE) {
2000 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2001 media_svc_debug("album does not exist. So start to make album art");
2002 extract_thumbnail = TRUE;
2003 append_album = TRUE;
2005 extract_thumbnail = TRUE;
2006 append_album = FALSE;
2009 content_info->album_id = album_id;
2010 append_album = FALSE;
2012 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
2013 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
2015 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2016 extract_thumbnail = TRUE;
2018 media_svc_debug("album already exists. don't need to make album art");
2019 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2020 extract_thumbnail = TRUE;
2024 /*Do not extract artwork for the USB Storage content*/
2025 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2026 extract_thumbnail = FALSE;
2028 if (extract_thumbnail == TRUE) {
2029 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2030 if (mmf_error != FILEINFO_ERROR_NONE) {
2031 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2032 SAFE_FREE(err_attr_name);
2034 /*media_svc_debug("artwork size1 [%d]", size); */
2037 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2038 if (mmf_error != FILEINFO_ERROR_NONE) {
2039 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2040 SAFE_FREE(err_attr_name);
2042 /*media_svc_debug("artwork size2 [%d]", size); */
2044 if (image != NULL && size > 0) {
2045 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2046 int artwork_mime_size = -1;
2048 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2049 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2050 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2051 if (ret != MS_MEDIA_ERR_NONE) {
2052 media_svc_error("Fail to Get Thumbnail Path");
2054 /* albumart resizing */
2055 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2057 SAFE_FREE(err_attr_name);
2060 if (strlen(thumb_path) > 0) {
2061 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2062 if (ret != MS_MEDIA_ERR_NONE) {
2063 media_svc_error("Fail to Save Thumbnail Image");
2065 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2066 if (ret != MS_MEDIA_ERR_NONE)
2067 media_svc_error("strcpy error");
2073 if (append_album == TRUE) {
2074 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2075 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2076 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2078 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2080 content_info->album_id = album_id;
2083 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2084 double longitude = 0.0;
2085 double latitude = 0.0;
2086 double altitude = 0.0;
2088 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2089 content_info->media_meta.longitude = longitude;
2090 content_info->media_meta.latitude = latitude;
2091 content_info->media_meta.altitude = altitude;
2093 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2094 if (mmf_error != FILEINFO_ERROR_NONE) {
2096 SAFE_FREE(err_attr_name);
2099 media_svc_debug("CDIS : %d", cdis_value);
2102 mmf_error = mm_file_destroy_tag_attrs(tag);
2103 if (mmf_error != FILEINFO_ERROR_NONE) {
2104 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2107 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2108 char *no_tag_title = NULL;
2109 media_svc_error("no tag information");
2111 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2113 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2114 if (ret != MS_MEDIA_ERR_NONE)
2115 media_svc_error("strcpy error");
2116 SAFE_FREE(no_tag_title);
2118 media_svc_error("Can't extract title");
2121 content_info->album_id = album_id;
2124 /*Get Content attribute ===========*/
2125 if (cdis_value == 1) {
2126 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2128 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2131 if (mmf_error == FILEINFO_ERROR_NONE) {
2132 /*Common attribute*/
2133 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2134 if (mmf_error != FILEINFO_ERROR_NONE) {
2135 SAFE_FREE(err_attr_name);
2136 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2138 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2141 /*Sound/Music attribute*/
2142 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2144 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2145 if (mmf_error != FILEINFO_ERROR_NONE) {
2146 SAFE_FREE(err_attr_name);
2147 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2149 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2152 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2153 if (mmf_error != FILEINFO_ERROR_NONE) {
2154 SAFE_FREE(err_attr_name);
2155 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2157 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2160 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2161 if (mmf_error != FILEINFO_ERROR_NONE) {
2162 SAFE_FREE(err_attr_name);
2163 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2165 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2168 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2169 if (mmf_error != FILEINFO_ERROR_NONE) {
2170 SAFE_FREE(err_attr_name);
2171 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2173 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2175 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2176 int audio_bitrate = 0;
2177 int video_bitrate = 0;
2179 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2180 if (mmf_error != FILEINFO_ERROR_NONE) {
2181 SAFE_FREE(err_attr_name);
2182 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2184 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2187 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2188 if (mmf_error != FILEINFO_ERROR_NONE) {
2189 SAFE_FREE(err_attr_name);
2190 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2192 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2195 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2197 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2198 if (mmf_error != FILEINFO_ERROR_NONE) {
2199 SAFE_FREE(err_attr_name);
2200 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2202 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2205 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2206 if (mmf_error != FILEINFO_ERROR_NONE) {
2207 SAFE_FREE(err_attr_name);
2208 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2210 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2214 media_svc_error("Not support type");
2215 mmf_error = mm_file_destroy_content_attrs(content);
2216 if (mmf_error != FILEINFO_ERROR_NONE) {
2217 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2219 return MS_MEDIA_ERR_INVALID_PARAMETER;
2222 mmf_error = mm_file_destroy_content_attrs(content);
2223 if (mmf_error != FILEINFO_ERROR_NONE) {
2224 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2227 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2230 return MS_MEDIA_ERR_NONE;
2233 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2235 media_svc_retm_if(content_info == NULL, "content info is NULL");
2237 /* Delete media_svc_content_info_s */
2238 SAFE_FREE(content_info->media_uuid);
2239 SAFE_FREE(content_info->path);
2240 SAFE_FREE(content_info->file_name);
2241 SAFE_FREE(content_info->mime_type);
2242 SAFE_FREE(content_info->folder_uuid);
2243 SAFE_FREE(content_info->thumbnail_path);
2244 SAFE_FREE(content_info->storage_uuid);
2246 /* Delete media_svc_content_meta_s */
2247 SAFE_FREE(content_info->media_meta.title);
2248 SAFE_FREE(content_info->media_meta.album);
2249 SAFE_FREE(content_info->media_meta.artist);
2250 SAFE_FREE(content_info->media_meta.album_artist);
2251 SAFE_FREE(content_info->media_meta.genre);
2252 SAFE_FREE(content_info->media_meta.composer);
2253 SAFE_FREE(content_info->media_meta.year);
2254 SAFE_FREE(content_info->media_meta.recorded_date);
2255 SAFE_FREE(content_info->media_meta.copyright);
2256 SAFE_FREE(content_info->media_meta.track_num);
2257 SAFE_FREE(content_info->media_meta.description);
2258 SAFE_FREE(content_info->media_meta.datetaken);
2259 SAFE_FREE(content_info->media_meta.exposure_time);
2260 SAFE_FREE(content_info->media_meta.model);
2261 SAFE_FREE(content_info->media_meta.weather);
2262 SAFE_FREE(content_info->media_meta.category);
2263 SAFE_FREE(content_info->media_meta.keyword);
2264 SAFE_FREE(content_info->media_meta.location_tag);
2265 SAFE_FREE(content_info->media_meta.content_name);
2266 SAFE_FREE(content_info->media_meta.age_rating);
2267 SAFE_FREE(content_info->media_meta.author);
2268 SAFE_FREE(content_info->media_meta.provider);
2270 SAFE_FREE(content_info->media_meta.title_pinyin);
2271 SAFE_FREE(content_info->media_meta.album_pinyin);
2272 SAFE_FREE(content_info->media_meta.artist_pinyin);
2273 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2274 SAFE_FREE(content_info->media_meta.genre_pinyin);
2275 SAFE_FREE(content_info->media_meta.composer_pinyin);
2276 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2277 SAFE_FREE(content_info->media_meta.description_pinyin);
2282 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2284 if (STRING_VALID(path)) {
2285 char *internal_path = _media_svc_get_path(uid);
2286 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2287 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2288 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2289 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2290 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2291 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2293 media_svc_error("Invalid Path");
2294 SAFE_FREE(internal_path);
2295 return MS_MEDIA_ERR_INVALID_PARAMETER;
2297 SAFE_FREE(internal_path);
2299 media_svc_error("INVALID parameter");
2300 return MS_MEDIA_ERR_INVALID_PARAMETER;
2303 return MS_MEDIA_ERR_NONE;
2306 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2309 size_t i, count = 0;
2310 size_t oldlen = strlen(olds);
2311 if (oldlen < 1) return s;
2312 size_t newlen = strlen(news);
2314 if (newlen != oldlen) {
2315 for (i = 0; s[i] != '\0';) {
2316 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2319 } else i = strlen(s);
2322 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2323 if (result == NULL) return NULL;
2327 if (memcmp(s, olds, oldlen) == 0) {
2328 memcpy(sr, news, newlen);
2331 } else *sr++ = *s++;
2339 bool _media_svc_is_drm_file(const char *path)
2344 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)
2346 int ret = MS_MEDIA_ERR_NONE;
2348 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2350 if (ret != MS_MEDIA_ERR_NONE) {
2351 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2352 ret = MS_MEDIA_ERR_INTERNAL;
2354 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2360 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2362 int ret = MS_MEDIA_ERR_NONE;
2364 pinyin_name_s *pinyinname = NULL;
2368 if (!STRING_VALID(src_str)) {
2369 media_svc_debug("String is invalid");
2373 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2374 if (ret == MS_MEDIA_ERR_NONE) {
2375 if (STRING_VALID(pinyinname[0].pinyin_name))
2376 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2378 *pinyin_str = strdup(src_str); //Return Original Non China Character
2381 _media_svc_pinyin_free(pinyinname, size);
2386 bool _media_svc_check_pinyin_support(void)
2392 int _media_svc_get_ini_value()
2394 if (g_ini_value == -1) {
2395 dictionary *dict = NULL;
2397 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2399 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2403 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2404 iniparser_freedict(dict);
2406 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2411 char* _media_svc_get_title_from_path(const char *path)
2413 char *filename = NULL;
2416 int filename_len = -1;
2417 int new_title_len = -1;
2420 media_svc_error("path is NULL");
2424 filename = g_path_get_basename(path);
2425 if (!STRING_VALID(filename)) {
2426 media_svc_error("wrong file name");
2427 SAFE_FREE(filename);
2431 filename_len = strlen(filename);
2433 ext = g_strrstr(filename, ".");
2435 media_svc_error("there is no file extention");
2439 new_title_len = filename_len - strlen(ext);
2440 if (new_title_len < 1) {
2441 media_svc_error("title length is zero");
2442 SAFE_FREE(filename);
2446 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2448 SAFE_FREE(filename);
2450 media_svc_debug("extract title is [%s]", title);
2455 #define BUF_LENGHT 256
2457 void _media_svc_print_stderror(void)
2459 char buf[BUF_LENGHT] = {0,};
2461 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));