Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / PlayList.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 using System;
19 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
21 using System.Threading.Tasks;
22
23 namespace Tizen.Content.MediaContent
24 {
25     /// <summary>
26     /// The PlayList API provides functions to manage media playlists.
27     /// </summary>
28     /// <remarks>
29     /// A PlayList is a list of songs which can be played in some sequence i.e. sequential or shuffled order.
30     /// The Media PlayList API provides functions to insert, delete or updates a media playlist in the database.
31     /// </remarks>
32     public class PlayList : ContentCollection
33     {
34         private readonly IDictionary<string, int> _dictionary = new Dictionary<string, int>();
35         private IntPtr _playlistHandle = IntPtr.Zero;
36         internal IntPtr Handle
37         {
38             get
39             {
40                 if (_playlistHandle == IntPtr.Zero)
41                 {
42                     throw new ObjectDisposedException(nameof(PlayList));
43                 }
44
45                 return _playlistHandle;
46             }
47
48             set
49             {
50                 _playlistHandle = value;
51             }
52         }
53
54         private void RefreshPlaylistDictionary()
55         {
56             _dictionary.Clear();
57             Interop.Playlist.PlaylistMemberCallback callback = (int memberId, IntPtr mediaHandle, IntPtr data) =>
58             {
59                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
60                 IntPtr val = IntPtr.Zero;
61                 try
62                 {
63                     MediaContentValidator.ThrowIfError(
64                         Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
65
66                     Interop.MediaInformation.GetMediaId(newHandle, out val);
67                     _dictionary.Add(Marshal.PtrToStringAnsi(val), memberId);
68                     return true;
69                 }
70                 finally
71                 {
72                     Interop.Libc.Free(val);
73                 }
74             };
75             MediaContentValidator.ThrowIfError(
76                 Interop.Playlist.ForeachMediaFromDb(Id, IntPtr.Zero, callback, IntPtr.Zero), "Failed to get playlist items");
77         }
78
79         /// <summary>
80         /// The ID of the media playlist
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         public int Id
84         {
85             get
86             {
87                 int id;
88                 MediaContentValidator.ThrowIfError(
89                     Interop.Playlist.GetPlaylistId(Handle, out id), "Failed to get value");
90
91                 return id;
92             }
93         }
94
95         internal string _playListName;
96         /// <summary>
97         /// The playlist name
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         public string Name
101         {
102             get
103             {
104                 return _playListName;
105             }
106
107             set
108             {
109                 MediaContentValidator.ThrowIfError(
110                     Interop.Playlist.SetName(Handle, value), "Failed to set value");
111                 _playListName = value;
112             }
113         }
114         /// <summary>
115         /// The path of the thumbnail
116         /// </summary>
117         /// <since_tizen> 3 </since_tizen>
118         public string ThumbnailPath
119         {
120             get
121             {
122                 IntPtr val = IntPtr.Zero;
123                 try
124                 {
125                     MediaContentValidator.ThrowIfError(
126                         Interop.Playlist.GetThumbnailPath(Handle, out val), "Failed to get value");
127                     return Marshal.PtrToStringAnsi(val);
128                 }
129                 finally
130                 {
131                     Interop.Libc.Free(val);
132                 }
133             }
134
135             set
136             {
137                 MediaContentValidator.ThrowIfError(
138                     Interop.Playlist.SetThumbnailPath(Handle, value), "Failed to set value");
139             }
140         }
141
142         /// <summary>
143         /// The constructor to create a new playlist with the given name in the media database.
144         /// </summary>
145         /// <since_tizen> 3 </since_tizen>
146         /// <param name="name">The name of the inserted playlist</param>
147         public PlayList(string name)
148         {
149             _playListName = name;
150         }
151
152         internal PlayList(IntPtr handle)
153         {
154             _playlistHandle = handle;
155             IntPtr val = IntPtr.Zero;
156             try
157             {
158                 MediaContentValidator.ThrowIfError(
159                     Interop.Playlist.GetName(handle, out val), "Failed to get value");
160                 _playListName = Marshal.PtrToStringAnsi(val);
161             }
162             finally
163             {
164                 Interop.Libc.Free(val);
165             }
166         }
167
168         /// <summary>
169         /// Adds a new media info to the playlist.
170         /// </summary>
171         /// <since_tizen> 3 </since_tizen>
172         /// <param name="mediaContent">The AudioContent obect to be added</param>
173         public void AddItem(MediaInformation mediaContent)
174         {
175             MediaContentValidator.ThrowIfError(
176                 Interop.Playlist.AddMedia(Handle, mediaContent.MediaId), "Failed to add item");
177         }
178
179         /// <summary>
180         /// Removes the playlist members related with the media from the given playlist.
181         /// </summary>
182         /// <since_tizen> 3 </since_tizen>
183         /// <param name="media">The AudioContent object to be removed</param>
184         public void RemoveItem(MediaInformation media)
185         {
186             int memberId = 0;
187             RefreshPlaylistDictionary();
188             _dictionary.TryGetValue(media.MediaId, out memberId);
189             MediaContentValidator.ThrowIfError(
190                 Interop.Playlist.RemoveMedia(Handle, memberId), "Failed to remove item");
191         }
192
193         /// <summary>
194         /// Sets the playing order in the playlist.
195         /// </summary>
196         /// <since_tizen> 3 </since_tizen>
197         /// <param name="media">The playlist reference</param>
198         /// <param name="playOrder">The playing order</param>
199         public void SetPlayOrder(MediaInformation media, int playOrder)
200         {
201             int memberId;
202             RefreshPlaylistDictionary();
203             _dictionary.TryGetValue(media.MediaId, out memberId);
204             MediaContentValidator.ThrowIfError(
205                 Interop.Playlist.SetPlayOrder(Handle, memberId, playOrder), "Failed to set play order");
206         }
207
208         /// <summary>
209         /// Gets the playing order in the playlist for the passed member id.
210         /// </summary>
211         /// <since_tizen> 3 </since_tizen>
212         /// <param name="media">The MediaInformation instance</param>
213         /// <returns>The number of play order</returns>
214         public int GetPlayOrder(MediaInformation media)
215         {
216             int playOrder;
217             int memberId;
218             RefreshPlaylistDictionary();
219             _dictionary.TryGetValue(media.MediaId, out memberId);
220             MediaContentValidator.ThrowIfError(
221                 Interop.Playlist.GetPlayOrder(Handle, memberId, out playOrder), "Failed to get play order");
222
223             return playOrder;
224         }
225
226         /// <summary>
227         /// Imports the playlist from m3u playlist file.
228         /// </summary>
229         /// <since_tizen> 3 </since_tizen>
230         /// <param name="name">The name of the playlist to save</param>
231         /// <param name="filePath">The path to import the playlist file</param>
232         /// <returns>The imported PlayList object</returns>
233         public static PlayList Import(string name, string filePath)
234         {
235             PlayList playList = null;
236             IntPtr playlistHandle;
237
238             MediaContentValidator.ThrowIfError(
239                 Interop.Playlist.ImportFromFile(name, filePath, out playlistHandle), "Failed to import");
240
241             playList = new PlayList(name);
242             playList.Handle = playlistHandle;
243             return playList;
244         }
245
246         /// <summary>
247         /// Exports the playlist to m3u playlist file.
248         /// </summary>
249         /// <since_tizen> 3 </since_tizen>
250         /// <param name="list">The playlist instance to export</param>
251         /// <param name="filePath">The path to save exported playlist</param>
252         /// <returns>path The path to export the playlist</returns>
253         public static void Export(PlayList list, string filePath)
254         {
255             MediaContentValidator.ThrowIfError(
256                 Interop.Playlist.ExportToFile(list.Handle, filePath), "Failed to export playlist:" + filePath);
257         }
258
259         /// <summary>
260         /// Gets the number of the media info for the given playlist present in the media database.
261         /// </summary>
262         /// <since_tizen> 3 </since_tizen>
263         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
264         /// <returns>The number of media contents matching the filter passed</returns>
265         public override int GetMediaInformationCount(ContentFilter filter)
266         {
267             int mediaCount;
268             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
269             MediaContentValidator.ThrowIfError(
270                 Interop.Playlist.GetMediaCountFromDb(Id, handle, out mediaCount), "Failed to get media count");
271
272             return mediaCount;
273         }
274
275         public override void Dispose()
276         {
277             if (_playlistHandle != IntPtr.Zero)
278             {
279                 Interop.Playlist.Destroy(_playlistHandle);
280                 _playlistHandle = IntPtr.Zero;
281             }
282         }
283
284         /// <summary>
285         /// Iterates through the media files with an optional filter in the given audio playlist from the media database.
286         /// This function gets all media files associated with the given media playlist and meeting desired filter option.
287         /// If NULL is passed to the filter, no filtering is applied.
288         /// </summary>
289         /// <since_tizen> 3 </since_tizen>
290         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
291         /// <returns>List of content media items matching the passed filter</returns>
292         public override IEnumerable<MediaInformation> GetMediaInformations(ContentFilter filter)
293         {
294             List<MediaInformation> mediaContents = new List<MediaInformation>();
295             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
296             Interop.Playlist.PlaylistMemberCallback callback = (int memberId, IntPtr mediaHandle, IntPtr data) =>
297             {
298                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
299                 MediaContentValidator.ThrowIfError(
300                     Interop.MediaInformation.Clone(out newHandle, mediaHandle), "Failed to clone media");
301
302                 MediaContentType type;
303                 Interop.MediaInformation.GetMediaType(newHandle, out type);
304                 if (type == MediaContentType.Image)
305                 {
306                     Interop.ImageInformation.SafeImageInformationHandle imageInfo;
307                     MediaContentValidator.ThrowIfError(
308                         Interop.MediaInformation.GetImage(mediaHandle, out imageInfo), "Failed to get image information");
309
310                     mediaContents.Add(new ImageInformation(imageInfo, newHandle));
311                 }
312                 else if ((type == MediaContentType.Music) || (type == MediaContentType.Sound))
313                 {
314                     Interop.AudioInformation.SafeAudioInformationHandle audioInfo;
315                     MediaContentValidator.ThrowIfError(
316                         Interop.MediaInformation.GetAudio(mediaHandle, out audioInfo), "Failed to get audio information");
317
318                     mediaContents.Add(new AudioInformation(audioInfo, newHandle));
319                 }
320                 else if (type == MediaContentType.Video)
321                 {
322                     Interop.VideoInformation.SafeVideoInformationHandle videoInfo;
323                     MediaContentValidator.ThrowIfError(
324                         Interop.MediaInformation.GetVideo(mediaHandle, out videoInfo), "Failed to get video information");
325
326                     mediaContents.Add(new VideoInformation(videoInfo, newHandle));
327                 }
328                 else if (type == MediaContentType.Others)
329                 {
330                     mediaContents.Add(new MediaInformation(newHandle));
331                 }
332
333                 return true;
334             };
335             MediaContentValidator.ThrowIfError(
336                 Interop.Playlist.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero), "Failed to get media information");
337
338             return mediaContents;
339         }
340     }
341 }