initial draft of the org.tizen.video-player
[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
38
39 static service_h pAppSvcHandle = NULL;
40 static int nPlayerType = MP_VIDEO_PLAYER_SIMPLE;
41 static char szStreamingCookie[STR_LEN_MAX] = {0};
42
43
44 /////////////////////////////////////////////////////////////////////////////
45 // Internal Function
46
47 bool MpVideoServiceCtrlCheckMime(service_h pVideoServiceHandle, char *szMediaUri)
48 {
49         VideoLogInfo("");
50
51         if(!pVideoServiceHandle)
52         {
53                 VideoLogInfo("[ERR] No Exist Service handle.");
54                 return FALSE;
55         }
56
57         if(!szMediaUri)
58         {
59                 VideoLogInfo("[ERR] No Exist media uri.");
60                 return FALSE;
61         }
62
63         bool bRet = TRUE;
64         char *szMimeContent = NULL;
65
66         if(service_get_extra_data(pVideoServiceHandle, AUL_K_MIME_CONTENT, &szMimeContent) != SERVICE_ERROR_NONE)
67         {
68                 VideoLogInfo("No exist mime type.");
69                 bRet = FALSE;
70         }
71
72         if(szMimeContent)
73         {
74                 strncpy(szMediaUri, szMimeContent, STR_LEN_MAX - 1);
75                 VideoLogInfo("Mime content : %s", szMediaUri);
76                 free(szMimeContent);
77                 bRet = TRUE;
78         }
79         else
80         {
81                 VideoLogInfo("No exist MIME type.");
82                 bRet = FALSE;
83         }
84
85         return bRet;
86 }
87
88 bool MpVideoServiceCtrlCheckBundle(service_h pVideoServiceHandle, char *szMediaUri)
89 {
90         if(!pVideoServiceHandle)
91         {
92                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
93                 return FALSE;
94         }
95
96         bool bRet = TRUE;
97         char *pStrVideoURI = NULL;
98
99         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_URI_PATH, &pStrVideoURI) != SERVICE_ERROR_NONE)
100         {
101                 VideoLogInfo("[ERR] No exist KEY of video/streaming URI.");
102                 bRet = FALSE;
103         }
104
105         if(pStrVideoURI)
106         {
107                 strncpy(szMediaUri, pStrVideoURI, STR_LEN_MAX - 1);
108                 VideoLogInfo("Video/Streaming URI path : %s", szMediaUri);
109                 free(pStrVideoURI);
110                 bRet = TRUE;
111         }
112         else
113         {
114                 VideoLogInfo("[ERR] No exist video/streaming URI.");
115                 bRet = FALSE;
116         }
117
118         return bRet;
119 }
120
121 bool MpVideoServiceCtrlCheckAUL(service_h pVideoServiceHandle, char *szMediaUri)
122 {
123         if(!pVideoServiceHandle)
124         {
125                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
126                 return FALSE;
127         }
128
129         char *pStrVideoURI = (char*)malloc(sizeof(char) * STR_LEN_MAX);
130         char *pGetOperation = (char*)malloc(sizeof(char) * SVC_LEN_MAX);
131
132         service_get_operation(pVideoServiceHandle, &pGetOperation);
133         if(!strcmp(SERVICE_OPERATION_VIEW , pGetOperation))
134         {
135                 service_get_uri(pVideoServiceHandle, &pStrVideoURI);
136                 if(strlen(pStrVideoURI) > 1)
137                 {
138                         strncpy(szMediaUri, pStrVideoURI, STR_LEN_MAX - 1);
139                 }
140                 else
141                 {
142                         VideoLogInfo("[ERR] No exist video/streaming URI.");
143                         return FALSE;
144                 }
145         }
146         else
147         {
148                 VideoLogInfo("[ERR] No exist Operation.");
149                 return FALSE;
150         }
151
152         VideoLogInfo("Video/Streaming URI path pStrVideoURI : %s", pStrVideoURI);
153         VideoLogInfo("Video/Streaming URI path pAppData->szMediaUri : %s", szMediaUri);
154
155         free(pStrVideoURI);
156         free(pGetOperation);
157
158         return TRUE;
159 }
160
161 bool MpVideoServiceCtrlIsXMLUrl(char *szMediaUri)
162 {
163         VideoLogInfo("URI : %s", szMediaUri);
164
165         if (!g_str_has_suffix(szMediaUri, ".xml")) {
166                 VideoLogInfo("No have xml uri.");
167                 return FALSE;
168         }
169
170         return TRUE;
171 }
172
173 bool MpVideoServiceCtrlGetUri(service_h pVideoServiceHandle, char* szMediaUri)
174 {
175         VideoLogInfo("");
176
177         if(!MpVideoServiceCtrlCheckMime(pVideoServiceHandle, szMediaUri))
178         {
179                 if(!MpVideoServiceCtrlCheckBundle(pVideoServiceHandle, szMediaUri))
180                 {
181                         if(!MpVideoServiceCtrlCheckAUL(pVideoServiceHandle, szMediaUri))
182                         {
183                                 VideoLogInfo("No exist URI path.");
184                                 return FALSE;
185                         }
186                 }
187         }
188
189         VideoLogInfo("szMediaUri", szMediaUri);
190
191         return TRUE;
192 }
193
194 int MpVideoServiceCtrlVideoUriType(char* szMediaUri)
195 {
196         if(szMediaUri == NULL)
197         {
198                 VideoLogInfo("[ERR]");
199                 return MP_ERROR_PLAYER_TYPE;
200         }
201
202         VideoLogInfo("");
203
204         int nRet = 0;
205
206         if(strlen(szMediaUri) > 0)
207         {
208                 if(MpUtilCheckPlayerType(szMediaUri))
209                 {
210                         VideoLogInfo("MP_STREAMING_PLAYER");
211                         nRet = MP_STREAMING_PLAYER;
212                 }
213                 else
214                 {
215                         VideoLogInfo("MP_VIDEO_PLAYER");
216                         nRet = MP_VIDEO_PLAYER;
217                 }
218         }
219         else
220         {
221                 VideoLogInfo("[ERR] No exist video/streaming URI. : MP_ERROR_PLAYER_TYPE");
222                 return MP_ERROR_PLAYER_TYPE;
223         }
224
225         VideoLogInfo("pAppData->szMediaUri : %s", szMediaUri);
226
227         return nRet;
228 }
229
230 bool MpVideoServiceCtrlCheckCookieForStreaming(service_h pVideoServiceHandle, char* szCookie)
231 {
232         if(!pVideoServiceHandle)
233         {
234                 VideoLogInfo("[ERR] No exist pBundleData pointer or Service handle count value.");
235                 return FALSE;
236         }
237
238         if(!szCookie)
239         {
240                 VideoLogInfo("[ERR] No allocate szCookie.");
241                 return FALSE;
242         }
243
244         bool bRet = TRUE;
245         char *pStrVideoCookie = NULL;
246
247         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_COOKIE, &pStrVideoCookie) != SERVICE_ERROR_NONE)
248         {
249                 VideoLogInfo("[ERR] No exist KEY of streaming COOKIE.");
250                 bRet = FALSE;
251         }
252
253         if(pStrVideoCookie)
254         {
255                 strncpy(szCookie, pStrVideoCookie, STR_LEN_MAX - 1);
256                 VideoLogInfo("Video/Streaming URI path Cookie : %s", szCookie);
257                 free(pStrVideoCookie);
258                 bRet = TRUE;
259         }
260         else
261         {
262                 VideoLogInfo("[ERR] No exist video/streaming URI.");
263                 bRet = FALSE;
264         }
265
266         return TRUE;
267 }
268
269 bool MpVideoServiceCtrlGetTagNameOfGallery(service_h pVideoServiceHandle, char *szBundleKey, char *szTagName)
270 {
271         VideoLogInfo("");
272
273         if(!pVideoServiceHandle)
274         {
275                 VideoLogInfo("No exist Service handle.");
276                 return FALSE;
277         }
278
279         if(!szBundleKey)
280         {
281                 VideoLogInfo("No exist Service handle kay.");
282                 return FALSE;
283         }
284
285         if(!szTagName)
286         {
287                 VideoLogInfo("No exist pAppData.");
288                 return FALSE;
289         }
290
291         bool nRet = TRUE;
292         char *szVideoTagName = NULL;            //char *szVideoTagName = (char*)malloc(sizeof(char) * SVC_LEN_MAX);
293
294         if(service_get_extra_data(pVideoServiceHandle, szBundleKey, &szVideoTagName) != SERVICE_ERROR_NONE)
295         {
296                 VideoLogInfo("No exist mime type.");
297                 nRet = FALSE;
298         }
299
300         if(szVideoTagName)
301         {
302                 strncpy(szTagName, szVideoTagName, STR_LEN_MAX - 1);
303                 VideoLogInfo("szTagName : %s", szTagName);
304                 free(szVideoTagName);
305                 nRet = TRUE;
306         }
307         else
308         {
309                 VideoLogInfo("No exist pointer of szVideoTagName.");
310                 nRet = FALSE;
311         }
312
313         return nRet;
314 }
315
316 int MpVideoServiceCtrlCheckLauncher(service_h pVideoServiceHandle)
317 {
318         VideoLogInfo("");
319
320         if(!pVideoServiceHandle)
321         {
322                 VideoLogInfo("No exist Service handle data.");
323                 return MP_VIDEO_PLAYER_SIMPLE;
324         }
325
326         char *szLaunchingByOtherApp = NULL;             //char *szLaunchingByOtherApp = (char*)malloc(sizeof(char) * SVC_LEN_MAX);
327
328         if(service_get_extra_data(pVideoServiceHandle, MP_VIDEO_LAUNCHED_BY_APP_KEY, &szLaunchingByOtherApp) != SERVICE_ERROR_NONE)
329         {
330                 VideoLogInfo("No exist Service handle key of MP_VIDEO_LAUNCHED_BY_APP_KEY");
331                 return MP_VIDEO_PLAYER_SIMPLE;
332         }
333
334         if(!szLaunchingByOtherApp)
335         {
336                 VideoLogInfo("No exist pointer of szLaunchingByOtherApp");
337                 return MP_VIDEO_PLAYER_SIMPLE;
338         }
339
340         MpPlayerViewType nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
341
342         if(!strcmp(szLaunchingByOtherApp, "gallery"))
343         {
344                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
345         }
346         else if(!strcmp(szLaunchingByOtherApp, "image_viewer"))
347         {
348                         nTmpLaunchingAppType = MP_VIDEO_PLAYER_GALLERY;
349                 }
350         else if(!strcmp(szLaunchingByOtherApp, "email"))
351         {
352                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_EMAIL;
353         }
354         else if(!strcmp(szLaunchingByOtherApp, "message"))
355         {
356                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_MMS;
357         }
358         else if(!strcmp(szLaunchingByOtherApp, "light_play_view"))
359         {
360                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
361         }
362         else
363         {
364                 nTmpLaunchingAppType = MP_VIDEO_PLAYER_SIMPLE;
365         }
366
367         VideoLogInfo("Start position time : %d", nTmpLaunchingAppType);
368
369         free(szLaunchingByOtherApp);
370
371         return (int)nTmpLaunchingAppType;
372 }
373
374
375
376 /////////////////////////////////////////////////////////////////////////////
377 // External Function
378
379 void MpVideoServiceCtrlReset(void)
380 {
381         VideoLogInfo("");
382
383         pAppSvcHandle = NULL;
384
385         memset(szStreamingCookie, 0, STR_LEN_MAX);
386 }
387
388 int MpVideoServiceCtrlInitServiceParser(void* pAppServiceHandle, char* szMediaUri)
389 {
390         if(!pAppServiceHandle)
391         {
392                 VideoLogInfo("");
393                 return MP_ERROR_PLAYER_TYPE;
394         }
395
396         VideoLogInfo("");
397
398         MpVideoServiceCtrlReset();
399
400         pAppSvcHandle = (service_h)pAppServiceHandle;
401
402         if(!MpVideoServiceCtrlGetUri(pAppSvcHandle, szMediaUri))
403         {
404                 VideoLogInfo("MP_ERROR_PLAYER_TYPE");
405                 nPlayerType = MP_ERROR_PLAYER_TYPE;
406                 return nPlayerType;
407         }
408
409         if(MpUtilIsXMLUrl(szMediaUri))
410         {
411                 if(!MpUtilParsingXML(szMediaUri))
412                 {
413                         VideoLogInfo("[ERR] Fail to get streaming URI path from XML.");
414                         nPlayerType = MP_ERROR_PLAYER_TYPE;
415                         return nPlayerType;
416                 }
417
418                 VideoLogInfo("MP_STREAMING_PLAYER");
419                 nPlayerType = MP_STREAMING_PLAYER;
420                 return nPlayerType;
421         }
422
423         if(strlen(szMediaUri) > 0)
424         {
425                 int nResultPlayerType = 0;
426
427                 nResultPlayerType = MpVideoServiceCtrlVideoUriType(szMediaUri);
428
429                 if(nResultPlayerType == MP_ERROR_PLAYER_TYPE)
430                 {
431                         VideoLogInfo("MP_ERROR_PLAYER_TYPE");
432                         nPlayerType = MP_ERROR_PLAYER_TYPE;
433                 }
434                 else if(nResultPlayerType == MP_VIDEO_PLAYER)
435                 {
436                         nPlayerType = MpVideoServiceCtrlCheckLauncher(pAppSvcHandle);
437                 }
438                 else if(nResultPlayerType == MP_STREAMING_PLAYER)
439                 {
440                         nPlayerType = MP_STREAMING_PLAYER;
441                         MpVideoServiceCtrlCheckCookieForStreaming(pAppSvcHandle, szStreamingCookie);
442                 }
443                 else
444                 {
445                 }
446         }
447
448         return nPlayerType;
449 }
450
451 int MpVideoServiceCtrlGetPlayerTypeWithoutLog(void)
452 {
453         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
454         {
455                 nPlayerType = MP_ERROR_PLAYER_TYPE;
456         }
457
458         return nPlayerType;
459 }
460
461 int MpVideoServiceCtrlGetPlayerType(void)
462 {
463         VideoLogInfo("");
464
465         if(MP_ERROR_PLAYER_TYPE > nPlayerType || MP_MAX_PLAYER_TYPE < nPlayerType)
466         {
467                 nPlayerType = MP_ERROR_PLAYER_TYPE;
468         }
469
470         VideoLogInfo("nPlayerType : %d", nPlayerType);
471
472         return nPlayerType;
473 }
474
475 void MpVideoServiceCtrlSetPlayerType(int nSetPlayerType)
476 {
477         VideoLogInfo("nSetPlayerType : %d", nSetPlayerType);
478
479         if(MP_ERROR_PLAYER_TYPE < nSetPlayerType || MP_MAX_PLAYER_TYPE > nSetPlayerType)
480         {
481                 nPlayerType = nSetPlayerType;
482         }
483 }
484
485 char* MpVideoServiceCtrlGetCookieForStreaming(void)
486 {
487         VideoLogInfo("");
488
489         if(strlen(szStreamingCookie) < 1)
490         {
491                 VideoLogInfo("No exist streaming cookie.");
492         }
493
494         return szStreamingCookie;
495 }