Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stco in trailing moov #1056

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions Slim/Formats/Movie.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ sub parseStream {
my $offset = $args->{_offset};
my $log = logger('player.streaming');

while (length($args->{_scanbuf}) > $args->{_offset} + $args->{_need} + 8) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just an optimization, which shouldn't have any impact?...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of - using $offset is just easier to read

while (length($args->{_scanbuf}) > $offset + $args->{_need} + 8) {
$args->{_atom} = substr($args->{_scanbuf}, $offset+4, 4);
$args->{_need} = unpack('N', substr($args->{_scanbuf}, $offset, 4));
$args->{_offset} = $args->{"_$args->{_atom}_"} = $offset;
$args->{"_$args->{_atom}_"} = $args->{_range} + $offset;

# a bit of sanity check
if ($offset == 0 && $args->{_atom} ne 'ftyp') {
if ($offset == 0 && $args->{_range} == 0 && $args->{_atom} ne 'ftyp') {
$log->warn("no header! this is supposed to be a mp4 track");
return 0;
}
Expand All @@ -271,8 +271,7 @@ sub parseStream {

# need to dive into atoms to find optional stco
$offset += ($args->{_atom} !~ /^(moov|trak|mdia|minf|stbl)$/) ? $args->{_need} : 8;

main::DEBUGLOG && $log->is_debug && $log->debug("atom $args->{_atom} at $args->{_offset} of size $args->{_need}");
main::DEBUGLOG && $log->is_debug && $log->debug("atom $args->{_atom} at ", $args->{"_$args->{_atom}_"}, " of size $args->{_need}");

# mdat reached = audio offset & size acquired
if ($args->{_atom} eq 'mdat') {
Expand All @@ -281,6 +280,7 @@ sub parseStream {
}
}

$args->{_offset} = $offset;
return -1 unless $args->{_mdat_};

# now make sure we have acquired a full moov atom
Expand All @@ -295,16 +295,18 @@ sub parseStream {
return -1 if $args->{_range};

# top 'moov' not found, need to seek beyond 'mdat'
$args->{_range} = $offset;
substr($args->{_scanbuf}, $args->{_offset}) = '';
$args->{_range} = $args->{_offset};
$args->{_scanbuf} = '';
$args->{_offset} = 0;
delete $args->{_need};
return $offset;

return $args->{_range};
} elsif ($args->{_atom} eq 'moov' && $len) {
return -1;
}

# finally got it, add 'moov' size it if was last atom
substr($args->{_scanbuf}, $args->{_offset} + ($args->{_atom} eq 'moov' ? $args->{_need} : 0)) = '';
# finally got it, align to beginning of 'moov'
substr($args->{_scanbuf}, 0, $args->{_moov_} - $args->{_range}, '');

# put at least 16 bytes after mdat or it confuses audio::scan (and header creation)
my $fh = File::Temp->new( DIR => Slim::Utils::Misc::getTempDir);
Expand Down
10 changes: 6 additions & 4 deletions Slim/Utils/Scanner/Remote.pm
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,9 @@ sub parseMp4Header {
Slim::Formats->loadTagFormatForType('mp4');
my $formatClass = Slim::Formats->classForFormat('mp4');

# need to memorize real length at first call
$args->{_contentLength} ||= $http->response->content_length;

# parse chunk
my $info = $formatClass->parseStream($dataref, $args, $http->response->content_length);

Expand Down Expand Up @@ -871,7 +874,6 @@ sub parseMp4Header {
foreach ( keys %{$info->{processors}} ) {
$track->processors($_, Slim::Schema::RemoteTrack::INITIAL_BLOCK_ALWAYS, $info->{processors}->{$_});
}

# change track attributes if format has been altered
if ( $format ne $track->content_type ) {
Slim::Schema->clearContentTypeCache( $track->url );
Expand All @@ -885,9 +887,9 @@ sub parseMp4Header {
}

# some mp4 file have wrong mdat length
if ($info->{audio_offset} + $info->{audio_size} > $http->response->content_length) {
$log->warn("inconsistent audio offset/size $info->{audio_offset}+$info->{audio_size}and content_length ", $http->response->content_length);
$track->audio_size($http->response->content_length - $info->{audio_offset});
if ($info->{audio_offset} + $info->{audio_size} > $args->{_contentLength}) {
$log->warn("inconsistent audio offset/size $info->{audio_offset}+$info->{audio_size} and content_length ", $args->{_contentLength});
$track->audio_size($args->{_contentLength} - $info->{audio_offset});
} else {
$track->audio_size($info->{audio_size});
}
Expand Down