Release 8.0.0.15812
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaControlMetadata.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 using System;
18 using Native = Interop.MediaControllerPlaylist;
19
20 namespace Tizen.Multimedia.Remoting
21 {
22     /// <summary>
23     /// Represents metadata for media control.
24     /// </summary>
25     /// <since_tizen> 4 </since_tizen>
26     public class MediaControlMetadata
27     {
28         /// <summary>
29         /// Initializes a new instance of the <see cref="MediaControlMetadata"/> class.
30         /// </summary>
31         /// <since_tizen> 4 </since_tizen>
32         public MediaControlMetadata()
33         {
34         }
35
36         internal MediaControlMetadata(IntPtr handle)
37         {
38             // If native framework return null handle,
39             // it means server doesn't set metadata yet and it's not error.
40             // So we need to return empty metadata instance as native framework does.
41             if (handle == IntPtr.Zero)
42             {
43                 return;
44             }
45
46             Title = Native.GetMetadata(handle, MediaControllerNativeAttribute.Title);
47             Artist = Native.GetMetadata(handle, MediaControllerNativeAttribute.Artist);
48             Album = Native.GetMetadata(handle, MediaControllerNativeAttribute.Album);
49             Author = Native.GetMetadata(handle, MediaControllerNativeAttribute.Author);
50             Genre = Native.GetMetadata(handle, MediaControllerNativeAttribute.Genre);
51             Duration = Native.GetMetadata(handle, MediaControllerNativeAttribute.Duration);
52             Date = Native.GetMetadata(handle, MediaControllerNativeAttribute.Date);
53             Copyright = Native.GetMetadata(handle, MediaControllerNativeAttribute.Copyright);
54             Description = Native.GetMetadata(handle, MediaControllerNativeAttribute.Description);
55             TrackNumber = Native.GetMetadata(handle, MediaControllerNativeAttribute.TrackNumber);
56             AlbumArtPath = Native.GetMetadata(handle, MediaControllerNativeAttribute.Picture);
57
58             EncodedSeason = Native.GetMetadata(handle, MediaControllerNativeAttribute.Season);
59             EncodedEpisode = Native.GetMetadata(handle, MediaControllerNativeAttribute.Episode);
60             EncodedResolution = Native.GetMetadata(handle, MediaControllerNativeAttribute.Resolution);
61         }
62
63         /// <summary>
64         /// Gets or sets the title.
65         /// </summary>
66         /// <since_tizen> 4 </since_tizen>
67         public string Title { get; set; }
68
69         /// <summary>
70         /// Gets or sets the artist.
71         /// </summary>
72         /// <since_tizen> 4 </since_tizen>
73         public string Artist { get; set; }
74
75         /// <summary>
76         /// Gets or sets the album.
77         /// </summary>
78         /// <since_tizen> 4 </since_tizen>
79         public string Album { get; set; }
80
81         /// <summary>
82         /// Gets or sets the author.
83         /// </summary>
84         /// <since_tizen> 4 </since_tizen>
85         public string Author { get; set; }
86
87         /// <summary>
88         /// Gets or sets the genre.
89         /// </summary>
90         /// <since_tizen> 4 </since_tizen>
91         public string Genre { get; set; }
92
93         /// <summary>
94         /// Gets or sets the duration.
95         /// </summary>
96         /// <since_tizen> 4 </since_tizen>
97         public string Duration { get; set; }
98
99         /// <summary>
100         /// Gets or sets the date.
101         /// </summary>
102         /// <since_tizen> 4 </since_tizen>
103         public string Date { get; set; }
104
105         /// <summary>
106         /// Gets or sets the copyright.
107         /// </summary>
108         /// <since_tizen> 4 </since_tizen>
109         public string Copyright { get; set; }
110
111         /// <summary>
112         /// Gets or sets the description.
113         /// </summary>
114         /// <since_tizen> 4 </since_tizen>
115         public string Description { get; set; }
116
117         /// <summary>
118         /// Gets or sets the track number.
119         /// </summary>
120         /// <since_tizen> 4 </since_tizen>
121         public string TrackNumber { get; set; }
122
123         /// <summary>
124         /// Gets or sets the path of the album art.
125         /// </summary>
126         /// <since_tizen> 4 </since_tizen>
127         public string AlbumArtPath { get; set; }
128
129         /// <summary>
130         /// Gets or sets the season information.
131         /// </summary>
132         /// <seealso cref="SeriesInformation"/>
133         /// <since_tizen> 6 </since_tizen>
134         public SeriesInformation Season
135         {
136             get => DecodeSeason(EncodedSeason);
137             set => EncodedSeason = EncodeSeason(value);
138         }
139
140         /// <summary>
141         /// Gets or sets the episode information.
142         /// </summary>
143         /// <seealso cref="SeriesInformation"/>
144         /// <since_tizen> 6 </since_tizen>
145         public SeriesInformation Episode
146         {
147             get => DecodeEpisode(EncodedEpisode);
148             set => EncodedEpisode = EncodeEpisode(value);
149         }
150
151         /// <summary>
152         /// Gets or sets the content resolution.
153         /// </summary>
154         /// <seealso cref="Size"/>
155         /// <since_tizen> 6 </since_tizen>
156         public Size Resolution
157         {
158             get => DecodeResolution(EncodedResolution);
159             set => EncodedResolution = EncodeResolution(value.Width, value.Height);
160         }
161
162         // Developers who use Tizen Native API must encode strings to set or get metadata of media
163         // such as season, episode, and resolution. It is inconvenient.
164         // TizenFX supports for using normal strings and using encoded strings internally.
165         internal string EncodedSeason { get; private set; }
166
167         internal string EncodedEpisode { get; private set; }
168
169         internal string EncodedResolution { get; private set; }
170
171         private static string EncodeSeason(SeriesInformation information)
172         {
173             Native.EncodeSeason(information.Number, information.Title, out string encodedSeason).
174                 ThrowIfError("Failed to encode season");
175
176             return encodedSeason;
177         }
178
179         private static string EncodeEpisode(SeriesInformation information)
180         {
181             Native.EncodeEpisode(information.Number, information.Title, out string encodedEpisode).
182                 ThrowIfError("Failed to encode episode");
183
184             return encodedEpisode;
185         }
186
187         private static string EncodeResolution(int width, int height)
188         {
189             Native.EncodeResolution((uint)width, (uint)height, out string encodedResolution).
190                 ThrowIfError("Failed to encode resolution");
191
192             return encodedResolution;
193         }
194
195         private static SeriesInformation DecodeSeason(string encodedSeason)
196         {
197             int number = 0;
198             string title = null;
199
200             if (encodedSeason != null)
201             {
202                 Native.DecodeSeason(encodedSeason, out number, out title).
203                     ThrowIfError("Failed to decode season");
204             }
205
206             return new SeriesInformation(number, title);
207         }
208
209         private static SeriesInformation DecodeEpisode(string encodedEpisode)
210         {
211             int number = 0;
212             string title = null;
213
214             if (encodedEpisode != null)
215             {
216                 Native.DecodeEpisode(encodedEpisode, out number, out title).
217                     ThrowIfError("Failed to decode episode");
218             }
219
220             return new SeriesInformation(number, title);
221         }
222
223         private static Size DecodeResolution(string encodedResolution)
224         {
225             uint width = 0, height = 0;
226
227             if (encodedResolution != null)
228             {
229                 Native.DecodeResolution(encodedResolution, out width, out height).
230                     ThrowIfError("Failed to decode resolution");
231             }
232
233             return new Size((int)width, (int)height);
234         }
235     }
236
237     /// <summary>
238     /// Represents properties for the video series information.
239     /// </summary>
240     /// <since_tizen> 6 </since_tizen>
241     public class SeriesInformation
242     {
243         /// <summary>
244         /// Initializes a new instance of the <see cref="SeriesInformation"/> class.
245         /// </summary>
246         /// <param name="number">The order of this video in entire series.</param>
247         /// <param name="title">The title.</param>
248         /// <since_tizen> 6 </since_tizen>
249         public SeriesInformation(int number, string title)
250         {
251             Number = number;
252             Title = title;
253         }
254
255         /// <summary>
256         /// Gets or sets the order of this video in entire series.
257         /// </summary>
258         /// <since_tizen> 6 </since_tizen>
259         public int Number { get; }
260
261         /// <summary>
262         /// Gets or sets the title.
263         /// </summary>
264         /// <since_tizen> 6 </since_tizen>
265         public string Title { get; }
266
267         /// <summary>
268         /// Returns a string that represents the current object.
269         /// </summary>
270         /// <returns>A string that represents the current object.</returns>
271         /// <since_tizen> 6 </since_tizen>
272         public override string ToString() => $"Number={Number.ToString()}, Title={Title}";
273     }
274 }