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 /// <since_tizen> 3 </since_tizen>
26 public struct AudioStreamProperties
29 /// Initializes a new instance of the AudioStreamProperties struct with the specified sample rate, channels, and bit rate.
31 /// <param name="sampleRate">The sample rate of the stream.</param>
32 /// <param name="channels">The number of channels of the stream.</param>
33 /// <param name="bitRate">The bit rate of the stream.</param>
34 /// <since_tizen> 3 </since_tizen>
35 public AudioStreamProperties(int sampleRate, int channels, int bitRate)
37 SampleRate = sampleRate;
40 Log.Debug(PlayerLog.Tag, "sampleRate : " + sampleRate + ", channels : " + channels + ", bitRate : " + bitRate);
44 /// Gets or sets the sample rate.
46 /// <value>The audio sample rate(Hz).</value>
47 /// <since_tizen> 3 </since_tizen>
55 /// Gets or sets the channels.
57 /// <since_tizen> 3 </since_tizen>
65 /// Gets or sets the bit rate.
67 /// <value>The audio bit rate(Hz).</value>
68 /// <since_tizen> 3 </since_tizen>
76 /// Returns a string that represents the current object.
78 /// <returns>A string that represents the current object.</returns>
79 /// <since_tizen> 3 </since_tizen>
80 public override string ToString() =>
81 $"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
85 /// Represents properties for the video stream.
87 /// <since_tizen> 3 </since_tizen>
88 public struct VideoStreamProperties
91 /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
93 /// <param name="fps">The fps of the stream.</param>
94 /// <param name="bitRate">The bit rate of the stream.</param>
95 /// <param name="size">The size of the stream.</param>
96 /// <since_tizen> 3 </since_tizen>
97 public VideoStreamProperties(int fps, int bitRate, Size size)
102 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
103 ", width : " + size.Width + ", height : " + size.Height);
106 /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width, and height.
108 /// <param name="fps">The fps of the stream.</param>
109 /// <param name="bitRate">The bit rate of the stream.</param>
110 /// <param name="width">The width of the stream.</param>
111 /// <param name="height">The height of the stream.</param>
112 /// <since_tizen> 3 </since_tizen>
113 public VideoStreamProperties(int fps, int bitRate, int width, int height)
117 Size = new Size(width, height);
118 Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
119 ", width : " + width + ", height : " + height);
123 /// Gets or sets the fps.
125 /// <since_tizen> 3 </since_tizen>
132 /// Gets or sets the bit rate.
134 /// <since_tizen> 3 </since_tizen>
142 /// Gets or sets the size.
144 /// <since_tizen> 3 </since_tizen>
152 /// Returns a string that represents the current object.
154 /// <returns>A string that represents the current object.</returns>
155 /// <since_tizen> 3 </since_tizen>
156 public override string ToString()
158 return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
163 /// Provides a means to retrieve stream information.
165 /// <since_tizen> 3 </since_tizen>
166 public class StreamInfo
168 internal StreamInfo(Player owner)
174 /// Retrieves the album art of the stream, or null if there is no album art data.
176 /// <returns>Raw byte array if album art exists; otherwise null.</returns>
178 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
179 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
181 /// <exception cref="ObjectDisposedException">
182 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
184 /// <exception cref="InvalidOperationException">
185 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
187 /// <since_tizen> 3 </since_tizen>
188 public byte[] GetAlbumArt()
190 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
192 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
193 ThrowIfFailed("Failed to get the album art");
195 if (art == IntPtr.Zero || size == 0)
197 Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
201 byte[] albumArt = new byte[size];
202 Marshal.Copy(art, albumArt, 0, size);
206 private string GetCodecInfo(bool audioInfo)
208 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
210 IntPtr audioPtr = IntPtr.Zero;
211 IntPtr videoPtr = IntPtr.Zero;
214 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
215 ThrowIfFailed("Failed to get codec info");
219 Log.Debug(PlayerLog.Tag, "it is audio case");
220 return Marshal.PtrToStringAnsi(audioPtr);
224 Log.Debug(PlayerLog.Tag, "it is video case");
225 return Marshal.PtrToStringAnsi(videoPtr);
230 LibcSupport.Free(audioPtr);
231 LibcSupport.Free(videoPtr);
236 /// Retrieves the codec name of the audio or null if there is no audio.
238 /// <returns>A string that represents the codec name.</returns>
239 /// <since_tizen> 3 </since_tizen>
240 public string GetAudioCodec()
242 return GetCodecInfo(true);
246 /// Retrieves the codec name of the video or null if there is no video.
248 /// <returns>A string that represents the codec name.</returns>
249 /// <since_tizen> 3 </since_tizen>
250 public string GetVideoCodec()
252 return GetCodecInfo(false);
256 /// Gets the duration.
258 /// <returns>The duration of the stream.</returns>
260 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
261 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
263 /// <exception cref="ObjectDisposedException">
264 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
266 /// <exception cref="InvalidOperationException">
267 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
269 /// <since_tizen> 3 </since_tizen>
270 public int GetDuration()
272 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
275 NativePlayer.GetDuration(Player.Handle, out duration).
276 ThrowIfFailed("Failed to get the duration");
278 Log.Info(PlayerLog.Tag, "get duration : " + duration);
283 /// Gets the properties of the audio.
285 /// <returns>A <see cref="AudioStreamProperties"/> that contains the audio stream information.</returns>
287 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
288 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
290 /// <exception cref="ObjectDisposedException">
291 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
293 /// <exception cref="InvalidOperationException">
294 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
296 /// <since_tizen> 3 </since_tizen>
297 public AudioStreamProperties GetAudioProperties()
299 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
305 NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
306 ThrowIfFailed("Failed to get audio stream info");
308 return new AudioStreamProperties(sampleRate, channels, bitRate);
312 /// Gets the properties of the video.
314 /// <returns>A <see cref="VideoStreamProperties"/> that contains the video stream information.</returns>
316 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
317 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
319 /// <exception cref="ObjectDisposedException">
320 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
322 /// <exception cref="InvalidOperationException">
323 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
325 /// <since_tizen> 3 </since_tizen>
326 public VideoStreamProperties GetVideoProperties()
328 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
333 NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
334 ThrowIfFailed("Failed to get the video stream info");
336 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
339 private Size GetVideoSize()
341 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
346 NativePlayer.GetVideoSize(Player.Handle, out width, out height).
347 ThrowIfFailed("Failed to get the video size");
349 return new Size(width, height);
353 /// Gets the metadata with the specified key.
355 /// <returns>A string that represents the value of the specified key.</returns>
356 /// <param name="key">The key to query.</param>
358 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
359 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</remarks>
360 /// <exception cref="ObjectDisposedException">
361 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
363 /// <exception cref="InvalidOperationException">
364 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
366 /// <since_tizen> 3 </since_tizen>
367 public string GetMetadata(StreamMetadataKey key)
369 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
371 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
373 IntPtr ptr = IntPtr.Zero;
377 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
378 ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
380 return Marshal.PtrToStringAnsi(ptr);
384 LibcSupport.Free(ptr);
389 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
391 /// <since_tizen> 3 </since_tizen>
392 public Player Player { get; }