Shaka Packager SDK
audio_stream_info.cc
1 // Copyright 2014 Google Inc. 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/base/audio_stream_info.h"
8 
9 #include <inttypes.h>
10 
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/string_number_conversions.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/media/base/limits.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 namespace {
20 std::string AudioCodecToString(Codec codec) {
21  switch (codec) {
22  case kCodecAAC:
23  return "AAC";
24  case kCodecAC3:
25  return "AC3";
26  case kCodecDTSC:
27  return "DTSC";
28  case kCodecDTSE:
29  return "DTSE";
30  case kCodecDTSH:
31  return "DTSH";
32  case kCodecDTSL:
33  return "DTSL";
34  case kCodecDTSM:
35  return "DTS-";
36  case kCodecDTSP:
37  return "DTS+";
38  case kCodecEAC3:
39  return "EAC3";
40  case kCodecAC4:
41  return "AC4";
42  case kCodecFlac:
43  return "FLAC";
44  case kCodecOpus:
45  return "Opus";
46  case kCodecVorbis:
47  return "Vorbis";
48  case kCodecMP3:
49  return "MP3";
50  default:
51  NOTIMPLEMENTED() << "Unknown Audio Codec: " << codec;
52  return "UnknownCodec";
53  }
54 }
55 } // namespace
56 
58  int track_id, uint32_t time_scale, uint64_t duration, Codec codec,
59  const std::string& codec_string, const uint8_t* codec_config,
60  size_t codec_config_size, uint8_t sample_bits, uint8_t num_channels,
61  uint32_t sampling_frequency, uint64_t seek_preroll_ns,
62  uint64_t codec_delay_ns, uint32_t max_bitrate, uint32_t avg_bitrate,
63  const std::string& language, bool is_encrypted)
64  : StreamInfo(kStreamAudio, track_id, time_scale, duration, codec,
65  codec_string, codec_config, codec_config_size, language,
66  is_encrypted),
67  sample_bits_(sample_bits),
68  num_channels_(num_channels),
69  sampling_frequency_(sampling_frequency),
70  seek_preroll_ns_(seek_preroll_ns),
71  codec_delay_ns_(codec_delay_ns),
72  max_bitrate_(max_bitrate),
73  avg_bitrate_(avg_bitrate) {}
74 
75 AudioStreamInfo::~AudioStreamInfo() {}
76 
78  return codec() != kUnknownCodec && num_channels_ != 0 &&
79  num_channels_ <= limits::kMaxChannels && sample_bits_ > 0 &&
80  sample_bits_ <= limits::kMaxBitsPerSample && sampling_frequency_ > 0 &&
81  sampling_frequency_ <= limits::kMaxSampleRate;
82 }
83 
84 std::string AudioStreamInfo::ToString() const {
85  std::string str = base::StringPrintf(
86  "%s codec: %s\n sample_bits: %d\n num_channels: %d\n "
87  "sampling_frequency: %d\n language: %s\n",
88  StreamInfo::ToString().c_str(), AudioCodecToString(codec()).c_str(),
89  sample_bits_, num_channels_, sampling_frequency_, language().c_str());
90  if (seek_preroll_ns_ != 0) {
91  base::StringAppendF(&str, " seek_preroll_ns: %" PRIu64 "\n",
92  seek_preroll_ns_);
93  }
94  if (codec_delay_ns_ != 0) {
95  base::StringAppendF(&str, " codec_delay_ns: %" PRIu64 "\n",
96  codec_delay_ns_);
97  }
98  return str;
99 }
100 
101 std::unique_ptr<StreamInfo> AudioStreamInfo::Clone() const {
102  return std::unique_ptr<StreamInfo>(new AudioStreamInfo(*this));
103 }
104 
105 std::string AudioStreamInfo::GetCodecString(Codec codec,
106  uint8_t audio_object_type) {
107  switch (codec) {
108  case kCodecAAC:
109  return "mp4a.40." + base::UintToString(audio_object_type);
110  case kCodecAC3:
111  return "ac-3";
112  case kCodecDTSC:
113  return "dtsc";
114  case kCodecDTSE:
115  return "dtse";
116  case kCodecDTSH:
117  return "dtsh";
118  case kCodecDTSL:
119  return "dtsl";
120  case kCodecDTSM:
121  return "dts-";
122  case kCodecDTSP:
123  return "dts+";
124  case kCodecEAC3:
125  return "ec-3";
126  case kCodecAC4:
127  // ETSI TS 103 190-2 Digital Audio Compression (AC-4) Standard; Part 2:
128  // Immersive and personalized audio E.13. audio_object_type is composed of
129  // bitstream_version (3bits), presentation_version (2bits) and
130  // mdcompat (3bits).
131  return base::StringPrintf(
132  "ac-4.%02d.%02d.%02d", (audio_object_type & 0xE0) >> 5,
133  (audio_object_type & 0x18) >> 3, audio_object_type & 0x7);
134  case kCodecFlac:
135  return "flac";
136  case kCodecOpus:
137  return "opus";
138  case kCodecMP3:
139  return "mp3";
140  case kCodecVorbis:
141  return "vorbis";
142  default:
143  NOTIMPLEMENTED() << "Codec: " << codec;
144  return "unknown";
145  }
146 }
147 
148 } // namespace media
149 } // namespace shaka
AudioStreamInfo(int track_id, uint32_t time_scale, uint64_t duration, Codec codec, const std::string &codec_string, const uint8_t *codec_config, size_t codec_config_size, uint8_t sample_bits, uint8_t num_channels, uint32_t sampling_frequency, uint64_t seek_preroll_ns, uint64_t codec_delay_ns, uint32_t max_bitrate, uint32_t avg_bitrate, const std::string &language, bool is_encrypted)
Construct an initialized audio stream info object.
std::unique_ptr< StreamInfo > Clone() const override
bool IsValidConfig() const override
std::string ToString() const override
static std::string GetCodecString(Codec codec, uint8_t audio_object_type)
Abstract class holds stream information.
Definition: stream_info.h:65
virtual std::string ToString() const
Definition: stream_info.cc:59
All the methods that are virtual are virtual for mocking.