HardwareDetector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using Android.App;
  2. using Android.Content;
  3. using Android.Hardware.Camera2;
  4. using Android.Media;
  5. using Android.OS;
  6. using Java.Lang;
  7. using System;
  8. using System.Collections.Generic;
  9. using Vinno.FIS.TRTCClient.Common.Enum;
  10. using Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo;
  11. using Vinno.vCloud.FIS.CrossPlatform.Common;
  12. using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
  13. using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware;
  14. using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware.Interface;
  15. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
  16. using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
  17. using Exception = System.Exception;
  18. namespace Vinno.vCloud.FIS.CrossPlatform.Android.Hardware
  19. {
  20. internal class HardwareDetector : IHardwareDetector
  21. {
  22. private CameraManager _cameraManager;
  23. private AudioManager _audioManager;
  24. private ICapturer _capturer;
  25. public HardwareInfo CurrentCameraDevice { get; set; }
  26. public HardwareInfo CurrentMicDevice { get; set; }
  27. public HardwareInfo CurrentSpeakerDevice { get; set; }
  28. /// <summary>
  29. /// Raised when mic volume is change.
  30. /// </summary>
  31. public event EventHandler<uint> MicVolumeUpdated;
  32. /// <summary>
  33. /// Raised when speaking volume is change.
  34. /// </summary>
  35. public event EventHandler<uint> SpeakerVolumeUpdated;
  36. /// <summary>
  37. /// Raised when camera preview image recevied.
  38. /// </summary>
  39. public event EventHandler<ImageFrameData> CameraPreviewImageReceived;
  40. public HardwareDetector()
  41. {
  42. _cameraManager = (CameraManager)Application.Context.GetSystemService(Context.CameraService);
  43. _audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
  44. }
  45. public void StartMicTest(string micId, string speakerId)
  46. {
  47. }
  48. /// <summary>
  49. /// Stop mic test.
  50. /// </summary>
  51. public void StopMicTest()
  52. {
  53. }
  54. /// <summary>
  55. /// Set mic device volume
  56. /// </summary>
  57. /// <param name="micId">The mic device id.</param>
  58. /// <param name="volume">The volume of the mic.</param>
  59. public void SetMicVolume(string micId, uint volume)
  60. {
  61. }
  62. /// <summary>
  63. /// Start speaker test.
  64. /// </summary>
  65. /// <param name="speakerId">The speaker id.</param>
  66. /// <param name="audioFilePath">The audio file path.</param>
  67. public void StartSpeakerTest(string speakerId, string audioFilePath)
  68. {
  69. }
  70. /// <summary>
  71. /// Stop speaker test.
  72. /// </summary>
  73. public void StopSpeakerTest()
  74. {
  75. }
  76. /// <summary>
  77. /// Set speaker volume.
  78. /// </summary>
  79. /// <param name="speakerId">The speaker device id.</param>
  80. /// <param name="volume">The volume of the speaker.</param>
  81. public void SetSpeakerVolume(string speakerId, uint volume)
  82. {
  83. if (volume > 15)
  84. {
  85. volume = 15;
  86. }
  87. if (volume == 0)
  88. {
  89. _audioManager.SetStreamVolume(Stream.Music, 1, 0);
  90. _audioManager.AdjustStreamVolume(Stream.Music, Adjust.Lower, VolumeNotificationFlags.ShowUi);
  91. }
  92. else
  93. {
  94. _audioManager.SetStreamVolume(Stream.Music, (int)volume, VolumeNotificationFlags.ShowUi);
  95. }
  96. }
  97. /// <summary>
  98. /// Set speakder mute.
  99. /// </summary>
  100. /// <param name="speakerId">The speaker id.</param>
  101. /// <param name="isMute">True: mute, False: not mute.</param>
  102. /// <returns></returns>
  103. public void SetSpeakerMute(string speakerId, bool isMute)
  104. {
  105. try
  106. {
  107. if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
  108. {
  109. if (isMute)
  110. {
  111. _audioManager.SetStreamVolume(Stream.VoiceCall, 1, VolumeNotificationFlags.ShowUi);
  112. _audioManager.AdjustStreamVolume(Stream.VoiceCall, Adjust.Lower, VolumeNotificationFlags.ShowUi);
  113. }
  114. else
  115. {
  116. int maxVolume = _audioManager.GetStreamMaxVolume(Stream.VoiceCall);
  117. _audioManager.SetStreamVolume(Stream.VoiceCall, (int)(maxVolume * 0.5), VolumeNotificationFlags.ShowUi);
  118. }
  119. _audioManager.AdjustStreamVolume(Stream.Music, isMute ? Adjust.Mute : Adjust.Unmute, VolumeNotificationFlags.ShowUi);
  120. }
  121. else
  122. {
  123. _audioManager.SetStreamMute(Stream.VoiceCall, isMute);
  124. _audioManager.SetStreamMute(Stream.Music, isMute);
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Set Speaker Mute Error{ex}");
  130. }
  131. }
  132. /// <summary>
  133. /// Set mic mute.
  134. /// </summary>
  135. /// <param name="micId">The mic id.</param>
  136. /// <param name="isMute">True: mute, False: not mute.</param>
  137. /// <returns></returns>
  138. public void SetMicMute(string micId, bool isMute)
  139. {
  140. _audioManager.MicrophoneMute = isMute;
  141. }
  142. /// <summary>
  143. /// Dispose.
  144. /// </summary>
  145. public void Dispose()
  146. {
  147. }
  148. private void OnSpeakerVolumeUpdated(object sender, uint e)
  149. {
  150. SpeakerVolumeUpdated?.Invoke(this, e);
  151. }
  152. private void OnMicVolumeUpdated(object sender, uint e)
  153. {
  154. MicVolumeUpdated.Invoke(this, e);
  155. }
  156. /// <summary>
  157. /// 使用AForge与NTSmartPublisherSDK 得到的Hardware List Mic,Speaker,Camera,其中Speaker与Mic的结果通用,Mic的Index顺序参考RTMP的方法
  158. /// Camera的情况下AForge得到的Id比RTC与RTMP多出“@device:pnp:”.
  159. /// </summary>
  160. public IEnumerable<HardwareInfo> GetHardwareList(EnumHardwareType type)
  161. {
  162. var hardwareList = new List<HardwareInfo>();
  163. int externalNumber = 0;
  164. switch (type)
  165. {
  166. case EnumHardwareType.Camera:
  167. try
  168. {
  169. foreach (var cameraId in _cameraManager.GetCameraIdList())
  170. {
  171. var characteristics = _cameraManager.GetCameraCharacteristics(cameraId);
  172. var facing = characteristics.Get(CameraCharacteristics.LensFacing);
  173. //默认打开前置摄像头
  174. if (facing != null && facing == Integer.ValueOf((int)LensFacing.Back))
  175. {
  176. hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Back Camera", new List<CameraCaptureCapability>(), 0, false));
  177. }
  178. else if (facing != null && facing == Integer.ValueOf((int)LensFacing.Front))
  179. {
  180. hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Front Camera", new List<CameraCaptureCapability>(), 0, false));
  181. }
  182. else if (facing != null && facing == Integer.ValueOf((int)LensFacing.External))
  183. {
  184. externalNumber++;
  185. hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "External Camera" + externalNumber, new List<CameraCaptureCapability>(), 0, false));
  186. }
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get camera hardwares error {ex}");
  192. }
  193. break;
  194. case EnumHardwareType.Mic:
  195. try
  196. {
  197. hardwareList.Add(new HardwareInfo(EnumHardwareType.Mic, "0", new List<string>(), "Microphone_0", new List<CameraCaptureCapability>(), 0, _audioManager.MicrophoneMute));
  198. }
  199. catch (Exception ex)
  200. {
  201. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get mic hardwares error {ex}");
  202. }
  203. break;
  204. case EnumHardwareType.Speaker:
  205. try
  206. {
  207. hardwareList.Add(new HardwareInfo(EnumHardwareType.Speaker, "0", new List<string>(), "Speaker_0", new List<CameraCaptureCapability>(), 0, _audioManager.IsStreamMute(Stream.Music)));
  208. }
  209. catch (Exception ex)
  210. {
  211. CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get speaker hardwares error {ex}");
  212. }
  213. break;
  214. }
  215. return hardwareList;
  216. }
  217. /// <summary>
  218. /// Only Get Camera For RTMP用,其中MIC与Speaker使用GetHardwareInfo
  219. /// </summary>
  220. /// <returns></returns>
  221. public IEnumerable<HardwareInfo> GetCameraListOnlyForRTMP()
  222. {
  223. return new List<HardwareInfo>();
  224. }
  225. /// <summary>
  226. /// Only used For RTC
  227. /// </summary>
  228. /// <param name="type"></param>
  229. /// <param name="deviceId"></param>
  230. /// <returns></returns>
  231. public string GetCorrectIdOnlyForRTC(EnumHardwareType type, string deviceId)
  232. {
  233. return null;
  234. }
  235. /// <summary>
  236. /// 获取设备当前选择的分辨率下小于指定尺寸的相同宽高比的最大分辨率
  237. /// </summary>
  238. /// <param name="id">设备ID</param>
  239. /// <param name="width">原始高度</param>
  240. /// <param name="height">原始宽度</param>
  241. /// <param name="pusherType">是否合流</param>
  242. /// <returns></returns>
  243. public CameraCaptureCapability GetMaxResolutionUnderTheSpecial(string id, int width, int height, EnumPusherType pusherType, EnumLiveChannelCategory category)
  244. {
  245. return null;
  246. }
  247. /// <summary>
  248. /// Start camera preview.
  249. /// </summary>
  250. /// <param name="cameraId">The camera hardware id.</param>
  251. public void StartCameraPreview(string cameraId)
  252. {
  253. if (_capturer == null)
  254. {
  255. _capturer = new Capturer(cameraId, 640, 480, 15, EnumLiveChannelCategory.Auxiliary1, false);
  256. _capturer.ImageFrameReceived += OnCameraPreviewDataReceived;
  257. _capturer.StartCapture();
  258. }
  259. }
  260. /// <summary>
  261. /// Stop camera preview.
  262. /// </summary>
  263. public void StopCameraPreview()
  264. {
  265. if (_capturer != null)
  266. {
  267. _capturer.ImageFrameReceived -= OnCameraPreviewDataReceived;
  268. _capturer.StopCapture();
  269. _capturer.Dispose();
  270. _capturer = null;
  271. }
  272. }
  273. private void OnCameraPreviewDataReceived(object sender, ImageFrameData e)
  274. {
  275. CameraPreviewImageReceived?.Invoke(this, e);
  276. }
  277. public int GetMicIndex(string micId)
  278. {
  279. return 0;
  280. }
  281. }
  282. }