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 OnTextSample(const TextSample& sample) = 0;
33  virtual Status OnStreamEnd() = 0;
34 
35  // Top level functions for output. These functions should be used by
36  // subclasses to write to files.
37  void WriteCue(const std::string& id,
38  uint64_t start,
39  uint64_t end,
40  const std::string& settings,
41  const std::string& payload);
42  // Writes the current state of the current segment to disk. This will
43  // reset the internal state and set it up for the next segment.
44  Status WriteSegmentToFile(const std::string& filename);
45 
46  private:
48  WebVttOutputHandler& operator=(const WebVttOutputHandler&) = delete;
49 
50  Status InitializeInternal() override;
51  Status Process(std::unique_ptr<StreamData> stream_data) override;
52  Status OnFlushRequest(size_t input_stream_index) override;
53 
54  // A buffer of characters waiting to be written to a file.
55  std::string buffer_;
56 };
57 
58 // This WebVttt output handler should only be used when the source WebVTT
59 // content needs to be segmented across multiple files.
61  public:
62  WebVttSegmentedOutputHandler(const MuxerOptions& muxer_options,
63  std::unique_ptr<MuxerListener> muxer_listener);
64 
65  private:
66  Status OnStreamInfo(const StreamInfo& info) override;
67  Status OnSegmentInfo(const SegmentInfo& info) override;
68  Status OnTextSample(const TextSample& sample) override;
69  Status OnStreamEnd() override;
70 
71  Status OnSegmentEnded();
72 
73  void GoToNextSegment(uint64_t start_time_ms);
74 
75  const MuxerOptions muxer_options_;
76  std::unique_ptr<MuxerListener> muxer_listener_;
77 
78  // Sum together all segment durations so we know how long the stream is.
79  uint64_t total_duration_ms_ = 0;
80  uint32_t segment_index_ = 0;
81 };
82 
83 } // namespace media
84 } // namespace shaka
85 
86 #endif // PACKAGER_MEDIA_FORMATS_WEBVTT_WEBVTT_TEXT_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:58
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