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 */
33 #include <system_info.h>
35 #include <glib/gstdio.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include <media-thumbnail.h>
46 #include "media-util-err.h"
47 #include "media-svc-util.h"
48 #include "media-svc-db-utils.h"
49 #include "media-svc-debug.h"
50 #include "media-svc-env.h"
51 #include "media-svc-hash.h"
52 #include "media-svc-album.h"
53 #include "media-svc-localize_ch.h"
56 #include <libxml/xmlmemory.h>
57 #include <libxml/parser.h>
58 #include <libxml/HTMLparser.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 */
69 #define MEDIA_SVC_CATEGORY_PVR 0x00000020 /**< PVR category */
70 #define MEDIA_SVC_CATEGORY_UHD 0x00000040 /**< UHD category */
71 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080 /**< SCSA category */
72 #define MEDIA_SVC_CATEGORY_BOOK 0x00000100 /**< ebook category */
74 #define CONTENT_TYPE_NUM 5
75 #define MUSIC_MIME_NUM 29
76 #define SOUND_MIME_NUM 1
77 #define MIME_TYPE_LENGTH 255
78 #define THUMB_HASH_LEN 256
79 #define MIME_LENGTH 50
80 #define _3GP_FILE ".3gp"
81 #define _MP4_FILE ".mp4"
82 #define _ASF_FILE ".asf"
83 #define MEDIA_SVC_ARTWORK_SIZE 2000
84 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
85 #define IMAGE_PREFIX "image/"
86 #define IMAGE_PREFIX_LEN 6
88 #define MEDIA_SVC_DEFAULT_GPS_VALUE -200 /**< Default GPS Value*/
90 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
91 #define MEDIA_SVC_PDF_BUF_SIZE 256
94 char content_type[15];
96 } _media_svc_content_table_s;
98 enum Exif_Orientation {
110 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
111 {"audio", MEDIA_SVC_CATEGORY_SOUND},
112 {"image", MEDIA_SVC_CATEGORY_IMAGE},
113 {"video", MEDIA_SVC_CATEGORY_VIDEO},
114 {"application", MEDIA_SVC_CATEGORY_ETC},
115 {"text", MEDIA_SVC_CATEGORY_ETC},
118 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
119 /*known mime types of normal files*/
125 /* known mime types of drm files*/
127 "x-mp3", /*alias of audio/mpeg*/
128 "x-mpeg", /*alias of audio/mpeg*/
130 "x-ogg", /*alias of audio/ogg*/
131 "vnd.ms-playready.media.pya:*.pya", /*playready*/
134 "x-m4a", /*alias of audio/mp4*/
135 /* below mimes are rare*/
145 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
146 "vorbis", /*alias of audio/x-vorbis+ogg*/
148 "x-mp2", /*alias of audio/mp2*/
149 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
150 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
154 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
158 char *_media_info_generate_uuid(void)
161 char uuid_unparsed[37];
164 uuid_generate(uuid_value);
165 uuid_unparse(uuid_value, uuid_unparsed);
167 if (strlen(uuid_unparsed) < 36) {
168 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
172 return g_strdup(uuid_unparsed);
175 /* GPS information is not ExifTag member */
176 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
179 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
180 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
182 char **tmp_split = NULL;
184 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
185 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
187 entry = exif_data_get_entry(ed, tagtype);
188 media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
190 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
191 gps_buf[strlen(gps_buf)] = '\0';
193 tmp_split = g_strsplit(gps_buf, ",", -1);
194 if (g_strv_length(tmp_split) != 3) {
195 g_strfreev(tmp_split);
196 media_svc_error("Wrong GPS format");
197 return MS_MEDIA_ERR_INTERNAL;
200 for (i = 0; i < 3; i++)
201 tmp_arr[i] = g_strtod(tmp_split[i], NULL);
203 g_strfreev(tmp_split);
205 *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
207 return MS_MEDIA_ERR_NONE;
210 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
214 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
215 media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
217 entry = exif_data_get_entry(ed, tagtype);
219 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
220 value[strlen(value)] = '\0';
223 return MS_MEDIA_ERR_NONE;
226 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
229 ExifByteOrder mByteOrder;
230 ExifRational mRational;
231 long numerator, denominator;
233 media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
235 entry = exif_data_get_entry(ed, tagtype);
236 media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
239 case EXIF_TAG_ORIENTATION:
240 case EXIF_TAG_PIXEL_X_DIMENSION:
241 case EXIF_TAG_PIXEL_Y_DIMENSION:
242 case EXIF_TAG_ISO_SPEED_RATINGS:
243 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
245 mByteOrder = exif_data_get_byte_order(ed);
246 short exif_value = exif_get_short(entry->data, mByteOrder);
247 *i_value = (int)exif_value;
249 case EXIF_TAG_EXPOSURE_TIME:
250 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
252 mByteOrder = exif_data_get_byte_order(ed);
253 mRational = exif_get_rational(entry->data, mByteOrder);
254 numerator = mRational.numerator;
255 denominator = mRational.denominator;
256 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
258 case EXIF_TAG_FNUMBER:
259 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
261 mByteOrder = exif_data_get_byte_order(ed);
262 mRational = exif_get_rational(entry->data, mByteOrder);
263 numerator = mRational.numerator;
264 denominator = mRational.denominator;
266 *d_value = ((numerator*1.0)/(denominator*1.0));
269 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
271 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
272 buf[strlen(buf)] = '\0';
275 return MS_MEDIA_ERR_NONE;
278 static time_t __media_svc_get_timeline_from_str(const char *timstr)
281 time_t modified_t = 0;
285 if (!STRING_VALID(timstr)) {
286 media_svc_error("Invalid Parameter");
290 /*Exif Format : %Y:%m:%d %H:%M:%S
291 Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
292 memset(&t, 0x00, sizeof(struct tm));
296 localtime_r(&rawtime, &timeinfo);
298 if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
299 t.tm_isdst = timeinfo.tm_isdst;
301 media_svc_debug("DST %d", t.tm_isdst);
303 /* If time string has timezone */
304 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
305 char tim_tmp_str[255] = { 0, };
307 /* ISO8601 Time string format */
308 strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
309 GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
311 modified_t = g_date_time_to_unix(pdatetime);
312 g_date_time_unref(pdatetime);
313 media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
316 modified_t = mktime(&t);
322 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);
324 media_svc_error("Failed to get timeline : [%s]", timstr);
330 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
334 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
335 media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
336 media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
338 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
340 /*categorize from mimetype */
341 for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
342 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
343 *category = (*category | content_category[idx].category_by_mime);
348 /*in application type, exitst sound file ex) x-smafs, asf */
349 if (*category & MEDIA_SVC_CATEGORY_ETC) {
350 int prefix_len = strlen(content_category[3].content_type) + 1;
353 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
354 if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
355 *category ^= MEDIA_SVC_CATEGORY_ETC;
356 *category |= MEDIA_SVC_CATEGORY_SOUND;
361 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
362 *category ^= MEDIA_SVC_CATEGORY_ETC;
363 *category |= MEDIA_SVC_CATEGORY_SOUND;
366 /*"asf" must check video stream and then categorize in directly. */
367 ext = strrchr(path, '.');
369 if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
374 err = mm_file_get_stream_info(path, &audio, &video);
376 if (audio > 0 && video == 0) {
377 *category ^= MEDIA_SVC_CATEGORY_ETC;
378 *category |= MEDIA_SVC_CATEGORY_MUSIC;
380 *category ^= MEDIA_SVC_CATEGORY_ETC;
381 *category |= MEDIA_SVC_CATEGORY_VIDEO;
387 if (g_str_has_suffix(mimetype, "epub+zip") || g_str_has_suffix(mimetype, "pdf")) {
388 *category ^= MEDIA_SVC_CATEGORY_ETC;
389 *category |= MEDIA_SVC_CATEGORY_BOOK;
393 /*check music file in sound files. */
394 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
395 int prefix_len = strlen(content_category[0].content_type) + 1;
397 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
398 if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
399 *category ^= MEDIA_SVC_CATEGORY_SOUND;
400 *category |= MEDIA_SVC_CATEGORY_MUSIC;
405 /*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*/
406 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
407 *category ^= MEDIA_SVC_CATEGORY_SOUND;
408 *category |= MEDIA_SVC_CATEGORY_ETC;
410 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
411 /*some video files don't have video stream. in this case it is categorize as music. */
413 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
414 ext = strrchr(path, '.');
416 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
421 err = mm_file_get_stream_info(path, &audio, &video);
423 if (audio > 0 && video == 0) {
424 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
425 *category |= MEDIA_SVC_CATEGORY_MUSIC;
428 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
433 return MS_MEDIA_ERR_NONE;
436 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
438 int ret = MS_MEDIA_ERR_NONE;
441 ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
442 if (ret != MS_MEDIA_ERR_NONE)
443 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
446 case MEDIA_SVC_CATEGORY_SOUND:
447 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
449 case MEDIA_SVC_CATEGORY_MUSIC:
450 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
452 case MEDIA_SVC_CATEGORY_IMAGE:
453 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
455 case MEDIA_SVC_CATEGORY_VIDEO:
456 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
458 case MEDIA_SVC_CATEGORY_BOOK:
459 *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
462 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
469 drm_contentifo is not NULL, if the file is OMA DRM.
470 If the file is not OMA DRM, drm_contentinfo must be NULL.
472 static int __media_svc_get_mime_type(const char *path, char *mimetype)
474 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
476 /*in case of normal files or failure to get mime in drm */
477 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
478 media_svc_error("aul_get_mime_from_file fail");
479 return MS_MEDIA_ERR_INTERNAL;
482 return MS_MEDIA_ERR_NONE;
485 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
489 for (i = strlen(file_path); i >= 0; i--) {
490 if (file_path[i] == '.') {
491 SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
495 if (file_path[i] == '/')
501 static int __media_svc_safe_atoi(char *buffer, int *si)
505 media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
507 const long sl = strtol(buffer, &end, 10);
509 media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
510 media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
511 media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
512 media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
513 media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
517 return MS_MEDIA_ERR_NONE;
520 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
522 int ret = MS_MEDIA_ERR_NONE;
524 char *thumb_path = NULL;
525 long bsize_kbytes = 0;
526 GError *error = NULL;
528 media_svc_retvm_if(!image || size == 0, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid image");
529 media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
531 ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
532 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path error");
534 ret = statfs(thumb_path, &fs);
536 media_svc_retvm_if(ret == -1, MS_MEDIA_ERR_INTERNAL, "statfs failed");
538 bsize_kbytes = fs.f_bsize >> 10;
539 media_svc_retvm_if((bsize_kbytes * fs.f_bavail) < 1024, MS_MEDIA_ERR_NOT_ENOUGH_SPACE, "Not enough space");
541 if (!g_file_set_contents(image_path, (const gchar *)image, (gssize)size, &error)) {
542 media_svc_error("g_file_set_contents faild:%s", error->message);
544 return MS_MEDIA_ERR_INTERNAL;
547 return MS_MEDIA_ERR_NONE;
550 static char *__media_svc_get_title_from_filepath(const char *path)
552 char *filename = NULL;
554 char *last_dot = NULL;
556 media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
558 filename = g_path_get_basename(path);
560 last_dot = strrchr(filename, '.');
562 title = g_strndup(filename, last_dot - filename);
568 media_svc_debug("extract title is [%s]", title);
573 void _media_svc_remove_file(const char *path)
575 if (!STRING_VALID(path))
578 if (remove(path) != 0)
579 media_svc_stderror("fail to remove file result");
582 int _media_svc_get_thumbnail_path(char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
584 int ret = MS_MEDIA_ERR_NONE;
585 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
586 char hash[THUMB_HASH_LEN] = {0, };
587 char *thumb_dir = NULL;
589 ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
590 media_svc_retvm_if(!STRING_VALID(thumb_dir), MS_MEDIA_ERR_INTERNAL, "ms_user_get_root_thumb_store_path failed");
592 if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
593 media_svc_error("Wrong path[%s]", thumb_dir);
595 return MS_MEDIA_ERR_INTERNAL;
598 memset(file_ext, 0, sizeof(file_ext));
599 if (!__media_svc_get_file_ext(pathname, file_ext))
600 media_svc_error("get file ext fail");
602 ret = mb_svc_generate_hash_code(pathname, hash, THUMB_HASH_LEN);
603 if (ret != MS_MEDIA_ERR_NONE) {
604 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
606 return MS_MEDIA_ERR_INTERNAL;
610 /* 'img_format' is mime-type */
611 if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
612 media_svc_error("Not proper img format");
614 return MS_MEDIA_ERR_INTERNAL;
617 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
619 if (strcasecmp(file_ext, "PNG") == 0)
620 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
622 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
627 return MS_MEDIA_ERR_NONE;
630 int _media_svc_get_file_time(const char *full_path)
632 struct stat statbuf = { 0, };
634 if (stat(full_path, &statbuf) == -1) {
635 media_svc_stderror("stat fails.");
639 return statbuf.st_mtime;
642 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
644 int ret = MS_MEDIA_ERR_NONE;
645 bool drm_type = false;
646 char mime_type[256] = {0, };
647 media_svc_media_type_e media_type;
649 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
650 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
652 content_info->path = g_strdup(path);
653 media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
656 memset(&st, 0, sizeof(struct stat));
657 if (stat(path, &st) == 0) {
658 content_info->modified_time = st.st_mtime;
659 content_info->timeline = content_info->modified_time;
660 content_info->size = st.st_size;
662 media_svc_stderror("stat failed");
665 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
667 media_svc_debug("refresh");
668 return MS_MEDIA_ERR_NONE;
671 content_info->storage_uuid = g_strdup(storage_id);
672 media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
674 content_info->storage_type = storage_type;
675 time(&content_info->added_time);
677 content_info->media_uuid = _media_info_generate_uuid();
678 media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
680 content_info->file_name = g_path_get_basename(path);
681 media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
683 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
684 if drm_contentinfo is not NULL, the file is OMA DRM.*/
685 ret = __media_svc_get_mime_type(path, mime_type);
686 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
688 media_svc_debug("mime [%s]", mime_type);
689 content_info->is_drm = drm_type;
691 ret = __media_svc_get_media_type(path, mime_type, &media_type);
692 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
694 content_info->mime_type = g_strdup(mime_type);
695 media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
697 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
699 content_info->media_type = media_type;
701 return MS_MEDIA_ERR_NONE;
704 static int __image_360_check(const char *path)
707 size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
708 unsigned char exif_header[4] = {0, };
709 unsigned char exif_app1[2] = {0, };
710 unsigned char exif_app1_xmp[2] = {0, };
711 unsigned char exif_app1_xmp_t[2] = {0, };
712 GString *xmp_data = NULL;
716 memset(exif_header, 0x00, sizeof(exif_header));
717 memset(exif_app1, 0x00, sizeof(exif_app1));
718 memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
719 memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
721 fp = fopen(path, "rb");
725 size = fread(exif_header, 1, sizeof(exif_header), fp);
726 if (size != sizeof(exif_header))
729 if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
730 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
731 if (size != sizeof(exif_app1))
734 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
737 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
738 if (fseek(fp, app1_size, SEEK_CUR) != 0)
741 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
742 if (size != sizeof(exif_app1_xmp))
745 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
746 size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
747 if (size != sizeof(exif_app1_xmp_t))
750 if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
753 exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
755 xmp_data = g_string_sized_new(exif_app1_xmp_size);
758 exif_app1_xmp_size--;
765 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
766 } while (exif_app1_xmp_size > 0);
768 if (strstr(xmp_data->str, "UsePanoramaViewer") &&
769 strstr(xmp_data->str, "True") &&
770 strstr(xmp_data->str, "ProjectionType") &&
771 strstr(xmp_data->str, "equirectangular"))
774 g_string_free(xmp_data, TRUE);
787 static char * __media_svc_get_title(MMHandleType tag, const char *path)
789 int ret = FILEINFO_ERROR_NONE;
795 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
796 if (ret == FILEINFO_ERROR_NONE && size > 0) {
797 while(p && isspace(*p))
804 title = __media_svc_get_title_from_filepath(path);
808 media_svc_error("Can't extract title");
810 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
813 char * _media_svc_get_title_by_path(const char *path)
815 /* No MMHandleType in media-svc.c */
816 return __media_svc_get_title(NULL, path);
819 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
822 int orient_value = 0;
826 bool has_datetaken = false;
827 double fnumber = 0.0;
831 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
833 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
834 media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
835 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
837 path = content_info->path;
838 content_info->media_meta.title = __media_svc_get_title(NULL, path);
840 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
841 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
842 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
844 /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
845 content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
846 content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
847 content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
848 content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
849 content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
850 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
851 content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
852 content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
854 /* Load an ExifData object from an EXIF file */
855 ed = exif_data_new_from_file(path);
857 media_svc_sec_debug("There is no exif data in [ %s ]", path);
858 goto GET_WIDTH_HEIGHT;
861 content_info->media_meta.is_360 = __image_360_check(path);
863 memset(buf, 0x00, sizeof(buf));
864 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
865 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
866 if (!g_strcmp0(buf, "S"))
868 content_info->media_meta.latitude = value;
872 memset(buf, 0x00, sizeof(buf));
873 if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
874 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
875 if (!g_strcmp0(buf, "W"))
878 content_info->media_meta.longitude = value;
882 memset(buf, 0x00, sizeof(buf));
883 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
885 content_info->media_meta.description = g_strdup(buf);
887 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
890 memset(buf, 0x00, sizeof(buf));
891 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
892 if (strlen(buf) > 0) {
893 has_datetaken = true;
894 content_info->media_meta.datetaken = g_strdup(buf);
896 /* This is same as recorded_date */
897 content_info->media_meta.recorded_date = g_strdup(buf);
901 memset(buf, 0x00, sizeof(buf));
902 if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
903 if (strlen(buf) > 0) {
904 has_datetaken = true;
905 content_info->media_meta.datetaken = g_strdup(buf);
907 /* This is same as recorded_date */
908 content_info->media_meta.recorded_date = g_strdup(buf);
913 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
914 if (content_info->timeline == 0)
915 content_info->timeline = content_info->modified_time;
917 media_svc_debug("Timeline : %ld", content_info->timeline);
920 memset(buf, 0x00, sizeof(buf));
921 /* Get exposure_time value from exif. */
922 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
924 content_info->media_meta.exposure_time = g_strdup(buf);
927 /* Get fnumber value from exif. */
928 if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
929 content_info->media_meta.fnumber = fnumber;
931 /* Get iso value from exif. */
932 if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
933 content_info->media_meta.iso = iso;
935 memset(buf, 0x00, sizeof(buf));
936 /* Get model value from exif. */
937 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
939 content_info->media_meta.model = g_strdup(buf);
942 /* Get orientation value from exif. */
943 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
944 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
945 content_info->media_meta.orientation = orient_value;
948 /* Get width value from exif. */
949 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
951 content_info->media_meta.width = exif_width;
954 /* Get height value from exif. */
955 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
957 content_info->media_meta.height = exif_height;
965 if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
966 /*Get image width, height*/
967 unsigned int img_width = 0;
968 unsigned int img_height = 0;
969 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
971 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
972 if (content_info->media_meta.width == 0)
973 content_info->media_meta.width = img_width;
975 if (content_info->media_meta.height == 0)
976 content_info->media_meta.height = img_height;
979 return MS_MEDIA_ERR_NONE;
982 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
984 int ret = FILEINFO_ERROR_NONE;
988 ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
989 if (ret == FILEINFO_ERROR_NONE && size > 0)
992 return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
995 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
997 MMHandleType tag = 0;
998 int mmf_error = FILEINFO_ERROR_NONE;
1000 content_info->path = g_strdup(path);
1002 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1003 if (mmf_error == FILEINFO_ERROR_NONE) {
1004 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1005 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1006 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1007 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1008 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1009 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1010 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1011 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1013 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1016 mm_file_destroy_tag_attrs(tag);
1018 return MS_MEDIA_ERR_NONE;
1021 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1023 MMHandleType content = 0;
1024 MMHandleType tag = 0;
1026 unsigned char *image = NULL;
1027 unsigned int size = 0;
1028 int mmf_error = FILEINFO_ERROR_NONE;
1030 int ret = MS_MEDIA_ERR_NONE;
1031 int convert_value = 0;
1034 /*Get Content Tag attribute ===========*/
1035 if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
1036 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1038 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1040 if (mmf_error == FILEINFO_ERROR_NONE) {
1041 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1042 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1043 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1044 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1045 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1046 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1047 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1048 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1049 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1051 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1052 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1053 if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1054 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00 +0000 like exif time format*/
1055 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1056 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1059 content_info->media_meta.recorded_date = g_strdup(p);
1062 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1063 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1064 if (content_info->timeline == 0)
1065 content_info->timeline = content_info->modified_time;
1067 /* This is same as datetaken */
1068 /* Remove compensation string */
1069 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1070 content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1071 g_free(content_info->media_meta.recorded_date);
1072 content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1074 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1079 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1080 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1081 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1082 content_info->media_meta.year = g_strdup(p);
1085 if (!content_info->media_meta.year)
1086 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1088 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1089 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1090 if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1091 content_info->media_meta.rating = convert_value;
1093 content_info->media_meta.rating = 0;
1096 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1098 /*Do not extract artwork for the USB Storage content*/
1099 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1100 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1101 if (mmf_error != FILEINFO_ERROR_NONE)
1102 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1104 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1105 if (mmf_error != FILEINFO_ERROR_NONE)
1106 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1108 if (image != NULL && size > 0) {
1109 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1110 int artwork_mime_size = -1;
1112 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1113 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1114 ret = _media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
1115 if (ret != MS_MEDIA_ERR_NONE) {
1116 media_svc_error("Fail to Get Thumbnail Path");
1118 ret = __media_svc_save_image(image, size, thumb_path, uid);
1119 if (ret != MS_MEDIA_ERR_NONE) {
1120 media_svc_error("Fail to Save Image");
1122 content_info->thumbnail_path = g_strdup(thumb_path);
1129 /*Initialize album_id to 0. below code will set the album_id*/
1130 content_info->album_id = album_id;
1131 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1132 if (ret != MS_MEDIA_ERR_NONE) {
1133 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1134 media_svc_debug("album does not exist. So start to make album art");
1135 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1136 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1137 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1139 ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1141 content_info->album_id = album_id;
1144 content_info->album_id = album_id;
1147 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1148 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1149 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1151 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1152 double longitude = 0.0;
1153 double latitude = 0.0;
1154 double altitude = 0.0;
1156 mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1157 MM_FILE_TAG_LATIDUE, &latitude,
1158 MM_FILE_TAG_ALTIDUE, &altitude,
1161 content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1162 content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1163 content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1165 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1166 if (mmf_error != FILEINFO_ERROR_NONE)
1169 media_svc_debug("CDIS : %d", cdis_value);
1171 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1172 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1173 content_info->media_meta.orientation = atoi(p);
1175 content_info->media_meta.orientation = 0;
1176 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1180 mmf_error = mm_file_destroy_tag_attrs(tag);
1181 if (mmf_error != FILEINFO_ERROR_NONE)
1182 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1184 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1185 content_info->album_id = album_id;
1188 /*Get Content attribute ===========*/
1189 if (cdis_value == 1)
1190 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1192 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1194 media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1196 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1197 int audio_bitrate = 0;
1198 int video_bitrate = 0;
1200 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1201 MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1202 MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1203 MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1204 MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1207 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1209 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1210 MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1211 MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1212 MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1213 MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1217 mm_file_destroy_content_attrs(content);
1219 return MS_MEDIA_ERR_NONE;
1222 static gchar * __media_svc_get_zipfile_data(zip_t *z, const char *fname)
1225 zip_int64_t index_num = 0;
1226 zip_file_t *file = NULL;
1227 zip_stat_t sb = {0, };
1230 media_svc_retvm_if(!z, NULL, "z is NULL");
1231 media_svc_retvm_if(!fname, NULL, "fname is NULL");
1233 index_num = zip_name_locate(z, fname, ZIP_FL_NOCASE);
1234 media_svc_retvm_if(index_num == -1, NULL, "fname is not exists [%s]", fname);
1236 err = zip_stat_index(z, index_num, ZIP_STAT_SIZE, &sb);
1237 media_svc_retvm_if(err == -1, NULL, "zip_stat_index failed");
1239 file = zip_fopen_index(z, index_num, ZIP_FL_UNCHANGED);
1240 media_svc_retvm_if(!file, NULL, "zip_fopen_index failed");
1242 buf = g_malloc0(sb.size);
1244 err = zip_fread(file, buf, sb.size);
1255 static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
1257 xmlNodePtr tmp = NULL;
1259 media_svc_retvm_if(!node, NULL, "node is NULL");
1260 media_svc_retvm_if(!key, NULL, "key is NULL");
1262 for (tmp = node->children; tmp; tmp = tmp->next) {
1263 if (xmlIsBlankNode(tmp))
1266 if (g_str_has_suffix((gchar *)tmp->name, key))
1273 static char * __media_svc_remove_escape_c(const char *value)
1279 media_svc_retv_if(!value, NULL);
1281 len = strlen(value);
1283 for (i = 0; i < len; i++) {
1284 if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
1292 end = end - start + 1;
1294 return g_strndup(value + start, end);
1297 static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
1299 xmlNodePtr tmp = NULL;
1300 char *tmp_res = NULL;
1303 media_svc_retvm_if(!node, NULL, "node is NULL");
1304 media_svc_retvm_if(!key, NULL, "key is NULL");
1306 for (tmp = node->children; tmp; tmp = tmp->next) {
1307 if (xmlIsBlankNode(tmp))
1310 if (tmp->children) {
1311 tmp_res = __media_svc_find_and_get_value(tmp, key);
1313 res = __media_svc_remove_escape_c(tmp_res);
1319 if (g_str_has_suffix((gchar *)tmp->name, key))
1320 return (char *)xmlNodeGetContent(tmp);
1326 static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
1329 xmlDocPtr doc = NULL;
1330 xmlNodePtr node = NULL;
1332 media_svc_retvm_if(!z, FALSE, "z is NULL");
1333 media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
1335 buf = __media_svc_get_zipfile_data(z, "META-INF/container.xml");
1336 media_svc_retvm_if(!buf, FALSE, "buf is NULL");
1338 doc = xmlParseDoc((const xmlChar *)buf);
1340 media_svc_retvm_if(!doc, FALSE, "doc is NULL");
1342 node = xmlDocGetRootElement(doc);
1343 node = __media_svc_find_node(node, "rootfiles");
1344 node = __media_svc_find_node(node, "rootfile");
1346 *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
1347 media_svc_sec_debug("OPF [%s]", *opf_file);
1353 static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
1355 xmlDocPtr doc = NULL;
1356 xmlNodePtr root = NULL;
1358 media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
1359 media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
1361 doc = xmlParseDoc(buffer);
1362 media_svc_retv_if(!doc, FALSE);
1364 root = xmlDocGetRootElement(doc);
1370 content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
1371 if (is_pdf && !content_info->media_meta.title) {
1376 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "creator");
1377 if (!content_info->media_meta.composer)
1378 content_info->media_meta.composer = __media_svc_find_and_get_value(root, "author");
1380 content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
1381 content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
1382 content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
1383 content_info->media_meta.description = __media_svc_find_and_get_value(root, "description");
1390 static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
1395 char *opf_path = NULL;
1397 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1400 z = zip_open(content_info->path, ZIP_RDONLY, &err);
1401 media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
1403 //2. find and read opf file
1404 if (!__media_svc_get_epub_root_file(z, &opf_path)) {
1405 media_svc_error("__media_svc_get_epub_root_file failed");
1407 return MS_MEDIA_ERR_INTERNAL;
1411 buf = __media_svc_get_zipfile_data(z, opf_path);
1414 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_data failed");
1416 if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
1417 media_svc_error("__media_svc_get_xml_metadata failed");
1421 return MS_MEDIA_ERR_NONE;
1424 static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
1430 int search_limit = 0;
1431 char tmp[MEDIA_SVC_PDF_BUF_SIZE + 1] = {0, };
1432 gchar *meta_buf = NULL;
1435 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1436 media_svc_retvm_if(content_info->size < 256, MS_MEDIA_ERR_INTERNAL, "open failed");
1438 fd = open(content_info->path, O_RDONLY);
1439 media_svc_retvm_if(fd < 0, MS_MEDIA_ERR_INTERNAL, "open failed");
1441 search_limit = content_info->size - MEDIA_SVC_PDF_TAG_TAIL_LEN;
1443 while (cur_pos <= search_limit) {
1444 lseek(fd, cur_pos, SEEK_SET);
1446 if (read(fd, &tmp, MEDIA_SVC_PDF_BUF_SIZE) != MEDIA_SVC_PDF_BUF_SIZE) {
1447 media_svc_error("read failed");
1451 //1.Find <x:xmpmeta .. </x:xmpmeta> block
1452 if (start_pos == 0 && (found = strstr(tmp, "<x:xmpmeta"))) {
1453 start_pos = cur_pos + (found - tmp);
1454 // media_svc_error("FIND START_POS[%d]", start_pos);
1459 if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
1460 end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
1461 // media_svc_error("FIND END_POS[%d]", end_pos);
1465 //2.get metadata using xml parser
1466 if (start_pos && end_pos) {
1467 meta_buf = g_malloc0(end_pos - start_pos + 1);
1469 lseek(fd, start_pos, SEEK_SET);
1470 if (read(fd, meta_buf, end_pos - start_pos) != end_pos - start_pos)
1473 if (__media_svc_get_xml_metadata((const xmlChar *)meta_buf, TRUE, content_info)) {
1491 return MS_MEDIA_ERR_NONE;
1494 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
1496 media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
1498 if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
1499 return __media_svc_get_epub_metadata(content_info);
1501 return __media_svc_get_pdf_metadata(content_info);
1504 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1506 media_svc_retm_if(!content_info, "content info is NULL");
1508 /* Delete media_svc_content_info_s */
1509 g_free(content_info->media_uuid);
1510 g_free(content_info->path);
1511 g_free(content_info->file_name);
1512 g_free(content_info->mime_type);
1513 g_free(content_info->folder_uuid);
1514 g_free(content_info->thumbnail_path);
1515 g_free(content_info->storage_uuid);
1517 /* Delete media_svc_content_meta_s */
1518 g_free(content_info->media_meta.title);
1519 g_free(content_info->media_meta.album);
1520 g_free(content_info->media_meta.artist);
1521 g_free(content_info->media_meta.album_artist);
1522 g_free(content_info->media_meta.genre);
1523 g_free(content_info->media_meta.composer);
1524 g_free(content_info->media_meta.year);
1525 g_free(content_info->media_meta.recorded_date);
1526 g_free(content_info->media_meta.copyright);
1527 g_free(content_info->media_meta.track_num);
1528 g_free(content_info->media_meta.description);
1529 g_free(content_info->media_meta.datetaken);
1530 g_free(content_info->media_meta.exposure_time);
1531 g_free(content_info->media_meta.model);
1533 g_free(content_info->file_name_pinyin);
1534 g_free(content_info->media_meta.title_pinyin);
1535 g_free(content_info->media_meta.album_pinyin);
1536 g_free(content_info->media_meta.artist_pinyin);
1537 g_free(content_info->media_meta.album_artist_pinyin);
1538 g_free(content_info->media_meta.genre_pinyin);
1539 g_free(content_info->media_meta.composer_pinyin);
1540 g_free(content_info->media_meta.copyright_pinyin);
1541 g_free(content_info->media_meta.description_pinyin);
1544 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1546 int ret = MS_MEDIA_ERR_NONE;
1548 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1549 media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1550 media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1552 ms_user_storage_type_e store_type = -1;
1553 ret = ms_user_get_storage_type(uid, path, &store_type);
1555 if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1556 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1557 return MS_MEDIA_ERR_INVALID_PARAMETER;
1560 media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1563 ret = _media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
1564 if (ret != MS_MEDIA_ERR_NONE) {
1565 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1566 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1571 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1572 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1574 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1578 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1580 int ret = MS_MEDIA_ERR_NONE;
1582 pinyin_name_s *pinyinname = NULL;
1584 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1585 media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1589 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1590 if (ret == MS_MEDIA_ERR_NONE) {
1591 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1592 *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1594 *pinyin_str = g_strdup(src_str); /* Return Original Non China Character */
1597 _media_svc_pinyin_free(pinyinname, size);
1602 bool _media_svc_check_pinyin_support(void)
1604 int ret = SYSTEM_INFO_ERROR_NONE;
1605 bool is_supported = false;
1606 static int media_svc_pinyin_support = -1;
1608 if (media_svc_pinyin_support == -1) {
1609 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1610 if (ret != SYSTEM_INFO_ERROR_NONE) {
1611 media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1615 media_svc_pinyin_support = is_supported;
1618 return media_svc_pinyin_support;
1621 int _media_svc_get_media_type(const char *path, int *mediatype)
1623 int ret = MS_MEDIA_ERR_NONE;
1624 char mime_type[256] = {0};
1625 media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1627 media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1629 ret = __media_svc_get_mime_type(path, mime_type);
1630 if (ret == MS_MEDIA_ERR_NONE)
1631 __media_svc_get_media_type(path, mime_type, &media_type);
1633 media_svc_error("__media_svc_get_mime_type failed");
1635 *mediatype = media_type;
1640 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1642 switch (storage_type) {
1643 case MS_USER_STORAGE_INTERNAL:
1644 case MS_USER_STORAGE_EXTERNAL:
1645 case MS_USER_STORAGE_EXTERNAL_USB:
1648 media_svc_error("storage type is incorrect[%d]", storage_type);