OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_FORMATS_WEBM_WEBM_PARSER_H_ | 5 #ifndef MEDIA_FORMATS_WEBM_WEBM_PARSER_H_ |
6 #define MEDIA_FORMATS_WEBM_WEBM_PARSER_H_ | 6 #define MEDIA_FORMATS_WEBM_WEBM_PARSER_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <string> | 10 #include <string> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/basictypes.h" | 13 #include "base/macros.h" |
12 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
13 | 15 |
14 namespace media { | 16 namespace media { |
15 | 17 |
16 // Interface for receiving WebM parser events. | 18 // Interface for receiving WebM parser events. |
17 // | 19 // |
18 // Each method is called when an element of the specified type is parsed. | 20 // Each method is called when an element of the specified type is parsed. |
19 // The ID of the element that was parsed is given along with the value | 21 // The ID of the element that was parsed is given along with the value |
20 // stored in the element. List elements generate calls at the start and | 22 // stored in the element. List elements generate calls at the start and |
21 // end of the list. Any pointers passed to these methods are only guaranteed | 23 // end of the list. Any pointers passed to these methods are only guaranteed |
22 // to be valid for the life of that call. Each method (except for OnListStart) | 24 // to be valid for the life of that call. Each method (except for OnListStart) |
23 // returns a bool that indicates whether the parsed data is valid. OnListStart | 25 // returns a bool that indicates whether the parsed data is valid. OnListStart |
24 // returns a pointer to a WebMParserClient object, which should be used to | 26 // returns a pointer to a WebMParserClient object, which should be used to |
25 // handle elements parsed out of the list being started. If false (or NULL by | 27 // handle elements parsed out of the list being started. If false (or NULL by |
26 // OnListStart) is returned then the parse is immediately terminated and an | 28 // OnListStart) is returned then the parse is immediately terminated and an |
27 // error is reported by the parser. | 29 // error is reported by the parser. |
28 class MEDIA_EXPORT WebMParserClient { | 30 class MEDIA_EXPORT WebMParserClient { |
29 public: | 31 public: |
30 virtual ~WebMParserClient(); | 32 virtual ~WebMParserClient(); |
31 | 33 |
32 virtual WebMParserClient* OnListStart(int id); | 34 virtual WebMParserClient* OnListStart(int id); |
33 virtual bool OnListEnd(int id); | 35 virtual bool OnListEnd(int id); |
34 virtual bool OnUInt(int id, int64 val); | 36 virtual bool OnUInt(int id, int64_t val); |
35 virtual bool OnFloat(int id, double val); | 37 virtual bool OnFloat(int id, double val); |
36 virtual bool OnBinary(int id, const uint8* data, int size); | 38 virtual bool OnBinary(int id, const uint8_t* data, int size); |
37 virtual bool OnString(int id, const std::string& str); | 39 virtual bool OnString(int id, const std::string& str); |
38 | 40 |
39 protected: | 41 protected: |
40 WebMParserClient(); | 42 WebMParserClient(); |
41 | 43 |
42 DISALLOW_COPY_AND_ASSIGN(WebMParserClient); | 44 DISALLOW_COPY_AND_ASSIGN(WebMParserClient); |
43 }; | 45 }; |
44 | 46 |
45 struct ListElementInfo; | 47 struct ListElementInfo; |
46 | 48 |
(...skipping 10 matching lines...) Expand all Loading... |
57 ~WebMListParser(); | 59 ~WebMListParser(); |
58 | 60 |
59 // Resets the state of the parser so it can start parsing a new list. | 61 // Resets the state of the parser so it can start parsing a new list. |
60 void Reset(); | 62 void Reset(); |
61 | 63 |
62 // Parses list data contained in |buf|. | 64 // Parses list data contained in |buf|. |
63 // | 65 // |
64 // Returns < 0 if the parse fails. | 66 // Returns < 0 if the parse fails. |
65 // Returns 0 if more data is needed. | 67 // Returns 0 if more data is needed. |
66 // Returning > 0 indicates success & the number of bytes parsed. | 68 // Returning > 0 indicates success & the number of bytes parsed. |
67 int Parse(const uint8* buf, int size); | 69 int Parse(const uint8_t* buf, int size); |
68 | 70 |
69 // Returns true if the entire list has been parsed. | 71 // Returns true if the entire list has been parsed. |
70 bool IsParsingComplete() const; | 72 bool IsParsingComplete() const; |
71 | 73 |
72 private: | 74 private: |
73 enum State { | 75 enum State { |
74 NEED_LIST_HEADER, | 76 NEED_LIST_HEADER, |
75 INSIDE_LIST, | 77 INSIDE_LIST, |
76 DONE_PARSING_LIST, | 78 DONE_PARSING_LIST, |
77 PARSE_ERROR, | 79 PARSE_ERROR, |
78 }; | 80 }; |
79 | 81 |
80 struct ListState { | 82 struct ListState { |
81 int id_; | 83 int id_; |
82 int64 size_; | 84 int64_t size_; |
83 int64 bytes_parsed_; | 85 int64_t bytes_parsed_; |
84 const ListElementInfo* element_info_; | 86 const ListElementInfo* element_info_; |
85 WebMParserClient* client_; | 87 WebMParserClient* client_; |
86 }; | 88 }; |
87 | 89 |
88 void ChangeState(State new_state); | 90 void ChangeState(State new_state); |
89 | 91 |
90 // Parses a single element in the current list. | 92 // Parses a single element in the current list. |
91 // | 93 // |
92 // |header_size| - The size of the element header | 94 // |header_size| - The size of the element header |
93 // |id| - The ID of the element being parsed. | 95 // |id| - The ID of the element being parsed. |
94 // |element_size| - The size of the element body. | 96 // |element_size| - The size of the element body. |
95 // |data| - Pointer to the element contents. | 97 // |data| - Pointer to the element contents. |
96 // |size| - Number of bytes in |data| | 98 // |size| - Number of bytes in |data| |
97 // |client| - Client to pass the parsed data to. | 99 // |client| - Client to pass the parsed data to. |
98 // | 100 // |
99 // Returns < 0 if the parse fails. | 101 // Returns < 0 if the parse fails. |
100 // Returns 0 if more data is needed. | 102 // Returns 0 if more data is needed. |
101 // Returning > 0 indicates success & the number of bytes parsed. | 103 // Returning > 0 indicates success & the number of bytes parsed. |
102 int ParseListElement(int header_size, | 104 int ParseListElement(int header_size, |
103 int id, int64 element_size, | 105 int id, |
104 const uint8* data, int size); | 106 int64_t element_size, |
| 107 const uint8_t* data, |
| 108 int size); |
105 | 109 |
106 // Called when starting to parse a new list. | 110 // Called when starting to parse a new list. |
107 // | 111 // |
108 // |id| - The ID of the new list. | 112 // |id| - The ID of the new list. |
109 // |size| - The size of the new list. | 113 // |size| - The size of the new list. |
110 // |client| - The client object to notify that a new list is being parsed. | 114 // |client| - The client object to notify that a new list is being parsed. |
111 // | 115 // |
112 // Returns true if this list can be started in the current context. False | 116 // Returns true if this list can be started in the current context. False |
113 // if starting this list causes some sort of parse error. | 117 // if starting this list causes some sort of parse error. |
114 bool OnListStart(int id, int64 size); | 118 bool OnListStart(int id, int64_t size); |
115 | 119 |
116 // Called when the end of the current list has been reached. This may also | 120 // Called when the end of the current list has been reached. This may also |
117 // signal the end of the current list's ancestors if the current list happens | 121 // signal the end of the current list's ancestors if the current list happens |
118 // to be at the end of its parent. | 122 // to be at the end of its parent. |
119 // | 123 // |
120 // Returns true if no errors occurred while ending this list(s). | 124 // Returns true if no errors occurred while ending this list(s). |
121 bool OnListEnd(); | 125 bool OnListEnd(); |
122 | 126 |
123 // Checks to see if |id_b| is a sibling or ancestor of |id_a|. | 127 // Checks to see if |id_b| is a sibling or ancestor of |id_a|. |
124 bool IsSiblingOrAncestor(int id_a, int id_b) const; | 128 bool IsSiblingOrAncestor(int id_a, int id_b) const; |
(...skipping 18 matching lines...) Expand all Loading... |
143 }; | 147 }; |
144 | 148 |
145 // Parses an element header & returns the ID and element size. | 149 // Parses an element header & returns the ID and element size. |
146 // | 150 // |
147 // Returns < 0 if the parse fails. | 151 // Returns < 0 if the parse fails. |
148 // Returns 0 if more data is needed. | 152 // Returns 0 if more data is needed. |
149 // Returning > 0 indicates success & the number of bytes parsed. | 153 // Returning > 0 indicates success & the number of bytes parsed. |
150 // |*id| contains the element ID on success and is undefined otherwise. | 154 // |*id| contains the element ID on success and is undefined otherwise. |
151 // |*element_size| contains the element size on success and is undefined | 155 // |*element_size| contains the element size on success and is undefined |
152 // otherwise. | 156 // otherwise. |
153 int MEDIA_EXPORT WebMParseElementHeader(const uint8* buf, int size, | 157 int MEDIA_EXPORT WebMParseElementHeader(const uint8_t* buf, |
154 int* id, int64* element_size); | 158 int size, |
| 159 int* id, |
| 160 int64_t* element_size); |
155 | 161 |
156 } // namespace media | 162 } // namespace media |
157 | 163 |
158 #endif // MEDIA_FORMATS_WEBM_WEBM_PARSER_H_ | 164 #endif // MEDIA_FORMATS_WEBM_WEBM_PARSER_H_ |
OLD | NEW |