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) {
24 timestamp &= 0x1FFFFFFFFull;
27 buffer.AppendInt(timestamp);
28 return std::string(buffer.Buffer(), buffer.Buffer() + buffer.Size());
33 uint32_t transport_stream_timestamp_offset)
34 : transport_stream_timestamp_offset_(transport_stream_timestamp_offset) {}
36 PackedAudioSegmenter::~PackedAudioSegmenter() =
default;
39 const StreamType stream_type = stream_info.stream_type();
40 if (stream_type != StreamType::kStreamAudio) {
41 LOG(ERROR) <<
"PackedAudioSegmenter cannot handle stream type " 43 return Status(error::MUXER_FAILURE,
"Unsupported stream type.");
46 codec_ = stream_info.codec();
47 audio_codec_config_ = stream_info.codec_config();
48 timescale_scale_ = kPackedAudioTimescale / stream_info.time_scale();
50 if (codec_ == kCodecAAC) {
51 adts_converter_ = CreateAdtsConverter();
52 if (!adts_converter_->Parse(audio_codec_config_)) {
53 return Status(error::MUXER_FAILURE,
"Invalid audio codec configuration.");
61 if (sample.is_encrypted() && audio_setup_information_.empty())
62 RETURN_IF_ERROR(EncryptionAudioSetup(sample));
64 if (start_of_new_segment_) {
65 RETURN_IF_ERROR(StartNewSegment(sample));
66 start_of_new_segment_ =
false;
69 if (adts_converter_) {
70 std::vector<uint8_t> audio_frame;
71 if (!adts_converter_->ConvertToADTS(sample.data(), sample.data_size(),
73 return Status(error::MUXER_FAILURE,
"Failed to convert to ADTS.");
74 segment_buffer_.AppendArray(audio_frame.data(), audio_frame.size());
76 segment_buffer_.AppendArray(sample.data(), sample.data_size());
82 start_of_new_segment_ =
true;
87 return timescale_scale_;
90 std::unique_ptr<AACAudioSpecificConfig>
91 PackedAudioSegmenter::CreateAdtsConverter() {
95 std::unique_ptr<Id3Tag> PackedAudioSegmenter::CreateId3Tag() {
96 return std::unique_ptr<Id3Tag>(
new Id3Tag);
102 const uint8_t* audio_setup_data = audio_codec_config_.data();
103 size_t audio_setup_data_size = audio_codec_config_.size();
104 if (codec_ == kCodecAC3) {
109 const size_t kSetupDataSize = 10u;
110 if (sample.data_size() < kSetupDataSize) {
111 LOG(ERROR) <<
"Sample is too small for AC3: " << sample.data_size();
112 return Status(error::MUXER_FAILURE,
"Sample is too small for AC3.");
114 audio_setup_data = sample.data();
115 audio_setup_data_size = kSetupDataSize;
119 if (!WriteAudioSetupInformation(codec_, audio_setup_data,
120 audio_setup_data_size, &buffer)) {
121 return Status(error::MUXER_FAILURE,
122 "Failed to write audio setup information.");
124 audio_setup_information_.assign(buffer.
Buffer(),
125 buffer.
Buffer() + buffer.Size());
130 segment_buffer_.Clear();
133 sample.pts() * timescale_scale_ + transport_stream_timestamp_offset_;
135 LOG(ERROR) <<
"Seeing negative timestamp " << pts
136 <<
" after applying offset " 137 << transport_stream_timestamp_offset_
138 <<
". Please check if it is expected. Adjust " 139 "--transport_stream_timestamp_offset_ms if needed.";
140 return Status(error::MUXER_FAILURE,
"Unsupported negative timestamp.");
144 std::unique_ptr<Id3Tag> id3_tag = CreateId3Tag();
145 id3_tag->AddPrivateFrame(kTimestampOwnerIdentifier, TimestampToString(pts));
146 if (!audio_setup_information_.empty()) {
147 id3_tag->AddPrivateFrame(kAudioDescriptionOwnerIdentifier,
148 audio_setup_information_);
150 CHECK(id3_tag->WriteToBuffer(&segment_buffer_));
All the methods that are virtual are virtual for mocking.