initial draft of the org.tizen.video-player
[apps/core/preloaded/video-player.git] / src / mp-video-info-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 #include <aul.h>
20 #include <vconf.h>
21 #include <libexif/exif-data.h>
22 #include <metadata_extractor.h>
23
24 #include <unicode/udat.h>
25 #include <unicode/ustring.h>
26 #include <unicode/uloc.h>
27 #include <unicode/ucal.h>
28 #include <unicode/udatpg.h>
29 #include <unicode/utmscale.h>
30
31 #include "mp-util.h"
32 #include "mp-drm-ctrl.h"
33 #include "mp-video-log.h"
34 #include "video-player.h"
35 #include "mp-video-info-ctrl.h"
36
37 enum VIDEO_PLAYER_FILE_SIZE_TYPE
38 {
39         SIZE_BYTE = 0,
40         SIZE_KB,
41         SIZE_MB,
42         SIZE_GB
43 };
44
45 bool MpVideoInfoCtrlGetGPS(char *szUriPath, double *nLongtitude, double *nLatitude)
46 {
47         if (!szUriPath) {
48                 VideoLogInfo("[ERR] No exist szUriPath.");
49                 return FALSE;
50         }
51
52         VideoLogInfo("");
53
54         if (MpVideoDrmIsDrmFile(szUriPath)) {
55                 VideoLogInfo("It's drm file.");
56                 return FALSE;
57         }
58
59         metadata_extractor_h pMetadata;
60
61         char *szTmp = NULL;
62
63         *nLatitude = 0.0;
64         *nLongtitude = 0.0;
65
66         if(metadata_extractor_create(&pMetadata) != METADATA_EXTRACTOR_ERROR_NONE)
67         {
68                 VideoLogInfo("[ERR] - metadata_extractor_create()");
69                 return FALSE;
70         }
71
72         if(metadata_extractor_set_path(pMetadata, szUriPath) != METADATA_EXTRACTOR_ERROR_NONE) 
73         {
74                 VideoLogInfo("[ERR] - metadata_extractor_set_path()");
75                 return FALSE;
76         }
77
78         if(metadata_extractor_get_metadata(pMetadata, METADATA_LATITUDE, &szTmp) != METADATA_EXTRACTOR_ERROR_NONE)
79         {
80                 VideoLogInfo("[ERR] - metadata_extractor_get_metadata() - METADATA_LATITUDE");
81                 metadata_extractor_destroy(pMetadata);
82                 return FALSE;
83         }
84         else
85         {
86                 if(szTmp)
87                 {
88                         VideoLogInfo("METADATA_LATITUDE : %s", szTmp);
89                         *nLatitude = atof(szTmp);
90                 }
91                 else
92                 {
93                         *nLatitude = 0.0;
94                 }
95
96                 free(szTmp);
97                 szTmp = NULL;
98         }
99
100         if(metadata_extractor_get_metadata(pMetadata, METADATA_LONGITUDE, &szTmp) != METADATA_EXTRACTOR_ERROR_NONE)
101         {
102                 VideoLogInfo("[ERR] - metadata_extractor_get_metadata() - METADATA_LONGITUDE");
103                 metadata_extractor_destroy(pMetadata);
104                 return FALSE;
105         }
106         else
107         {
108                 if(szTmp)
109                 {
110                         VideoLogInfo("METADATA_LONGITUDE : %s", szTmp);
111                         *nLongtitude = atof(szTmp);
112                 }
113                 else
114                 {
115                         *nLongtitude = 0.0;
116                         VideoLogInfo("METADATA_LONGITUDE : %s", szTmp);
117                 }
118
119                 free(szTmp);
120                 szTmp = NULL;
121         }
122
123         metadata_extractor_destroy(pMetadata);
124
125         return TRUE;    
126 }
127
128 bool MpVideoInfoCtrlGetResolution(char *szWidthResolution, char *szHeightResolution, void *pUserData)
129 {
130         if (!pUserData) {
131                 VideoLogInfo("[ERR] No exist pUserData.");
132                 return FALSE;
133         }
134
135         VideoLogInfo("");
136
137         VideoAppData *pAppData = (VideoAppData *)pUserData;
138
139         pAppData->VideoAppCtrlCallbackFunc.VideoAppCtrlGetVideoResolution(pAppData);
140
141         if (pAppData->nVideoWidthResolution > 0 && pAppData->nVideoHeightResolution > 0) {
142                 snprintf(szWidthResolution, STR_LEN_MAX, "%d", pAppData->nVideoWidthResolution);
143                 snprintf(szHeightResolution, STR_LEN_MAX, "%d", pAppData->nVideoHeightResolution);
144         } else {
145                 return FALSE;
146         }
147
148         return TRUE;
149 }
150
151 static char *MpVideoInfoCtrlConvertingString(const char *szLocale, const char *szCustomSkeleton, const char *szTimezone, UDate st_Date)
152 {
153 #define UG_ICU_ARR_LENGTH                       256
154
155         // Copy a byte string encoded in the default codepage to a ustring.
156         // Copies at most n characters. The result will be null terminated if the length of src is less than n.
157         // Performs a host byte to UChar conversion.
158
159         UChar ucustomSkeleton[UG_ICU_ARR_LENGTH] = {0,};
160
161         if (u_uastrncpy(ucustomSkeleton, szCustomSkeleton, UG_ICU_ARR_LENGTH) == NULL) {
162                 VideoLogInfo("u_uastrncpy() error.");
163                 return NULL;
164         }
165
166         UChar utimezone[UG_ICU_ARR_LENGTH] = {0,};
167
168         if (u_uastrncpy(utimezone, szTimezone, UG_ICU_ARR_LENGTH) == NULL) {
169                 VideoLogInfo("u_uastrncpy() error.");
170                 return NULL;
171         }
172
173         UErrorCode status = U_ZERO_ERROR;
174         UDateTimePatternGenerator *generator;
175         UDateFormat     *formatter;
176
177         UChar bestPattern[UG_ICU_ARR_LENGTH] = {0,};
178         UChar formatted[UG_ICU_ARR_LENGTH] = {0,};
179         char formattedString[UG_ICU_ARR_LENGTH] = {0,};
180         int32_t bestPatternLength, formattedLength;
181
182         ucal_setDefaultTimeZone(utimezone , &status);
183
184         if (U_FAILURE(status)) {
185                 VideoLogInfo("ucal_setDefaultTimeZone() is failed.");
186                 return NULL;
187         }
188
189         uloc_setDefault(getenv("LC_TIME"), &status);
190
191         if (U_FAILURE(status)) {
192                 VideoLogInfo("ucal_setDefaultTimeZone() is failed.");
193                 return NULL;
194         }
195
196         generator = udatpg_open(szLocale, &status);
197         if (generator == NULL) {
198                 return NULL;
199         }
200
201         bestPatternLength = udatpg_getBestPattern(generator, ucustomSkeleton, u_strlen(ucustomSkeleton), bestPattern, UG_ICU_ARR_LENGTH, &status);
202         if (bestPatternLength <= 0) {
203                 return NULL;
204         }
205
206         formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, szLocale, NULL, -1, bestPattern, -1, &status);
207         if (formatter == 0) {
208                 return NULL;
209         }
210
211         formattedLength = udat_format(formatter, st_Date, formatted, UG_ICU_ARR_LENGTH, NULL, &status);
212         if (formattedLength <= 0) {
213                 return NULL;
214         }
215
216         u_austrcpy(formattedString, formatted);
217         udatpg_close(generator);
218         udat_close(formatter);
219
220         if (strlen(formattedString) == 0) {
221                 return NULL;
222         }
223
224         return strdup(formattedString);
225 }
226
227 static char* MpVideoInfoCtrlGetDateOfFile(time_t mtime)
228 {
229 #define UG_DATE_FORMAT_12                       "yMMMdhms"
230 #define UG_DATE_FORMAT_24                       "yMMMdHms"
231
232         VideoLogInfo("");
233
234         char* szSkeleton = NULL;
235
236 //jdlee
237 /*
238         enum appcore_time_format nTimeformat;
239
240         int nRet = appcore_get_timeformat(&nTimeformat);
241         if(nRet == -1)
242         {
243                 VideoLogInfo("Cannot get timeformat.");
244                 nTimeformat = APPCORE_TIME_FORMAT_12;
245         }
246
247         if(nTimeformat == APPCORE_TIME_FORMAT_12)
248         {
249                 szSkeleton = UG_DATE_FORMAT_12;
250         }
251         else if(nTimeformat == APPCORE_TIME_FORMAT_24)
252         {
253                 szSkeleton = UG_DATE_FORMAT_24;
254         }
255         else
256         {
257                 VideoLogInfo("Invalid time format : %d", nTimeformat);
258                 szSkeleton = UG_DATE_FORMAT_12;         // Default value.
259         }
260 */
261         szSkeleton = UG_DATE_FORMAT_12;
262
263         char *szLocale = vconf_get_str(VCONFKEY_REGIONFORMAT);          // eg en_US.UTF-8
264         if(szLocale == NULL)
265         {
266                 VideoLogInfo("Cannot get region format.");
267                 szLocale = "en_US.UTF-8";                       // Default value.
268         }
269
270         char *szTimezone = NULL;
271         szTimezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);       // eg Asia/Seoul
272         if(szTimezone == NULL)
273         {
274                 VideoLogInfo("Cannot get time zone.");
275                 return strdup("N/A");
276         }
277
278         VideoLogInfo("Locale : %s TimeZone : %s TimeFormat : %s", szLocale, szSkeleton, szTimezone);
279
280         char* szDatestr = NULL;
281         szDatestr = MpVideoInfoCtrlConvertingString(szLocale, szSkeleton, szTimezone, (UDate)mtime * 1000);
282         if(!szDatestr)
283         {
284                 VideoLogInfo("Cannot get time string.");
285                 return strdup("N/A");
286         }
287
288         VideoLogInfo("ICU Date : %s", szDatestr);
289
290         return szDatestr;
291 }
292
293 static char* MpVideoInfoCtrlGetDrmFileExtension(char* szPath)
294 {
295 #define EXT_SIZE                128
296 #define EXT_SPLITTER    '.'
297
298         if (!szPath) {
299                 VideoLogInfo("[ERR] No exist szPath.");
300                 return NULL;
301         }
302
303         char szDrmContentType[STR_LEN_MAX] = {0};
304
305         MpVideoDrmGetFileExtension(szPath, szDrmContentType, STR_LEN_MAX);
306         VideoLogInfo("szDrmContentType : %s", szDrmContentType);
307
308         char szTmpExt[EXT_SIZE] = {0,};
309         char *szExt = NULL;
310         int nErr = -1;
311
312         nErr = aul_get_mime_extension(szDrmContentType, szTmpExt, sizeof(szTmpExt));
313
314         if (0 == nErr) {
315                 szExt = strrchr(szTmpExt, EXT_SPLITTER);
316                 if((szExt == NULL) || (szExt + 1 == NULL)) {
317                         return NULL;
318                 }
319         } else {
320                 return NULL;
321         }
322
323         return strdup(szExt + 1);
324
325 }
326
327 char* MpVideoInfoCtrlGetFileExtension(char *szPath)
328 {
329         if (!szPath) {
330                 VideoLogInfo("Cannot get file extension. path is NULL");
331                 return strdup("Unknown");
332         }
333
334         if (MpVideoDrmIsDrmFile(szPath)) {
335                 VideoLogInfo("It's drm file.");
336
337                 char *szFileExtension = MpVideoInfoCtrlGetDrmFileExtension(szPath);
338                 if (szFileExtension) {
339                         VideoLogInfo("Drm file extension. - %s", szFileExtension);
340                         return szFileExtension;
341                 } else {
342                         VideoLogInfo("No have file extension.");
343                         return strdup("Unknown");
344                 }
345         } else {
346                 char *szExt = NULL;
347                 szExt = strrchr(szPath, '.');
348
349                 if ((szExt != NULL) && ((szExt+1) != NULL)) {
350                         return strdup(szExt + 1);
351                 }
352         }
353
354         return strdup("Unknown");
355 }
356
357 char* MpVideoInfoCtrlGetFileSize(size_t size)
358 {
359         VideoLogInfo("");
360
361 #define FILE_SIZE_LEN_MAX                       64
362 #define BASIC_SIZE                                      1024    //used for file size check
363
364         int nCount = 0;
365
366         unsigned long int lsize = (unsigned long int)size;
367
368         char *pTmp = (char *)calloc(1, sizeof(char) * FILE_SIZE_LEN_MAX + 1);
369         if (pTmp == NULL) {
370                 return NULL;
371         }
372
373         while (size >= (BASIC_SIZE)) {
374                 lsize = size;
375                 size /= BASIC_SIZE;
376                 nCount++;
377         }
378
379         if (nCount == SIZE_BYTE) {
380                 snprintf(pTmp, FILE_SIZE_LEN_MAX,"%zu B", size);
381         } else if (nCount == SIZE_KB) {
382                 snprintf(pTmp, FILE_SIZE_LEN_MAX,"%zu KB", size);
383         } else if (nCount == SIZE_MB) {
384                 snprintf(pTmp, FILE_SIZE_LEN_MAX,"%zu MB", size);
385         } else if(nCount == SIZE_GB) {
386                 snprintf(pTmp, FILE_SIZE_LEN_MAX,"%zu GB", size);
387         }
388
389         VideoLogInfo("Size = %lu, OrigSize=%zu", lsize, size);
390
391         return pTmp;
392 }
393
394 bool MpVideoInfoCtrlGetFileInfo(char *szUriPath, char *szFileDate, int nFileDateSize, char *szFileExtension, int nFileExtensionSize, char *szFileSize, int nFilesizeSize)
395 {
396         if (!szUriPath) {
397                 VideoLogInfo("[ERR] No exist szUriPath.");
398                 return FALSE;
399         }
400
401         VideoLogInfo("");
402
403         struct stat statbuf;
404
405         if (stat(szUriPath, &statbuf) == -1) {
406                 VideoLogInfo("%s file is NULL", szUriPath);
407                 return FALSE;
408         }
409
410         memset(szFileDate, 0, nFileDateSize);
411         memset(szFileExtension, 0, nFileExtensionSize);
412         memset(szFileSize, 0, nFilesizeSize);
413
414         char *szTmpDateOfFile = MpVideoInfoCtrlGetDateOfFile(statbuf.st_mtime);
415         char *szTmpFileExtension = MpVideoInfoCtrlGetFileExtension(szUriPath);
416         char *szTmpFileSize = MpVideoInfoCtrlGetFileSize(statbuf.st_size);      
417
418         if (szTmpDateOfFile) {
419                 VideoLogInfo("szTmpDateOfFile : %s", szTmpDateOfFile);
420                 strncpy(szFileDate, szTmpDateOfFile, nFileDateSize);
421                 free(szTmpDateOfFile);
422         }
423
424         if (szTmpFileExtension) {
425                 VideoLogInfo("szTmpFileExtension : %s", szTmpFileExtension);
426                 strncpy(szFileExtension, szTmpFileExtension, nFileExtensionSize);
427                 free(szTmpFileExtension);
428         }
429
430         if (szTmpFileSize) {
431                 VideoLogInfo("szTmpFileSize : %s", szTmpFileSize);
432                 strncpy(szFileSize, szTmpFileSize, nFilesizeSize);
433                 free(szTmpFileSize);
434         }
435
436         return TRUE;
437 }