channel.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'dart:async';
  2. import 'package:fis_common/event/event_type.dart';
  3. import 'package:fis_common/index.dart';
  4. import 'package:fis_vid/async_vid/vid_data.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:vid/us/vid_us_data_http_reader.dart';
  7. import 'package:vid/us/vid_us_image.dart';
  8. import 'package:vid/us/vid_us_image_data.dart';
  9. import 'package:vid/us/vid_us_probe.dart';
  10. import 'channel_stub.dart'
  11. if (dart.library.html) 'channel_web.dart'
  12. if (dart.library.io) 'channel_io.dart';
  13. import 'progress_info.dart';
  14. abstract class VidDataChannel {
  15. @protected
  16. AsyncVidImageDataBase? source;
  17. bool get _isVrdPlayer => url.endsWith(".0");
  18. final String url;
  19. VidDataChannel(this.url);
  20. factory VidDataChannel.create(String url) {
  21. return VidDataChannelImpl(url);
  22. }
  23. static void receiveChunk(String id, Uint8List chunk) {
  24. VidDataChannelImpl.receiveChunk(id, chunk);
  25. }
  26. /// 下载进度变更事件
  27. final downloadProgressChanged = FEventHandler<VidDownloadProgressInfo>();
  28. bool _initialized = false;
  29. /// 是否初始化完成
  30. bool get initialized => _initialized;
  31. /// 开始加载数据
  32. ///
  33. /// [timeout] 超时时间,单位ms, 默认5s
  34. Future<bool> load([int timeout = 5 * 1000]) async {
  35. if (_initialized) return true;
  36. try {
  37. _initialized = await _innerLoad(timeout);
  38. } catch (e) {}
  39. return _initialized;
  40. }
  41. /// 分发下载进度回调
  42. @protected
  43. void onDownloadCallback(double progress, DownloadErrorException? error) {
  44. final info = VidDownloadProgressInfo(progress, error: error);
  45. downloadProgressChanged.emit(this, info);
  46. }
  47. Future<bool> _innerLoad(int timeout) async {
  48. try {
  49. source = await buildSource();
  50. source!.setReadHeaderTimeout(timeout + 50); // 50ms buffer 4 timer
  51. await source!.initialize();
  52. print("_innerLoad initialize end");
  53. return true;
  54. } catch (e) {
  55. return false;
  56. }
  57. }
  58. ///获取缓冲量
  59. int getBufferSize() {
  60. return source!.getReader().downloadedSize;
  61. }
  62. /// 获取文件尺寸
  63. int getFileSize() {
  64. return source!.getReader().totalSize;
  65. }
  66. /// 是否缓冲完成
  67. bool get isBufferedDone {
  68. var bufferSize = getBufferSize();
  69. var fileSized = getFileSize();
  70. var result = bufferSize >= fileSized;
  71. print("是否缓冲完成:$result $bufferSize $fileSized");
  72. return result;
  73. }
  74. VidUsProbe get probe => source!.probe;
  75. int get imageCount => source!.imageCount;
  76. VidUsImageFormat get imageFormat => source!.imageFormat;
  77. Uint8List get extendedData => source!.extendedData;
  78. /// 获取指定帧
  79. ///
  80. /// [index] 帧索引
  81. ///
  82. /// [timeout] 超时时间ms
  83. Future<VidUsImage> getImage(int index, [int timeout = 500]) async {
  84. source!.setReadImageTimeout(timeout);
  85. return source!.getImage(index);
  86. }
  87. bool isBufferedDoneForFrames(int index) {
  88. if (_isVrdPlayer) {
  89. bool result = source!.getReader().frames.containsKey(index);
  90. if (result) {
  91. result = source!.getReader().frames[index]!.isNotEmpty;
  92. }
  93. return result;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /// 关闭通道
  99. void close() {
  100. _initialized = false;
  101. try {
  102. source?.close();
  103. } catch (e) {}
  104. }
  105. @protected
  106. Future<AsyncVidImageDataBase> buildSource();
  107. }