summaryrefslogtreecommitdiffstats
path: root/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/PlaybackInfo.java
blob: c743e35661c082ab519ffddd0aeff63045b20f64 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * 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 org.mozilla.thirdparty.com.google.android.exoplayer2;

import androidx.annotation.CheckResult;
import androidx.annotation.Nullable;
import org.mozilla.thirdparty.com.google.android.exoplayer2.source.MediaSource.MediaPeriodId;
import org.mozilla.thirdparty.com.google.android.exoplayer2.source.TrackGroupArray;
import org.mozilla.thirdparty.com.google.android.exoplayer2.trackselection.TrackSelectorResult;

/**
 * Information about an ongoing playback.
 */
/* package */ final class PlaybackInfo {

  /**
   * Dummy media period id used while the timeline is empty and no period id is specified. This id
   * is used when playback infos are created with {@link #createDummy(long, TrackSelectorResult)}.
   */
  private static final MediaPeriodId DUMMY_MEDIA_PERIOD_ID =
      new MediaPeriodId(/* periodUid= */ new Object());

  /** The current {@link Timeline}. */
  public final Timeline timeline;
  /** The {@link MediaPeriodId} of the currently playing media period in the {@link #timeline}. */
  public final MediaPeriodId periodId;
  /**
   * The start position at which playback started in {@link #periodId} relative to the start of the
   * associated period in the {@link #timeline}, in microseconds. Note that this value changes for
   * each position discontinuity.
   */
  public final long startPositionUs;
  /**
   * If {@link #periodId} refers to an ad, the position of the suspended content relative to the
   * start of the associated period in the {@link #timeline}, in microseconds. {@link C#TIME_UNSET}
   * if {@link #periodId} does not refer to an ad or if the suspended content should be played from
   * its default position.
   */
  public final long contentPositionUs;
  /** The current playback state. One of the {@link Player}.STATE_ constants. */
  @Player.State public final int playbackState;
  /** The current playback error, or null if this is not an error state. */
  @Nullable public final ExoPlaybackException playbackError;
  /** Whether the player is currently loading. */
  public final boolean isLoading;
  /** The currently available track groups. */
  public final TrackGroupArray trackGroups;
  /** The result of the current track selection. */
  public final TrackSelectorResult trackSelectorResult;
  /** The {@link MediaPeriodId} of the currently loading media period in the {@link #timeline}. */
  public final MediaPeriodId loadingMediaPeriodId;

  /**
   * Position up to which media is buffered in {@link #loadingMediaPeriodId) relative to the start
   * of the associated period in the {@link #timeline}, in microseconds.
   */
  public volatile long bufferedPositionUs;
  /**
   * Total duration of buffered media from {@link #positionUs} to {@link #bufferedPositionUs}
   * including all ads.
   */
  public volatile long totalBufferedDurationUs;
  /**
   * Current playback position in {@link #periodId} relative to the start of the associated period
   * in the {@link #timeline}, in microseconds.
   */
  public volatile long positionUs;

  /**
   * Creates empty dummy playback info which can be used for masking as long as no real playback
   * info is available.
   *
   * @param startPositionUs The start position at which playback should start, in microseconds.
   * @param emptyTrackSelectorResult An empty track selector result with null entries for each
   *     renderer.
   * @return A dummy playback info.
   */
  public static PlaybackInfo createDummy(
      long startPositionUs, TrackSelectorResult emptyTrackSelectorResult) {
    return new PlaybackInfo(
        Timeline.EMPTY,
        DUMMY_MEDIA_PERIOD_ID,
        startPositionUs,
        /* contentPositionUs= */ C.TIME_UNSET,
        Player.STATE_IDLE,
        /* playbackError= */ null,
        /* isLoading= */ false,
        TrackGroupArray.EMPTY,
        emptyTrackSelectorResult,
        DUMMY_MEDIA_PERIOD_ID,
        startPositionUs,
        /* totalBufferedDurationUs= */ 0,
        startPositionUs);
  }

  /**
   * Create playback info.
   *
   * @param timeline See {@link #timeline}.
   * @param periodId See {@link #periodId}.
   * @param startPositionUs See {@link #startPositionUs}.
   * @param contentPositionUs See {@link #contentPositionUs}.
   * @param playbackState See {@link #playbackState}.
   * @param isLoading See {@link #isLoading}.
   * @param trackGroups See {@link #trackGroups}.
   * @param trackSelectorResult See {@link #trackSelectorResult}.
   * @param loadingMediaPeriodId See {@link #loadingMediaPeriodId}.
   * @param bufferedPositionUs See {@link #bufferedPositionUs}.
   * @param totalBufferedDurationUs See {@link #totalBufferedDurationUs}.
   * @param positionUs See {@link #positionUs}.
   */
  public PlaybackInfo(
      Timeline timeline,
      MediaPeriodId periodId,
      long startPositionUs,
      long contentPositionUs,
      @Player.State int playbackState,
      @Nullable ExoPlaybackException playbackError,
      boolean isLoading,
      TrackGroupArray trackGroups,
      TrackSelectorResult trackSelectorResult,
      MediaPeriodId loadingMediaPeriodId,
      long bufferedPositionUs,
      long totalBufferedDurationUs,
      long positionUs) {
    this.timeline = timeline;
    this.periodId = periodId;
    this.startPositionUs = startPositionUs;
    this.contentPositionUs = contentPositionUs;
    this.playbackState = playbackState;
    this.playbackError = playbackError;
    this.isLoading = isLoading;
    this.trackGroups = trackGroups;
    this.trackSelectorResult = trackSelectorResult;
    this.loadingMediaPeriodId = loadingMediaPeriodId;
    this.bufferedPositionUs = bufferedPositionUs;
    this.totalBufferedDurationUs = totalBufferedDurationUs;
    this.positionUs = positionUs;
  }

  /**
   * Returns dummy media period id for the first-to-be-played period of the current timeline.
   *
   * @param shuffleModeEnabled Whether shuffle mode is enabled.
   * @param window A writable {@link Timeline.Window}.
   * @param period A writable {@link Timeline.Period}.
   * @return A dummy media period id for the first-to-be-played period of the current timeline.
   */
  public MediaPeriodId getDummyFirstMediaPeriodId(
      boolean shuffleModeEnabled, Timeline.Window window, Timeline.Period period) {
    if (timeline.isEmpty()) {
      return DUMMY_MEDIA_PERIOD_ID;
    }
    int firstWindowIndex = timeline.getFirstWindowIndex(shuffleModeEnabled);
    int firstPeriodIndex = timeline.getWindow(firstWindowIndex, window).firstPeriodIndex;
    int currentPeriodIndex = timeline.getIndexOfPeriod(periodId.periodUid);
    long windowSequenceNumber = C.INDEX_UNSET;
    if (currentPeriodIndex != C.INDEX_UNSET) {
      int currentWindowIndex = timeline.getPeriod(currentPeriodIndex, period).windowIndex;
      if (firstWindowIndex == currentWindowIndex) {
        // Keep window sequence number if the new position is still in the same window.
        windowSequenceNumber = periodId.windowSequenceNumber;
      }
    }
    return new MediaPeriodId(timeline.getUidOfPeriod(firstPeriodIndex), windowSequenceNumber);
  }

  /**
   * Copies playback info with new playing position.
   *
   * @param periodId New playing media period. See {@link #periodId}.
   * @param positionUs New position. See {@link #positionUs}.
   * @param contentPositionUs New content position. See {@link #contentPositionUs}. Value is ignored
   *     if {@code periodId.isAd()} is true.
   * @param totalBufferedDurationUs New buffered duration. See {@link #totalBufferedDurationUs}.
   * @return Copied playback info with new playing position.
   */
  @CheckResult
  public PlaybackInfo copyWithNewPosition(
      MediaPeriodId periodId,
      long positionUs,
      long contentPositionUs,
      long totalBufferedDurationUs) {
    return new PlaybackInfo(
        timeline,
        periodId,
        positionUs,
        periodId.isAd() ? contentPositionUs : C.TIME_UNSET,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with the new timeline.
   *
   * @param timeline New timeline. See {@link #timeline}.
   * @return Copied playback info with the new timeline.
   */
  @CheckResult
  public PlaybackInfo copyWithTimeline(Timeline timeline) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with new playback state.
   *
   * @param playbackState New playback state. See {@link #playbackState}.
   * @return Copied playback info with new playback state.
   */
  @CheckResult
  public PlaybackInfo copyWithPlaybackState(int playbackState) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with a playback error.
   *
   * @param playbackError The error. See {@link #playbackError}.
   * @return Copied playback info with the playback error.
   */
  @CheckResult
  public PlaybackInfo copyWithPlaybackError(@Nullable ExoPlaybackException playbackError) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with new loading state.
   *
   * @param isLoading New loading state. See {@link #isLoading}.
   * @return Copied playback info with new loading state.
   */
  @CheckResult
  public PlaybackInfo copyWithIsLoading(boolean isLoading) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with new track information.
   *
   * @param trackGroups New track groups. See {@link #trackGroups}.
   * @param trackSelectorResult New track selector result. See {@link #trackSelectorResult}.
   * @return Copied playback info with new track information.
   */
  @CheckResult
  public PlaybackInfo copyWithTrackInfo(
      TrackGroupArray trackGroups, TrackSelectorResult trackSelectorResult) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }

  /**
   * Copies playback info with new loading media period.
   *
   * @param loadingMediaPeriodId New loading media period id. See {@link #loadingMediaPeriodId}.
   * @return Copied playback info with new loading media period.
   */
  @CheckResult
  public PlaybackInfo copyWithLoadingMediaPeriodId(MediaPeriodId loadingMediaPeriodId) {
    return new PlaybackInfo(
        timeline,
        periodId,
        startPositionUs,
        contentPositionUs,
        playbackState,
        playbackError,
        isLoading,
        trackGroups,
        trackSelectorResult,
        loadingMediaPeriodId,
        bufferedPositionUs,
        totalBufferedDurationUs,
        positionUs);
  }
}