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

Allows discard of overlapping iFrame only chunks #10407

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public void getNextChunk(

boolean shouldSpliceIn =
HlsMediaChunk.shouldSpliceIn(
previous, selectedPlaylistUrl, playlist, segmentBaseHolder, startOfPlaylistInPeriodUs);
previous, selectedPlaylistUrl, playlist, playlistFormats[selectedTrackIndex], segmentBaseHolder, startOfPlaylistInPeriodUs);
if (shouldSpliceIn && segmentBaseHolder.isPreload) {
// We don't support discarding spliced-in segments [internal: b/159904763], but preload
// parts may need to be discarded if they are removed before becoming permanently published.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.analytics.PlayerId;
Expand Down Expand Up @@ -197,6 +198,7 @@ public static HlsMediaChunk createInstance(
* in the queue.
* @param playlistUrl The URL of the playlist from which the new chunk will be obtained.
* @param mediaPlaylist The {@link HlsMediaPlaylist} containing the new chunk.
* @param playlistFormat The {@link Format} of the playlist for the new chunk
* @param segmentBaseHolder The {@link HlsChunkSource.SegmentBaseHolder} with information about
* the new chunk.
* @param startOfPlaylistInPeriodUs The start time of the playlist in the period, in microseconds.
Expand All @@ -206,6 +208,7 @@ public static boolean shouldSpliceIn(
@Nullable HlsMediaChunk previousChunk,
Uri playlistUrl,
HlsMediaPlaylist mediaPlaylist,
Format playlistFormat,
HlsChunkSource.SegmentBaseHolder segmentBaseHolder,
long startOfPlaylistInPeriodUs) {
if (previousChunk == null) {
Expand All @@ -221,8 +224,11 @@ public static boolean shouldSpliceIn(
// non-overlapping segments to avoid the splice.
long segmentStartTimeInPeriodUs =
startOfPlaylistInPeriodUs + segmentBaseHolder.segmentBase.relativeStartTimeUs;
boolean areBothChunksTrickplay = (previousChunk.trackFormat.roleFlags & C.ROLE_FLAG_TRICK_PLAY) != 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

The decision to splice in depends on whether we are guaranteed that either
(1) The next chunk is a continuation of the previous one (checked above at L217) or
(2) The next chunk starts with a keyframe (currently checked by isIndependent)
AND its first frame comes after the start time of the last frame of the previous chunk. (currently checked by segmentStartTime < previousEndTime)

I think we can make this condition more targeted to also work in cases where not both chunks are from a trick-play tracks. And there is also the edge case of trick play tracks whose start time is further in the past than the current trick play track. So I think it could be something like:

// Need to splice in if the next chunk doesn't start with a keyframe.
if (!isIndependent(...) && !nextChunkIsTrickPlay) {
  return true;
}
// Need to splice in if chunks are overlapping.
return segmentStartTimeInPeriodUs < previousChunk.startTimeUs
    || (!previousChunkIsTrickPlay && segmentStartTimeInPeriodUs < previousChunk.endTimeUs);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that would be great. Right now, the way trickplay track selection is coded, we have to cause a full new TrackSelection with either all iFrame only or all non-iFrame only tracks so this use case does not occur.

Eventually we want this dynamic based on a PlaybackParameter so we would have adaption (and of course require discard on trickPlay to non-TrickPlay track) between tracks. I want to get to adding this to the main demo and submitting pull requests for all of it, internally our first priority is to catch up our branch to nearer the latest ExoPlayer version.

&& (playlistFormat.roleFlags & C.ROLE_FLAG_TRICK_PLAY) != 0;

return !isIndependent(segmentBaseHolder, mediaPlaylist)
|| segmentStartTimeInPeriodUs < previousChunk.endTimeUs;
|| (segmentStartTimeInPeriodUs < previousChunk.endTimeUs && !areBothChunksTrickplay);
}

public static final String PRIV_TIMESTAMP_FRAME_OWNER =
Expand Down Expand Up @@ -397,6 +403,11 @@ public void load() throws IOException {
}
}

@VisibleForTesting
void setLoadCompleted() {
loadCompleted = true;
}

/**
* Whether the chunk is a published chunk as opposed to a preload hint that may change when the
* playlist updates.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.source.hls;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.analytics.PlayerId;
import com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser;
import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.util.MimeTypes;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

/** Unit tests for {@link HlsMediaChunk}. */
@RunWith(AndroidJUnit4.class)
public class HlsMediaChunkTest {

private static final String PLAYLIST_INDEPENDENT_SEGMENTS =
"media/m3u8/media_playlist_independent_segments";
private static final String PLAYLIST_INDEPENDENT_PART =
"media/m3u8/live_low_latency_segment_with_independent_part";

private static final String PLAYLIST_IFRAME_2s =
"media/m3u8/media_playlist_independent_2second_iframe";
private static final String PLAYLIST_IFRAME_4s =
"media/m3u8/media_playlist_independent_4second_iframe";

private static final Uri PLAYLIST_URI = Uri.parse("http://example.com/");

private static final String PLAYLIST_NON_INDEPENDENT_SEGMENTS =
"media/m3u8/media_playlist";

@Mock private HlsExtractorFactory mockExtractorFactory;
@Mock private DataSource mockDataSource;
private HlsMediaPlaylist playlist;


private static final Format BASE_VIDEO_FORMAT =
new Format.Builder()
.setSampleMimeType(MimeTypes.VIDEO_H264)
.setAverageBitrate(30_000)
.setWidth(1280)
.setHeight(720)
.build();

private static final Format IFRAME_FORMAT =
BASE_VIDEO_FORMAT.buildUpon()
.setRoleFlags(C.ROLE_FLAG_TRICK_PLAY)
.build();
@Before
public void setUp() throws Exception {

InputStream inputStream =
TestUtil.getInputStream(
ApplicationProvider.getApplicationContext(), PLAYLIST_INDEPENDENT_SEGMENTS);
playlist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(Uri.EMPTY, inputStream);

mockDataSource = new FakeDataSource();
mockExtractorFactory = new DefaultHlsExtractorFactory();
}

@Test
public void test_shouldSpliceIn_isFalse_NoPrevious() {
boolean result =
HlsMediaChunk.shouldSpliceIn(null, Uri.EMPTY, playlist, BASE_VIDEO_FORMAT, null, 0);
assertThat(result).isFalse();
}

@Test
public void test_shouldSpliceIn_PreviousLoaded_SamePlaylist() {
HlsChunkSource.SegmentBaseHolder segmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);
HlsMediaChunk previousChunk = createTestHlsMediaChunk(BASE_VIDEO_FORMAT, segmentBaseHolder, PLAYLIST_URI, true);
previousChunk.setLoadCompleted();
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, PLAYLIST_URI, playlist, BASE_VIDEO_FORMAT, segmentBaseHolder, 0);

assertThat(result).isFalse();
}

@Test
public void test_shouldSpliceIn_NotIndependent_DifferentPlaylist() {
HlsChunkSource.SegmentBaseHolder previousSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);

Uri variant1 = Uri.parse("http://example.com/variant1.m3u8");
HlsMediaChunk previousChunk = createTestHlsMediaChunk(BASE_VIDEO_FORMAT, previousSegmentBaseHolder, variant1, true);

Uri variant2 = Uri.parse("http://example.com/variant2.m3u8");
HlsMediaPlaylist nonIndependentPlaylist = HlsTestUtils.getHlsMediaPlaylist(PLAYLIST_NON_INDEPENDENT_SEGMENTS, variant2);
HlsChunkSource.SegmentBaseHolder nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(nonIndependentPlaylist.segments.get(0), 0, 0);

// Switch to non-independent segment playlist requires splice-in, regardless if the prev and
// next segments overlap or not

assertThat(previousSegmentBaseHolder.segmentBase.relativeStartTimeUs).isEqualTo(0); // inputs assertions
assertThat(nextSegmentBaseHolder.segmentBase.relativeStartTimeUs + nextSegmentBaseHolder.segmentBase.durationUs).isGreaterThan(0);
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, nonIndependentPlaylist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);
assertThat(result).isTrue();

nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(nonIndependentPlaylist.segments.get(1), 1, 0);
result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, nonIndependentPlaylist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);
assertThat(result).isTrue();
}

