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.IntRange;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.SystemApi;
24 import android.media.AudioPresentation;
25 import android.media.MediaCodec.LinearBlock;
26 
27 import java.util.Collections;
28 import java.util.List;
29 
30 /**
31  * Filter event sent from {@link Filter} objects with media type.
32  *
33  * @hide
34  */
35 @SystemApi
36 public class MediaEvent extends FilterEvent {
37     private long mNativeContext;
38     private boolean mReleased = false;
39     private final Object mLock = new Object();
40 
nativeGetAudioHandle()41     private native Long nativeGetAudioHandle();
nativeGetLinearBlock()42     private native LinearBlock nativeGetLinearBlock();
nativeFinalize()43     private native void nativeFinalize();
44 
45     private final int mStreamId;
46     private final boolean mIsPtsPresent;
47     private final long mPts;
48     private final boolean mIsDtsPresent;
49     private final long mDts;
50     private final long mDataLength;
51     private final long mOffset;
52     private LinearBlock mLinearBlock;
53     private final boolean mIsSecureMemory;
54     private final long mDataId;
55     private final int mMpuSequenceNumber;
56     private final boolean mIsPrivateData;
57     private final int mScIndexMask;
58     private final AudioDescriptor mExtraMetaData;
59     private final List<AudioPresentation> mAudioPresentations;
60 
61     // 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)62     private MediaEvent(int streamId, boolean isPtsPresent, long pts, boolean isDtsPresent, long dts,
63             long dataLength, long offset, LinearBlock buffer, boolean isSecureMemory, long dataId,
64             int mpuSequenceNumber, boolean isPrivateData, int scIndexMask,
65             AudioDescriptor extraMetaData, List<AudioPresentation> audioPresentations) {
66         mStreamId = streamId;
67         mIsPtsPresent = isPtsPresent;
68         mPts = pts;
69         mIsDtsPresent = isDtsPresent;
70         mDts = dts;
71         mDataLength = dataLength;
72         mOffset = offset;
73         mLinearBlock = buffer;
74         mIsSecureMemory = isSecureMemory;
75         mDataId = dataId;
76         mMpuSequenceNumber = mpuSequenceNumber;
77         mIsPrivateData = isPrivateData;
78         mScIndexMask = scIndexMask;
79         mExtraMetaData = extraMetaData;
80         mAudioPresentations = audioPresentations;
81     }
82 
83     /**
84      * Gets stream ID.
85      */
getStreamId()86     public int getStreamId() {
87         return mStreamId;
88     }
89 
90     /**
91      * Returns whether PTS (Presentation Time Stamp) is present.
92      *
93      * @return {@code true} if PTS is present in PES header; {@code false} otherwise.
94      */
isPtsPresent()95     public boolean isPtsPresent() {
96         return mIsPtsPresent;
97     }
98 
99     /**
100      * Gets PTS (Presentation Time Stamp) for audio or video frame.
101      */
getPts()102     public long getPts() {
103         return mPts;
104     }
105 
106     /**
107      * Returns whether DTS (Decode Time Stamp) is present.
108      *
109      * <p>This query is only supported in Tuner 2.0 or higher version. Unsupported version will
110      * return {@code false}.
111      * Use {@link TunerVersionChecker#getTunerVersion()} to get the version information.
112      *
113      * @return {@code true} if DTS is present in PES header; {@code false} otherwise.
114      */
isDtsPresent()115     public boolean isDtsPresent() { return mIsDtsPresent; }
116 
117     /**
118      * Gets DTS (Decode Time Stamp) for audio or video frame.
119      *
120      * * <p>This query is only supported in Tuner 2.0 or higher version. Unsupported version will
121      * return {@code -1}.
122      * Use {@link TunerVersionChecker#getTunerVersion()} to get the version information.
123      */
getDts()124     public long getDts() { return mDts; }
125 
126     /**
127      * Gets data size in bytes of audio or video frame.
128      */
129     @BytesLong
getDataLength()130     public long getDataLength() {
131         return mDataLength;
132     }
133 
134     /**
135      * The offset in the memory block which is shared among multiple Media Events.
136      */
137     @BytesLong
getOffset()138     public long getOffset() {
139         return mOffset;
140     }
141 
142     /**
143      * Gets a linear block associated to the memory where audio or video data stays.
144      */
145     @Nullable
getLinearBlock()146     public LinearBlock getLinearBlock() {
147         synchronized (mLock) {
148             if (mLinearBlock == null) {
149                 mLinearBlock = nativeGetLinearBlock();
150             }
151             return mLinearBlock;
152         }
153     }
154 
155     /**
156      * Returns whether the data is secure.
157      *
158      * @return {@code true} if the data is in secure area, and isn't mappable;
159      *         {@code false} otherwise.
160      */
isSecureMemory()161     public boolean isSecureMemory() {
162         return mIsSecureMemory;
163     }
164 
165     /**
166      * Gets the ID which is used by HAL to provide additional information for AV data.
167      *
168      * <p>For secure audio, it's the audio handle used by Audio Track.
169      */
getAvDataId()170     public long getAvDataId() {
171         return mDataId;
172     }
173 
174     /**
175      * Gets the audio handle.
176      *
177      * <p>Client gets audio handle from {@link MediaEvent}, and queues it to
178      * {@link android.media.AudioTrack} in
179      * {@link android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE} format.
180      *
181      * @return the audio handle.
182      * @see android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE
183      */
getAudioHandle()184     public long getAudioHandle() {
185         nativeGetAudioHandle();
186         return mDataId;
187     }
188 
189     /**
190      * Gets MPU sequence number of filtered data.
191      */
192     @IntRange(from = 0)
getMpuSequenceNumber()193     public int getMpuSequenceNumber() {
194         return mMpuSequenceNumber;
195     }
196 
197     /**
198      * Returns whether the data is private.
199      *
200      * @return {@code true} if the data is in private; {@code false} otherwise.
201      */
isPrivateData()202     public boolean isPrivateData() {
203         return mIsPrivateData;
204     }
205 
206     /**
207      * Gets SC (Start Code) index mask.
208      *
209      * <p>This API is only supported by Tuner HAL 2.0 or higher. Unsupported version would return
210      * {@code 0}. Use {@link TunerVersionChecker#getTunerVersion()} to check the version.
211      */
212     @RecordSettings.ScIndexMask
getScIndexMask()213     public int getScIndexMask() {
214         return mScIndexMask;
215     }
216 
217     /**
218      * Gets audio extra metadata.
219      */
220     @Nullable
getExtraMetaData()221     public AudioDescriptor getExtraMetaData() {
222         return mExtraMetaData;
223     }
224 
225     /**
226      * Gets audio presentations.
227      *
228      * <p>The audio presentation order matters. As specified in ETSI EN 300 468 V1.17.1, all the
229      * audio programme components corresponding to the first audio preselection in the loop are
230      * contained in the main NGA stream.
231      */
232     @NonNull
getAudioPresentations()233     public List<AudioPresentation> getAudioPresentations() {
234         return mAudioPresentations == null ? Collections.emptyList() : mAudioPresentations;
235     }
236 
237     /**
238      * Finalize the MediaEvent object.
239      * @hide
240      */
241     @Override
finalize()242     protected void finalize() {
243         release();
244     }
245 
246     /**
247      * Releases the MediaEvent object.
248      */
release()249     public void release() {
250         synchronized (mLock) {
251             if (mReleased) {
252                 return;
253             }
254             nativeFinalize();
255             mNativeContext = 0;
256             mReleased = true;
257         }
258     }
259 }
260