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 public AudioStreamProperties(int sampleRate, int channels, int bitRate)
32 SampleRate = sampleRate;
35 Log.Debug(PlayerLog.Tag, "sampleRate : " + sampleRate + ", channels : " + channels + ", bitRate : " + bitRate);
39 /// Gets or sets the sample rate.
41 /// <value>The audio sample rate(Hz).</value>
49 /// Gets or sets the channels.
58 /// Gets or sets the bit rate.
60 /// <value>The audio bit rate(Hz).</value>
67 public override string ToString() =>
68 $"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
72 /// Represents properties for video stream.
74 public struct VideoStreamProperties
77 /// Initialize a new instance of the VideoStreamProperties struct with the specified fps, bit rate and size.
79 public VideoStreamProperties(int fps, int bitRate, Size size)
84 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
85 ", width : " + size.Width + ", height : " + size.Height);
88 /// Initialize a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width and height.
90 public VideoStreamProperties(int fps, int bitRate, int width, int height)
94 Size = new Size(width, height);
95 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
96 ", width : " + width + ", height : " + height);
100 /// Gets or sets the fps.
108 /// Gets or sets the bit rate.
117 /// Gets or sets the size.
125 public override string ToString()
127 return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
132 /// Provides means to retrieve stream information.
134 public class StreamInfo
136 internal StreamInfo(Player owner)
142 /// Retrieves the album art of the stream or null if there is no album art data.
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()
149 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
151 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
152 ThrowIfFailed("Failed to get the album art");
154 if (art == IntPtr.Zero || size == 0)
156 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
160 byte[] albumArt = new byte[size];
161 Marshal.Copy(art, albumArt, 0, size);
165 private string GetCodecInfo(bool audioInfo)
167 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
169 IntPtr audioPtr = IntPtr.Zero;
170 IntPtr videoPtr = IntPtr.Zero;
173 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
174 ThrowIfFailed("Failed to get codec info");
178 Log.Debug(PlayerLog.Tag, "it is audio case");
179 return Marshal.PtrToStringAnsi(audioPtr);
183 Log.Debug(PlayerLog.Tag, "it is video case");
184 return Marshal.PtrToStringAnsi(videoPtr);
189 LibcSupport.Free(audioPtr);
190 LibcSupport.Free(videoPtr);
195 /// Retrieves the codec name of audio or null if there is no audio.
197 public string GetAudioCodec()
199 return GetCodecInfo(true);
203 /// Retrieves the codec name of video or null if there is no video.
205 public string GetVideoCodec()
207 return GetCodecInfo(false);
211 /// Gets the duration.
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()
218 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
221 NativePlayer.GetDuration(Player.Handle, out duration).
222 ThrowIfFailed("Failed to get the duration");
224 Log.Info(PlayerLog.Tag, "get duration : " + duration);
229 /// Gets the properties of audio.
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 GetAudioProperties()
236 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
242 NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
243 ThrowIfFailed("Failed to get audio stream info");
245 // TODO should we check value is zero and return null?
247 return new AudioStreamProperties(sampleRate, channels, bitRate);
251 /// Gets the properties of video.
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()
258 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
263 NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
264 ThrowIfFailed("Failed to get the video stream info");
266 // TODO should we check value is zero and return null?
268 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
271 private Size GetVideoSize()
273 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
278 NativePlayer.GetVideoSize(Player.Handle, out width, out height).
279 ThrowIfFailed("Failed to get the video size");
281 return new Size(width, height);
285 /// Gets the metadata with the specified key.
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)
293 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
295 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
297 IntPtr ptr = IntPtr.Zero;
301 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
302 ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
304 return Marshal.PtrToStringAnsi(ptr);
308 LibcSupport.Free(ptr);
313 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
315 public Player Player { get; }