1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #ifndef SRC_BLOCK_GROUP_PARSER_H_
9 #define SRC_BLOCK_GROUP_PARSER_H_
10 
11 #include "src/block_additions_parser.h"
12 #include "src/block_parser.h"
13 #include "src/int_parser.h"
14 #include "src/master_value_parser.h"
15 #include "src/slices_parser.h"
16 #include "src/virtual_block_parser.h"
17 #include "webm/dom_types.h"
18 #include "webm/id.h"
19 
20 namespace webm {
21 
22 // Spec reference:
23 // http://matroska.org/technical/specs/index.html#BlockGroup
24 // http://www.webmproject.org/docs/container/#BlockGroup
25 class BlockGroupParser : public MasterValueParser<BlockGroup> {
26  public:
BlockGroupParser()27   BlockGroupParser()
28       : MasterValueParser<BlockGroup>(
29             MakeChild<BlockParser>(Id::kBlock, &BlockGroup::block),
30             MakeChild<VirtualBlockParser>(Id::kBlockVirtual,
31                                           &BlockGroup::virtual_block),
32             MakeChild<BlockAdditionsParser>(Id::kBlockAdditions,
33                                             &BlockGroup::additions),
34             MakeChild<UnsignedIntParser>(Id::kBlockDuration,
35                                          &BlockGroup::duration),
36             MakeChild<SignedIntParser>(Id::kReferenceBlock,
37                                        &BlockGroup::references),
38             MakeChild<SignedIntParser>(Id::kDiscardPadding,
39                                        &BlockGroup::discard_padding),
40             MakeChild<SlicesParser>(Id::kSlices, &BlockGroup::slices)) {}
41 
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)42   Status Feed(Callback* callback, Reader* reader,
43               std::uint64_t* num_bytes_read) override {
44     *num_bytes_read = 0;
45 
46     if (!parse_started_event_completed()) {
47       Action action = Action::kRead;
48       Status status = OnParseStarted(callback, &action);
49       if (!status.completed_ok()) {
50         return status;
51       }
52 
53       set_parse_started_event_completed_with_action(action);
54     }
55 
56     return MasterValueParser::Feed(callback, reader, num_bytes_read);
57   }
58 
59  protected:
OnParseStarted(Callback * callback,Action * action)60   Status OnParseStarted(Callback* callback, Action* action) override {
61     return callback->OnBlockGroupBegin(metadata(Id::kBlockGroup), action);
62   }
63 
OnParseCompleted(Callback * callback)64   Status OnParseCompleted(Callback* callback) override {
65     return callback->OnBlockGroupEnd(metadata(Id::kBlockGroup), value());
66   }
67 };
68 
69 }  // namespace webm
70 
71 #endif  // SRC_BLOCK_GROUP_PARSER_H_
72