OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_MP4_BOX_READER_H_ | 5 #ifndef MEDIA_MP4_BOX_READER_H_ |
6 #define MEDIA_MP4_BOX_READER_H_ | 6 #define MEDIA_MP4_BOX_READER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "media/base/media_export.h" | 13 #include "media/base/media_export.h" |
| 14 #include "media/base/media_log.h" |
14 #include "media/mp4/fourccs.h" | 15 #include "media/mp4/fourccs.h" |
15 #include "media/mp4/rcheck.h" | 16 #include "media/mp4/rcheck.h" |
16 | 17 |
17 namespace media { | 18 namespace media { |
18 namespace mp4 { | 19 namespace mp4 { |
19 | 20 |
20 class BoxReader; | 21 class BoxReader; |
21 | 22 |
22 struct MEDIA_EXPORT Box { | 23 struct MEDIA_EXPORT Box { |
23 virtual ~Box(); | 24 virtual ~Box(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 ~BoxReader(); | 72 ~BoxReader(); |
72 | 73 |
73 // Create a BoxReader from a buffer. Note that this function may return NULL | 74 // Create a BoxReader from a buffer. Note that this function may return NULL |
74 // if an intact, complete box was not available in the buffer. If |*err| is | 75 // if an intact, complete box was not available in the buffer. If |*err| is |
75 // set, there was a stream-level error when creating the box; otherwise, NULL | 76 // set, there was a stream-level error when creating the box; otherwise, NULL |
76 // values are only expected when insufficient data is available. | 77 // values are only expected when insufficient data is available. |
77 // | 78 // |
78 // |buf| is retained but not owned, and must outlive the BoxReader instance. | 79 // |buf| is retained but not owned, and must outlive the BoxReader instance. |
79 static BoxReader* ReadTopLevelBox(const uint8* buf, | 80 static BoxReader* ReadTopLevelBox(const uint8* buf, |
80 const int buf_size, | 81 const int buf_size, |
| 82 const LogCB& log_cb, |
81 bool* err); | 83 bool* err); |
82 | 84 |
83 // Read the box header from the current buffer. This function returns true if | 85 // Read the box header from the current buffer. This function returns true if |
84 // there is enough data to read the header and the header is sane; that is, it | 86 // there is enough data to read the header and the header is sane; that is, it |
85 // does not check to ensure the entire box is in the buffer before returning | 87 // does not check to ensure the entire box is in the buffer before returning |
86 // true. The semantics of |*err| are the same as above. | 88 // true. The semantics of |*err| are the same as above. |
87 // | 89 // |
88 // |buf| is not retained. | 90 // |buf| is not retained. |
89 static bool StartTopLevelBox(const uint8* buf, | 91 static bool StartTopLevelBox(const uint8* buf, |
90 const int buf_size, | 92 const int buf_size, |
| 93 const LogCB& log_cb, |
91 FourCC* type, | 94 FourCC* type, |
92 int* box_size, | 95 int* box_size, |
93 bool* err) WARN_UNUSED_RESULT; | 96 bool* err) WARN_UNUSED_RESULT; |
94 | 97 |
95 // Returns true if |type| is recognized to be a top-level box, false | 98 // Returns true if |type| is recognized to be a top-level box, false |
96 // otherwise. This returns true for some boxes which we do not parse. | 99 // otherwise. This returns true for some boxes which we do not parse. |
97 // Helpful in debugging misaligned appends. | 100 // Helpful in debugging misaligned appends. |
98 static bool IsValidTopLevelBox(const FourCC& type); | 101 static bool IsValidTopLevelBox(const FourCC& type, |
| 102 const LogCB& log_cb); |
99 | 103 |
100 // Scan through all boxes within the current box, starting at the current | 104 // Scan through all boxes within the current box, starting at the current |
101 // buffer position. Must be called before any of the *Child functions work. | 105 // buffer position. Must be called before any of the *Child functions work. |
102 bool ScanChildren() WARN_UNUSED_RESULT; | 106 bool ScanChildren() WARN_UNUSED_RESULT; |
103 | 107 |
104 // Read exactly one child box from the set of children. The type of the child | 108 // Read exactly one child box from the set of children. The type of the child |
105 // will be determined by the BoxType() method of |child|. | 109 // will be determined by the BoxType() method of |child|. |
106 bool ReadChild(Box* child) WARN_UNUSED_RESULT; | 110 bool ReadChild(Box* child) WARN_UNUSED_RESULT; |
107 | 111 |
108 // Read one child if available. Returns false on error, true on successful | 112 // Read one child if available. Returns false on error, true on successful |
(...skipping 17 matching lines...) Expand all Loading... |
126 // Populate the values of 'version()' and 'flags()' from a full box header. | 130 // Populate the values of 'version()' and 'flags()' from a full box header. |
127 // Many boxes, but not all, use these values. This call should happen after | 131 // Many boxes, but not all, use these values. This call should happen after |
128 // the box has been initialized, and does not re-read the main box header. | 132 // the box has been initialized, and does not re-read the main box header. |
129 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; | 133 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; |
130 | 134 |
131 FourCC type() const { return type_; } | 135 FourCC type() const { return type_; } |
132 uint8 version() const { return version_; } | 136 uint8 version() const { return version_; } |
133 uint32 flags() const { return flags_; } | 137 uint32 flags() const { return flags_; } |
134 | 138 |
135 private: | 139 private: |
136 BoxReader(const uint8* buf, const int size); | 140 BoxReader(const uint8* buf, const int size, const LogCB& log_cb); |
137 | 141 |
138 // Must be called immediately after init. If the return is false, this | 142 // Must be called immediately after init. If the return is false, this |
139 // indicates that the box header and its contents were not available in the | 143 // indicates that the box header and its contents were not available in the |
140 // stream or were nonsensical, and that the box must not be used further. In | 144 // stream or were nonsensical, and that the box must not be used further. In |
141 // this case, if |*err| is false, the problem was simply a lack of data, and | 145 // this case, if |*err| is false, the problem was simply a lack of data, and |
142 // should only be an error condition if some higher-level component knows that | 146 // should only be an error condition if some higher-level component knows that |
143 // no more data is coming (i.e. EOS or end of containing box). If |*err| is | 147 // no more data is coming (i.e. EOS or end of containing box). If |*err| is |
144 // true, the error is unrecoverable and the stream should be aborted. | 148 // true, the error is unrecoverable and the stream should be aborted. |
145 bool ReadHeader(bool* err); | 149 bool ReadHeader(bool* err); |
146 | 150 |
| 151 LogCB log_cb_; |
147 FourCC type_; | 152 FourCC type_; |
148 uint8 version_; | 153 uint8 version_; |
149 uint32 flags_; | 154 uint32 flags_; |
150 | 155 |
151 typedef std::multimap<FourCC, BoxReader> ChildMap; | 156 typedef std::multimap<FourCC, BoxReader> ChildMap; |
152 | 157 |
153 // The set of child box FourCCs and their corresponding buffer readers. Only | 158 // The set of child box FourCCs and their corresponding buffer readers. Only |
154 // valid if scanned_ is true. | 159 // valid if scanned_ is true. |
155 ChildMap children_; | 160 ChildMap children_; |
156 bool scanned_; | 161 bool scanned_; |
(...skipping 28 matching lines...) Expand all Loading... |
185 return true; | 190 return true; |
186 } | 191 } |
187 | 192 |
188 template<typename T> | 193 template<typename T> |
189 bool BoxReader::ReadAllChildren(std::vector<T>* children) { | 194 bool BoxReader::ReadAllChildren(std::vector<T>* children) { |
190 DCHECK(!scanned_); | 195 DCHECK(!scanned_); |
191 scanned_ = true; | 196 scanned_ = true; |
192 | 197 |
193 bool err = false; | 198 bool err = false; |
194 while (pos() < size()) { | 199 while (pos() < size()) { |
195 BoxReader child_reader(&buf_[pos_], size_ - pos_); | 200 BoxReader child_reader(&buf_[pos_], size_ - pos_, log_cb_); |
196 if (!child_reader.ReadHeader(&err)) break; | 201 if (!child_reader.ReadHeader(&err)) break; |
197 T child; | 202 T child; |
198 RCHECK(child.Parse(&child_reader)); | 203 RCHECK(child.Parse(&child_reader)); |
199 children->push_back(child); | 204 children->push_back(child); |
200 pos_ += child_reader.size(); | 205 pos_ += child_reader.size(); |
201 } | 206 } |
202 | 207 |
203 return !err; | 208 return !err; |
204 } | 209 } |
205 | 210 |
206 } // namespace mp4 | 211 } // namespace mp4 |
207 } // namespace media | 212 } // namespace media |
208 | 213 |
209 #endif // MEDIA_MP4_BOX_READER_H_ | 214 #endif // MEDIA_MP4_BOX_READER_H_ |
OLD | NEW |