vid_us_image_data.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import 'dart:typed_data';
  2. import 'package:vid/us/vid_us_application.dart';
  3. import 'package:vid/us/vid_us_data_http_reader.dart';
  4. import 'package:vid/us/vid_us_data_reader.dart';
  5. import 'package:vid/us/vid_us_data_writer.dart';
  6. import 'package:vid/us/vid_us_image.dart';
  7. import 'package:vid/us/vid_us_probe.dart';
  8. enum VidUsImageFormat {
  9. Jpeg,
  10. Png,
  11. H264,
  12. Zip,
  13. Diff,
  14. }
  15. ///Not suport carotid3d for now.
  16. class VidUsImageData {
  17. late VidUsDataReader _reader;
  18. late List<int> _imagePositionList;
  19. late int _version;
  20. late int _imageCount;
  21. late VidUsProbe _probe;
  22. late VidUsImageFormat _imageFormat;
  23. late Uint8List _extendedData;
  24. late List<VidUsImage> _imagesList;
  25. final String header = "VINNO IMAGE DATA";
  26. /// Gets the version of this image data.
  27. int get version => _version;
  28. /// Gets the image count of this image data.
  29. int get imageCount => _imageCount;
  30. /// Gets the probe information.
  31. VidUsProbe get probe => _probe;
  32. /// Gets the image format of this image data.
  33. VidUsImageFormat get imageFormat => _imageFormat;
  34. /// Gets or sets the extended data.
  35. Uint8List get extendedData => _extendedData;
  36. /// Create a VINNO Image Data.
  37. VidUsImageData(Uint8List data) {
  38. _reader = VidUsDataReader(data);
  39. var header = _reader.readString();
  40. if (header != header) {
  41. throw Exception("The input data is not a VID data.");
  42. }
  43. _version = _reader.readInt();
  44. //Get probe info
  45. var probeData = _reader.readBytes();
  46. _probe = VidUsProbe.fromBytes(probeData);
  47. _imageFormat = VidUsImageFormat.values[_reader.readInt()];
  48. _extendedData = _reader.readBytes();
  49. _imagePositionList = [];
  50. var imagePositionListData = _reader.readBytes();
  51. _imageCount = imagePositionListData.length ~/ 8;
  52. var imagePositionReader = VidUsDataReader(imagePositionListData);
  53. for (var i = 0; i < _imageCount; i++) {
  54. _imagePositionList.add(imagePositionReader.readInt64V2());
  55. }
  56. _imagesList = [];
  57. }
  58. /// Create an empty third part VINNO Image Data
  59. VidUsImageData.empty(int fps, VidUsImageFormat imageFormat) {
  60. _imageCount = 0;
  61. _version = 1;
  62. _probe = VidUsProbe(
  63. "ThirdPart",
  64. VidUsProbeType.Linear,
  65. VidUsApplication(
  66. "ThirdPart",
  67. "ThirdPart",
  68. "ThirdPart",
  69. "ThirdPart",
  70. false,
  71. ),
  72. fps.toDouble(),
  73. );
  74. _imageFormat = imageFormat;
  75. _extendedData = Uint8List(10);
  76. _imagePositionList = [];
  77. _imagesList = [];
  78. }
  79. /// Add an image to the image data.
  80. void addJpegImage(Uint8List jpegBytes, int width, int height) {
  81. if (_imageFormat != VidUsImageFormat.Jpeg) {
  82. throw Exception("The image format is not Jpeg.");
  83. }
  84. if (_imagesList.length != _imageCount) {
  85. _fillImageList();
  86. }
  87. VidUsImage image = VidUsImage(_imageCount, width, height, jpegBytes);
  88. _imageCount++;
  89. _imagesList.add(image);
  90. }
  91. Uint8List flush() {
  92. if (_imageCount != _imagesList.length) {
  93. _fillImageList();
  94. }
  95. var imagePositionOffset = 0;
  96. var writer = VidUsDataWriter();
  97. writer.writeString(header);
  98. writer.writeInt(_version);
  99. writer.writeBytes(_probe.toBytes());
  100. writer.writeInt(_imageFormat.index);
  101. writer.writeBytes(_extendedData);
  102. imagePositionOffset = writer.data.length + _imageCount * 8 + 4;
  103. var imagePositionWriter = VidUsDataWriter();
  104. for (var i = 0; i < _imageCount; i++) {
  105. imagePositionWriter.writeInt64(imagePositionOffset);
  106. imagePositionOffset += _imagesList[i].toBytes().length + 4;
  107. }
  108. writer.writeBytes(imagePositionWriter.data);
  109. for (var i = 0; i < _imageCount; i++) {
  110. writer.writeBytes(_imagesList[i].toBytes());
  111. }
  112. return writer.data;
  113. }
  114. /// Get one image from the vid.
  115. VidUsImage getImage(int index) {
  116. if (index >= _imageCount || index < 0) {
  117. throw Exception("Can not find image Data");
  118. }
  119. //Jump to image.
  120. var imageData = _reader.readBytes(_imagePositionList[index]);
  121. return VidUsImage.fromBytes(imageData);
  122. }
  123. /// Fill all images to the image list.
  124. void _fillImageList() {
  125. _imagesList = [];
  126. for (var i = 0; i < _imageCount; i++) {
  127. _imagesList.add(getImage(i));
  128. }
  129. }
  130. }
  131. ///Raised when getting data from the downloaded data timeout. depends on the readHeaderTimeout and
  132. ///readImageTimeout parameters.
  133. class ReadTimeoutException implements Exception {}
  134. ///Raised when the Http operation already closed.
  135. class AlreadyClosedException implements Exception {}
  136. ///Only suport read for now.
  137. ///Not suport carotid3d for now.
  138. class HttpVidUsImageData {
  139. late VidUsDataHttpReader _reader;
  140. late List<int> _imagePositionList;
  141. late int _version;
  142. late int _imageCount;
  143. late VidUsProbe _probe;
  144. late VidUsImageFormat _imageFormat;
  145. late Uint8List _extendedData;
  146. late int _readHeaderTimeout;
  147. late int _readImageTimeout;
  148. late bool _initialized;
  149. late bool _closed;
  150. final Duration delayDuration = const Duration(milliseconds: 10);
  151. final String header = "VINNO IMAGE DATA";
  152. /// Gets the version of this image data.
  153. int get version => _version;
  154. /// Gets the image count of this image data.
  155. int get imageCount => _imageCount;
  156. /// Gets the probe information.
  157. VidUsProbe get probe => _probe;
  158. /// Gets the image format of this image data.
  159. VidUsImageFormat get imageFormat => _imageFormat;
  160. /// Gets the extended data.
  161. Uint8List get extendedData => _extendedData;
  162. /// Gets the initialized state.
  163. bool get initialized => _initialized;
  164. /// Create a VINNO Image Data.
  165. /// [readHeaderTimeout] is the timeout value of reading header and probe information etc..., we need give as much time as possible
  166. /// to let the reader read the header.
  167. /// [readImageTimeout] is the timeout value of reading one frame.
  168. /// [minChunkSize] The min download chunk size, set to a large value may speedup the download speed, but will cause the progress not smooth.
  169. HttpVidUsImageData(String url,
  170. {DownloadCallback? downloadCallback,
  171. int minChunkSize = 65536,
  172. int readHeaderTimeout = 3000,
  173. int readImageTimeout = 500}) {
  174. _initialized = false;
  175. _closed = false;
  176. _readHeaderTimeout = readHeaderTimeout;
  177. _readImageTimeout = readImageTimeout;
  178. _reader = VidUsDataHttpReader(url,
  179. downloadCallback: downloadCallback, minChunkSize: minChunkSize);
  180. }
  181. ///Get the downloaded data of this HttpVidUsImageData
  182. Uint8List getDownloadedData() {
  183. return _reader.toBytes();
  184. }
  185. ///Initialize the HttpVidUsImageData, must be called firstly.
  186. Future initialize() async {
  187. if (!_initialized) {
  188. var header = await _readHeader();
  189. if (header != header) {
  190. throw Exception("The input data is not a VID data.");
  191. }
  192. _version = await _readVersion();
  193. //Get probe info
  194. _probe = await _readProbe();
  195. _imageFormat = await _readImageFormat();
  196. _extendedData = await _readExtendedData();
  197. _imagePositionList = [];
  198. var imagePositionListData = await _readImagePositionListData();
  199. _imageCount = imagePositionListData.length ~/ 8;
  200. var imagePositionReader = VidUsDataReader(imagePositionListData);
  201. for (var i = 0; i < _imageCount; i++) {
  202. _imagePositionList.add(imagePositionReader.readInt64V2());
  203. }
  204. _initialized = true;
  205. }
  206. }
  207. ///Close the HttpVidUsImageData, it will force close the download operation and reading operation.
  208. void close() {
  209. _closed = true;
  210. _reader.close();
  211. }
  212. ///Read header from http data.
  213. Future<String> _readHeader() async {
  214. var timeout = 0;
  215. while (!_closed) {
  216. try {
  217. var header = _reader.readString();
  218. return header;
  219. } catch (ex) {
  220. if (ex is NotReadyException) {
  221. if (timeout >= _readHeaderTimeout) {
  222. throw ReadTimeoutException();
  223. }
  224. await Future.delayed(delayDuration);
  225. timeout += delayDuration.inMilliseconds;
  226. } else {
  227. rethrow;
  228. }
  229. }
  230. }
  231. throw AlreadyClosedException();
  232. }
  233. ///Read the version from http data.
  234. Future<int> _readVersion() async {
  235. var timeout = 0;
  236. while (!_closed) {
  237. try {
  238. var version = _reader.readInt();
  239. return version;
  240. } catch (ex) {
  241. if (ex is NotReadyException) {
  242. if (timeout >= _readHeaderTimeout) {
  243. throw ReadTimeoutException();
  244. }
  245. await Future.delayed(delayDuration);
  246. timeout += delayDuration.inMilliseconds;
  247. } else {
  248. rethrow;
  249. }
  250. }
  251. }
  252. throw AlreadyClosedException();
  253. }
  254. ///Read the probe info from http data.
  255. Future<VidUsProbe> _readProbe() async {
  256. var timeout = 0;
  257. while (!_closed) {
  258. try {
  259. var probeData = _reader.readBytes();
  260. return VidUsProbe.fromBytes(probeData);
  261. } catch (ex) {
  262. if (ex is NotReadyException) {
  263. if (timeout >= _readHeaderTimeout) {
  264. throw ReadTimeoutException();
  265. }
  266. await Future.delayed(delayDuration);
  267. timeout += delayDuration.inMilliseconds;
  268. } else {
  269. rethrow;
  270. }
  271. }
  272. }
  273. throw AlreadyClosedException();
  274. }
  275. ///Read the image format from http data.
  276. Future<VidUsImageFormat> _readImageFormat() async {
  277. var timeout = 0;
  278. while (!_closed) {
  279. try {
  280. return VidUsImageFormat.values[_reader.readInt()];
  281. } catch (ex) {
  282. if (ex is NotReadyException) {
  283. if (timeout >= _readHeaderTimeout) {
  284. throw ReadTimeoutException();
  285. }
  286. await Future.delayed(delayDuration);
  287. timeout += delayDuration.inMilliseconds;
  288. } else {
  289. rethrow;
  290. }
  291. }
  292. }
  293. throw AlreadyClosedException();
  294. }
  295. ///Read extended data from http data.
  296. Future<Uint8List> _readExtendedData() async {
  297. var timeout = 0;
  298. while (!_closed) {
  299. try {
  300. return _reader.readBytes();
  301. } catch (ex) {
  302. if (ex is NotReadyException) {
  303. if (timeout >= _readHeaderTimeout) {
  304. throw ReadTimeoutException();
  305. }
  306. await Future.delayed(delayDuration);
  307. timeout += delayDuration.inMilliseconds;
  308. } else {
  309. rethrow;
  310. }
  311. }
  312. }
  313. throw AlreadyClosedException();
  314. }
  315. ///Read the image positions data from http data.
  316. Future<Uint8List> _readImagePositionListData() async {
  317. var timeout = 0;
  318. while (!_closed) {
  319. try {
  320. return _reader.readBytes();
  321. } catch (ex) {
  322. if (ex is NotReadyException) {
  323. if (timeout >= _readHeaderTimeout) {
  324. throw ReadTimeoutException();
  325. }
  326. await Future.delayed(delayDuration);
  327. timeout += delayDuration.inMilliseconds;
  328. } else {
  329. rethrow;
  330. }
  331. }
  332. }
  333. throw AlreadyClosedException();
  334. }
  335. /// Get one image from the vid.
  336. Future<VidUsImage> getImage(int index) async {
  337. if (index >= _imageCount || index < 0) {
  338. throw Exception("Can not find image Data");
  339. }
  340. //Jump to image.
  341. var timeout = 0;
  342. while (!_closed) {
  343. try {
  344. var imageData = _reader.readBytes(_imagePositionList[index]);
  345. return VidUsImage.fromBytes(imageData);
  346. } catch (ex) {
  347. if (ex is NotReadyException) {
  348. if (timeout >= _readImageTimeout) {
  349. throw ReadTimeoutException();
  350. }
  351. await Future.delayed(delayDuration);
  352. timeout += delayDuration.inMilliseconds;
  353. } else {
  354. rethrow;
  355. }
  356. }
  357. }
  358. throw AlreadyClosedException();
  359. }
  360. }