Shaka Packager SDK
muxer.h
1 // Copyright 2014 Google LLC. 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 // Defines the muxer interface.
8 
9 #ifndef PACKAGER_MEDIA_BASE_MUXER_H_
10 #define PACKAGER_MEDIA_BASE_MUXER_H_
11 
12 #include <memory>
13 #include <vector>
14 
15 #include <packager/media/base/media_handler.h>
16 #include <packager/media/base/muxer_options.h>
17 #include <packager/media/event/muxer_listener.h>
18 #include <packager/media/event/progress_listener.h>
19 #include <packager/mpd/base/mpd_builder.h>
20 #include <packager/status.h>
21 
22 namespace shaka {
23 namespace media {
24 
25 class MediaSample;
26 
30 class Muxer : public MediaHandler {
31  public:
32  explicit Muxer(const MuxerOptions& options);
33  virtual ~Muxer();
34 
37  void Cancel();
38 
41  void SetMuxerListener(std::unique_ptr<MuxerListener> muxer_listener);
42 
45  void SetProgressListener(std::unique_ptr<ProgressListener> progress_listener);
46 
47  const std::vector<std::shared_ptr<const StreamInfo>>& streams() const {
48  return streams_;
49  }
50 
57  void set_clock(std::shared_ptr<Clock> clock) { clock_ = clock; }
58 
59  protected:
62  Status InitializeInternal() override { return Status::OK; }
63  Status Process(std::unique_ptr<StreamData> stream_data) override;
64  Status OnFlushRequest(size_t input_stream_index) override;
66 
67  const MuxerOptions& options() const { return options_; }
68  MuxerListener* muxer_listener() { return muxer_listener_.get(); }
69  ProgressListener* progress_listener() { return progress_listener_.get(); }
70 
71  uint64_t Now() const {
72  auto duration = clock_->now().time_since_epoch();
73  auto seconds =
74  std::chrono::duration_cast<std::chrono::seconds>(duration).count();
75  return static_cast<uint64_t>(seconds);
76  }
77 
78  private:
79  Muxer(const Muxer&) = delete;
80  Muxer& operator=(const Muxer&) = delete;
81 
82  // Initialize the muxer. InitializeMuxer may be called multiple times with
83  // |options()| updated between calls, which is used to support separate file
84  // per Representation per Period for Ad Insertion.
85  virtual Status InitializeMuxer() = 0;
86 
87  // Final clean up.
88  virtual Status Finalize() = 0;
89 
90  // Add a new media sample. This does nothing by default; so subclasses that
91  // handle media samples will need to replace this.
92  virtual Status AddMediaSample(size_t stream_id, const MediaSample& sample);
93 
94  // Add a new text sample. This does nothing by default; so subclasses that
95  // handle text samples will need to replace this.
96  virtual Status AddTextSample(size_t stream_id, const TextSample& sample);
97 
98  // Finalize the segment or subsegment.
99  virtual Status FinalizeSegment(
100  size_t stream_id,
101  const SegmentInfo& segment_info) = 0;
102 
103  // Re-initialize Muxer. Could be called on StreamInfo or CueEvent.
104  // |timestamp| may be used to set the output file name.
105  Status ReinitializeMuxer(int64_t timestamp);
106 
107  MuxerOptions options_;
108  std::vector<std::shared_ptr<const StreamInfo>> streams_;
109  std::vector<uint8_t> current_key_id_;
110  bool encryption_started_ = false;
111  bool cancelled_ = false;
112 
113  std::unique_ptr<MuxerListener> muxer_listener_;
114  std::unique_ptr<ProgressListener> progress_listener_;
115  std::shared_ptr<Clock> clock_;
116 
117  // In VOD single segment case with Ad Cues, |output_file_name| is allowed to
118  // be a template. In this case, there will be NumAdCues + 1 files generated.
119  std::string output_file_template_;
120  size_t output_file_index_ = 1;
121 };
122 
123 } // namespace media
124 } // namespace shaka
125 
126 #endif // PACKAGER_MEDIA_BASE_MUXER_H_
void SetMuxerListener(std::unique_ptr< MuxerListener > muxer_listener)
Definition: muxer.cc:39
void SetProgressListener(std::unique_ptr< ProgressListener > progress_listener)
Definition: muxer.cc:43
Status InitializeInternal() override
Definition: muxer.h:62
void set_clock(std::shared_ptr< Clock > clock)
Definition: muxer.h:57
Status OnFlushRequest(size_t input_stream_index) override
Event handler for flush request at the specific input stream index.
Definition: muxer.cc:106
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: muxer.cc:48
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:19