Shaka Packager SDK
muxer.h
1 // Copyright 2014 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 // 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/base/time/clock.h"
16 #include "packager/media/base/media_handler.h"
17 #include "packager/media/base/muxer_options.h"
18 #include "packager/media/event/muxer_listener.h"
19 #include "packager/media/event/progress_listener.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(base::Clock* clock) {
58  clock_ = clock;
59  }
60 
61  protected:
64  Status InitializeInternal() override { return Status::OK; }
65  Status Process(std::unique_ptr<StreamData> stream_data) override;
66  Status OnFlushRequest(size_t input_stream_index) override;
68 
69  const MuxerOptions& options() const { return options_; }
70  MuxerListener* muxer_listener() { return muxer_listener_.get(); }
71  ProgressListener* progress_listener() { return progress_listener_.get(); }
72  base::Clock* clock() { return clock_; }
73 
74  private:
75  Muxer(const Muxer&) = delete;
76  Muxer& operator=(const Muxer&) = delete;
77 
78  // Initialize the muxer. InitializeMuxer may be called multiple times with
79  // |options()| updated between calls, which is used to support separate file
80  // per Representation per Period for Ad Insertion.
81  virtual Status InitializeMuxer() = 0;
82 
83  // Final clean up.
84  virtual Status Finalize() = 0;
85 
86  // Add a new media sample. This does nothing by default; so subclasses that
87  // handle media samples will need to replace this.
88  virtual Status AddMediaSample(size_t stream_id, const MediaSample& sample);
89 
90  // Add a new text sample. This does nothing by default; so subclasses that
91  // handle text samples will need to replace this.
92  virtual Status AddTextSample(size_t stream_id, const TextSample& sample);
93 
94  // Finalize the segment or subsegment.
95  virtual Status FinalizeSegment(
96  size_t stream_id,
97  const SegmentInfo& segment_info) = 0;
98 
99  // Re-initialize Muxer. Could be called on StreamInfo or CueEvent.
100  // |timestamp| may be used to set the output file name.
101  Status ReinitializeMuxer(int64_t timestamp);
102 
103  MuxerOptions options_;
104  std::vector<std::shared_ptr<const StreamInfo>> streams_;
105  std::vector<uint8_t> current_key_id_;
106  bool encryption_started_ = false;
107  bool cancelled_ = false;
108 
109  std::unique_ptr<MuxerListener> muxer_listener_;
110  std::unique_ptr<ProgressListener> progress_listener_;
111  // An external injected clock, can be NULL.
112  base::Clock* clock_ = nullptr;
113 
114  // In VOD single segment case with Ad Cues, |output_file_name| is allowed to
115  // be a template. In this case, there will be NumAdCues + 1 files generated.
116  std::string output_file_template_;
117  size_t output_file_index_ = 0;
118 };
119 
120 } // namespace media
121 } // namespace shaka
122 
123 #endif // PACKAGER_MEDIA_BASE_MUXER_H_
void SetMuxerListener(std::unique_ptr< MuxerListener > muxer_listener)
Definition: muxer.cc:35
void SetProgressListener(std::unique_ptr< ProgressListener > progress_listener)
Definition: muxer.cc:39
Status InitializeInternal() override
Definition: muxer.h:64
void set_clock(base::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:102
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: muxer.cc:44
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