7 #include "packager/media/formats/packed_audio/packed_audio_writer.h" 9 #include "packager/media/base/muxer_util.h" 10 #include "packager/media/formats/packed_audio/packed_audio_segmenter.h" 11 #include "packager/status_macros.h" 17 :
Muxer(muxer_options),
18 transport_stream_timestamp_offset_(
19 muxer_options.transport_stream_timestamp_offset_ms *
20 kPackedAudioTimescale / 1000),
24 PackedAudioWriter::~PackedAudioWriter() =
default;
26 Status PackedAudioWriter::InitializeMuxer() {
27 if (streams().size() > 1u)
28 return Status(error::MUXER_FAILURE,
"Cannot handle more than one streams.");
30 RETURN_IF_ERROR(segmenter_->Initialize(*streams()[0]));
34 DCHECK(!file_name.empty());
35 output_file_.reset(
File::Open(file_name.c_str(),
"w"));
37 return Status(error::FILE_FAILURE,
38 "Cannot open file for write " + file_name);
42 if (muxer_listener()) {
43 muxer_listener()->
OnMediaStart(options(), *streams().front(),
44 kPackedAudioTimescale,
45 MuxerListener::kContainerPackedAudio);
50 Status PackedAudioWriter::Finalize() {
52 RETURN_IF_ERROR(CloseFile(std::move(output_file_)));
54 if (muxer_listener()) {
56 media_ranges_, total_duration_ * segmenter_->TimescaleScale());
61 Status PackedAudioWriter::AddSample(
size_t stream_id,
63 DCHECK_EQ(stream_id, 0u);
64 return segmenter_->AddSample(sample);
67 Status PackedAudioWriter::FinalizeSegment(
size_t stream_id,
69 DCHECK_EQ(stream_id, 0u);
71 if (segment_info.is_subsegment)
74 RETURN_IF_ERROR(segmenter_->FinalizeSegment());
76 const uint64_t segment_timestamp =
77 segment_info.start_timestamp * segmenter_->TimescaleScale();
78 std::string segment_path =
81 : GetSegmentName(options().segment_template, segment_timestamp,
82 segment_number_++, options().bandwidth);
85 const size_t segment_size = segmenter_->segment_buffer()->Size();
87 RETURN_IF_ERROR(WriteSegment(segment_path, segmenter_->segment_buffer()));
88 total_duration_ += segment_info.duration;
90 if (muxer_listener()) {
92 segment_path, segment_timestamp + transport_stream_timestamp_offset_,
93 segment_info.duration * segmenter_->TimescaleScale(), segment_size);
98 Status PackedAudioWriter::WriteSegment(
const std::string& segment_path,
100 std::unique_ptr<File, FileCloser> file;
107 range.end = range.start + segment_buffer->Size() - 1;
110 file.reset(
File::Open(segment_path.c_str(),
"w"));
112 return Status(error::FILE_FAILURE,
113 "Cannot open file for write " + segment_path);
117 RETURN_IF_ERROR(segment_buffer->
WriteToFile(output_file_ ? output_file_.get()
121 RETURN_IF_ERROR(CloseFile(std::move(file)));
125 Status PackedAudioWriter::CloseFile(std::unique_ptr<File, FileCloser> file) {
126 std::string file_name = file->file_name();
127 if (!file.release()->Close()) {
130 "Cannot close file " + file_name +
131 ", possibly file permission issue or running out of disk space.");
All the methods that are virtual are virtual for mocking.
virtual bool Open()=0
Internal open. Should not be used directly.