7 #include <packager/media/formats/mp4/low_latency_segment_segmenter.h>
11 #include <absl/log/check.h>
13 #include <packager/file.h>
14 #include <packager/file/file_closer.h>
15 #include <packager/macros/logging.h>
16 #include <packager/macros/status.h>
17 #include <packager/media/base/buffer_writer.h>
18 #include <packager/media/base/media_handler.h>
19 #include <packager/media/base/muxer_options.h>
20 #include <packager/media/base/muxer_util.h>
21 #include <packager/media/event/muxer_listener.h>
22 #include <packager/media/formats/mp4/box_definitions.h>
23 #include <packager/media/formats/mp4/fragmenter.h>
24 #include <packager/media/formats/mp4/key_frame_info.h>
30 LowLatencySegmentSegmenter::LowLatencySegmentSegmenter(
31 const MuxerOptions& options,
32 std::unique_ptr<FileType> ftyp,
33 std::unique_ptr<Movie> moov)
34 : Segmenter(options, std::move(ftyp), std::move(moov)),
35 styp_(new SegmentType),
38 styp_->major_brand = Segmenter::ftyp()->major_brand;
39 styp_->compatible_brands = Segmenter::ftyp()->compatible_brands;
41 std::replace(styp_->compatible_brands.begin(), styp_->compatible_brands.end(),
42 FOURCC_cmfc, FOURCC_cmfs);
45 LowLatencySegmentSegmenter::~LowLatencySegmentSegmenter() {}
47 bool LowLatencySegmentSegmenter::GetInitRange(
size_t* offset,
size_t* size) {
48 VLOG(1) <<
"LowLatencySegmentSegmenter outputs init segment: "
49 << options().output_file_name;
53 bool LowLatencySegmentSegmenter::GetIndexRange(
size_t* offset,
size_t* size) {
54 VLOG(1) <<
"LowLatencySegmentSegmenter does not have index range.";
58 std::vector<Range> LowLatencySegmentSegmenter::GetSegmentRanges() {
59 VLOG(1) <<
"LowLatencySegmentSegmenter does not have media segment ranges.";
60 return std::vector<Range>();
63 Status LowLatencySegmentSegmenter::DoInitialize() {
64 return WriteInitSegment();
67 Status LowLatencySegmentSegmenter::DoFinalize() {
69 RETURN_IF_ERROR(WriteInitSegment());
74 Status LowLatencySegmentSegmenter::DoFinalizeSegment(int64_t segment_number) {
75 return FinalizeSegment();
78 Status LowLatencySegmentSegmenter::DoFinalizeChunk(int64_t segment_number) {
79 if (is_initial_chunk_in_seg_) {
80 return WriteInitialChunk(segment_number);
85 Status LowLatencySegmentSegmenter::WriteInitSegment() {
89 std::unique_ptr<File, FileCloser> file(
90 File::Open(options().output_file_name.c_str(),
"w"));
92 return Status(error::FILE_FAILURE,
93 "Cannot open file for write " + options().output_file_name);
95 std::unique_ptr<BufferWriter> buffer(
new BufferWriter);
96 ftyp()->Write(buffer.get());
97 moov()->Write(buffer.get());
98 return buffer->WriteToFile(file.get());
101 Status LowLatencySegmentSegmenter::WriteInitialChunk(int64_t segment_number) {
103 DCHECK(fragment_buffer());
106 DCHECK(!sidx()->references.empty());
109 sidx()->earliest_presentation_time =
110 sidx()->references[0].earliest_presentation_time;
112 if (options().segment_template.empty()) {
114 file_name_ = options().output_file_name.c_str();
116 file_name_ = GetSegmentName(options().segment_template,
117 sidx()->earliest_presentation_time,
118 num_segments_, options().bandwidth);
122 segment_file_.reset(File::Open(file_name_.c_str(),
"a"));
123 if (!segment_file_) {
124 return Status(error::FILE_FAILURE,
125 "Cannot open segment file: " + file_name_);
128 std::unique_ptr<BufferWriter> buffer(
new BufferWriter());
131 styp_->Write(buffer.get());
133 const size_t segment_header_size = buffer->Size();
134 segment_size_ = segment_header_size + fragment_buffer()->Size();
135 DCHECK_NE(segment_size_, 0u);
137 RETURN_IF_ERROR(buffer->WriteToFile(segment_file_.get()));
138 if (muxer_listener()) {
139 for (
const KeyFrameInfo& key_frame_info : key_frame_infos()) {
140 muxer_listener()->OnKeyFrame(
141 key_frame_info.timestamp,
142 segment_header_size + key_frame_info.start_byte_offset,
143 key_frame_info.size);
148 RETURN_IF_ERROR(fragment_buffer()->WriteToFile(segment_file_.get()));
150 uint64_t segment_duration = GetSegmentDuration();
151 UpdateProgress(segment_duration);
153 if (muxer_listener()) {
154 if (!ll_dash_mpd_values_initialized_) {
157 muxer_listener()->OnSampleDurationReady(sample_duration());
158 muxer_listener()->OnAvailabilityOffsetReady();
159 muxer_listener()->OnSegmentDurationReady();
160 ll_dash_mpd_values_initialized_ =
true;
164 muxer_listener()->OnNewSegment(
165 file_name_, sidx()->earliest_presentation_time, segment_duration,
166 segment_size_, segment_number);
167 is_initial_chunk_in_seg_ =
false;
173 Status LowLatencySegmentSegmenter::WriteChunk() {
174 DCHECK(fragment_buffer());
177 RETURN_IF_ERROR(fragment_buffer()->WriteToFile(segment_file_.get()));
179 UpdateProgress(GetSegmentDuration());
184 Status LowLatencySegmentSegmenter::FinalizeSegment() {
185 if (muxer_listener()) {
186 muxer_listener()->OnCompletedSegment(GetSegmentDuration(), segment_size_);
189 if (!segment_file_.release()->Close()) {
192 "Cannot close file " + file_name_ +
193 ", possibly file permission issue or running out of disk space.");
198 is_initial_chunk_in_seg_ =
true;
205 uint64_t LowLatencySegmentSegmenter::GetSegmentDuration() {
208 uint64_t segment_duration = 0;
211 for (
size_t i = 0; i < sidx()->references.size(); ++i)
212 segment_duration += sidx()->references[i].subsegment_duration;
214 return segment_duration;
All the methods that are virtual are virtual for mocking.