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>
40 #include <libexif/exif-data.h>
41 #include <media-thumbnail.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <img-codec-parser.h>
45 #include <image_util.h>
46 #include <image_util_internal.h>
47 #include "media-util-err.h"
48 #include "media-svc-util.h"
49 #include "media-svc-db-utils.h"
50 #include "media-svc-debug.h"
51 #include "media-svc-env.h"
52 #include "media-svc-hash.h"
53 #include "media-svc-album.h"
54 #include "media-svc-localize-utils.h"
55 #include "media-svc-localize_ch.h"
57 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
59 /* Define data structures for media type and mime type */
60 #define MEDIA_SVC_CATEGORY_UNKNOWN 0x00000000 /**< Default */
61 #define MEDIA_SVC_CATEGORY_ETC 0x00000001 /**< ETC category */
62 #define MEDIA_SVC_CATEGORY_IMAGE 0x00000002 /**< Image category */
63 #define MEDIA_SVC_CATEGORY_VIDEO 0x00000004 /**< Video category */
64 #define MEDIA_SVC_CATEGORY_MUSIC 0x00000008 /**< Music category */
65 #define MEDIA_SVC_CATEGORY_SOUND 0x00000010 /**< Sound category */
66 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
67 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
68 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA 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_ARTWORK_SIZE 2000
81 char content_type[15];
83 } _media_svc_content_table_s;
85 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
86 {"audio", MEDIA_SVC_CATEGORY_SOUND},
87 {"image", MEDIA_SVC_CATEGORY_IMAGE},
88 {"video", MEDIA_SVC_CATEGORY_VIDEO},
89 {"application", MEDIA_SVC_CATEGORY_ETC},
90 {"text", MEDIA_SVC_CATEGORY_ETC},
93 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
94 /*known mime types of normal files*/
100 /* known mime types of drm files*/
102 "x-mp3", /*alias of audio/mpeg*/
103 "x-mpeg", /*alias of audio/mpeg*/
105 "x-ogg", /*alias of audio/ogg*/
106 "vnd.ms-playready.media.pya:*.pya", /*playready*/
109 "x-m4a", /*alias of audio/mp4*/
110 /* below mimes are rare*/
120 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
121 "vorbis", /*alias of audio/x-vorbis+ogg*/
123 "x-mp2", /*alias of audio/mp2*/
124 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
125 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
129 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
134 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
135 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
136 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
137 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
138 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
139 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
140 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
141 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
142 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
143 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
144 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
145 MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
146 } media_svc_extracted_field_e;
148 char *_media_info_generate_uuid(void)
151 static char uuid_unparsed[37];
154 uuid_generate(uuid_value);
155 uuid_unparse(uuid_value, uuid_unparsed);
157 if (strlen(uuid_unparsed) < 36) {
158 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
162 return uuid_unparsed;
165 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
167 if (!x_src || strlen(x_src) == 0) {
168 media_svc_error("x_src is NULL");
173 media_svc_error("length is Wrong");
177 strncpy(x_dst, x_src, max_len - 1);
178 x_dst[max_len - 1] = '\0';
181 static int __media_svc_split_to_double(char *input, double *arr)
183 char tmp_arr[255] = {0, };
184 int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
186 if (!STRING_VALID(input)) {
187 media_svc_error("Invalid parameter");
188 return MS_MEDIA_ERR_INVALID_PARAMETER;
190 memset(tmp_arr, 0x0, sizeof(tmp_arr));
192 /*media_svc_debug("input: [%s]", input); */
196 for (idx = 0; idx < (len + 1); idx++) {
197 if (input[idx] == ' ') {
199 } else if ((input[idx] == ',') || (idx == len)) {
200 arr[arr_idx] = atof(tmp_arr);
203 /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
204 memset(tmp_arr, 0x0, sizeof(tmp_arr));
206 tmp_arr[str_idx] = input[idx];
212 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
213 return MS_MEDIA_ERR_INTERNAL;
216 return MS_MEDIA_ERR_NONE;
219 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
225 return MS_MEDIA_ERR_INVALID_PARAMETER;
229 entry = exif_data_get_entry(ed, tag);
231 /* Get the contents of the tag in human-readable form */
232 if (tag == EXIF_TAG_ORIENTATION ||
233 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
234 tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
235 tag == EXIF_TAG_ISO_SPEED_RATINGS) {
237 if (i_value == NULL) {
238 media_svc_debug("i_value is NULL");
239 return MS_MEDIA_ERR_INVALID_PARAMETER;
242 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
243 short exif_value = exif_get_short(entry->data, mByteOrder);
244 *i_value = (int)exif_value;
246 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
248 if (d_value == NULL) {
249 media_svc_debug("d_value is NULL");
250 return MS_MEDIA_ERR_INVALID_PARAMETER;
253 /* Get the contents of the tag in human-readable form */
254 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
255 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
256 gps_buf[strlen(gps_buf)] = '\0';
257 int ret = MS_MEDIA_ERR_NONE;
259 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
261 ret = __media_svc_split_to_double(gps_buf, tmp_arr);
262 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
264 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
265 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
268 media_svc_debug("buf is NULL");
269 return MS_MEDIA_ERR_INVALID_PARAMETER;
272 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
273 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
274 long numerator = mRational.numerator;
275 long denominator = mRational.denominator;
276 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
278 } else if (tag == EXIF_TAG_FNUMBER) {
280 if (d_value == NULL) {
281 media_svc_debug("d_value is NULL");
282 return MS_MEDIA_ERR_INVALID_PARAMETER;
285 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
286 ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
287 long numerator = mRational.numerator;
288 long denominator = mRational.denominator;
290 *d_value = ((numerator*1.0)/(denominator*1.0));
295 media_svc_debug("buf is NULL");
296 return MS_MEDIA_ERR_INVALID_PARAMETER;
299 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
300 buf[strlen(buf)] = '\0';
304 return MS_MEDIA_ERR_NONE;
307 time_t __media_svc_get_timeline_from_str(const char *timstr)
310 time_t modified_t = 0;
314 if (!STRING_VALID(timstr)) {
315 media_svc_error("Invalid Parameter");
319 /*Exif Format : %Y:%m:%d %H:%M:%S
320 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
321 memset(&t, 0x00, sizeof(struct tm));
325 localtime_r(&rawtime, &timeinfo);
327 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
328 t.tm_isdst = timeinfo.tm_isdst;
330 media_svc_debug("DST %d", t.tm_isdst);
332 modified_t = mktime(&t);
336 media_svc_debug("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);
338 media_svc_error("Failed to get timeline : [%s]", timstr);
344 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
348 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
350 /*categorize from mimetype */
351 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
352 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
353 *category = (*category | content_category[idx].category_by_mime);
358 /*in application type, exitst sound file ex) x-smafs, asf */
359 if (*category & MEDIA_SVC_CATEGORY_ETC) {
360 int prefix_len = strlen(content_category[0].content_type);
363 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
364 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
365 *category ^= MEDIA_SVC_CATEGORY_ETC;
366 *category |= MEDIA_SVC_CATEGORY_SOUND;
371 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
372 *category ^= MEDIA_SVC_CATEGORY_ETC;
373 *category |= MEDIA_SVC_CATEGORY_SOUND;
376 /*"asf" must check video stream and then categorize in directly. */
377 ext = strrchr(path, '.');
379 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
384 err = mm_file_get_stream_info(path, &audio, &video);
386 if (audio > 0 && video == 0) {
387 *category ^= MEDIA_SVC_CATEGORY_ETC;
388 *category |= MEDIA_SVC_CATEGORY_MUSIC;
390 *category ^= MEDIA_SVC_CATEGORY_ETC;
391 *category |= MEDIA_SVC_CATEGORY_VIDEO;
398 /*check music file in soun files. */
399 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
400 int prefix_len = strlen(content_category[0].content_type) + 1;
402 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
403 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
404 *category ^= MEDIA_SVC_CATEGORY_SOUND;
405 *category |= MEDIA_SVC_CATEGORY_MUSIC;
410 /*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*/
411 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
412 *category ^= MEDIA_SVC_CATEGORY_SOUND;
413 *category |= MEDIA_SVC_CATEGORY_ETC;
415 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
416 /*some video files don't have video stream. in this case it is categorize as music. */
418 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
419 ext = strrchr(path, '.');
421 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
426 err = mm_file_get_stream_info(path, &audio, &video);
428 if (audio > 0 && video == 0) {
429 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
430 *category |= MEDIA_SVC_CATEGORY_MUSIC;
433 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
438 return MS_MEDIA_ERR_NONE;
441 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
443 int ret = MS_MEDIA_ERR_NONE;
446 media_svc_media_type_e type;
448 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
449 if (ret != MS_MEDIA_ERR_NONE)
450 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
452 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
453 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
454 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
455 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
456 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
464 drm_contentifo is not NULL, if the file is OMA DRM.
465 If the file is not OMA DRM, drm_contentinfo must be NULL.
467 static int __media_svc_get_mime_type(const char *path, char *mimetype)
470 return MS_MEDIA_ERR_INVALID_PARAMETER;
472 /*in case of normal files or failure to get mime in drm */
473 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
474 media_svc_error("aul_get_mime_from_file fail");
475 return MS_MEDIA_ERR_INTERNAL;
478 return MS_MEDIA_ERR_NONE;
481 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
485 for (i = strlen(file_path); i >= 0; i--) {
486 if (file_path[i] == '.') {
487 _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
491 if (file_path[i] == '/')
497 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
499 char *err_attr_name = NULL;
500 double gps_value = 0.0;
501 int mmf_error = FILEINFO_ERROR_NONE;
503 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
504 if (mmf_error == FILEINFO_ERROR_NONE) {
505 if (longitude != NULL)
506 *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
508 SAFE_FREE(err_attr_name);
511 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
512 if (mmf_error == FILEINFO_ERROR_NONE) {
513 if (latitude != NULL)
514 *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
516 SAFE_FREE(err_attr_name);
519 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
520 if (mmf_error == FILEINFO_ERROR_NONE) {
521 if (altitude != NULL)
522 *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
524 SAFE_FREE(err_attr_name);
527 return MS_MEDIA_ERR_NONE;
530 static int __media_svc_encode_jpeg(unsigned char *src, unsigned long width, unsigned long height, image_util_colorspace_e colorspace, int quality, unsigned char **dst, unsigned long long *dst_size)
532 int res = IMAGE_UTIL_ERROR_NONE;
533 image_util_encode_h encoder = NULL;
534 unsigned char *encoded_data = NULL;
535 res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
536 if (res != IMAGE_UTIL_ERROR_NONE) {
537 media_svc_error("image_util_encode_create failed! (%d)", res);
538 return MS_MEDIA_ERR_INTERNAL;
540 res = image_util_encode_set_resolution(encoder, width, height);
541 if (res != IMAGE_UTIL_ERROR_NONE) {
542 media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
543 image_util_encode_destroy(encoder);
544 return MS_MEDIA_ERR_INTERNAL;
546 res = image_util_encode_set_colorspace(encoder, colorspace);
547 if (res != IMAGE_UTIL_ERROR_NONE) {
548 media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
549 image_util_encode_destroy(encoder);
550 return MS_MEDIA_ERR_INTERNAL;
552 res = image_util_encode_set_quality(encoder, quality);
553 if (res != IMAGE_UTIL_ERROR_NONE) {
554 media_svc_error("image_util_encode_set_quality failed! (%d)", res);
555 image_util_encode_destroy(encoder);
556 return MS_MEDIA_ERR_INTERNAL;
558 res = image_util_encode_set_input_buffer(encoder, src);
559 if (res != IMAGE_UTIL_ERROR_NONE) {
560 media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
561 image_util_encode_destroy(encoder);
562 return MS_MEDIA_ERR_INTERNAL;
564 res = image_util_encode_set_output_buffer(encoder, &encoded_data);
565 if (res != IMAGE_UTIL_ERROR_NONE) {
566 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
567 image_util_encode_destroy(encoder);
568 return MS_MEDIA_ERR_INTERNAL;
570 res = image_util_encode_run(encoder, dst_size);
571 if (res != IMAGE_UTIL_ERROR_NONE) {
572 media_svc_error("image_util_encode_run failed! (%d)", res);
573 image_util_encode_destroy(encoder);
574 return MS_MEDIA_ERR_INTERNAL;
576 if (encoded_data != NULL) {
577 *dst = (unsigned char *)calloc(1, *dst_size);
579 media_svc_error("memory allocation failed! (%lld)", *dst_size);
580 image_util_encode_destroy(encoder);
581 return MS_MEDIA_ERR_INTERNAL;
583 memcpy(*dst, encoded_data, *dst_size);
585 res = image_util_encode_destroy(encoder);
586 if (res != IMAGE_UTIL_ERROR_NONE) {
587 media_svc_error("image_util_encode_destroy failed! (%d)", res);
588 return MS_MEDIA_ERR_INTERNAL;
590 SAFE_FREE(encoded_data);
591 return MS_MEDIA_ERR_NONE;
594 static int __media_svc_decode_jpeg(unsigned char *src, unsigned long long size, image_util_colorspace_e colorspace, unsigned char **dst, unsigned long *width, unsigned long *height, unsigned long long *dst_size)
596 int res = IMAGE_UTIL_ERROR_NONE;
597 image_util_decode_h decoder = NULL;
598 res = image_util_decode_create(&decoder);
599 if (res != IMAGE_UTIL_ERROR_NONE) {
600 media_svc_error("image_util_decode_create failed! (%d)", res);
601 return MS_MEDIA_ERR_INTERNAL;
603 res = image_util_decode_set_input_buffer(decoder, src, size);
604 if (res != IMAGE_UTIL_ERROR_NONE) {
605 media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
606 image_util_decode_destroy(decoder);
607 return MS_MEDIA_ERR_INTERNAL;
609 res = image_util_decode_set_colorspace(decoder, colorspace);
610 if (res != IMAGE_UTIL_ERROR_NONE) {
611 media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
612 image_util_decode_destroy(decoder);
613 return MS_MEDIA_ERR_INTERNAL;
615 res = image_util_decode_set_output_buffer(decoder, dst);
616 if (res != IMAGE_UTIL_ERROR_NONE) {
617 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
618 image_util_decode_destroy(decoder);
619 return MS_MEDIA_ERR_INTERNAL;
621 res = image_util_decode_run(decoder, width, height, dst_size);
622 if (res != IMAGE_UTIL_ERROR_NONE) {
623 media_svc_error("image_util_decode_run failed! (%d)", res);
624 image_util_decode_destroy(decoder);
625 return MS_MEDIA_ERR_INTERNAL;
628 res = image_util_decode_destroy(decoder);
629 if (res != IMAGE_UTIL_ERROR_NONE) {
630 media_svc_error("image_util_decode_destroy failed! (%d)", res);
631 return MS_MEDIA_ERR_INTERNAL;
633 return MS_MEDIA_ERR_NONE;
636 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
638 int ret = MS_MEDIA_ERR_NONE;
639 unsigned char *raw_image = NULL;
642 unsigned long long raw_size = 0;
643 void *resized_raw_image = NULL;
644 int resized_width = 0;
645 int resized_height = 0;
646 unsigned int buf_size = 0;
647 image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
649 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
650 media_svc_debug("type [jpeg] size [%d]", size);
652 ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, &raw_size);
653 if (ret != MS_MEDIA_ERR_NONE) {
654 media_svc_error("__media_svc_decode_jpeg failed");
655 *resize_image = image;
657 return MS_MEDIA_ERR_NONE;
660 if (raw_image == NULL) {
661 media_svc_error("raw_image is null");
662 *resize_image = image;
664 return MS_MEDIA_ERR_NONE;
667 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
668 media_svc_debug("No need resizing");
669 *resize_image = image;
671 SAFE_FREE(raw_image);
672 return MS_MEDIA_ERR_NONE;
675 if (width > height) {
676 resized_height = MEDIA_SVC_ARTWORK_SIZE;
677 resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
679 resized_width = MEDIA_SVC_ARTWORK_SIZE;
680 resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
683 image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
685 resized_raw_image = malloc(buf_size);
687 if (resized_raw_image == NULL) {
688 media_svc_error("malloc failed");
689 *resize_image = image;
691 SAFE_FREE(raw_image);
692 return MS_MEDIA_ERR_NONE;
695 memset(resized_raw_image, 0, buf_size);
697 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
698 if (ret != MS_MEDIA_ERR_NONE) {
699 media_svc_error("image_util_resize failed");
700 *resize_image = image;
702 SAFE_FREE(raw_image);
703 SAFE_FREE(resized_raw_image);
704 return MS_MEDIA_ERR_NONE;
706 SAFE_FREE(raw_image);
709 ret = __media_svc_encode_jpeg((unsigned char *)resized_raw_image, (unsigned long)resized_width, (unsigned long)resized_height, colorspace, 90, resize_image, (unsigned long long *)resize_size);
710 if (ret != MS_MEDIA_ERR_NONE) {
711 media_svc_error("__media_svc_encode_jpeg failed");
712 *resize_image = image;
714 SAFE_FREE(resized_raw_image);
715 return MS_MEDIA_ERR_NONE;
717 SAFE_FREE(resized_raw_image);
719 if (*resize_image == NULL) {
720 media_svc_error("*resize_image is null");
721 *resize_image = image;
723 return MS_MEDIA_ERR_NONE;
725 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
726 media_svc_debug("type [png] size [%d]", size);
727 *resize_image = image;
731 media_svc_debug("Not proper img format");
732 *resize_image = image;
739 static int __media_svc_safe_atoi(char *buffer, int *si)
743 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
745 const long sl = strtol(buffer, &end, 10);
747 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
748 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
749 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
750 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
751 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
755 return MS_MEDIA_ERR_NONE;
758 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
760 int ret = MS_MEDIA_ERR_NONE;
762 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
765 media_svc_error("invalid image..");
766 return MS_MEDIA_ERR_INVALID_PARAMETER;
770 char *thumb_path = NULL;
771 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
772 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
774 if (-1 == statfs(thumb_path, &fs)) {
775 media_svc_error("error in statfs");
776 SAFE_FREE(thumb_path);
777 return MS_MEDIA_ERR_INTERNAL;
780 SAFE_FREE(thumb_path);
782 long bsize_kbytes = fs.f_bsize >> 10;
784 if ((bsize_kbytes * fs.f_bavail) < 1024) {
785 media_svc_error("not enought space...");
786 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
791 if (image != NULL && size > 0) {
792 fp = fopen(image_path, "w");
795 media_svc_error("failed to open file");
796 return MS_MEDIA_ERR_INTERNAL;
799 nwrite = fwrite(image, 1, size, fp);
800 if (nwrite != size) {
801 media_svc_error("failed to write thumbnail");
803 return MS_MEDIA_ERR_INTERNAL;
808 return MS_MEDIA_ERR_NONE;
811 static char *__media_svc_get_title_from_filepath(const char *path)
813 char *filename = NULL;
816 int filename_len = -1;
817 int new_title_len = -1;
820 media_svc_error("path is NULL");
824 filename = g_path_get_basename(path);
825 if (!STRING_VALID(filename)) {
826 media_svc_error("wrong file name");
831 filename_len = strlen(filename);
833 ext = g_strrstr(filename, ".");
835 media_svc_error("there is no file extention");
839 new_title_len = filename_len - strlen(ext);
840 if (new_title_len < 1) {
841 media_svc_error("title length is zero");
846 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
850 media_svc_debug("extract title is [%s]", title);
855 int _media_svc_rename_file(const char *old_name, const char *new_name)
857 if ((old_name == NULL) || (new_name == NULL)) {
858 media_svc_error("invalid file name");
859 return MS_MEDIA_ERR_INVALID_PARAMETER;
862 if (rename(old_name, new_name) < 0) {
863 media_svc_stderror(" ");
864 return MS_MEDIA_ERR_INTERNAL;
867 return MS_MEDIA_ERR_NONE;
870 int _media_svc_remove_file(const char *path)
874 result = remove(path);
876 media_svc_debug("success to remove file");
877 return MS_MEDIA_ERR_NONE;
879 media_svc_stderror("fail to remove file result");
880 return MS_MEDIA_ERR_INTERNAL;
884 int _media_svc_remove_all_files_in_dir(const char *dir_path)
887 struct dirent *result;
889 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
892 dir = opendir(dir_path);
894 media_svc_error("%s is not exist", dir_path);
895 return MS_MEDIA_ERR_INVALID_PARAMETER;
898 while (!readdir_r(dir, &entry, &result)) {
902 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
905 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
907 if (stat(filename, &st) != 0)
910 if (S_ISDIR(st.st_mode))
913 if (unlink(filename) != 0) {
914 media_svc_stderror("failed to remove");
916 return MS_MEDIA_ERR_INTERNAL;
921 return MS_MEDIA_ERR_NONE;
924 static int __media_svc_check_thumb_dir(const char *thumb_dir)
928 dir = opendir(thumb_dir);
932 return MS_MEDIA_ERR_INTERNAL;
934 return MS_MEDIA_ERR_NONE;
937 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)
939 int ret = MS_MEDIA_ERR_NONE;
940 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
941 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
942 char *thumb_dir = NULL;
943 char hash[255 + 1] = {0, };
944 char *thumbfile_ext = NULL;
945 char *internal_thumb_path = NULL;
946 char *external_thumb_path = NULL;
948 ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
949 ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
951 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
952 media_svc_error("fail to get thumbnail path");
953 SAFE_FREE(internal_thumb_path);
954 SAFE_FREE(external_thumb_path);
955 return MS_MEDIA_ERR_INTERNAL;
958 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
960 ret = __media_svc_check_thumb_dir(thumb_dir);
961 if (ret != MS_MEDIA_ERR_NONE) {
962 media_svc_error("__media_svc_check_thumb_dir");
963 SAFE_FREE(internal_thumb_path);
964 SAFE_FREE(external_thumb_path);
965 return MS_MEDIA_ERR_INTERNAL;
968 memset(file_ext, 0, sizeof(file_ext));
969 if (!__media_svc_get_file_ext(pathname, file_ext))
970 media_svc_error("get file ext fail");
972 ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
973 if (ret != MS_MEDIA_ERR_NONE) {
974 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
975 SAFE_FREE(internal_thumb_path);
976 SAFE_FREE(external_thumb_path);
977 return MS_MEDIA_ERR_INTERNAL;
980 /*media_svc_debug("img format is [%s]", img_format); */
982 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
983 thumbfile_ext = (char *)"jpg";
984 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
985 thumbfile_ext = (char *)"png";
986 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
987 thumbfile_ext = (char *)"gif";
988 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
989 thumbfile_ext = (char *)"bmp";
991 media_svc_error("Not proper img format");
992 SAFE_FREE(internal_thumb_path);
993 SAFE_FREE(external_thumb_path);
994 return MS_MEDIA_ERR_INTERNAL;
997 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
998 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
999 /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1001 SAFE_FREE(internal_thumb_path);
1002 SAFE_FREE(external_thumb_path);
1004 return MS_MEDIA_ERR_NONE;
1007 int _media_svc_get_file_time(const char *full_path)
1009 struct stat statbuf;
1012 memset(&statbuf, 0, sizeof(struct stat));
1013 fd = stat(full_path, &statbuf);
1015 media_svc_error("stat(%s) fails.", full_path);
1016 return MS_MEDIA_ERR_INTERNAL;
1019 return statbuf.st_mtime;
1022 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1024 /* Set default GPS value before extracting meta information */
1025 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1026 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1027 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1029 /* Set filename to title for all media */
1031 title = __media_svc_get_title_from_filepath(content_info->path);
1033 content_info->media_meta.title = g_strdup(title);
1035 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1037 media_svc_error("Can't extract title");
1038 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1039 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1042 /* Set default value before extracting meta information */
1043 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1044 media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1046 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1047 media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1049 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1050 media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1052 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1053 media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1055 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1056 media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1058 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1059 media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1061 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1062 media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1064 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1065 media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1067 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1068 media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1071 media_svc_debug("refresh");
1072 return MS_MEDIA_ERR_NONE;
1075 content_info->played_count = 0;
1076 content_info->last_played_time = 0;
1077 content_info->last_played_position = 0;
1078 content_info->favourate = 0;
1079 content_info->media_meta.rating = 0;
1081 return MS_MEDIA_ERR_NONE;
1084 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1085 const char *path, media_svc_media_type_e *media_type, bool refresh)
1087 int ret = MS_MEDIA_ERR_NONE;
1088 char * media_uuid = NULL;
1089 char * file_name = NULL;
1090 bool drm_type = false;
1091 char mime_type[256] = {0, };
1093 content_info->path = g_strdup(path);
1094 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1097 memset(&st, 0, sizeof(struct stat));
1098 if (stat(path, &st) == 0) {
1099 content_info->modified_time = st.st_mtime;
1100 content_info->timeline = content_info->modified_time;
1101 content_info->size = st.st_size;
1103 media_svc_stderror("stat failed");
1106 _media_svc_set_default_value(content_info, refresh);
1108 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1110 media_svc_debug("refresh");
1111 return MS_MEDIA_ERR_NONE;
1114 content_info->storage_uuid = g_strdup(storage_id);
1115 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1117 content_info->storage_type = storage_type;
1118 time(&content_info->added_time);
1120 media_uuid = _media_info_generate_uuid();
1121 if (media_uuid == NULL) {
1122 _media_svc_destroy_content_info(content_info);
1123 return MS_MEDIA_ERR_INTERNAL;
1126 content_info->media_uuid = g_strdup(media_uuid);
1127 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1129 file_name = g_path_get_basename(path);
1130 content_info->file_name = g_strdup(file_name);
1131 SAFE_FREE(file_name);
1132 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1134 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1135 if drm_contentinfo is not NULL, the file is OMA DRM.*/
1136 ret = __media_svc_get_mime_type(path, mime_type);
1137 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1139 media_svc_debug("mime [%s]", mime_type);
1140 content_info->is_drm = drm_type;
1142 ret = __media_svc_get_media_type(path, mime_type, media_type);
1143 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1144 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1145 media_svc_error("invalid media_type condition[%d]", *media_type);
1146 return MS_MEDIA_ERR_INVALID_PARAMETER;
1149 content_info->mime_type = g_strdup(mime_type);
1150 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1152 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1154 content_info->media_type = *media_type;
1156 return MS_MEDIA_ERR_NONE;
1159 int image_360_check(char *path)
1164 unsigned char exif_header[4];
1165 unsigned char exif_app1[2];
1166 unsigned char exif_app1_xmp[2];
1167 long exif_app1_xmp_size = 0;
1168 unsigned char exif_app1_xmp_t[2];
1169 char *xmp_data = NULL;
1175 fp = fopen(path, "rb");
1179 size = fread(exif_header, 1, sizeof(exif_header), fp);
1183 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1184 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1188 size1 = exif_app1[0];
1189 size2 = exif_app1[1];
1191 app1_size = size1 * 256 + size2 - 2;
1193 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1196 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1200 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1203 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1207 size1 = exif_app1_xmp_t[0];
1208 size2 = exif_app1_xmp_t[1];
1210 exif_app1_xmp_size = size1 * 256 + size2 - 2;
1212 xmp_data = (char *)malloc(exif_app1_xmp_size);
1213 if (xmp_data != NULL) {
1214 memset(xmp_data, 0x0, exif_app1_xmp_size);
1217 while (exif_app1_xmp_size >= 0) {
1218 exif_app1_xmp_size--;
1230 if (strstr(ptr, "UsePanoramaViewer")
1231 && strstr(ptr, "True")
1232 && strstr(ptr, "ProjectionType")
1233 && strstr(ptr, "equirectangular"))
1236 SAFE_FREE(xmp_data);
1238 media_svc_error("malloc failed");
1260 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1263 int orient_value = 0;
1265 int exif_height = 0;
1266 ExifData *ed = NULL;
1267 int has_datetaken = FALSE;
1268 double fnumber = 0.0;
1272 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1274 memset(buf, 0x00, sizeof(buf));
1276 if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1277 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1278 return MS_MEDIA_ERR_INVALID_PARAMETER;
1281 path = content_info->path;
1282 if (!STRING_VALID(path)) {
1283 media_svc_error("Invalid Path");
1284 return MS_MEDIA_ERR_INVALID_PARAMETER;
1287 /* Load an ExifData object from an EXIF file */
1288 ed = exif_data_new_from_file(path);
1291 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1292 goto GET_WIDTH_HEIGHT;
1295 content_info->media_meta.is_360 = image_360_check(path);
1297 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1298 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1299 if (strlen(buf) > 0) {
1300 if (strcmp(buf, "S") == 0)
1303 content_info->media_meta.latitude = value;
1305 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1308 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1311 memset(buf, 0x00, sizeof(buf));
1313 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1314 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1315 if (strlen(buf) > 0) {
1316 if (strcmp(buf, "W") == 0)
1319 content_info->media_meta.longitude = value;
1321 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1324 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1327 memset(buf, 0x00, sizeof(buf));
1329 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1330 if (strlen(buf) == 0)
1331 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1333 content_info->media_meta.description = g_strdup(buf);
1335 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1338 memset(buf, 0x00, sizeof(buf));
1340 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1341 if (strlen(buf) > 0) {
1342 has_datetaken = TRUE;
1343 content_info->media_meta.datetaken = g_strdup(buf);
1345 /* This is same as recorded_date */
1346 content_info->media_meta.recorded_date = g_strdup(buf);
1350 memset(buf, 0x00, sizeof(buf));
1352 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1353 if (strlen(buf) > 0) {
1354 has_datetaken = TRUE;
1355 content_info->media_meta.datetaken = g_strdup(buf);
1357 /* This is same as recorded_date */
1358 content_info->media_meta.recorded_date = g_strdup(buf);
1362 if (has_datetaken) {
1363 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1364 if (content_info->timeline == 0)
1365 content_info->timeline = content_info->modified_time;
1367 media_svc_debug("Timeline : %ld", content_info->timeline);
1370 memset(buf, 0x00, sizeof(buf));
1372 /* Get exposure_time value from exif. */
1373 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1374 if (strlen(buf) > 0)
1375 content_info->media_meta.exposure_time = g_strdup(buf);
1378 /* Get fnumber value from exif. */
1379 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1381 content_info->media_meta.fnumber = fnumber;
1383 content_info->media_meta.fnumber = 0.0;
1385 content_info->media_meta.fnumber = 0.0;
1388 /* Get iso value from exif. */
1389 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1391 content_info->media_meta.iso = iso;
1393 content_info->media_meta.iso = 0;
1395 content_info->media_meta.iso = 0;
1398 memset(buf, 0x00, sizeof(buf));
1400 /* Get model value from exif. */
1401 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1402 if (strlen(buf) > 0)
1403 content_info->media_meta.model = g_strdup(buf);
1406 /* Get orientation value from exif. */
1407 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1408 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1409 content_info->media_meta.orientation = orient_value;
1411 content_info->media_meta.orientation = 0;
1413 content_info->media_meta.orientation = 0;
1416 /* Get width value from exif. */
1417 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1419 content_info->media_meta.width = exif_width;
1421 content_info->media_meta.width = 0;
1423 content_info->media_meta.width = 0;
1426 /* Get height value from exif. */
1427 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1428 if (exif_height > 0)
1429 content_info->media_meta.height = exif_height;
1431 content_info->media_meta.height = 0;
1433 content_info->media_meta.height = 0;
1436 if (ed != NULL) exif_data_unref(ed);
1440 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1441 /*Get image width, height*/
1442 unsigned int img_width = 0;
1443 unsigned int img_height = 0;
1444 ImgCodecType img_type = IMG_CODEC_NONE;
1446 ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1447 if (content_info->media_meta.width == 0)
1448 content_info->media_meta.width = img_width;
1450 if (content_info->media_meta.height == 0)
1451 content_info->media_meta.height = img_height;
1454 return MS_MEDIA_ERR_NONE;
1457 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1459 MMHandleType tag = 0;
1462 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1463 int mmf_error = FILEINFO_ERROR_NONE;
1464 char *err_attr_name = NULL;
1467 /*Get Content Tag attribute ===========*/
1468 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1470 if (mmf_error == FILEINFO_ERROR_NONE) {
1471 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1472 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1473 content_info->media_meta.album = g_strdup(p);
1475 SAFE_FREE(err_attr_name);
1478 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1479 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1480 content_info->media_meta.artist = g_strdup(p);
1482 SAFE_FREE(err_attr_name);
1485 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1486 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1487 content_info->media_meta.album_artist = g_strdup(p);
1489 SAFE_FREE(err_attr_name);
1492 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1493 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1494 content_info->media_meta.genre = g_strdup(p);
1496 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1498 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1499 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1503 SAFE_FREE(err_attr_name);
1506 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1507 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1509 content_info->media_meta.title = g_strdup(p);
1511 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1515 for (idx = 0; idx < size; idx++) {
1517 media_svc_debug("SPACE [%s]", p);
1521 media_svc_debug("Not SPACE [%s]", p);
1522 content_info->media_meta.title = g_strdup(p);
1529 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1530 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1531 content_info->media_meta.description = g_strdup(p);
1533 SAFE_FREE(err_attr_name);
1536 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1537 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1538 content_info->media_meta.composer = g_strdup(p);
1539 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1541 SAFE_FREE(err_attr_name);
1544 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1545 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1546 content_info->media_meta.copyright = g_strdup(p);
1547 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1549 SAFE_FREE(err_attr_name);
1552 mmf_error = mm_file_destroy_tag_attrs(tag);
1553 if (mmf_error != FILEINFO_ERROR_NONE)
1554 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1556 content_info->album_id = album_id;
1559 return MS_MEDIA_ERR_NONE;
1562 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1564 MMHandleType content = 0;
1565 MMHandleType tag = 0;
1567 unsigned char *image = NULL;
1568 unsigned int size = 0;
1569 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1570 int mmf_error = FILEINFO_ERROR_NONE;
1571 char *err_attr_name = NULL;
1572 bool extract_thumbnail = FALSE;
1573 bool append_album = FALSE;
1575 int ret = MS_MEDIA_ERR_NONE;
1577 unsigned int resize_size = 0;
1578 unsigned char *resize_image = NULL;
1580 /*Get Content Tag attribute ===========*/
1581 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1583 if (mmf_error == FILEINFO_ERROR_NONE) {
1584 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1585 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1586 content_info->media_meta.album = g_strdup(p);
1588 SAFE_FREE(err_attr_name);
1591 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1592 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1593 content_info->media_meta.artist = g_strdup(p);
1595 SAFE_FREE(err_attr_name);
1598 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1599 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1600 content_info->media_meta.album_artist = g_strdup(p);
1602 SAFE_FREE(err_attr_name);
1605 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1606 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1607 content_info->media_meta.genre = g_strdup(p);
1609 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1611 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1612 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1616 SAFE_FREE(err_attr_name);
1619 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1620 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1622 content_info->media_meta.title = g_strdup(p);
1623 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1627 for (idx = 0; idx < size; idx++) {
1629 media_svc_debug("SPACE [%s]", p);
1633 media_svc_debug("Not SPACE [%s]", p);
1634 content_info->media_meta.title = g_strdup(p);
1641 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1642 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1643 content_info->media_meta.description = g_strdup(p);
1645 SAFE_FREE(err_attr_name);
1648 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1649 if (mmf_error != FILEINFO_ERROR_NONE)
1650 SAFE_FREE(err_attr_name);
1652 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1653 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1654 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1655 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1656 char *time_info = (char*)calloc(1, (size + 1));
1658 char *time_value = time_info;
1659 if (time_info != NULL) {
1660 while (*p_value != '\0') {
1661 if (*p_value == '-')
1664 *time_value = *p_value;
1669 content_info->media_meta.recorded_date = g_strdup(time_info);
1670 SAFE_FREE(time_info);
1672 media_svc_error("memory allocation error");
1673 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1676 content_info->media_meta.recorded_date = g_strdup(p);
1679 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1680 /* This is same as datetaken */
1681 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1683 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1684 if (content_info->timeline == 0)
1685 content_info->timeline = content_info->modified_time;
1687 media_svc_debug("Timeline : %ld", content_info->timeline);
1690 SAFE_FREE(err_attr_name);
1693 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1694 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1695 content_info->media_meta.composer = g_strdup(p);
1696 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1698 SAFE_FREE(err_attr_name);
1701 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1702 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1703 content_info->media_meta.copyright = g_strdup(p);
1704 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1706 SAFE_FREE(err_attr_name);
1709 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1710 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1711 content_info->media_meta.track_num = g_strdup(p);
1713 SAFE_FREE(err_attr_name);
1716 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1717 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1719 if ((p != NULL) && ((ret != __media_svc_safe_atoi(p, &year)) == MS_MEDIA_ERR_NONE)) {
1720 content_info->media_meta.year = g_strdup(p);
1722 media_svc_debug("Wrong Year Information [%s]", p);
1725 SAFE_FREE(err_attr_name);
1728 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1729 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1731 if (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE)
1732 content_info->media_meta.rating = rate;
1734 SAFE_FREE(err_attr_name);
1735 content_info->media_meta.rating = 0;
1738 /*Initialize album_id to 0. below code will set the album_id*/
1739 content_info->album_id = album_id;
1740 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1742 if (ret != MS_MEDIA_ERR_NONE) {
1743 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1744 media_svc_debug("album does not exist. So start to make album art");
1745 extract_thumbnail = TRUE;
1746 append_album = TRUE;
1748 extract_thumbnail = TRUE;
1749 append_album = FALSE;
1752 content_info->album_id = album_id;
1753 append_album = FALSE;
1755 if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1756 (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1757 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1758 extract_thumbnail = TRUE;
1760 media_svc_debug("album already exists. don't need to make album art");
1761 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1762 extract_thumbnail = TRUE;
1766 /*Do not extract artwork for the USB Storage content*/
1767 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1768 extract_thumbnail = FALSE;
1770 if (extract_thumbnail == TRUE) {
1771 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1772 if (mmf_error != FILEINFO_ERROR_NONE) {
1773 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1774 SAFE_FREE(err_attr_name);
1776 /*media_svc_debug("artwork size1 [%d]", size); */
1779 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1780 if (mmf_error != FILEINFO_ERROR_NONE) {
1781 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1782 SAFE_FREE(err_attr_name);
1784 /*media_svc_debug("artwork size2 [%d]", size); */
1786 if (image != NULL && size > 0) {
1787 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1788 int artwork_mime_size = -1;
1790 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1791 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1792 ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1793 if (ret != MS_MEDIA_ERR_NONE)
1794 media_svc_error("Fail to Get Thumbnail Path");
1795 /* albumart resizing */
1796 __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1798 SAFE_FREE(err_attr_name);
1801 if (strlen(thumb_path) > 0) {
1802 ret = __media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1803 if (ret != MS_MEDIA_ERR_NONE) {
1804 media_svc_error("Fail to Save Thumbnail Image");
1806 content_info->thumbnail_path = g_strdup(thumb_path);
1812 if (append_album == TRUE) {
1813 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1814 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1815 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1817 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1819 content_info->album_id = album_id;
1822 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1823 double longitude = 0.0;
1824 double latitude = 0.0;
1825 double altitude = 0.0;
1827 __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1828 content_info->media_meta.longitude = longitude;
1829 content_info->media_meta.latitude = latitude;
1830 content_info->media_meta.altitude = altitude;
1832 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1833 if (mmf_error != FILEINFO_ERROR_NONE) {
1835 SAFE_FREE(err_attr_name);
1838 media_svc_debug("CDIS : %d", cdis_value);
1840 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1841 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1842 content_info->media_meta.orientation = atoi(p);
1844 SAFE_FREE(err_attr_name);
1845 content_info->media_meta.orientation = 0;
1846 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1850 mmf_error = mm_file_destroy_tag_attrs(tag);
1851 if (mmf_error != FILEINFO_ERROR_NONE)
1852 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1854 content_info->album_id = album_id;
1857 /*Get Content attribute ===========*/
1858 if (cdis_value == 1)
1859 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1861 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1863 if (mmf_error == FILEINFO_ERROR_NONE) {
1864 /*Common attribute*/
1865 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1866 if (mmf_error != FILEINFO_ERROR_NONE) {
1867 SAFE_FREE(err_attr_name);
1868 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1870 /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1873 /*Sound/Music attribute*/
1874 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1876 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1877 if (mmf_error != FILEINFO_ERROR_NONE) {
1878 SAFE_FREE(err_attr_name);
1879 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1881 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1884 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1885 if (mmf_error != FILEINFO_ERROR_NONE) {
1886 SAFE_FREE(err_attr_name);
1887 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1889 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1892 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1893 if (mmf_error != FILEINFO_ERROR_NONE) {
1894 SAFE_FREE(err_attr_name);
1895 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1897 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1900 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1901 if (mmf_error != FILEINFO_ERROR_NONE) {
1902 SAFE_FREE(err_attr_name);
1903 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1905 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1907 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
1908 int audio_bitrate = 0;
1909 int video_bitrate = 0;
1911 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1912 if (mmf_error != FILEINFO_ERROR_NONE) {
1913 SAFE_FREE(err_attr_name);
1914 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1916 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1919 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1920 if (mmf_error != FILEINFO_ERROR_NONE) {
1921 SAFE_FREE(err_attr_name);
1922 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1924 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1927 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1929 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1930 if (mmf_error != FILEINFO_ERROR_NONE) {
1931 SAFE_FREE(err_attr_name);
1932 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1934 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1937 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1938 if (mmf_error != FILEINFO_ERROR_NONE) {
1939 SAFE_FREE(err_attr_name);
1940 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1942 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1945 media_svc_error("Not support type");
1946 mmf_error = mm_file_destroy_content_attrs(content);
1947 if (mmf_error != FILEINFO_ERROR_NONE)
1948 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1950 return MS_MEDIA_ERR_INVALID_PARAMETER;
1953 mmf_error = mm_file_destroy_content_attrs(content);
1954 if (mmf_error != FILEINFO_ERROR_NONE)
1955 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1957 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1960 return MS_MEDIA_ERR_NONE;
1963 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1965 media_svc_retm_if(content_info == NULL, "content info is NULL");
1967 /* Delete media_svc_content_info_s */
1968 SAFE_FREE(content_info->media_uuid);
1969 SAFE_FREE(content_info->path);
1970 SAFE_FREE(content_info->file_name);
1971 SAFE_FREE(content_info->mime_type);
1972 SAFE_FREE(content_info->folder_uuid);
1973 SAFE_FREE(content_info->thumbnail_path);
1974 SAFE_FREE(content_info->storage_uuid);
1976 /* Delete media_svc_content_meta_s */
1977 SAFE_FREE(content_info->media_meta.title);
1978 SAFE_FREE(content_info->media_meta.album);
1979 SAFE_FREE(content_info->media_meta.artist);
1980 SAFE_FREE(content_info->media_meta.album_artist);
1981 SAFE_FREE(content_info->media_meta.genre);
1982 SAFE_FREE(content_info->media_meta.composer);
1983 SAFE_FREE(content_info->media_meta.year);
1984 SAFE_FREE(content_info->media_meta.recorded_date);
1985 SAFE_FREE(content_info->media_meta.copyright);
1986 SAFE_FREE(content_info->media_meta.track_num);
1987 SAFE_FREE(content_info->media_meta.description);
1988 SAFE_FREE(content_info->media_meta.datetaken);
1989 SAFE_FREE(content_info->media_meta.exposure_time);
1990 SAFE_FREE(content_info->media_meta.model);
1991 SAFE_FREE(content_info->media_meta.weather);
1992 SAFE_FREE(content_info->media_meta.category);
1993 SAFE_FREE(content_info->media_meta.keyword);
1994 SAFE_FREE(content_info->media_meta.location_tag);
1995 SAFE_FREE(content_info->media_meta.content_name);
1996 SAFE_FREE(content_info->media_meta.age_rating);
1997 SAFE_FREE(content_info->media_meta.author);
1998 SAFE_FREE(content_info->media_meta.provider);
2000 SAFE_FREE(content_info->media_meta.title_pinyin);
2001 SAFE_FREE(content_info->media_meta.album_pinyin);
2002 SAFE_FREE(content_info->media_meta.artist_pinyin);
2003 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2004 SAFE_FREE(content_info->media_meta.genre_pinyin);
2005 SAFE_FREE(content_info->media_meta.composer_pinyin);
2006 SAFE_FREE(content_info->media_meta.copyright_pinyin);
2007 SAFE_FREE(content_info->media_meta.description_pinyin);
2012 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2014 char result[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
2015 memset(result, 0x00, sizeof(result));
2017 if (STRING_VALID(s) && STRING_VALID(olds) && STRING_VALID(news)) {
2018 if (strncmp(s, olds, strlen(olds)) == 0) {
2019 snprintf(result, sizeof(result), "%s%s", news, s + strlen(olds));
2023 if (STRING_VALID(result))
2024 return g_strdup(result);
2030 bool _media_svc_is_drm_file(const char *path)
2035 int _media_svc_request_thumbnail(const char *path, char *thumb_path, int max_length, uid_t uid)
2037 int ret = MS_MEDIA_ERR_NONE;
2039 ret = thumbnail_request_from_db(path, thumb_path, max_length, uid);
2040 if (ret != MS_MEDIA_ERR_NONE) {
2041 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2042 ret = MS_MEDIA_ERR_INTERNAL;
2044 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2050 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2052 int ret = MS_MEDIA_ERR_NONE;
2054 pinyin_name_s *pinyinname = NULL;
2058 if (!STRING_VALID(src_str)) {
2059 media_svc_debug("String is invalid");
2063 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2064 if (ret == MS_MEDIA_ERR_NONE) {
2065 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2066 *pinyin_str = strdup(pinyinname[0].pinyin_name);
2068 *pinyin_str = strdup(src_str); /* Return Original Non China Character */
2071 _media_svc_pinyin_free(pinyinname, size);
2076 bool _media_svc_check_pinyin_support(void)
2082 char* _media_svc_get_title_from_path(const char *path)
2084 char *filename = NULL;
2087 int filename_len = -1;
2088 int new_title_len = -1;
2091 media_svc_error("path is NULL");
2095 filename = g_path_get_basename(path);
2096 if (!STRING_VALID(filename)) {
2097 media_svc_error("wrong file name");
2098 SAFE_FREE(filename);
2102 filename_len = strlen(filename);
2104 ext = g_strrstr(filename, ".");
2106 media_svc_error("there is no file extention");
2110 new_title_len = filename_len - strlen(ext);
2111 if (new_title_len < 1) {
2112 media_svc_error("title length is zero");
2113 SAFE_FREE(filename);
2117 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2119 SAFE_FREE(filename);
2121 media_svc_debug("extract title is [%s]", title);
2126 int _media_svc_get_media_type(const char *path, int *mediatype)
2128 int ret = MS_MEDIA_ERR_NONE;
2129 char mime_type[256] = {0};
2130 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
2132 ret = __media_svc_get_mime_type(path, mime_type);
2133 if (ret == MS_MEDIA_ERR_NONE) {
2134 __media_svc_get_media_type(path, mime_type, &media_type);
2136 media_svc_error("__media_svc_get_mime_type failed");
2139 *mediatype = media_type;