[MediaPlayer] fix APIs to get the list of really supported formats (#990)
authornam <36914158+aferin@users.noreply.github.com>
Tue, 27 Aug 2019 08:19:32 +0000 (17:19 +0900)
committerGitHub <noreply@github.com>
Tue, 27 Aug 2019 08:19:32 +0000 (17:19 +0900)
* [MediaPlayer] fix APIs to get the list of really supported formats

src/Tizen.Multimedia.MediaPlayer/Interop/Interop.Player.cs
src/Tizen.Multimedia.MediaPlayer/Player/MediaStreamSource.cs

index 819ce0f..1818a01 100644 (file)
@@ -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
index b078642..0bee6d1 100644 (file)
@@ -33,6 +33,8 @@ namespace Tizen.Multimedia
     {
         private readonly MediaFormat _audioMediaFormat;
         private readonly MediaFormat _videoMediaFormat;
+        private static List<MediaFormatAudioMimeType> _supportedAudioFormats;
+        private static List<MediaFormatVideoMimeType> _supportedVideoFormats;
 
         /// <summary>
         /// 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<MediaFormatAudioMimeType>();
+                    _supportedVideoFormats = new List<MediaFormatVideoMimeType>();
+
+                    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);
         }
 
-
         /// <summary>
         /// Initializes a new instance of the MediaStreamSource class
         /// with the specified <see cref="AudioMediaFormat"/> and <see cref="VideoMediaFormat"/>.
@@ -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
         /// <since_tizen> 3 </since_tizen>
         public MediaStreamSource(AudioMediaFormat audioMediaFormat)
         {
-            if (audioMediaFormat == null)
-            {
-                throw new ArgumentNullException(nameof(audioMediaFormat));
-            }
-
-            _audioMediaFormat = audioMediaFormat;
-
+            _audioMediaFormat = audioMediaFormat ?? throw new ArgumentNullException(nameof(audioMediaFormat));
             AudioConfiguration = CreateAudioConfiguration(audioMediaFormat);
         }
         /// <summary>
@@ -154,13 +197,7 @@ namespace Tizen.Multimedia
         /// <since_tizen> 3 </since_tizen>
         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;
             }