apply FSL license
[apps/core/preloaded/video-player.git] / src / mp-util-media-service.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *     http://www.tizenopensource.org/license
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19 #include <media-svc.h>
20 #include <visual-svc.h>
21 #include <media-svc-error.h>
22 #include <media-svc-types.h>
23
24 #include "mp-video-log.h"
25 #include "mp-video-value-define.h"
26 #include "mp-video-type-define.h"
27 #include "mp-util-media-service.h"
28 #include "mp-video-string-define.h"
29
30 static Eina_List *VideoItemList = NULL;
31 static Eina_List *pAllTagList = NULL;
32
33 MediaSvcHandle *pMediaSvcHandle = NULL;
34
35 /*
36  * Callback function
37  */
38 static int MpUtilMediaSvcMitemIterateCb(Mitem * pItem, void *pUserData)
39 {
40         VideoLogInfo("Cluster ID: %s, File path:%s", pItem->cluster_uuid, pItem->file_url);
41
42         if (pItem->type == MINFO_ITEM_VIDEO) {
43                 Eina_List **pList = (Eina_List **)pUserData;
44                 *pList = eina_list_append(*pList, pItem);
45         }
46
47         return 0;
48 }
49
50 static int MpUtilMediaSvcMtagIterateGetTagListCb(Mtag * pItem, void *pUserData)
51 {
52         VideoLogInfo("");
53
54         Eina_List **pList = (Eina_List **)pUserData;
55         *pList = eina_list_append(*pList, pItem);
56
57         return 0;
58 }
59
60 /*
61  * Internal function
62  */
63 bool MpUtilMediaSvcGetFileDirectoryName(char *szFilePath, char *szFileName,
64                                         char *szDirectoryName,
65                                         char *szDirectoryPath)
66 {
67         VideoLogInfo("");
68
69         if (!szFilePath) {
70                 VideoLogInfo("[ERR]");
71                 return FALSE;
72         }
73
74         int nCount = 0;
75         int nLoopCount = 0;
76
77         for (nLoopCount = strlen(szFilePath); nLoopCount >= 0; nLoopCount--) {
78                 if (szFilePath[nLoopCount] != '\0')
79                         nCount++;
80
81                 if (szFilePath[nLoopCount] == '/') {
82                         if (szFileName) {
83                                 memcpy(szFileName, &szFilePath[nLoopCount + 1], --nCount);
84                                 *(szFileName + nCount) = '\0';
85                                 VideoLogInfo("File name = %s\n", szFileName);
86                         }
87
88                         if (szDirectoryPath) {
89                                 memcpy(szDirectoryPath, &szFilePath[0], nLoopCount);
90                                 *(szDirectoryPath + nLoopCount + 1) = '\0';
91                                 VideoLogInfo("Directory path = %s", szDirectoryPath);
92                         }
93
94                         if (szDirectoryName) {
95                                 nCount = 0;
96                                 for (--nLoopCount; nLoopCount >= 0; nLoopCount--) {
97                                         nCount++;
98                                         if (szFilePath[nLoopCount] == '/') {
99                                                 memcpy(szDirectoryName, &szFilePath[nLoopCount + 1], --nCount);
100                                                 *(szDirectoryName + nCount) = '\0';
101                                                 VideoLogInfo("Directory Name = %s", szDirectoryName);
102
103                                                 return TRUE;
104                                         }
105                                 }
106                         }
107
108                         return TRUE;
109                 }
110         }
111
112         return FALSE;
113 }
114
115 bool MpUtilMediaSvcDestroyList(Eina_List *pList)
116 {
117         if (!pList) {
118                 VideoLogInfo("pList is empty already.");
119                 return FALSE;
120         }
121
122         Mitem *pMitem = NULL;
123         EINA_LIST_FREE(pList, pMitem) {
124                 if (pMitem) {
125                         minfo_destroy_mtype_item(pMitem);
126                         pMitem = NULL;
127                 }
128         }
129
130         pList = NULL;
131
132         return TRUE;
133 }
134
135 /*
136  * External function
137  */
138
139 void MpUtilMediaSvcInitSession(void)
140 {
141         VideoLogInfo("");
142
143         int nRet = 0;
144
145         nRet = media_svc_connect(&pMediaSvcHandle);
146
147         if(nRet < 0)
148         {
149                 VideoLogInfo("[ERR]");
150         }
151 }
152
153 void MpUtilMediaSvcFinishSession(void)
154 {
155         VideoLogInfo("");
156
157         media_svc_disconnect(pMediaSvcHandle);
158 }
159
160 void MpUtilMediaSvcDestoryVideoList(void)
161 {
162         VideoLogInfo("");
163
164         if (VideoItemList) {
165                 MpUtilMediaSvcDestroyList(VideoItemList);
166                 VideoItemList = NULL;
167         }
168 }
169
170 bool MpUtilMediaSvcDestroyItem(char *szFilePath)
171 {
172         VideoLogInfo("");
173
174         if (!szFilePath) {
175                 VideoLogInfo("[ERR]");
176                 return FALSE;
177         }
178
179         if (!VideoItemList) {
180                 VideoLogInfo("pList is empty already.");
181                 return FALSE;
182         }
183
184         Mitem *pMitem = NULL;
185         Eina_List *pIterateList = NULL;
186         EINA_LIST_FOREACH(VideoItemList, pIterateList, pMitem) {
187                 if (pMitem) {
188                         if (!strcmp(pMitem->file_url, szFilePath)) {
189                                 VideoItemList = eina_list_remove(VideoItemList, pMitem);
190                         }
191                 }
192         }
193         return TRUE;
194 }
195
196 bool MpUtilMediaSvcGetVideoFileListFromFolder(char *szFilePath, int nSortType)
197 {
198         VideoLogInfo("");
199
200         if (!szFilePath) {
201                 VideoLogInfo("[ERR]");
202                 return FALSE;
203         }
204
205         if (VideoItemList) {
206                 VideoLogInfo("Already exist video list.");
207                 return FALSE;
208         }
209
210         VideoLogInfo("szFilePath : %s", szFilePath);
211
212         int nRet = 0;
213         char szFolderID[STR_UUID_LEN_MAX] = { 0, };
214         char szDirectoryPath[STR_LEN_MAX] = { 0, };
215         minfo_item_filter m_Filter = { 0, };
216
217         // Get directory path from filepath.
218         MpUtilMediaSvcGetFileDirectoryName(szFilePath, NULL, NULL, szDirectoryPath);
219         VideoLogInfo("Directory Path : %s", szDirectoryPath);
220
221         // Get folder ID by directory path.
222         nRet = minfo_get_cluster_id_by_url(pMediaSvcHandle, szDirectoryPath, szFolderID, STR_UUID_LEN_MAX);
223         VideoLogInfo("Folder ID : %s", szFolderID);
224
225         // Set sorting type.
226         switch (nSortType) {
227         case MP_MEDIA_SORT_BY_NONE:
228                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NONE;
229                 break;
230
231         case MP_MEDIA_SORT_BY_NAME_DESC:
232                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_DESC;
233                 break;
234
235         case MP_MEDIA_SORT_BY_NAME_ASC:
236                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
237                 break;
238
239         case MP_MEDIA_SORT_BY_DATE_DESC:
240                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_DATE_DESC;
241                 break;
242
243         case MP_MEDIA_SORT_BY_DATE_ASC:
244                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
245                 break;
246
247         default:
248                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
249         }
250
251         // Set sorting type.
252         m_Filter.favorite = MINFO_MEDIA_FAV_ALL;
253         m_Filter.file_type = MINFO_ITEM_VIDEO;
254         m_Filter.start_pos = -1;
255         m_Filter.end_pos = -1;
256         m_Filter.with_meta = TRUE;
257
258         // Get Item List.
259         minfo_get_item_list(pMediaSvcHandle, szFolderID, m_Filter, MpUtilMediaSvcMitemIterateCb, &VideoItemList);
260         return TRUE;
261 }
262
263 bool MpUtilMediaSvcGetVideoFileListFromAllFolderOfGallery(int nSortType)
264 {
265         VideoLogInfo("");
266
267         if (VideoItemList) {
268                 VideoLogInfo("Already exist video list.");
269                 return FALSE;
270         }
271
272         minfo_item_filter m_Filter = { 0 };
273
274         /* Set sorting type. */
275         switch (nSortType) {
276         case MP_MEDIA_SORT_BY_NONE:
277                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NONE;
278                 break;
279
280         case MP_MEDIA_SORT_BY_NAME_DESC:
281                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_DESC;
282                 break;
283
284         case MP_MEDIA_SORT_BY_NAME_ASC:
285                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
286                 break;
287
288         case MP_MEDIA_SORT_BY_DATE_DESC:
289                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_DATE_DESC;
290                 break;
291
292         case MP_MEDIA_SORT_BY_DATE_ASC:
293                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
294                 break;
295
296         default:
297                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
298         }
299
300         VideoLogInfo("Sorting : %d", nSortType);
301
302         /* Set sorting type. */
303         m_Filter.favorite = MINFO_MEDIA_FAV_ALL;
304         m_Filter.file_type = MINFO_ITEM_VIDEO;
305         m_Filter.start_pos = -1;
306         m_Filter.end_pos = -1;
307         m_Filter.with_meta = TRUE;
308
309         /* Get All Item. */
310         minfo_get_all_item_list(pMediaSvcHandle, MINFO_CLUSTER_TYPE_LOCAL_ALL, m_Filter, MpUtilMediaSvcMitemIterateCb, &VideoItemList);
311
312         return TRUE;
313 }
314
315 bool MpUtilMediaSvcGetVideoFileListByTagName(char *szTagName)
316 {
317         VideoLogInfo("");
318
319         if (!szTagName) {
320                 VideoLogInfo("[ERR] No Exist tag name.");
321                 return FALSE;
322         }
323
324         if (VideoItemList) {
325                 VideoLogInfo("Already exist video list.");
326                 return FALSE;
327         }
328
329         VideoLogInfo("szTagName : %s", szTagName);
330
331         int nRet = minfo_get_media_list_by_tagname(pMediaSvcHandle, szTagName, FALSE, MpUtilMediaSvcMitemIterateCb, &VideoItemList);
332         if (nRet < 0) {
333                 VideoLogInfo("Failed to get a media items' list. error code->%d", nRet);
334                 return FALSE;
335         }
336
337         return TRUE;
338 }
339
340 bool MpUtilMediaSvcGetVideoFileListFromFavorite(int nSortType)
341 {
342         VideoLogInfo("");
343
344         if (VideoItemList) {
345                 VideoLogInfo("Already exist video list.");
346                 return FALSE;
347         }
348
349         minfo_item_filter m_Filter = { 0 };
350
351         /* Set sorting type. */
352         switch (nSortType) {
353         case MP_MEDIA_SORT_BY_NONE:
354                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NONE;
355                 break;
356
357         case MP_MEDIA_SORT_BY_NAME_DESC:
358                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_DESC;
359                 break;
360
361         case MP_MEDIA_SORT_BY_NAME_ASC:
362                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
363                 break;
364
365         case MP_MEDIA_SORT_BY_DATE_DESC:
366                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_DATE_DESC;
367                 break;
368
369         case MP_MEDIA_SORT_BY_DATE_ASC:
370                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
371                 break;
372
373         default:
374                 m_Filter.sort_type = MINFO_MEDIA_SORT_BY_DATE_DESC;
375         }
376
377         m_Filter.favorite = MINFO_MEDIA_FAV_ONLY;
378         m_Filter.file_type = MINFO_ITEM_VIDEO;
379         m_Filter.start_pos = -1;
380         m_Filter.end_pos = -1;
381         m_Filter.with_meta = FALSE;
382
383         int nRet = minfo_get_item_list(pMediaSvcHandle, NULL, m_Filter, MpUtilMediaSvcMitemIterateCb, &VideoItemList);
384         if (nRet < 0) {
385                 VideoLogInfo("Failed to get a media items' list. error code->%d", nRet);
386                 return FALSE;
387         }
388
389         return TRUE;
390 }
391
392 void MpUtilMediaSvcMtagIterateDestroyTagList(void)
393 {
394         VideoLogInfo("");
395
396         if (!pAllTagList) {
397                 VideoLogInfo("pAllTagList is empty already.");
398                 return;
399         }
400
401         Mtag *pMtag = NULL;
402         EINA_LIST_FREE(pAllTagList, pMtag) {
403                 if (pMtag) {
404                         if (pMtag->tag_name) {
405                                 free(pMtag->tag_name);
406                                 pMtag->tag_name = NULL;
407                         }
408                         free(pMtag);
409                         pMtag = NULL;
410                 }
411         }
412
413         pAllTagList = NULL;
414 }
415
416 int MpUtilMediaSvcMtagGetAllTagList(char *pTagList[])
417 {
418         VideoLogInfo("");
419
420         int nErr = 0;
421         int nCount = 0;
422
423         if (pAllTagList) {
424                 MpUtilMediaSvcMtagIterateDestroyTagList();
425         }
426
427         minfo_get_tag_list_by_media_id(pMediaSvcHandle, NULL, MpUtilMediaSvcMtagIterateGetTagListCb, &pAllTagList);
428
429         if (nErr == 0 && pAllTagList != NULL) {
430                 Mtag *pMtag = NULL;
431                 Eina_List *pTmpList = NULL;
432
433                 EINA_LIST_FOREACH(pAllTagList, pTmpList, pMtag) {
434                         if (pMtag && pMtag->tag_name) {
435                                 VideoLogInfo("%s of the tag has %d item(s).", pMtag->tag_name, pMtag->count);
436                                 pTagList[nCount++] = pMtag->tag_name;
437                         }
438                 }
439         }
440
441         VideoLogInfo("Size of User tag list is %d.", nCount);
442
443         return nCount;
444 }
445
446 bool MpUtilMediaSvcMtagAddItemToTag(const char *szTagName, char *szMediaUri)
447 {
448         VideoLogInfo("");
449
450         if (!szTagName) {
451                 VideoLogInfo("[ERR] No exist string pointer szTagName.");
452                 return FALSE;
453         }
454
455         Mitem *m_Item = NULL;
456
457         if (szMediaUri) {
458                 minfo_get_item(pMediaSvcHandle, szMediaUri, &m_Item);
459         }
460
461         if (m_Item) {
462                 minfo_add_tag(pMediaSvcHandle, m_Item->uuid, szTagName);
463                 minfo_destroy_mtype_item(m_Item);
464         } else {
465                 minfo_add_tag(pMediaSvcHandle, NULL, szTagName);
466         }
467
468         return TRUE;
469 }
470
471 bool MpUtilMediaSvcMtagGetItemToFavorite(char *szMediaUri)
472 {
473         VideoLogInfo("");
474
475         if (!szMediaUri) {
476                 VideoLogInfo("[ERR] No exist string pointer szMediaUri.");
477                 return FALSE;
478         }
479
480         Mitem *m_Item = NULL;
481
482         minfo_get_item(pMediaSvcHandle, szMediaUri, &m_Item);
483
484         if (m_Item) {
485                 VideoLogInfo("Favorate value : %d", m_Item->rate);
486                 if (m_Item->rate) {
487                         minfo_destroy_mtype_item(m_Item);
488                         return TRUE;
489                 } else {
490                         minfo_destroy_mtype_item(m_Item);
491                         return FALSE;
492                 }
493
494         } else {
495                 VideoLogInfo("[ERR] m_Item is NULL.");
496                 return FALSE;
497         }
498
499         return TRUE;
500 }
501
502 bool MpUtilMediaSvcMtagAddItemToFavorite(char *szMediaUri, bool bFavorite)
503 {
504         VideoLogInfo("");
505
506         if (!szMediaUri) {
507                 VideoLogInfo("[ERR] No exist string pointer szMediaUri.");
508                 return FALSE;
509         }
510
511         Mitem *m_Item = NULL;
512
513         minfo_get_item(pMediaSvcHandle, szMediaUri, &m_Item);
514
515         if (m_Item) {
516                 int nRet = minfo_update_media_favorite(pMediaSvcHandle, m_Item->uuid, bFavorite);
517
518                 if (nRet == 0) {
519                         VideoLogInfo("success to add item to favourites folder.");
520                 } else {
521                         VideoLogInfo("fail - error code : %d ", nRet);
522                 }
523
524                 minfo_destroy_mtype_item(m_Item);
525         } else {
526                 VideoLogInfo("[ERR] m_Item is NULL.");
527                 return FALSE;
528         }
529
530         return TRUE;
531 }
532
533 char *MpUtilMediaSvcMitemGetThumbnailUrl(char *szMediaUri)
534 {
535         VideoLogInfo("");
536
537         if (!szMediaUri) {
538                 VideoLogInfo("[ERR] No exist string pointer szMediaUri.");
539                 return FALSE;
540         }
541
542         Mitem *m_Item = NULL;
543         char *thumb_path = NULL;
544
545         minfo_get_item(pMediaSvcHandle, szMediaUri, &m_Item);
546
547         if (m_Item) {
548                 /* prevent 2011/07/12 */
549                 thumb_path = strdup(m_Item->thumb_url);
550                 minfo_destroy_mtype_item(m_Item);
551                 return thumb_path;
552         }
553
554         return NULL;
555 }
556
557 bool MpUtilMediaSvcGetPreNextVideoUrl(char *szCurrMediaUri, char *szPreMediaUri, char *szNextMediaUri)
558 {
559         VideoLogInfo("");
560
561         if (!szCurrMediaUri) {
562                 VideoLogInfo("[ERR] No exist current file path.");
563                 return FALSE;
564         }
565
566         if (!VideoItemList) {
567                 VideoLogInfo("No exist VideoItemList.");
568                 return FALSE;
569         }
570
571         int nCount = 0;
572         int nListSize = 0;
573         Mitem *m_Item = NULL;
574         Eina_List *pIterateList = NULL;
575
576         memset(szPreMediaUri, 0, sizeof(char) * STR_LEN_MAX);
577         memset(szNextMediaUri, 0, sizeof(char) * STR_LEN_MAX);
578
579         nListSize = eina_list_count(VideoItemList);
580
581         if (nListSize <= 1) {
582                 VideoLogInfo("Video list size == 1.");
583                 return FALSE;
584         }
585
586         EINA_LIST_FOREACH(VideoItemList, pIterateList, m_Item) {
587                 if (m_Item == NULL)
588                         continue;
589
590                 VideoLogInfo("URL of the File item - %s (%d / %d)", m_Item->file_url, nCount, nListSize);
591
592                 if (!strcmp(m_Item->file_url, szCurrMediaUri)) {
593                         m_Item = NULL;
594                         m_Item = (Mitem *) eina_list_nth(VideoItemList, nCount - 1);
595                         if (m_Item) {
596                                 strncpy(szPreMediaUri, m_Item->file_url, (sizeof(char) * STR_LEN_MAX) - 1);
597                         }
598
599                         m_Item = NULL;
600                         m_Item = (Mitem *) eina_list_nth(VideoItemList, nCount + 1);
601                         if (m_Item) {
602                                 strncpy(szNextMediaUri, m_Item->file_url, (sizeof(char) * STR_LEN_MAX) - 1);
603                         }
604
605                         VideoLogInfo("PreMediaUri:%s", szPreMediaUri);
606                         VideoLogInfo("NextMediaUri:%s", szNextMediaUri);
607
608                         return TRUE;
609                 }
610
611                 ++nCount;
612         }
613
614         return FALSE;
615 }
616
617 void MpUtilMediaSvcGetVideoID(const char *szPath, char *szVideoID)
618 {
619
620         VideoLogInfo("");
621
622         Mitem *item = NULL;
623         int ret = 0;
624
625         ret = minfo_get_item(pMediaSvcHandle, szPath, &item);
626         if (ret < 0) {
627                 VideoLogInfo("minfo_get_item API error return:%s, %d", szPath, ret);
628                 return;
629         }
630
631         if (item) {
632                 memset((void*)szVideoID, 0, STR_LEN_MAX);
633                 strncpy(szVideoID, item->uuid, STR_LEN_MAX-1);
634                 minfo_destroy_mtype_item(item);
635         }
636 }
637
638
639 void MpUtilMediaSvcSaveLastPlayedTime(char* szMediaUri, unsigned int nLastPlayedTime)
640 {
641         if (!szMediaUri) {
642                 VideoLogInfo("No exist media uri.");
643                 return;
644         }
645
646         VideoLogInfo("");
647
648         char szVid[STR_LEN_MAX] = {0};
649
650         MpUtilMediaSvcGetVideoID(szMediaUri, szVid);
651
652         if (strlen(szVid) > 0) {
653                 minfo_update_video_meta_info_int(pMediaSvcHandle, szVid, MINFO_VIDEO_META_BOOKMARK_LAST_PLAYED, nLastPlayedTime);
654         } else {
655                 VideoLogInfo("Invalid video ID.");
656         }
657 }