Home | History | Annotate | Download | only in webm
      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 #include "media/formats/webm/webm_webvtt_parser.h"
      6 
      7 namespace media {
      8 
      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);
     15 }
     16 
     17 WebMWebVTTParser::WebMWebVTTParser(const uint8* payload, int payload_size)
     18     : ptr_(payload),
     19       ptr_end_(payload + payload_size) {
     20 }
     21 
     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_);
     28 }
     29 
     30 bool WebMWebVTTParser::GetByte(uint8* byte) {
     31   if (ptr_ >= ptr_end_)
     32     return false;  // indicates end-of-stream
     33 
     34   *byte = *ptr_++;
     35   return true;
     36 }
     37 
     38 void WebMWebVTTParser::UngetByte() {
     39   --ptr_;
     40 }
     41 
     42 void WebMWebVTTParser::ParseLine(std::string* line) {
     43   line->clear();
     44 
     45   // Consume characters from the stream, until we reach end-of-line.
     46 
     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
     52 
     53   // The spec is here:
     54   //  http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
     55 
     56   enum {
     57     kLF = '\x0A',
     58     kCR = '\x0D'
     59   };
     60 
     61   for (;;) {
     62     uint8 byte;
     63 
     64     if (!GetByte(&byte) || byte == kLF)
     65       return;
     66 
     67     if (byte == kCR) {
     68       if (GetByte(&byte) && byte != kLF)
     69         UngetByte();
     70 
     71       return;
     72     }
     73 
     74     line->push_back(byte);
     75   }
     76 }
     77 
     78 }  // namespace media
     79