a38079fa149a82fc7d77a7ed64d5bb63c04929cb
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.MediaPlayer / Player / StreamInfo.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 using System;
17 using System.Runtime.InteropServices;
18 using static Interop;
19
20 namespace Tizen.Multimedia
21 {
22     /// <summary>
23     /// Represents properties for the audio stream.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public struct AudioStreamProperties
27     {
28         /// <summary>
29         /// Initializes a new instance of the AudioStreamProperties struct with the specified sample rate, channels, and bit rate.
30         /// </summary>
31         /// <param name="sampleRate">The sample rate of the stream.</param>
32         /// <param name="channels">The number of channels of the stream.</param>
33         /// <param name="bitRate">The bit rate of the stream.</param>
34         /// <since_tizen> 3 </since_tizen>
35         public AudioStreamProperties(int sampleRate, int channels, int bitRate)
36         {
37             SampleRate = sampleRate;
38             Channels = channels;
39             BitRate = bitRate;
40
41             Log.Debug(PlayerLog.Tag, $"sampleRate={sampleRate}, channels={channels}, bitRate={bitRate}");
42         }
43
44         /// <summary>
45         /// Gets or sets the sample rate.
46         /// </summary>
47         /// <value>The audio sample rate(Hz).</value>
48         /// <since_tizen> 3 </since_tizen>
49         public int SampleRate
50         {
51             get;
52             set;
53         }
54
55         /// <summary>
56         /// Gets or sets the channels.
57         /// </summary>
58         /// <since_tizen> 3 </since_tizen>
59         public int Channels
60         {
61             get;
62             set;
63         }
64
65         /// <summary>
66         /// Gets or sets the bit rate.
67         /// </summary>
68         /// <value>The audio bit rate(Hz).</value>
69         /// <since_tizen> 3 </since_tizen>
70         public int BitRate
71         {
72             get;
73             set;
74         }
75
76         /// <summary>
77         /// Returns a string that represents the current object.
78         /// </summary>
79         /// <returns>A string that represents the current object.</returns>
80         /// <since_tizen> 3 </since_tizen>
81         public override string ToString() =>
82             $"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
83     }
84
85     /// <summary>
86     /// Represents properties for the video stream.
87     /// </summary>
88     /// <since_tizen> 3 </since_tizen>
89     public struct VideoStreamProperties
90     {
91         /// <summary>
92         /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
93         /// </summary>
94         /// <param name="fps">The fps of the stream.</param>
95         /// <param name="bitRate">The bit rate of the stream.</param>
96         /// <param name="size">The size of the stream.</param>
97         /// <since_tizen> 3 </since_tizen>
98         public VideoStreamProperties(int fps, int bitRate, Size size)
99         {
100             Fps = fps;
101             BitRate = bitRate;
102             Size = size;
103             Log.Debug(PlayerLog.Tag, $"fps={fps}, bitrate={bitRate}, size=({size})");
104         }
105         /// <summary>
106         /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width, and height.
107         /// </summary>
108         /// <param name="fps">The fps of the stream.</param>
109         /// <param name="bitRate">The bit rate of the stream.</param>
110         /// <param name="width">The width of the stream.</param>
111         /// <param name="height">The height of the stream.</param>
112         /// <since_tizen> 3 </since_tizen>
113         public VideoStreamProperties(int fps, int bitRate, int width, int height)
114             : this(fps, bitRate, new Size(width, height))
115         {
116         }
117
118         /// <summary>
119         /// Gets or sets the fps.
120         /// </summary>
121         /// <since_tizen> 3 </since_tizen>
122         public int Fps
123         {
124             get;
125             set;
126         }
127         /// <summary>
128         /// Gets or sets the bit rate.
129         /// </summary>
130         /// <since_tizen> 3 </since_tizen>
131         public int BitRate
132         {
133             get;
134             set;
135         }
136
137         /// <summary>
138         /// Gets or sets the size.
139         /// </summary>
140         /// <since_tizen> 3 </since_tizen>
141         public Size Size
142         {
143             get;
144             set;
145         }
146
147         /// <summary>
148         /// Returns a string that represents the current object.
149         /// </summary>
150         /// <returns>A string that represents the current object.</returns>
151         /// <since_tizen> 3 </since_tizen>
152         public override string ToString()
153         {
154             return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
155         }
156     }
157
158     /// <summary>
159     /// Provides a means to retrieve stream information.
160     /// </summary>
161     /// <since_tizen> 3 </since_tizen>
162     public class StreamInfo
163     {
164         internal StreamInfo(Player owner)
165         {
166             Player = owner;
167         }
168
169         /// <summary>
170         /// Retrieves the album art of the stream, or null if there is no album art data.
171         /// </summary>
172         /// <returns>Raw byte array if album art exists; otherwise null.</returns>
173         /// <remarks>
174         /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
175         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
176         /// </remarks>
177         /// <exception cref="ObjectDisposedException">
178         /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
179         /// </exception>
180         /// <exception cref="InvalidOperationException">
181         /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
182         /// </exception>
183         /// <since_tizen> 3 </since_tizen>
184         public byte[] GetAlbumArt()
185         {
186             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
187
188             NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
189                 ThrowIfFailed(Player, "Failed to get the album art");
190
191             if (art == IntPtr.Zero || size == 0)
192             {
193                 return null;
194             }
195
196             byte[] albumArt = new byte[size];
197             Marshal.Copy(art, albumArt, 0, size);
198             return albumArt;
199         }
200
201         private string GetCodecInfo(bool audioInfo)
202         {
203             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
204
205             IntPtr audioPtr = IntPtr.Zero;
206             IntPtr videoPtr = IntPtr.Zero;
207             try
208             {
209                 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
210                     ThrowIfFailed(Player, "Failed to get codec info");
211
212                 if (audioInfo)
213                 {
214                     Log.Debug(PlayerLog.Tag, "it is audio case");
215                     return Marshal.PtrToStringAnsi(audioPtr);
216                 }
217                 else
218                 {
219                     Log.Debug(PlayerLog.Tag, "it is video case");
220                     return Marshal.PtrToStringAnsi(videoPtr);
221                 }
222             }
223             finally
224             {
225                 LibcSupport.Free(audioPtr);
226                 LibcSupport.Free(videoPtr);
227             }
228         }
229
230         /// <summary>
231         /// Retrieves the codec name of the audio or null if there is no audio.
232         /// </summary>
233         /// <returns>A string that represents the codec name.</returns>
234         /// <since_tizen> 3 </since_tizen>
235         public string GetAudioCodec()
236         {
237             return GetCodecInfo(true);
238         }
239
240         /// <summary>
241         /// Retrieves the codec name of the video or null if there is no video.
242         /// </summary>
243         /// <returns>A string that represents the codec name.</returns>
244         /// <since_tizen> 3 </since_tizen>
245         public string GetVideoCodec()
246         {
247             return GetCodecInfo(false);
248         }
249
250         /// <summary>
251         /// Gets the duration.
252         /// </summary>
253         /// <returns>The duration of the stream.</returns>
254         /// <remarks>
255         /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
256         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
257         /// </remarks>
258         /// <exception cref="ObjectDisposedException">
259         /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
260         /// </exception>
261         /// <exception cref="InvalidOperationException">
262         /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
263         /// </exception>
264         /// <since_tizen> 3 </since_tizen>
265         public int GetDuration()
266         {
267             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
268
269             NativePlayer.GetDuration(Player.Handle, out var duration).
270                 ThrowIfFailed(Player, "Failed to get the duration");
271
272             Log.Info(PlayerLog.Tag, "get duration : " + duration);
273             return duration;
274         }
275
276         /// <summary>
277         /// Gets the properties of the audio.
278         /// </summary>
279         /// <returns>A <see cref="AudioStreamProperties"/> that contains the audio stream information.</returns>
280         /// <remarks>
281         /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
282         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
283         /// </remarks>
284         /// <exception cref="ObjectDisposedException">
285         /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
286         /// </exception>
287         /// <exception cref="InvalidOperationException">
288         /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
289         /// </exception>
290         /// <since_tizen> 3 </since_tizen>
291         public AudioStreamProperties GetAudioProperties()
292         {
293             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
294
295             NativePlayer.GetAudioStreamInfo(Player.Handle, out var sampleRate,
296                 out var channels, out var bitRate).
297                 ThrowIfFailed(Player, "Failed to get audio stream info");
298
299             return new AudioStreamProperties(sampleRate, channels, bitRate);
300         }
301
302         /// <summary>
303         /// Gets the properties of the video.
304         /// </summary>
305         /// <returns>A <see cref="VideoStreamProperties"/> that contains the video stream information.</returns>
306         /// <remarks>
307         /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
308         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
309         /// </remarks>
310         /// <exception cref="ObjectDisposedException">
311         /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
312         /// </exception>
313         /// <exception cref="InvalidOperationException">
314         /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
315         /// </exception>
316         /// <since_tizen> 3 </since_tizen>
317         public VideoStreamProperties GetVideoProperties()
318         {
319             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
320
321             NativePlayer.GetVideoStreamInfo(Player.Handle, out var fps, out var bitRate).
322                 ThrowIfFailed(Player, "Failed to get the video stream info");
323
324             return new VideoStreamProperties(fps, bitRate, GetVideoSize());
325         }
326
327         private Size GetVideoSize()
328         {
329             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
330
331             NativePlayer.GetVideoSize(Player.Handle, out var width, out var height).
332                 ThrowIfFailed(Player, "Failed to get the video size");
333
334             return new Size(width, height);
335         }
336
337         /// <summary>
338         /// Gets the metadata with the specified key.
339         /// </summary>
340         /// <returns>A string that represents the value of the specified key.</returns>
341         /// <param name="key">The key to query.</param>
342         /// <remarks>
343         /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
344         /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</remarks>
345         /// <exception cref="ObjectDisposedException">
346         /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
347         /// </exception>
348         /// <exception cref="InvalidOperationException">
349         /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
350         /// </exception>
351         /// <since_tizen> 3 </since_tizen>
352         public string GetMetadata(StreamMetadataKey key)
353         {
354             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
355
356             ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
357
358             IntPtr ptr = IntPtr.Zero;
359
360             try
361             {
362                 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
363                     ThrowIfFailed(Player, $"Failed to get the meta data with the key '{ key }'");
364
365                 return Marshal.PtrToStringAnsi(ptr);
366             }
367             finally
368             {
369                 LibcSupport.Free(ptr);
370             }
371         }
372
373         /// <summary>
374         /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
375         /// </summary>
376         /// <since_tizen> 3 </since_tizen>
377         public Player Player { get; }
378     }
379 }