Tizen 2.1 base
[platform/framework/native/content.git] / src / FCntVideoContentInfo.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FCntVideoContentInfo.cpp
19  * @brief               This is the implementation file for the %VideoContentInfo class.
20  *
21  * This file contains implementation of the %VideoContentInfo class.
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FCntVideoContentInfo.h>
26 #include <FCntContentManagerUtil.h>
27 #include <FCntVideoMetadata.h>
28 #include <FIoFile.h>
29 #include <FIoDirectory.h>
30 #include <FSysEnvironment.h>
31 //#include <FLocLocationProvider.h>
32 //#include <FLocQualifiedCoordinates.h>
33 #include <FIo_FileImpl.h>
34 #include <FApp_AppInfo.h>
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Io;
38 using namespace Tizen::Locations;
39 using namespace Tizen::App;
40 using namespace Tizen::System;
41 using namespace std;
42
43 namespace Tizen { namespace Content
44 {
45
46 VideoContentInfo::VideoContentInfo(void)
47         : ContentInfo()
48         , __pVideoContentData(null)
49         , __pImpl(null)
50 {
51
52 }
53
54 VideoContentInfo::~VideoContentInfo(void)
55 {
56         if (__pVideoContentData != null)
57         {
58                 if (__pVideoContentData->pArtist != null)
59                 {
60                         delete __pVideoContentData->pArtist;
61                         __pVideoContentData->pArtist = null;
62                 }
63                 if (__pVideoContentData->pGenre != null)
64                 {
65                         delete __pVideoContentData->pGenre;
66                         __pVideoContentData->pGenre = null;
67                 }
68                 if (__pVideoContentData->pTitle != null)
69                 {
70                         delete __pVideoContentData->pTitle;
71                         __pVideoContentData->pTitle = null;
72                 }
73                 if (__pVideoContentData->pAlbumName != null)
74                 {
75                         delete __pVideoContentData->pAlbumName;
76                         __pVideoContentData->pAlbumName = null;
77                 }
78                 delete __pVideoContentData;
79                 __pVideoContentData = null;
80         }
81 }
82
83 /**
84  * E_SUCCESS
85  * E_FILE_NOT_FOUND
86  * E_INVALID_ARG
87  * E_OUT_OF_MEMORY
88  * - E_IO
89  */
90 result
91 VideoContentInfo::Construct(const String& contentPath, const String& thumbnailPath, bool setGps)
92 {
93         SysAssertf(__pVideoContentData == null,
94                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
95
96         ClearLastResult();
97
98         result r = E_SUCCESS;
99         _ContentData contentData;
100         int contentLength = 0;
101         FileAttributes attribute;
102
103         // checks parameters
104         contentLength = contentPath.GetLength();
105         SysTryReturnResult(NID_CNT, _FileImpl::IsMediaPath(contentPath), E_INVALID_ARG,
106                         "The contentPath should start with /Media or /Storagecard/Media(%ls).", contentPath.GetPointer());
107         SysTryReturnResult(NID_CNT, File::IsFileExist(contentPath), E_FILE_NOT_FOUND,
108                         "The file corresponding to contentPath could not be found.");
109
110         if (!thumbnailPath.IsEmpty())
111         {
112                 SysLog(NID_CNT,
113                                 "The thumbnailPath is not supported but you can get the thumbnail managed by Tizen from ContentInfo::GetThumbnailN().");
114         }
115
116         if (setGps)
117         {
118                 SysLog(NID_CNT, "The setGps is not supported.");
119         }
120
121         // Sets the content path
122         contentData.contentPath = contentPath;
123
124         // Sets the content type
125         contentData.contentType = CONTENT_TYPE_VIDEO;
126
127         // E_INVALID_ARG, E_OUT_OF_MEMORY
128         r = SetContentData(&contentData);
129         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
130
131         _VideoContentData* pVideoContentData = new (nothrow) _VideoContentData();
132         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
133
134         __pVideoContentData = pVideoContentData;
135
136         return r;
137 }
138
139 /**
140  * E_SUCCESS
141  * E_FILE_NOT_FOUND
142  * E_INVALID_ARG
143  * E_OUT_OF_MEMORY
144  * - E_IO
145  * - E_SYSTEM
146  */
147 result
148 VideoContentInfo::Construct(const String* pContentPath)
149 {
150         SysAssertf(__pVideoContentData == null,
151                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
152
153         ClearLastResult();
154
155         result r = E_SUCCESS;
156         _ContentData contentData;
157
158         if (pContentPath != null)
159         {
160                 if (!_AppInfo::IsOspCompat())
161                 {
162                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0) || pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
163                         {
164                                 SysLogException(NID_CNT, E_INVALID_ARG,
165                                                 "[E_INVALID_ARG] /Media/ and /Storagecard/Media/ are not supported from Tizen 2.0.");
166                                 return E_INVALID_ARG;
167                         }
168                         if (!(pContentPath->StartsWith(Environment::GetMediaPath(), 0)
169                                 || pContentPath->StartsWith(Environment::GetExternalStoragePath(), 0)))
170                         {
171                                 SysLogException(NID_CNT, E_INVALID_ARG,
172                                                 "[E_INVALID_ARG] %ls is not supported.", pContentPath->GetPointer());
173                                 return E_INVALID_ARG;
174                         }
175                 }
176                 else
177                 {
178                         // prior to 2.0
179                         if (pContentPath->StartsWith(OSP_MEDIA_PHONE, 0))
180                         {
181                                 // Because the content path is saved like /opt/media or /opt/storage/sdcard/ in SLP database,
182                                 // it should be converted in 2.0.
183                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_PHONE, Environment::GetMediaPath());
184                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
185                         }
186                         else if (pContentPath->StartsWith(OSP_MEDIA_MMC, 0))
187                         {
188                                 r = (const_cast<String*>(pContentPath))->Replace(OSP_MEDIA_MMC, Environment::GetExternalStoragePath());
189                                 SysTryReturnResult(NID_CNT, !IsFailed(r), E_INVALID_ARG, "Construct() failed.");
190                         }
191                         else
192                         {
193                                 SysLogException(NID_CNT, E_INVALID_ARG,
194                                                 "[E_INVALID_ARG] The contentPath should start with /Media or /Storagecard/Media.");
195                                 return E_INVALID_ARG;
196                         }
197                 }
198
199                 int length = pContentPath->GetLength();
200                 SysTryReturnResult(NID_CNT, length != 0, E_INVALID_ARG,
201                                 "The length of pContentPath is 0.");
202                 SysTryReturnResult(NID_CNT, File::IsFileExist(*pContentPath), E_FILE_NOT_FOUND,
203                                   "The file corresponding to pContentPath could not be found.");
204
205                 // Sets the content path
206                 contentData.contentPath = *pContentPath;
207
208                 // Sets the content type
209                 contentData.contentType = CONTENT_TYPE_VIDEO;
210
211                 r = SetContentData(&contentData);
212                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
213         }
214         else
215         {
216                 contentData.contentType = CONTENT_TYPE_VIDEO;
217
218                 r = SetContentData(&contentData);
219                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Construct() failed.");
220         }
221
222
223         _VideoContentData* pVideoContentData = new (nothrow) _VideoContentData();
224         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "Construct() failed.");
225
226         __pVideoContentData = pVideoContentData;
227
228         return r;
229 }
230
231 result
232 VideoContentInfo::SetVideoContentData(const _VideoContentData* pVideoContentData)
233 {
234         ClearLastResult();
235
236         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_INVALID_ARG, "pVideoContentData is null.");
237
238         if (__pVideoContentData == null)
239         {
240                 __pVideoContentData = new (nothrow) _VideoContentData;
241                 SysTryReturnResult(NID_CNT, __pVideoContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
242         }
243
244         __pVideoContentData->width = pVideoContentData->width;
245         __pVideoContentData->height = pVideoContentData->height;
246         __pVideoContentData->framerate = pVideoContentData->framerate;
247         __pVideoContentData->audioBitrate = pVideoContentData->audioBitrate;
248         __pVideoContentData->videoBitrate = pVideoContentData->videoBitrate;
249         __pVideoContentData->duration = pVideoContentData->duration;
250
251         if (pVideoContentData->pArtist != null)
252         {
253                 __pVideoContentData->pArtist = new (nothrow) String(*(pVideoContentData->pArtist));
254                 SysTryReturnResult(NID_CNT, __pVideoContentData->pArtist != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
255         }
256         if (pVideoContentData->pGenre != null)
257         {
258                 __pVideoContentData->pGenre = new (nothrow) String(*(pVideoContentData->pGenre));
259                 SysTryReturnResult(NID_CNT, __pVideoContentData->pGenre != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
260         }
261         if (pVideoContentData->pTitle != null)
262         {
263                 __pVideoContentData->pTitle = new (nothrow) String(*(pVideoContentData->pTitle));
264                 SysTryReturnResult(NID_CNT, __pVideoContentData->pTitle != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
265         }
266         if (pVideoContentData->pAlbumName != null)
267         {
268                 __pVideoContentData->pAlbumName = new (nothrow) String(*(pVideoContentData->pAlbumName));
269                 SysTryReturnResult(NID_CNT, __pVideoContentData->pAlbumName != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
270         }
271
272         return E_SUCCESS;
273 }
274
275 VideoContentInfo::_VideoContentData*
276 VideoContentInfo::GetVideoContentData(void)
277 {
278         if (__pVideoContentData == null)
279         {
280                 __pVideoContentData = new (nothrow) VideoContentInfo::_VideoContentData;
281                 SysTryReturn(NID_CNT, __pVideoContentData != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
282         }
283         return __pVideoContentData;
284 }
285
286 String
287 VideoContentInfo::GetGenre(void) const
288 {
289         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
290
291         if (__pVideoContentData->pGenre == null)
292         {
293                 SysLog(NID_CNT, "GetGenre() failed.");
294                 return String(L"Unknown");
295         }
296
297         return *(__pVideoContentData->pGenre);
298 }
299
300 String
301 VideoContentInfo::GetArtist(void) const
302 {
303         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
304
305         if (__pVideoContentData->pArtist == null)
306         {
307                 SysLog(NID_CNT, "GetArtist() failed.");
308                 return String(L"Unknown");
309         }
310
311         return *(__pVideoContentData->pArtist);
312 }
313
314 int
315 VideoContentInfo::GetBitrate(void) const
316 {
317         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
318
319         if (__pVideoContentData->audioBitrate == 0)
320         {
321                 // The audio bitrate is not managed in video table.
322                 if (this->GetVideoMetadata() != E_SUCCESS)
323                 {
324                         SysLog(NID_CNT, "GetBitrate() failed.");
325                         return 0;
326                 }
327         }
328
329         return __pVideoContentData->audioBitrate;
330 }
331
332 int
333 VideoContentInfo::GetAudioBitrate(void) const
334 {
335         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
336
337         if (__pVideoContentData->audioBitrate == 0)
338         {
339                 // The audio bitrate is not managed in video table.
340                 if (this->GetVideoMetadata() != E_SUCCESS)
341                 {
342                         SysLog(NID_CNT, "GetAudioBitrate() failed.");
343                         return 0;
344                 }
345         }
346
347         return __pVideoContentData->audioBitrate;
348 }
349
350 int
351 VideoContentInfo::GetVideoBitrate(void) const
352 {
353         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
354
355         if (__pVideoContentData->videoBitrate == 0)
356         {
357                 // The video bitrate is not managed in video table.
358                 if (this->GetVideoMetadata() != E_SUCCESS)
359                 {
360                         SysLog(NID_CNT, "GetVideoBitrate() failed.");
361                         return 0;
362                 }
363         }
364
365         return __pVideoContentData->videoBitrate;
366 }
367
368 int
369 VideoContentInfo::GetFramerate(void) const
370 {
371         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
372
373         if (__pVideoContentData->framerate == 0)
374         {
375                 // The framerate is not managed in video table.
376                 if (this->GetVideoMetadata() != E_SUCCESS)
377                 {
378                         SysLog(NID_CNT, "GetFramerate() failed.");
379                         return 0;
380                 }
381         }
382
383         return __pVideoContentData->framerate;
384 }
385
386 int
387 VideoContentInfo::GetWidth(void) const
388 {
389         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
390
391         return __pVideoContentData->width;
392 }
393
394 int
395 VideoContentInfo::GetHeight(void) const
396 {
397         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
398
399         return __pVideoContentData->height;
400 }
401
402 String
403 VideoContentInfo::GetTitle(void) const
404 {
405         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
406
407         if (__pVideoContentData->pTitle == null)
408         {
409                 SysLog(NID_CNT, "GetTitle() failed.");
410                 return String(L"Unknown");
411         }
412
413         return *(__pVideoContentData->pTitle);
414 }
415
416 String
417 VideoContentInfo::GetAlbumName(void) const
418 {
419         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
420
421         if (__pVideoContentData->pAlbumName == null)
422         {
423                 SysLog(NID_CNT, "GetAlbumName() failed.");
424                 return String(L"Unknown");
425         }
426
427         return *(__pVideoContentData->pAlbumName);
428 }
429
430 long
431 VideoContentInfo::GetDuration(void) const
432 {
433         SysAssertf(__pVideoContentData != null, "Not yet constructed. Construct() should be called before use.");
434
435         return __pVideoContentData->duration;
436 }
437
438 result
439 VideoContentInfo::GetVideoMetadata(void) const
440 {
441         ClearLastResult();
442
443         result r = E_SUCCESS;
444
445         VideoMetadata* pVideoMetadata = ContentManagerUtil::GetVideoMetaN(this->GetContentPath());
446         r = GetLastResult();
447         SysTryReturn(NID_CNT, pVideoMetadata != null, r, r, "[%s] GetVideoMetadata() failed.", GetErrorMessage(r));
448
449         // framerate
450         __pVideoContentData->framerate = pVideoMetadata->GetFramerate();
451
452         // audioBitrate
453         __pVideoContentData->audioBitrate = pVideoMetadata->GetAudioBitrate();
454
455         // videoBitrate
456         __pVideoContentData->videoBitrate = pVideoMetadata->GetVideoBitrate();
457
458         delete pVideoMetadata;
459
460         return r;
461 }
462 }}