Shaka Packager SDK
Loading...
Searching...
No Matches
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#include <cstdint>
8#include <string>
9
10namespace shaka {
11namespace media {
12
13void WebMWebVTTParser::Parse(const uint8_t* payload,
14 int payload_size,
15 std::string* id,
16 std::string* settings,
17 std::string* content) {
18 WebMWebVTTParser parser(payload, payload_size);
19 parser.Parse(id, settings, content);
20}
21
22WebMWebVTTParser::WebMWebVTTParser(const uint8_t* payload, int payload_size)
23 : ptr_(payload), ptr_end_(payload + payload_size) {}
24
25void WebMWebVTTParser::Parse(std::string* id,
26 std::string* settings,
27 std::string* content) {
28 ParseLine(id);
29 ParseLine(settings);
30 content->assign(ptr_, ptr_end_);
31}
32
33bool WebMWebVTTParser::GetByte(uint8_t* byte) {
34 if (ptr_ >= ptr_end_)
35 return false; // indicates end-of-stream
36
37 *byte = *ptr_++;
38 return true;
39}
40
41void WebMWebVTTParser::UngetByte() {
42 --ptr_;
43}
44
45void WebMWebVTTParser::ParseLine(std::string* line) {
46 line->clear();
47
48 // Consume characters from the stream, until we reach end-of-line.
49
50 // The WebVTT spec states that lines may be terminated in any of the following
51 // three ways:
52 // LF
53 // CR
54 // CR LF
55
56 // The spec is here:
57 // http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
58
59 enum { kLF = '\x0A', kCR = '\x0D' };
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 shaka
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.
All the methods that are virtual are virtual for mocking.