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>
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 video stream.
81 public struct VideoStreamProperties
84 /// Initialize 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 /// Initialize 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>
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()
168 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
170 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
171 ThrowIfFailed("Failed to get the album art");
173 if (art == IntPtr.Zero || size == 0)
175 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
179 byte[] albumArt = new byte[size];
180 Marshal.Copy(art, albumArt, 0, size);
184 private string GetCodecInfo(bool audioInfo)
186 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
188 IntPtr audioPtr = IntPtr.Zero;
189 IntPtr videoPtr = IntPtr.Zero;
192 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
193 ThrowIfFailed("Failed to get codec info");
197 Log.Debug(PlayerLog.Tag, "it is audio case");
198 return Marshal.PtrToStringAnsi(audioPtr);
202 Log.Debug(PlayerLog.Tag, "it is video case");
203 return Marshal.PtrToStringAnsi(videoPtr);
208 LibcSupport.Free(audioPtr);
209 LibcSupport.Free(videoPtr);
214 /// Retrieves the codec name of audio or null if there is no audio.
216 /// <returns>A string that represents codec name.</returns>
217 public string GetAudioCodec()
219 return GetCodecInfo(true);
223 /// Retrieves the codec name of video or null if there is no video.
225 /// <returns>A string that represents codec name.</returns>
226 public string GetVideoCodec()
228 return GetCodecInfo(false);
232 /// Gets the duration.
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()
240 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
243 NativePlayer.GetDuration(Player.Handle, out duration).
244 ThrowIfFailed("Failed to get the duration");
246 Log.Info(PlayerLog.Tag, "get duration : " + duration);
251 /// Gets the properties of audio.
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()
259 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
265 NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
266 ThrowIfFailed("Failed to get audio stream info");
268 // TODO should we check value is zero and return null?
270 return new AudioStreamProperties(sampleRate, channels, bitRate);
274 /// Gets the properties of video.
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()
282 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
287 NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
288 ThrowIfFailed("Failed to get the video stream info");
290 // TODO should we check value is zero and return null?
292 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
295 private Size GetVideoSize()
297 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
302 NativePlayer.GetVideoSize(Player.Handle, out width, out height).
303 ThrowIfFailed("Failed to get the video size");
305 return new Size(width, height);
309 /// Gets the metadata with the specified key.
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)
318 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
320 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
322 IntPtr ptr = IntPtr.Zero;
326 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
327 ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
329 return Marshal.PtrToStringAnsi(ptr);
333 LibcSupport.Free(ptr);
338 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
340 public Player Player { get; }