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 audio stream.
25 public struct AudioStreamProperties
28 /// Initialize 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>
70 public override string ToString() =>
71 $"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
75 /// Represents properties for video stream.
77 public struct VideoStreamProperties
80 /// Initialize a new instance of the VideoStreamProperties struct with the specified fps, bit rate and size.
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)
90 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
91 ", width : " + size.Width + ", height : " + size.Height);
94 /// Initialize a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width and height.
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)
104 Size = new Size(width, height);
105 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
106 ", width : " + width + ", height : " + height);
110 /// Gets or sets the fps.
118 /// Gets or sets the bit rate.
127 /// Gets or sets the size.
135 public override string ToString()
137 return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
142 /// Provides a means to retrieve stream information.
144 public class StreamInfo
146 internal StreamInfo(Player owner)
152 /// Retrieves the album art of the stream or null if there is no album art data.
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()
160 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
162 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
163 ThrowIfFailed("Failed to get the album art");
165 if (art == IntPtr.Zero || size == 0)
167 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
171 byte[] albumArt = new byte[size];
172 Marshal.Copy(art, albumArt, 0, size);
176 private string GetCodecInfo(bool audioInfo)
178 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
180 IntPtr audioPtr = IntPtr.Zero;
181 IntPtr videoPtr = IntPtr.Zero;
184 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
185 ThrowIfFailed("Failed to get codec info");
189 Log.Debug(PlayerLog.Tag, "it is audio case");
190 return Marshal.PtrToStringAnsi(audioPtr);
194 Log.Debug(PlayerLog.Tag, "it is video case");
195 return Marshal.PtrToStringAnsi(videoPtr);
200 LibcSupport.Free(audioPtr);
201 LibcSupport.Free(videoPtr);
206 /// Retrieves the codec name of audio or null if there is no audio.
208 /// <returns>A string that represents codec name.</returns>
209 public string GetAudioCodec()
211 return GetCodecInfo(true);
215 /// Retrieves the codec name of video or null if there is no video.
217 /// <returns>A string that represents codec name.</returns>
218 public string GetVideoCodec()
220 return GetCodecInfo(false);
224 /// Gets the duration.
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()
232 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
235 NativePlayer.GetDuration(Player.Handle, out duration).
236 ThrowIfFailed("Failed to get the duration");
238 Log.Info(PlayerLog.Tag, "get duration : " + duration);
243 /// Gets the properties of audio.
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()
251 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
257 NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
258 ThrowIfFailed("Failed to get audio stream info");
260 // TODO should we check value is zero and return null?
262 return new AudioStreamProperties(sampleRate, channels, bitRate);
266 /// Gets the properties of video.
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()
274 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
279 NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
280 ThrowIfFailed("Failed to get the video stream info");
282 // TODO should we check value is zero and return null?
284 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
287 private Size GetVideoSize()
289 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
294 NativePlayer.GetVideoSize(Player.Handle, out width, out height).
295 ThrowIfFailed("Failed to get the video size");
297 return new Size(width, height);
301 /// Gets the metadata with the specified key.
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)
310 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
312 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
314 IntPtr ptr = IntPtr.Zero;
318 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
319 ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
321 return Marshal.PtrToStringAnsi(ptr);
325 LibcSupport.Free(ptr);
330 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
332 public Player Player { get; }