Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "packager/base/compiler_specific.h"
17 #include "packager/media/base/media_parser.h"
18 #include "packager/media/formats/webvtt/cue.h"
19 #include "packager/media/formats/webvtt/webvtt_sample_converter.h"
20 
21 namespace shaka {
22 namespace media {
23 
24 // WebVTT parser.
25 // The input may not be encrypted so decryption_key_source is ignored.
27  public:
29  ~WebVttMediaParser() override;
30 
33  void Init(const InitCB& init_cb,
34  const NewSampleCB& new_sample_cb,
35  KeySource* decryption_key_source) override;
36  bool Flush() override WARN_UNUSED_RESULT;
37  bool Parse(const uint8_t* buf, int size) override WARN_UNUSED_RESULT;
39 
40  void InjectWebVttSampleConvertForTesting(
41  std::unique_ptr<WebVttSampleConverter> converter);
42 
43  private:
44  enum WebVttReadingState {
45  kHeader,
46  kMetadata,
47  kCueIdentifierOrTimingOrComment,
48  kCueTiming,
49  kCuePayload,
50  kComment,
51  kParseError,
52  };
53 
54  // Sends current cue to sample converter, and dispatches any ready samples to
55  // the callback.
56  // current_cue_ is always cleared.
57  bool ProcessCurrentCue(bool flush);
58 
59  InitCB init_cb_;
60  NewSampleCB new_sample_cb_;
61 
62  // All the unprocessed data passed to this parser.
63  std::string data_;
64 
65  // The WEBVTT text + metadata header (global settings) for this webvtt.
66  // One element per line.
67  std::vector<std::string> header_;
68 
69  // This is set to what the parser is expecting. For example, if the parse is
70  // expecting a kCueTiming, then the next line that it parses should be a
71  // WebVTT timing line or an empty line.
72  WebVttReadingState state_;
73 
74  Cue current_cue_;
75 
76  std::unique_ptr<WebVttSampleConverter> sample_converter_;
77 
78  DISALLOW_COPY_AND_ASSIGN(WebVttMediaParser);
79 };
80 
81 } // namespace media
82 } // namespace shaka
83 
84 #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
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:45