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