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