DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
webm_parser.h
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
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
6 #define MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "packager/base/macros.h"
14 
15 namespace edash_packager {
16 namespace media {
17 
18 // Interface for receiving WebM parser events.
19 //
20 // Each method is called when an element of the specified type is parsed.
21 // The ID of the element that was parsed is given along with the value
22 // stored in the element. List elements generate calls at the start and
23 // end of the list. Any pointers passed to these methods are only guaranteed
24 // to be valid for the life of that call. Each method (except for OnListStart)
25 // returns a bool that indicates whether the parsed data is valid. OnListStart
26 // returns a pointer to a WebMParserClient object, which should be used to
27 // handle elements parsed out of the list being started. If false (or NULL by
28 // OnListStart) is returned then the parse is immediately terminated and an
29 // error is reported by the parser.
31  public:
32  virtual ~WebMParserClient();
33 
34  virtual WebMParserClient* OnListStart(int id);
35  virtual bool OnListEnd(int id);
36  virtual bool OnUInt(int id, int64_t val);
37  virtual bool OnFloat(int id, double val);
38  virtual bool OnBinary(int id, const uint8_t* data, int size);
39  virtual bool OnString(int id, const std::string& str);
40 
41  protected:
43 
44  DISALLOW_COPY_AND_ASSIGN(WebMParserClient);
45 };
46 
47 struct ListElementInfo;
48 
49 // Parses a WebM list element and all of its children. This
50 // class supports incremental parsing of the list so Parse()
51 // can be called multiple times with pieces of the list.
52 // IsParsingComplete() will return true once the entire list has
53 // been parsed.
55  public:
56  // |id| - Element ID of the list we intend to parse.
57  // |client| - Called as different elements in the list are parsed.
58  WebMListParser(int id, WebMParserClient* client);
59  ~WebMListParser();
60 
61  // Resets the state of the parser so it can start parsing a new list.
62  void Reset();
63 
64  // Parses list data contained in |buf|.
65  //
66  // Returns < 0 if the parse fails.
67  // Returns 0 if more data is needed.
68  // Returning > 0 indicates success & the number of bytes parsed.
69  int Parse(const uint8_t* buf, int size);
70 
71  // Returns true if the entire list has been parsed.
72  bool IsParsingComplete() const;
73 
74  private:
75  enum State {
76  NEED_LIST_HEADER,
77  INSIDE_LIST,
78  DONE_PARSING_LIST,
79  PARSE_ERROR,
80  };
81 
82  struct ListState {
83  int id_;
84  int64_t size_;
85  int64_t bytes_parsed_;
86  const ListElementInfo* element_info_;
87  WebMParserClient* client_;
88  };
89 
90  void ChangeState(State new_state);
91 
92  // Parses a single element in the current list.
93  //
94  // |header_size| - The size of the element header
95  // |id| - The ID of the element being parsed.
96  // |element_size| - The size of the element body.
97  // |data| - Pointer to the element contents.
98  // |size| - Number of bytes in |data|
99  // |client| - Client to pass the parsed data to.
100  //
101  // Returns < 0 if the parse fails.
102  // Returns 0 if more data is needed.
103  // Returning > 0 indicates success & the number of bytes parsed.
104  int ParseListElement(int header_size,
105  int id,
106  int64_t element_size,
107  const uint8_t* data,
108  int size);
109 
110  // Called when starting to parse a new list.
111  //
112  // |id| - The ID of the new list.
113  // |size| - The size of the new list.
114  // |client| - The client object to notify that a new list is being parsed.
115  //
116  // Returns true if this list can be started in the current context. False
117  // if starting this list causes some sort of parse error.
118  bool OnListStart(int id, int64_t size);
119 
120  // Called when the end of the current list has been reached. This may also
121  // signal the end of the current list's ancestors if the current list happens
122  // to be at the end of its parent.
123  //
124  // Returns true if no errors occurred while ending this list(s).
125  bool OnListEnd();
126 
127  // Checks to see if |id_b| is a sibling or ancestor of |id_a|.
128  bool IsSiblingOrAncestor(int id_a, int id_b) const;
129 
130  State state_;
131 
132  // Element ID passed to the constructor.
133  const int root_id_;
134 
135  // Element level for |root_id_|. Used to verify that elements appear at
136  // the correct level.
137  const int root_level_;
138 
139  // WebMParserClient to handle the root list.
140  WebMParserClient* const root_client_;
141 
142  // Stack of state for all the lists currently being parsed. Lists are
143  // added and removed from this stack as they are parsed.
144  std::vector<ListState> list_state_stack_;
145 
146  DISALLOW_COPY_AND_ASSIGN(WebMListParser);
147 };
148 
149 // Parses an element header & returns the ID and element size.
150 //
151 // Returns < 0 if the parse fails.
152 // Returns 0 if more data is needed.
153 // Returning > 0 indicates success & the number of bytes parsed.
154 // |*id| contains the element ID on success and is undefined otherwise.
155 // |*element_size| contains the element size on success and is undefined
156 // otherwise.
157 int WebMParseElementHeader(const uint8_t* buf,
158  int size,
159  int* id,
160  int64_t* element_size);
161 
162 } // namespace media
163 } // namespace edash_packager
164 
165 #endif // MEDIA_FORMATS_WEBM_WEBM_PARSER_H_