1 /*
2  * Copyright 2015 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;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 
22 /**
23  * Class that embodies one timed metadata access unit, including
24  *
25  * <ul>
26  * <li> a time stamp, and </li>
27  * <li> raw uninterpreted byte-array extracted directly from the container. </li>
28  * </ul>
29  *
30  * @see MediaPlayer#setOnTimedMetaDataAvailableListener(android.media.MediaPlayer.OnTimedMetaDataAvailableListener)
31  */
32 public final class TimedMetaData {
33     private static final String TAG = "TimedMetaData";
34 
35     private long mTimestampUs;
36     private byte[] mMetaData;
37 
38     /**
39      * @hide
40      */
createTimedMetaDataFromParcel(Parcel parcel)41     static TimedMetaData createTimedMetaDataFromParcel(Parcel parcel) {
42         return new TimedMetaData(parcel);
43     }
44 
TimedMetaData(Parcel parcel)45     private TimedMetaData(Parcel parcel) {
46         if (!parseParcel(parcel)) {
47             throw new IllegalArgumentException("parseParcel() fails");
48         }
49     }
50 
51     /**
52      * Constructor.
53      *
54      * @param timestampUs the timestamp in microsecond for the timed metadata
55      * @param metaData the metadata array for the timed metadata. No data copying is made.
56      *     It should not be null.
57      */
TimedMetaData(long timestampUs, @NonNull byte[] metaData)58     public TimedMetaData(long timestampUs, @NonNull byte[] metaData) {
59         if (metaData == null) {
60             throw new IllegalArgumentException("null metaData is not allowed");
61         }
62         mTimestampUs = timestampUs;
63         mMetaData = metaData;
64     }
65 
66     /**
67      * @return the timestamp associated with this metadata access unit in microseconds;
68      * 0 denotes playback start.
69      */
getTimestamp()70     public long getTimestamp() {
71         return mTimestampUs;
72     }
73 
74     /**
75      * @return raw, uninterpreted content of this metadata access unit; for ID3 tags this includes
76      * everything starting from the 3 byte signature "ID3".
77      */
getMetaData()78     public byte[] getMetaData() {
79         return mMetaData;
80     }
81 
parseParcel(Parcel parcel)82     private boolean parseParcel(Parcel parcel) {
83         parcel.setDataPosition(0);
84         if (parcel.dataAvail() == 0) {
85             return false;
86         }
87 
88         mTimestampUs = parcel.readLong();
89         mMetaData = new byte[parcel.readInt()];
90         parcel.readByteArray(mMetaData);
91 
92         return true;
93     }
94 }
95