1 /* 2 * Copyright 2019 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.tuner.filter; 18 19 import android.annotation.BytesLong; 20 import android.annotation.FlaggedApi; 21 import android.annotation.IntRange; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.SystemApi; 25 import android.media.AudioPresentation; 26 import android.media.MediaCodec.LinearBlock; 27 import android.media.tv.flags.Flags; 28 29 import java.util.Collections; 30 import java.util.List; 31 32 /** 33 * Filter event sent from {@link Filter} objects with media type. 34 * 35 * @hide 36 */ 37 @SystemApi 38 public class MediaEvent extends FilterEvent { 39 private long mNativeContext; 40 private boolean mReleased = false; 41 private final Object mLock = new Object(); 42 nativeGetAudioHandle()43 private native Long nativeGetAudioHandle(); nativeGetLinearBlock()44 private native LinearBlock nativeGetLinearBlock(); nativeFinalize()45 private native void nativeFinalize(); 46 47 private final int mStreamId; 48 private final boolean mIsPtsPresent; 49 private final long mPts; 50 private final boolean mIsDtsPresent; 51 private final long mDts; 52 private final long mDataLength; 53 private final long mOffset; 54 private LinearBlock mLinearBlock; 55 private final boolean mIsSecureMemory; 56 private final long mDataId; 57 private final int mMpuSequenceNumber; 58 private final boolean mIsPrivateData; 59 private final int mScIndexMask; 60 private final AudioDescriptor mExtraMetaData; 61 private final List<AudioPresentation> mAudioPresentations; 62 private final int mNumDataPieces; 63 private final int mIndexInDataGroup; 64 private final int mDataGroupId; 65 66 // This constructor is used by JNI code only MediaEvent(int streamId, boolean isPtsPresent, long pts, boolean isDtsPresent, long dts, long dataLength, long offset, LinearBlock buffer, boolean isSecureMemory, long dataId, int mpuSequenceNumber, boolean isPrivateData, int scIndexMask, AudioDescriptor extraMetaData, List<AudioPresentation> audioPresentations, int numDataPieces, int indexInDataGroup, int dataGroupId)67 private MediaEvent(int streamId, boolean isPtsPresent, long pts, boolean isDtsPresent, long dts, 68 long dataLength, long offset, LinearBlock buffer, boolean isSecureMemory, long dataId, 69 int mpuSequenceNumber, boolean isPrivateData, int scIndexMask, 70 AudioDescriptor extraMetaData, List<AudioPresentation> audioPresentations, 71 int numDataPieces, int indexInDataGroup, int dataGroupId) { 72 mStreamId = streamId; 73 mIsPtsPresent = isPtsPresent; 74 mPts = pts; 75 mIsDtsPresent = isDtsPresent; 76 mDts = dts; 77 mDataLength = dataLength; 78 mOffset = offset; 79 mLinearBlock = buffer; 80 mIsSecureMemory = isSecureMemory; 81 mDataId = dataId; 82 mMpuSequenceNumber = mpuSequenceNumber; 83 mIsPrivateData = isPrivateData; 84 mScIndexMask = scIndexMask; 85 mExtraMetaData = extraMetaData; 86 mAudioPresentations = audioPresentations; 87 mNumDataPieces = numDataPieces; 88 mIndexInDataGroup = indexInDataGroup; 89 mDataGroupId = dataGroupId; 90 } 91 92 /** 93 * Gets stream ID. 94 */ getStreamId()95 public int getStreamId() { 96 return mStreamId; 97 } 98 99 /** 100 * Returns whether PTS (Presentation Time Stamp) is present. 101 * 102 * @return {@code true} if PTS is present in PES header; {@code false} otherwise. 103 */ isPtsPresent()104 public boolean isPtsPresent() { 105 return mIsPtsPresent; 106 } 107 108 /** 109 * Gets PTS (Presentation Time Stamp) for audio or video frame. 110 */ getPts()111 public long getPts() { 112 return mPts; 113 } 114 115 /** 116 * Returns whether DTS (Decode Time Stamp) is present. 117 * 118 * <p>This query is only supported in Tuner 2.0 or higher version. Unsupported version will 119 * return {@code false}. 120 * Use {@link TunerVersionChecker#getTunerVersion()} to get the version information. 121 * 122 * @return {@code true} if DTS is present in PES header; {@code false} otherwise. 123 */ isDtsPresent()124 public boolean isDtsPresent() { return mIsDtsPresent; } 125 126 /** 127 * Gets DTS (Decode Time Stamp) for audio or video frame. 128 * 129 * * <p>This query is only supported in Tuner 2.0 or higher version. Unsupported version will 130 * return {@code -1}. 131 * Use {@link TunerVersionChecker#getTunerVersion()} to get the version information. 132 */ getDts()133 public long getDts() { return mDts; } 134 135 /** 136 * Gets data size in bytes of audio or video frame. 137 */ 138 @BytesLong getDataLength()139 public long getDataLength() { 140 return mDataLength; 141 } 142 143 /** 144 * The offset in the memory block which is shared among multiple Media Events. 145 */ 146 @BytesLong getOffset()147 public long getOffset() { 148 return mOffset; 149 } 150 151 /** 152 * Gets a linear block associated to the memory where audio or video data stays. 153 */ 154 @Nullable getLinearBlock()155 public LinearBlock getLinearBlock() { 156 synchronized (mLock) { 157 if (mLinearBlock == null) { 158 mLinearBlock = nativeGetLinearBlock(); 159 } 160 return mLinearBlock; 161 } 162 } 163 164 /** 165 * Returns whether the data is secure. 166 * 167 * @return {@code true} if the data is in secure area, and isn't mappable; 168 * {@code false} otherwise. 169 */ isSecureMemory()170 public boolean isSecureMemory() { 171 return mIsSecureMemory; 172 } 173 174 /** 175 * Gets the ID which is used by HAL to provide additional information for AV data. 176 * 177 * <p>For secure audio, it's the audio handle used by Audio Track. 178 */ getAvDataId()179 public long getAvDataId() { 180 return mDataId; 181 } 182 183 /** 184 * Gets the audio handle. 185 * 186 * <p>Client gets audio handle from {@link MediaEvent}, and queues it to 187 * {@link android.media.AudioTrack} in 188 * {@link android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE} format. 189 * 190 * @return the audio handle. 191 * @see android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE 192 */ getAudioHandle()193 public long getAudioHandle() { 194 nativeGetAudioHandle(); 195 return mDataId; 196 } 197 198 /** 199 * Gets MPU sequence number of filtered data. 200 */ 201 @IntRange(from = 0) getMpuSequenceNumber()202 public int getMpuSequenceNumber() { 203 return mMpuSequenceNumber; 204 } 205 206 /** 207 * Returns whether the data is private. 208 * 209 * @return {@code true} if the data is in private; {@code false} otherwise. 210 */ isPrivateData()211 public boolean isPrivateData() { 212 return mIsPrivateData; 213 } 214 215 /** 216 * Gets SC (Start Code) index mask. 217 * 218 * <p>This API is only supported by Tuner HAL 2.0 or higher. Unsupported version would return 219 * {@code 0}. Use {@link TunerVersionChecker#getTunerVersion()} to check the version. 220 */ 221 @RecordSettings.ScIndexMask getScIndexMask()222 public int getScIndexMask() { 223 return mScIndexMask; 224 } 225 226 /** 227 * Gets audio extra metadata. 228 */ 229 @Nullable getExtraMetaData()230 public AudioDescriptor getExtraMetaData() { 231 return mExtraMetaData; 232 } 233 234 /** 235 * Gets audio presentations. 236 * 237 * <p>The audio presentation order matters. As specified in ETSI EN 300 468 V1.17.1, all the 238 * audio programme components corresponding to the first audio preselection in the loop are 239 * contained in the main NGA stream. 240 */ 241 @NonNull getAudioPresentations()242 public List<AudioPresentation> getAudioPresentations() { 243 return mAudioPresentations == null ? Collections.emptyList() : mAudioPresentations; 244 } 245 246 /** 247 * Gets the number of data pieces into which the original data was split. 248 * 249 * <p>The {@link #getNumDataPieces()}, {@link #getIndexInDataGroup()} and 250 * {@link #getDataGroupId()} methods should be used together to reassemble the original data if 251 * it was split into pieces. Use {@link #getLinearBlock()} to get the memory where the data 252 * pieces are stored. 253 * 254 * @return 0 or 1 if this MediaEvent object contains the complete data; otherwise the number of 255 * pieces into which the original data was split. 256 * @see #getIndexInDataGroup() 257 * @see #getDataGroupId() 258 * @see #getLinearBlock() 259 */ 260 @FlaggedApi(Flags.FLAG_TUNER_W_APIS) 261 @IntRange(from = 0) getNumDataPieces()262 public int getNumDataPieces() { 263 return mNumDataPieces; 264 } 265 266 /** 267 * Gets the index of the data piece. The index in the data group indicates the order in which 268 * this {@link MediaEvent}'s data piece should be reassembled. The result should be within the 269 * range [0, {@link #getNumDataPieces()}). 270 * 271 * <p>The {@link #getNumDataPieces()}, {@link #getIndexInDataGroup()} and 272 * {@link #getDataGroupId()} methods should be used together to reassemble the original data if 273 * it was split into pieces. Use {@link #getLinearBlock()} to get the memory where the data 274 * pieces are stored. 275 * 276 * @return The index in the data group. 277 * @see #getNumDataPieces() 278 * @see #getDataGroupId() 279 * @see #getLinearBlock() 280 */ 281 @FlaggedApi(Flags.FLAG_TUNER_W_APIS) 282 @IntRange(from = 0) getIndexInDataGroup()283 public int getIndexInDataGroup() { 284 return mIndexInDataGroup; 285 } 286 287 /** 288 * Gets the group ID for reassembling the complete data. {@link MediaEvent}s that have the same 289 * data group ID contain different pieces of the same data. This value should be ignored if 290 * {@link #getNumDataPieces()} returns 0 or 1. 291 * 292 * <p>The {@link #getNumDataPieces()}, {@link #getIndexInDataGroup()} and 293 * {@link #getDataGroupId()} methods should be used together to reassemble the original data if 294 * it was split into pieces. Use {@link #getLinearBlock()} to get the memory where the data 295 * pieces are stored. 296 * 297 * @return The data group ID. 298 * @see #getNumDataPieces() 299 * @see #getIndexInDataGroup() 300 * @see #getLinearBlock() 301 */ 302 @FlaggedApi(Flags.FLAG_TUNER_W_APIS) getDataGroupId()303 public int getDataGroupId() { 304 return mDataGroupId; 305 } 306 307 /** 308 * Finalize the MediaEvent object. 309 * @hide 310 */ 311 @Override finalize()312 protected void finalize() { 313 release(); 314 } 315 316 /** 317 * Releases the MediaEvent object. 318 */ release()319 public void release() { 320 synchronized (mLock) { 321 if (mReleased) { 322 return; 323 } 324 nativeFinalize(); 325 mNativeContext = 0; 326 mReleased = true; 327 } 328 } 329 } 330