2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using System.Runtime.InteropServices;
20 namespace Tizen.Multimedia
23 /// Represents properties for the audio stream.
25 public struct AudioStreamProperties
28 /// Initializes a new instance of the AudioStreamProperties struct with the specified sample rate, channels, and bit rate.
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)
35 SampleRate = sampleRate;
38 Log.Debug(PlayerLog.Tag, "sampleRate : " + sampleRate + ", channels : " + channels + ", bitRate : " + bitRate);
42 /// Gets or sets the sample rate.
44 /// <value>The audio sample rate(Hz).</value>
52 /// Gets or sets the channels.
61 /// Gets or sets the bit rate.
63 /// <value>The audio bit rate(Hz).</value>
71 /// Returns a string that represents the current object.
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() }";
79 /// Represents properties for the video stream.
81 public struct VideoStreamProperties
84 /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
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)
94 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
95 ", width : " + size.Width + ", height : " + size.Height);
98 /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width, and height.
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)
108 Size = new Size(width, height);
109 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
110 ", width : " + width + ", height : " + height);
114 /// Gets or sets the fps.
122 /// Gets or sets the bit rate.
131 /// Gets or sets the size.
140 /// Returns a string that represents the current object.
142 /// <returns>A string that represents the current object.</returns>
143 public override string ToString()
145 return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
150 /// Provides a means to retrieve stream information.
152 public class StreamInfo
154 internal StreamInfo(Player owner)
160 /// Retrieves the album art of the stream, or null if there is no album art data.
162 /// <returns>Raw byte array if album art exists; otherwise null.</returns>
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.
167 /// <exception cref="ObjectDisposedException">
168 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
170 /// <exception cref="InvalidOperationException">
171 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
173 public byte[] GetAlbumArt()
175 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
177 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
178 ThrowIfFailed("Failed to get the album art");
180 if (art == IntPtr.Zero || size == 0)
182 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
186 byte[] albumArt = new byte[size];
187 Marshal.Copy(art, albumArt, 0, size);
191 private string GetCodecInfo(bool audioInfo)
193 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
195 IntPtr audioPtr = IntPtr.Zero;
196 IntPtr videoPtr = IntPtr.Zero;
199 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
200 ThrowIfFailed("Failed to get codec info");
204 Log.Debug(PlayerLog.Tag, "it is audio case");
205 return Marshal.PtrToStringAnsi(audioPtr);
209 Log.Debug(PlayerLog.Tag, "it is video case");
210 return Marshal.PtrToStringAnsi(videoPtr);
215 LibcSupport.Free(audioPtr);
216 LibcSupport.Free(videoPtr);
221 /// Retrieves the codec name of the audio or null if there is no audio.
223 /// <returns>A string that represents the codec name.</returns>
224 public string GetAudioCodec()
226 return GetCodecInfo(true);
230 /// Retrieves the codec name of the video or null if there is no video.
232 /// <returns>A string that represents the codec name.</returns>
233 public string GetVideoCodec()
235 return GetCodecInfo(false);
239 /// Gets the duration.
241 /// <returns>The duration of the stream.</returns>
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.
246 /// <exception cref="ObjectDisposedException">
247 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
249 /// <exception cref="InvalidOperationException">
250 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
252 public int GetDuration()
254 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
257 NativePlayer.GetDuration(Player.Handle, out duration).
258 ThrowIfFailed("Failed to get the duration");
260 Log.Info(PlayerLog.Tag, "get duration : " + duration);
265 /// Gets the properties of the audio.
267 /// <returns>A <see cref="AudioStreamProperties"/> that contains the audio stream information.</returns>
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.
272 /// <exception cref="ObjectDisposedException">
273 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
275 /// <exception cref="InvalidOperationException">
276 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
278 public AudioStreamProperties GetAudioProperties()
280 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
286 NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
287 ThrowIfFailed("Failed to get audio stream info");
289 return new AudioStreamProperties(sampleRate, channels, bitRate);
293 /// Gets the properties of the video.
295 /// <returns>A <see cref="VideoStreamProperties"/> that contains the video stream information.</returns>
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.
300 /// <exception cref="ObjectDisposedException">
301 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
303 /// <exception cref="InvalidOperationException">
304 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
306 public VideoStreamProperties GetVideoProperties()
308 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
313 NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
314 ThrowIfFailed("Failed to get the video stream info");
316 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
319 private Size GetVideoSize()
321 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
326 NativePlayer.GetVideoSize(Player.Handle, out width, out height).
327 ThrowIfFailed("Failed to get the video size");
329 return new Size(width, height);
333 /// Gets the metadata with the specified key.
335 /// <returns>A string that represents the value of the specified key.</returns>
336 /// <param name="key">The key to query.</param>
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.
343 /// <exception cref="InvalidOperationException">
344 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
346 public string GetMetadata(StreamMetadataKey key)
348 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
350 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
352 IntPtr ptr = IntPtr.Zero;
356 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
357 ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
359 return Marshal.PtrToStringAnsi(ptr);
363 LibcSupport.Free(ptr);
368 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
370 public Player Player { get; }