@Test
public void test_shouldSpliceIn_Independent_DifferentPlaylist() {
HlsChunkSource.SegmentBaseHolder previousSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);

Uri variant1 = Uri.parse("http://example.com/variant1.m3u8");
HlsMediaChunk previousChunk = createTestHlsMediaChunk(BASE_VIDEO_FORMAT, previousSegmentBaseHolder, variant1, true);

// NOTE, playlist change is checked by Uri match, so can use same playlist, mock change with URI
Uri variant2 = Uri.parse("http://example.com/variant2.m3u8");
HlsChunkSource.SegmentBaseHolder nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);

// Switch to inependent segment playlist requires splice-in, only if the start of the next segment is
// less than the end of the previous

assertThat(previousSegmentBaseHolder.segmentBase.relativeStartTimeUs).isEqualTo(0);
assertThat(nextSegmentBaseHolder.segmentBase.relativeStartTimeUs + nextSegmentBaseHolder.segmentBase.durationUs).isGreaterThan(0);
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, playlist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);
assertThat(result).isTrue();

nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(1), 1, 0);
result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, playlist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);
assertThat(result).isFalse();
}

@Test
public void test_shouldSpliceIn_SegmentParts() {
HlsChunkSource.SegmentBaseHolder previousSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);

Uri variant1 = Uri.parse("http://example.com/variant1.m3u8");
HlsMediaChunk previousChunk = createTestHlsMediaChunk(BASE_VIDEO_FORMAT, previousSegmentBaseHolder, variant1, true);
previousChunk.setLoadCompleted();

