DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
webm_webvtt_parser.cc
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 "packager/media/formats/webm/webm_webvtt_parser.h"
6 
7 namespace edash_packager {
8 namespace media {
9 
10 void WebMWebVTTParser::Parse(const uint8_t* payload,
11  int payload_size,
12  std::string* id,
13  std::string* settings,
14  std::string* content) {
15  WebMWebVTTParser parser(payload, payload_size);
16  parser.Parse(id, settings, content);
17 }
18 
19 WebMWebVTTParser::WebMWebVTTParser(const uint8_t* payload, int payload_size)
20  : ptr_(payload), ptr_end_(payload + payload_size) {}
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_t* 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_t 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 } // namespace edash_packager
static void Parse(const uint8_t *payload, int payload_size, std::string *id, std::string *settings, std::string *content)
Utility function to parse the WebVTT cue from a byte stream.