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 #include "src/segment_parser.h"
9 
10 #include "src/chapters_parser.h"
11 #include "src/cluster_parser.h"
12 #include "src/cues_parser.h"
13 #include "src/info_parser.h"
14 #include "src/seek_head_parser.h"
15 #include "src/skip_callback.h"
16 #include "src/tags_parser.h"
17 #include "src/tracks_parser.h"
18 #include "webm/id.h"
19 
20 namespace webm {
21 
SegmentParser()22 SegmentParser::SegmentParser()
23     : MasterParser(MakeChild<ChaptersParser>(Id::kChapters),
24                    MakeChild<ClusterParser>(Id::kCluster),
25                    MakeChild<CuesParser>(Id::kCues),
26                    MakeChild<InfoParser>(Id::kInfo),
27                    MakeChild<SeekHeadParser>(Id::kSeekHead),
28                    MakeChild<TagsParser>(Id::kTags),
29                    MakeChild<TracksParser>(Id::kTracks)) {}
30 
Init(const ElementMetadata & metadata,std::uint64_t max_size)31 Status SegmentParser::Init(const ElementMetadata& metadata,
32                            std::uint64_t max_size) {
33   assert(metadata.size == kUnknownElementSize || metadata.size <= max_size);
34 
35   begin_done_ = false;
36   parse_completed_ = false;
37   return MasterParser::Init(metadata, max_size);
38 }
39 
InitAfterSeek(const Ancestory & child_ancestory,const ElementMetadata & child_metadata)40 void SegmentParser::InitAfterSeek(const Ancestory& child_ancestory,
41                                   const ElementMetadata& child_metadata) {
42   MasterParser::InitAfterSeek(child_ancestory, child_metadata);
43 
44   begin_done_ = true;
45   parse_completed_ = false;
46   action_ = Action::kRead;
47 }
48 
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)49 Status SegmentParser::Feed(Callback* callback, Reader* reader,
50                            std::uint64_t* num_bytes_read) {
51   assert(callback != nullptr);
52   assert(reader != nullptr);
53   assert(num_bytes_read != nullptr);
54 
55   *num_bytes_read = 0;
56 
57   if (!begin_done_) {
58     const ElementMetadata metadata{Id::kSegment, header_size(), size(),
59                                    position()};
60     const Status status = callback->OnSegmentBegin(metadata, &action_);
61     if (!status.completed_ok()) {
62       return status;
63     }
64     begin_done_ = true;
65   }
66 
67   SkipCallback skip_callback;
68   if (action_ == Action::kSkip) {
69     callback = &skip_callback;
70   }
71 
72   if (!parse_completed_) {
73     const Status status = MasterParser::Feed(callback, reader, num_bytes_read);
74     if (!status.completed_ok()) {
75       return status;
76     }
77     parse_completed_ = true;
78   }
79 
80   return callback->OnSegmentEnd(
81       {Id::kSegment, header_size(), size(), position()});
82 }
83 
WasSkipped() const84 bool SegmentParser::WasSkipped() const { return action_ == Action::kSkip; }
85 
86 }  // namespace webm
87