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