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> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "media/base/media_export.h" |
11 | 13 |
12 namespace media { | 14 namespace media { |
13 | 15 |
14 // Interface for receiving WebM parser events. | 16 // Interface for receiving WebM parser events. |
15 // | 17 // |
16 // Each method is called when an element of the specified type is parsed. | 18 // Each method is called when an element of the specified type is parsed. |
17 // The ID of the element that was parsed is given along with the value | 19 // The ID of the element that was parsed is given along with the value |
18 // stored in the element. List elements generate calls at the start and | 20 // stored in the element. List elements generate calls at the start and |
19 // end of the list. Any pointers passed to these methods are only guaranteed | 21 // end of the list. Any pointers passed to these methods are only guaranteed |
20 // to be valid for the life of that call. Each method returns a bool that | 22 // to be valid for the life of that call. Each method returns a bool that |
21 // indicates whether the parsed data is valid. If false is returned | 23 // indicates whether the parsed data is valid. If false is returned |
22 // then the parse is immediately terminated and an error is reported by the | 24 // then the parse is immediately terminated and an error is reported by the |
23 // parser. | 25 // parser. |
24 class WebMParserClient { | 26 class MEDIA_EXPORT WebMParserClient { |
25 public: | 27 public: |
26 virtual ~WebMParserClient(); | 28 virtual ~WebMParserClient(); |
27 | 29 |
28 virtual bool OnListStart(int id) = 0; | 30 virtual bool OnListStart(int id) = 0; |
29 virtual bool OnListEnd(int id) = 0; | 31 virtual bool OnListEnd(int id) = 0; |
30 virtual bool OnUInt(int id, int64 val) = 0; | 32 virtual bool OnUInt(int id, int64 val) = 0; |
31 virtual bool OnFloat(int id, double val) = 0; | 33 virtual bool OnFloat(int id, double val) = 0; |
32 virtual bool OnBinary(int id, const uint8* data, int size) = 0; | 34 virtual bool OnBinary(int id, const uint8* data, int size) = 0; |
33 virtual bool OnString(int id, const std::string& str) = 0; | 35 virtual bool OnString(int id, const std::string& str) = 0; |
34 virtual bool OnSimpleBlock(int track_num, int timecode, | 36 virtual bool OnSimpleBlock(int track_num, int timecode, |
35 int flags, | 37 int flags, |
36 const uint8* data, int size) = 0; | 38 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); |
37 }; | 139 }; |
38 | 140 |
39 // Parses an element header & returns the ID and element size. | 141 // Parses an element header & returns the ID and element size. |
40 // | 142 // |
41 // Returns < 0 if the parse fails. | 143 // Returns < 0 if the parse fails. |
42 // Returns 0 if more data is needed. | 144 // Returns 0 if more data is needed. |
43 // Returning > 0 indicates success & the number of bytes parsed. | 145 // Returning > 0 indicates success & the number of bytes parsed. |
44 // |*id| contains the element ID on success and is undefined otherwise. | 146 // |*id| contains the element ID on success and is undefined otherwise. |
45 // |*element_size| contains the element size on success and is undefined | 147 // |*element_size| contains the element size on success and is undefined |
46 // otherwise. | 148 // otherwise. |
47 int WebMParseElementHeader(const uint8* buf, int size, | 149 int WebMParseElementHeader(const uint8* buf, int size, |
48 int* id, int64* element_size); | 150 int* id, int64* element_size); |
49 | 151 |
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 | |
59 } // namespace media | 152 } // namespace media |
60 | 153 |
61 #endif // MEDIA_WEBM_WEBM_PARSER_H_ | 154 #endif // MEDIA_WEBM_WEBM_PARSER_H_ |
OLD | NEW |