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" 19 PackedAudioWriter::~PackedAudioWriter() =
default;
21 Status PackedAudioWriter::InitializeMuxer() {
22 if (streams().size() > 1u)
23 return Status(error::MUXER_FAILURE,
"Cannot handle more than one streams.");
25 RETURN_IF_ERROR(segmenter_->Initialize(*streams()[0]));
29 DCHECK(!file_name.empty());
30 output_file_.reset(
File::Open(file_name.c_str(),
"w"));
32 return Status(error::FILE_FAILURE,
33 "Cannot open file for write " + file_name);
37 if (muxer_listener()) {
38 muxer_listener()->
OnMediaStart(options(), *streams().front(),
39 kPackedAudioTimescale,
40 MuxerListener::kContainerPackedAudio);
45 Status PackedAudioWriter::Finalize() {
47 RETURN_IF_ERROR(CloseFile(std::move(output_file_)));
49 if (muxer_listener()) {
51 media_ranges_, total_duration_ * segmenter_->TimescaleScale());
56 Status PackedAudioWriter::AddSample(
size_t stream_id,
58 DCHECK_EQ(stream_id, 0u);
59 return segmenter_->AddSample(sample);
62 Status PackedAudioWriter::FinalizeSegment(
size_t stream_id,
64 DCHECK_EQ(stream_id, 0u);
66 if (segment_info.is_subsegment)
69 RETURN_IF_ERROR(segmenter_->FinalizeSegment());
71 const uint64_t segment_timestamp =
72 segment_info.start_timestamp * segmenter_->TimescaleScale();
73 std::string segment_path =
76 : GetSegmentName(options().segment_template, segment_timestamp,
77 segment_number_++, options().bandwidth);
80 const size_t segment_size = segmenter_->segment_buffer()->Size();
82 RETURN_IF_ERROR(WriteSegment(segment_path, segmenter_->segment_buffer()));
83 total_duration_ += segment_info.duration;
85 if (muxer_listener()) {
87 segment_path, segment_timestamp,
88 segment_info.duration * segmenter_->TimescaleScale(), segment_size);
93 Status PackedAudioWriter::WriteSegment(
const std::string& segment_path,
95 std::unique_ptr<File, FileCloser> file;
102 range.end = range.start + segment_buffer->Size() - 1;
105 file.reset(
File::Open(segment_path.c_str(),
"w"));
107 return Status(error::FILE_FAILURE,
108 "Cannot open file for write " + segment_path);
112 RETURN_IF_ERROR(segment_buffer->
WriteToFile(output_file_ ? output_file_.get()
116 RETURN_IF_ERROR(CloseFile(std::move(file)));
120 Status PackedAudioWriter::CloseFile(std::unique_ptr<File, FileCloser> file) {
121 std::string file_name = file->file_name();
122 if (!file.release()->Close()) {
125 "Cannot close file " + file_name +
126 ", 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.