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