hardwaresetting.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'package:fis_jsonrpc/services/authentication.m.dart';
  2. import 'package:fis_lib_media_rt/implementations/trtc/client_getter_mixin.dart';
  3. import 'package:flyinsonolite/jsonrpc/fisLib/services/log.m.dart';
  4. import 'package:flyinsonolite/jsonrpc/jsonrpcclientforFISLib.dart';
  5. import 'package:flyinsonolite/infrastructure/logger.dart';
  6. import 'package:flyinsonolite/infrastructure/storage.dart';
  7. import 'package:tencent_trtc_cloud/tx_device_manager.dart';
  8. /// 音视频硬件设置
  9. class HardwareSetting with TrtcClientGetterMixin {
  10. HardwareSetting._internal();
  11. static HardwareSetting? _instance;
  12. /// 获取单例
  13. static HardwareSetting get ins {
  14. _instance ??= HardwareSetting._internal();
  15. return _instance!;
  16. }
  17. String _cameraName = '';
  18. String _speakerName = '';
  19. String _micName = '';
  20. /// 推荐的Web端摄像头设备名称
  21. String get cameraName => _cameraName;
  22. /// 推荐的Web端扬声器设备名称
  23. String get speakerName => _speakerName;
  24. /// 推荐的Web端麦克风设备名称
  25. String get micName => _micName;
  26. Future setHardwareSettingsAsync() async {
  27. try {
  28. var hardwareInfo =
  29. await jsonRpcProxyForFISLib.hardware.getCurrentHardwareInfo();
  30. _cameraName = hardwareInfo.cameraInfo == null
  31. ? ''
  32. : (await _getTrtcCameraNameAsync(hardwareInfo.cameraInfo!.name!));
  33. _micName = hardwareInfo.micInfo == null
  34. ? ''
  35. : (await _getTrtcMicNameAsync(hardwareInfo.micInfo!.name!));
  36. _speakerName = hardwareInfo.speakerInfo?.name ?? '';
  37. await loggerAsyn(FISDeviceLogCategory.Info,
  38. "setHardwareSetting cameraName:$_cameraName,micName:$_micName,speakerName:$_speakerName");
  39. } catch (ex) {
  40. await loggerAsyn(FISDeviceLogCategory.Error,
  41. 'HardwareSetting setHardwareSettingsAsync error :$ex');
  42. }
  43. }
  44. Future<String> _getTrtcCameraNameAsync(String deviceName) async {
  45. final TXDeviceManager deviceManager = client.getDeviceManager();
  46. final Map<dynamic, dynamic>? cameras =
  47. await deviceManager.getDevicesList(2);
  48. if (Storage.platform != Platform.Windows) {
  49. switch (deviceName.toLowerCase()) {
  50. case "back camera":
  51. deviceName = "camera_1";
  52. break;
  53. default:
  54. deviceName = "camera_0"; //默认前置摄像头
  55. break;
  56. }
  57. }
  58. String cameraLabel = '';
  59. if (cameras != null && cameras.isNotEmpty) {
  60. cameras.forEach((key, value) {
  61. if (key == 'deviceList') {
  62. // print("🔍 _trtcGetCameras $key $value");
  63. if (value is List) {
  64. for (var element in value) {
  65. if (element is Map) {
  66. if (element["label"]
  67. .toString()
  68. .toLowerCase()
  69. .contains(deviceName.toLowerCase())) {
  70. cameraLabel = element["label"];
  71. }
  72. }
  73. }
  74. }
  75. }
  76. });
  77. }
  78. return cameraLabel;
  79. }
  80. Future<String> _getTrtcMicNameAsync(String deviceName) async {
  81. final Map<dynamic, dynamic>? microphones =
  82. await deviceManager.getDevicesList(0);
  83. String micLabel = '';
  84. if (microphones != null && microphones.isNotEmpty) {
  85. microphones.forEach((key, value) {
  86. if (key == 'deviceList') {
  87. // print("🔍 _trtcGetCameras $key $value");
  88. if (value is List) {
  89. for (var element in value) {
  90. if (element is Map) {
  91. if (element["label"]
  92. .toString()
  93. .toLowerCase()
  94. .contains(deviceName.toLowerCase())) {
  95. micLabel = element["label"];
  96. }
  97. }
  98. }
  99. }
  100. }
  101. });
  102. }
  103. return micLabel;
  104. }
  105. }