1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media.tv; 18 19 import android.annotation.NonNull; 20 import android.media.MediaCodec.BufferFlag; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.os.SharedMemory; 24 25 import java.io.IOException; 26 27 /** 28 * Buffer for advertisement data. 29 */ 30 public final class AdBuffer implements Parcelable { 31 private final int mId; 32 @NonNull 33 private final String mMimeType; 34 @NonNull 35 private final SharedMemory mBuffer; 36 private final int mOffset; 37 private final int mLength; 38 private final long mPresentationTimeUs; 39 private final int mFlags; 40 AdBuffer( int id, @NonNull String mimeType, @NonNull SharedMemory buffer, int offset, int length, long presentationTimeUs, @BufferFlag int flags)41 public AdBuffer( 42 int id, 43 @NonNull String mimeType, 44 @NonNull SharedMemory buffer, 45 int offset, 46 int length, 47 long presentationTimeUs, 48 @BufferFlag int flags) { 49 this.mId = id; 50 this.mMimeType = mimeType; 51 com.android.internal.util.AnnotationValidations.validate( 52 NonNull.class, null, mimeType); 53 this.mBuffer = buffer; 54 com.android.internal.util.AnnotationValidations.validate( 55 NonNull.class, null, buffer); 56 this.mOffset = offset; 57 this.mLength = length; 58 this.mPresentationTimeUs = presentationTimeUs; 59 this.mFlags = flags; 60 } 61 62 /** @hide **/ dupAdBuffer(AdBuffer buffer)63 public static AdBuffer dupAdBuffer(AdBuffer buffer) throws IOException { 64 if (buffer == null) { 65 return null; 66 } 67 return new AdBuffer(buffer.mId, buffer.mMimeType, 68 SharedMemory.fromFileDescriptor(buffer.mBuffer.getFdDup()), buffer.mOffset, 69 buffer.mLength, buffer.mPresentationTimeUs, buffer.mFlags); 70 } 71 72 /** 73 * Gets corresponding AD request ID. 74 * 75 * @return The ID of the ad request 76 */ getId()77 public int getId() { 78 return mId; 79 } 80 81 /** 82 * Gets the mime type of the data. 83 * 84 * @return The mime type of the data. 85 */ 86 @NonNull getMimeType()87 public String getMimeType() { 88 return mMimeType; 89 } 90 91 /** 92 * Gets the {@link SharedMemory} which stores the data. 93 * 94 * <p> Information on how the data in this buffer is formatted can be found using 95 * {@link AdRequest#getMetadata()} 96 * <p> This data lives in a {@link SharedMemory} instance because of the 97 * potentially large amount of data needed to store the ad. This optimizes the 98 * data communication between the ad data source and the service responsible for 99 * its display. 100 * 101 * @see SharedMemory#create(String, int) 102 * @return The {@link SharedMemory} that stores the data for this ad buffer. 103 */ 104 @NonNull getSharedMemory()105 public SharedMemory getSharedMemory() { 106 return mBuffer; 107 } 108 109 /** 110 * Gets the offset into the shared memory to begin mapping. 111 * 112 * @see SharedMemory#map(int, int, int) 113 * @return The offset of this ad buffer in the shared memory in bytes. 114 */ getOffset()115 public int getOffset() { 116 return mOffset; 117 } 118 119 /** 120 * Gets the data length of this ad buffer. 121 * 122 * @return The data length of this ad buffer in bytes. 123 */ getLength()124 public int getLength() { 125 return mLength; 126 } 127 128 /** 129 * Gets the presentation time. 130 * 131 * @return The presentation time in microseconds. 132 */ getPresentationTimeUs()133 public long getPresentationTimeUs() { 134 return mPresentationTimeUs; 135 } 136 137 /** 138 * Gets the buffer flags for this ad buffer. 139 * 140 * @see android.media.MediaCodec 141 * @return The buffer flags for this ad buffer. 142 */ 143 @BufferFlag getFlags()144 public int getFlags() { 145 return mFlags; 146 } 147 148 @Override writeToParcel(@onNull android.os.Parcel dest, int flags)149 public void writeToParcel(@NonNull android.os.Parcel dest, int flags) { 150 dest.writeInt(mId); 151 dest.writeString(mMimeType); 152 dest.writeTypedObject(mBuffer, flags); 153 dest.writeInt(mOffset); 154 dest.writeInt(mLength); 155 dest.writeLong(mPresentationTimeUs); 156 dest.writeInt(mFlags); 157 } 158 159 @Override describeContents()160 public int describeContents() { 161 return 0; 162 } 163 AdBuffer(@onNull Parcel in)164 private AdBuffer(@NonNull Parcel in) { 165 int id = in.readInt(); 166 String mimeType = in.readString(); 167 SharedMemory buffer = (SharedMemory) in.readTypedObject(SharedMemory.CREATOR); 168 int offset = in.readInt(); 169 int length = in.readInt(); 170 long presentationTimeUs = in.readLong(); 171 int flags = in.readInt(); 172 173 this.mId = id; 174 this.mMimeType = mimeType; 175 com.android.internal.util.AnnotationValidations.validate( 176 NonNull.class, null, mMimeType); 177 this.mBuffer = buffer; 178 com.android.internal.util.AnnotationValidations.validate( 179 NonNull.class, null, mBuffer); 180 this.mOffset = offset; 181 this.mLength = length; 182 this.mPresentationTimeUs = presentationTimeUs; 183 this.mFlags = flags; 184 } 185 186 public static final @NonNull Parcelable.Creator<AdBuffer> CREATOR = 187 new Parcelable.Creator<AdBuffer>() { 188 @Override 189 public AdBuffer[] newArray(int size) { 190 return new AdBuffer[size]; 191 } 192 193 @Override 194 public AdBuffer createFromParcel(Parcel in) { 195 return new AdBuffer(in); 196 } 197 }; 198 } 199