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