Merge branch 'master' into 2.0_beta
[apps/core/preloaded/video-player.git] / src / mp-video-service-ctrl.c
1 /*
2  * To apply the Flora License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
3  * 
4  *    Copyright [2012] [JongDong Lee <jongdong.lee@samsung.com>, ChangSun Lee <cs78.lee@samsung.com>]
5  * 
6  *    Licensed under the Flora License, Version 1.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  * 
10  *        http://www.tizenopensource.org/license
11  * 
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19
20 #include <app.h>
21 #include <aul.h>
22 #include <glib.h>
23
24 #include "mp-util.h"
25 #include "mp-video-log.h"
26 #include "mp-video-service-ctrl.h"
27
28
29 #define MP_VIDEO_LAUNCHED_BY_APP_KEY            "launching_application"
30 #define MP_VIDEO_SORT_TYPE_KEY                          "order_type"
31 #define MP_VIDEO_LIST_TYPE_KEY                          "video_list_type"
32 #define MP_VIDEO_TAG_NAME_KEY                           "tag_name"
33 #define MP_VIDEO_EDIT_MODE_KEY                          "edit_mode"
34 #define MP_VIDEO_START_POS_TIME_KEY                     "start_pos_time"
35 #define MP_VIDEO_URI_PATH                                       "path"
36 #define MP_VIDEO_COOKIE                                         "cookie"
37 #define MP_VIDEO_MULTI_WIN_TYPE_KEY                     "is_multi_win_type"
38 #define MP_VIDEO_LAUNCH_TYPE                            "launch-type"
39 #define MP_VIDEO_SHORTCUT_VIDEO                         "shortcut-videoplayer"
40
41 static service_h pAppSvcHandle = NULL;
42 static int nPlayerType = MP_NONE_VIEW_TYPE;
43 static int nPrevPlayerType = MP_NONE_VIEW_TYPE;
44 static int nGalleryLatestTime = 0;
45 static char *g_szStreamingCookie = NULL;
46 static bool bIsShortcutType = FALSE;
47
48
49 /////////////////////////////////////////////////////////////////////////////
50 // Internal Function
51
52 bool MpVideoServiceCtrlCheckMime(service_h pVideoServiceHandle, char **szMediaUri)
53 {
54         VideoLogInfo("");
55
56         if(!pVideoServiceHandle)
57         {
58                 VideoLogInfo("[ERR] No Exist Service handle.");
59                 return FALSE;
60         }
61
62         bool bRet = TRUE;
63         char *szMimeContent = NULL;
64
65         if(service_get_extra_data(pVideoServiceHandle, AUL_K_MIME_CONTENT, &szMimeContent) != SERVICE_ERROR_NONE)
66         {
67                 VideoLogInfo("No exist mime type.");
68                 bRet = FALSE;
69         }
70
71         if(szMimeContent)
72         {
73                 *szMediaUri = (char*)malloc(strlen(szMimeContent) + 1);
74                 memset(*szMediaUri, 0, strlen(szMimeContent) + 1);
75                 strncpy(*szMediaUri, szMimeContent, strlen(szMimeContent));
76                 VideoLogInfo("Mime content : %s", *szMediaUri);
77                 free(szMimeContent);
78                 bRet = TRUE;
79         }
80         else
81         {
82                 VideoLogInfo("No exist MIME type.");
83                 bRet = FALSE;
84         }
85
86         return bRet;
87 }
88
89 bool MpVideoServiceCtrlCheckBundle(service_h pVideoServiceHandle, char **szMediaUri)
90 {
91         if(!pVideoServiceHandle)
92         {
93                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
94                 return FALSE;
95         }
96
97         bool bRet = TRUE;
98         char *szStrVideoURI = NULL;
99
100         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_URI_PATH, &szStrVideoURI) != SERVICE_ERROR_NONE)
101         {
102                 VideoLogInfo("No exist KEY of video/streaming URI.");
103                 bRet = FALSE;
104                 return bRet;
105         }
106
107         if(szStrVideoURI)
108         {
109                 *szMediaUri = (char*)malloc(strlen(szStrVideoURI) + 1);
110                 memset(*szMediaUri, 0, strlen(szStrVideoURI) + 1);
111                 strncpy(*szMediaUri, szStrVideoURI, strlen(szStrVideoURI));
112                 VideoLogInfo("Video/Streaming URI path : %s", *szMediaUri);
113                 free(szStrVideoURI);
114                 szStrVideoURI = NULL;
115                 bRet = TRUE;
116         }
117         else
118         {
119                 VideoLogInfo("No exist video/streaming URI.");
120                 bRet = FALSE;
121         }
122
123         return bRet;
124 }
125
126 bool MpVideoServiceCtrlCheckAUL(service_h pVideoServiceHandle, char **szMediaUri)
127 {
128         if(!pVideoServiceHandle)
129         {
130                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
131                 return FALSE;
132         }
133
134
135         char *pGetOperation = NULL;
136
137         service_get_operation(pVideoServiceHandle, &pGetOperation);
138
139         if(pGetOperation == NULL)
140         {
141                 return FALSE;
142         }
143
144         if(!strcmp(SERVICE_OPERATION_VIEW , pGetOperation))
145         {
146                 char *szStrVideoURI = NULL;
147
148                 service_get_uri(pVideoServiceHandle, &szStrVideoURI);
149
150                 if(szStrVideoURI)
151                 {
152                         *szMediaUri = (char*)malloc(strlen(szStrVideoURI) + 1);
153                         memset(*szMediaUri, 0, strlen(szStrVideoURI) + 1);
154                         strncpy(*szMediaUri, szStrVideoURI, strlen(szStrVideoURI));
155                         VideoLogInfo("Video/Streaming URI path : %s", *szMediaUri);
156                         free(szStrVideoURI);
157                         szStrVideoURI = NULL;
158                 }
159                 else
160                 {
161                         VideoLogInfo("[ERR] No exist video/streaming URI.");
162
163                         if(pGetOperation)
164                         {
165                                 free(pGetOperation);
166                                 pGetOperation = NULL;
167                         }
168
169                         return FALSE;
170                 }
171         }
172         else
173         {
174                 VideoLogInfo("No exist Operation.");
175
176                 if(pGetOperation)
177                 {
178                         free(pGetOperation);
179                         pGetOperation = NULL;
180                 }
181
182                 return FALSE;
183         }
184
185         VideoLogInfo("Video/Streaming URI path pAppData->szMediaUri : %s", *szMediaUri);
186
187         if(pGetOperation)
188         {
189         free(pGetOperation);
190                 pGetOperation = NULL;
191         }
192
193         return TRUE;
194 }
195
196 bool MpVideoServiceCtrlIsXMLUrl(char **szMediaUri)
197 {
198         VideoLogInfo("URI : %s", *szMediaUri);
199
200         if (!g_str_has_suffix(*szMediaUri, ".xml")) {
201                 VideoLogInfo("No have xml uri.");
202                 return FALSE;
203         }
204
205         return TRUE;
206 }
207
208 bool MpVideoServiceCtrlParseUri(service_h pVideoServiceHandle, char** szMediaUri)
209 {
210         VideoLogInfo("");
211
212         if(!MpVideoServiceCtrlCheckMime(pVideoServiceHandle, szMediaUri))
213         {
214                 if(!MpVideoServiceCtrlCheckBundle(pVideoServiceHandle, szMediaUri))
215                 {
216                         if(!MpVideoServiceCtrlCheckAUL(pVideoServiceHandle, szMediaUri))
217                         {
218                                 VideoLogInfo("No exist URI path.");
219                                 return FALSE;
220                         }
221                 }
222         }
223
224         VideoLogInfo("szMediaUri : %s", *szMediaUri);
225
226         return TRUE;
227 }
228
229 int MpVideoServiceCtrlCheckUriType(char* szMediaUri)
230 {
231         if(!szMediaUri)
232         {
233                 VideoLogInfo("[ERR] No exist media uri.");
234                 return MP_ERROR_PLAYER_TYPE;
235         }
236
237         VideoLogInfo("");
238
239         int nRet = 0;
240
241         if(MpUtilCheckUriType(szMediaUri))
242                 {
243                         VideoLogInfo("MP_STREAMING_PLAYER");
244                         nRet = MP_STREAMING_PLAYER;
245                 }
246                 else
247                 {
248                         VideoLogInfo("MP_VIDEO_PLAYER");
249                         nRet = MP_VIDEO_PLAYER;
250                 }
251
252         return nRet;
253 }
254
255 bool MpVideoServiceCtrlCheckCookieForStreaming(service_h pVideoServiceHandle)
256 {
257         if(!pVideoServiceHandle)
258         {
259                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
260                 return FALSE;
261         }
262
263         bool bRet = TRUE;
264         char *pStrVideoCookie = NULL;
265
266         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_COOKIE, &pStrVideoCookie) != SERVICE_ERROR_NONE)
267         {
268                 VideoLogInfo("[ERR] No exist KEY of streaming COOKIE.");
269                 bRet = FALSE;
270         }
271
272         if(pStrVideoCookie)
273         {
274                 g_szStreamingCookie = (char*)malloc(strlen(pStrVideoCookie) + 1);
275                 memset(g_szStreamingCookie, 0, strlen(pStrVideoCookie) + 1);
276                 strncpy(g_szStreamingCookie, pStrVideoCookie, strlen(pStrVideoCookie));
277                 VideoLogInfo("Video/Streaming URI path Cookie : %s", g_szStreamingCookie);
278                 free(pStrVideoCookie);
279         }
280         else
281         {
282                 VideoLogInfo("[ERR] No exist video/streaming URI.");
283                 bRet = FALSE;
284         }
285
286         return TRUE;
287 }
288
289 int MpVideoServiceCtrlCheckStartPositionTime(service_h pVideoServiceHandle, char *szBundleKey)
290 {
291         VideoLogInfo("");
292
293         if(!pVideoServiceHandle)
294         {
295                 VideoLogInfo("No exist Service handle data.");
296                 return 0;
297         }
298
299         int nStartPositionTime = 0;
300         char *szStartPosTime = NULL;
301
302         if(service_get_extra_data(pVideoServiceHandle, szBundleKey, &szStartPosTime) != SERVICE_ERROR_NONE)
303         {
304                 VideoLogInfo("No exist mime type.");
305                 return 0;
306         }
307
308         if(!szStartPosTime)
309         {
310                 VideoLogInfo("No exist pointer of position time.");
311                 return 0;
312         }
313
314         nStartPositionTime = atoi(szStartPosTime);
315
316         VideoLogInfo("Start position time : %d", nStartPositionTime);
317
318         free(szStartPosTime);
319
320         if(nStartPositionTime < 0)
321         {
322                 nStartPositionTime = 0;
323         }
324
325         return nStartPositionTime;
326 }
327
328 bool MpVideoServiceCtrlGetTagNameOfGallery(service_h pVideoServiceHandle, char *szBundleKey, char *szTagName)
329 {
330         VideoLogInfo("");
331
332         if(!pVideoServiceHandle)
333         {
334                 VideoLogInfo("No exist Service handle.");
335                 return FALSE;
336         }
337
338         if(!szBundleKey)
339         {
340                 VideoLogInfo("No exist Service handle kay.");
341                 return FALSE;
342         }
343
344         if(!szTagName)
345         {
346                 VideoLogInfo("No exist pAppData.");
347                 return FALSE;
348         }
349
350         bool nRet = TRUE;
351         char *szVideoTagName = NULL;
352
353         if(service_get_extra_data(pVideoServiceHandle, szBundleKey, &szVideoTagName) != SERVICE_ERROR_NONE)
354         {
355                 VideoLogInfo("No exist mime type.");
356                 nRet = FALSE;
357         }
358
359         if(szVideoTagName)
360         {
361                 strncpy(szTagName, szVideoTagName, STR_LEN_MAX - 1);
362                 VideoLogInfo("szTagName : %s", szTagName);
363                 free(szVideoTagName);
364                 nRet = TRUE;
365         }
366         else
367         {
368                 VideoLogInfo("No exist pointer of szVideoTagName.");
369                 nRet = FALSE;
370         }
371
372         return nRet;
373 }
374
375 int MpVideoServiceCtrlCheckLauncher(service_h pVideoServiceHandle)
376 {
377         VideoLogInfo("");
378
379         if(!pVideoServiceHandle)
380         {
381                 VideoLogInfo("No exist Service handle data.");
382                 return MP_VIDEO_PLAYER_SIMPLE;
383         }
384
385         char *szLaunchingByOtherApp = NULL;
386
387         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_LAUNCHED_BY_APP_KEY, &szLaunchingByOtherApp) != SERVICE_ERROR_NONE)
388         {
389                 VideoLogInfo("No exist Service handle key of MP_VIDEO_LAUNCHED_BY_APP_KEY");
390                 return MP_VIDEO_PLAYER_SIMPLE;
391         }
392
393         if(!szLaunchingByOtherApp)
394         {
395                 VideoLogInfo("No exist pointer of szLaunchingByOtherApp");
396                 return MP_VIDEO_PLAYER_SIMPLE;
397         }
398
399         MpPlayerViewType nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
400
401         if(!strcmp(szLaunchingByOtherApp, "gallery"))
402         {
403                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
404
405                 nGalleryLatestTime = MpVideoServiceCtrlCheckStartPositionTime(pVideoServiceHandle, MP_VIDEO_START_POS_TIME_KEY);
406         }
407         else if(!strcmp(szLaunchingByOtherApp, "image_viewer"))
408         {
409                         nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
410                 }
411         else if(!strcmp(szLaunchingByOtherApp, "email"))
412         {
413                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_EMAIL;
414         }
415         else if(!strcmp(szLaunchingByOtherApp, "message"))
416         {
417                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_MMS;
418         }
419         else if(!strcmp(szLaunchingByOtherApp, "light_play_view"))
420         {
421                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
422         }
423         else
424         {
425                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
426         }
427
428         VideoLogInfo("Start position time : %d", nTmpLaunchingAppType);
429
430         free(szLaunchingByOtherApp);
431
432         return (int)nTmpLaunchingAppType;
433 }
434
435 bool MpVideoServiceCtrlCheckShortcut(service_h pVideoServiceHandle)
436 {
437         if(!pVideoServiceHandle)
438         {
439                 VideoLogInfo("No exist Service handle.");
440                 return FALSE;
441         }
442
443         VideoLogInfo("");
444
445         char *pGetOperation = NULL;
446
447         service_get_operation(pVideoServiceHandle, &pGetOperation);
448
449         if(pGetOperation == NULL)
450         {
451                 bIsShortcutType = FALSE;
452                 return bIsShortcutType;
453         }
454
455         if(!strcmp(pGetOperation, SERVICE_OPERATION_DEFAULT))
456         {
457                 char *szOperationType = NULL;
458
459                 if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_LAUNCH_TYPE, &szOperationType) != SERVICE_ERROR_NONE)
460                 {
461                         VideoLogInfo("No exist Service handle key of MP_VIDEO_LAUNCHED_BY_APP_KEY");
462                         bIsShortcutType = FALSE;
463                 }
464
465                 if(!szOperationType)
466                 {
467                         VideoLogInfo("No exist pointer of szOperationType");
468                         bIsShortcutType = FALSE;
469                 }
470                 else
471                 {
472                         if(!strcmp(szOperationType, MP_VIDEO_SHORTCUT_VIDEO))
473                         {
474                                 bIsShortcutType = TRUE;
475                         }
476                         else
477                         {
478                                 bIsShortcutType = FALSE;
479                         }
480
481                         if(szOperationType)
482                         {
483                                 free(szOperationType);
484                                 szOperationType = NULL;
485                         }
486                 }
487         }
488
489         return bIsShortcutType;
490 }
491
492
493
494 /////////////////////////////////////////////////////////////////////////////
495 // External Function
496
497 void MpVideoServiceCtrlReset(void)
498 {
499         VideoLogInfo("");
500
501         pAppSvcHandle = NULL;
502
503         nGalleryLatestTime = 0;
504
505         if(g_szStreamingCookie)
506         {
507                 free(g_szStreamingCookie);
508                 g_szStreamingCookie = NULL;
509         }
510 }
511
512 int MpVideoServiceCtrlInitServiceParser(void* pAppServiceHandle, char** szMediaUri)
513 {
514         if(!pAppServiceHandle)
515         {
516                 VideoLogInfo("");
517                 return MP_ERROR_PLAYER_TYPE;
518         }
519
520         VideoLogInfo("");
521
522         MpVideoServiceCtrlReset();
523
524         pAppSvcHandle = (service_h)pAppServiceHandle;
525         nPlayerType = MP_NONE_VIEW_TYPE;
526
527         if(!MpVideoServiceCtrlParseUri(pAppSvcHandle, szMediaUri))
528         {
529                 VideoLogInfo("MP_VIDEO_PLAYER_LIST");
530                 nPlayerType = MP_VIDEO_PLAYER;
531         }
532
533         if(!*szMediaUri)
534                 {
535                         return nPlayerType;
536                 }
537         else
538         {
539                 VideoLogInfo("szMediaUri : %s", *szMediaUri);
540         }
541
542         int nUriType = 0;
543
544         nUriType = MpVideoServiceCtrlCheckUriType(*szMediaUri);
545
546         if(nUriType == MP_VIDEO_PLAYER)
547                 {
548                         nPlayerType = MpVideoServiceCtrlCheckLauncher(pAppSvcHandle);
549                 }
550
551         if(nUriType == MP_STREAMING_PLAYER)
552                 {
553                         nPlayerType = MP_STREAMING_PLAYER;
554                 MpVideoServiceCtrlCheckCookieForStreaming(pAppSvcHandle);
555                 }
556
557         return nPlayerType;
558                 }
559
560 int MpVideoServiceCtrlGetLatestTime(void)
561 {
562         VideoLogInfo("");
563
564         if(nGalleryLatestTime < 0)
565         {
566                 nGalleryLatestTime = 0;
567         }
568
569         return nGalleryLatestTime;
570 }
571
572 int MpVideoServiceCtrlGetPlayerTypeWithoutLog(void)
573 {
574         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
575         {
576                 nPlayerType = MP_ERROR_PLAYER_TYPE;
577         }
578
579         return nPlayerType;
580 }
581
582 int MpVideoServiceCtrlGetPlayerType(void)
583 {
584         VideoLogInfo("");
585
586         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
587         {
588                 nPlayerType = MP_ERROR_PLAYER_TYPE;
589         }
590
591         VideoLogInfo("nPlayerType : %d", nPlayerType);
592
593         return nPlayerType;
594 }
595
596 void MpVideoServiceCtrlSetPlayerType(int nSetPlayerType)
597 {
598         VideoLogInfo("nSetPlayerType : %d", nSetPlayerType);
599
600         if(MP_ERROR_PLAYER_TYPE < nSetPlayerType || MP_MAX_PLAYER_TYPE > nSetPlayerType)
601         {
602                 MpVideoServiceCtrlSetPrevPlayerType(nPlayerType);
603                 nPlayerType = nSetPlayerType;
604         }
605 }
606
607 int MpVideoServiceCtrlGetPrevPlayerType(void)
608 {
609         VideoLogInfo("");
610
611         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
612         {
613                 nPrevPlayerType = MP_ERROR_PLAYER_TYPE;
614         }
615
616         VideoLogInfo("nPrevPlayerType : %d", nPrevPlayerType);
617
618         return nPrevPlayerType;
619 }
620
621 void MpVideoServiceCtrlSetPrevPlayerType(int nSetPlayerType)
622 {
623         VideoLogInfo("nSetPlayerType : %d", nSetPlayerType);
624
625         if(MP_ERROR_PLAYER_TYPE < nSetPlayerType || MP_MAX_PLAYER_TYPE > nSetPlayerType)
626         {
627                 nPrevPlayerType = nSetPlayerType;
628         }
629 }
630
631 char* MpVideoServiceCtrlGetCookieForStreaming(void)
632 {
633         VideoLogInfo("");
634
635         if(!g_szStreamingCookie)
636         {
637                 VideoLogInfo("No exist streaming cookie.");
638                 return NULL;
639         }
640
641         return g_szStreamingCookie;
642 }