Skip to content

Commit 12e6ed6

Browse files
klopez4212Terp
andcommitted
fix(mobile): open videos without waiting for remote posters
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz> Signed-off-by: kenny lopez <[email protected]>
1 parent fb09f9c commit 12e6ed6

4 files changed

Lines changed: 159 additions & 116 deletions

File tree

‎mobile/ios/NativeControlTests/NativePhotoZoomTests.swift‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ struct NativePhotoZoomTests {
102102
precondition(!viewer.shouldBeginDismissal(location: CGPoint(x: 195, y: 400), velocity: CGVector(dx: 300, dy: 0)), "Video horizontal scrubbing must not dismiss")
103103
let first = pager.viewControllers!.first!
104104
first.loadViewIfNeeded()
105+
let posterView = descendants(first.view).compactMap { $0 as? UIImageView }.first!
106+
let oldPoster = posterView.image
107+
viewer.updateFrame(data, at: 0)
108+
precondition(posterView.image !== oldPoster, "A late poster updates the selected video")
109+
let selectedPoster = posterView.image
110+
viewer.updateFrame(data, at: 2)
111+
precondition(posterView.image === selectedPoster, "A stale poster cannot replace the selected video")
105112
let playerController = first.children.compactMap { $0 as? AVPlayerViewController }.first!
106113
let player = playerController.player!
107114
let item = player.currentItem!

‎mobile/ios/Runner/NativePhotoZoomCoordinator.swift‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ private final class NativePhotoZoomViewController: UIViewController, UIPageViewC
360360
// Flutter decodes authenticated originals and forwards continuing frames.
361361
// Ignore old pages and closed presentations before allocating a UIImage.
362362
func updateFrame(_ data: Data, at value: Int) {
363-
guard !finishing, !videoIndices.contains(value), value == index, let image = UIImage(data: data) else { return }
364-
images[value] = image
363+
guard !finishing, value == index, let image = UIImage(data: data) else { return }
364+
if videoIndices.contains(value) { previews[value] = image }
365+
else { images[value] = image }
365366
for page in pager.children.compactMap({ $0 as? NativePhotoPage }) where page.index == value {
366367
page.setImage(image)
367368
}

‎mobile/lib/features/channels/native_photo_zoom_preview.dart‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ class NativePhotoZoomPreview {
9595
});
9696
window = loads;
9797
final initialPoster = previewProvider ?? previewProviders?[initialIndex];
98-
final bytes = videoIndices.contains(initialIndex)
99-
? (initialPoster == null
100-
? null
101-
: await loads.load(
102-
initialIndex,
103-
initialPoster,
104-
preview: true,
105-
)) ??
98+
final isVideo = videoIndices.contains(initialIndex);
99+
final posterLoad = isVideo && initialPoster != null
100+
? loads.load(initialIndex, initialPoster, preview: true)
101+
: null;
102+
// Only wait for PNG encoding when the poster was already decoded.
103+
// Network-backed posters must never delay opening playable video.
104+
final cachedPoster =
105+
loads.loads[(initialIndex, true)]?.receivedFrame == true;
106+
final bytes = isVideo
107+
? (cachedPoster ? await posterLoad : null) ??
106108
await _snapshot(sourceKeys[initialIndex])
107109
: await loads.load(
108110
initialIndex,
109-
previewProvider ??
110-
previewProviders?[initialIndex] ??
111-
providers[initialIndex],
111+
initialPoster ?? providers[initialIndex],
112112
preview: true,
113113
);
114114
if (bytes == null) return true;
@@ -161,7 +161,7 @@ class NativePhotoZoomPreview {
161161
}
162162
return null;
163163
});
164-
final result = await channel.invokeMethod<String>('present', {
164+
final presentation = channel.invokeMethod<String>('present', {
165165
...geometry,
166166
'bytes': bytes,
167167
'session': session,
@@ -173,6 +173,20 @@ class NativePhotoZoomPreview {
173173
'canDelete': actions?.canDelete ?? false,
174174
'reduceMotion': reduceMotion,
175175
});
176+
if (posterLoad != null && !cachedPoster) {
177+
unawaited(
178+
posterLoad
179+
.then((poster) async {
180+
if (poster != null &&
181+
!loads.disposed &&
182+
loads.index == initialIndex) {
183+
await loads.onFrame(initialIndex, poster);
184+
}
185+
})
186+
.catchError((Object _) {}),
187+
);
188+
}
189+
final result = await presentation;
176190
if (result == 'reply' && context.mounted) onReply?.call();
177191
return result == null || result == 'unavailable';
178192
} catch (_) {

‎mobile/test/features/channels/native_photo_zoom_preview_test.dart‎

Lines changed: 123 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22
import 'dart:async';
33
import 'dart:io';
44
import 'dart:ui' as ui;
5+
56
import 'package:flutter/services.dart';
67

78
import 'package:buzz/features/channels/native_photo_zoom_preview.dart';
@@ -483,118 +484,138 @@ void main() {
483484
debugDefaultTargetPlatformOverride = null;
484485
}, skip: !NativePhotoZoomPreview.enabled);
485486

486-
for (final usePoster in [false, true]) {
487-
testWidgets(
488-
'video uses ${usePoster ? 'poster' : 'snapshot'} and only loads the selected video',
489-
(tester) async {
490-
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
491-
final key = GlobalKey();
492-
late BuildContext context;
493-
await tester.pumpWidget(
494-
MaterialApp(
495-
home: Builder(
496-
builder: (value) {
497-
context = value;
498-
return Center(
499-
child: RepaintBoundary(
500-
key: key,
501-
child: const SizedBox(
502-
width: 100,
503-
height: 100,
504-
child: ColoredBox(color: Colors.black),
505-
),
487+
for (final posterMode in ['none', 'cached', 'pending']) {
488+
final usePoster = posterMode != 'none';
489+
testWidgets('video uses $posterMode poster without delaying presentation', (
490+
tester,
491+
) async {
492+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
493+
final key = GlobalKey();
494+
late BuildContext context;
495+
await tester.pumpWidget(
496+
MaterialApp(
497+
home: Builder(
498+
builder: (value) {
499+
context = value;
500+
return Center(
501+
child: RepaintBoundary(
502+
key: key,
503+
child: const SizedBox(
504+
width: 100,
505+
height: 100,
506+
child: ColoredBox(color: Colors.black),
506507
),
507-
);
508-
},
509-
),
508+
),
509+
);
510+
},
510511
),
512+
),
513+
);
514+
final image = _PendingPreview();
515+
final pendingPoster = _PendingPreview();
516+
late Completer<void> posterUpdated;
517+
final videos = <int>[];
518+
const codec = StandardMethodCodec();
519+
Future<Object?> nativeCall(String method, int session, int index) async {
520+
final reply = Completer<Object?>();
521+
await messenger.handlePlatformMessage(
522+
NativePhotoZoomPreview.channel.name,
523+
codec.encodeMethodCall(
524+
MethodCall(method, {'session': session, 'index': index}),
525+
),
526+
(data) => reply.complete(codec.decodeEnvelope(data!)),
511527
);
512-
final image = _PendingPreview();
513-
final videos = <int>[];
514-
const codec = StandardMethodCodec();
515-
Future<Object?> nativeCall(
516-
String method,
517-
int session,
518-
int index,
519-
) async {
520-
final reply = Completer<Object?>();
521-
await messenger.handlePlatformMessage(
522-
NativePhotoZoomPreview.channel.name,
523-
codec.encodeMethodCall(
524-
MethodCall(method, {'session': session, 'index': index}),
525-
),
526-
(data) => reply.complete(codec.decodeEnvelope(data!)),
527-
);
528-
return reply.future;
529-
}
528+
return reply.future;
529+
}
530530

531-
messenger.setMockMethodCallHandler(NativePhotoZoomPreview.channel, (
532-
call,
533-
) async {
534-
if (call.method == 'isSupported') return true;
535-
final args = call.arguments as Map;
536-
expect(args['videoIndices'], [0, 2]);
537-
expect(args['bytes'], isA<Uint8List>());
538-
if (usePoster) {
539-
final codec = await ui.instantiateImageCodec(
540-
args['bytes'] as Uint8List,
541-
);
542-
final frame = await codec.getNextFrame();
543-
expect(
544-
frame.image.width,
545-
2,
546-
reason: 'Use the poster, not the 100-pixel source snapshot',
547-
);
548-
frame.image.dispose();
549-
codec.dispose();
550-
}
551-
final session = args['session'] as int;
552-
expect(await nativeCall('image', session, 0), isNull);
553-
expect(await nativeCall('video', session, 1), isNull);
554-
expect(await nativeCall('video', session, 2), isNull);
555-
expect(await nativeCall('video', session - 1, 0), isNull);
556-
expect(await nativeCall('video', session, 0), '/temporary/clip.mp4');
557-
await nativeCall('select', session, 2);
558-
expect(await nativeCall('video', session, 0), isNull);
559-
expect(await nativeCall('video', session, 2), '/temporary/clip.mp4');
560-
return 'closed';
561-
});
562-
await tester.runAsync(() async {
531+
messenger.setMockMethodCallHandler(NativePhotoZoomPreview.channel, (
532+
call,
533+
) async {
534+
if (call.method == 'isSupported') return true;
535+
if (call.method == 'imageFrame') {
536+
posterUpdated.complete();
537+
return null;
538+
}
539+
final args = call.arguments as Map;
540+
if (posterMode == 'pending') {
563541
expect(
564-
await NativePhotoZoomPreview.open(
565-
context: context,
566-
sourceKeys: [key, key, key],
567-
providers: [image, provider, image],
568-
previewProviders: usePoster
569-
? [provider, provider, provider]
570-
: null,
571-
urls: [
572-
'https://example.org/clip.mp4',
573-
'https://example.org/photo.jpg',
574-
'https://example.org/second.mov',
575-
],
576-
labels: ['Clip', 'Photo', 'Second'],
577-
initialIndex: 0,
578-
videoIndices: {0, 2},
579-
loadVideo: (index) async {
580-
videos.add(index);
581-
return '/temporary/clip.mp4';
582-
},
583-
onSelect: (_) async {},
584-
),
542+
pendingPoster.frame.isCompleted,
585543
isFalse,
544+
reason: 'Presentation must not wait for the remote poster',
586545
);
587-
});
588-
expect(videos, [0, 2]);
546+
final decoder = await ui.instantiateImageCodec(provider.bytes);
547+
pendingPoster.frame.complete(
548+
ImageInfo(image: (await decoder.getNextFrame()).image),
549+
);
550+
decoder.dispose();
551+
await posterUpdated.future.timeout(const Duration(seconds: 5));
552+
}
553+
expect(args['videoIndices'], [0, 2]);
554+
expect(args['bytes'], isA<Uint8List>());
555+
if (posterMode == 'cached') {
556+
final codec = await ui.instantiateImageCodec(
557+
args['bytes'] as Uint8List,
558+
);
559+
final frame = await codec.getNextFrame();
560+
expect(
561+
frame.image.width,
562+
2,
563+
reason: 'Use the poster, not the 100-pixel source snapshot',
564+
);
565+
frame.image.dispose();
566+
codec.dispose();
567+
}
568+
final session = args['session'] as int;
569+
expect(await nativeCall('image', session, 0), isNull);
570+
expect(await nativeCall('video', session, 1), isNull);
571+
expect(await nativeCall('video', session, 2), isNull);
572+
expect(await nativeCall('video', session - 1, 0), isNull);
573+
expect(await nativeCall('video', session, 0), '/temporary/clip.mp4');
574+
await nativeCall('select', session, 2);
575+
expect(await nativeCall('video', session, 0), isNull);
576+
expect(await nativeCall('video', session, 2), '/temporary/clip.mp4');
577+
return 'closed';
578+
});
579+
await tester.runAsync(() async {
580+
posterUpdated = Completer<void>();
581+
if (posterMode == 'cached') await precacheImage(provider, context);
589582
expect(
590-
image.started,
583+
await NativePhotoZoomPreview.open(
584+
context: context,
585+
sourceKeys: [key, key, key],
586+
providers: [image, provider, image],
587+
previewProviders: usePoster
588+
? [
589+
posterMode == 'pending' ? pendingPoster : provider,
590+
provider,
591+
provider,
592+
]
593+
: null,
594+
urls: [
595+
'https://example.org/clip.mp4',
596+
'https://example.org/photo.jpg',
597+
'https://example.org/second.mov',
598+
],
599+
labels: ['Clip', 'Photo', 'Second'],
600+
initialIndex: 0,
601+
videoIndices: {0, 2},
602+
loadVideo: (index) async {
603+
videos.add(index);
604+
return '/temporary/clip.mp4';
605+
},
606+
onSelect: (_) async {},
607+
),
591608
isFalse,
592-
reason: 'A video URL must never enter the image decoder',
593609
);
594-
debugDefaultTargetPlatformOverride = null;
595-
},
596-
skip: !NativePhotoZoomPreview.enabled,
597-
);
610+
});
611+
expect(videos, [0, 2]);
612+
expect(
613+
image.started,
614+
isFalse,
615+
reason: 'A video URL must never enter the image decoder',
616+
);
617+
debugDefaultTargetPlatformOverride = null;
618+
}, skip: !NativePhotoZoomPreview.enabled);
598619
}
599620

600621
testWidgets(

0 commit comments

Comments
 (0)