From 28ab721c8232dd2730262cb2acd7438cd4a0920b Mon Sep 17 00:00:00 2001 From: nam <36914158+aferin@users.noreply.github.com> Date: Tue, 27 Aug 2019 17:19:32 +0900 Subject: [PATCH] [MediaPlayer] fix APIs to get the list of really supported formats (#990) * [MediaPlayer] fix APIs to get the list of really supported formats --- .../Interop/Interop.Player.cs | 3 + .../Player/MediaStreamSource.cs | 78 ++++++++++++++++------ 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs b/src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs index 819ce0f..1818a01 100644 --- a/src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs +++ b/src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs @@ -378,6 +378,9 @@ internal static partial class Interop [DllImport(Libraries.Player, EntryPoint = "player_audio_offload_foreach_supported_format")] internal static extern PlayerErrorCode SupportedAudioOffloadFormat(IntPtr player, SupportedMediaFormatCallback callback, IntPtr userData); + + [DllImport(Libraries.Player, EntryPoint = "player_foreach_media_stream_supported_format")] + internal static extern PlayerErrorCode SupportedMediaStreamFormat(IntPtr player, SupportedMediaFormatCallback callback, IntPtr userData); } internal class PlayerHandle : SafeHandle diff --git a/src/Tizen.Multimedia.MediaPlayer/Player/MediaStreamSource.cs b/src/Tizen.Multimedia.MediaPlayer/Player/MediaStreamSource.cs index b078642..0bee6d1 100644 --- a/src/Tizen.Multimedia.MediaPlayer/Player/MediaStreamSource.cs +++ b/src/Tizen.Multimedia.MediaPlayer/Player/MediaStreamSource.cs @@ -33,6 +33,8 @@ namespace Tizen.Multimedia { private readonly MediaFormat _audioMediaFormat; private readonly MediaFormat _videoMediaFormat; + private static List _supportedAudioFormats; + private static List _supportedVideoFormats; /// /// Gets all supported audio types. @@ -42,7 +44,8 @@ namespace Tizen.Multimedia { get { - yield return MediaFormatAudioMimeType.Aac; + GetSupportedTypes(); + return _supportedAudioFormats.AsReadOnly(); } } @@ -54,7 +57,54 @@ namespace Tizen.Multimedia { get { - yield return MediaFormatVideoMimeType.H264SP; + GetSupportedTypes(); + return _supportedVideoFormats.AsReadOnly(); + } + } + + private static void GetSupportedTypes() + { + if (_supportedAudioFormats == null && _supportedVideoFormats == null) + { + PlayerHandle _playerHandle; + IntPtr _handle; + + NativePlayer.Create(out _playerHandle).ThrowIfFailed(null, "Failed to create player"); + _handle = _playerHandle.DangerousGetHandle(); + Debug.Assert(_handle != IntPtr.Zero); + + try + { + _supportedAudioFormats = new List(); + _supportedVideoFormats = new List(); + + NativePlayer.SupportedMediaFormatCallback callback = (int format, IntPtr userData) => + { + if (Enum.IsDefined(typeof(MediaFormatAudioMimeType), format)) + { + Log.Debug(PlayerLog.Tag, "supported audio : " + ((MediaFormatAudioMimeType)format).ToString()); + _supportedAudioFormats.Add((MediaFormatAudioMimeType)format); + } + else if (Enum.IsDefined(typeof(MediaFormatVideoMimeType), format)) + { + Log.Debug(PlayerLog.Tag, "supported video : " + ((MediaFormatVideoMimeType)format).ToString()); + _supportedVideoFormats.Add((MediaFormatVideoMimeType)format); + } + else + { + Log.Debug(PlayerLog.Tag, "skipped : " + format.ToString()); + } + + return true; + }; + + NativePlayer.SupportedMediaStreamFormat(_handle, callback, IntPtr.Zero). + ThrowIfFailed(null, "Failed to get the list"); + } + finally + { + _playerHandle.Dispose(); + } } } @@ -92,7 +142,6 @@ namespace Tizen.Multimedia return new MediaStreamConfiguration(this, StreamType.Video); } - /// /// Initializes a new instance of the MediaStreamSource class /// with the specified and . @@ -113,7 +162,7 @@ namespace Tizen.Multimedia { if (audioMediaFormat == null && videoMediaFormat == null) { - throw new ArgumentNullException(nameof(audioMediaFormat) + " and " + nameof(videoMediaFormat)); + throw new ArgumentNullException(string.Concat(nameof(_audioMediaFormat), " and ", nameof(_videoMediaFormat))); } _audioMediaFormat = audioMediaFormat; @@ -134,13 +183,7 @@ namespace Tizen.Multimedia /// 3 public MediaStreamSource(AudioMediaFormat audioMediaFormat) { - if (audioMediaFormat == null) - { - throw new ArgumentNullException(nameof(audioMediaFormat)); - } - - _audioMediaFormat = audioMediaFormat; - + _audioMediaFormat = audioMediaFormat ?? throw new ArgumentNullException(nameof(audioMediaFormat)); AudioConfiguration = CreateAudioConfiguration(audioMediaFormat); } /// @@ -154,13 +197,7 @@ namespace Tizen.Multimedia /// 3 public MediaStreamSource(VideoMediaFormat videoMediaFormat) { - if (videoMediaFormat == null) - { - throw new ArgumentNullException(nameof(videoMediaFormat)); - } - - _videoMediaFormat = videoMediaFormat; - + _videoMediaFormat = videoMediaFormat ?? throw new ArgumentNullException(nameof(videoMediaFormat)); VideoConfiguration = CreateVideoConfiguration(videoMediaFormat); } @@ -206,11 +243,13 @@ namespace Tizen.Multimedia Log.Error(PlayerLog.Tag, "The source is not set as a source to a player yet."); throw new InvalidOperationException("The source is not set as a source to a player yet."); } + if (packet == null) { Log.Error(PlayerLog.Tag, "packet is null"); throw new ArgumentNullException(nameof(packet)); } + if (packet.IsDisposed) { Log.Error(PlayerLog.Tag, "packet is disposed"); @@ -234,6 +273,7 @@ namespace Tizen.Multimedia Log.Error(PlayerLog.Tag, "Video is not configured with the current source."); throw new ArgumentException("Video is not configured with the current source."); } + if (packet.Format.Type == MediaFormatType.Audio && _audioMediaFormat == null) { Log.Error(PlayerLog.Tag, "Audio is not configured with the current source."); @@ -250,7 +290,7 @@ namespace Tizen.Multimedia { if (mediaFormat == null) { - Log.Error(PlayerLog.Tag, "invalid media format"); + Log.Warn(PlayerLog.Tag, "invalid media format"); return; } -- 2.7.4