Skip to content

Commit

Permalink
Track number of frame passed on to videoencoder (#802)
Browse files Browse the repository at this point in the history
The current frame number can be used to seek to the current position
in the recorded video. It is tracked for matching and failed screens.

First part for implementing poo#19300

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
  • Loading branch information
StefanBruens authored and coolo committed May 26, 2017
1 parent 60fac26 commit afaaab4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
29 changes: 19 additions & 10 deletions backend/baseclass.pm
Expand Up @@ -52,10 +52,11 @@ __PACKAGE__->mk_accessors(
sub new {
my $class = shift;
my $self = bless({class => $class}, $class);
$self->{started} = 0;
$self->{serialfile} = "serial0";
$self->{serial_offset} = 0;
$self->{video_frame_data} = [];
$self->{started} = 0;
$self->{serialfile} = "serial0";
$self->{serial_offset} = 0;
$self->{video_frame_data} = [];
$self->{video_frame_number} = 0;
return $self;
}

Expand Down Expand Up @@ -384,6 +385,7 @@ sub enqueue_screenshot {
push(@{$self->{video_frame_data}}, $imgdata);
}
$self->{select}->add($self->{encoder_pipe});
$self->{video_frame_number} += 1;

$watch->stop();
if ($watch->as_data()->{total_time} > $self->screenshot_interval) {
Expand Down Expand Up @@ -787,10 +789,12 @@ sub _failed_screens_to_json {

my @json_fails;
for my $l (@$failed_screens) {
my ($img, $failed_candidates, $testtime, $similarity) = @$l;
my ($img, $failed_candidates, $testtime, $similarity, $frame) = @$l;
my $h = {
candidates => $failed_candidates,
image => encode_base64($img->ppm_data)};
image => encode_base64($img->ppm_data),
frame => $frame,
};
push(@json_fails, $h);
}

Expand All @@ -815,6 +819,7 @@ sub check_asserted_screen {
my $watch = OpenQA::Benchmark::Stopwatch->new();
my $timestamp = $self->last_screenshot;
my $n = $self->_time_to_assert_screen_deadline;
my $frame = $self->{video_frame_number};

my $search_ratio = 0.02;
$search_ratio = 1 if ($n % 5 == 0);
Expand Down Expand Up @@ -843,7 +848,8 @@ sub check_asserted_screen {
return {
image => encode_base64($img->ppm_data),
found => $foundneedle,
candidates => $failed_candidates
candidates => $failed_candidates,
frame => $frame,
};
}

Expand All @@ -866,7 +872,7 @@ sub check_asserted_screen {
}
my $failed_screens = $self->assert_screen_fails;
# store the final mismatch
push(@$failed_screens, [$img, $failed_candidates, 0, 1000]);
push(@$failed_screens, [$img, $failed_candidates, 0, 1000, $frame]);
my $hash = $self->_failed_screens_to_json;
$hash->{image} = encode_base64($img->ppm_data);
# store stall status
Expand All @@ -888,7 +894,7 @@ sub check_asserted_screen {
$sim = $failed_screens->[-1]->[0]->similarity($img);
}
if ($sim < 30) {
push(@$failed_screens, [$img, $failed_candidates, $n, $sim]);
push(@$failed_screens, [$img, $failed_candidates, $n, $sim, $frame]);
}
# clean up every once in a while to avoid excessive memory consumption.
# The value here is an arbitrary limit.
Expand Down Expand Up @@ -936,7 +942,10 @@ sub cont_vm {
sub last_screenshot_data {
my ($self, $args) = @_;
return {} unless $self->last_image;
return {image => encode_base64($self->last_image->ppm_data)};
return {
image => encode_base64($self->last_image->ppm_data),
frame => $self->{video_frame_number},
};
}

sub verify_image {
Expand Down
22 changes: 19 additions & 3 deletions basetest.pm
Expand Up @@ -108,8 +108,19 @@ sub post_fail_hook {
return 1;
}

=head2 _framenumber_to_timerange
Create a media fragment time from a given framenumber
=cut

sub _framenumber_to_timerange {
my $frame = shift;
return [sprintf("%.2f", $frame / 24.0), sprintf("%.2f", ($frame + 1) / 24.0)];
}

sub record_screenmatch {
my ($self, $img, $match, $tags, $failed_needles) = @_;
my ($self, $img, $match, $tags, $failed_needles, $frame) = @_;
$tags ||= [];
$failed_needles ||= [];

Expand All @@ -118,11 +129,12 @@ sub record_screenmatch {
my $result = {
needle => $h->{name},
area => $h->{area},
tags => [@$tags], # make a copy
tags => [@$tags], # make a copy
screenshot => $self->next_resultname('png'),
result => 'ok',
properties => [@$properties],
json => $h->{json},
frametime => _framenumber_to_timerange($frame),
};

# make sure needle is blessed
Expand Down Expand Up @@ -194,6 +206,7 @@ sub record_screenfail {
my $tags = $args{tags} || [];
my $status = $args{result} || 'fail';
my $overall = $args{overall}; # whether and how to set global test result
my $frame = $args{frame};

my $candidates;
for my $cand (@{$needles || []}) {
Expand All @@ -203,6 +216,7 @@ sub record_screenfail {
my $result = {
screenshot => $self->next_resultname('png'),
result => $status,
frametime => _framenumber_to_timerange($frame),
};

$result->{needles} = $candidates if $candidates;
Expand Down Expand Up @@ -463,13 +477,15 @@ internal function to add a screenshot to an existing result structure
sub _result_add_screenshot {
my ($self, $result) = @_;

my $img = autotest::query_isotovideo('backend_last_screenshot_data')->{image};
my $rsp = autotest::query_isotovideo('backend_last_screenshot_data');
my $img = $rsp->{image};
return $result unless $img;

$img = tinycv::from_ppm(decode_base64($img));
return $result unless $img;

$result->{screenshot} = $self->next_resultname('png');
$result->{frametime} = _framenumber_to_timerange($rsp->{frame});

my $fn = join('/', bmwqemu::result_dir(), $result->{screenshot});
$img->write_with_thumbnail($fn);
Expand Down
11 changes: 7 additions & 4 deletions testapi.pm
Expand Up @@ -214,8 +214,9 @@ sub _handle_found_needle {
my ($foundneedle, $rsp, $tags) = @_;
# convert the needle back to an object
$foundneedle->{needle} = needle->new($foundneedle->{needle});
my $img = tinycv::from_ppm(decode_base64($rsp->{image}));
$autotest::current_test->record_screenmatch($img, $foundneedle, $tags, $rsp->{candidates});
my $img = tinycv::from_ppm(decode_base64($rsp->{image}));
my $frame = $rsp->{frame};
$autotest::current_test->record_screenmatch($img, $foundneedle, $tags, $rsp->{candidates}, $frame);
my $lastarea = $foundneedle->{area}->[-1];
bmwqemu::fctres(sprintf("found %s, similarity %.2f @ %d/%d", $foundneedle->{needle}->{name}, $lastarea->{similarity}, $lastarea->{x}, $lastarea->{y}));
$last_matched_needle = $foundneedle;
Expand Down Expand Up @@ -248,7 +249,8 @@ sub _check_backend_response {
img => $img,
needles => $l->{candidates},
tags => $tags,
result => $result
result => $result,
frame => $l->{frame},
);
}
else {
Expand All @@ -257,7 +259,8 @@ sub _check_backend_response {
needles => $l->{candidates},
tags => $tags,
result => $result,
overall => $check ? undef : 'fail'
overall => $check ? undef : 'fail',
frame => $l->{frame},
);
}
}
Expand Down

0 comments on commit afaaab4

Please sign in to comment.