summaryrefslogtreecommitdiff
path: root/tree/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/manifest/DashManifestTest.java
blob: b260bf2cee748ca1accc257b4ede0ce5ace0ce1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (C) 2017 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.dash.manifest;

import static com.google.common.truth.Truth.assertThat;

import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.dash.manifest.SegmentBase.SingleSegmentBase;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;

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

  private static final UtcTimingElement DUMMY_UTC_TIMING = new UtcTimingElement("", "");
  private static final SingleSegmentBase DUMMY_SEGMENT_BASE = new SingleSegmentBase();
  private static final Format DUMMY_FORMAT = new Format.Builder().build();

  @Test
  public void copy() {
    Representation[][][] representations = newRepresentations(3, 2, 3);
    DashManifest sourceManifest =
        newDashManifest(
            10,
            newPeriod(
                "1",
                1,
                newAdaptationSet(2, representations[0][0]),
                newAdaptationSet(3, representations[0][1])),
            newPeriod(
                "4",
                4,
                newAdaptationSet(5, representations[1][0]),
                newAdaptationSet(6, representations[1][1])),
            newPeriod(
                "7",
                7,
                newAdaptationSet(8, representations[2][0]),
                newAdaptationSet(9, representations[2][1])));

    List<StreamKey> keys =
        Arrays.asList(
            new StreamKey(0, 0, 0),
            new StreamKey(0, 0, 1),
            new StreamKey(0, 1, 2),
            new StreamKey(1, 0, 1),
            new StreamKey(1, 1, 0),
            new StreamKey(1, 1, 2),
            new StreamKey(2, 0, 1),
            new StreamKey(2, 0, 2),
            new StreamKey(2, 1, 0));
    // Keys don't need to be in any particular order
    Collections.shuffle(keys, new Random(0));

    DashManifest copyManifest = sourceManifest.copy(keys);

    DashManifest expectedManifest =
        newDashManifest(
            10,
            newPeriod(
                "1",
                1,
                newAdaptationSet(2, representations[0][0][0], representations[0][0][1]),
                newAdaptationSet(3, representations[0][1][2])),
            newPeriod(
                "4",
                4,
                newAdaptationSet(5, representations[1][0][1]),
                newAdaptationSet(6, representations[1][1][0], representations[1][1][2])),
            newPeriod(
                "7",
                7,
                newAdaptationSet(8, representations[2][0][1], representations[2][0][2]),
                newAdaptationSet(9, representations[2][1][0])));
    assertManifestEquals(expectedManifest, copyManifest);
  }

  @Test
  public void copySameAdaptationIndexButDifferentPeriod() {
    Representation[][][] representations = newRepresentations(2, 1, 1);
    DashManifest sourceManifest =
        newDashManifest(
            10,
            newPeriod("1", 1, newAdaptationSet(2, representations[0][0])),
            newPeriod("4", 4, newAdaptationSet(5, representations[1][0])));

    DashManifest copyManifest =
        sourceManifest.copy(Arrays.asList(new StreamKey(0, 0, 0), new StreamKey(1, 0, 0)));

    DashManifest expectedManifest =
        newDashManifest(
            10,
            newPeriod("1", 1, newAdaptationSet(2, representations[0][0])),
            newPeriod("4", 4, newAdaptationSet(5, representations[1][0])));
    assertManifestEquals(expectedManifest, copyManifest);
  }

  @Test
  public void copySkipPeriod() {
    Representation[][][] representations = newRepresentations(3, 2, 3);
    DashManifest sourceManifest =
        newDashManifest(
            10,
            newPeriod(
                "1",
                1,
                newAdaptationSet(2, representations[0][0]),
                newAdaptationSet(3, representations[0][1])),
            newPeriod(
                "4",
                4,
                newAdaptationSet(5, representations[1][0]),
                newAdaptationSet(6, representations[1][1])),
            newPeriod(
                "7",
                7,
                newAdaptationSet(8, representations[2][0]),
                newAdaptationSet(9, representations[2][1])));

    DashManifest copyManifest =
        sourceManifest.copy(
            Arrays.asList(
                new StreamKey(0, 0, 0),
                new StreamKey(0, 0, 1),
                new StreamKey(0, 1, 2),
                new StreamKey(2, 0, 1),
                new StreamKey(2, 0, 2),
                new StreamKey(2, 1, 0)));

    DashManifest expectedManifest =
        newDashManifest(
            7,
            newPeriod(
                "1",
                1,
                newAdaptationSet(2, representations[0][0][0], representations[0][0][1]),
                newAdaptationSet(3, representations[0][1][2])),
            newPeriod(
                "7",
                4,
                newAdaptationSet(8, representations[2][0][1], representations[2][0][2]),
                newAdaptationSet(9, representations[2][1][0])));
    assertManifestEquals(expectedManifest, copyManifest);
  }

  private static void assertManifestEquals(DashManifest expected, DashManifest actual) {
    assertThat(actual.availabilityStartTimeMs).isEqualTo(expected.availabilityStartTimeMs);
    assertThat(actual.durationMs).isEqualTo(expected.durationMs);
    assertThat(actual.minBufferTimeMs).isEqualTo(expected.minBufferTimeMs);
    assertThat(actual.dynamic).isEqualTo(expected.dynamic);
    assertThat(actual.minUpdatePeriodMs).isEqualTo(expected.minUpdatePeriodMs);
    assertThat(actual.timeShiftBufferDepthMs).isEqualTo(expected.timeShiftBufferDepthMs);
    assertThat(actual.suggestedPresentationDelayMs)
        .isEqualTo(expected.suggestedPresentationDelayMs);
    assertThat(actual.publishTimeMs).isEqualTo(expected.publishTimeMs);
    assertThat(actual.utcTiming).isEqualTo(expected.utcTiming);
    assertThat(actual.location).isEqualTo(expected.location);
    assertThat(actual.getPeriodCount()).isEqualTo(expected.getPeriodCount());
    for (int i = 0; i < expected.getPeriodCount(); i++) {
      Period expectedPeriod = expected.getPeriod(i);
      Period actualPeriod = actual.getPeriod(i);
      assertThat(actualPeriod.id).isEqualTo(expectedPeriod.id);
      assertThat(actualPeriod.startMs).isEqualTo(expectedPeriod.startMs);
      List<AdaptationSet> expectedAdaptationSets = expectedPeriod.adaptationSets;
      List<AdaptationSet> actualAdaptationSets = actualPeriod.adaptationSets;
      assertThat(actualAdaptationSets).hasSize(expectedAdaptationSets.size());
      for (int j = 0; j < expectedAdaptationSets.size(); j++) {
        AdaptationSet expectedAdaptationSet = expectedAdaptationSets.get(j);
        AdaptationSet actualAdaptationSet = actualAdaptationSets.get(j);
        assertThat(actualAdaptationSet.id).isEqualTo(expectedAdaptationSet.id);
        assertThat(actualAdaptationSet.type).isEqualTo(expectedAdaptationSet.type);
        assertThat(actualAdaptationSet.accessibilityDescriptors)
            .isEqualTo(expectedAdaptationSet.accessibilityDescriptors);
        assertThat(actualAdaptationSet.representations)
            .isEqualTo(expectedAdaptationSet.representations);
      }
    }
  }

  private static Representation[][][] newRepresentations(
      int periodCount, int adaptationSetCounts, int representationCounts) {
    Representation[][][] representations = new Representation[periodCount][][];
    for (int i = 0; i < periodCount; i++) {
      representations[i] = new Representation[adaptationSetCounts][];
      for (int j = 0; j < adaptationSetCounts; j++) {
        representations[i][j] = new Representation[representationCounts];
        for (int k = 0; k < representationCounts; k++) {
          representations[i][j][k] = newRepresentation();
        }
      }
    }
    return representations;
  }

  private static Representation newRepresentation() {
    return Representation.newInstance(
        /* revisionId= */ 0, DUMMY_FORMAT, /* baseUrl= */ "", DUMMY_SEGMENT_BASE);
  }

  private static DashManifest newDashManifest(int duration, Period... periods) {
    return new DashManifest(
        /* availabilityStartTimeMs= */ 0,
        duration,
        /* minBufferTimeMs= */ 1,
        /* dynamic= */ false,
        /* minUpdatePeriodMs= */ 2,
        /* timeShiftBufferDepthMs= */ 3,
        /* suggestedPresentationDelayMs= */ 4,
        /* publishTimeMs= */ 12345,
        /* programInformation= */ null,
        DUMMY_UTC_TIMING,
        Uri.EMPTY,
        Arrays.asList(periods));
  }

  private static Period newPeriod(String id, int startMs, AdaptationSet... adaptationSets) {
    return new Period(id, startMs, Arrays.asList(adaptationSets));
  }

  private static AdaptationSet newAdaptationSet(int seed, Representation... representations) {
    return new AdaptationSet(
        ++seed,
        ++seed,
        Arrays.asList(representations),
        /* accessibilityDescriptors= */ Collections.emptyList(),
        /* essentialProperties= */ Collections.emptyList(),
        /* supplementalProperties= */ Collections.emptyList());
  }
}