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;
41 Log.Debug(PlayerLog.Tag, $"sampleRate={sampleRate}, channels={channels}, bitRate={bitRate}");
45 /// Gets or sets the sample rate.
47 /// <value>The audio sample rate(Hz).</value>
48 /// <since_tizen> 3 </since_tizen>
56 /// Gets or sets the channels.
58 /// <since_tizen> 3 </since_tizen>
66 /// Gets or sets the bit rate.
68 /// <value>The audio bit rate(Hz).</value>
69 /// <since_tizen> 3 </since_tizen>
77 /// Returns a string that represents the current object.
79 /// <returns>A string that represents the current object.</returns>
80 /// <since_tizen> 3 </since_tizen>
81 public override string ToString() =>
82 $"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
86 /// Represents properties for the video stream.
88 /// <since_tizen> 3 </since_tizen>
89 public struct VideoStreamProperties
92 /// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
94 /// <param name="fps">The fps of the stream.</param>
95 /// <param name="bitRate">The bit rate of the stream.</param>
96 /// <param name="size">The size of the stream.</param>
97 /// <since_tizen> 3 </since_tizen>
98 public VideoStreamProperties(int fps, int bitRate, Size size)
103 Log.Debug(PlayerLog.Tag, $"fps={fps}, bitrate={bitRate}, size=({size})");
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)
114 : this(fps, bitRate, new Size(width, height))
119 /// Gets or sets the fps.
121 /// <since_tizen> 3 </since_tizen>
128 /// Gets or sets the bit rate.
130 /// <since_tizen> 3 </since_tizen>
138 /// Gets or sets the size.
140 /// <since_tizen> 3 </since_tizen>
148 /// Returns a string that represents the current object.
150 /// <returns>A string that represents the current object.</returns>
151 /// <since_tizen> 3 </since_tizen>
152 public override string ToString()
154 return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
159 /// Provides a means to retrieve stream information.
161 /// <since_tizen> 3 </since_tizen>
162 public class StreamInfo
164 internal StreamInfo(Player owner)
170 /// Retrieves the album art of the stream, or null if there is no album art data.
172 /// <returns>Raw byte array if album art exists; otherwise null.</returns>
174 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
175 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
177 /// <exception cref="ObjectDisposedException">
178 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
180 /// <exception cref="InvalidOperationException">
181 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
183 /// <since_tizen> 3 </since_tizen>
184 public byte[] GetAlbumArt()
186 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
188 NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
189 ThrowIfFailed(Player, "Failed to get the album art");
191 if (art == IntPtr.Zero || size == 0)
196 byte[] albumArt = new byte[size];
197 Marshal.Copy(art, albumArt, 0, size);
201 private string GetCodecInfo(bool audioInfo)
203 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
205 IntPtr audioPtr = IntPtr.Zero;
206 IntPtr videoPtr = IntPtr.Zero;
209 NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
210 ThrowIfFailed(Player, "Failed to get codec info");
214 Log.Debug(PlayerLog.Tag, "it is audio case");
215 return Marshal.PtrToStringAnsi(audioPtr);
219 Log.Debug(PlayerLog.Tag, "it is video case");
220 return Marshal.PtrToStringAnsi(videoPtr);
225 Marshal.FreeHGlobal(audioPtr);
226 Marshal.FreeHGlobal(videoPtr);
231 /// Retrieves the codec name of the audio or null if there is no audio.
233 /// <returns>A string that represents the codec name.</returns>
234 /// <since_tizen> 3 </since_tizen>
235 public string GetAudioCodec()
237 return GetCodecInfo(true);
241 /// Retrieves the codec name of the video or null if there is no video.
243 /// <returns>A string that represents the codec name.</returns>
244 /// <since_tizen> 3 </since_tizen>
245 public string GetVideoCodec()
247 return GetCodecInfo(false);
251 /// Gets the duration.
253 /// <returns>The duration of the stream.</returns>
255 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
256 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
258 /// <exception cref="ObjectDisposedException">
259 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
261 /// <exception cref="InvalidOperationException">
262 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
264 /// <seealso cref="GetDurationNanoseconds"/>
265 /// <since_tizen> 3 </since_tizen>
266 public int GetDuration()
268 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
270 NativePlayer.GetDuration(Player.Handle, out var duration).
271 ThrowIfFailed(Player, "Failed to get the duration");
273 Log.Info(PlayerLog.Tag, "get duration : " + duration);
278 /// Gets the duration in nanoseconds.
280 /// <returns>The duration of the stream.</returns>
282 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
283 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
285 /// <exception cref="ObjectDisposedException">
286 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
288 /// <exception cref="InvalidOperationException">
289 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
291 /// <seealso cref="GetDuration"/>
292 /// <since_tizen> 5 </since_tizen>
293 public long GetDurationNanoseconds()
295 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
297 NativePlayer.GetDurationNanoseconds(Player.Handle, out var duration).
298 ThrowIfFailed(Player, "Failed to get the duration in nanoseconds");
300 Log.Info(PlayerLog.Tag, "get duration(nsec) : " + duration);
305 /// Gets the properties of the audio.
307 /// <returns>A <see cref="AudioStreamProperties"/> that contains the audio stream information.</returns>
309 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
310 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
312 /// <exception cref="ObjectDisposedException">
313 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
315 /// <exception cref="InvalidOperationException">
316 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
318 /// <since_tizen> 3 </since_tizen>
319 public AudioStreamProperties GetAudioProperties()
321 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
323 NativePlayer.GetAudioStreamInfo(Player.Handle, out var sampleRate,
324 out var channels, out var bitRate).
325 ThrowIfFailed(Player, "Failed to get audio stream info");
327 return new AudioStreamProperties(sampleRate, channels, bitRate);
331 /// Gets the properties of the video.
333 /// <returns>A <see cref="VideoStreamProperties"/> that contains the video stream information.</returns>
335 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
336 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.
338 /// <exception cref="ObjectDisposedException">
339 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
341 /// <exception cref="InvalidOperationException">
342 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
344 /// <since_tizen> 3 </since_tizen>
345 public VideoStreamProperties GetVideoProperties()
347 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
349 NativePlayer.GetVideoStreamInfo(Player.Handle, out var fps, out var bitRate).
350 ThrowIfFailed(Player, "Failed to get the video stream info");
352 return new VideoStreamProperties(fps, bitRate, GetVideoSize());
355 private Size GetVideoSize()
357 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
359 NativePlayer.GetVideoSize(Player.Handle, out var width, out var height).
360 ThrowIfFailed(Player, "Failed to get the video size");
362 return new Size(width, height);
366 /// Gets the metadata with the specified key.
368 /// <returns>A string that represents the value of the specified key.</returns>
369 /// <param name="key">The key to query.</param>
371 /// The <see cref="Multimedia.Player"/> that owns this instance must be in the <see cref="PlayerState.Ready"/>,
372 /// <see cref="PlayerState.Playing"/>, or <see cref="PlayerState.Paused"/> state.</remarks>
373 /// <exception cref="ObjectDisposedException">
374 /// The <see cref="Multimedia.Player"/> that this instance belongs to has been disposed of.
376 /// <exception cref="InvalidOperationException">
377 /// The <see cref="Multimedia.Player"/> that this instance belongs to is not in the valid state.
379 /// <since_tizen> 3 </since_tizen>
380 public string GetMetadata(StreamMetadataKey key)
382 Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
384 ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key, nameof(key));
386 IntPtr ptr = IntPtr.Zero;
390 NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
391 ThrowIfFailed(Player, $"Failed to get the meta data with the key '{ key }'");
393 return Marshal.PtrToStringAnsi(ptr);
397 Marshal.FreeHGlobal(ptr);
402 /// Gets the <see cref="Multimedia.Player"/> that owns this instance.
404 /// <since_tizen> 3 </since_tizen>
405 public Player Player { get; }