b31fb417cd8b7840c047bf3d6c801ce7e9a39f71
[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         /// <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 video stream.
80     /// </summary>
81     public struct VideoStreamProperties
82     {
83         /// <summary>
84         /// Initialize 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         /// Initialize 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>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>
164         /// <exception cref="ObjectDisposedException">The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed.</exception>
165         /// <exception cref="InvalidOperationException">The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.</exception>
166         public byte[] GetAlbumArt()
167         {
168             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
169
170             NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
171                 ThrowIfFailed("Failed to get the album art");
172
173             if (art == IntPtr.Zero || size == 0)
174             {
175                 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
176                 return null;
177             }
178
179             byte[] albumArt = new byte[size];
180             Marshal.Copy(art, albumArt, 0, size);
181             return albumArt;
182         }
183
184         private string GetCodecInfo(bool audioInfo)
185         {
186             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
187
188             IntPtr audioPtr = IntPtr.Zero;
189             IntPtr videoPtr = IntPtr.Zero;
190             try
191             {
192                 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
193                     ThrowIfFailed("Failed to get codec info");
194
195                 if (audioInfo)
196                 {
197                     Log.Debug(PlayerLog.Tag, "it is audio case");
198                     return Marshal.PtrToStringAnsi(audioPtr);
199                 }
200                 else
201                 {
202                     Log.Debug(PlayerLog.Tag, "it is video case");
203                     return Marshal.PtrToStringAnsi(videoPtr);
204                 }
205             }
206             finally
207             {
208                 LibcSupport.Free(audioPtr);
209                 LibcSupport.Free(videoPtr);
210             }
211         }
212
213         /// <summary>
214         /// Retrieves the codec name of audio or null if there is no audio.
215         /// </summary>
216         /// <returns>A string that represents codec name.</returns>
217         public string GetAudioCodec()
218         {
219             return GetCodecInfo(true);
220         }
221
222         /// <summary>
223         /// Retrieves the codec name of video or null if there is no video.
224         /// </summary>
225         /// <returns>A string that represents codec name.</returns>
226         public string GetVideoCodec()
227         {
228             return GetCodecInfo(false);
229         }
230
231         /// <summary>
232         /// Gets the duration.
233         /// </summary>
234         /// <returns>The duration of the stream.</returns>
235         /// <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>
236         /// <exception cref="ObjectDisposedException">The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed.</exception>
237         /// <exception cref="InvalidOperationException">The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.</exception>
238         public int GetDuration()
239         {
240             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
241
242             int duration = 0;
243             NativePlayer.GetDuration(Player.Handle, out duration).
244                 ThrowIfFailed("Failed to get the duration");
245
246             Log.Info(PlayerLog.Tag, "get duration : " + duration);
247             return duration;
248         }
249
250         /// <summary>
251         /// Gets the properties of audio.
252         /// </summary>
253         /// <returns>A <see cref="AudioStreamProperties"/> that contains audio stream information.</returns>
254         /// <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>
255         /// <exception cref="ObjectDisposedException">The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed.</exception>
256         /// <exception cref="InvalidOperationException">The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.</exception>
257         public AudioStreamProperties GetAudioProperties()
258         {
259             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
260
261             int sampleRate = 0;
262             int channels = 0;
263             int bitRate = 0;
264
265             NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
266                 ThrowIfFailed("Failed to get audio stream info");
267
268             // TODO should we check value is zero and return null?
269
270             return new AudioStreamProperties(sampleRate, channels, bitRate);
271         }
272
273         /// <summary>
274         /// Gets the properties of video.
275         /// </summary>
276         /// <returns>A <see cref="VideoStreamProperties"/> that contains video stream information.</returns>
277         /// <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>
278         /// <exception cref="ObjectDisposedException">The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed.</exception>
279         /// <exception cref="InvalidOperationException">The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.</exception>
280         public VideoStreamProperties GetVideoProperties()
281         {
282             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
283
284             int fps = 0;
285             int bitRate = 0;
286
287             NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
288                 ThrowIfFailed("Failed to get the video stream info");
289
290             // TODO should we check value is zero and return null?
291
292             return new VideoStreamProperties(fps, bitRate, GetVideoSize());
293         }
294
295         private Size GetVideoSize()
296         {
297             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
298
299             int height = 0;
300             int width = 0;
301
302             NativePlayer.GetVideoSize(Player.Handle, out width, out height).
303                 ThrowIfFailed("Failed to get the video size");
304
305             return new Size(width, height);
306         }
307
308         /// <summary>
309         /// Gets the metadata with the specified key.
310         /// </summary>
311         /// <returns>A string that represents the value of the specified key.</returns>
312         /// <param name="key">The key to query.</param>
313         /// <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>
314         /// <exception cref="ObjectDisposedException">The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed.</exception>
315         /// <exception cref="InvalidOperationException">The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.</exception>
316         public string GetMetadata(StreamMetadataKey key)
317         {
318             Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
319
320             ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
321
322             IntPtr ptr = IntPtr.Zero;
323
324             try
325             {
326                 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
327                     ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
328
329                 return Marshal.PtrToStringAnsi(ptr);
330             }
331             finally
332             {
333                 LibcSupport.Free(ptr);
334             }
335         }
336
337         /// <summary>
338         /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
339         /// </summary>
340         public Player Player { get; }
341     }
342 }