From 021d725c0a1642cc5610b7c7d0ea2d6e1776fafd Mon Sep 17 00:00:00 2001 From: kimvde Date: Mon, 19 Oct 2020 16:49:38 +0100 Subject: [PATCH] Add motion photo metadata entry PiperOrigin-RevId: 337863184 --- .../metadata}/mp4/MdtaMetadataEntry.java | 2 +- .../exoplayer2/metadata/mp4/MotionPhoto.java | 116 ++++++++++++++++++ .../exoplayer2/metadata/mp4/package-info.java | 19 +++ .../metadata}/mp4/MdtaMetadataEntryTest.java | 2 +- .../metadata/mp4/MotionPhotoTest.java | 48 ++++++++ .../extractor/mp4/MetadataUtil.java | 1 + 6 files changed, 186 insertions(+), 2 deletions(-) rename library/{extractor/src/main/java/com/google/android/exoplayer2/extractor => common/src/main/java/com/google/android/exoplayer2/metadata}/mp4/MdtaMetadataEntry.java (98%) create mode 100644 library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MotionPhoto.java create mode 100644 library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/package-info.java rename library/{extractor/src/test/java/com/google/android/exoplayer2/extractor => common/src/test/java/com/google/android/exoplayer2/metadata}/mp4/MdtaMetadataEntryTest.java (96%) create mode 100644 library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MotionPhotoTest.java diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntry.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntry.java similarity index 98% rename from library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntry.java rename to library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntry.java index 9a584832ef..913056f3c2 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntry.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntry.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.android.exoplayer2.extractor.mp4; +package com.google.android.exoplayer2.metadata.mp4; import android.os.Parcel; import android.os.Parcelable; diff --git a/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MotionPhoto.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MotionPhoto.java new file mode 100644 index 0000000000..ca1a110c61 --- /dev/null +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/MotionPhoto.java @@ -0,0 +1,116 @@ +/* + * 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; + +/** Metadata of a motion photo file. */ +public final class MotionPhoto implements Metadata.Entry { + + /** The start offset of the photo data, in bytes. */ + public final int photoStartPosition; + /** The size of the photo data, in bytes. */ + public final int photoSize; + /** The start offset of the video data, in bytes. */ + public final int videoStartPosition; + /** The size of the video data, in bytes. */ + public final int videoSize; + + /** Creates an instance. */ + public MotionPhoto(int photoStartPosition, int photoSize, int videoStartPosition, int videoSize) { + this.photoStartPosition = photoStartPosition; + this.photoSize = photoSize; + this.videoStartPosition = videoStartPosition; + this.videoSize = videoSize; + } + + private MotionPhoto(Parcel in) { + photoStartPosition = in.readInt(); + photoSize = in.readInt(); + videoStartPosition = in.readInt(); + videoSize = in.readInt(); + } + + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + MotionPhoto other = (MotionPhoto) obj; + return photoStartPosition == other.photoStartPosition + && photoSize == other.photoSize + && videoStartPosition == other.videoStartPosition + && videoSize == other.videoSize; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + photoStartPosition; + result = 31 * result + photoSize; + result = 31 * result + videoStartPosition; + result = 31 * result + videoSize; + return result; + } + + @Override + public String toString() { + return "Motion photo: photoStartPosition=" + + photoStartPosition + + ", photoSize=" + + photoSize + + ", videoStartPosition=" + + videoStartPosition + + ", videoSize=" + + videoSize; + } + + // Parcelable implementation. + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(photoStartPosition); + dest.writeInt(photoSize); + dest.writeInt(videoStartPosition); + dest.writeInt(videoSize); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + + @Override + public MotionPhoto createFromParcel(Parcel in) { + return new MotionPhoto(in); + } + + @Override + public MotionPhoto[] newArray(int size) { + return new MotionPhoto[size]; + } + }; +} diff --git a/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/package-info.java b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/package-info.java new file mode 100644 index 0000000000..8ddf4040c1 --- /dev/null +++ b/library/common/src/main/java/com/google/android/exoplayer2/metadata/mp4/package-info.java @@ -0,0 +1,19 @@ +/* + * 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. + */ +@NonNullApi +package com.google.android.exoplayer2.metadata.mp4; + +import com.google.android.exoplayer2.util.NonNullApi; diff --git a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntryTest.java b/library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntryTest.java similarity index 96% rename from library/extractor/src/test/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntryTest.java rename to library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntryTest.java index 6fba801355..15b2c112e6 100644 --- a/library/extractor/src/test/java/com/google/android/exoplayer2/extractor/mp4/MdtaMetadataEntryTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MdtaMetadataEntryTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.android.exoplayer2.extractor.mp4; +package com.google.android.exoplayer2.metadata.mp4; import static com.google.common.truth.Truth.assertThat; diff --git a/library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MotionPhotoTest.java b/library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MotionPhotoTest.java new file mode 100644 index 0000000000..596ae210f5 --- /dev/null +++ b/library/common/src/test/java/com/google/android/exoplayer2/metadata/mp4/MotionPhotoTest.java @@ -0,0 +1,48 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + +import android.os.Parcel; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Test for {@link MotionPhoto}. */ +@RunWith(AndroidJUnit4.class) +public class MotionPhotoTest { + + @Test + public void parcelable() { + MotionPhoto motionPhotoToParcel = + new MotionPhoto( + /* photoStartPosition= */ 0, + /* photoSize= */ 10, + /* videoStartPosition= */ 15, + /* videoSize= */ 35); + + Parcel parcel = Parcel.obtain(); + motionPhotoToParcel.writeToParcel(parcel, /* flags= */ 0); + parcel.setDataPosition(0); + + MotionPhoto motionPhotoFromParcel = MotionPhoto.CREATOR.createFromParcel(parcel); + assertThat(motionPhotoFromParcel).isEqualTo(motionPhotoToParcel); + + parcel.recycle(); + } +} diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MetadataUtil.java b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MetadataUtil.java index 32667689c7..b61b5134b2 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MetadataUtil.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/extractor/mp4/MetadataUtil.java @@ -28,6 +28,7 @@ import com.google.android.exoplayer2.metadata.id3.CommentFrame; import com.google.android.exoplayer2.metadata.id3.Id3Frame; import com.google.android.exoplayer2.metadata.id3.InternalFrame; import com.google.android.exoplayer2.metadata.id3.TextInformationFrame; +import com.google.android.exoplayer2.metadata.mp4.MdtaMetadataEntry; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.ParsableByteArray; import java.util.ArrayList;