Preview image versions before sending, and repair unheard chunks
Send sheet: opens on what recipients will see (the encoded photo decoded on this device) instead of the original. "Try another version" encodes the whole photo again with a near-invisible zoom/shift; the generative decoder reconstructs each version differently, and on Kodak the best of three raised CLIP agreement from 0.828 to 0.853. The user picks the version to send; packet count and airtime follow the selection. Version 1 is byte-identical to the previous encode, so the wire format is unchanged. The sender's bubble shows the chosen version and carries the AI-reconstructed label. Repeater echoes: the raw RX log handler now decrypts GRP_DATA as well as GRP_TXT and counts repeaters re-flooding each chunk of our own images. The bubble shows the repeat count like channel text, and Packet info lists each packet. About 15 s after sending, chunks no repeater echoed are resent once, but only when another chunk of the same image was echoed, so senders with no repeater in earshot do not double their airtime. Outgoing entries now record their payload size.
This commit is contained in:
1 parent
aafd541b0f
commit
53e4e457ca
36 files changed
+2366
-150
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/models/channel.dart';
|
||||
import 'package:meshcore_open/review_mode/review_radio.dart';
|
||||
import 'package:meshcore_open/review_mode/review_script.dart';
|
||||
import 'package:meshcore_open/services/image_chunk_transport.dart';
|
||||
import 'package:meshcore_open/services/message_retry_service.dart';
|
||||
import 'package:meshcore_open/services/path_history_service.dart';
|
||||
import 'package:meshcore_open/services/storage_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/widgets/image_send_codec_binding.dart';
|
||||
import 'package:pointycastle/export.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Future<void> _waitFor(bool Function() condition, {int seconds = 8}) async {
|
||||
final end = DateTime.now().add(Duration(seconds: seconds));
|
||||
while (!condition()) {
|
||||
if (DateTime.now().isAfter(end)) fail('Timed out waiting for condition');
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
}
|
||||
}
|
||||
|
||||
Uint8List _psk(int slot) =>
|
||||
Channel.parsePskHex(ReviewScript.channels[slot].pskHex);
|
||||
|
||||
/// A raw-log frame for a repeater re-flooding [blob] as GRP_DATA on [psk]'s
|
||||
/// channel, encrypted the way the firmware's createGroupDatagram does.
|
||||
Uint8List _logRxGroupData(Uint8List psk, Uint8List blob, {int hops = 1}) {
|
||||
final plain = <int>[
|
||||
dataTypeAeicImage & 0xFF,
|
||||
dataTypeAeicImage >> 8,
|
||||
blob.length,
|
||||
...blob,
|
||||
];
|
||||
final padded = Uint8List(((plain.length + 15) ~/ 16) * 16)
|
||||
..setRange(0, plain.length, plain);
|
||||
final cipher = ECBBlockCipher(AESEngine())..init(true, KeyParameter(psk));
|
||||
final encrypted = Uint8List(padded.length);
|
||||
for (var i = 0; i < padded.length; i += 16) {
|
||||
cipher.processBlock(padded, i, encrypted, i);
|
||||
}
|
||||
final key32 = Uint8List(32)..setRange(0, 16, psk);
|
||||
final mac = crypto.Hmac(crypto.sha256, key32).convert(encrypted).bytes;
|
||||
final channelHash = crypto.sha256.convert(psk).bytes[0];
|
||||
return Uint8List.fromList([
|
||||
pushCodeLogRxData,
|
||||
40, // snr
|
||||
0xA0, // rssi
|
||||
0x19, // flood route, GRP_DATA payload
|
||||
hops, // path length: one 1-byte hash per hop
|
||||
for (var i = 0; i < hops; i++) 0x42 + i,
|
||||
channelHash,
|
||||
mac[0],
|
||||
mac[1],
|
||||
...encrypted,
|
||||
]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
late MeshCoreConnector connector;
|
||||
|
||||
// One connector for the file: the notification service it starts accepts a
|
||||
// single listener per process.
|
||||
setUpAll(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
connector = MeshCoreConnector();
|
||||
final retryService = MessageRetryService();
|
||||
connector.initialize(
|
||||
retryService: retryService,
|
||||
pathHistoryService: PathHistoryService(StorageService()),
|
||||
);
|
||||
final radio = ReviewRadio(
|
||||
scheduledMessageInterval: const Duration(hours: 1),
|
||||
ackDelay: const Duration(milliseconds: 50),
|
||||
replyDelay: const Duration(hours: 1),
|
||||
);
|
||||
connector.reviewRadioFactoryForTest = () => radio;
|
||||
await connector.connectReview();
|
||||
await _waitFor(() => connector.channelMapConfirmed);
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
await connector.disconnect();
|
||||
connector.dispose();
|
||||
});
|
||||
|
||||
Future<ImageChunkSet> sendImage({
|
||||
required int slot,
|
||||
required int imgId,
|
||||
}) async {
|
||||
final chunkSet = buildImageChunks(
|
||||
payload: Uint8List.fromList(List<int>.generate(200, (i) => i & 0xFF)),
|
||||
metadata: const ImageStreamMetadata(rate: ImageCodecRatePoint.standard),
|
||||
senderPrefix: senderPrefixFromKey(connector.selfPublicKey)!,
|
||||
imgId: imgId,
|
||||
);
|
||||
expect(
|
||||
await connector.sendImageChunks(
|
||||
chunkSet.blobs,
|
||||
channelIndex: slot,
|
||||
interChunkDelay: Duration.zero,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
return chunkSet;
|
||||
}
|
||||
|
||||
test('a repeater echo of our own chunk is counted for that chunk', () async {
|
||||
final chunkSet = await sendImage(slot: 0, imgId: 77);
|
||||
expect(chunkSet.blobs.length, 3, reason: '2 data chunks + parity');
|
||||
expect(connector.ownImageEchoes(0, 77), [0, 0, 0]);
|
||||
|
||||
connector.handleFrameForTest(_logRxGroupData(_psk(0), chunkSet.blobs[1]));
|
||||
connector.handleFrameForTest(_logRxGroupData(_psk(0), chunkSet.blobs[1]));
|
||||
connector.handleFrameForTest(_logRxGroupData(_psk(0), chunkSet.blobs[2]));
|
||||
|
||||
expect(connector.ownImageEchoes(0, 77), [0, 2, 1]);
|
||||
});
|
||||
|
||||
test('chunks from someone else, or on another channel key, are not '
|
||||
'counted', () async {
|
||||
final chunkSet = await sendImage(slot: 0, imgId: 78);
|
||||
|
||||
// Same image id, different sender.
|
||||
final foreign = Uint8List.fromList(chunkSet.blobs[0]);
|
||||
foreign[0] ^= 0xFF;
|
||||
connector.handleFrameForTest(_logRxGroupData(_psk(0), foreign));
|
||||
// Our chunk, but encrypted for a different channel than it was sent on.
|
||||
connector.handleFrameForTest(_logRxGroupData(_psk(1), chunkSet.blobs[0]));
|
||||
|
||||
expect(connector.ownImageEchoes(0, 78), [0, 0, 0]);
|
||||
});
|
||||
}
|
||||
@@ -826,6 +826,73 @@ void main() {
|
||||
expect(h.decoder.calls, 0);
|
||||
});
|
||||
|
||||
test('a decoded preview is labelled synthesized and keeps its byte count '
|
||||
'and echoes', () async {
|
||||
final h = _build();
|
||||
final entry = await h.store.registerOutgoing(
|
||||
channelIndex: 1,
|
||||
senderPrefix: kSelf,
|
||||
imgId: 4,
|
||||
previewPng: Uint8List.fromList(List<int>.filled(64, 1)),
|
||||
rate: AeicRatePoint.ft32,
|
||||
chunkCount: 2,
|
||||
synthesized: true,
|
||||
payloadBytes: 171,
|
||||
chunkEchoes: const [1, 0, 2],
|
||||
);
|
||||
expect(entry.synthesized, isTrue);
|
||||
expect(entry.bitstreamByteCount, 171);
|
||||
expect(entry.bitstreamStored, isFalse);
|
||||
expect(entry.minDataChunkEchoes, 0);
|
||||
|
||||
final restored = ReceivedImageEntry.fromJson(entry.toJson());
|
||||
expect(restored.synthesized, isTrue);
|
||||
expect(restored.chunkEchoes, [1, 0, 2]);
|
||||
});
|
||||
|
||||
test(
|
||||
'setOutgoingEchoes updates the newest matching outgoing image',
|
||||
() async {
|
||||
final h = _build();
|
||||
final older = await h.store.registerOutgoing(
|
||||
channelIndex: 1,
|
||||
senderPrefix: kSelf,
|
||||
imgId: 9,
|
||||
previewPng: Uint8List.fromList(List<int>.filled(64, 1)),
|
||||
rate: AeicRatePoint.ft32,
|
||||
chunkCount: 1,
|
||||
);
|
||||
h.advance(const Duration(seconds: 5));
|
||||
final newer = await h.store.registerOutgoing(
|
||||
channelIndex: 1,
|
||||
senderPrefix: kSelf,
|
||||
imgId: 9,
|
||||
previewPng: Uint8List.fromList(List<int>.filled(64, 2)),
|
||||
rate: AeicRatePoint.ft32,
|
||||
chunkCount: 1,
|
||||
);
|
||||
|
||||
await h.store.setOutgoingEchoes(
|
||||
channelIndex: 1,
|
||||
senderPrefix: kSelf,
|
||||
imgId: 9,
|
||||
echoes: const [3, 1],
|
||||
);
|
||||
expect(h.store.entryFor(newer.streamId)!.chunkEchoes, [3, 1]);
|
||||
expect(h.store.entryFor(newer.streamId)!.minDataChunkEchoes, 3);
|
||||
expect(h.store.entryFor(older.streamId)!.chunkEchoes, isNull);
|
||||
|
||||
// Another channel's image with the same id is left alone.
|
||||
await h.store.setOutgoingEchoes(
|
||||
channelIndex: 2,
|
||||
senderPrefix: kSelf,
|
||||
imgId: 9,
|
||||
echoes: const [7, 7],
|
||||
);
|
||||
expect(h.store.entryFor(newer.streamId)!.chunkEchoes, [3, 1]);
|
||||
},
|
||||
);
|
||||
|
||||
test('spent outgoing entries count toward maxEntries', () async {
|
||||
final h = _build(maxEntries: 2, maxAge: const Duration(minutes: 10));
|
||||
final ids = <String>[];
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/widgets/image_send_codec_binding.dart';
|
||||
|
||||
void main() {
|
||||
group('rerollSourceRect', () {
|
||||
const sizes = [
|
||||
(4000, 3000),
|
||||
(3000, 4000),
|
||||
(1920, 1080),
|
||||
(512, 512),
|
||||
(7, 3),
|
||||
];
|
||||
|
||||
test('variant 0 is the whole photo', () {
|
||||
for (final (w, h) in sizes) {
|
||||
final r = rerollSourceRect(w, h, 0);
|
||||
expect(r.left, 0);
|
||||
expect(r.top, 0);
|
||||
expect(r.width, w);
|
||||
expect(r.height, h);
|
||||
}
|
||||
});
|
||||
|
||||
test('every variant stays inside the photo and keeps its shape', () {
|
||||
for (final (w, h) in sizes) {
|
||||
for (var v = 0; v < kImageSendMaxVersions; v++) {
|
||||
final r = rerollSourceRect(w, h, v);
|
||||
expect(r.left, greaterThanOrEqualTo(0), reason: '$w x $h v$v');
|
||||
expect(r.top, greaterThanOrEqualTo(0), reason: '$w x $h v$v');
|
||||
expect(r.left + r.width, lessThanOrEqualTo(w + 1e-9));
|
||||
expect(r.top + r.height, lessThanOrEqualTo(h + 1e-9));
|
||||
// Same aspect, so the version keeps the photo's aspect code.
|
||||
expect(r.width / r.height, closeTo(w / h, (w / h) * 0.01));
|
||||
// "The whole photo": at most a few percent trimmed.
|
||||
expect(r.width, greaterThanOrEqualTo(w * 0.95));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('variants differ from each other and are deterministic', () {
|
||||
final rects = [
|
||||
for (var v = 0; v < kImageSendMaxVersions; v++)
|
||||
rerollSourceRect(4000, 3000, v),
|
||||
];
|
||||
expect(rects.toSet().length, kImageSendMaxVersions);
|
||||
expect(rerollSourceRect(4000, 3000, 3), rects[3]);
|
||||
});
|
||||
|
||||
test('rejects a variant outside the table', () {
|
||||
expect(() => rerollSourceRect(10, 10, -1), throwsRangeError);
|
||||
expect(
|
||||
() => rerollSourceRect(10, 10, kImageSendMaxVersions),
|
||||
throwsRangeError,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -336,4 +336,145 @@ void main() {
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
expect(_texts(tester), contains(l10n.imageSend_codecUnavailable));
|
||||
});
|
||||
|
||||
group('versions', () {
|
||||
final previewCodec = FakeImageSendCodec(
|
||||
latency: Duration.zero,
|
||||
previewPng: _onePixelPng,
|
||||
);
|
||||
|
||||
Future<void> tapText(WidgetTester tester, String text) async {
|
||||
final finder = find.text(text);
|
||||
await tester.tap(finder);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Future<ImageSendPreviewResult?> send(
|
||||
WidgetTester tester,
|
||||
ImageSendPreviewResult? Function() result,
|
||||
) async {
|
||||
await tester.tap(find.byType(FilledButton));
|
||||
await tester.pumpAndSettle();
|
||||
return result();
|
||||
}
|
||||
|
||||
testWidgets('opens on what recipients see, as Version 1', (tester) async {
|
||||
_tallScreen(tester);
|
||||
await _openSheet(tester, radio: _knownRadio, codec: previewCodec);
|
||||
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
final texts = _texts(tester);
|
||||
expect(texts, contains(l10n.imageSend_versionLabel(1)));
|
||||
expect(texts, contains(l10n.imageSend_tryAnother));
|
||||
expect(texts, isNot(contains(l10n.imageSend_reconstructionUnavailable)));
|
||||
final segments = tester.widget<SegmentedButton<bool>>(
|
||||
find.byType(SegmentedButton<bool>),
|
||||
);
|
||||
expect(segments.selected, {
|
||||
false,
|
||||
}, reason: 'the reconstruction, not the original, is the default view');
|
||||
});
|
||||
|
||||
testWidgets('trying another version selects it and sends its payload and '
|
||||
'reconstruction', (tester) async {
|
||||
_tallScreen(tester);
|
||||
ImageSendPreviewResult? result;
|
||||
await tester.pumpWidget(_host(previewCodec, (r) => result = r));
|
||||
await tester.tap(find.text('open'));
|
||||
await tester.pumpAndSettle();
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
|
||||
await tapText(tester, l10n.imageSend_tryAnother);
|
||||
expect(_texts(tester), contains(l10n.imageSend_versionLabel(2)));
|
||||
|
||||
final sent = await send(tester, () => result);
|
||||
expect(
|
||||
sent!.payload.length,
|
||||
FakeImageSendCodec.payloadBytesFor(kImageSendRatePoint, 1),
|
||||
);
|
||||
expect(sent.previewPng, isNotNull);
|
||||
});
|
||||
|
||||
testWidgets('going back to Version 1 sends Version 1', (tester) async {
|
||||
_tallScreen(tester);
|
||||
ImageSendPreviewResult? result;
|
||||
await tester.pumpWidget(_host(previewCodec, (r) => result = r));
|
||||
await tester.tap(find.text('open'));
|
||||
await tester.pumpAndSettle();
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
|
||||
await tapText(tester, l10n.imageSend_tryAnother);
|
||||
await tapText(tester, l10n.imageSend_versionLabel(1));
|
||||
|
||||
final sent = await send(tester, () => result);
|
||||
expect(
|
||||
sent!.payload.length,
|
||||
FakeImageSendCodec.payloadBytesFor(kImageSendRatePoint, 0),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('stops offering versions at the maximum', (tester) async {
|
||||
_tallScreen(tester);
|
||||
await _openSheet(tester, radio: _knownRadio, codec: previewCodec);
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
|
||||
for (var i = 1; i < kImageSendMaxVersions; i++) {
|
||||
await tapText(tester, l10n.imageSend_tryAnother);
|
||||
}
|
||||
final texts = _texts(tester);
|
||||
expect(
|
||||
texts,
|
||||
contains(l10n.imageSend_versionLabel(kImageSendMaxVersions)),
|
||||
);
|
||||
expect(texts, isNot(contains(l10n.imageSend_tryAnother)));
|
||||
});
|
||||
|
||||
testWidgets('a device that cannot decode shows the original and no '
|
||||
'versions', (tester) async {
|
||||
_tallScreen(tester);
|
||||
await _openSheet(tester, radio: _knownRadio);
|
||||
|
||||
final l10n = await AppLocalizations.delegate.load(const Locale('en'));
|
||||
final texts = _texts(tester);
|
||||
expect(texts, contains(l10n.imageSend_reconstructionUnavailable));
|
||||
expect(texts, isNot(contains(l10n.imageSend_versionLabel(1))));
|
||||
expect(texts, isNot(contains(l10n.imageSend_tryAnother)));
|
||||
expect(find.byType(SegmentedButton<bool>), findsNothing);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Room for the whole sheet, so version tiles and "Try another version" are
|
||||
/// on screen without scrolling the draggable sheet.
|
||||
void _tallScreen(WidgetTester tester) {
|
||||
tester.view.physicalSize = const Size(1000, 2400);
|
||||
tester.view.devicePixelRatio = 1.0;
|
||||
addTearDown(tester.view.reset);
|
||||
}
|
||||
|
||||
/// A host app whose button opens the sheet and reports what it returned.
|
||||
Widget _host(
|
||||
ImageSendCodec codec,
|
||||
void Function(ImageSendPreviewResult?) onResult,
|
||||
) => MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Builder(
|
||||
builder: (context) => Scaffold(
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: () async => onResult(
|
||||
await showImageSendPreviewSheet(
|
||||
context: context,
|
||||
imageBytes: _onePixelPng,
|
||||
originalFileBytes: 1024,
|
||||
codec: codec,
|
||||
radio: _knownRadio,
|
||||
),
|
||||
),
|
||||
child: const Text('open'),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
Reference in new issue
Block a user