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