/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Runtime.InteropServices;
using static Interop;
namespace Tizen.Multimedia
{
///
/// Represents properties for the audio stream.
///
/// 3
public struct AudioStreamProperties
{
///
/// Initializes a new instance of the AudioStreamProperties struct with the specified sample rate, channels, and bit rate.
///
/// The sample rate of the stream.
/// The number of channels of the stream.
/// The bit rate of the stream.
/// 3
public AudioStreamProperties(int sampleRate, int channels, int bitRate)
{
SampleRate = sampleRate;
Channels = channels;
BitRate = bitRate;
Log.Debug(PlayerLog.Tag, "sampleRate : " + sampleRate + ", channels : " + channels + ", bitRate : " + bitRate);
}
///
/// Gets or sets the sample rate.
///
/// The audio sample rate(Hz).
/// 3
public int SampleRate
{
get;
set;
}
///
/// Gets or sets the channels.
///
/// 3
public int Channels
{
get;
set;
}
///
/// Gets or sets the bit rate.
///
/// The audio bit rate(Hz).
/// 3
public int BitRate
{
get;
set;
}
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
/// 3
public override string ToString() =>
$"SampleRate={ SampleRate.ToString() }, Channels={ Channels.ToString() }, BitRate={ BitRate.ToString() }";
}
///
/// Represents properties for the video stream.
///
/// 3
public struct VideoStreamProperties
{
///
/// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, and size.
///
/// The fps of the stream.
/// The bit rate of the stream.
/// The size of the stream.
/// 3
public VideoStreamProperties(int fps, int bitRate, Size size)
{
Fps = fps;
BitRate = bitRate;
Size = size;
Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
", width : " + size.Width + ", height : " + size.Height);
}
///
/// Initializes a new instance of the VideoStreamProperties struct with the specified fps, bit rate, width, and height.
///
/// The fps of the stream.
/// The bit rate of the stream.
/// The width of the stream.
/// The height of the stream.
/// 3
public VideoStreamProperties(int fps, int bitRate, int width, int height)
{
Fps = fps;
BitRate = bitRate;
Size = new Size(width, height);
Log.Debug(PlayerLog.Tag, "fps : " + fps + ", bitrate : " + bitRate +
", width : " + width + ", height : " + height);
}
///
/// Gets or sets the fps.
///
/// 3
public int Fps
{
get;
set;
}
///
/// Gets or sets the bit rate.
///
/// 3
public int BitRate
{
get;
set;
}
///
/// Gets or sets the size.
///
/// 3
public Size Size
{
get;
set;
}
///
/// Returns a string that represents the current object.
///
/// A string that represents the current object.
/// 3
public override string ToString()
{
return $"Fps={ Fps.ToString() }, BitRate={ BitRate.ToString() }, Size=[{ Size.ToString() }]";
}
}
///
/// Provides a means to retrieve stream information.
///
/// 3
public class StreamInfo
{
internal StreamInfo(Player owner)
{
Player = owner;
}
///
/// Retrieves the album art of the stream, or null if there is no album art data.
///
/// Raw byte array if album art exists; otherwise null.
///
/// The that owns this instance must be in the ,
/// , or state.
///
///
/// The that this instance belongs to has been disposed of.
///
///
/// The that this instance belongs to is not in the valid state.
///
/// 3
public byte[] GetAlbumArt()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
NativePlayer.GetAlbumArt(Player.Handle, out var art, out var size).
ThrowIfFailed("Failed to get the album art");
if (art == IntPtr.Zero || size == 0)
{
Log.Error(PlayerLog.Tag, "art is null or size is 0 : " + size);
return null;
}
byte[] albumArt = new byte[size];
Marshal.Copy(art, albumArt, 0, size);
return albumArt;
}
private string GetCodecInfo(bool audioInfo)
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
IntPtr audioPtr = IntPtr.Zero;
IntPtr videoPtr = IntPtr.Zero;
try
{
NativePlayer.GetCodecInfo(Player.Handle, out audioPtr, out videoPtr).
ThrowIfFailed("Failed to get codec info");
if (audioInfo)
{
Log.Debug(PlayerLog.Tag, "it is audio case");
return Marshal.PtrToStringAnsi(audioPtr);
}
else
{
Log.Debug(PlayerLog.Tag, "it is video case");
return Marshal.PtrToStringAnsi(videoPtr);
}
}
finally
{
LibcSupport.Free(audioPtr);
LibcSupport.Free(videoPtr);
}
}
///
/// Retrieves the codec name of the audio or null if there is no audio.
///
/// A string that represents the codec name.
/// 3
public string GetAudioCodec()
{
return GetCodecInfo(true);
}
///
/// Retrieves the codec name of the video or null if there is no video.
///
/// A string that represents the codec name.
/// 3
public string GetVideoCodec()
{
return GetCodecInfo(false);
}
///
/// Gets the duration.
///
/// The duration of the stream.
///
/// The that owns this instance must be in the ,
/// , or state.
///
///
/// The that this instance belongs to has been disposed of.
///
///
/// The that this instance belongs to is not in the valid state.
///
/// 3
public int GetDuration()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
int duration = 0;
NativePlayer.GetDuration(Player.Handle, out duration).
ThrowIfFailed("Failed to get the duration");
Log.Info(PlayerLog.Tag, "get duration : " + duration);
return duration;
}
///
/// Gets the properties of the audio.
///
/// A that contains the audio stream information.
///
/// The that owns this instance must be in the ,
/// , or state.
///
///
/// The that this instance belongs to has been disposed of.
///
///
/// The that this instance belongs to is not in the valid state.
///
/// 3
public AudioStreamProperties GetAudioProperties()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
int sampleRate = 0;
int channels = 0;
int bitRate = 0;
NativePlayer.GetAudioStreamInfo(Player.Handle, out sampleRate, out channels, out bitRate).
ThrowIfFailed("Failed to get audio stream info");
return new AudioStreamProperties(sampleRate, channels, bitRate);
}
///
/// Gets the properties of the video.
///
/// A that contains the video stream information.
///
/// The that owns this instance must be in the ,
/// , or state.
///
///
/// The that this instance belongs to has been disposed of.
///
///
/// The that this instance belongs to is not in the valid state.
///
/// 3
public VideoStreamProperties GetVideoProperties()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
int fps = 0;
int bitRate = 0;
NativePlayer.GetVideoStreamInfo(Player.Handle, out fps, out bitRate).
ThrowIfFailed("Failed to get the video stream info");
return new VideoStreamProperties(fps, bitRate, GetVideoSize());
}
private Size GetVideoSize()
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
int height = 0;
int width = 0;
NativePlayer.GetVideoSize(Player.Handle, out width, out height).
ThrowIfFailed("Failed to get the video size");
return new Size(width, height);
}
///
/// Gets the metadata with the specified key.
///
/// A string that represents the value of the specified key.
/// The key to query.
///
/// The that owns this instance must be in the ,
/// , or state.
///
/// The that this instance belongs to has been disposed of.
///
///
/// The that this instance belongs to is not in the valid state.
///
/// 3
public string GetMetadata(StreamMetadataKey key)
{
Player.ValidatePlayerState(PlayerState.Ready, PlayerState.Playing, PlayerState.Paused);
ValidationUtil.ValidateEnum(typeof(StreamMetadataKey), key);
IntPtr ptr = IntPtr.Zero;
try
{
NativePlayer.GetContentInfo(Player.Handle, key, out ptr).
ThrowIfFailed($"Failed to get the meta data with the key '{ key }'");
return Marshal.PtrToStringAnsi(ptr);
}
finally
{
LibcSupport.Free(ptr);
}
}
///
/// Gets the that owns this instance.
///
/// 3
public Player Player { get; }
}
}