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