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_xmp[2];
1241 long exif_app1_xmp_size = 0;
1242 unsigned char exif_app1_xmp_t[2];
1249 fp = fopen(path, "rb");
1253 size = fread(exif_header, 1, sizeof (exif_header), fp);
1257 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1258 size = fread(exif_app1, 1, sizeof (exif_app1), fp);
1262 size1 = exif_app1[0];
1263 size2 = exif_app1[1];
1265 app1_size = size1 * 256 + size2 - 2;
1267 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1270 size = fread(exif_app1_xmp, 1, sizeof (exif_app1_xmp), fp);
1274 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1277 size = fread(exif_app1_xmp_t, 1, sizeof (exif_app1_xmp_t), fp);
1281 size1 = exif_app1_xmp_t[0];
1282 size2 = exif_app1_xmp_t[1];
1284 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1286 xmp_data = (char *)malloc(exif_app1_xmp_size);
1287 memset(xmp_data, 0x00, exif_app1_xmp_size);
1289 while (exif_app1_xmp_size >= 0) {
1290 exif_app1_xmp_size--;
1300 if(strstr(ptr, "UsePanoramaViewer")
1301 && strstr(ptr, "True")
1302 && strstr(ptr, "ProjectionType")
1303 && strstr(ptr, "equirectangular"))
1306 SAFE_FREE(xmp_data);
1326 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1328 int ret = MS_MEDIA_ERR_NONE;
1330 int orient_value = 0;
1332 int exif_height = 0;
1333 ExifData *ed = NULL;
1334 int has_datetaken = FALSE;
1335 int datetaken_size = 19;
1336 double fnumber = 0.0;
1340 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1341 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1342 char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1343 char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1345 memset(buf, 0x00, sizeof(buf));
1346 memset(description_buf, 0x00, sizeof(description_buf));
1347 memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1348 memset(model_buf, 0x00, sizeof(model_buf));
1350 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1351 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1352 return MS_MEDIA_ERR_INVALID_PARAMETER;
1355 path = content_info->path;
1356 if (!STRING_VALID(path)) {
1357 media_svc_error("Invalid Path");
1358 return MS_MEDIA_ERR_INVALID_PARAMETER;
1361 /* Load an ExifData object from an EXIF file */
1362 ed = exif_data_new_from_file(path);
1365 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1366 goto GET_WIDTH_HEIGHT;
1369 content_info->media_meta.is_360 = image_360_check(path);
1371 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1372 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1373 if (strlen(buf) > 0) {
1374 if (strcmp(buf, "S") == 0) {
1378 content_info->media_meta.latitude = value;
1380 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1383 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1386 memset(buf, 0x00, sizeof(buf));
1388 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1389 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1390 if (strlen(buf) > 0) {
1391 if (strcmp(buf, "W") == 0) {
1395 content_info->media_meta.longitude = value;
1397 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1400 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1403 memset(buf, 0x00, sizeof(buf));
1405 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1406 if (strlen(description_buf) == 0) {
1407 /*media_svc_debug("Use 'No description'"); */
1408 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1409 if (ret != MS_MEDIA_ERR_NONE)
1410 media_svc_error("strcpy error");
1412 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1413 if (ret != MS_MEDIA_ERR_NONE)
1414 media_svc_error("strcpy error");
1417 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1418 if (ret != MS_MEDIA_ERR_NONE)
1419 media_svc_error("strcpy error");
1422 memset(buf, 0x00, sizeof(buf));
1424 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1425 if (strlen(buf) == 0) {
1426 /*media_svc_debug("time is NULL"); */
1428 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1429 if (ret != MS_MEDIA_ERR_NONE) {
1430 media_svc_error("strcpy error");
1432 has_datetaken = TRUE;
1433 /* This is same as recorded_date */
1434 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1435 if (ret != MS_MEDIA_ERR_NONE)
1436 media_svc_error("strcpy error");
1441 memset(buf, 0x00, sizeof(buf));
1443 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1444 if (strlen(buf) == 0) {
1445 /*media_svc_debug("time is NULL"); */
1447 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1448 if (ret != MS_MEDIA_ERR_NONE) {
1449 media_svc_error("strcpy error");
1451 has_datetaken = TRUE;
1452 /* This is same as recorded_date */
1453 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1454 if (ret != MS_MEDIA_ERR_NONE)
1455 media_svc_error("strcpy error");
1460 if (has_datetaken) {
1461 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1462 if (content_info->timeline == 0) {
1463 content_info->timeline = content_info->modified_time;
1465 media_svc_debug("Timeline : %ld", content_info->timeline);
1469 /* Get exposure_time value from exif. */
1470 if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1471 if (strlen(exposure_time_buf) == 0) {
1472 //media_svc_debug("exposure_time_buf is NULL");
1474 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1475 if (ret != MS_MEDIA_ERR_NONE)
1476 media_svc_error("strcpy error");
1480 /* Get fnumber value from exif. */
1481 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1482 if (fnumber > 0.0) {
1483 content_info->media_meta.fnumber = fnumber;
1485 content_info->media_meta.fnumber = 0.0;
1488 content_info->media_meta.fnumber = 0.0;
1491 /* Get iso value from exif. */
1492 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1494 content_info->media_meta.iso = iso;
1496 content_info->media_meta.iso = 0;
1499 content_info->media_meta.iso = 0;
1502 /* Get model value from exif. */
1503 if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1504 if (strlen(model_buf) == 0) {
1505 //media_svc_debug("model_buf is NULL");
1507 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1508 if (ret != MS_MEDIA_ERR_NONE)
1509 media_svc_error("strcpy error");
1513 /* Get orientation value from exif. */
1514 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1515 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
1516 content_info->media_meta.orientation = orient_value;
1518 content_info->media_meta.orientation = 0;
1521 content_info->media_meta.orientation = 0;
1524 /* Get width value from exif. */
1525 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1526 if (exif_width > 0) {
1527 content_info->media_meta.width = exif_width;
1529 content_info->media_meta.width = 0;
1532 content_info->media_meta.width = 0;
1535 /* Get height value from exif. */
1536 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1537 if (exif_height > 0) {
1538 content_info->media_meta.height = exif_height;
1540 content_info->media_meta.height = 0;
1543 content_info->media_meta.height = 0;
1546 if (ed != NULL) exif_data_unref(ed);
1550 if (content_info->media_meta.width == 0 ||
1551 content_info->media_meta.height == 0) {
1552 /*Get image width, height*/
1553 unsigned int img_width = 0;
1554 unsigned int img_height = 0;
1555 ImgCodecType img_type = IMG_CODEC_NONE;
1557 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1559 if (content_info->media_meta.width == 0)
1560 content_info->media_meta.width = img_width;
1562 if (content_info->media_meta.height == 0)
1563 content_info->media_meta.height = img_height;
1566 return MS_MEDIA_ERR_NONE;
1569 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1571 MMHandleType tag = 0;
1574 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1575 int mmf_error = FILEINFO_ERROR_NONE;
1576 char *err_attr_name = NULL;
1579 int ret = MS_MEDIA_ERR_NONE;
1581 /*Get Content Tag attribute ===========*/
1582 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1584 if (mmf_error == FILEINFO_ERROR_NONE) {
1585 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1586 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1587 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1588 if (ret != MS_MEDIA_ERR_NONE)
1589 media_svc_error("strcpy error");
1591 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1593 SAFE_FREE(err_attr_name);
1594 /*media_svc_debug("album - unknown"); */
1597 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1598 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1599 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1600 if (ret != MS_MEDIA_ERR_NONE)
1601 media_svc_error("strcpy error");
1602 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1604 SAFE_FREE(err_attr_name);
1605 /*media_svc_debug("artist - unknown"); */
1608 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1609 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1610 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1611 if (ret != MS_MEDIA_ERR_NONE)
1612 media_svc_error("strcpy error");
1613 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1615 SAFE_FREE(err_attr_name);
1616 /*media_svc_debug("album_artist - unknown"); */
1619 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1620 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1621 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1622 if (ret != MS_MEDIA_ERR_NONE)
1623 media_svc_error("strcpy error");
1625 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1626 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1628 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1629 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1633 SAFE_FREE(err_attr_name);
1634 /*media_svc_debug("genre - unknown"); */
1637 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1638 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1640 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1641 if (ret != MS_MEDIA_ERR_NONE)
1642 media_svc_error("strcpy error");
1644 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1648 for (idx = 0; idx < size; idx++) {
1650 media_svc_debug("SPACE [%s]", p);
1654 media_svc_debug("Not SPACE [%s]", p);
1655 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1656 if (ret != MS_MEDIA_ERR_NONE)
1657 media_svc_error("strcpy error");
1663 media_svc_debug("Can't extract title. All string is space");
1664 title = _media_svc_get_title_from_filepath(content_info->path);
1666 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1667 if (ret != MS_MEDIA_ERR_NONE)
1668 media_svc_error("strcpy error");
1671 media_svc_error("Can't extract title");
1676 SAFE_FREE(err_attr_name);
1677 title = _media_svc_get_title_from_filepath(content_info->path);
1679 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1680 if (ret != MS_MEDIA_ERR_NONE)
1681 media_svc_error("strcpy error");
1684 media_svc_error("Can't extract title");
1688 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1689 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1690 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1691 if (ret != MS_MEDIA_ERR_NONE)
1692 media_svc_error("strcpy error");
1693 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1695 SAFE_FREE(err_attr_name);
1698 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1699 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1700 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1701 if (ret != MS_MEDIA_ERR_NONE)
1702 media_svc_error("strcpy error");
1703 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1704 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1706 /*media_svc_debug("composer - unknown"); */
1707 SAFE_FREE(err_attr_name);
1710 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1711 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1712 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1713 if (ret != MS_MEDIA_ERR_NONE)
1714 media_svc_error("strcpy error");
1715 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1716 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1718 /*media_svc_debug("copyright - unknown"); */
1719 SAFE_FREE(err_attr_name);
1722 mmf_error = mm_file_destroy_tag_attrs(tag);
1723 if (mmf_error != FILEINFO_ERROR_NONE) {
1724 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1727 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1728 char *no_tag_title = NULL;
1729 media_svc_error("no tag information");
1731 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1733 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1734 if (ret != MS_MEDIA_ERR_NONE)
1735 media_svc_error("strcpy error");
1736 SAFE_FREE(no_tag_title);
1738 media_svc_error("Can't extract title");
1741 content_info->album_id = album_id;
1744 return MS_MEDIA_ERR_NONE;
1747 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1749 MMHandleType content = 0;
1750 MMHandleType tag = 0;
1752 unsigned char *image = NULL;
1753 unsigned int size = 0;
1754 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1755 int mmf_error = FILEINFO_ERROR_NONE;
1756 char *err_attr_name = NULL;
1758 bool extract_thumbnail = FALSE;
1759 bool append_album = FALSE;
1761 int ret = MS_MEDIA_ERR_NONE;
1763 unsigned int resize_size = 0;
1764 unsigned char *resize_image = NULL;
1766 /*Get Content Tag attribute ===========*/
1767 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1769 if (mmf_error == FILEINFO_ERROR_NONE) {
1770 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1771 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1772 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1773 if (ret != MS_MEDIA_ERR_NONE)
1774 media_svc_error("strcpy error");
1776 /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1778 SAFE_FREE(err_attr_name);
1779 /*media_svc_debug("album - unknown"); */
1782 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1783 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1784 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1785 if (ret != MS_MEDIA_ERR_NONE)
1786 media_svc_error("strcpy error");
1787 /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1789 SAFE_FREE(err_attr_name);
1790 /*media_svc_debug("artist - unknown"); */
1793 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1794 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1795 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1796 if (ret != MS_MEDIA_ERR_NONE)
1797 media_svc_error("strcpy error");
1798 /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1800 SAFE_FREE(err_attr_name);
1801 /*media_svc_debug("album_artist - unknown"); */
1804 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1805 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1806 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1807 if (ret != MS_MEDIA_ERR_NONE)
1808 media_svc_error("strcpy error");
1810 /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1811 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1813 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1814 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1818 SAFE_FREE(err_attr_name);
1819 /*media_svc_debug("genre - unknown"); */
1822 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1823 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1825 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1826 if (ret != MS_MEDIA_ERR_NONE)
1827 media_svc_error("strcpy error");
1829 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1833 for (idx = 0; idx < size; idx++) {
1835 media_svc_debug("SPACE [%s]", p);
1839 media_svc_debug("Not SPACE [%s]", p);
1840 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1841 if (ret != MS_MEDIA_ERR_NONE)
1842 media_svc_error("strcpy error");
1848 media_svc_debug("Can't extract title. All string is space");
1849 title = _media_svc_get_title_from_filepath(content_info->path);
1851 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1852 if (ret != MS_MEDIA_ERR_NONE)
1853 media_svc_error("strcpy error");
1856 media_svc_error("Can't extract title");
1861 SAFE_FREE(err_attr_name);
1862 title = _media_svc_get_title_from_filepath(content_info->path);
1864 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1865 if (ret != MS_MEDIA_ERR_NONE)
1866 media_svc_error("strcpy error");
1869 media_svc_error("Can't extract title");
1873 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1874 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1875 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1876 if (ret != MS_MEDIA_ERR_NONE)
1877 media_svc_error("strcpy error");
1878 /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1880 SAFE_FREE(err_attr_name);
1883 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1885 if (mmf_error != FILEINFO_ERROR_NONE) {
1886 SAFE_FREE(err_attr_name);
1889 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1890 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1891 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1892 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1893 char time_info[64] = {0, };
1894 char p_value[64] = {0, };
1896 memset(time_info, 0x00, sizeof(time_info));
1897 memset(p_value, 0x00, sizeof(p_value));
1898 strncpy(p_value, p, size);
1899 for (idx = 0; idx < size; idx++) {
1900 if (p_value[idx] == '-') {
1901 time_info[idx] = ':';
1902 } else if (p_value[idx] != '\0') {
1903 time_info[idx] = p_value[idx];
1905 media_svc_error("strcpy error");
1909 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1911 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1914 if (ret != MS_MEDIA_ERR_NONE) {
1915 media_svc_error("strcpy error");
1917 /* This is same as datetaken */
1919 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1921 int datetaken_size = 19;
1922 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1924 if (ret != MS_MEDIA_ERR_NONE)
1925 media_svc_error("strcpy error");
1927 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1928 if (content_info->timeline == 0) {
1929 content_info->timeline = content_info->modified_time;
1931 media_svc_debug("Timeline : %ld", content_info->timeline);
1934 /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1936 SAFE_FREE(err_attr_name);
1939 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1940 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1941 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1942 if (ret != MS_MEDIA_ERR_NONE)
1943 media_svc_error("strcpy error");
1944 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1945 /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1947 /*media_svc_debug("composer - unknown"); */
1948 SAFE_FREE(err_attr_name);
1951 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1952 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1953 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1954 if (ret != MS_MEDIA_ERR_NONE)
1955 media_svc_error("strcpy error");
1956 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1957 /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1959 /*media_svc_debug("copyright - unknown"); */
1960 SAFE_FREE(err_attr_name);
1963 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1964 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1965 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1966 if (ret != MS_MEDIA_ERR_NONE)
1967 media_svc_error("strcpy error");
1969 SAFE_FREE(err_attr_name);
1972 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1973 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1975 if ((p != NULL) && (sscanf(p, "%d", &year))) {
1976 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1977 if (ret != MS_MEDIA_ERR_NONE)
1978 media_svc_error("strcpy error");
1980 media_svc_debug("Wrong Year Information [%s]", p);
1983 SAFE_FREE(err_attr_name);
1986 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1987 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1988 content_info->media_meta.rating = atoi(p);
1990 SAFE_FREE(err_attr_name);
1991 content_info->media_meta.rating = 0;
1994 /*Initialize album_id to 0. below code will set the album_id*/
1995 content_info->album_id = album_id;
1996 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1998 if (ret != MS_MEDIA_ERR_NONE) {
1999 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2000 media_svc_debug("album does not exist. So start to make album art");
2001 extract_thumbnail = TRUE;
2002 append_album = TRUE;
2004 extract_thumbnail = TRUE;
2005 append_album = FALSE;
2008 content_info->album_id = album_id;
2009 append_album = FALSE;
2011 if ((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
2012 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
2014 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2015 extract_thumbnail = TRUE;
2017 media_svc_debug("album already exists. don't need to make album art");
2018 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2019 extract_thumbnail = TRUE;
2023 /*Do not extract artwork for the USB Storage content*/
2024 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2025 extract_thumbnail = FALSE;
2027 if (extract_thumbnail == TRUE) {
2028 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2029 if (mmf_error != FILEINFO_ERROR_NONE) {
2030 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2031 SAFE_FREE(err_attr_name);
2033 /*media_svc_debug("artwork size1 [%d]", size); */
2036 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2037 if (mmf_error != FILEINFO_ERROR_NONE) {
2038 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2039 SAFE_FREE(err_attr_name);
2041 /*media_svc_debug("artwork size2 [%d]", size); */
2043 if (image != NULL && size > 0) {
2044 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2045 int artwork_mime_size = -1;
2047 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2048 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2049 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2050 if (ret != MS_MEDIA_ERR_NONE) {
2051 media_svc_error("Fail to Get Thumbnail Path");
2053 /* albumart resizing */
2054 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2056 SAFE_FREE(err_attr_name);
2059 if (strlen(thumb_path) > 0) {
2060 ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2061 if (ret != MS_MEDIA_ERR_NONE) {
2062 media_svc_error("Fail to Save Thumbnail Image");
2064 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2065 if (ret != MS_MEDIA_ERR_NONE)
2066 media_svc_error("strcpy error");
2072 if (append_album == TRUE) {
2073 if ((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
2074 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
2075 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2077 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2079 content_info->album_id = album_id;
2082 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2083 double longitude = 0.0;
2084 double latitude = 0.0;
2085 double altitude = 0.0;
2087 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2088 content_info->media_meta.longitude = longitude;
2089 content_info->media_meta.latitude = latitude;
2090 content_info->media_meta.altitude = altitude;
2092 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2093 if (mmf_error != FILEINFO_ERROR_NONE) {
2095 SAFE_FREE(err_attr_name);
2098 media_svc_debug("CDIS : %d", cdis_value);
2101 mmf_error = mm_file_destroy_tag_attrs(tag);
2102 if (mmf_error != FILEINFO_ERROR_NONE) {
2103 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2106 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2107 char *no_tag_title = NULL;
2108 media_svc_error("no tag information");
2110 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2112 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2113 if (ret != MS_MEDIA_ERR_NONE)
2114 media_svc_error("strcpy error");
2115 SAFE_FREE(no_tag_title);
2117 media_svc_error("Can't extract title");
2120 content_info->album_id = album_id;
2123 /*Get Content attribute ===========*/
2124 if (cdis_value == 1) {
2125 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2127 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2130 if (mmf_error == FILEINFO_ERROR_NONE) {
2131 /*Common attribute*/
2132 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2133 if (mmf_error != FILEINFO_ERROR_NONE) {
2134 SAFE_FREE(err_attr_name);
2135 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2137 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2140 /*Sound/Music attribute*/
2141 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2143 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2144 if (mmf_error != FILEINFO_ERROR_NONE) {
2145 SAFE_FREE(err_attr_name);
2146 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2148 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2151 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2152 if (mmf_error != FILEINFO_ERROR_NONE) {
2153 SAFE_FREE(err_attr_name);
2154 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2156 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2159 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2160 if (mmf_error != FILEINFO_ERROR_NONE) {
2161 SAFE_FREE(err_attr_name);
2162 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2164 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2167 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2168 if (mmf_error != FILEINFO_ERROR_NONE) {
2169 SAFE_FREE(err_attr_name);
2170 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2172 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2174 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
2175 int audio_bitrate = 0;
2176 int video_bitrate = 0;
2178 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2179 if (mmf_error != FILEINFO_ERROR_NONE) {
2180 SAFE_FREE(err_attr_name);
2181 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2183 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2186 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2187 if (mmf_error != FILEINFO_ERROR_NONE) {
2188 SAFE_FREE(err_attr_name);
2189 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2191 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2194 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2196 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2197 if (mmf_error != FILEINFO_ERROR_NONE) {
2198 SAFE_FREE(err_attr_name);
2199 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2201 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2204 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2205 if (mmf_error != FILEINFO_ERROR_NONE) {
2206 SAFE_FREE(err_attr_name);
2207 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2209 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2213 media_svc_error("Not support type");
2214 mmf_error = mm_file_destroy_content_attrs(content);
2215 if (mmf_error != FILEINFO_ERROR_NONE) {
2216 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2218 return MS_MEDIA_ERR_INVALID_PARAMETER;
2221 mmf_error = mm_file_destroy_content_attrs(content);
2222 if (mmf_error != FILEINFO_ERROR_NONE) {
2223 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2226 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2229 return MS_MEDIA_ERR_NONE;
2232 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2234 media_svc_retm_if(content_info == NULL, "content info is NULL");
2236 /* Delete media_svc_content_info_s */
2237 SAFE_FREE(content_info->media_uuid);
2238 SAFE_FREE(content_info->path);
2239 SAFE_FREE(content_info->file_name);
2240 SAFE_FREE(content_info->mime_type);
2241 SAFE_FREE(content_info->folder_uuid);
2242 SAFE_FREE(content_info->thumbnail_path);
2243 SAFE_FREE(content_info->storage_uuid);
2245 /* Delete media_svc_content_meta_s */
2246 SAFE_FREE(content_info->media_meta.title);
2247 SAFE_FREE(content_info->media_meta.album);
2248 SAFE_FREE(content_info->media_meta.artist);
2249 SAFE_FREE(content_info->media_meta.album_artist);
2250 SAFE_FREE(content_info->media_meta.genre);
2251 SAFE_FREE(content_info->media_meta.composer);
2252 SAFE_FREE(content_info->media_meta.year);
2253 SAFE_FREE(content_info->media_meta.recorded_date);
2254 SAFE_FREE(content_info->media_meta.copyright);
2255 SAFE_FREE(content_info->media_meta.track_num);
2256 SAFE_FREE(content_info->media_meta.description);
2257 SAFE_FREE(content_info->media_meta.datetaken);
2258 SAFE_FREE(content_info->media_meta.exposure_time);
2259 SAFE_FREE(content_info->media_meta.model);
2260 SAFE_FREE(content_info->media_meta.weather);
2261 SAFE_FREE(content_info->media_meta.category);
2262 SAFE_FREE(content_info->media_meta.keyword);
2263 SAFE_FREE(content_info->media_meta.location_tag);
2264 SAFE_FREE(content_info->media_meta.content_name);
2265 SAFE_FREE(content_info->media_meta.age_rating);
2266 SAFE_FREE(content_info->media_meta.author);
2267 SAFE_FREE(content_info->media_meta.provider);
2269 SAFE_FREE(content_info->media_meta.title_pinyin);
2270 SAFE_FREE(content_info->media_meta.album_pinyin);
2271 SAFE_FREE(content_info->media_meta.artist_pinyin);
2272 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2273 SAFE_FREE(content_info->media_meta.genre_pinyin);
2274 SAFE_FREE(content_info->media_meta.composer_pinyin);
2275 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2276 SAFE_FREE(content_info->media_meta.description_pinyin);
2281 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2283 if (STRING_VALID(path)) {
2284 char *internal_path = _media_svc_get_path(uid);
2285 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2286 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2287 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2288 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2289 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2290 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2292 media_svc_error("Invalid Path");
2293 SAFE_FREE(internal_path);
2294 return MS_MEDIA_ERR_INVALID_PARAMETER;
2296 SAFE_FREE(internal_path);
2298 media_svc_error("INVALID parameter");
2299 return MS_MEDIA_ERR_INVALID_PARAMETER;
2302 return MS_MEDIA_ERR_NONE;
2305 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2308 size_t i, count = 0;
2309 size_t oldlen = strlen(olds);
2310 if (oldlen < 1) return s;
2311 size_t newlen = strlen(news);
2313 if (newlen != oldlen) {
2314 for (i = 0; s[i] != '\0';) {
2315 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
2318 } else i = strlen(s);
2321 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
2322 if (result == NULL) return NULL;
2326 if (memcmp(s, olds, oldlen) == 0) {
2327 memcpy(sr, news, newlen);
2330 } else *sr++ = *s++;
2338 bool _media_svc_is_drm_file(const char *path)
2343 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)
2345 int ret = MS_MEDIA_ERR_NONE;
2347 ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2349 if (ret != MS_MEDIA_ERR_NONE) {
2350 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2351 ret = MS_MEDIA_ERR_INTERNAL;
2353 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2359 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2361 int ret = MS_MEDIA_ERR_NONE;
2363 pinyin_name_s *pinyinname = NULL;
2367 if (!STRING_VALID(src_str)) {
2368 media_svc_debug("String is invalid");
2372 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2373 if (ret == MS_MEDIA_ERR_NONE) {
2374 if (STRING_VALID(pinyinname[0].pinyin_name))
2375 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2377 *pinyin_str = strdup(src_str); //Return Original Non China Character
2380 _media_svc_pinyin_free(pinyinname, size);
2385 bool _media_svc_check_pinyin_support(void)
2391 int _media_svc_get_ini_value()
2393 if (g_ini_value == -1) {
2394 dictionary *dict = NULL;
2396 dict = iniparser_load(MEDIA_SVC_INI_DEFAULT_PATH);
2398 media_svc_error("%s load failed", MEDIA_SVC_INI_DEFAULT_PATH);
2402 MEDIA_SVC_INI_GET_INT(dict, "media-content-config:thumbnail_activation", g_ini_value, 0);
2403 iniparser_freedict(dict);
2405 media_svc_debug("Thumb-server activation level = %d", g_ini_value);
2410 char* _media_svc_get_title_from_path(const char *path)
2412 char *filename = NULL;
2415 int filename_len = -1;
2416 int new_title_len = -1;
2419 media_svc_error("path is NULL");
2423 filename = g_path_get_basename(path);
2424 if (!STRING_VALID(filename)) {
2425 media_svc_error("wrong file name");
2426 SAFE_FREE(filename);
2430 filename_len = strlen(filename);
2432 ext = g_strrstr(filename, ".");
2434 media_svc_error("there is no file extention");
2438 new_title_len = filename_len - strlen(ext);
2439 if (new_title_len < 1) {
2440 media_svc_error("title length is zero");
2441 SAFE_FREE(filename);
2445 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2447 SAFE_FREE(filename);
2449 media_svc_debug("extract title is [%s]", title);
2454 #define BUF_LENGHT 256
2456 void _media_svc_print_stderror(void)
2458 char buf[BUF_LENGHT] = {0,};
2460 media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));