Merge branch 'master' into release
[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
197 bool MpVideoServiceCtrlParseUri(service_h pVideoServiceHandle, char** szMediaUri)
198 {
199         VideoLogInfo("");
200
201         if(!MpVideoServiceCtrlCheckMime(pVideoServiceHandle, szMediaUri))
202         {
203                 if(!MpVideoServiceCtrlCheckBundle(pVideoServiceHandle, szMediaUri))
204                 {
205                         if(!MpVideoServiceCtrlCheckAUL(pVideoServiceHandle, szMediaUri))
206                         {
207                                 VideoLogInfo("No exist URI path.");
208                                 return FALSE;
209                         }
210                 }
211         }
212
213         VideoLogInfo("szMediaUri : %s", *szMediaUri);
214
215         return TRUE;
216 }
217
218 int MpVideoServiceCtrlCheckUriType(char* szMediaUri)
219 {
220         if(!szMediaUri)
221         {
222                 VideoLogInfo("[ERR] No exist media uri.");
223                 return MP_ERROR_PLAYER_TYPE;
224         }
225
226         VideoLogInfo("");
227
228         int nRet = 0;
229
230         if(MpUtilCheckUriType(szMediaUri))
231                 {
232                         VideoLogInfo("MP_STREAMING_PLAYER");
233                         nRet = MP_STREAMING_PLAYER;
234                 }
235                 else
236                 {
237                         VideoLogInfo("MP_VIDEO_PLAYER");
238                         nRet = MP_VIDEO_PLAYER;
239                 }
240
241         return nRet;
242 }
243
244 bool MpVideoServiceCtrlCheckCookieForStreaming(service_h pVideoServiceHandle)
245 {
246         if(!pVideoServiceHandle)
247         {
248                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
249                 return FALSE;
250         }
251
252         bool bRet = TRUE;
253         char *pStrVideoCookie = NULL;
254
255         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_COOKIE, &pStrVideoCookie) != SERVICE_ERROR_NONE)
256         {
257                 VideoLogInfo("[ERR] No exist KEY of streaming COOKIE.");
258                 bRet = FALSE;
259         }
260
261         if(pStrVideoCookie)
262         {
263                 g_szStreamingCookie = (char*)malloc(strlen(pStrVideoCookie) + 1);
264                 memset(g_szStreamingCookie, 0, strlen(pStrVideoCookie) + 1);
265                 strncpy(g_szStreamingCookie, pStrVideoCookie, strlen(pStrVideoCookie));
266                 VideoLogInfo("Video/Streaming URI path Cookie : %s", g_szStreamingCookie);
267                 free(pStrVideoCookie);
268         }
269         else
270         {
271                 VideoLogInfo("[ERR] No exist video/streaming URI.");
272                 bRet = FALSE;
273         }
274
275         return TRUE;
276 }
277
278 int MpVideoServiceCtrlCheckStartPositionTime(service_h pVideoServiceHandle, char *szBundleKey)
279 {
280         VideoLogInfo("");
281
282         if(!pVideoServiceHandle)
283         {
284                 VideoLogInfo("No exist Service handle data.");
285                 return 0;
286         }
287
288         int nStartPositionTime = 0;
289         char *szStartPosTime = NULL;
290
291         if(service_get_extra_data(pVideoServiceHandle, szBundleKey, &szStartPosTime) != SERVICE_ERROR_NONE)
292         {
293                 VideoLogInfo("No exist mime type.");
294                 return 0;
295         }
296
297         if(!szStartPosTime)
298         {
299                 VideoLogInfo("No exist pointer of position time.");
300                 return 0;
301         }
302
303         nStartPositionTime = atoi(szStartPosTime);
304
305         VideoLogInfo("Start position time : %d", nStartPositionTime);
306
307         free(szStartPosTime);
308
309         if(nStartPositionTime < 0)
310         {
311                 nStartPositionTime = 0;
312         }
313
314         return nStartPositionTime;
315 }
316
317 bool MpVideoServiceCtrlGetTagNameOfGallery(service_h pVideoServiceHandle, char *szBundleKey, char *szTagName)
318 {
319         VideoLogInfo("");
320
321         if(!pVideoServiceHandle)
322         {
323                 VideoLogInfo("No exist Service handle.");
324                 return FALSE;
325         }
326
327         if(!szBundleKey)
328         {
329                 VideoLogInfo("No exist Service handle kay.");
330                 return FALSE;
331         }
332
333         if(!szTagName)
334         {
335                 VideoLogInfo("No exist pAppData.");
336                 return FALSE;
337         }
338
339         bool nRet = TRUE;
340         char *szVideoTagName = NULL;
341
342         if(service_get_extra_data(pVideoServiceHandle, szBundleKey, &szVideoTagName) != SERVICE_ERROR_NONE)
343         {
344                 VideoLogInfo("No exist mime type.");
345                 nRet = FALSE;
346         }
347
348         if(szVideoTagName)
349         {
350                 strncpy(szTagName, szVideoTagName, STR_LEN_MAX - 1);
351                 VideoLogInfo("szTagName : %s", szTagName);
352                 free(szVideoTagName);
353                 nRet = TRUE;
354         }
355         else
356         {
357                 VideoLogInfo("No exist pointer of szVideoTagName.");
358                 nRet = FALSE;
359         }
360
361         return nRet;
362 }
363
364 int MpVideoServiceCtrlCheckLauncher(service_h pVideoServiceHandle)
365 {
366         VideoLogInfo("");
367
368         if(!pVideoServiceHandle)
369         {
370                 VideoLogInfo("No exist Service handle data.");
371                 return MP_VIDEO_PLAYER_SIMPLE;
372         }
373
374         char *szLaunchingByOtherApp = NULL;
375
376         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_LAUNCHED_BY_APP_KEY, &szLaunchingByOtherApp) != SERVICE_ERROR_NONE)
377         {
378                 VideoLogInfo("No exist Service handle key of MP_VIDEO_LAUNCHED_BY_APP_KEY");
379                 return MP_VIDEO_PLAYER_SIMPLE;
380         }
381
382         if(!szLaunchingByOtherApp)
383         {
384                 VideoLogInfo("No exist pointer of szLaunchingByOtherApp");
385                 return MP_VIDEO_PLAYER_SIMPLE;
386         }
387
388         MpPlayerViewType nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
389
390         if(!strcmp(szLaunchingByOtherApp, "gallery"))
391         {
392                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
393
394                 nGalleryLatestTime = MpVideoServiceCtrlCheckStartPositionTime(pVideoServiceHandle, MP_VIDEO_START_POS_TIME_KEY);
395         }
396         else if(!strcmp(szLaunchingByOtherApp, "image_viewer"))
397         {
398                         nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
399                 }
400         else if(!strcmp(szLaunchingByOtherApp, "email"))
401         {
402                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_EMAIL;
403         }
404         else if(!strcmp(szLaunchingByOtherApp, "message"))
405         {
406                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_MMS;
407         }
408         else if(!strcmp(szLaunchingByOtherApp, "light_play_view"))
409         {
410                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
411         }
412         else
413         {
414                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
415         }
416
417         VideoLogInfo("Start position time : %d", nTmpLaunchingAppType);
418
419         free(szLaunchingByOtherApp);
420
421         return (int)nTmpLaunchingAppType;
422 }
423
424 bool MpVideoServiceCtrlCheckShortcut(service_h pVideoServiceHandle)
425 {
426         if(!pVideoServiceHandle)
427         {
428                 VideoLogInfo("No exist Service handle.");
429                 return FALSE;
430         }
431
432         VideoLogInfo("");
433
434         char *pGetOperation = NULL;
435
436         service_get_operation(pVideoServiceHandle, &pGetOperation);
437
438         if(pGetOperation == NULL)
439         {
440                 bIsShortcutType = FALSE;
441                 return bIsShortcutType;
442         }
443
444         if(!strcmp(pGetOperation, SERVICE_OPERATION_DEFAULT))
445         {
446                 char *szOperationType = NULL;
447
448                 if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_LAUNCH_TYPE, &szOperationType) != SERVICE_ERROR_NONE)
449                 {
450                         VideoLogInfo("No exist Service handle key of MP_VIDEO_LAUNCHED_BY_APP_KEY");
451                         bIsShortcutType = FALSE;
452                 }
453
454                 if(!szOperationType)
455                 {
456                         VideoLogInfo("No exist pointer of szOperationType");
457                         bIsShortcutType = FALSE;
458                 }
459                 else
460                 {
461                         if(!strcmp(szOperationType, MP_VIDEO_SHORTCUT_VIDEO))
462                         {
463                                 bIsShortcutType = TRUE;
464                         }
465                         else
466                         {
467                                 bIsShortcutType = FALSE;
468                         }
469
470                         if(szOperationType)
471                         {
472                                 free(szOperationType);
473                                 szOperationType = NULL;
474                         }
475                 }
476         }
477
478         return bIsShortcutType;
479 }
480
481
482
483 /////////////////////////////////////////////////////////////////////////////
484 // External Function
485
486 void MpVideoServiceCtrlReset(void)
487 {
488         VideoLogInfo("");
489
490         pAppSvcHandle = NULL;
491
492         nGalleryLatestTime = 0;
493
494         if(g_szStreamingCookie)
495         {
496                 free(g_szStreamingCookie);
497                 g_szStreamingCookie = NULL;
498         }
499 }
500
501 int MpVideoServiceCtrlInitServiceParser(void* pAppServiceHandle, char** szMediaUri)
502 {
503         if(!pAppServiceHandle)
504         {
505                 VideoLogInfo("");
506                 return MP_ERROR_PLAYER_TYPE;
507         }
508
509         VideoLogInfo("");
510
511         MpVideoServiceCtrlReset();
512
513         pAppSvcHandle = (service_h)pAppServiceHandle;
514         nPlayerType = MP_NONE_VIEW_TYPE;
515
516         if(!MpVideoServiceCtrlParseUri(pAppSvcHandle, szMediaUri))
517         {
518                 VideoLogInfo("MP_VIDEO_PLAYER_LIST");
519                 nPlayerType = MP_VIDEO_PLAYER;
520         }
521
522         if(!*szMediaUri)
523                 {
524                         return nPlayerType;
525                 }
526         else
527         {
528                 VideoLogInfo("szMediaUri : %s", *szMediaUri);
529         }
530
531         int nUriType = 0;
532
533         nUriType = MpVideoServiceCtrlCheckUriType(*szMediaUri);
534
535         if(nUriType == MP_VIDEO_PLAYER)
536                 {
537                         nPlayerType = MpVideoServiceCtrlCheckLauncher(pAppSvcHandle);
538                 }
539
540         if(nUriType == MP_STREAMING_PLAYER)
541                 {
542                         nPlayerType = MP_STREAMING_PLAYER;
543                 MpVideoServiceCtrlCheckCookieForStreaming(pAppSvcHandle);
544                 }
545
546         return nPlayerType;
547                 }
548
549 int MpVideoServiceCtrlGetLatestTime(void)
550 {
551         VideoLogInfo("");
552
553         if(nGalleryLatestTime < 0)
554         {
555                 nGalleryLatestTime = 0;
556         }
557
558         return nGalleryLatestTime;
559 }
560
561 int MpVideoServiceCtrlGetPlayerTypeWithoutLog(void)
562 {
563         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
564         {
565                 nPlayerType = MP_ERROR_PLAYER_TYPE;
566         }
567
568         return nPlayerType;
569 }
570
571 int MpVideoServiceCtrlGetPlayerType(void)
572 {
573         VideoLogInfo("");
574
575         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
576         {
577                 nPlayerType = MP_ERROR_PLAYER_TYPE;
578         }
579
580         VideoLogInfo("nPlayerType : %d", nPlayerType);
581
582         return nPlayerType;
583 }
584
585 void MpVideoServiceCtrlSetPlayerType(int nSetPlayerType)
586 {
587         VideoLogInfo("nSetPlayerType : %d", nSetPlayerType);
588
589         if(MP_ERROR_PLAYER_TYPE < nSetPlayerType || MP_MAX_PLAYER_TYPE > nSetPlayerType)
590         {
591                 MpVideoServiceCtrlSetPrevPlayerType(nPlayerType);
592                 nPlayerType = nSetPlayerType;
593         }
594 }
595
596 int MpVideoServiceCtrlGetPrevPlayerType(void)
597 {
598         VideoLogInfo("");
599
600         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
601         {
602                 nPrevPlayerType = MP_ERROR_PLAYER_TYPE;
603         }
604
605         VideoLogInfo("nPrevPlayerType : %d", nPrevPlayerType);
606
607         return nPrevPlayerType;
608 }
609
610 void MpVideoServiceCtrlSetPrevPlayerType(int nSetPlayerType)
611 {
612         VideoLogInfo("nSetPlayerType : %d", nSetPlayerType);
613
614         if(MP_ERROR_PLAYER_TYPE < nSetPlayerType || MP_MAX_PLAYER_TYPE > nSetPlayerType)
615         {
616                 nPrevPlayerType = nSetPlayerType;
617         }
618 }
619
620 char* MpVideoServiceCtrlGetCookieForStreaming(void)
621 {
622         VideoLogInfo("");
623
624         if(!g_szStreamingCookie)
625         {
626                 VideoLogInfo("No exist streaming cookie.");
627                 return NULL;
628         }
629
630         return g_szStreamingCookie;
631 }