OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_WEBM_WEBM_PARSER_H_ | 5 #ifndef MEDIA_WEBM_WEBM_PARSER_H_ |
6 #define MEDIA_WEBM_WEBM_PARSER_H_ | 6 #define MEDIA_WEBM_WEBM_PARSER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "media/base/media_export.h" | |
13 | 11 |
14 namespace media { | 12 namespace media { |
15 | 13 |
16 // Interface for receiving WebM parser events. | 14 // Interface for receiving WebM parser events. |
17 // | 15 // |
18 // Each method is called when an element of the specified type is parsed. | 16 // 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 | 17 // 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 | 18 // 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 | 19 // 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 returns a bool that | 20 // to be valid for the life of that call. Each method returns a bool that |
23 // indicates whether the parsed data is valid. If false is returned | 21 // indicates whether the parsed data is valid. If false is returned |
24 // then the parse is immediately terminated and an error is reported by the | 22 // then the parse is immediately terminated and an error is reported by the |
25 // parser. | 23 // parser. |
26 class MEDIA_EXPORT WebMParserClient { | 24 class WebMParserClient { |
27 public: | 25 public: |
28 virtual ~WebMParserClient(); | 26 virtual ~WebMParserClient(); |
29 | 27 |
30 virtual bool OnListStart(int id) = 0; | 28 virtual bool OnListStart(int id) = 0; |
31 virtual bool OnListEnd(int id) = 0; | 29 virtual bool OnListEnd(int id) = 0; |
32 virtual bool OnUInt(int id, int64 val) = 0; | 30 virtual bool OnUInt(int id, int64 val) = 0; |
33 virtual bool OnFloat(int id, double val) = 0; | 31 virtual bool OnFloat(int id, double val) = 0; |
34 virtual bool OnBinary(int id, const uint8* data, int size) = 0; | 32 virtual bool OnBinary(int id, const uint8* data, int size) = 0; |
35 virtual bool OnString(int id, const std::string& str) = 0; | 33 virtual bool OnString(int id, const std::string& str) = 0; |
36 virtual bool OnSimpleBlock(int track_num, int timecode, | 34 virtual bool OnSimpleBlock(int track_num, int timecode, |
37 int flags, | 35 int flags, |
38 const uint8* data, int size) = 0; | 36 const uint8* data, int size) = 0; |
39 protected: | |
40 WebMParserClient(); | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(WebMParserClient); | |
43 }; | |
44 | |
45 struct ListElementInfo; | |
46 | |
47 // Parses a WebM list element and all of its children. This | |
48 // class supports incremental parsing of the list so Parse() | |
49 // can be called multiple times with pieces of the list. | |
50 // IsParsingComplete() will return true once the entire list has | |
51 // been parsed. | |
52 class MEDIA_EXPORT WebMListParser { | |
53 public: | |
54 // |id| - Element ID of the list we intend to parse. | |
55 explicit WebMListParser(int id); | |
56 ~WebMListParser(); | |
57 | |
58 // Resets the state of the parser so it can start parsing a new list. | |
59 void Reset(); | |
60 | |
61 // Parses list data contained in |buf|. | |
62 // |client| Called as different elements in the list are parsed. | |
63 // | |
64 // Returns < 0 if the parse fails. | |
65 // Returns 0 if more data is needed. | |
66 // Returning > 0 indicates success & the number of bytes parsed. | |
67 int Parse(const uint8* buf, int size, WebMParserClient* client); | |
68 | |
69 // Returns true if the entire list has been parsed. | |
70 bool IsParsingComplete() const; | |
71 | |
72 private: | |
73 enum State { | |
74 NEED_LIST_HEADER, | |
75 INSIDE_LIST, | |
76 DONE_PARSING_LIST, | |
77 PARSE_ERROR, | |
78 }; | |
79 | |
80 struct ListState { | |
81 int id_; | |
82 int size_; | |
83 int bytes_parsed_; | |
84 const ListElementInfo* element_info_; | |
85 }; | |
86 | |
87 void ChangeState(State new_state); | |
88 | |
89 // Parses a single element in the current list. | |
90 // | |
91 // |header_size| - The size of the element header | |
92 // |id| - The ID of the element being parsed. | |
93 // |element_size| - The size of the element body. | |
94 // |data| - Pointer to the element contents. | |
95 // |size| - Number of bytes in |data| | |
96 // |client| - Client to pass the parsed data to. | |
97 // | |
98 // Returns < 0 if the parse fails. | |
99 // Returns 0 if more data is needed. | |
100 // Returning > 0 indicates success & the number of bytes parsed. | |
101 int ParseListElement(int header_size, | |
102 int id, int64 element_size, | |
103 const uint8* data, int size, | |
104 WebMParserClient* client); | |
105 | |
106 // Called when starting to parse a new list. | |
107 // | |
108 // |id| - The ID of the new list. | |
109 // |size| - The size of the new list. | |
110 // |client| - The client object to notify that a new list is being parsed. | |
111 // | |
112 // Returns true if this list can be started in the current context. False | |
113 // if starting this list causes some sort of parse error. | |
114 bool OnListStart(int id, int64 size, WebMParserClient* client); | |
115 | |
116 // 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 | |
118 // to be at the end of its parent. | |
119 // | |
120 // |client| - The client to notify about lists ending. | |
121 // | |
122 // Returns true if no errors occurred while ending this list(s). | |
123 bool OnListEnd(WebMParserClient* client); | |
124 | |
125 State state_; | |
126 | |
127 // Element ID passed to the constructor. | |
128 int root_id_; | |
129 | |
130 // Element level for |root_id_|. Used to verify that elements appear at | |
131 // the correct level. | |
132 int root_level_; | |
133 | |
134 // Stack of state for all the lists currently being parsed. Lists are | |
135 // added and removed from this stack as they are parsed. | |
136 std::vector<ListState> list_state_stack_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(WebMListParser); | |
139 }; | 37 }; |
140 | 38 |
141 // Parses an element header & returns the ID and element size. | 39 // Parses an element header & returns the ID and element size. |
142 // | 40 // |
143 // Returns < 0 if the parse fails. | 41 // Returns < 0 if the parse fails. |
144 // Returns 0 if more data is needed. | 42 // Returns 0 if more data is needed. |
145 // Returning > 0 indicates success & the number of bytes parsed. | 43 // Returning > 0 indicates success & the number of bytes parsed. |
146 // |*id| contains the element ID on success and is undefined otherwise. | 44 // |*id| contains the element ID on success and is undefined otherwise. |
147 // |*element_size| contains the element size on success and is undefined | 45 // |*element_size| contains the element size on success and is undefined |
148 // otherwise. | 46 // otherwise. |
149 int WebMParseElementHeader(const uint8* buf, int size, | 47 int WebMParseElementHeader(const uint8* buf, int size, |
150 int* id, int64* element_size); | 48 int* id, int64* element_size); |
151 | 49 |
| 50 // Parses a single list element that matches |id|. This method fails if the |
| 51 // buffer points to an element that does not match |id|. |
| 52 // |
| 53 // Returns -1 if the parse fails. |
| 54 // Returns 0 if more data is needed. |
| 55 // Returns the number of bytes parsed on success. |
| 56 int WebMParseListElement(const uint8* buf, int size, int id, |
| 57 int level, WebMParserClient* client); |
| 58 |
152 } // namespace media | 59 } // namespace media |
153 | 60 |
154 #endif // MEDIA_WEBM_WEBM_PARSER_H_ | 61 #endif // MEDIA_WEBM_WEBM_PARSER_H_ |
OLD | NEW |