1 /*
2  * Copyright (C) 2014 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 WEBMELEMENT_H_
18 #define WEBMELEMENT_H_
19 
20 #include <media/stagefright/MediaBuffer.h>
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/foundation/ABuffer.h>
23 #include <utils/List.h>
24 
25 namespace android {
26 
27 struct WebmElement : public LightRefBase<WebmElement> {
28     const uint64_t mId, mSize;
29 
30     WebmElement(uint64_t id, uint64_t size);
31     virtual ~WebmElement();
32 
33     virtual int serializePayloadSize(uint8_t *buf);
34     virtual void serializePayload(uint8_t *buf)=0;
35     uint64_t totalSize();
36     uint64_t serializeInto(uint8_t *buf);
37     uint8_t *serialize(uint64_t& size);
38     int write(int fd, uint64_t& size);
39 
40     static sp<WebmElement> EbmlHeader(
41             int ver = 1,
42             int readVer = 1,
43             int maxIdLen = 4,
44             int maxSizeLen = 8,
45             int docVer = 2,
46             int docReadVer = 2);
47 
48     static sp<WebmElement> SegmentInfo(uint64_t scale = 1000000, double dur = 0);
49 
50     static sp<WebmElement> AudioTrackEntry(
51             int chans,
52             double rate,
53             const sp<ABuffer> &buf,
54             int bps = 0,
55             uint64_t uid = 0,
56             bool lacing = false,
57             const char *lang = "und");
58 
59     static sp<WebmElement> VideoTrackEntry(
60             uint64_t width,
61             uint64_t height,
62             uint64_t uid = 0,
63             bool lacing = false,
64             const char *lang = "und");
65 
66     static sp<WebmElement> SeekEntry(uint64_t id, uint64_t off);
67     static sp<WebmElement> CuePointEntry(uint64_t time, int track, uint64_t off);
68     static sp<WebmElement> SimpleBlock(
69             int trackNum,
70             int16_t timecode,
71             bool key,
72             const uint8_t *data,
73             uint64_t dataSize);
74 };
75 
76 struct WebmUnsigned : public WebmElement {
77     WebmUnsigned(uint64_t id, uint64_t value);
78     const uint64_t mValue;
79     void serializePayload(uint8_t *buf);
80 };
81 
82 struct WebmFloat : public WebmElement {
83     const double mValue;
84     WebmFloat(uint64_t id, float value);
85     WebmFloat(uint64_t id, double value);
86     void serializePayload(uint8_t *buf);
87 };
88 
89 struct WebmBinary : public WebmElement {
90     const sp<ABuffer> mRef;
91     WebmBinary(uint64_t id, const sp<ABuffer> &ref);
92     void serializePayload(uint8_t *buf);
93 };
94 
95 struct WebmString : public WebmElement {
96     const char *const mStr;
97     WebmString(uint64_t id, const char *str);
98     void serializePayload(uint8_t *buf);
99 };
100 
101 struct WebmSimpleBlock : public WebmElement {
102     const int mTrackNum;
103     const int16_t mRelTimecode;
104     const bool mKey;
105     const sp<ABuffer> mRef;
106 
107     WebmSimpleBlock(int trackNum, int16_t timecode, bool key, const sp<ABuffer>& orig);
108     void serializePayload(uint8_t *buf);
109 };
110 
111 struct EbmlVoid : public WebmElement {
112     const uint64_t mSizeWidth;
113     EbmlVoid(uint64_t totalSize);
114     int serializePayloadSize(uint8_t *buf);
115     void serializePayload(uint8_t *buf);
116 };
117 
118 struct WebmMaster : public WebmElement {
119     const List<sp<WebmElement> > mChildren;
120     WebmMaster(uint64_t id);
121     WebmMaster(uint64_t id, const List<sp<WebmElement> > &children);
122     int serializePayloadSize(uint8_t *buf);
123     void serializePayload(uint8_t *buf);
124 };
125 
126 } /* namespace android */
127 #endif /* WEBMELEMENT_H_ */
128