123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- using Android.App;
- using Android.Content;
- using Android.Hardware.Camera2;
- using Android.Media;
- using Android.OS;
- using Java.Lang;
- using System;
- using System.Collections.Generic;
- using Vinno.FIS.TRTCClient.Common.Enum;
- using Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo;
- using Vinno.vCloud.FIS.CrossPlatform.Common;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware.Interface;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo;
- using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
- using Exception = System.Exception;
- namespace Vinno.vCloud.FIS.CrossPlatform.Android.Hardware
- {
- internal class HardwareDetector : IHardwareDetector
- {
- private CameraManager _cameraManager;
- private AudioManager _audioManager;
- private ICapturer _capturer;
- public HardwareInfo CurrentCameraDevice { get; set; }
- public HardwareInfo CurrentMicDevice { get; set; }
- public HardwareInfo CurrentSpeakerDevice { get; set; }
- /// <summary>
- /// Raised when mic volume is change.
- /// </summary>
- public event EventHandler<uint> MicVolumeUpdated;
- /// <summary>
- /// Raised when speaking volume is change.
- /// </summary>
- public event EventHandler<uint> SpeakerVolumeUpdated;
- /// <summary>
- /// Raised when camera preview image recevied.
- /// </summary>
- public event EventHandler<ImageFrameData> CameraPreviewImageReceived;
- public HardwareDetector()
- {
- _cameraManager = (CameraManager)Application.Context.GetSystemService(Context.CameraService);
- _audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
- }
- public void StartMicTest(string micId, string speakerId)
- {
- }
- /// <summary>
- /// Stop mic test.
- /// </summary>
- public void StopMicTest()
- {
- }
- /// <summary>
- /// Set mic device volume
- /// </summary>
- /// <param name="micId">The mic device id.</param>
- /// <param name="volume">The volume of the mic.</param>
- public void SetMicVolume(string micId, uint volume)
- {
- }
- /// <summary>
- /// Start speaker test.
- /// </summary>
- /// <param name="speakerId">The speaker id.</param>
- /// <param name="audioFilePath">The audio file path.</param>
- public void StartSpeakerTest(string speakerId, string audioFilePath)
- {
- }
- /// <summary>
- /// Stop speaker test.
- /// </summary>
- public void StopSpeakerTest()
- {
- }
- /// <summary>
- /// Set speaker volume.
- /// </summary>
- /// <param name="speakerId">The speaker device id.</param>
- /// <param name="volume">The volume of the speaker.</param>
- public void SetSpeakerVolume(string speakerId, uint volume)
- {
- if (volume > 15)
- {
- volume = 15;
- }
- if (volume == 0)
- {
- _audioManager.SetStreamVolume(Stream.Music, 1, 0);
- _audioManager.AdjustStreamVolume(Stream.Music, Adjust.Lower, VolumeNotificationFlags.ShowUi);
- }
- else
- {
- _audioManager.SetStreamVolume(Stream.Music, (int)volume, VolumeNotificationFlags.ShowUi);
- }
- }
- /// <summary>
- /// Set speakder mute.
- /// </summary>
- /// <param name="speakerId">The speaker id.</param>
- /// <param name="isMute">True: mute, False: not mute.</param>
- /// <returns></returns>
- public void SetSpeakerMute(string speakerId, bool isMute)
- {
- try
- {
- if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
- {
- if (isMute)
- {
- _audioManager.SetStreamVolume(Stream.VoiceCall, 1, VolumeNotificationFlags.ShowUi);
- _audioManager.AdjustStreamVolume(Stream.VoiceCall, Adjust.Lower, VolumeNotificationFlags.ShowUi);
- }
- else
- {
- int maxVolume = _audioManager.GetStreamMaxVolume(Stream.VoiceCall);
- _audioManager.SetStreamVolume(Stream.VoiceCall, (int)(maxVolume * 0.5), VolumeNotificationFlags.ShowUi);
- }
- _audioManager.AdjustStreamVolume(Stream.Music, isMute ? Adjust.Mute : Adjust.Unmute, VolumeNotificationFlags.ShowUi);
- }
- else
- {
- _audioManager.SetStreamMute(Stream.VoiceCall, isMute);
- _audioManager.SetStreamMute(Stream.Music, isMute);
- }
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Set Speaker Mute Error{ex}");
- }
- }
- /// <summary>
- /// Set mic mute.
- /// </summary>
- /// <param name="micId">The mic id.</param>
- /// <param name="isMute">True: mute, False: not mute.</param>
- /// <returns></returns>
- public void SetMicMute(string micId, bool isMute)
- {
- _audioManager.MicrophoneMute = isMute;
- }
- /// <summary>
- /// Dispose.
- /// </summary>
- public void Dispose()
- {
- }
- private void OnSpeakerVolumeUpdated(object sender, uint e)
- {
- SpeakerVolumeUpdated?.Invoke(this, e);
- }
- private void OnMicVolumeUpdated(object sender, uint e)
- {
- MicVolumeUpdated.Invoke(this, e);
- }
- /// <summary>
- /// 使用AForge与NTSmartPublisherSDK 得到的Hardware List Mic,Speaker,Camera,其中Speaker与Mic的结果通用,Mic的Index顺序参考RTMP的方法
- /// Camera的情况下AForge得到的Id比RTC与RTMP多出“@device:pnp:”.
- /// </summary>
- public IEnumerable<HardwareInfo> GetHardwareList(EnumHardwareType type)
- {
- var hardwareList = new List<HardwareInfo>();
- int externalNumber = 0;
- switch (type)
- {
- case EnumHardwareType.Camera:
- try
- {
- foreach (var cameraId in _cameraManager.GetCameraIdList())
- {
- var characteristics = _cameraManager.GetCameraCharacteristics(cameraId);
- var facing = characteristics.Get(CameraCharacteristics.LensFacing);
- //默认打开前置摄像头
- if (facing != null && facing == Integer.ValueOf((int)LensFacing.Back))
- {
- hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Back Camera", new List<CameraCaptureCapability>(), 0, false));
- }
- else if (facing != null && facing == Integer.ValueOf((int)LensFacing.Front))
- {
- hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Front Camera", new List<CameraCaptureCapability>(), 0, false));
- }
- else if (facing != null && facing == Integer.ValueOf((int)LensFacing.External))
- {
- externalNumber++;
- hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "External Camera" + externalNumber, new List<CameraCaptureCapability>(), 0, false));
- }
- }
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get camera hardwares error {ex}");
- }
- break;
- case EnumHardwareType.Mic:
- try
- {
- hardwareList.Add(new HardwareInfo(EnumHardwareType.Mic, "0", new List<string>(), "Microphone_0", new List<CameraCaptureCapability>(), 0, _audioManager.MicrophoneMute));
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get mic hardwares error {ex}");
- }
- break;
- case EnumHardwareType.Speaker:
- try
- {
- hardwareList.Add(new HardwareInfo(EnumHardwareType.Speaker, "0", new List<string>(), "Speaker_0", new List<CameraCaptureCapability>(), 0, _audioManager.IsStreamMute(Stream.Music)));
- }
- catch (Exception ex)
- {
- CrossPlatformHelper.Instance.LogWriter?.WriteLineError($"Get speaker hardwares error {ex}");
- }
- break;
- }
- return hardwareList;
- }
- /// <summary>
- /// Only Get Camera For RTMP用,其中MIC与Speaker使用GetHardwareInfo
- /// </summary>
- /// <returns></returns>
- public IEnumerable<HardwareInfo> GetCameraListOnlyForRTMP()
- {
- return new List<HardwareInfo>();
- }
- /// <summary>
- /// Only used For RTC
- /// </summary>
- /// <param name="type"></param>
- /// <param name="deviceId"></param>
- /// <returns></returns>
- public string GetCorrectIdOnlyForRTC(EnumHardwareType type, string deviceId)
- {
- return null;
- }
- /// <summary>
- /// 获取设备当前选择的分辨率下小于指定尺寸的相同宽高比的最大分辨率
- /// </summary>
- /// <param name="id">设备ID</param>
- /// <param name="width">原始高度</param>
- /// <param name="height">原始宽度</param>
- /// <param name="pusherType">是否合流</param>
- /// <returns></returns>
- public CameraCaptureCapability GetMaxResolutionUnderTheSpecial(string id, int width, int height, EnumPusherType pusherType, EnumLiveChannelCategory category)
- {
- return null;
- }
- /// <summary>
- /// Start camera preview.
- /// </summary>
- /// <param name="cameraId">The camera hardware id.</param>
- public void StartCameraPreview(string cameraId)
- {
- if (_capturer == null)
- {
- _capturer = new Capturer(cameraId, 640, 480, 15, EnumLiveChannelCategory.Auxiliary1, false);
- _capturer.ImageFrameReceived += OnCameraPreviewDataReceived;
- _capturer.StartCapture();
- }
- }
- /// <summary>
- /// Stop camera preview.
- /// </summary>
- public void StopCameraPreview()
- {
- if (_capturer != null)
- {
- _capturer.ImageFrameReceived -= OnCameraPreviewDataReceived;
- _capturer.StopCapture();
- _capturer.Dispose();
- _capturer = null;
- }
- }
- private void OnCameraPreviewDataReceived(object sender, ImageFrameData e)
- {
- CameraPreviewImageReceived?.Invoke(this, e);
- }
- public int GetMicIndex(string micId)
- {
- return 0;
- }
- }
- }
|