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_AUDIO_PARSER_H_ 9 #define SRC_AUDIO_PARSER_H_ 10 11 #include <cassert> 12 #include <cstdint> 13 14 #include "src/float_parser.h" 15 #include "src/int_parser.h" 16 #include "src/master_value_parser.h" 17 #include "webm/callback.h" 18 #include "webm/dom_types.h" 19 #include "webm/id.h" 20 #include "webm/reader.h" 21 #include "webm/status.h" 22 23 namespace webm { 24 25 // Spec reference: 26 // http://matroska.org/technical/specs/index.html#Audio 27 // http://www.webmproject.org/docs/container/#Audio 28 class AudioParser : public MasterValueParser<Audio> { 29 public: AudioParser()30 AudioParser() 31 : MasterValueParser<Audio>( 32 MakeChild<FloatParser>(Id::kSamplingFrequency, 33 &Audio::sampling_frequency), 34 MakeChild<FloatParser>(Id::kOutputSamplingFrequency, 35 &Audio::output_frequency) 36 .NotifyOnParseComplete(), 37 MakeChild<UnsignedIntParser>(Id::kChannels, &Audio::channels), 38 MakeChild<UnsignedIntParser>(Id::kBitDepth, &Audio::bit_depth)) {} 39 Init(const ElementMetadata & metadata,std::uint64_t max_size)40 Status Init(const ElementMetadata& metadata, 41 std::uint64_t max_size) override { 42 output_frequency_has_value_ = false; 43 44 return MasterValueParser::Init(metadata, max_size); 45 } 46 InitAfterSeek(const Ancestory & child_ancestory,const ElementMetadata & child_metadata)47 void InitAfterSeek(const Ancestory& child_ancestory, 48 const ElementMetadata& child_metadata) override { 49 output_frequency_has_value_ = false; 50 51 return MasterValueParser::InitAfterSeek(child_ancestory, child_metadata); 52 } 53 Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)54 Status Feed(Callback* callback, Reader* reader, 55 std::uint64_t* num_bytes_read) override { 56 const Status status = 57 MasterValueParser::Feed(callback, reader, num_bytes_read); 58 if (status.completed_ok()) { 59 FixMissingOutputFrequency(); 60 } 61 return status; 62 } 63 64 protected: OnChildParsed(const ElementMetadata & metadata)65 void OnChildParsed(const ElementMetadata& metadata) override { 66 assert(metadata.id == Id::kOutputSamplingFrequency); 67 68 output_frequency_has_value_ = metadata.size > 0; 69 } 70 71 private: 72 bool output_frequency_has_value_; 73 FixMissingOutputFrequency()74 void FixMissingOutputFrequency() { 75 if (!output_frequency_has_value_) { 76 *mutable_value()->output_frequency.mutable_value() = 77 value().sampling_frequency.value(); 78 } 79 } 80 }; 81 82 } // namespace webm 83 84 #endif // SRC_AUDIO_PARSER_H_ 85