Shaka Packager SDK
webvtt_output_handler.h
1 // Copyright 2017 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 PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_TEXT_HANDLER_H_
8 #define PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_TEXT_HANDLER_H_
9 
10 #include <stdint.h>
11 
12 #include <vector>
13 
14 #include "packager/media/base/media_handler.h"
15 #include "packager/media/base/muxer_options.h"
16 #include "packager/media/event/muxer_listener.h"
17 
18 namespace shaka {
19 namespace media {
20 
21 // WebVttOutputHandler is the base class for all WebVtt text output handlers.
22 // It handles taking in the samples and writing the text out, but relies on
23 // sub classes to handle the logic of when and where to write the information.
25  public:
26  WebVttOutputHandler() = default;
27  virtual ~WebVttOutputHandler() = default;
28 
29  protected:
30  virtual Status OnStreamInfo(const StreamInfo& info) = 0;
31  virtual Status OnSegmentInfo(const SegmentInfo& info) = 0;
32  virtual Status OnCueEvent(const CueEvent& event) = 0;
33  virtual Status OnTextSample(const TextSample& sample) = 0;
34  virtual Status OnStreamEnd() = 0;
35 
36  // Top level functions for output. These functions should be used by
37  // subclasses to write to files.
38  void WriteCue(const std::string& id,
39  uint64_t start,
40  uint64_t end,
41  const std::string& settings,
42  const std::string& payload);
43  // Writes the current state of the current segment to disk. This will
44  // reset the internal state and set it up for the next segment.
45  Status WriteSegmentToFile(const std::string& filename);
46 
47  private:
49  WebVttOutputHandler& operator=(const WebVttOutputHandler&) = delete;
50 
51  Status InitializeInternal() override;
52  Status Process(std::unique_ptr<StreamData> stream_data) override;
53  Status OnFlushRequest(size_t input_stream_index) override;
54 
55  // A buffer of characters waiting to be written to a file.
56  std::string buffer_;
57 };
58 
59 // This WebVttt output handler should only be used when the source WebVTT
60 // content needs to be segmented across multiple files.
62  public:
63  WebVttSegmentedOutputHandler(const MuxerOptions& muxer_options,
64  std::unique_ptr<MuxerListener> muxer_listener);
65 
66  private:
67  Status OnStreamInfo(const StreamInfo& info) override;
68  Status OnSegmentInfo(const SegmentInfo& info) override;
69  Status OnCueEvent(const CueEvent& event) override;
70  Status OnTextSample(const TextSample& sample) override;
71  Status OnStreamEnd() override;
72 
73  Status OnSegmentEnded();
74 
75  void GoToNextSegment(uint64_t start_time_ms);
76 
77  const MuxerOptions muxer_options_;
78  std::unique_ptr<MuxerListener> muxer_listener_;
79 
80  // Sum together all segment durations so we know how long the stream is.
81  uint64_t total_duration_ms_ = 0;
82  uint32_t segment_index_ = 0;
83 };
84 
85 } // namespace media
86 } // namespace shaka
87 
88 #endif // PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_TEXT_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:59
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20