Uri variant2 = Uri.parse("http://example.com/variant2.m3u8");
HlsMediaPlaylist hlsMediaPlaylist = HlsTestUtils.getHlsMediaPlaylist(PLAYLIST_INDEPENDENT_PART, variant2);

// First Part checks segment level independent
HlsChunkSource.SegmentBaseHolder nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(hlsMediaPlaylist.trailingParts.get(0), 0, 0);
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, hlsMediaPlaylist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);

assertThat(result).isFalse();

// Additional Parts must be themselves independent, regardless of the independence of the playlist
nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(hlsMediaPlaylist.trailingParts.get(1), 1, 1);
result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, hlsMediaPlaylist, BASE_VIDEO_FORMAT, nextSegmentBaseHolder, 0);
assertThat(result).isFalse();

}

@Test
public void test_shouldSpliceIn_IntraTrickPlay() {

// Trick play to trick play track should never need a splice, even overlapping

Uri variant4 = Uri.parse("http://example.com/iframe4.m3u8");
HlsMediaPlaylist hlsMediaPlaylist4s = HlsTestUtils.getHlsMediaPlaylist(PLAYLIST_IFRAME_4s, variant4);
HlsChunkSource.SegmentBaseHolder previousSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(hlsMediaPlaylist4s.segments.get(0), 0, 0);
HlsMediaChunk previousChunk = createTestHlsMediaChunk(IFRAME_FORMAT, previousSegmentBaseHolder, variant4, true);

Uri variant2 = Uri.parse("http://example.com/iframe2.m3u8");
HlsMediaPlaylist hlsMediaPlaylist2s = HlsTestUtils.getHlsMediaPlaylist(PLAYLIST_IFRAME_2s, variant2);
HlsChunkSource.SegmentBaseHolder nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(hlsMediaPlaylist2s.segments.get(1), 0, 0);
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, hlsMediaPlaylist2s, IFRAME_FORMAT, nextSegmentBaseHolder, 0);

assertThat(result).isFalse();
}

