7 #include "packager/media/formats/packed_audio/packed_audio_segmenter.h" 11 #include "packager/media/base/id3_tag.h" 12 #include "packager/media/base/media_sample.h" 13 #include "packager/media/codecs/aac_audio_specific_config.h" 14 #include "packager/media/codecs/hls_audio_util.h" 15 #include "packager/status_macros.h" 20 std::string TimestampToString(uint64_t timestamp) {
22 buffer.AppendInt(timestamp);
23 return std::string(buffer.Buffer(), buffer.Buffer() + buffer.Size());
28 uint32_t transport_stream_timestamp_offset)
29 : transport_stream_timestamp_offset_(transport_stream_timestamp_offset) {}
31 PackedAudioSegmenter::~PackedAudioSegmenter() =
default;
34 const StreamType stream_type = stream_info.stream_type();
35 if (stream_type != StreamType::kStreamAudio) {
36 LOG(ERROR) <<
"PackedAudioSegmenter cannot handle stream type " 38 return Status(error::MUXER_FAILURE,
"Unsupported stream type.");
41 codec_ = stream_info.codec();
42 audio_codec_config_ = stream_info.codec_config();
43 timescale_scale_ = kPackedAudioTimescale / stream_info.time_scale();
45 if (codec_ == kCodecAAC) {
46 adts_converter_ = CreateAdtsConverter();
47 if (!adts_converter_->Parse(audio_codec_config_)) {
48 return Status(error::MUXER_FAILURE,
"Invalid audio codec configuration.");
56 if (sample.is_encrypted() && audio_setup_information_.empty())
57 RETURN_IF_ERROR(EncryptionAudioSetup(sample));
59 if (start_of_new_segment_) {
60 RETURN_IF_ERROR(StartNewSegment(sample));
61 start_of_new_segment_ =
false;
64 if (adts_converter_) {
65 std::vector<uint8_t> audio_frame(sample.data(),
66 sample.data() + sample.data_size());
67 if (!adts_converter_->ConvertToADTS(&audio_frame))
68 return Status(error::MUXER_FAILURE,
"Failed to convert to ADTS.");
69 segment_buffer_.AppendArray(audio_frame.data(), audio_frame.size());
71 segment_buffer_.AppendArray(sample.data(), sample.data_size());
77 start_of_new_segment_ =
true;
82 return timescale_scale_;
85 std::unique_ptr<AACAudioSpecificConfig>
86 PackedAudioSegmenter::CreateAdtsConverter() {
90 std::unique_ptr<Id3Tag> PackedAudioSegmenter::CreateId3Tag() {
91 return std::unique_ptr<Id3Tag>(
new Id3Tag);
97 const uint8_t* audio_setup_data = audio_codec_config_.data();
98 size_t audio_setup_data_size = audio_codec_config_.size();
99 if (codec_ == kCodecAC3) {
104 const size_t kSetupDataSize = 10u;
105 if (sample.data_size() < kSetupDataSize) {
106 LOG(ERROR) <<
"Sample is too small for AC3: " << sample.data_size();
107 return Status(error::MUXER_FAILURE,
"Sample is too small for AC3.");
109 audio_setup_data = sample.data();
110 audio_setup_data_size = kSetupDataSize;
114 if (!WriteAudioSetupInformation(codec_, audio_setup_data,
115 audio_setup_data_size, &buffer)) {
116 return Status(error::MUXER_FAILURE,
117 "Failed to write audio setup information.");
119 audio_setup_information_.assign(buffer.
Buffer(),
120 buffer.
Buffer() + buffer.Size());
125 segment_buffer_.Clear();
128 sample.pts() * timescale_scale_ + transport_stream_timestamp_offset_;
130 LOG(ERROR) <<
"Seeing negative timestamp " << pts
131 <<
" after applying offset " 132 << transport_stream_timestamp_offset_
133 <<
". Please check if it is expected. Adjust " 134 "--transport_stream_timestamp_offset_ms if needed.";
135 return Status(error::MUXER_FAILURE,
"Unsupported negative timestamp.");
139 std::unique_ptr<Id3Tag> id3_tag = CreateId3Tag();
140 id3_tag->AddPrivateFrame(kTimestampOwnerIdentifier, TimestampToString(pts));
141 if (!audio_setup_information_.empty()) {
142 id3_tag->AddPrivateFrame(kAudioDescriptionOwnerIdentifier,
143 audio_setup_information_);
145 CHECK(id3_tag->WriteToBuffer(&segment_buffer_));
All the methods that are virtual are virtual for mocking.