2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 using System.Collections.Generic;
21 using System.Collections.ObjectModel;
22 using System.Runtime.InteropServices;
23 using System.Threading;
24 using System.Threading.Tasks;
26 namespace Tizen.Content.MediaContent
29 /// MediaContent class API gives the information related to the media stored in the device</summary>
31 /// The API's provide the functionlity to insert, clone, delete, get the number and content of files from DB.
32 /// You can get and set properties and parameters such as storage type, provider, and category of media info,
33 /// handling with thumbnail and updating media info to DB.</remarks>
34 public class MediaInformation
36 private readonly Interop.MediaInformation.SafeMediaInformationHandle _handle;
39 /// Gets the count of media tags for the passed filter in the given mediaId from the media database.
41 /// <since_tizen> 3 </since_tizen>
43 /// int count</returns>
44 /// <param name="filter">The Filter for matching Tags</param>
45 public int GetTagCount(ContentFilter filter)
48 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
49 MediaContentValidator.ThrowIfError(
50 Interop.MediaInformation.GetTagCount(MediaId, handle, out count), "Failed to get count");
56 /// Moves the media info to the given destination path in the media database.
58 /// <since_tizen> 3 </since_tizen>
61 /// <param name="destination">The Destination path</param>
62 public void Move(string destination)
64 MediaContentValidator.ThrowIfError(
65 Interop.MediaInformation.MoveToDB(_handle, destination), "Failed to move");
69 /// Refreshes the media metadata to the media database.
71 /// <since_tizen> 3 </since_tizen>
76 MediaContentValidator.ThrowIfError(
77 Interop.MediaInformation.RefreshMetadataToDB(MediaId), "Failed to refresh");
81 /// Creates a thumbnail image for the given media, asynchronously
82 /// If a thumbnail already exists for the given media, then the path of thumbnail will be returned.
84 /// <since_tizen> 3 </since_tizen>
86 /// Task for creation of Thumbnail </returns>
87 public Task<string> CreateThumbnailAsync()
89 var task = new TaskCompletionSource<string>();
90 Interop.MediaInformation.MediaThumbnailCompletedCallback thumbnailResult = (MediaContentError createResult, string path, IntPtr userData) =>
92 if (createResult != MediaContentError.None)
94 task.SetException(new InvalidOperationException("Failed to create thumbnail:" + createResult));
99 MediaContentValidator.ThrowIfError(
100 Interop.MediaInformation.CreateThumbnail(_handle, thumbnailResult, IntPtr.Zero), "Failed to create thumbnail");
106 /// Creates a thumbnail image for the given media, asynchronously
107 /// which can be cancelled
108 /// If a thumbnail already exists for the given media, then the path of thumbnail will be returned.
110 /// <since_tizen> 3 </since_tizen>
111 /// <param name="cancellationToken">Token to cancel the requested operation</param>
113 /// Task for creation of Thumbnail
115 public Task<string> CreateThumbnailAsync(CancellationToken cancellationToken)
117 var task = new TaskCompletionSource<string>();
118 cancellationToken.Register(() =>
120 MediaContentValidator.ThrowIfError(
121 Interop.MediaInformation.CancelThumbnail(_handle), "Failed to cancel");
126 Interop.MediaInformation.MediaThumbnailCompletedCallback thumbnailResult = (MediaContentError createResult, string path, IntPtr userData) =>
128 if (createResult != MediaContentError.None)
130 task.SetException(new InvalidOperationException("Failed to create thumbnail:" + createResult));
133 task.SetResult(path);
136 MediaContentValidator.ThrowIfError(
137 Interop.MediaInformation.CreateThumbnail(_handle, thumbnailResult, IntPtr.Zero), "Failed to create thumbnail");
143 /// Iterates through the media tag in the given media info from the media database.
145 /// <since_tizen> 3 </since_tizen>
147 /// Task to get all the Tags </returns>
148 /// <param name="filter"> The filter for the Tags</param>
149 public IEnumerable<Tag> GetTags(ContentFilter filter)
151 Collection<Tag> coll = new Collection<Tag>();
153 IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
154 Interop.MediaInformation.MediaTagCallback tagsCallback = (IntPtr tagHandle, IntPtr userData) =>
157 MediaContentValidator.ThrowIfError(
158 Interop.Tag.Clone(out newHandle, tagHandle), "Failed to clone");
159 coll.Add(new Tag(newHandle));
163 MediaContentValidator.ThrowIfError(
164 Interop.MediaInformation.GetAllTags(MediaId, handle, tagsCallback, IntPtr.Zero), "Failed to get information");
170 /// Gets the ID of the media.
172 /// <since_tizen> 3 </since_tizen>
173 public virtual string MediaId
177 IntPtr val = IntPtr.Zero;
180 MediaContentValidator.ThrowIfError(
181 Interop.MediaInformation.GetMediaId(_handle, out val), "Failed to get value");
183 return Marshal.PtrToStringAnsi(val);
187 Interop.Libc.Free(val);
193 /// Gets the path to the media.
195 /// <since_tizen> 3 </since_tizen>
196 public string FilePath
200 IntPtr val = IntPtr.Zero;
203 MediaContentValidator.ThrowIfError(
204 Interop.MediaInformation.GetFilePath(_handle, out val), "Failed to get value");
206 return Marshal.PtrToStringAnsi(val);
210 Interop.Libc.Free(val);
216 /// Name of the media.
218 /// <since_tizen> 3 </since_tizen>
219 public string DisplayName
223 IntPtr val = IntPtr.Zero;
226 MediaContentValidator.ThrowIfError(
227 Interop.MediaInformation.GetDisplayName(_handle, out val), "Failed to get value");
229 return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
233 Interop.Libc.Free(val);
239 MediaContentValidator.ThrowIfError(
240 Interop.MediaInformation.SetDisplayName(_handle, value), "failed to set value");
245 /// Gets the content type of the media.
247 /// <since_tizen> 3 </since_tizen>
248 public MediaContentType MediaType
252 MediaContentType contentType = MediaContentType.Others;
253 MediaContentValidator.ThrowIfError(
254 Interop.MediaInformation.GetMediaType(_handle, out contentType), "Failed to get value");
261 /// Gets the MIME type from the media.
263 /// <since_tizen> 3 </since_tizen>
264 public string MimeType
268 IntPtr val = IntPtr.Zero;
271 MediaContentValidator.ThrowIfError(
272 Interop.MediaInformation.GetMimeType(_handle, out val), "Failed to get value");
274 return Marshal.PtrToStringAnsi(val);
278 Interop.Libc.Free(val);
284 /// Gets the media file size in bytes.
286 /// <since_tizen> 3 </since_tizen>
292 MediaContentValidator.ThrowIfError(
293 Interop.MediaInformation.GetSize(_handle, out size), "Failed to get value");
300 /// Addition time of the media.
302 /// <since_tizen> 3 </since_tizen>
303 public DateTime AddedAt
308 MediaContentValidator.ThrowIfError(
309 Interop.MediaInformation.GetAddedTime(_handle, out time), "Failed to get value");
311 DateTime utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
313 return utc.ToLocalTime();
318 MediaContentValidator.ThrowIfError(
319 Interop.MediaInformation.SetAddedTime(_handle, (int)value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds), "failed to set time");
324 /// Gets the date of modification of media.
326 /// <since_tizen> 3 </since_tizen>
327 public DateTime ModifiedAt
332 MediaContentValidator.ThrowIfError(
333 Interop.MediaInformation.GetModifiedTime(_handle, out time), "Failed to get value");
335 DateTime utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
337 return utc.ToLocalTime();
342 /// Gets the timeline of media.
344 /// <since_tizen> 3 </since_tizen>
345 public DateTime TimeLine
350 MediaContentValidator.ThrowIfError(
351 Interop.MediaInformation.GetTimeline(_handle, out time), "Failed to get value");
353 DateTime utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
355 return utc.ToLocalTime();
360 /// Gets the thumbnail of media.
362 /// <since_tizen> 3 </since_tizen>
363 public string ThumbnailPath
367 IntPtr val = IntPtr.Zero;
370 MediaContentValidator.ThrowIfError(
371 Interop.MediaInformation.GetThumbnailPath(_handle, out val), "Failed to get value");
373 return Marshal.PtrToStringAnsi(val);
377 Interop.Libc.Free(val);
383 /// Description of media.
384 /// If the media info has no description, the property returns empty string.
386 /// <since_tizen> 3 </since_tizen>
387 public string Description
391 IntPtr val = IntPtr.Zero;
394 MediaContentValidator.ThrowIfError(
395 Interop.MediaInformation.GetDescription(_handle, out val), "Failed to get value");
397 return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
401 Interop.Libc.Free(val);
407 MediaContentValidator.ThrowIfError(
408 Interop.MediaInformation.SetDescription(_handle, value), "failed to set value");
413 /// Longitude of media.
414 /// Default Value is 0.0.
416 /// <since_tizen> 3 </since_tizen>
417 public double Longitude
421 double longitude = 0.0;
422 MediaContentValidator.ThrowIfError(
423 Interop.MediaInformation.GetLongitude(_handle, out longitude), "Failed to get value");
430 MediaContentValidator.ThrowIfError(
431 Interop.MediaInformation.SetLongitude(_handle, value), "failed to set value");
436 /// Latitude of media.
437 /// Default Value is 0.0.
439 /// <since_tizen> 3 </since_tizen>
440 public double Latitude
444 double latitude = 0.0;
445 MediaContentValidator.ThrowIfError(
446 Interop.MediaInformation.GetLatitude(_handle, out latitude), "Failed to get value");
453 MediaContentValidator.ThrowIfError(
454 Interop.MediaInformation.SetLatitude(_handle, value), "failed to set value");
459 /// Altitude of media.
460 /// Default Value is 0.0.
462 /// <since_tizen> 3 </since_tizen>
463 public double Altitude
467 double altitude = 0.0;
468 MediaContentValidator.ThrowIfError(
469 Interop.MediaInformation.GetAltitude(_handle, out altitude), "Failed to get value");
476 MediaContentValidator.ThrowIfError(
477 Interop.MediaInformation.SetAltitude(_handle, value), "failed to set value");
482 /// Weather information of media.
484 /// <since_tizen> 3 </since_tizen>
485 public string Weather
489 IntPtr val = IntPtr.Zero;
492 MediaContentValidator.ThrowIfError(
493 Interop.MediaInformation.GetWeather(_handle, out val), "Failed to get value");
495 return Marshal.PtrToStringAnsi(val);
499 Interop.Libc.Free(val);
505 MediaContentValidator.ThrowIfError(
506 Interop.MediaInformation.SetWeather(_handle, value), "failed to set value");
513 /// <since_tizen> 3 </since_tizen>
519 MediaContentValidator.ThrowIfError(
520 Interop.MediaInformation.GetRating(_handle, out rating), "Failed to get value");
526 MediaContentValidator.ThrowIfError(
527 Interop.MediaInformation.SetRating(_handle, value), "failed to set value");
532 /// Favorite status of media.
533 /// true if media info is set as favorite, otherwise false if media info is not set as favorite.
535 /// <since_tizen> 3 </since_tizen>
536 public bool IsFavourite
540 bool isFavourtite = false;
541 MediaContentValidator.ThrowIfError(
542 Interop.MediaInformation.GetFavorite(_handle, out isFavourtite), "Failed to get value");
549 MediaContentValidator.ThrowIfError(
550 Interop.MediaInformation.SetFavorite(_handle, value), "failed to set value");
557 /// <since_tizen> 3 </since_tizen>
562 IntPtr val = IntPtr.Zero;
565 MediaContentValidator.ThrowIfError(
566 Interop.MediaInformation.GetAuthor(_handle, out val), "Failed to get value");
568 return Marshal.PtrToStringAnsi(val);
572 Interop.Libc.Free(val);
578 MediaContentValidator.ThrowIfError(
579 Interop.MediaInformation.SetAuthor(_handle, value), "failed to set value");
584 /// Provider of media.
586 /// <since_tizen> 3 </since_tizen>
587 public string Provider
591 IntPtr val = IntPtr.Zero;
594 MediaContentValidator.ThrowIfError(
595 Interop.MediaInformation.GetProvider(_handle, out val), "Failed to get value");
597 return Marshal.PtrToStringAnsi(val);
601 Interop.Libc.Free(val);
607 MediaContentValidator.ThrowIfError(
608 Interop.MediaInformation.SetProvider(_handle, value), "failed to set value");
613 /// Content name of media.
615 /// <since_tizen> 3 </since_tizen>
616 public string ContentName
620 IntPtr val = IntPtr.Zero;
623 MediaContentValidator.ThrowIfError(
624 Interop.MediaInformation.GetContentName(_handle, out val), "Failed to get value");
626 return Marshal.PtrToStringAnsi(val);
630 Interop.Libc.Free(val);
636 MediaContentValidator.ThrowIfError(
637 Interop.MediaInformation.SetContentName(_handle, value), "failed to set value");
642 /// Gets the title of media.
643 /// If the media content has no title, the property returns empty string.
645 /// <since_tizen> 3 </since_tizen>
650 IntPtr val = IntPtr.Zero;
653 MediaContentValidator.ThrowIfError(
654 Interop.MediaInformation.GetTitle(_handle, out val), "Failed to get value");
656 return MediaContentValidator.CheckString(Marshal.PtrToStringAnsi(val));
660 Interop.Libc.Free(val);
666 /// Category of media.
668 /// <since_tizen> 3 </since_tizen>
669 public string Category
673 IntPtr val = IntPtr.Zero;
676 MediaContentValidator.ThrowIfError(
677 Interop.MediaInformation.GetCategory(_handle, out val), "Failed to get value");
679 return Marshal.PtrToStringAnsi(val);
683 Interop.Libc.Free(val);
689 MediaContentValidator.ThrowIfError(
690 Interop.MediaInformation.SetCategory(_handle, value), "failed to set value");
695 /// location tag of media.
697 /// <since_tizen> 3 </since_tizen>
698 public string LocationTag
702 IntPtr val = IntPtr.Zero;
705 MediaContentValidator.ThrowIfError(
706 Interop.MediaInformation.GetLocationTag(_handle, out val), "Failed to get value");
708 return Marshal.PtrToStringAnsi(val);
712 Interop.Libc.Free(val);
718 MediaContentValidator.ThrowIfError(
719 Interop.MediaInformation.SetLocationTag(_handle, value), "failed to set value");
724 /// Age Rating of media.
726 /// <since_tizen> 3 </since_tizen>
727 public string AgeRating
731 IntPtr val = IntPtr.Zero;
734 MediaContentValidator.ThrowIfError(
735 Interop.MediaInformation.GetAgeRating(_handle, out val), "Failed to get value");
737 return Marshal.PtrToStringAnsi(val);
741 Interop.Libc.Free(val);
747 MediaContentValidator.ThrowIfError(
748 Interop.MediaInformation.SetAgeRating(_handle, value), "Failed to set value");
753 /// Keyword of media.
755 /// <since_tizen> 3 </since_tizen>
756 public string Keyword
760 IntPtr val = IntPtr.Zero;
763 MediaContentValidator.ThrowIfError(
764 Interop.MediaInformation.GetKeyword(_handle, out val), "Failed to get value");
766 return Marshal.PtrToStringAnsi(val);
770 Interop.Libc.Free(val);
776 MediaContentValidator.ThrowIfError(
777 Interop.MediaInformation.SetKeyword(_handle, value), "failed to set value");
782 /// Gets the storage id of media.
784 /// <since_tizen> 3 </since_tizen>
785 public string StorageId
789 IntPtr val = IntPtr.Zero;
792 MediaContentValidator.ThrowIfError(
793 Interop.MediaInformation.GetStorageId(_handle, out val), "Failed to get value");
795 return Marshal.PtrToStringAnsi(val);
799 Interop.Libc.Free(val);
805 /// Checks whether the media is protected via DRM.
807 /// <since_tizen> 3 </since_tizen>
813 MediaContentValidator.ThrowIfError(
814 Interop.MediaInformation.IsDrm(_handle, out isDRM), "Failed to get value");
821 /// Gets the storage type of media.
823 /// <since_tizen> 3 </since_tizen>
824 public ContentStorageType StorageType
828 ContentStorageType storageType = ContentStorageType.Internal;
829 MediaContentValidator.ThrowIfError(
830 Interop.MediaInformation.GetStorageType(_handle, out storageType), "Failed to get value");
837 /// Number which represents how many times given content has been played.
838 /// While Setting the played count, it will only be incremented by 1, the value provided will be ignored.
840 /// <since_tizen> 3 </since_tizen>
841 public int PlayedCount
846 MediaContentValidator.ThrowIfError(
847 Interop.MediaInformation.GetPlayedCount(_handle, out playedCount), "Failed to get value");
854 MediaContentValidator.ThrowIfError(
855 Interop.MediaInformation.IncreasePlayedCount(_handle), "failed to set value");
860 /// Content's latest played(opened) time of the media file.
861 /// for set the current time is automatically taken from the system, the value provided will be ignored.
863 /// <since_tizen> 3 </since_tizen>
864 public DateTime PlayedAt
869 MediaContentValidator.ThrowIfError(
870 Interop.MediaInformation.GetPlayedAt(_handle, out time), "Failed to get value");
872 DateTime utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
874 return utc.ToLocalTime();
879 MediaContentValidator.ThrowIfError(
880 Interop.MediaInformation.SetPlayedAt(_handle), "failed to set value");
884 internal IntPtr MediaHandle
888 return _handle.DangerousGetHandle();
892 internal MediaInformation(Interop.MediaInformation.SafeMediaInformationHandle handle)