@Test
@Ignore
public void test_shouldSpliceIn_NonTrickPlay_To_TrickPlay() {

// Switch to a trick-play track never requires splice in, even if overlapping
HlsChunkSource.SegmentBaseHolder previousSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(playlist.segments.get(0), 0, 0);
HlsMediaChunk previousChunk = createTestHlsMediaChunk(BASE_VIDEO_FORMAT, previousSegmentBaseHolder, PLAYLIST_URI, true);

Uri variant2 = Uri.parse("http://example.com/iframe2.m3u8");
HlsMediaPlaylist hlsMediaPlaylist2s = HlsTestUtils.getHlsMediaPlaylist(PLAYLIST_IFRAME_2s, variant2);
HlsChunkSource.SegmentBaseHolder nextSegmentBaseHolder =
new HlsChunkSource.SegmentBaseHolder(hlsMediaPlaylist2s.segments.get(1), 0, 0);
boolean result =
HlsMediaChunk.shouldSpliceIn(previousChunk, variant2, hlsMediaPlaylist2s, IFRAME_FORMAT, nextSegmentBaseHolder, 0);

assertThat(result).isFalse();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this returns true with my proposed logic because it really needs to be spliced in?

If I read the example data correctly, previousChunk has dense (non-trick-play) samples from 0 to 4 seconds and we now attempt to read an i-frame-only chunk from 2 to 4 seconds. This chunk has to be spliced in because we may have written the dense samples between 2 and 4 seconds already and need to replace them by the samples from the new chunk. I'd assume the test works if you choose a trick-play segment starting at 4 seconds?

I think the test the other way round is even more interesting (trick-play to non-trick-play) because you should be able to choose a dense track starting without splicing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the splice will effectively discard the downloaded "lower res" media like evaluateQueueSize() with discard does in favor of the adaptive switch occurs?

If this is true (more dense samples from non-trickplay overwrite (replace) the less dense trick-play samples then yes, I agree I need to add a test case and support trick-play to non-trick play mode. We are handling this non-adaptively now by forcing a seek and a completely new TrackGroup (so sample queue is flushed), but it would be better if this could happen entirely in the adaptive switch. For obvious reasons you would not want to continue with a very low frame rate set of samples after switching back to normal playback.

}

private HlsMediaChunk createTestHlsMediaChunk(
Format selectedTrackFormat,
HlsChunkSource.SegmentBaseHolder segmentBaseHolder,
Uri selectedPlaylistUrl,
boolean shouldSpliceIn) {
return HlsMediaChunk.createInstance(
mockExtractorFactory,
mockDataSource,
selectedTrackFormat,
0,
playlist,
segmentBaseHolder,
selectedPlaylistUrl,
null,
C.SELECTION_REASON_INITIAL,
null,
true,
new TimestampAdjusterProvider(),
null,
null,
null,
shouldSpliceIn,
new PlayerId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.source.hls;

import static org.junit.Assert.fail;

import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.IOException;

public class HlsTestUtils {

/**
* Load a mock HLS playlist from a test asset file.
*
* @param file - source of the text of the test playlist
* @param playlistUri - Uri to set as base for the playlist
* @return test {@link HlsMediaPlaylist}
*/
public static HlsMediaPlaylist getHlsMediaPlaylist(String file, Uri playlistUri) {
try {
return (HlsMediaPlaylist)
new HlsPlaylistParser()
.parse(
playlistUri,
TestUtil.getInputStream(ApplicationProvider.getApplicationContext(), file));
} catch (IOException e) {
fail(e.getMessage());
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#EXTM3U
#EXT-X-TARGETDURATION:4
#EXT-X-PART-INF:PART-TARGET=1.000400
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:10
#EXTINF:4.00000,
fileSequence15.ts
#EXT-X-PART:DURATION=1.00000,URI="fileSequence16.0.ts"
#EXT-X-PART:DURATION=1.00000,INDEPENDENT=YES,URI="fileSequence16.1.ts"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#EXTM3U
#EXT-X-MEDIA-SEQUENCE:2
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-I-FRAMES-ONLY
#EXT-X-MAP:URI="init.mp4"
#EXTINF:2,
#EXT-X-BYTERANGE:3008@376
2.mp4
#EXTINF:2,
#EXT-X-BYTERANGE:10716@3384
2.mp4
#EXTINF:2,
#EXT-X-BYTERANGE:58844@14100
2.mp4
#EXT-X-ENDLIST
Loading