Shaka Packager SDK
packed_audio_segmenter.cc
1 // Copyright 2018 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/packed_audio/packed_audio_segmenter.h"
8 
9 #include <memory>
10 
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"
16 
17 namespace shaka {
18 namespace media {
19 namespace {
20 std::string TimestampToString(uint64_t timestamp) {
21  BufferWriter buffer;
22  buffer.AppendInt(timestamp);
23  return std::string(buffer.Buffer(), buffer.Buffer() + buffer.Size());
24 }
25 } // namespace
26 
28  uint32_t transport_stream_timestamp_offset)
29  : transport_stream_timestamp_offset_(transport_stream_timestamp_offset) {}
30 
31 PackedAudioSegmenter::~PackedAudioSegmenter() = default;
32 
34  const StreamType stream_type = stream_info.stream_type();
35  if (stream_type != StreamType::kStreamAudio) {
36  LOG(ERROR) << "PackedAudioSegmenter cannot handle stream type "
37  << stream_type;
38  return Status(error::MUXER_FAILURE, "Unsupported stream type.");
39  }
40 
41  codec_ = stream_info.codec();
42  audio_codec_config_ = stream_info.codec_config();
43  timescale_scale_ = kPackedAudioTimescale / stream_info.time_scale();
44 
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.");
49  }
50  }
51 
52  return Status::OK;
53 }
54 
56  if (sample.is_encrypted() && audio_setup_information_.empty())
57  RETURN_IF_ERROR(EncryptionAudioSetup(sample));
58 
59  if (start_of_new_segment_) {
60  RETURN_IF_ERROR(StartNewSegment(sample));
61  start_of_new_segment_ = false;
62  }
63 
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());
70  } else {
71  segment_buffer_.AppendArray(sample.data(), sample.data_size());
72  }
73  return Status::OK;
74 }
75 
77  start_of_new_segment_ = true;
78  return Status::OK;
79 }
80 
82  return timescale_scale_;
83 }
84 
85 std::unique_ptr<AACAudioSpecificConfig>
86 PackedAudioSegmenter::CreateAdtsConverter() {
87  return std::unique_ptr<AACAudioSpecificConfig>(new AACAudioSpecificConfig);
88 }
89 
90 std::unique_ptr<Id3Tag> PackedAudioSegmenter::CreateId3Tag() {
91  return std::unique_ptr<Id3Tag>(new Id3Tag);
92 }
93 
94 Status PackedAudioSegmenter::EncryptionAudioSetup(const MediaSample& sample) {
95  // For codecs other than AC3, audio setup data is the audio codec
96  // configuration data.
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) {
100  // https://goo.gl/N7Tvqi MPEG-2 Stream Encryption Format for HTTP Live
101  // Streaming 2.3.2.2 AC-3 Setup: For AC-3, the setup_data in the
102  // audio_setup_information is the first 10 bytes of the audio data (the
103  // syncframe()).
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.");
108  }
109  audio_setup_data = sample.data();
110  audio_setup_data_size = kSetupDataSize;
111  }
112 
113  BufferWriter buffer;
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.");
118  }
119  audio_setup_information_.assign(buffer.Buffer(),
120  buffer.Buffer() + buffer.Size());
121  return Status::OK;
122 }
123 
124 Status PackedAudioSegmenter::StartNewSegment(const MediaSample& sample) {
125  segment_buffer_.Clear();
126 
127  const int64_t pts =
128  sample.pts() * timescale_scale_ + transport_stream_timestamp_offset_;
129  if (pts < 0) {
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.");
136  }
137 
138  // Use a unique_ptr so it can be mocked for testing.
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_);
144  }
145  CHECK(id3_tag->WriteToBuffer(&segment_buffer_));
146 
147  return Status::OK;
148 }
149 
150 } // namespace media
151 } // namespace shaka
Abstract class holds stream information.
Definition: stream_info.h:61
const uint8_t * Buffer() const
Definition: buffer_writer.h:61
All the methods that are virtual are virtual for mocking.
virtual Status Initialize(const StreamInfo &stream_info)
Class to hold a media sample.
Definition: media_sample.h:22
PackedAudioSegmenter(uint32_t transport_stream_timestamp_offset)
virtual Status AddSample(const MediaSample &sample)