Skip to content

Commit 9047d60

Browse files
committed
MacVideoPlayer: Show decoding errors
1 parent d47bb2f commit 9047d60

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Meta/Lagom/Contrib/MacVideoPlayer/Document.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ - (BOOL)readFromData:(NSData*)data ofType:(NSString*)typeName error:(NSError**)o
8888
auto manager_or = Media::PlaybackManager::from_data(ReadonlyBytes { [data bytes], [data length] });
8989
if (manager_or.is_error()) {
9090
auto description = manager_or.error().description();
91-
NSLog(@"failed to load: %.*s", (int)description.length(), description.characters_without_null_termination());
91+
NSString* error_message = [NSString stringWithFormat:@"%.*s", (int)description.length(), description.characters_without_null_termination()];
9292
if (outError) {
9393
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain
9494
code:unimpErr
95-
userInfo:nil];
95+
userInfo:@{ NSLocalizedRecoverySuggestionErrorKey : error_message }];
9696
}
9797
return NO;
9898
}

Meta/Lagom/Contrib/MacVideoPlayer/View.mm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ @interface View ()
1212
{
1313
Media::PlaybackManager* _manager;
1414
NSBitmapImageRep* _currentFrame;
15+
NSString* _errorMessage;
1516
}
1617
@end
1718

@@ -52,6 +53,27 @@ - (void)setPlaybackManager:(Media::PlaybackManager*)manager
5253
if (strong_self) {
5354
auto* ns_frame = ns_from_gfx(move(frame));
5455
strong_self->_currentFrame = ns_frame;
56+
strong_self->_errorMessage = nil;
57+
[strong_self setNeedsDisplay:YES];
58+
}
59+
};
60+
61+
_manager->on_decoder_error = [weak_self](auto error) {
62+
View* strong_self = weak_self;
63+
if (strong_self) {
64+
auto error_string = error.description();
65+
strong_self->_currentFrame = nil;
66+
strong_self->_errorMessage = [NSString stringWithFormat:@"Decoder error: %.*s", (int)error_string.length(), error_string.characters_without_null_termination()];
67+
[strong_self setNeedsDisplay:YES];
68+
}
69+
};
70+
71+
_manager->on_fatal_playback_error = [weak_self](auto const& error) {
72+
View* strong_self = weak_self;
73+
if (strong_self) {
74+
auto error_string = error.string_literal();
75+
strong_self->_currentFrame = nil;
76+
strong_self->_errorMessage = [NSString stringWithFormat:@"Fatal playback error: %.*s", (int)error_string.length(), error_string.characters_without_null_termination()];
5577
[strong_self setNeedsDisplay:YES];
5678
}
5779
};
@@ -64,6 +86,23 @@ - (void)drawRect:(NSRect)rect
6486
[[NSColor blackColor] setFill];
6587
NSRectFill(rect);
6688

89+
if (_errorMessage) {
90+
NSFont* font = [NSFont preferredFontForTextStyle:NSFontTextStyleTitle1 options:@{}];
91+
92+
NSMutableParagraphStyle* paragraph_style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
93+
paragraph_style.alignment = NSTextAlignmentCenter;
94+
95+
NSRect bounding_rect = self.bounds;
96+
bounding_rect.size.height *= 0.66;
97+
98+
[_errorMessage drawInRect:bounding_rect
99+
withAttributes:@{
100+
NSFontAttributeName : font,
101+
NSForegroundColorAttributeName : [NSColor whiteColor],
102+
NSParagraphStyleAttributeName : paragraph_style,
103+
}];
104+
}
105+
67106
if (!_currentFrame)
68107
return;
69108

0 commit comments

Comments
 (0)