CodecManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProMovieCapture
  6. {
  7. public enum MediaApi
  8. {
  9. Unknown = -1,
  10. AVFoundation = 0,
  11. MediaFoundation = 1,
  12. DirectShow = 2,
  13. MediaCodec = 3, // Android
  14. }
  15. public enum CodecType
  16. {
  17. Video,
  18. Audio,
  19. }
  20. public class Codec : IMediaApiItem
  21. {
  22. private CodecType _codecType;
  23. private int _index;
  24. private string _name;
  25. private bool _hasConfigWindow;
  26. private MediaApi _api;
  27. public CodecType CodecType { get { return _codecType; } }
  28. public int Index { get { return _index; } }
  29. public string Name { get { return _name; } }
  30. public MediaApi MediaApi { get { return _api; } }
  31. public bool HasConfigwindow { get { return _hasConfigWindow; } }
  32. public void ShowConfigWindow()
  33. {
  34. if (_hasConfigWindow)
  35. {
  36. if (_codecType == CodecType.Video)
  37. {
  38. NativePlugin.ConfigureVideoCodec(_index);
  39. }
  40. else if (_codecType == CodecType.Audio)
  41. {
  42. NativePlugin.ConfigureAudioCodec(_index);
  43. }
  44. }
  45. }
  46. internal Codec(CodecType codecType, int index, string name, MediaApi api, bool hasConfigWindow = false)
  47. {
  48. _codecType = codecType;
  49. _index = index;
  50. _name = name;
  51. _api = api;
  52. _hasConfigWindow = hasConfigWindow;
  53. }
  54. }
  55. public class CodecList : IEnumerable
  56. {
  57. internal CodecList(Codec[] codecs)
  58. {
  59. _codecs = codecs;
  60. }
  61. public Codec FindCodec(string name, MediaApi mediaApi = MediaApi.Unknown)
  62. {
  63. Codec result = null;
  64. foreach (Codec codec in _codecs)
  65. {
  66. if (codec.Name == name)
  67. {
  68. if (mediaApi == MediaApi.Unknown || mediaApi == codec.MediaApi)
  69. {
  70. result = codec;
  71. break;
  72. }
  73. }
  74. }
  75. return result;
  76. }
  77. public Codec GetFirstWithMediaApi(MediaApi api)
  78. {
  79. Codec result = null;
  80. foreach (Codec codec in _codecs)
  81. {
  82. if (codec.MediaApi == api)
  83. {
  84. result = codec;
  85. break;
  86. }
  87. }
  88. return result;
  89. }
  90. public IEnumerator GetEnumerator()
  91. {
  92. return _codecs.GetEnumerator();
  93. }
  94. public Codec[] Codecs { get { return _codecs; } }
  95. public int Count { get{ return _codecs.Length; } }
  96. private Codec[] _codecs = new Codec[0];
  97. }
  98. public static class CodecManager
  99. {
  100. public static Codec FindCodec(CodecType codecType, string name)
  101. {
  102. CheckInit();
  103. Codec result = null;
  104. CodecList codecs = GetCodecs(codecType);
  105. result = codecs.FindCodec(name);
  106. return result;
  107. }
  108. public static int GetCodecCount(CodecType codecType)
  109. {
  110. CheckInit();
  111. return GetCodecs(codecType).Count;
  112. }
  113. private static void CheckInit()
  114. {
  115. if (!_isEnumerated)
  116. {
  117. if (NativePlugin.Init())
  118. {
  119. EnumerateCodecs();
  120. }
  121. }
  122. }
  123. private static CodecList GetCodecs(CodecType codecType)
  124. {
  125. CodecList result = null;
  126. switch (codecType)
  127. {
  128. case CodecType.Video:
  129. result = _videoCodecs;
  130. break;
  131. case CodecType.Audio:
  132. result = _audioCodecs;
  133. break;
  134. }
  135. return result;
  136. }
  137. private static void EnumerateCodecs()
  138. {
  139. {
  140. Codec[] videoCodecs = new Codec[NativePlugin.GetVideoCodecCount()];
  141. for (int i = 0; i < videoCodecs.Length; i++)
  142. {
  143. videoCodecs[i] = new Codec(CodecType.Video, i, NativePlugin.GetVideoCodecName(i), NativePlugin.GetVideoCodecMediaApi(i), NativePlugin.IsConfigureVideoCodecSupported(i));
  144. }
  145. _videoCodecs = new CodecList(videoCodecs);
  146. }
  147. {
  148. Codec[] audioCodecs = new Codec[NativePlugin.GetAudioCodecCount()];
  149. for (int i = 0; i < audioCodecs.Length; i++)
  150. {
  151. audioCodecs[i] = new Codec(CodecType.Audio, i, NativePlugin.GetAudioCodecName(i), NativePlugin.GetAudioCodecMediaApi(i), NativePlugin.IsConfigureAudioCodecSupported(i));
  152. }
  153. _audioCodecs = new CodecList(audioCodecs);
  154. }
  155. _isEnumerated = true;
  156. }
  157. public static CodecList VideoCodecs { get { CheckInit(); return _videoCodecs; } }
  158. public static CodecList AudioCodecs { get { CheckInit(); return _audioCodecs; } }
  159. private static bool _isEnumerated = false;
  160. private static CodecList _videoCodecs = new CodecList(new Codec[0]);
  161. private static CodecList _audioCodecs = new CodecList(new Codec[0]);
  162. }
  163. }