Shaka Packager SDK
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 PACKAGER_MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
6 #define PACKAGER_MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
7 
8 #include <cstdint>
9 #include <string>
10 #include <vector>
11 
12 #include <packager/macros/classes.h>
13 
14 namespace shaka {
15 namespace media {
16 
30  public:
31  virtual ~WebMParserClient();
32 
33  virtual WebMParserClient* OnListStart(int id);
34  virtual bool OnListEnd(int id);
35  virtual bool OnUInt(int id, int64_t val);
36  virtual bool OnFloat(int id, double val);
37  virtual bool OnBinary(int id, const uint8_t* data, int size);
38  virtual bool OnString(int id, const std::string& str);
39 
40  protected:
42 
43  DISALLOW_COPY_AND_ASSIGN(WebMParserClient);
44 };
45 
46 struct ListElementInfo;
47 
54  public:
57  WebMListParser(int id, WebMParserClient* client);
58  ~WebMListParser();
59 
61  void Reset();
62 
67  int Parse(const uint8_t* buf, int size);
68 
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  int64_t size_;
83  int64_t bytes_parsed_;
84  const ListElementInfo* element_info_;
85  WebMParserClient* client_;
86  };
87 
88  void ChangeState(State new_state);
89 
90  // Parses a single element in the current list.
91  //
92  // |header_size| - The size of the element header
93  // |id| - The ID of the element being parsed.
94  // |element_size| - The size of the element body.
95  // |data| - Pointer to the element contents.
96  // |size| - Number of bytes in |data|
97  // |client| - Client to pass the parsed data to.
98  //
99  // Returns < 0 if the parse fails.
100  // Returns 0 if more data is needed.
101  // Returning > 0 indicates success & the number of bytes parsed.
102  int ParseListElement(int header_size,
103  int id,
104  int64_t element_size,
105  const uint8_t* data,
106  int size);
107 
108  // Called when starting to parse a new list.
109  //
110  // |id| - The ID of the new list.
111  // |size| - The size of the new list.
112  // |client| - The client object to notify that a new list is being parsed.
113  //
114  // Returns true if this list can be started in the current context. False
115  // if starting this list causes some sort of parse error.
116  bool OnListStart(int id, int64_t size);
117 
118  // Called when the end of the current list has been reached. This may also
119  // signal the end of the current list's ancestors if the current list happens
120  // to be at the end of its parent.
121  //
122  // Returns true if no errors occurred while ending this list(s).
123  bool OnListEnd();
124 
125  // Checks to see if |id_b| is a sibling or ancestor of |id_a|.
126  bool IsSiblingOrAncestor(int id_a, int id_b) const;
127 
128  State state_;
129 
130  // Element ID passed to the constructor.
131  const int root_id_;
132 
133  // Element level for |root_id_|. Used to verify that elements appear at
134  // the correct level.
135  const int root_level_;
136 
137  // WebMParserClient to handle the root list.
138  WebMParserClient* const root_client_;
139 
140  // Stack of state for all the lists currently being parsed. Lists are
141  // added and removed from this stack as they are parsed.
142  std::vector<ListState> list_state_stack_;
143 
144  DISALLOW_COPY_AND_ASSIGN(WebMListParser);
145 };
146 
154 int WebMParseElementHeader(const uint8_t* buf,
155  int size,
156  int* id,
157  int64_t* element_size);
158 
159 } // namespace media
160 } // namespace shaka
161 
162 #endif // PACKAGER_MEDIA_FORMATS_WEBM_WEBM_PARSER_H_
void Reset()
Resets the state of the parser so it can start parsing a new list.
Definition: webm_parser.cc:742
WebMListParser(int id, WebMParserClient *client)
Definition: webm_parser.cc:731
int Parse(const uint8_t *buf, int size)
Definition: webm_parser.cc:747
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66