1 /*
2  * Copyright (C) 2010 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 #ifndef ES_QUEUE_H_
18 
19 #define ES_QUEUE_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/foundation/AMessage.h>
23 #include <utils/Errors.h>
24 #include <utils/List.h>
25 #include <utils/RefBase.h>
26 #include <vector>
27 
28 #include "SampleDecryptor.h"
29 
30 namespace android {
31 
32 struct ABuffer;
33 class MetaData;
34 
35 struct ElementaryStreamQueue {
36     enum Mode {
37         INVALID = 0,
38         H264,
39         AAC,
40         AC3,
41         EAC3,
42         AC4,
43         MPEG_AUDIO,
44         MPEG_VIDEO,
45         MPEG4_VIDEO,
46         PCM_AUDIO,
47         METADATA,
48     };
49 
50     enum Flags {
51         // Data appended to the queue is always at access unit boundaries.
52         kFlag_AlignedData = 1,
53         kFlag_ScrambledData = 2,
54         kFlag_SampleEncryptedData = 4,
55     };
56     explicit ElementaryStreamQueue(Mode mode, uint32_t flags = 0);
57 
58     status_t appendData(const void *data, size_t size,
59             int64_t timeUs, int32_t payloadOffset = 0,
60             uint32_t pesScramblingControl = 0);
61 
62     void appendScrambledData(
63             const void *data, size_t size,
64             size_t leadingClearBytes,
65             int32_t keyId, bool isSync,
66             sp<ABuffer> clearSizes, sp<ABuffer> encSizes);
67 
68     void signalEOS();
69     void clear(bool clearFormat);
70 
71     sp<ABuffer> dequeueAccessUnit();
72 
73     sp<MetaData> getFormat();
74 
75     bool isScrambled() const;
76 
77     void setCasInfo(int32_t systemId, const std::vector<uint8_t> &sessionId);
78 
79     void signalNewSampleAesKey(const sp<AMessage> &keyItem);
80 
81 private:
82     struct RangeInfo {
83         int64_t mTimestampUs;
84         size_t mLength;
85         int32_t mPesOffset;
86         uint32_t mPesScramblingControl;
87     };
88 
89     struct ScrambledRangeInfo {
90         size_t mLength;
91         size_t mLeadingClearBytes;
92         int32_t mKeyId;
93         int32_t mIsSync;
94         sp<ABuffer> mClearSizes;
95         sp<ABuffer> mEncSizes;
96     };
97 
98     Mode mMode;
99     uint32_t mFlags;
100     bool mEOSReached;
101 
102     sp<ABuffer> mBuffer;
103     List<RangeInfo> mRangeInfos;
104 
105     sp<ABuffer> mScrambledBuffer;
106     List<ScrambledRangeInfo> mScrambledRangeInfos;
107     int32_t mCASystemId;
108     std::vector<uint8_t> mCasSessionId;
109 
110     sp<MetaData> mFormat;
111 
112     sp<SampleDecryptor> mSampleDecryptor;
113     int mAUIndex;
114 
isSampleEncryptedElementaryStreamQueue115     bool isSampleEncrypted() const {
116         return (mFlags & kFlag_SampleEncryptedData) != 0;
117     }
118 
119     sp<ABuffer> dequeueAccessUnitH264();
120     sp<ABuffer> dequeueAccessUnitAAC();
121     sp<ABuffer> dequeueAccessUnitEAC3();
122     sp<ABuffer> dequeueAccessUnitAC4();
123     sp<ABuffer> dequeueAccessUnitMPEGAudio();
124     sp<ABuffer> dequeueAccessUnitMPEGVideo();
125     sp<ABuffer> dequeueAccessUnitMPEG4Video();
126     sp<ABuffer> dequeueAccessUnitPCMAudio();
127     sp<ABuffer> dequeueAccessUnitMetadata();
128 
129     // consume a logical (compressed) access unit of size "size",
130     // returns its timestamp in us (or -1 if no time information).
131     int64_t fetchTimestamp(size_t size,
132             int32_t *pesOffset = NULL,
133             int32_t *pesScramblingControl = NULL);
134 
135     sp<ABuffer> dequeueScrambledAccessUnit();
136 
137     DISALLOW_EVIL_CONSTRUCTORS(ElementaryStreamQueue);
138 };
139 
140 }  // namespace android
141 
142 #endif  // ES_QUEUE_H_
143