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