Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / webm / webm_webvtt_parser.cc
blob64de1ef4434f127ed0a9363885d0088a5638bb87
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.
5 #include "media/formats/webm/webm_webvtt_parser.h"
7 namespace media {
9 void WebMWebVTTParser::Parse(const uint8* payload, int payload_size,
10 std::string* id,
11 std::string* settings,
12 std::string* content) {
13 WebMWebVTTParser parser(payload, payload_size);
14 parser.Parse(id, settings, content);
17 WebMWebVTTParser::WebMWebVTTParser(const uint8* payload, int payload_size)
18 : ptr_(payload),
19 ptr_end_(payload + payload_size) {
22 void WebMWebVTTParser::Parse(std::string* id,
23 std::string* settings,
24 std::string* content) {
25 ParseLine(id);
26 ParseLine(settings);
27 content->assign(ptr_, ptr_end_);
30 bool WebMWebVTTParser::GetByte(uint8* byte) {
31 if (ptr_ >= ptr_end_)
32 return false; // indicates end-of-stream
34 *byte = *ptr_++;
35 return true;
38 void WebMWebVTTParser::UngetByte() {
39 --ptr_;
42 void WebMWebVTTParser::ParseLine(std::string* line) {
43 line->clear();
45 // Consume characters from the stream, until we reach end-of-line.
47 // The WebVTT spec states that lines may be terminated in any of the following
48 // three ways:
49 // LF
50 // CR
51 // CR LF
53 // The spec is here:
54 // http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
56 enum {
57 kLF = '\x0A',
58 kCR = '\x0D'
61 for (;;) {
62 uint8 byte;
64 if (!GetByte(&byte) || byte == kLF)
65 return;
67 if (byte == kCR) {
68 if (GetByte(&byte) && byte != kLF)
69 UngetByte();
71 return;
74 line->push_back(byte);
78 } // namespace media