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 kCodecFlac:
41  return "FLAC";
42  case kCodecOpus:
43  return "Opus";
44  case kCodecVorbis:
45  return "Vorbis";
46  default:
47  NOTIMPLEMENTED() << "Unknown Audio Codec: " << codec;
48  return "UnknownCodec";
49  }
50 }
51 } // namespace
52 
54  int track_id, uint32_t time_scale, uint64_t duration, Codec codec,
55  const std::string& codec_string, const uint8_t* codec_config,
56  size_t codec_config_size, uint8_t sample_bits, uint8_t num_channels,
57  uint32_t sampling_frequency, uint64_t seek_preroll_ns,
58  uint64_t codec_delay_ns, uint32_t max_bitrate, uint32_t avg_bitrate,
59  const std::string& language, bool is_encrypted)
60  : StreamInfo(kStreamAudio, track_id, time_scale, duration, codec,
61  codec_string, codec_config, codec_config_size, language,
62  is_encrypted),
63  sample_bits_(sample_bits),
64  num_channels_(num_channels),
65  sampling_frequency_(sampling_frequency),
66  seek_preroll_ns_(seek_preroll_ns),
67  codec_delay_ns_(codec_delay_ns),
68  max_bitrate_(max_bitrate),
69  avg_bitrate_(avg_bitrate) {}
70 
71 AudioStreamInfo::~AudioStreamInfo() {}
72 
74  return codec() != kUnknownCodec && num_channels_ != 0 &&
75  num_channels_ <= limits::kMaxChannels && sample_bits_ > 0 &&
76  sample_bits_ <= limits::kMaxBitsPerSample && sampling_frequency_ > 0 &&
77  sampling_frequency_ <= limits::kMaxSampleRate;
78 }
79 
80 std::string AudioStreamInfo::ToString() const {
81  std::string str = base::StringPrintf(
82  "%s codec: %s\n sample_bits: %d\n num_channels: %d\n "
83  "sampling_frequency: %d\n language: %s\n",
84  StreamInfo::ToString().c_str(), AudioCodecToString(codec()).c_str(),
85  sample_bits_, num_channels_, sampling_frequency_, language().c_str());
86  if (seek_preroll_ns_ != 0) {
87  base::StringAppendF(&str, " seek_preroll_ns: %" PRIu64 "\n",
88  seek_preroll_ns_);
89  }
90  if (codec_delay_ns_ != 0) {
91  base::StringAppendF(&str, " codec_delay_ns: %" PRIu64 "\n",
92  codec_delay_ns_);
93  }
94  return str;
95 }
96 
97 std::unique_ptr<StreamInfo> AudioStreamInfo::Clone() const {
98  return std::unique_ptr<StreamInfo>(new AudioStreamInfo(*this));
99 }
100 
101 std::string AudioStreamInfo::GetCodecString(Codec codec,
102  uint8_t audio_object_type) {
103  switch (codec) {
104  case kCodecAAC:
105  return "mp4a.40." + base::UintToString(audio_object_type);
106  case kCodecAC3:
107  return "ac-3";
108  case kCodecDTSC:
109  return "dtsc";
110  case kCodecDTSE:
111  return "dtse";
112  case kCodecDTSH:
113  return "dtsh";
114  case kCodecDTSL:
115  return "dtsl";
116  case kCodecDTSM:
117  return "dts-";
118  case kCodecDTSP:
119  return "dts+";
120  case kCodecEAC3:
121  return "ec-3";
122  case kCodecFlac:
123  return "flac";
124  case kCodecOpus:
125  return "opus";
126  case kCodecVorbis:
127  return "vorbis";
128  default:
129  NOTIMPLEMENTED() << "Codec: " << codec;
130  return "unknown";
131  }
132 }
133 
134 } // namespace media
135 } // namespace shaka
Abstract class holds stream information.
Definition: stream_info.h:61
virtual std::string ToString() const
Definition: stream_info.cc:58
std::string ToString() const override
All the methods that are virtual are virtual for mocking.
bool IsValidConfig() const override
static std::string GetCodecString(Codec codec, uint8_t audio_object_type)
std::unique_ptr< StreamInfo > Clone() const override
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.