123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- import 'dart:typed_data';
- import 'package:vid/us/vid_us_data_http_reader.dart';
- import 'package:vid/us/vid_us_data_reader.dart';
- import 'package:vid/us/vid_us_image.dart';
- import 'package:vid/us/vid_us_probe.dart';
- enum VidUsImageFormat {
- Jpeg,
- Png,
- H264,
- Zip,
- Diff,
- }
- class VidUsImageData {
- late VidUsDataReader _reader;
- late List<int> _imagePositionList;
- late int _version;
- late int _imageCount;
- late VidUsProbe _probe;
- late VidUsImageFormat _imageFormat;
- late Uint8List _extendedData;
- final String header = "VINNO IMAGE DATA";
-
- int get version => _version;
-
- int get imageCount => _imageCount;
-
- VidUsProbe get probe => _probe;
-
- VidUsImageFormat get imageFormat => _imageFormat;
-
- Uint8List get extendedData => _extendedData;
-
- VidUsImageData(Uint8List data) {
- _reader = VidUsDataReader(data);
- var header = _reader.readString();
- if (header != header) {
- throw Exception("The input data is not a VID data.");
- }
- _version = _reader.readInt();
-
- var probeData = _reader.readBytes();
- _probe = VidUsProbe.fromBytes(probeData);
- _imageFormat = VidUsImageFormat.values[_reader.readInt()];
- _extendedData = _reader.readBytes();
- _imagePositionList = [];
- var imagePositionListData = _reader.readBytes();
- _imageCount = imagePositionListData.length ~/ 8;
- var imagePositionReader = VidUsDataReader(imagePositionListData);
- for (var i = 0; i < _imageCount; i++) {
- _imagePositionList.add(imagePositionReader.readInt64V2());
- }
- }
-
- VidUsImage getImage(int index) {
- if (index >= _imageCount || index < 0) {
- throw Exception("Can not find image Data");
- }
-
- var imageData = _reader.readBytes(_imagePositionList[index]);
- return VidUsImage.fromBytes(imageData);
- }
- }
- class ReadTimeoutException implements Exception {}
- class AlreadyClosedException implements Exception {}
- class HttpVidUsImageData {
- late VidUsDataHttpReader _reader;
- late List<int> _imagePositionList;
- late int _version;
- late int _imageCount;
- late VidUsProbe _probe;
- late VidUsImageFormat _imageFormat;
- late Uint8List _extendedData;
- late int _readHeaderTimeout;
- late int _readImageTimeout;
- late bool _initialized;
- late bool _closed;
- final Duration delayDuration = const Duration(milliseconds: 10);
- final String header = "VINNO IMAGE DATA";
-
- int get version => _version;
-
- int get imageCount => _imageCount;
-
- VidUsProbe get probe => _probe;
-
- VidUsImageFormat get imageFormat => _imageFormat;
-
- Uint8List get extendedData => _extendedData;
-
- bool get initialized => _initialized;
-
-
-
-
-
- HttpVidUsImageData(String url, {DownloadCallback? downloadCallback, int minChunkSize = 65536, int readHeaderTimeout = 3000, int readImageTimeout = 500}) {
- _initialized = false;
- _closed = false;
- _readHeaderTimeout = readHeaderTimeout;
- _readImageTimeout = readImageTimeout;
- _reader = VidUsDataHttpReader(url, downloadCallback: downloadCallback, minChunkSize: minChunkSize);
- }
-
- Uint8List getDownloadedData() {
- return _reader.toBytes();
- }
-
- Future initialize() async {
- if (!_initialized) {
- var header = await _readHeader();
- if (header != header) {
- throw Exception("The input data is not a VID data.");
- }
- _version = await _readVersion();
-
- _probe = await _readProbe();
- _imageFormat = await _readImageFormat();
- _extendedData = await _readExtendedData();
- _imagePositionList = [];
- var imagePositionListData = await _readImagePositionListData();
- _imageCount = imagePositionListData.length ~/ 8;
- var imagePositionReader = VidUsDataReader(imagePositionListData);
- for (var i = 0; i < _imageCount; i++) {
- _imagePositionList.add(imagePositionReader.readInt64V2());
- }
- _initialized = true;
- }
- }
-
- void close() {
- _closed = true;
- _reader.close();
- }
-
- Future<String> _readHeader() async {
- var timeout = 0;
- while (!_closed) {
- try {
- var header = _reader.readString();
- return header;
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<int> _readVersion() async {
- var timeout = 0;
- while (!_closed) {
- try {
- var version = _reader.readInt();
- return version;
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<VidUsProbe> _readProbe() async {
- var timeout = 0;
- while (!_closed) {
- try {
- var probeData = _reader.readBytes();
- return VidUsProbe.fromBytes(probeData);
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<VidUsImageFormat> _readImageFormat() async {
- var timeout = 0;
- while (!_closed) {
- try {
- return VidUsImageFormat.values[_reader.readInt()];
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<Uint8List> _readExtendedData() async {
- var timeout = 0;
- while (!_closed) {
- try {
- return _reader.readBytes();
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<Uint8List> _readImagePositionListData() async {
- var timeout = 0;
- while (!_closed) {
- try {
- return _reader.readBytes();
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readHeaderTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
-
- Future<VidUsImage> getImage(int index) async {
- if (index >= _imageCount || index < 0) {
- throw Exception("Can not find image Data");
- }
-
- var timeout = 0;
- while (!_closed) {
- try {
- var imageData = _reader.readBytes(_imagePositionList[index]);
- return VidUsImage.fromBytes(imageData);
- } catch (ex) {
- if (ex is NotReadyException) {
- if (timeout >= _readImageTimeout) {
- throw ReadTimeoutException();
- }
- await Future.delayed(delayDuration);
- timeout += delayDuration.inMilliseconds;
- } else {
- rethrow;
- }
- }
- }
- throw AlreadyClosedException();
- }
- }
|