reader.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import 'dart:typed_data';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:vid/us/vid_us_data_http_reader.dart';
  4. typedef AsyncVidDataReaderBytesProxy = Uint8List Function();
  5. abstract class AsyncVidDataReaderBase {
  6. Uint8List _chunkedData = Uint8List(0);
  7. int _bufferSize = 0;
  8. int _index = 0;
  9. bool _error = false;
  10. String _errorMsg = '';
  11. double _downloadProgress = 0;
  12. String url;
  13. //科研版内靠这个获取缓存
  14. Map<int, Uint8List> frames = {};
  15. bool get isVrd => url.endsWith(".0");
  16. DownloadCallback? downloadCallback;
  17. AsyncVidDataReaderBytesProxy? _bytesProxy;
  18. AsyncVidDataReaderBase(this.url, {this.downloadCallback}) {
  19. frames = {};
  20. startDownload();
  21. }
  22. Uint8List get _underlyingData {
  23. if (_bytesProxy != null) {
  24. return _bytesProxy!.call();
  25. }
  26. return _chunkedData;
  27. }
  28. int get downloadedSize => _bufferSize;
  29. int get totalSize => _underlyingData.length;
  30. void startDownload();
  31. //跳帧
  32. void skipToFrame(int index);
  33. ///批量获取帧数
  34. Future<void> fetchFrames(int startIndex, int size,
  35. {bool isNeedReload = false});
  36. void initChunk(int size) {
  37. _chunkedData = Uint8List(size);
  38. }
  39. void appendChunk(Uint8List chunk) {
  40. print('appendChunk: $_bufferSize ${chunk.length}');
  41. if (isVrd && _chunkedData.length < _bufferSize + chunk.length) {
  42. _chunkedData = extendUint8List(
  43. _chunkedData, _bufferSize + chunk.length - _chunkedData.length);
  44. }
  45. _chunkedData.setAll(_bufferSize, chunk);
  46. setBufferSize(_bufferSize + chunk.length);
  47. }
  48. void appendFrame(Uint8List frame, int index) {
  49. if (frames.containsKey(index) && frames[index]! == frame) {
  50. return;
  51. }
  52. frames[index] = frame;
  53. }
  54. Uint8List extendUint8List(Uint8List originalData, int additionalSize) {
  55. // 创建新的 Uint8List,其长度等于原始长度 + 额外需要的长度
  56. Uint8List newData = Uint8List(originalData.length + additionalSize);
  57. // 将原始数据复制到新的 Uint8List 中
  58. newData.setRange(0, originalData.length, originalData);
  59. return newData; // 返回扩展后的新数据
  60. }
  61. @protected
  62. void setBufferSize(int size) {
  63. print('setBufferSize size:$size');
  64. _bufferSize = size;
  65. }
  66. void setError(bool hasErr, [String msg = '']) {
  67. _error = hasErr;
  68. _errorMsg = hasErr ? msg : '';
  69. if (_error) {
  70. downloadCallback?.call(_downloadProgress, DownloadErrorException(msg));
  71. }
  72. }
  73. void updateProgress(double progress, [DownloadErrorException? err]) {
  74. _downloadProgress = progress;
  75. print('updateProgress : $progress');
  76. downloadCallback?.call(_downloadProgress, err);
  77. }
  78. /// 通过代理从其他渠道读取字节数组
  79. ///
  80. /// 仅用于接入[VidUsDataHttpReader]
  81. @protected
  82. void setBytesProxy(AsyncVidDataReaderBytesProxy proxy) {
  83. _bytesProxy = proxy;
  84. }
  85. Uint8List toBytes() {
  86. return _underlyingData;
  87. }
  88. //Close the reader and its overhead.
  89. void close();
  90. ///Read int value from binary data(little endian),
  91. int readInt([int index = -1]) {
  92. if (_error) {
  93. throw DownloadErrorException(_errorMsg);
  94. }
  95. if (index == -1) {
  96. if ((_bufferSize - _index) < 4) {
  97. throw NotReadyException();
  98. }
  99. var intData = _underlyingData.buffer.asByteData(_index, 4);
  100. _index += 4;
  101. return intData.getInt32(0, Endian.little);
  102. } else {
  103. if ((_bufferSize - index) < 4) {
  104. throw NotReadyException();
  105. }
  106. var intData = _underlyingData.buffer.asByteData(index, 4);
  107. _index = index + 4;
  108. return intData.getInt32(0, Endian.little);
  109. }
  110. }
  111. ///Read string from the binary data, the string format is utf-16, which is unicode in C#
  112. String readString([int index = -1]) {
  113. if (_error) {
  114. throw DownloadErrorException(_errorMsg);
  115. }
  116. int dataLength;
  117. if (index == -1) {
  118. dataLength = readInt();
  119. if ((_bufferSize - _index) < dataLength) {
  120. throw NotReadyException();
  121. }
  122. } else {
  123. dataLength = readInt(index);
  124. if ((_bufferSize - index) < dataLength) {
  125. throw NotReadyException();
  126. }
  127. }
  128. var stringData = _underlyingData.buffer.asUint8List(_index, dataLength);
  129. _index += dataLength;
  130. var unicodeStringData =
  131. Uint8List.fromList(stringData.toList()).buffer.asUint16List();
  132. return String.fromCharCodes(unicodeStringData);
  133. }
  134. ///Read int16 value from binary data(little endian)
  135. int readInt16([int index = -1]) {
  136. if (_error) {
  137. throw DownloadErrorException(_errorMsg);
  138. }
  139. if (index == -1) {
  140. if ((_bufferSize - _index) < 2) {
  141. throw NotReadyException();
  142. }
  143. var intData = _underlyingData.buffer.asByteData(_index, 2);
  144. _index += 2;
  145. return intData.getInt16(0, Endian.little);
  146. } else {
  147. if ((_bufferSize - index) < 2) {
  148. throw NotReadyException();
  149. }
  150. var intData = _underlyingData.buffer.asByteData(index, 2);
  151. _index = index + 2;
  152. return intData.getInt16(0, Endian.little);
  153. }
  154. }
  155. ///Read int64 value from binary data(little endian)
  156. ///this method is not support in web platform use readInt64V2 instead.
  157. int readInt64([int index = -1]) {
  158. if (_error) {
  159. throw DownloadErrorException(_errorMsg);
  160. }
  161. if (index == -1) {
  162. if ((_bufferSize - _index) < 8) {
  163. throw NotReadyException();
  164. }
  165. var intData = _underlyingData.buffer.asByteData(_index, 8);
  166. _index += 8;
  167. return intData.getInt64(0, Endian.little);
  168. } else {
  169. if ((_bufferSize - index) < 8) {
  170. throw NotReadyException();
  171. }
  172. var intData = _underlyingData.buffer.asByteData(index, 8);
  173. _index = index + 8;
  174. return intData.getInt64(0, Endian.little);
  175. }
  176. }
  177. ///Read int64 value from binary data(little endian)
  178. ///this method use two int32 to support read int64 on web platform
  179. int readInt64V2([int index = -1]) {
  180. if (_error) {
  181. throw DownloadErrorException(_errorMsg);
  182. }
  183. if (index == -1) {
  184. if ((_bufferSize - _index) < 8) {
  185. throw NotReadyException();
  186. }
  187. int low = readInt();
  188. int high = readInt();
  189. int value = high >> 32;
  190. value |= low;
  191. return value;
  192. } else {
  193. if ((_bufferSize - index) < 8) {
  194. throw NotReadyException();
  195. }
  196. int low = readInt(index);
  197. //Read the next int.
  198. int high = readInt();
  199. int value = high >> 32;
  200. value |= low;
  201. return value;
  202. }
  203. }
  204. ///Read float value from binary data(little endian)
  205. double readFloat([int index = -1]) {
  206. if (_error) {
  207. throw DownloadErrorException(_errorMsg);
  208. }
  209. if (index == -1) {
  210. if ((_bufferSize - _index) < 4) {
  211. throw NotReadyException();
  212. }
  213. var floatData = _underlyingData.buffer.asByteData(_index, 4);
  214. _index += 4;
  215. return floatData.getFloat32(0, Endian.little);
  216. } else {
  217. if ((_bufferSize - index) < 4) {
  218. throw NotReadyException();
  219. }
  220. var floatData = _underlyingData.buffer.asByteData(index, 4);
  221. _index = index + 4;
  222. return floatData.getFloat32(0, Endian.little);
  223. }
  224. }
  225. ///Read double value from binary data(little endian)
  226. double readDouble([int index = -1]) {
  227. if (_error) {
  228. throw DownloadErrorException(_errorMsg);
  229. }
  230. if (index == -1) {
  231. if ((_bufferSize - _index) < 8) {
  232. throw NotReadyException();
  233. }
  234. var floatData = _underlyingData.buffer.asByteData(_index, 8);
  235. _index += 8;
  236. return floatData.getFloat64(0, Endian.little);
  237. } else {
  238. if ((_bufferSize - index) < 8) {
  239. throw NotReadyException();
  240. }
  241. var floatData = _underlyingData.buffer.asByteData(index, 8);
  242. _index = index + 8;
  243. return floatData.getFloat64(0, Endian.little);
  244. }
  245. }
  246. ///Read bool value from binary data
  247. bool readBool([int index = -1]) {
  248. if (_error) {
  249. throw DownloadErrorException(_errorMsg);
  250. }
  251. if (index == -1) {
  252. if ((_bufferSize - _index) < 1) {
  253. throw NotReadyException();
  254. }
  255. var boolData = _underlyingData.buffer.asByteData(_index, 1);
  256. _index++;
  257. return boolData.getInt8(0) == 1;
  258. } else {
  259. if ((_bufferSize - index) < 1) {
  260. throw NotReadyException();
  261. }
  262. var boolData = _underlyingData.buffer.asByteData(index, 1);
  263. _index = index + 1;
  264. return boolData.getInt8(0) == 1;
  265. }
  266. }
  267. ///Read byte value from binary data
  268. int readByte([int index = -1]) {
  269. if (_error) {
  270. throw DownloadErrorException(_errorMsg);
  271. }
  272. if (index == -1) {
  273. if ((_bufferSize - _index) < 1) {
  274. throw NotReadyException();
  275. }
  276. var byteData = _underlyingData.buffer.asByteData(_index, 1);
  277. _index++;
  278. return byteData.getInt8(0);
  279. } else {
  280. if ((_bufferSize - index) < 1) {
  281. throw NotReadyException();
  282. }
  283. var byteData = _underlyingData.buffer.asByteData(index, 1);
  284. _index = index + 1;
  285. return byteData.getInt8(0);
  286. }
  287. }
  288. ///Read bytes from the binary data.
  289. Uint8List readBytes([int index = -1]) {
  290. if (_error) {
  291. throw DownloadErrorException(_errorMsg);
  292. }
  293. var dataLength = readInt(index);
  294. if ((_bufferSize - _index) < dataLength) {
  295. throw NotReadyException();
  296. }
  297. var bytes = _underlyingData.buffer.asUint8List(_index, dataLength);
  298. _index += dataLength;
  299. return bytes;
  300. }
  301. Uint8List getImageBytes(int index) {
  302. print("getImageBytes by index: $index,frames count:${frames.length}");
  303. Uint8List result;
  304. if (frames.containsKey(index)) {
  305. result = frames[index] ?? Uint8List(0);
  306. } else {
  307. result = Uint8List(0);
  308. }
  309. return result;
  310. }
  311. }