Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / VideoInformation.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
18
19 using System;
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using System.Threading.Tasks;
23 using System.Collections.ObjectModel;
24
25 namespace Tizen.Content.MediaContent
26 {
27     /// <summary>
28     /// VideoContent class API gives the information related to the image media stored in the device
29     /// </summary>
30     public class VideoInformation : MediaInformation
31     {
32         /// <summary>
33         ///  Gets the ID of the media.
34         /// </summary>
35         /// <since_tizen> 3 </since_tizen>
36         public string MediaId
37         {
38             get
39             {
40                 IntPtr val = IntPtr.Zero;
41                 try
42                 {
43                     MediaContentValidator.ThrowIfError(
44                         Interop.VideoInformation.GetMediaId(_handle, out val), "Failed to get value");
45
46                     return Marshal.PtrToStringAnsi(val);
47                 }
48                 finally
49                 {
50                     Interop.Libc.Free(val);
51                 }
52             }
53         }
54
55         /// <summary>
56         ///  Gets the album name.
57         ///  If the media content has no album information, the property returns empty string.
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         public string Album
61         {
62             get
63             {
64                 IntPtr val = IntPtr.Zero;
65                 try
66                 {
67                     MediaContentValidator.ThrowIfError(
68                         Interop.VideoInformation.GetAlbum(_handle, out val), "Failed to get value");
69
70                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
71                 }
72                 finally
73                 {
74                     Interop.Libc.Free(val);
75                 }
76             }
77         }
78
79         /// <summary>
80         ///  Gets the artist name.
81         ///  If the media content has no artist information, the property returns empty string.
82         /// </summary>
83         /// <since_tizen> 3 </since_tizen>
84         public string Artist
85         {
86             get
87             {
88                 IntPtr val = IntPtr.Zero;
89                 try
90                 {
91                     MediaContentValidator.ThrowIfError(
92                         Interop.VideoInformation.GetArtist(_handle, out val), "Failed to get value");
93
94                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
95                 }
96                 finally
97                 {
98                     Interop.Libc.Free(val);
99                 }
100             }
101         }
102
103         /// <summary>
104         ///  Gets the album artist name.
105         ///  If the media content has no album artist information, the property returns empty string.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         public string AlbumArtist
109         {
110             get
111             {
112                 IntPtr val = IntPtr.Zero;
113                 try
114                 {
115                     MediaContentValidator.ThrowIfError(
116                         Interop.VideoInformation.GetAlbumArtist(_handle, out val), "Failed to get value");
117
118                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
119                 }
120                 finally
121                 {
122                     Interop.Libc.Free(val);
123                 }
124             }
125         }
126
127         /// <summary>
128         ///  Gets the genre name.
129         ///  If the media content has no genre information, the property returns empty string.
130         /// </summary>
131         /// <since_tizen> 3 </since_tizen>
132         public string Genre
133         {
134             get
135             {
136                 IntPtr val = IntPtr.Zero;
137                 try
138                 {
139                     MediaContentValidator.ThrowIfError(
140                         Interop.VideoInformation.GetGenre(_handle, out val), "Failed to get value");
141
142                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
143                 }
144                 finally
145                 {
146                     Interop.Libc.Free(val);
147                 }
148             }
149         }
150
151         /// <summary>
152         ///  Gets the composer name.
153         ///  If the media content has no composer information, the property returns empty string.
154         /// </summary>
155         /// <since_tizen> 3 </since_tizen>
156         public string Composer
157         {
158             get
159             {
160                 IntPtr val = IntPtr.Zero;
161                 try
162                 {
163                     MediaContentValidator.ThrowIfError(
164                         Interop.VideoInformation.GetComposer(_handle, out val), "Failed to get value");
165
166                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
167                 }
168                 finally
169                 {
170                     Interop.Libc.Free(val);
171                 }
172             }
173         }
174
175         /// <summary>
176         ///  Gets the year.
177         ///  If the media content has no year information, the property returns empty string.
178         /// </summary>
179         /// <since_tizen> 3 </since_tizen>
180         public string Year
181         {
182             get
183             {
184                 IntPtr val = IntPtr.Zero;
185                 try
186                 {
187                     MediaContentValidator.ThrowIfError(
188                         Interop.VideoInformation.GetYear(_handle, out val), "Failed to get value");
189
190                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
191                 }
192                 finally
193                 {
194                     Interop.Libc.Free(val);
195                 }
196             }
197         }
198
199         /// <summary>
200         ///  Gets the recorded date.
201         /// </summary>
202         /// <since_tizen> 3 </since_tizen>
203         public string RecordedDate
204         {
205             get
206             {
207                 IntPtr val = IntPtr.Zero;
208                 try
209                 {
210                     MediaContentValidator.ThrowIfError(
211                         Interop.VideoInformation.GetRecordedDate(_handle, out val), "Failed to get value");
212
213                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
214                 }
215                 finally
216                 {
217                     Interop.Libc.Free(val);
218                 }
219             }
220         }
221
222         /// <summary>
223         ///  Gets the copyright notice.
224         ///  If the media content has no copyright information, the property returns empty string.
225         /// </summary>
226         /// <since_tizen> 3 </since_tizen>
227         public string Copyright
228         {
229             get
230             {
231                 IntPtr val = IntPtr.Zero;
232                 try
233                 {
234                     MediaContentValidator.ThrowIfError(
235                         Interop.VideoInformation.GetCopyright(_handle, out val), "Failed to get value");
236
237                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
238                 }
239                 finally
240                 {
241                     Interop.Libc.Free(val);
242                 }
243             }
244         }
245
246         /// <summary>
247         ///  Gets the track number.
248         ///  If the media content has no track information, the property returns empty string.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         public string TrackNumber
252         {
253             get
254             {
255                 IntPtr val = IntPtr.Zero;
256                 try
257                 {
258                     MediaContentValidator.ThrowIfError(
259                         Interop.VideoInformation.GetTrackNum(_handle, out val), "Failed to get value");
260
261                     return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
262                 }
263                 finally
264                 {
265                     Interop.Libc.Free(val);
266                 }
267             }
268         }
269
270         /// <summary>
271         ///  Gets the bitrate in bit per second [bps].
272         /// </summary>
273         /// <since_tizen> 3 </since_tizen>
274         public int BitRate
275         {
276             get
277             {
278                 int bitrate = 0;
279                 MediaContentValidator.ThrowIfError(
280                     Interop.VideoInformation.GetBitRate(_handle, out bitrate), "Failed to get value");
281
282                 return bitrate;
283             }
284         }
285
286         /// <summary>
287         ///  Gets the track duration in Milliseconds.
288         /// </summary>
289         /// <since_tizen> 3 </since_tizen>
290         public int Duration
291         {
292             get
293             {
294                 int duration = 0;
295                 MediaContentValidator.ThrowIfError(
296                     Interop.VideoInformation.GetDuration(_handle, out duration), "Failed to get value");
297
298                 return duration;
299             }
300         }
301
302         /// <summary>
303         ///  Gets the video width in pixels.
304         /// </summary>
305         /// <since_tizen> 3 </since_tizen>
306         public int Width
307         {
308             get
309             {
310                 int width = 0;
311                 MediaContentValidator.ThrowIfError(
312                     Interop.VideoInformation.GetWidth(_handle, out width), "Failed to get value");
313
314                 return width;
315             }
316         }
317
318         /// <summary>
319         ///  Gets the video height in pixels.
320         /// </summary>
321         /// <since_tizen> 3 </since_tizen>
322         public int Height
323         {
324             get
325             {
326                 int height = 0;
327                 MediaContentValidator.ThrowIfError(
328                     Interop.VideoInformation.GetHeight(_handle, out height), "Failed to get value");
329
330                 return height;
331             }
332         }
333
334         /// <summary>
335         /// Gets the number of bookmarks for the passed filter in the given media ID from the media database.
336         /// </summary>
337         /// <since_tizen> 3 </since_tizen>
338         /// <returns>
339         /// int count</returns>
340         /// <param name="filter">The Filter for matching Bookmarks</param>
341         public int GetMediaBookmarkCount(ContentFilter filter)
342         {
343             int count = 0;
344             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
345             MediaContentValidator.ThrowIfError(
346                 Interop.MediaInformation.GetBookmarkCount(MediaId, handle, out count), "Failed to get count");
347
348             return count;
349         }
350
351         /// <summary>
352         /// Iterates through the media bookmark in the given media info from the media database.
353         /// </summary>
354         /// <since_tizen> 3 </since_tizen>
355         /// <returns>
356         /// Task to get all the Bookmarks </returns>
357         /// <param name="filter"> filter for the Tags</param>
358         public IEnumerable<MediaBookmark> GetMediaBookmarks(ContentFilter filter)
359         {
360             Collection<MediaBookmark> result = new Collection<MediaBookmark>();
361             IntPtr filterHandle = (filter != null) ? filter.Handle : IntPtr.Zero;
362             Interop.MediaInformation.MediaBookmarkCallback callback = (IntPtr handle, IntPtr userData) =>
363             {
364                 IntPtr newHandle = IntPtr.Zero;
365                 MediaContentValidator.ThrowIfError(
366                     Interop.MediaBookmark.Clone(out newHandle, handle), "Failed to clone Tag");
367                 result.Add(new MediaBookmark(newHandle));
368                 return true;
369             };
370             MediaContentValidator.ThrowIfError(
371                 Interop.MediaInformation.GetAllBookmarks(MediaId, filterHandle, callback, IntPtr.Zero), "Failed to get value");
372
373             return result;
374         }
375
376         /// <summary>
377         /// Adds a bookmark to the video
378         /// </summary>
379         /// <since_tizen> 3 </since_tizen>
380         /// <param name="offset">Offset of the video in seconds</param>
381         /// <param name="thumbnailPath">Thumbnail path for the bookmark</param>
382         /// <returns>Task with added MediaBookmark instance </returns>
383         public MediaBookmark AddBookmark(uint offset, string thumbnailPath)
384         {
385             MediaBookmark result = null;
386             ContentManager.Database.Insert(MediaId, offset, thumbnailPath);
387             ContentFilter bookmarkfilter = new ContentFilter();
388             bookmarkfilter.Condition = ContentColumns.Bookmark.Offset + " = " + offset;
389             IEnumerable<MediaBookmark> bookmarksList = null;
390             bookmarksList = GetMediaBookmarks(bookmarkfilter);
391             foreach (MediaBookmark bookmark in bookmarksList)
392             {
393                 if (bookmark.Offset == offset)
394                 {
395                     result = bookmark;
396                     break;
397                 }
398             }
399
400             bookmarkfilter.Dispose();
401             return result;
402         }
403
404         /// <summary>
405         /// Deletes a bookmark from the media database.
406         /// For other types Unsupported exception is thrown.
407         /// </summary>
408         /// <since_tizen> 3 </since_tizen>
409         /// <param name="bookmark">The bookmark to be deleted</param>
410         public void DeleteBookmark(MediaBookmark bookmark)
411         {
412             ContentManager.Database.Delete(bookmark);
413         }
414
415         internal IntPtr VideoHandle
416         {
417             get
418             {
419                 return _handle.DangerousGetHandle();
420             }
421         }
422
423         private readonly Interop.VideoInformation.SafeVideoInformationHandle _handle;
424
425         internal VideoInformation(Interop.VideoInformation.SafeVideoInformationHandle handle, Interop.MediaInformation.SafeMediaInformationHandle mediaInformationHandle)
426             : base(mediaInformationHandle)
427         {
428             _handle = handle;
429         }
430     }
431 }