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