DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
webvtt_media_parser.h
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_FORMATS_WEBVTT_WEBVTT_MEDIA_PARSER_H_
8 #define MEDIA_FORMATS_WEBVTT_WEBVTT_MEDIA_PARSER_H_
9 
10 #include <stdint.h>
11 #include <string>
12 #include <vector>
13 
14 #include "packager/base/compiler_specific.h"
15 #include "packager/media/base/media_parser.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 // If comment is not empty, then this is metadata and other fields must
21 // be empty.
22 // Data that can be multiline are vector of strings.
23 struct Cue {
24  Cue();
25  ~Cue();
26 
27  std::string identifier;
28  uint64_t start_time;
29  uint64_t duration;
30  std::string settings;
31  std::vector<std::string> payload;
32  std::vector<std::string> comment;
33 };
34 
38 std::shared_ptr<MediaSample> CueToMediaSample(const Cue& cue);
39 
43 Cue MediaSampleToCue(const MediaSample& sample);
44 
45 // WebVTT parser.
46 // The input may not be encrypted so decryption_key_source is ignored.
48  public:
50  ~WebVttMediaParser() override;
51 
54  void Init(const InitCB& init_cb,
55  const NewSampleCB& new_sample_cb,
56  KeySource* decryption_key_source) override;
57  bool Flush() override WARN_UNUSED_RESULT;
58  bool Parse(const uint8_t* buf, int size) override WARN_UNUSED_RESULT;
60 
61  private:
62  enum WebVttReadingState {
63  kHeader,
64  kMetadata,
65  kCueIdentifierOrTimingOrComment,
66  kCueTiming,
67  kCuePayload,
68  kComment,
69  kParseError,
70  };
71 
72  InitCB init_cb_;
73  NewSampleCB new_sample_cb_;
74 
75  // All the unprocessed data passed to this parser.
76  std::string data_;
77 
78  // The WEBVTT text + metadata header (global settings) for this webvtt.
79  // One element per line.
80  std::vector<std::string> header_;
81 
82  // This is set to what the parser is expecting. For example, if the parse is
83  // expecting a kCueTiming, then the next line that it parses should be a
84  // WebVTT timing line or an empty line.
85  WebVttReadingState state_;
86 
87  Cue current_cue_;
88 
89  DISALLOW_COPY_AND_ASSIGN(WebVttMediaParser);
90 };
91 
92 } // namespace media
93 } // namespace shaka
94 
95 #endif // MEDIA_FORMATS_WEBVTT_WEBVTT_MEDIA_PARSER_H_
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
base::Callback< void(const std::vector< std::shared_ptr< StreamInfo > > &stream_info)> InitCB
Definition: media_parser.h:34
base::Callback< bool(uint32_t track_id, const std::shared_ptr< MediaSample > &media_sample)> NewSampleCB
Definition: media_parser.h:43
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
bool Flush() override WARN_UNUSED_RESULT
Class to hold a media sample.
Definition: media_sample.h:22
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30