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 */
419 if (*category & MEDIA_SVC_CATEGORY_ETC) {
420 int prefix_len = strlen(content_category[0].content_type);
422 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
423 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
424 *category ^= MEDIA_SVC_CATEGORY_ETC;
425 *category |= MEDIA_SVC_CATEGORY_SOUND;
430 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
431 *category ^= MEDIA_SVC_CATEGORY_ETC;
432 *category |= MEDIA_SVC_CATEGORY_SOUND;
436 /*check music file in soun files. */
437 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
438 int prefix_len = strlen(content_category[0].content_type) + 1;
440 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
441 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
442 *category ^= MEDIA_SVC_CATEGORY_SOUND;
443 *category |= MEDIA_SVC_CATEGORY_MUSIC;
448 /*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*/
449 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
450 *category ^= MEDIA_SVC_CATEGORY_SOUND;
451 *category |= MEDIA_SVC_CATEGORY_ETC;
453 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
454 /*some video files don't have video stream. in this case it is categorize as music. */
456 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
457 ext = strrchr(path, '.');
459 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0) || (strncasecmp(ext, _ASF_FILE, 5) == 0)) {
464 err = mm_file_get_stream_info(path, &audio, &video);
466 if (audio > 0 && video == 0) {
467 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
468 *category |= MEDIA_SVC_CATEGORY_MUSIC;
471 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
476 return MS_MEDIA_ERR_NONE;
479 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
481 int ret = MS_MEDIA_ERR_NONE;
484 media_svc_media_type_e type;
486 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
487 if (ret != MS_MEDIA_ERR_NONE) {
488 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
491 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
492 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
493 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
494 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
495 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
503 drm_contentifo is not NULL, if the file is OMA DRM.
504 If the file is not OMA DRM, drm_contentinfo must be NULL.
506 static int __media_svc_get_mime_type(const char *path, char *mimetype)
509 return MS_MEDIA_ERR_INVALID_PARAMETER;
511 /*in case of normal files or failure to get mime in drm */
512 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
513 media_svc_error("aul_get_mime_from_file fail");
514 return MS_MEDIA_ERR_INTERNAL;
517 return MS_MEDIA_ERR_NONE;
520 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
524 for (i = strlen(file_path); i >= 0; i--) {
525 if (file_path[i] == '.') {
526 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
530 if (file_path[i] == '/') {
537 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
539 char *err_attr_name = NULL;
540 double gps_value = 0.0;
541 int mmf_error = FILEINFO_ERROR_NONE;
543 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
544 if (mmf_error == FILEINFO_ERROR_NONE) {
545 if (longitude != NULL) {
546 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
549 SAFE_FREE(err_attr_name);
552 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
553 if (mmf_error == FILEINFO_ERROR_NONE) {
554 if (latitude != NULL) {
555 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
558 SAFE_FREE(err_attr_name);
561 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
562 if (mmf_error == FILEINFO_ERROR_NONE) {
563 if (altitude != NULL) {
564 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
567 SAFE_FREE(err_attr_name);
570 return MS_MEDIA_ERR_NONE;
573 static char *__media_svc_get_thumb_path(uid_t uid)
575 char *result_passwd = NULL;
576 struct group *grpinfo = NULL;
577 if (uid == getuid()) {
578 grpinfo = getgrnam("users");
579 if (grpinfo == NULL) {
580 media_svc_error("getgrnam(users) returns NULL !");
583 result_passwd = g_strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
585 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
586 struct passwd *userinfo = getpwuid(uid);
587 if (userinfo == NULL) {
588 media_svc_error("getpwuid(%d) returns NULL !", uid);
591 grpinfo = getgrnam("users");
592 if (grpinfo == NULL) {
593 media_svc_error("getgrnam(users) returns NULL !");
596 /* Compare git_t type and not group name */
597 if (grpinfo->gr_gid != userinfo->pw_gid) {
598 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
601 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb", userinfo->pw_dir);
602 result_passwd = g_strdup(passwd_str);
605 return result_passwd;
608 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
610 int ret = MS_MEDIA_ERR_NONE;
611 unsigned char *raw_image = NULL;
614 unsigned int raw_size = 0;
615 void *resized_raw_image = NULL;
616 int resized_width = 0;
617 int resized_height = 0;
618 unsigned int buf_size = 0;
620 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
621 media_svc_debug("type [jpeg] size [%d]", size);
623 ret = image_util_decode_jpeg_from_memory(image, size, IMAGE_UTIL_COLORSPACE_RGB888, &raw_image, &width, &height, &raw_size);
624 if (ret != MS_MEDIA_ERR_NONE) {
625 media_svc_error("image_util_decode_jpeg_from_memory failed");
626 *resize_image = image;
628 return MS_MEDIA_ERR_NONE;
631 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
632 media_svc_debug("No need resizing");
633 *resize_image = image;
635 SAFE_FREE(raw_image);
636 return MS_MEDIA_ERR_NONE;
639 if (width > height) {
640 resized_height = MEDIA_SVC_ARTWORK_SIZE;
641 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
643 resized_width = MEDIA_SVC_ARTWORK_SIZE;
644 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
647 image_util_calculate_buffer_size(resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888 , &buf_size);
649 resized_raw_image = malloc(buf_size);
651 if (resized_raw_image == NULL) {
652 media_svc_error("malloc failed");
653 *resize_image = image;
655 SAFE_FREE(raw_image);
656 return MS_MEDIA_ERR_NONE;
659 memset(resized_raw_image, 0, buf_size);
661 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, IMAGE_UTIL_COLORSPACE_RGB888);
662 if (ret != MS_MEDIA_ERR_NONE) {
663 media_svc_error("image_util_resize failed");
664 *resize_image = image;
666 SAFE_FREE(raw_image);
667 SAFE_FREE(resized_raw_image);
668 return MS_MEDIA_ERR_NONE;
670 SAFE_FREE(raw_image);
673 ret = image_util_encode_jpeg_to_memory(resized_raw_image, resized_width, resized_height, IMAGE_UTIL_COLORSPACE_RGB888, 90, resize_image, resize_size);
674 if (ret != MS_MEDIA_ERR_NONE) {
675 media_svc_error("image_util_encode_jpeg_to_memory failed");
676 *resize_image = image;
678 SAFE_FREE(resized_raw_image);
679 return MS_MEDIA_ERR_NONE;
681 SAFE_FREE(resized_raw_image);
683 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
684 media_svc_debug("type [png] size [%d]", size);
685 *resize_image = image;
689 media_svc_debug("Not proper img format");
690 *resize_image = image;
697 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
699 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
702 media_svc_error("invalid image..");
703 return MS_MEDIA_ERR_INVALID_PARAMETER;
707 char *thumb_path = __media_svc_get_thumb_path(uid);
708 if (!STRING_VALID(thumb_path)) {
709 media_svc_error("fail to get thumb_path");
710 return MS_MEDIA_ERR_INTERNAL;
713 if (-1 == statfs(thumb_path, &fs)) {
714 media_svc_error("error in statfs");
715 SAFE_FREE(thumb_path);
716 return MS_MEDIA_ERR_INTERNAL;
719 SAFE_FREE(thumb_path);
721 long bsize_kbytes = fs.f_bsize >> 10;
723 if ((bsize_kbytes * fs.f_bavail) < 1024) {
724 media_svc_error("not enought space...");
725 return MS_MEDIA_ERR_INTERNAL;
730 if (image != NULL && size > 0) {
731 fp = fopen(image_path, "w");
734 media_svc_error("failed to open file");
735 return MS_MEDIA_ERR_INTERNAL;
738 nwrite = fwrite(image, 1, size, fp);
739 if (nwrite != size) {
740 media_svc_error("failed to write thumbnail");
742 return MS_MEDIA_ERR_INTERNAL;
747 return MS_MEDIA_ERR_NONE;
750 static char *_media_svc_get_title_from_filepath(const char *path)
752 char *filename = NULL;
755 int filename_len = -1;
756 int new_title_len = -1;
759 media_svc_error("path is NULL");
763 filename = g_path_get_basename(path);
764 if (!STRING_VALID(filename)) {
765 media_svc_error("wrong file name");
770 filename_len = strlen(filename);
772 ext = g_strrstr(filename, ".");
774 media_svc_error("there is no file extention");
778 new_title_len = filename_len - strlen(ext);
779 if (new_title_len < 1) {
780 media_svc_error("title length is zero");
785 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
789 media_svc_debug("extract title is [%s]", title);
794 int _media_svc_rename_file(const char *old_name, const char *new_name)
796 if ((old_name == NULL) || (new_name == NULL)) {
797 media_svc_error("invalid file name");
798 return MS_MEDIA_ERR_INVALID_PARAMETER;
801 if (rename(old_name, new_name) < 0) {
802 media_svc_stderror(" ");
803 return MS_MEDIA_ERR_INTERNAL;
806 return MS_MEDIA_ERR_NONE;
809 int _media_svc_remove_file(const char *path)
813 result = remove(path);
815 media_svc_debug("success to remove file");
816 return MS_MEDIA_ERR_NONE;
818 media_svc_stderror("fail to remove file result");
819 return MS_MEDIA_ERR_INTERNAL;
823 int _media_svc_remove_all_files_in_dir(const char *dir_path)
826 struct dirent *result;
828 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
831 dir = opendir(dir_path);
833 media_svc_error("%s is not exist", dir_path);
834 return MS_MEDIA_ERR_INVALID_PARAMETER;
837 while (!readdir_r(dir, &entry, &result)) {
841 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
844 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
846 if (stat(filename, &st) != 0) {
849 if (S_ISDIR(st.st_mode)) {
852 if (unlink(filename) != 0) {
853 media_svc_stderror("failed to remove");
855 return MS_MEDIA_ERR_INTERNAL;
860 return MS_MEDIA_ERR_NONE;
863 char *_media_svc_get_thumb_internal_path(uid_t uid)
865 char *result_passwd = NULL;
866 struct group *grpinfo = NULL;
867 if (uid == getuid()) {
868 grpinfo = getgrnam("users");
869 if (grpinfo == NULL) {
870 media_svc_error("getgrnam(users) returns NULL !");
873 result_passwd = g_strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
875 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
876 struct passwd *userinfo = getpwuid(uid);
877 if (userinfo == NULL) {
878 media_svc_error("getpwuid(%d) returns NULL !", uid);
881 grpinfo = getgrnam("users");
882 if (grpinfo == NULL) {
883 media_svc_error("getgrnam(users) returns NULL !");
886 /* Compare git_t type and not group name */
887 if (grpinfo->gr_gid != userinfo->pw_gid) {
888 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
891 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/phone", userinfo->pw_dir);
892 result_passwd = g_strdup(passwd_str);
895 return result_passwd;
898 char *_media_svc_get_thumb_external_path(uid_t uid)
900 char *result_passwd = NULL;
901 struct group *grpinfo = NULL;
902 if (uid == getuid()) {
903 grpinfo = getgrnam("users");
904 if (grpinfo == NULL) {
905 media_svc_error("getgrnam(users) returns NULL !");
908 result_passwd = g_strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
910 char passwd_str[MEDIA_SVC_PATHNAME_SIZE] = {0, };
911 struct passwd *userinfo = getpwuid(uid);
912 if (userinfo == NULL) {
913 media_svc_error("getpwuid(%d) returns NULL !", uid);
916 grpinfo = getgrnam("users");
917 if (grpinfo == NULL) {
918 media_svc_error("getgrnam(users) returns NULL !");
921 /* Compare git_t type and not group name */
922 if (grpinfo->gr_gid != userinfo->pw_gid) {
923 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
926 snprintf(passwd_str, sizeof(passwd_str), "%s/share/media/.thumb/mmc", userinfo->pw_dir);
927 result_passwd = g_strdup(passwd_str);
930 return result_passwd;
933 static int __media_svc_check_thumb_dir(const char *thumb_dir)
938 dir = opendir(thumb_dir);
942 media_svc_stderror("opendir fail");
943 if (errno == ENOENT) {
944 media_svc_error("[%s] is not exit. So, make it", thumb_dir);
945 ret = mkdir(thumb_dir, 0777);
947 media_svc_error("make fail");
954 ret = chmod(thumb_dir, 0777);
956 media_svc_stderror("chmod failed");
958 ret = chown(thumb_dir, 5000, 5000);
960 media_svc_stderror("chown failed");
964 return MS_MEDIA_ERR_NONE;
970 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)
972 int ret = MS_MEDIA_ERR_NONE;
973 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
974 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
975 char *thumb_dir = NULL;
976 char hash[255 + 1] = {0, };
977 char *thumbfile_ext = NULL;
978 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
979 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
981 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
982 media_svc_error("fail to get thumbnail path");
983 SAFE_FREE(internal_thumb_path);
984 SAFE_FREE(external_thumb_path);
985 return MS_MEDIA_ERR_INTERNAL;
988 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
990 ret = __media_svc_check_thumb_dir(thumb_dir);
991 if (ret != MS_MEDIA_ERR_NONE) {
992 media_svc_error("__media_svc_check_thumb_dir");
993 SAFE_FREE(internal_thumb_path);
994 SAFE_FREE(external_thumb_path);
995 return MS_MEDIA_ERR_INTERNAL;
998 memset(file_ext, 0, sizeof(file_ext));
999 if (!__media_svc_get_file_ext(pathname, file_ext)) {
1000 media_svc_error("get file ext fail");
1003 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1004 if (ret != MS_MEDIA_ERR_NONE) {
1005 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1006 SAFE_FREE(internal_thumb_path);
1007 SAFE_FREE(external_thumb_path);
1008 return MS_MEDIA_ERR_INTERNAL;
1011 /*media_svc_debug("img format is [%s]", img_format); */
1013 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1014 thumbfile_ext = (char *)"jpg";
1015 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1016 thumbfile_ext = (char *)"png";
1017 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1018 thumbfile_ext = (char *)"gif";
1019 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1020 thumbfile_ext = (char *)"bmp";
1022 media_svc_error("Not proper img format");
1023 SAFE_FREE(internal_thumb_path);
1024 SAFE_FREE(external_thumb_path);
1025 return MS_MEDIA_ERR_INTERNAL;
1028 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1029 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1030 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1032 SAFE_FREE(internal_thumb_path);
1033 SAFE_FREE(external_thumb_path);
1035 return MS_MEDIA_ERR_NONE;
1038 int _media_svc_get_file_time(const char *full_path)
1040 struct stat statbuf;
1043 memset(&statbuf, 0, sizeof(struct stat));
1044 fd = stat(full_path, &statbuf);
1046 media_svc_error("stat(%s) fails.", full_path);
1047 return MS_MEDIA_ERR_INTERNAL;
1050 return statbuf.st_mtime;
1053 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1055 int ret = MS_MEDIA_ERR_NONE;
1057 /* Set default GPS value before extracting meta information */
1058 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1059 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1060 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1062 /* Set filename to title for all media */
1064 title = _media_svc_get_title_from_filepath(content_info->path);
1066 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1067 if (ret != MS_MEDIA_ERR_NONE)
1068 media_svc_error("strcpy error");
1071 media_svc_error("Can't extract title");
1072 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1073 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1076 /* Set default value before extracting meta information */
1077 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1078 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1080 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1081 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1083 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1084 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1086 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1087 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1089 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1090 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1092 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
1093 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1095 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1096 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1098 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1099 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1101 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1102 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1105 media_svc_debug("refresh");
1106 return MS_MEDIA_ERR_NONE;
1109 content_info->played_count = 0;
1110 content_info->last_played_time = 0;
1111 content_info->last_played_position = 0;
1112 content_info->favourate = 0;
1113 content_info->media_meta.rating = 0;
1115 return MS_MEDIA_ERR_NONE;
1118 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1119 const char *path, media_svc_media_type_e *media_type, bool refresh)
1121 int ret = MS_MEDIA_ERR_NONE;
1122 char * media_uuid = NULL;
1123 char * file_name = NULL;
1124 bool drm_type = false;
1125 char mime_type[256] = {0, };
1127 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1128 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1130 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1132 memset(&st, 0, sizeof(struct stat));
1133 if (stat(path, &st) == 0) {
1134 content_info->modified_time = st.st_mtime;
1135 content_info->timeline = content_info->modified_time;
1136 content_info->size = st.st_size;
1137 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
1139 media_svc_stderror("stat failed");
1143 _media_svc_set_default_value(content_info, refresh);
1145 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1147 media_svc_debug("refresh");
1148 return MS_MEDIA_ERR_NONE;
1151 ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1152 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1154 content_info->storage_type = storage_type;
1155 time(&content_info->added_time);
1157 media_uuid = _media_info_generate_uuid();
1158 if (media_uuid == NULL) {
1159 _media_svc_destroy_content_info(content_info);
1160 return MS_MEDIA_ERR_INTERNAL;
1163 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1164 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1166 file_name = g_path_get_basename(path);
1167 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1168 SAFE_FREE(file_name);
1169 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1171 if (storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1172 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1173 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1174 ret = __media_svc_get_mime_type(path, mime_type);
1175 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1177 media_svc_debug("mime [%s]", mime_type);
1178 content_info->is_drm = drm_type;
1180 ret = __media_svc_get_media_type(path, mime_type, media_type);
1181 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1183 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1184 media_svc_error("invalid media_type condition[%d]", *media_type);
1185 return MS_MEDIA_ERR_INVALID_PARAMETER;
1188 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1189 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1191 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1193 content_info->media_type = *media_type;
1196 return MS_MEDIA_ERR_NONE;
1199 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1201 int ret = MS_MEDIA_ERR_NONE;
1203 int orient_value = 0;
1205 int exif_height = 0;
1206 ExifData *ed = NULL;
1207 int has_datetaken = FALSE;
1208 int datetaken_size = 19;
1209 double fnumber = 0.0;
1213 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1214 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1215 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1216 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1218 memset(buf, 0x00, sizeof(buf));
1219 memset(description_buf, 0x00, sizeof(description_buf));
1220 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1221 memset(model_buf, 0x00, sizeof(model_buf));
1223 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1224 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1225 return MS_MEDIA_ERR_INVALID_PARAMETER;
1228 path = content_info->path;
1229 if (!STRING_VALID(path)) {
1230 media_svc_error("Invalid Path");
1231 return MS_MEDIA_ERR_INVALID_PARAMETER;
1234 /* Load an ExifData object from an EXIF file */
1235 ed = exif_data_new_from_file(path);
1238 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1239 goto GET_WIDTH_HEIGHT;
1242 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1243 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1244 if (strlen(buf) > 0) {
1245 if (strcmp(buf, "S") == 0) {
1249 content_info->media_meta.latitude = value;
1251 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1254 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1257 memset(buf, 0x00, sizeof(buf));
1259 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1260 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1261 if (strlen(buf) > 0) {
1262 if (strcmp(buf, "W") == 0) {
1266 content_info->media_meta.longitude = value;
1268 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1271 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1274 memset(buf, 0x00, sizeof(buf));
1276 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1277 if (strlen(description_buf) == 0) {
1278 /*media_svc_debug("Use 'No description'"); */
1279 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1280 if (ret != MS_MEDIA_ERR_NONE)
1281 media_svc_error("strcpy error");
1283 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1284 if (ret != MS_MEDIA_ERR_NONE)
1285 media_svc_error("strcpy error");
1288 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1289 if (ret != MS_MEDIA_ERR_NONE)
1290 media_svc_error("strcpy error");
1293 memset(buf, 0x00, sizeof(buf));
1295 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1296 if (strlen(buf) == 0) {
1297 /*media_svc_debug("time is NULL"); */
1299 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1300 if (ret != MS_MEDIA_ERR_NONE) {
1301 media_svc_error("strcpy error");
1303 has_datetaken = TRUE;
1304 /* This is same as recorded_date */
1305 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1306 if (ret != MS_MEDIA_ERR_NONE)
1307 media_svc_error("strcpy error");
1312 memset(buf, 0x00, sizeof(buf));
1314 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1315 if (strlen(buf) == 0) {
1316 /*media_svc_debug("time is NULL"); */
1318 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1319 if (ret != MS_MEDIA_ERR_NONE) {
1320 media_svc_error("strcpy error");
1322 has_datetaken = TRUE;
1323 /* This is same as recorded_date */
1324 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1325 if (ret != MS_MEDIA_ERR_NONE)
1326 media_svc_error("strcpy error");
1331 if (has_datetaken) {
1332 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1333 if (content_info->timeline == 0) {
1334 content_info->timeline = content_info->modified_time;
1336 media_svc_debug("Timeline : %ld", content_info->timeline);
1340 /* Get exposure_time value from exif. */
1341 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1342 if (strlen(exposure_time_buf) == 0) {
1343 //media_svc_debug("exposure_time_buf is NULL");
1345 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1346 if (ret != MS_MEDIA_ERR_NONE)
1347 media_svc_error("strcpy error");
1351 /* Get fnumber value from exif. */
1352 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1353 if (fnumber > 0.0) {
1354 content_info->media_meta.fnumber = fnumber;
1356 content_info->media_meta.fnumber = 0.0;
1359 content_info->media_meta.fnumber = 0.0;
1362 /* Get iso value from exif. */
1363 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1365 content_info->media_meta.iso = iso;
1367 content_info->media_meta.iso = 0;
1370 content_info->media_meta.iso = 0;
1373 /* Get model value from exif. */
1374 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1375 if (strlen(model_buf) == 0) {
1376 //media_svc_debug("model_buf is NULL");
1378 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1379 if (ret != MS_MEDIA_ERR_NONE)
1380 media_svc_error("strcpy error");
1384 /* Get orientation value from exif. */
1385 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1386 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1387 content_info->media_meta.orientation = orient_value;
1389 content_info->media_meta.orientation = 0;
1392 content_info->media_meta.orientation = 0;
1395 /* Get width value from exif. */
1396 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1397 if (exif_width > 0) {
1398 content_info->media_meta.width = exif_width;
1400 content_info->media_meta.width = 0;
1403 content_info->media_meta.width = 0;
1406 /* Get height value from exif. */
1407 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1408 if (exif_height > 0) {
1409 content_info->media_meta.height = exif_height;
1411 content_info->media_meta.height = 0;
1414 content_info->media_meta.height = 0;
1417 if (ed != NULL) exif_data_unref(ed);
1421 if (content_info->media_meta.width == 0 ||
1422 content_info->media_meta.height == 0) {
1423 /*Get image width, height*/
1424 unsigned int img_width = 0;
1425 unsigned int img_height = 0;
1426 ImgCodecType img_type = IMG_CODEC_NONE;
1428 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1430 if (content_info->media_meta.width == 0)
1431 content_info->media_meta.width = img_width;
1433 if (content_info->media_meta.height == 0)
1434 content_info->media_meta.height = img_height;
1437 return MS_MEDIA_ERR_NONE;
1440 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1442 MMHandleType tag = 0;
1445 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1446 int mmf_error = FILEINFO_ERROR_NONE;
1447 char *err_attr_name = NULL;
1450 int ret = MS_MEDIA_ERR_NONE;
1452 /*Get Content Tag attribute ===========*/
1453 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1455 if (mmf_error == FILEINFO_ERROR_NONE) {
1456 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1457 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1458 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1459 if (ret != MS_MEDIA_ERR_NONE)
1460 media_svc_error("strcpy error");
1462 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1464 SAFE_FREE(err_attr_name);
1465 /*media_svc_debug("album - unknown"); */
1468 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1469 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1470 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1471 if (ret != MS_MEDIA_ERR_NONE)
1472 media_svc_error("strcpy error");
1473 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1475 SAFE_FREE(err_attr_name);
1476 /*media_svc_debug("artist - unknown"); */
1479 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1480 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1481 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1482 if (ret != MS_MEDIA_ERR_NONE)
1483 media_svc_error("strcpy error");
1484 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1486 SAFE_FREE(err_attr_name);
1487 /*media_svc_debug("album_artist - unknown"); */
1490 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1491 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1492 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1493 if (ret != MS_MEDIA_ERR_NONE)
1494 media_svc_error("strcpy error");
1496 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1497 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1499 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1500 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1504 SAFE_FREE(err_attr_name);
1505 /*media_svc_debug("genre - unknown"); */
1508 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1509 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1511 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1512 if (ret != MS_MEDIA_ERR_NONE)
1513 media_svc_error("strcpy error");
1515 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1519 for (idx = 0; idx < size; idx++) {
1521 media_svc_debug("SPACE [%s]", p);
1525 media_svc_debug("Not SPACE [%s]", p);
1526 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1527 if (ret != MS_MEDIA_ERR_NONE)
1528 media_svc_error("strcpy error");
1534 media_svc_debug("Can't extract title. All string is space");
1535 title = _media_svc_get_title_from_filepath(content_info->path);
1537 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1538 if (ret != MS_MEDIA_ERR_NONE)
1539 media_svc_error("strcpy error");
1542 media_svc_error("Can't extract title");
1547 SAFE_FREE(err_attr_name);
1548 title = _media_svc_get_title_from_filepath(content_info->path);
1550 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1551 if (ret != MS_MEDIA_ERR_NONE)
1552 media_svc_error("strcpy error");
1555 media_svc_error("Can't extract title");
1559 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1560 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1561 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1562 if (ret != MS_MEDIA_ERR_NONE)
1563 media_svc_error("strcpy error");
1564 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1566 SAFE_FREE(err_attr_name);
1569 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1570 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1571 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1572 if (ret != MS_MEDIA_ERR_NONE)
1573 media_svc_error("strcpy error");
1574 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1575 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1577 /*media_svc_debug("composer - unknown"); */
1578 SAFE_FREE(err_attr_name);
1581 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1582 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1583 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1584 if (ret != MS_MEDIA_ERR_NONE)
1585 media_svc_error("strcpy error");
1586 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1587 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1589 /*media_svc_debug("copyright - unknown"); */
1590 SAFE_FREE(err_attr_name);
1593 mmf_error = mm_file_destroy_tag_attrs(tag);
1594 if (mmf_error != FILEINFO_ERROR_NONE) {
1595 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1598 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1599 char *no_tag_title = NULL;
1600 media_svc_error("no tag information");
1602 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1604 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1605 if (ret != MS_MEDIA_ERR_NONE)
1606 media_svc_error("strcpy error");
1607 SAFE_FREE(no_tag_title);
1609 media_svc_error("Can't extract title");
1612 content_info->album_id = album_id;
1615 return MS_MEDIA_ERR_NONE;
1618 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1620 MMHandleType content = 0;
1621 MMHandleType tag = 0;
1623 unsigned char *image = NULL;
1624 unsigned int size = 0;
1625 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1626 int mmf_error = FILEINFO_ERROR_NONE;
1627 char *err_attr_name = NULL;
1629 bool extract_thumbnail = FALSE;
1630 bool append_album = FALSE;
1632 int ret = MS_MEDIA_ERR_NONE;
1634 unsigned int resize_size = 0;
1635 unsigned char *resize_image = NULL;
1637 /*Get Content Tag attribute ===========*/
1638 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1640 if (mmf_error == FILEINFO_ERROR_NONE) {
1641 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1642 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1643 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1644 if (ret != MS_MEDIA_ERR_NONE)
1645 media_svc_error("strcpy error");
1647 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1649 SAFE_FREE(err_attr_name);
1650 /*media_svc_debug("album - unknown"); */
1653 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1654 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1655 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1656 if (ret != MS_MEDIA_ERR_NONE)
1657 media_svc_error("strcpy error");
1658 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1660 SAFE_FREE(err_attr_name);
1661 /*media_svc_debug("artist - unknown"); */
1664 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1665 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1666 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1667 if (ret != MS_MEDIA_ERR_NONE)
1668 media_svc_error("strcpy error");
1669 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1671 SAFE_FREE(err_attr_name);
1672 /*media_svc_debug("album_artist - unknown"); */
1675 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1676 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1677 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1678 if (ret != MS_MEDIA_ERR_NONE)
1679 media_svc_error("strcpy error");
1681 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1682 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1684 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1685 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1689 SAFE_FREE(err_attr_name);
1690 /*media_svc_debug("genre - unknown"); */
1693 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1694 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1696 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1697 if (ret != MS_MEDIA_ERR_NONE)
1698 media_svc_error("strcpy error");
1700 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1704 for (idx = 0; idx < size; idx++) {
1706 media_svc_debug("SPACE [%s]", p);
1710 media_svc_debug("Not SPACE [%s]", p);
1711 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1712 if (ret != MS_MEDIA_ERR_NONE)
1713 media_svc_error("strcpy error");
1719 media_svc_debug("Can't extract title. All string is space");
1720 title = _media_svc_get_title_from_filepath(content_info->path);
1722 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1723 if (ret != MS_MEDIA_ERR_NONE)
1724 media_svc_error("strcpy error");
1727 media_svc_error("Can't extract title");
1732 SAFE_FREE(err_attr_name);
1733 title = _media_svc_get_title_from_filepath(content_info->path);
1735 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1736 if (ret != MS_MEDIA_ERR_NONE)
1737 media_svc_error("strcpy error");
1740 media_svc_error("Can't extract title");
1744 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1745 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1746 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1747 if (ret != MS_MEDIA_ERR_NONE)
1748 media_svc_error("strcpy error");
1749 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1751 SAFE_FREE(err_attr_name);
1754 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1755 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1756 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1757 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1758 char time_info[64] = {0, };
1759 char p_value[64] = {0, };
1761 memset(time_info, 0x00, sizeof(time_info));
1762 memset(p_value, 0x00, sizeof(p_value));
1763 strncpy(p_value, p, size);
1764 for (idx = 0; idx < size; idx++) {
1765 if (p_value[idx] == '-') {
1766 time_info[idx] = ':';
1767 } else if (p_value[idx] != '\0') {
1768 time_info[idx] = p_value[idx];
1770 media_svc_error("strcpy error");
1774 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1776 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1779 if (ret != MS_MEDIA_ERR_NONE) {
1780 media_svc_error("strcpy error");
1782 /* This is same as datetaken */
1784 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1786 int datetaken_size = 19;
1787 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1789 if (ret != MS_MEDIA_ERR_NONE)
1790 media_svc_error("strcpy error");
1792 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1793 if (content_info->timeline == 0) {
1794 content_info->timeline = content_info->modified_time;
1796 media_svc_debug("Timeline : %ld", content_info->timeline);
1799 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1801 SAFE_FREE(err_attr_name);
1804 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1805 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1806 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1807 if (ret != MS_MEDIA_ERR_NONE)
1808 media_svc_error("strcpy error");
1809 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1810 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1812 /*media_svc_debug("composer - unknown"); */
1813 SAFE_FREE(err_attr_name);
1816 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1817 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1818 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1819 if (ret != MS_MEDIA_ERR_NONE)
1820 media_svc_error("strcpy error");
1821 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1822 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1824 /*media_svc_debug("copyright - unknown"); */
1825 SAFE_FREE(err_attr_name);
1828 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1829 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1830 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1831 if (ret != MS_MEDIA_ERR_NONE)
1832 media_svc_error("strcpy error");
1834 SAFE_FREE(err_attr_name);
1837 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1838 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1840 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1841 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1842 if (ret != MS_MEDIA_ERR_NONE)
1843 media_svc_error("strcpy error");
1845 media_svc_debug("Wrong Year Information [%s]", p);
1848 SAFE_FREE(err_attr_name);
1851 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1852 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1853 content_info->media_meta.rating = atoi(p);
1855 SAFE_FREE(err_attr_name);
1856 content_info->media_meta.rating = 0;
1859 /*Initialize album_id to 0. below code will set the album_id*/
1860 content_info->album_id = album_id;
1861 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1863 if (ret != MS_MEDIA_ERR_NONE) {
1864 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1865 media_svc_debug("album does not exist. So start to make album art");
1866 extract_thumbnail = TRUE;
1867 append_album = TRUE;
1869 extract_thumbnail = TRUE;
1870 append_album = FALSE;
1873 content_info->album_id = album_id;
1874 append_album = FALSE;
1876 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1877 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1879 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1880 extract_thumbnail = TRUE;
1882 media_svc_debug("album already exists. don't need to make album art");
1883 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1884 extract_thumbnail = TRUE;
1888 /*Do not extract artwork for the USB Storage content*/
1889 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1890 extract_thumbnail = FALSE;
1892 if (extract_thumbnail == TRUE) {
1893 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1894 if (mmf_error != FILEINFO_ERROR_NONE) {
1895 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1896 SAFE_FREE(err_attr_name);
1898 /*media_svc_debug("artwork size1 [%d]", size); */
1901 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1902 if (mmf_error != FILEINFO_ERROR_NONE) {
1903 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1904 SAFE_FREE(err_attr_name);
1906 /*media_svc_debug("artwork size2 [%d]", size); */
1908 if (image != NULL && size > 0) {
1909 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1910 int artwork_mime_size = -1;
1912 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1913 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1914 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1915 if (ret != MS_MEDIA_ERR_NONE) {
1916 media_svc_error("Fail to Get Thumbnail Path");
1918 /* albumart resizing */
1919 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1921 SAFE_FREE(err_attr_name);
1924 if (strlen(thumb_path) > 0) {
1925 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1926 if (ret != MS_MEDIA_ERR_NONE) {
1927 media_svc_error("Fail to Save Thumbnail Image");
1929 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1930 if (ret != MS_MEDIA_ERR_NONE)
1931 media_svc_error("strcpy error");
1937 if (append_album == TRUE) {
1938 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1939 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1940 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1942 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1944 content_info->album_id = album_id;
1947 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1948 double longitude = 0.0;
1949 double latitude = 0.0;
1950 double altitude = 0.0;
1952 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1953 content_info->media_meta.longitude = longitude;
1954 content_info->media_meta.latitude = latitude;
1955 content_info->media_meta.altitude = altitude;
1957 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1958 if (mmf_error != FILEINFO_ERROR_NONE) {
1960 SAFE_FREE(err_attr_name);
1963 media_svc_debug("CDIS : %d", cdis_value);
1966 mmf_error = mm_file_destroy_tag_attrs(tag);
1967 if (mmf_error != FILEINFO_ERROR_NONE) {
1968 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1971 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1972 char *no_tag_title = NULL;
1973 media_svc_error("no tag information");
1975 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1977 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1978 if (ret != MS_MEDIA_ERR_NONE)
1979 media_svc_error("strcpy error");
1980 SAFE_FREE(no_tag_title);
1982 media_svc_error("Can't extract title");
1985 content_info->album_id = album_id;
1988 /*Get Content attribute ===========*/
1989 if (cdis_value == 1) {
1990 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1992 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1995 if (mmf_error == FILEINFO_ERROR_NONE) {
1996 /*Common attribute*/
1997 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1998 if (mmf_error != FILEINFO_ERROR_NONE) {
1999 SAFE_FREE(err_attr_name);
2000 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2002 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2005 /*Sound/Music attribute*/
2006 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2008 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2009 if (mmf_error != FILEINFO_ERROR_NONE) {
2010 SAFE_FREE(err_attr_name);
2011 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2013 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2016 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2017 if (mmf_error != FILEINFO_ERROR_NONE) {
2018 SAFE_FREE(err_attr_name);
2019 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2021 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2024 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2025 if (mmf_error != FILEINFO_ERROR_NONE) {
2026 SAFE_FREE(err_attr_name);
2027 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2029 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2032 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2033 if (mmf_error != FILEINFO_ERROR_NONE) {
2034 SAFE_FREE(err_attr_name);
2035 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2037 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2039 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2040 int audio_bitrate = 0;
2041 int video_bitrate = 0;
2043 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2044 if (mmf_error != FILEINFO_ERROR_NONE) {
2045 SAFE_FREE(err_attr_name);
2046 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2048 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2051 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2052 if (mmf_error != FILEINFO_ERROR_NONE) {
2053 SAFE_FREE(err_attr_name);
2054 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2056 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2059 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2061 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2062 if (mmf_error != FILEINFO_ERROR_NONE) {
2063 SAFE_FREE(err_attr_name);
2064 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2066 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2069 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2070 if (mmf_error != FILEINFO_ERROR_NONE) {
2071 SAFE_FREE(err_attr_name);
2072 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2074 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2078 media_svc_error("Not support type");
2079 mmf_error = mm_file_destroy_content_attrs(content);
2080 if (mmf_error != FILEINFO_ERROR_NONE) {
2081 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2083 return MS_MEDIA_ERR_INVALID_PARAMETER;
2086 mmf_error = mm_file_destroy_content_attrs(content);
2087 if (mmf_error != FILEINFO_ERROR_NONE) {
2088 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2091 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2094 return MS_MEDIA_ERR_NONE;
2097 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2099 media_svc_retm_if(content_info == NULL, "content info is NULL");
2101 /* Delete media_svc_content_info_s */
2102 SAFE_FREE(content_info->media_uuid);
2103 SAFE_FREE(content_info->path);
2104 SAFE_FREE(content_info->file_name);
2105 SAFE_FREE(content_info->mime_type);
2106 SAFE_FREE(content_info->folder_uuid);
2107 SAFE_FREE(content_info->thumbnail_path);
2108 SAFE_FREE(content_info->storage_uuid);
2110 /* Delete media_svc_content_meta_s */
2111 SAFE_FREE(content_info->media_meta.title);
2112 SAFE_FREE(content_info->media_meta.album);
2113 SAFE_FREE(content_info->media_meta.artist);
2114 SAFE_FREE(content_info->media_meta.album_artist);
2115 SAFE_FREE(content_info->media_meta.genre);
2116 SAFE_FREE(content_info->media_meta.composer);
2117 SAFE_FREE(content_info->media_meta.year);
2118 SAFE_FREE(content_info->media_meta.recorded_date);
2119 SAFE_FREE(content_info->media_meta.copyright);
2120 SAFE_FREE(content_info->media_meta.track_num);
2121 SAFE_FREE(content_info->media_meta.description);
2122 SAFE_FREE(content_info->media_meta.datetaken);
2123 SAFE_FREE(content_info->media_meta.exposure_time);
2124 SAFE_FREE(content_info->media_meta.model);
2125 SAFE_FREE(content_info->media_meta.weather);
2126 SAFE_FREE(content_info->media_meta.category);
2127 SAFE_FREE(content_info->media_meta.keyword);
2128 SAFE_FREE(content_info->media_meta.location_tag);
2129 SAFE_FREE(content_info->media_meta.content_name);
2130 SAFE_FREE(content_info->media_meta.age_rating);
2131 SAFE_FREE(content_info->media_meta.author);
2132 SAFE_FREE(content_info->media_meta.provider);
2134 SAFE_FREE(content_info->media_meta.title_pinyin);
2135 SAFE_FREE(content_info->media_meta.album_pinyin);
2136 SAFE_FREE(content_info->media_meta.artist_pinyin);
2137 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2138 SAFE_FREE(content_info->media_meta.genre_pinyin);
2139 SAFE_FREE(content_info->media_meta.composer_pinyin);
2140 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2141 SAFE_FREE(content_info->media_meta.description_pinyin);
2146 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2148 if (STRING_VALID(path)) {
2149 char *internal_path = _media_svc_get_path(uid);
2150 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2151 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2152 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2153 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2154 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2155 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2157 media_svc_error("Invalid Path");
2158 SAFE_FREE(internal_path);
2159 return MS_MEDIA_ERR_INVALID_PARAMETER;
2161 SAFE_FREE(internal_path);
2163 media_svc_error("INVALID parameter");
2164 return MS_MEDIA_ERR_INVALID_PARAMETER;
2167 return MS_MEDIA_ERR_NONE;
2170 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2173 size_t i, count = 0;
2174 size_t oldlen = strlen(olds);
2175 if (oldlen < 1) return s;
2176 size_t newlen = strlen(news);
2178 if (newlen != oldlen) {
2179 for (i = 0; s[i] != '\0';) {
2180 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2183 } else i = strlen(s);
2186 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2187 if (result == NULL) return NULL;
2191 if (memcmp(s, olds, oldlen) == 0) {
2192 memcpy(sr, news, newlen);
2195 } else *sr++ = *s++;
2203 bool _media_svc_is_drm_file(const char *path)
2208 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)
2210 int ret = MS_MEDIA_ERR_NONE;
2212 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2214 if (ret != MS_MEDIA_ERR_NONE) {
2215 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2216 ret = MS_MEDIA_ERR_INTERNAL;
2218 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2224 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2226 int ret = MS_MEDIA_ERR_NONE;
2228 pinyin_name_s *pinyinname = NULL;
2232 if (!STRING_VALID(src_str)) {
2233 media_svc_debug("String is invalid");
2237 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2238 if (ret == MS_MEDIA_ERR_NONE) {
2239 if (STRING_VALID(pinyinname[0].pinyin_name))
2240 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2242 *pinyin_str = strdup(src_str); //Return Original Non China Character
2245 _media_svc_pinyin_free(pinyinname, size);
2250 bool _media_svc_check_pinyin_support(void)
2256 int _media_svc_get_ini_value()
2258 if (g_ini_value == -1) {
2259 dictionary *dict = NULL;
2261 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2263 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2267 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2268 iniparser_freedict(dict);
2270 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2275 char* _media_svc_get_title_from_path(const char *path)
2277 char *filename = NULL;
2280 int filename_len = -1;
2281 int new_title_len = -1;
2284 media_svc_error("path is NULL");
2288 filename = g_path_get_basename(path);
2289 if (!STRING_VALID(filename)) {
2290 media_svc_error("wrong file name");
2291 SAFE_FREE(filename);
2295 filename_len = strlen(filename);
2297 ext = g_strrstr(filename, ".");
2299 media_svc_error("there is no file extention");
2303 new_title_len = filename_len - strlen(ext);
2304 if (new_title_len < 1) {
2305 media_svc_error("title length is zero");
2306 SAFE_FREE(filename);
2310 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2312 SAFE_FREE(filename);
2314 media_svc_debug("extract title is [%s]", title);
2319 #define BUF_LENGHT 256
2321 void _media_svc_print_stderror(void)
2323 char buf[BUF_LENGHT] = {0,};
2325 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));