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.
24 #include <media-thumbnail.h>
25 #include "media-svc.h"
26 #include "audio-svc.h"
27 #include "visual-svc.h"
29 #define MEDIA_SVC_PLUGIN_ERROR_NONE 0
30 #define MEDIA_SVC_PLUGIN_ERROR -1
32 #define STRING_VALID(str) \
33 ((str != NULL && strlen(str) > 0) ? TRUE : FALSE)
34 #define STORAGE_VALID(storage)\
35 (((storage == MEDIA_SVC_STORAGE_INTERNAL) || (storage == MEDIA_SVC_STORAGE_EXTERNAL)) ? TRUE : FALSE)
46 }media_svc_error_type_e;
48 #define MS_CATEGORY_UNKNOWN 0x00000000 /**< Default */
49 #define MS_CATEGORY_ETC 0x00000001 /**< ETC category */
50 #define MS_CATEGORY_IMAGE 0x00000002 /**< Image category */
51 #define MS_CATEGORY_VIDEO 0x00000004 /**< Video category */
52 #define MS_CATEGORY_MUSIC 0x00000008 /**< Music category */
53 #define MS_CATEGORY_SOUND 0x00000010 /**< Sound category */
55 #define CONTENT_TYPE_NUM 4
56 #define MUSIC_MIME_NUM 28
57 #define SOUND_MIME_NUM 1
58 #define MIME_TYPE_LENGTH 255
59 #define MIME_LENGTH 50
60 #define _3GP_FILE ".3gp"
61 #define _MP4_FILE ".mp4"
65 char content_type[15];
67 } fex_content_table_t;
69 static const fex_content_table_t content_category[CONTENT_TYPE_NUM] = {
70 {"audio", MS_CATEGORY_SOUND},
71 {"image", MS_CATEGORY_IMAGE},
72 {"video", MS_CATEGORY_VIDEO},
73 {"application", MS_CATEGORY_ETC},
76 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
77 /*known mime types of normal files*/
83 /* known mime types of drm files*/
85 "x-mp3", /*alias of audio/mpeg*/
86 "x-mpeg", /*alias of audio/mpeg*/
88 "x-ogg", /*alias of audio/ogg*/
89 "vnd.ms-playready.media.pya:*.pya", /*playready*/
92 "x-m4a", /*alias of audio/mp4*/
93 /* below mimes are rare*/
103 "x-vorbis", /*alias of audio/x-vorbis+ogg*/
104 "vorbis", /*alias of audio/x-vorbis+ogg*/
106 "x-mp2", /*alias of audio/mp2*/
107 "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
108 "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
111 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
115 static int __get_content_type_from_mime(const char * path, const char * mimetype, int * category);
116 static int __get_content_type(const char * file_path, const char * mime_type);
117 static void __set_error_message(int err_type, char ** err_msg);
119 static int __get_content_type_from_mime(const char * path, const char * mimetype, int * category)
124 *category = MS_CATEGORY_UNKNOWN;
126 //MS_DBG("mime type : %s", mimetype);
128 /*categorize from mimetype */
129 for (i = 0; i < CONTENT_TYPE_NUM; i++) {
130 if (strstr(mimetype, content_category[i].content_type) != NULL) {
131 *category = (*category | content_category[i].category_by_mime);
136 /*in application type, exitst sound file ex) x-smafs */
137 if (*category & MS_CATEGORY_ETC) {
138 int prefix_len = strlen(content_category[0].content_type);
140 for (i = 0; i < SOUND_MIME_NUM; i++) {
141 if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
142 *category ^= MS_CATEGORY_ETC;
143 *category |= MS_CATEGORY_SOUND;
149 /*check music file in soun files. */
150 if (*category & MS_CATEGORY_SOUND) {
151 int prefix_len = strlen(content_category[0].content_type) + 1;
153 //MS_DBG("mime_type : %s", mimetype + prefix_len);
155 for (i = 0; i < MUSIC_MIME_NUM; i++) {
156 if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
157 *category ^= MS_CATEGORY_SOUND;
158 *category |= MS_CATEGORY_MUSIC;
162 } else if (*category & MS_CATEGORY_VIDEO) {
163 /*some video files don't have video stream. in this case it is categorize as music. */
165 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
166 ext = strrchr(path, '.');
168 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
172 err = mm_file_get_stream_info(path, &audio, &video);
174 if (audio > 0 && video == 0) {
175 *category ^= MS_CATEGORY_VIDEO;
176 *category |= MS_CATEGORY_MUSIC;
183 //MS_DBG("category_from_ext : %d", *category);
188 static int __get_content_type(const char * file_path, const char * mime_type)
190 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
193 ret = __get_content_type_from_mime(file_path, mime_type, &category);
197 if (category & MS_CATEGORY_SOUND) return MEDIA_SVC_MEDIA_TYPE_SOUND;
198 else if (category & MS_CATEGORY_MUSIC) return MEDIA_SVC_MEDIA_TYPE_MUSIC;
199 else if (category & MS_CATEGORY_IMAGE) return MEDIA_SVC_MEDIA_TYPE_IMAGE;
200 else if (category & MS_CATEGORY_VIDEO) return MEDIA_SVC_MEDIA_TYPE_VIDEO;
201 else return MEDIA_SVC_MEDIA_TYPE_OTHER;
204 static void __set_error_message(int err_type, char ** err_msg)
209 if(err_type == ERR_FILE_PATH)
210 *err_msg = strdup("invalid file path");
211 if(err_type == ERR_HANDLE)
212 *err_msg = strdup("invalid handle");
213 else if(err_type == ERR_MIME_TYPE)
214 *err_msg = strdup("invalid mime type");
215 else if(err_type == ERR_NOT_MEDIAFILE)
216 *err_msg = strdup("not media content");
217 else if(err_type == ERR_STORAGE_TYPE)
218 *err_msg = strdup("invalid storage type");
219 else if(err_type == ERR_CHECK_ITEM)
220 *err_msg = strdup("item does not exist");
221 else if(err_type == MEDIA_INFO_ERROR_DATABASE_CONNECT)
222 *err_msg = strdup("DB connect error");
223 else if(err_type == MEDIA_INFO_ERROR_DATABASE_DISCONNECT)
224 *err_msg = strdup("DB disconnect error");
225 else if((err_type == AUDIO_SVC_ERROR_INVALID_PARAMETER) || (err_type == MB_SVC_ERROR_INVALID_PARAMETER) || (err_type == MEDIA_INFO_ERROR_INVALID_PARAMETER))
226 *err_msg = strdup("invalid parameter");
227 else if((err_type == AUDIO_SVC_ERROR_DB_INTERNAL) ||(err_type == MB_SVC_ERROR_DB_INTERNAL) ||(err_type == MEDIA_INFO_ERROR_DATABASE_INTERNAL))
228 *err_msg = strdup("DB internal error");
229 else if((err_type == AUDIO_SVC_ERROR_INTERNAL) ||(err_type == MB_SVC_ERROR_INTERNAL) || (err_type == MEDIA_INFO_ERROR_INTERNAL))
230 *err_msg = strdup("media service internal error");
232 *err_msg = strdup("error unknown");
236 int check_item(const char *file_path, const char * mime_type, char ** err_msg)
238 if (!STRING_VALID(file_path)) {
239 __set_error_message(ERR_FILE_PATH, err_msg);
240 return MEDIA_SVC_PLUGIN_ERROR;
243 if (!STRING_VALID(mime_type)) {
244 __set_error_message(ERR_MIME_TYPE, err_msg);
245 return MEDIA_SVC_PLUGIN_ERROR;
248 return MEDIA_SVC_PLUGIN_ERROR_NONE;
251 int connect(void ** handle, char ** err_msg)
253 int ret = media_svc_connect(handle);
256 __set_error_message(ret, err_msg);
257 return MEDIA_SVC_PLUGIN_ERROR;
260 return MEDIA_SVC_PLUGIN_ERROR_NONE;
263 int disconnect(void * handle, char ** err_msg)
265 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
268 __set_error_message(ERR_HANDLE, err_msg);
269 return MEDIA_SVC_PLUGIN_ERROR;
272 ret = media_svc_disconnect(handle);
274 __set_error_message(ret, err_msg);
275 return MEDIA_SVC_PLUGIN_ERROR;
278 return MEDIA_SVC_PLUGIN_ERROR_NONE;
281 int check_item_exist(void* handle, const char *file_path, int storage_type, char ** err_msg)
283 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
286 __set_error_message(ERR_HANDLE, err_msg);
287 return MEDIA_SVC_PLUGIN_ERROR;
290 if (!STRING_VALID(file_path)) {
291 __set_error_message(ERR_FILE_PATH, err_msg);
292 return MEDIA_SVC_PLUGIN_ERROR;
295 if(!STORAGE_VALID(storage_type)) {
296 __set_error_message(ERR_STORAGE_TYPE, err_msg);
297 return MEDIA_SVC_PLUGIN_ERROR;
300 ret = media_svc_check_item_exist_by_path(handle, file_path);
301 if(ret == MEDIA_INFO_ERROR_NONE)
302 return MEDIA_SVC_PLUGIN_ERROR_NONE; //exist
304 __set_error_message(ERR_CHECK_ITEM, err_msg);
306 return MEDIA_SVC_PLUGIN_ERROR; //not exist
309 int insert_item_begin(void * handle, int item_cnt, char ** err_msg)
311 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
314 __set_error_message(ERR_HANDLE, err_msg);
315 return MEDIA_SVC_PLUGIN_ERROR;
318 ret = media_svc_insert_item_begin(handle, item_cnt);
320 __set_error_message(ret, err_msg);
321 return MEDIA_SVC_PLUGIN_ERROR;
324 return MEDIA_SVC_PLUGIN_ERROR_NONE;
327 int insert_item_end(void * handle, char ** err_msg)
329 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
332 __set_error_message(ERR_HANDLE, err_msg);
333 return MEDIA_SVC_PLUGIN_ERROR;
336 ret = media_svc_insert_item_end(handle);
338 __set_error_message(ret, err_msg);
339 return MEDIA_SVC_PLUGIN_ERROR;
342 return MEDIA_SVC_PLUGIN_ERROR_NONE;
345 int insert_item(void * handle, const char *file_path, int storage_type, const char * mime_type, char ** err_msg)
347 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
350 __set_error_message(ERR_HANDLE, err_msg);
351 return MEDIA_SVC_PLUGIN_ERROR;
354 if (!STRING_VALID(file_path)) {
355 __set_error_message(ERR_FILE_PATH, err_msg);
356 return MEDIA_SVC_PLUGIN_ERROR;
359 if (!STRING_VALID(mime_type)) {
360 __set_error_message(ERR_MIME_TYPE, err_msg);
361 return MEDIA_SVC_PLUGIN_ERROR;
364 if(!STORAGE_VALID(storage_type)) {
365 __set_error_message(ERR_STORAGE_TYPE, err_msg);
366 return MEDIA_SVC_PLUGIN_ERROR;
369 media_svc_media_type_e content_type = __get_content_type(file_path, mime_type);
371 ret = media_svc_insert_item_bulk(handle, storage_type, file_path, mime_type, content_type);
373 __set_error_message(ret, err_msg);
374 return MEDIA_SVC_PLUGIN_ERROR;
377 return MEDIA_SVC_PLUGIN_ERROR_NONE;
380 int insert_item_immediately(void * handle, const char *file_path, int storage_type, const char * mime_type, char ** err_msg)
382 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
385 __set_error_message(ERR_HANDLE, err_msg);
386 return MEDIA_SVC_PLUGIN_ERROR;
389 if (!STRING_VALID(file_path)) {
390 __set_error_message(ERR_FILE_PATH, err_msg);
391 return MEDIA_SVC_PLUGIN_ERROR;
394 if (!STRING_VALID(mime_type)) {
395 __set_error_message(ERR_MIME_TYPE, err_msg);
396 return MEDIA_SVC_PLUGIN_ERROR;
399 if(!STORAGE_VALID(storage_type)) {
400 __set_error_message(ERR_STORAGE_TYPE, err_msg);
401 return MEDIA_SVC_PLUGIN_ERROR;
404 media_svc_media_type_e content_type = __get_content_type(file_path, mime_type);
406 ret = media_svc_insert_item_immediately(handle, storage_type, file_path, mime_type, content_type);
409 __set_error_message(ret, err_msg);
410 return MEDIA_SVC_PLUGIN_ERROR;
413 return MEDIA_SVC_PLUGIN_ERROR_NONE;
416 int move_item_begin(void * handle, int item_cnt, char ** err_msg)
418 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
421 __set_error_message(ERR_HANDLE, err_msg);
422 return MEDIA_SVC_PLUGIN_ERROR;
425 ret = media_svc_move_item_begin(handle, item_cnt);
427 __set_error_message(ret, err_msg);
428 return MEDIA_SVC_PLUGIN_ERROR;
431 return MEDIA_SVC_PLUGIN_ERROR_NONE;
434 int move_item_end(void * handle, char ** err_msg)
436 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
439 __set_error_message(ERR_HANDLE, err_msg);
440 return MEDIA_SVC_PLUGIN_ERROR;
443 ret = media_svc_move_item_end(handle);
445 __set_error_message(ret, err_msg);
446 return MEDIA_SVC_PLUGIN_ERROR;
449 return MEDIA_SVC_PLUGIN_ERROR_NONE;
452 int move_item(void * handle, const char *src_path, int src_storage_type, const char *dest_path, int dest_storage_type, const char * mime_type, char ** err_msg)
454 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
457 __set_error_message(ERR_HANDLE, err_msg);
458 return MEDIA_SVC_PLUGIN_ERROR;
461 if ((!STRING_VALID(src_path)) || (!STRING_VALID(dest_path))) {
462 __set_error_message(ERR_FILE_PATH, err_msg);
463 return MEDIA_SVC_PLUGIN_ERROR;
466 if (!STRING_VALID(mime_type)) {
467 __set_error_message(ERR_MIME_TYPE, err_msg);
468 return MEDIA_SVC_PLUGIN_ERROR;
471 if((!STORAGE_VALID(src_storage_type)) || (!STORAGE_VALID(dest_storage_type))) {
472 __set_error_message(ERR_STORAGE_TYPE, err_msg);
473 return MEDIA_SVC_PLUGIN_ERROR;
476 ret = media_svc_move_item(handle, src_storage_type, src_path, dest_storage_type, dest_path);
478 __set_error_message(ret, err_msg);
479 return MEDIA_SVC_PLUGIN_ERROR;
482 return MEDIA_SVC_PLUGIN_ERROR_NONE;
485 int set_all_storage_items_validity(void * handle, int storage_type, int validity, char ** err_msg)
487 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
490 __set_error_message(ERR_HANDLE, err_msg);
491 return MEDIA_SVC_PLUGIN_ERROR;
494 if(!STORAGE_VALID(storage_type)) {
495 __set_error_message(ERR_STORAGE_TYPE, err_msg);
496 return MEDIA_SVC_PLUGIN_ERROR;
499 ret = media_svc_set_all_storage_items_validity(handle, storage_type, validity);
501 __set_error_message(ret, err_msg);
502 return MEDIA_SVC_PLUGIN_ERROR;
505 return MEDIA_SVC_PLUGIN_ERROR_NONE;
508 int set_item_validity_begin(void * handle, int item_cnt, char ** err_msg)
510 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
513 __set_error_message(ERR_HANDLE, err_msg);
514 return MEDIA_SVC_PLUGIN_ERROR;
517 ret = media_svc_set_item_validity_begin(handle, item_cnt);
519 __set_error_message(ret, err_msg);
520 return MEDIA_SVC_PLUGIN_ERROR;
523 return MEDIA_SVC_PLUGIN_ERROR_NONE;
526 int set_item_validity_end(void * handle, char ** err_msg)
528 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
531 __set_error_message(ERR_HANDLE, err_msg);
532 return MEDIA_SVC_PLUGIN_ERROR;
535 ret = media_svc_set_item_validity_end(handle);
537 __set_error_message(ret, err_msg);
538 return MEDIA_SVC_PLUGIN_ERROR;
541 return MEDIA_SVC_PLUGIN_ERROR_NONE;
544 int set_item_validity(void * handle, const char *file_path, int storage_type, const char * mime_type, int validity, char ** err_msg)
546 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
549 __set_error_message(ERR_HANDLE, err_msg);
550 return MEDIA_SVC_PLUGIN_ERROR;
553 if (!STRING_VALID(file_path)) {
554 __set_error_message(ERR_FILE_PATH, err_msg);
555 return MEDIA_SVC_PLUGIN_ERROR;
558 if (!STRING_VALID(mime_type)) {
559 __set_error_message(ERR_MIME_TYPE, err_msg);
560 return MEDIA_SVC_PLUGIN_ERROR;
563 if(!STORAGE_VALID(storage_type)) {
564 __set_error_message(ERR_STORAGE_TYPE, err_msg);
565 return MEDIA_SVC_PLUGIN_ERROR;
568 ret = media_svc_set_item_validity(handle, file_path, validity);
571 __set_error_message(ret, err_msg);
572 return MEDIA_SVC_PLUGIN_ERROR;
575 return MEDIA_SVC_PLUGIN_ERROR_NONE;
578 int delete_item(void * handle, const char *file_path, int storage_type, char ** err_msg)
580 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
583 __set_error_message(ERR_HANDLE, err_msg);
584 return MEDIA_SVC_PLUGIN_ERROR;
587 if (!STRING_VALID(file_path)) {
588 __set_error_message(ERR_FILE_PATH, err_msg);
589 return MEDIA_SVC_PLUGIN_ERROR;
592 if(!STORAGE_VALID(storage_type)) {
593 __set_error_message(ERR_STORAGE_TYPE, err_msg);
594 return MEDIA_SVC_PLUGIN_ERROR;
597 ret = media_svc_check_item_exist_by_path(handle, file_path);
599 ret = media_svc_delete_item_by_path(handle, file_path);
602 __set_error_message(ret, err_msg);
603 return MEDIA_SVC_PLUGIN_ERROR;
606 return MEDIA_SVC_PLUGIN_ERROR_NONE;
609 __set_error_message(ERR_CHECK_ITEM, err_msg); //not exist in DB so can't delete item.
610 return MEDIA_SVC_PLUGIN_ERROR;
613 int delete_all_items_in_storage(void * handle, int storage_type, char ** err_msg)
615 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
618 __set_error_message(ERR_HANDLE, err_msg);
619 return MEDIA_SVC_PLUGIN_ERROR;
622 if(!STORAGE_VALID(storage_type)) {
623 __set_error_message(ERR_STORAGE_TYPE, err_msg);
624 return MEDIA_SVC_PLUGIN_ERROR;
627 ret = media_svc_delete_all_items_in_storage(handle, storage_type);
629 __set_error_message(ret, err_msg);
630 return MEDIA_SVC_PLUGIN_ERROR;
633 return MEDIA_SVC_PLUGIN_ERROR_NONE;
636 int delete_all_invalid_items_in_storage(void * handle, int storage_type, char ** err_msg)
638 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
641 __set_error_message(ERR_HANDLE, err_msg);
642 return MEDIA_SVC_PLUGIN_ERROR;
645 if(!STORAGE_VALID(storage_type)) {
646 __set_error_message(ERR_STORAGE_TYPE, err_msg);
647 return MEDIA_SVC_PLUGIN_ERROR;
650 ret = media_svc_delete_invalid_items_in_storage(handle, storage_type);
652 __set_error_message(ret, err_msg);
653 return MEDIA_SVC_PLUGIN_ERROR;
656 return MEDIA_SVC_PLUGIN_ERROR_NONE;
659 int delete_all_items(void * handle, char ** err_msg)
661 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
664 __set_error_message(ERR_HANDLE, err_msg);
665 return MEDIA_SVC_PLUGIN_ERROR;
668 ret = delete_all_items_in_storage(handle, MEDIA_SVC_STORAGE_INTERNAL, err_msg);
670 return MEDIA_SVC_PLUGIN_ERROR;
672 ret = delete_all_items_in_storage(handle, MEDIA_SVC_STORAGE_EXTERNAL, err_msg);
674 return MEDIA_SVC_PLUGIN_ERROR;
676 return MEDIA_SVC_PLUGIN_ERROR_NONE;
679 int refresh_item(void * handle, const char *file_path, int storage_type, const char * mime_type, char ** err_msg)
681 int ret = MEDIA_SVC_PLUGIN_ERROR_NONE;
684 __set_error_message(ERR_HANDLE, err_msg);
685 return MEDIA_SVC_PLUGIN_ERROR;
688 if (!STRING_VALID(file_path)) {
689 __set_error_message(ERR_FILE_PATH, err_msg);
690 return MEDIA_SVC_PLUGIN_ERROR;
693 if (!STRING_VALID(mime_type)) {
694 __set_error_message(ERR_MIME_TYPE, err_msg);
695 return MEDIA_SVC_PLUGIN_ERROR;
698 if(!STORAGE_VALID(storage_type)) {
699 __set_error_message(ERR_STORAGE_TYPE, err_msg);
700 return MEDIA_SVC_PLUGIN_ERROR;
703 media_svc_media_type_e content_type = __get_content_type(file_path, mime_type);
705 ret = media_svc_refresh_item(handle, storage_type, file_path, content_type);
708 __set_error_message(ret, err_msg);
709 return MEDIA_SVC_PLUGIN_ERROR;
712 return MEDIA_SVC_PLUGIN_ERROR_NONE;
715 int update_begin(void)
717 return MEDIA_SVC_PLUGIN_ERROR_NONE;
722 return MEDIA_SVC_PLUGIN_ERROR_NONE;