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 long mDataLength;
44     private final long mOffset;
45     private LinearBlock mLinearBlock;
46     private final boolean mIsSecureMemory;
47     private final long mDataId;
48     private final int mMpuSequenceNumber;
49     private final boolean mIsPrivateData;
50     private final AudioDescriptor mExtraMetaData;
51 
52     // This constructor is used by JNI code only
MediaEvent(int streamId, boolean isPtsPresent, long pts, long dataLength, long offset, LinearBlock buffer, boolean isSecureMemory, long dataId, int mpuSequenceNumber, boolean isPrivateData, AudioDescriptor extraMetaData)53     private MediaEvent(int streamId, boolean isPtsPresent, long pts, long dataLength, long offset,
54             LinearBlock buffer, boolean isSecureMemory, long dataId, int mpuSequenceNumber,
55             boolean isPrivateData, AudioDescriptor extraMetaData) {
56         mStreamId = streamId;
57         mIsPtsPresent = isPtsPresent;
58         mPts = pts;
59         mDataLength = dataLength;
60         mOffset = offset;
61         mLinearBlock = buffer;
62         mIsSecureMemory = isSecureMemory;
63         mDataId = dataId;
64         mMpuSequenceNumber = mpuSequenceNumber;
65         mIsPrivateData = isPrivateData;
66         mExtraMetaData = extraMetaData;
67     }
68 
69     /**
70      * Gets stream ID.
71      */
getStreamId()72     public int getStreamId() {
73         return mStreamId;
74     }
75 
76     /**
77      * Returns whether PTS (Presentation Time Stamp) is present.
78      *
79      * @return {@code true} if PTS is present in PES header; {@code false} otherwise.
80      */
isPtsPresent()81     public boolean isPtsPresent() {
82         return mIsPtsPresent;
83     }
84 
85     /**
86      * Gets PTS (Presentation Time Stamp) for audio or video frame.
87      */
getPts()88     public long getPts() {
89         return mPts;
90     }
91 
92     /**
93      * Gets data size in bytes of audio or video frame.
94      */
95     @BytesLong
getDataLength()96     public long getDataLength() {
97         return mDataLength;
98     }
99 
100     /**
101      * The offset in the memory block which is shared among multiple Media Events.
102      */
103     @BytesLong
getOffset()104     public long getOffset() {
105         return mOffset;
106     }
107 
108     /**
109      * Gets a linear block associated to the memory where audio or video data stays.
110      */
111     @Nullable
getLinearBlock()112     public LinearBlock getLinearBlock() {
113         synchronized (mLock) {
114             if (mLinearBlock == null) {
115                 mLinearBlock = nativeGetLinearBlock();
116             }
117             return mLinearBlock;
118         }
119     }
120 
121     /**
122      * Returns whether the data is secure.
123      *
124      * @return {@code true} if the data is in secure area, and isn't mappable;
125      *         {@code false} otherwise.
126      */
isSecureMemory()127     public boolean isSecureMemory() {
128         return mIsSecureMemory;
129     }
130 
131     /**
132      * Gets the ID which is used by HAL to provide additional information for AV data.
133      *
134      * <p>For secure audio, it's the audio handle used by Audio Track.
135      */
getAvDataId()136     public long getAvDataId() {
137         return mDataId;
138     }
139 
140     /**
141      * Gets the audio handle.
142      *
143      * <p>Client gets audio handle from {@link MediaEvent}, and queues it to
144      * {@link android.media.AudioTrack} in
145      * {@link android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE} format.
146      *
147      * @return the audio handle.
148      * @see android.media.AudioTrack#ENCAPSULATION_MODE_HANDLE
149      */
getAudioHandle()150     public long getAudioHandle() {
151         nativeGetAudioHandle();
152         return mDataId;
153     }
154 
155     /**
156      * Gets MPU sequence number of filtered data.
157      */
158     @IntRange(from = 0)
getMpuSequenceNumber()159     public int getMpuSequenceNumber() {
160         return mMpuSequenceNumber;
161     }
162 
163     /**
164      * Returns whether the data is private.
165      *
166      * @return {@code true} if the data is in private; {@code false} otherwise.
167      */
isPrivateData()168     public boolean isPrivateData() {
169         return mIsPrivateData;
170     }
171 
172     /**
173      * Gets audio extra metadata.
174      */
175     @Nullable
getExtraMetaData()176     public AudioDescriptor getExtraMetaData() {
177         return mExtraMetaData;
178     }
179 
180 
181     /**
182      * Finalize the MediaEvent object.
183      * @hide
184      */
185     @Override
finalize()186     protected void finalize() {
187         release();
188     }
189 
190     /**
191      * Releases the MediaEvent object.
192      */
release()193     public void release() {
194         synchronized (mLock) {
195             if (mReleased) {
196                 return;
197             }
198             nativeFinalize();
199             mNativeContext = 0;
200             mReleased = true;
201         }
202     }
203 }
204