/*
 * Copyright 2020 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.metadata.mp4;

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.List;

/** Holds information about the segments of slow motion playback within a track. */
public final class SlowMotionData implements Metadata.Entry {

  /** Holds information about a single segment of slow motion playback within a track. */
  public static final class Segment implements Parcelable {

    /** The start time, in milliseconds, of the track segment that is intended to be slow motion. */
    public final long startTimeMs;
    /** The end time, in milliseconds, of the track segment that is intended to be slow motion. */
    public final long endTimeMs;
    /**
     * The speed reduction factor.
     *
     * <p>For example, 4 would mean the segment should be played at a quarter (1/4) of the normal
     * speed.
     */
    public final int speedDivisor;

    /**
     * Creates an instance.
     *
     * @param startTimeMs See {@link #startTimeMs}.
     * @param endTimeMs See {@link #endTimeMs}.
     * @param speedDivisor See {@link #speedDivisor}.
     */
    public Segment(long startTimeMs, long endTimeMs, int speedDivisor) {
      this.startTimeMs = startTimeMs;
      this.endTimeMs = endTimeMs;
      this.speedDivisor = speedDivisor;
    }

    @Override
    public String toString() {
      return Util.formatInvariant(
          "Segment: startTimeMs=%d, endTimeMs=%d, speedDivisor=%d",
          startTimeMs, endTimeMs, speedDivisor);
    }

    @Override
    public boolean equals(@Nullable Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }
      Segment segment = (Segment) o;
      return startTimeMs == segment.startTimeMs
          && endTimeMs == segment.endTimeMs
          && speedDivisor == segment.speedDivisor;
    }

    @Override
    public int hashCode() {
      return Objects.hashCode(startTimeMs, endTimeMs, speedDivisor);
    }

    @Override
    public int describeContents() {
      return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
      dest.writeLong(startTimeMs);
      dest.writeLong(endTimeMs);
      dest.writeInt(speedDivisor);
    }

    public static final Creator<Segment> CREATOR =
        new Creator<Segment>() {

          @Override
          public Segment createFromParcel(Parcel in) {
            long startTimeMs = in.readLong();
            long endTimeMs = in.readLong();
            int speedDivisor = in.readInt();
            return new Segment(startTimeMs, endTimeMs, speedDivisor);
          }

          @Override
          public Segment[] newArray(int size) {
            return new Segment[size];
          }
        };
  }

  public final List<Segment> segments;

  /** Creates an instance with a list of {@link Segment}s. */
  public SlowMotionData(List<Segment> segments) {
    this.segments = segments;
  }

  @Override
  public String toString() {
    return "SlowMotion: segments=" + segments;
  }

  @Override
  public boolean equals(@Nullable Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    SlowMotionData that = (SlowMotionData) o;
    return segments.equals(that.segments);
  }

  @Override
  public int hashCode() {
    return segments.hashCode();
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(segments);
  }

  public static final Creator<SlowMotionData> CREATOR =
      new Creator<SlowMotionData>() {
        @Override
        public SlowMotionData createFromParcel(Parcel in) {
          List<Segment> slowMotionSegments = new ArrayList<>();
          in.readList(slowMotionSegments, Segment.class.getClassLoader());
          return new SlowMotionData(slowMotionSegments);
        }

        @Override
        public SlowMotionData[] newArray(int size) {
          return new SlowMotionData[size];
        }
      };
}

@@ -662,16 +662,92 @@
 a.mp4;%0A%0A
+import static com.google.android.exoplayer2.util.Assertions.checkArgument;%0A%0A
 import a
@@ -764,16 +764,16 @@
 Parcel;%0A
-
 import a
@@ -1884,32 +1884,61 @@
 k #startTimeMs%7D.
+ Must be less than endTimeMs.
 %0A     * @param e
@@ -2101,24 +2101,70 @@
 dDivisor) %7B%0A
+      checkArgument(startTimeMs %3C endTimeMs);%0A
       this.s
@@ -3786,16 +3786,21 @@
 ;%0A%0A  /**
+%0A   *
  Creates
@@ -3844,16 +3844,171 @@
 gment%7Ds.
+%0A   *%0A   * %3Cp%3EThe segments must not overlap, that is that the start time of a segment can not be between%0A   * the start and end time of another segment.%0A  
  */%0A  pu
@@ -4075,32 +4075,81 @@
 nts = segments;%0A
+    checkArgument(!doSegmentsOverlap(segments));%0A
   %7D%0A%0A  @Override
@@ -5249,19 +5249,19 @@
       %7D%0A
-
       %7D;
 %0A%7D%0A
@@ -5257,11 +5257,413 @@
       %7D;
+%0A%0A  private static boolean doSegmentsOverlap(List%3CSegment%3E segments) %7B%0A    if (segments.isEmpty()) %7B%0A      return false;%0A    %7D%0A    long previousEndTimeMs = segments.get(0).endTimeMs;%0A    for (int i = 1; i %3C segments.size(); i++) %7B%0A      if (segments.get(i).startTimeMs %3C previousEndTimeMs) %7B%0A        return true;%0A      %7D%0A      previousEndTimeMs = segments.get(i).endTimeMs;%0A    %7D%0A%0A    return false;%0A  %7D
 %0A%7D%0A

Enforce stricter SlowMotionData and Segment initialisation checks.