DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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 
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 
55  public:
58  WebMListParser(int id, WebMParserClient* client);
59  ~WebMListParser();
60 
62  void Reset();
63 
68  int Parse(const uint8_t* buf, int size);
69 
71  bool IsParsingComplete() const;
72 
73  private:
74  enum State {
75  NEED_LIST_HEADER,
76  INSIDE_LIST,
77  DONE_PARSING_LIST,
78  PARSE_ERROR,
79  };
80 
81  struct ListState {
82  int id_;
83  int64_t size_;
84  int64_t bytes_parsed_;
85  const ListElementInfo* element_info_;
86  WebMParserClient* client_;
87  };
88 
89  void ChangeState(State new_state);
90 
91  // Parses a single element in the current list.
92  //
93  // |header_size| - The size of the element header
94  // |id| - The ID of the element being parsed.
95  // |element_size| - The size of the element body.
96  // |data| - Pointer to the element contents.
97  // |size| - Number of bytes in |data|
98  // |client| - Client to pass the parsed data to.
99  //
100  // Returns < 0 if the parse fails.
101  // Returns 0 if more data is needed.
102  // Returning > 0 indicates success & the number of bytes parsed.
103  int ParseListElement(int header_size,
104  int id,
105  int64_t element_size,
106  const uint8_t* data,
107  int size);
108 
109  // Called when starting to parse a new list.
110  //
111  // |id| - The ID of the new list.
112  // |size| - The size of the new list.
113  // |client| - The client object to notify that a new list is being parsed.
114  //
115  // Returns true if this list can be started in the current context. False
116  // if starting this list causes some sort of parse error.
117  bool OnListStart(int id, int64_t size);
118 
119  // Called when the end of the current list has been reached. This may also
120  // signal the end of the current list's ancestors if the current list happens
121  // to be at the end of its parent.
122  //
123  // Returns true if no errors occurred while ending this list(s).
124  bool OnListEnd();
125 
126  // Checks to see if |id_b| is a sibling or ancestor of |id_a|.
127  bool IsSiblingOrAncestor(int id_a, int id_b) const;
128 
129  State state_;
130 
131  // Element ID passed to the constructor.
132  const int root_id_;
133 
134  // Element level for |root_id_|. Used to verify that elements appear at
135  // the correct level.
136  const int root_level_;
137 
138  // WebMParserClient to handle the root list.
139  WebMParserClient* const root_client_;
140 
141  // Stack of state for all the lists currently being parsed. Lists are
142  // added and removed from this stack as they are parsed.
143  std::vector<ListState> list_state_stack_;
144 
145  DISALLOW_COPY_AND_ASSIGN(WebMListParser);
146 };
147 
155 int WebMParseElementHeader(const uint8_t* buf,
156  int size,
157  int* id,
158  int64_t* element_size);
159 
160 } // namespace media
161 } // namespace edash_packager
162 
163 #endif // MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
WebMListParser(int id, WebMParserClient *client)
Definition: webm_parser.cc:703
int Parse(const uint8_t *buf, int size)
Definition: webm_parser.cc:719
void Reset()
Resets the state of the parser so it can start parsing a new list.
Definition: webm_parser.cc:714